(Standard Targets): Use @smallexample.
[make.git] / main.c
bloba03397f2d48500e8a65e7f38faea32fd254b0f51
1 /* Argument parsing and main program of GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 94, 1995 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "commands.h"
21 #include "dep.h"
22 #include "file.h"
23 #include "variable.h"
24 #include "job.h"
25 #include "getopt.h"
26 #include <assert.h>
29 extern void print_variable_data_base ();
30 extern void print_dir_data_base ();
31 extern void print_rule_data_base ();
32 extern void print_file_data_base ();
33 extern void print_vpath_data_base ();
35 #ifndef HAVE_UNISTD_H
36 extern int chdir ();
37 #endif
38 #ifndef STDC_HEADERS
39 #ifndef sun /* Sun has an incorrect decl in a header. */
40 extern void exit ();
41 #endif
42 extern double atof ();
43 #endif
44 extern char *mktemp ();
46 static void print_data_base (), print_version ();
47 static void decode_switches (), decode_env_switches ();
48 static void define_makeflags ();
49 static char *quote_as_word ();
51 /* The structure that describes an accepted command switch. */
53 struct command_switch
55 char c; /* The switch character. */
57 enum /* Type of the value. */
59 flag, /* Turn int flag on. */
60 flag_off, /* Turn int flag off. */
61 string, /* One string per switch. */
62 positive_int, /* A positive integer. */
63 floating, /* A floating-point number (double). */
64 ignore /* Ignored. */
65 } type;
67 char *value_ptr; /* Pointer to the value-holding variable. */
69 unsigned int env:1; /* Can come from MAKEFLAGS. */
70 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
71 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
73 char *noarg_value; /* Pointer to value used if no argument is given. */
74 char *default_value;/* Pointer to default value. */
76 char *long_name; /* Long option name. */
77 char *argdesc; /* Descriptive word for argument. */
78 char *description; /* Description for usage message. */
82 /* The structure used to hold the list of strings given
83 in command switches of a type that takes string arguments. */
85 struct stringlist
87 char **list; /* Nil-terminated list of strings. */
88 unsigned int idx; /* Index into above. */
89 unsigned int max; /* Number of pointers allocated. */
93 /* The recognized command switches. */
95 /* Nonzero means do not print commands to be executed (-s). */
97 int silent_flag;
99 /* Nonzero means just touch the files
100 that would appear to need remaking (-t) */
102 int touch_flag;
104 /* Nonzero means just print what commands would need to be executed,
105 don't actually execute them (-n). */
107 int just_print_flag;
109 /* Print debugging trace info (-d). */
111 int debug_flag = 0;
113 /* Environment variables override makefile definitions. */
115 int env_overrides = 0;
117 /* Nonzero means ignore status codes returned by commands
118 executed to remake files. Just treat them all as successful (-i). */
120 int ignore_errors_flag = 0;
122 /* Nonzero means don't remake anything, just print the data base
123 that results from reading the makefile (-p). */
125 int print_data_base_flag = 0;
127 /* Nonzero means don't remake anything; just return a nonzero status
128 if the specified targets are not up to date (-q). */
130 int question_flag = 0;
132 /* Nonzero means do not use any of the builtin rules (-r). */
134 int no_builtin_rules_flag = 0;
136 /* Nonzero means keep going even if remaking some file fails (-k). */
138 int keep_going_flag;
139 int default_keep_going_flag = 0;
141 /* Nonzero means print directory before starting and when done (-w). */
143 int print_directory_flag = 0;
145 /* Nonzero means ignore print_directory_flag and never print the directory.
146 This is necessary because print_directory_flag is set implicitly. */
148 int inhibit_print_directory_flag = 0;
150 /* Nonzero means print version information. */
152 int print_version_flag = 0;
154 /* List of makefiles given with -f switches. */
156 static struct stringlist *makefiles = 0;
159 /* Number of job slots (commands that can be run at once). */
161 unsigned int job_slots = 1;
162 unsigned int default_job_slots = 1;
164 /* Value of job_slots that means no limit. */
166 static unsigned int inf_jobs = 0;
168 /* Maximum load average at which multiple jobs will be run.
169 Negative values mean unlimited, while zero means limit to
170 zero load (which could be useful to start infinite jobs remotely
171 but one at a time locally). */
173 double max_load_average = -1.0;
174 double default_load_average = -1.0;
176 /* List of directories given with -C switches. */
178 static struct stringlist *directories = 0;
180 /* List of include directories given with -I switches. */
182 static struct stringlist *include_directories = 0;
184 /* List of files given with -o switches. */
186 static struct stringlist *old_files = 0;
188 /* List of files given with -W switches. */
190 static struct stringlist *new_files = 0;
192 /* If nonzero, we should just print usage and exit. */
194 static int print_usage_flag = 0;
196 /* If nonzero, we should print a warning message
197 for each reference to an undefined variable. */
199 int warn_undefined_variables_flag;
201 /* The table of command switches. */
203 static const struct command_switch switches[] =
205 { 'b', ignore, 0, 0, 0, 0, 0, 0,
206 0, 0,
207 "Ignored for compatibility" },
208 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
209 "directory", "DIRECTORY",
210 "Change to DIRECTORY before doing anything" },
211 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
212 "debug", 0,
213 "Print lots of debugging information" },
214 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
215 "environment-overrides", 0,
216 "Environment variables override makefiles" },
217 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
218 "file", "FILE",
219 "Read FILE as a makefile" },
220 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
221 "help", 0,
222 "Print this message and exit" },
223 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
224 "ignore-errors", 0,
225 "Ignore errors from commands" },
226 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
227 "include-dir", "DIRECTORY",
228 "Search DIRECTORY for included makefiles" },
229 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
230 (char *) &inf_jobs, (char *) &default_job_slots,
231 "jobs", "N",
232 "Allow N jobs at once; infinite jobs with no arg" },
233 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
234 0, (char *) &default_keep_going_flag,
235 "keep-going", 0,
236 "Keep going when some targets can't be made" },
237 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
238 (char *) &default_load_average, (char *) &default_load_average,
239 "load-average", "N",
240 "Don't start multiple jobs unless load is below N" },
241 { 'm', ignore, 0, 0, 0, 0, 0, 0,
242 0, 0,
243 "-b" },
244 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
245 "just-print", 0,
246 "Don't actually run any commands; just print them" },
247 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
248 "old-file", "FILE",
249 "Consider FILE to be very old and don't remake it" },
250 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
251 "print-data-base", 0,
252 "Print make's internal database" },
253 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
254 "question", 0,
255 "Run no commands; exit status says if up to date" },
256 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
257 "no-builtin-rules", 0,
258 "Disable the built-in implicit rules" },
259 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
260 "silent", 0,
261 "Don't echo commands" },
262 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
263 0, (char *) &default_keep_going_flag,
264 "no-keep-going", 0,
265 "Turns off -k" },
266 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
267 "touch", 0,
268 "Touch targets instead of remaking them" },
269 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
270 "version", 0,
271 "Print the version number of make and exit" },
272 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
273 "print-directory", 0,
274 "Print the current directory" },
275 { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
276 "no-print-directory", 0,
277 "Turn off -w, even if it was turned on implicitly" },
278 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
279 "what-if", "FILE",
280 "Consider FILE to be infinitely new" },
281 { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
282 "warn-undefined-variables", 0,
283 "Warn when an undefined variable is referenced" },
284 { '\0', }
287 /* Secondary long names for options. */
289 static struct option long_option_aliases[] =
291 { "quiet", no_argument, 0, 's' },
292 { "stop", no_argument, 0, 'S' },
293 { "new-file", required_argument, 0, 'W' },
294 { "assume-new", required_argument, 0, 'W' },
295 { "assume-old", required_argument, 0, 'o' },
296 { "max-load", optional_argument, 0, 'l' },
297 { "dry-run", no_argument, 0, 'n' },
298 { "recon", no_argument, 0, 'n' },
299 { "makefile", required_argument, 0, 'f' },
302 /* The usage message prints the descriptions of options starting in
303 this column. Make sure it leaves enough room for the longest
304 description to fit in less than 80 characters. */
306 #define DESCRIPTION_COLUMN 30
308 /* List of goal targets. */
310 static struct dep *goals, *lastgoal;
312 /* List of variables which were defined on the command line
313 (or, equivalently, in MAKEFLAGS). */
315 struct command_variable
317 struct command_variable *next;
318 struct variable *variable;
320 static struct command_variable *command_variables;
322 /* The name we were invoked with. */
324 char *program;
326 /* Our current directory before processing any -C options. */
328 char *directory_before_chdir;
330 /* Our current directory after processing all -C options. */
332 char *starting_directory;
334 /* Value of the MAKELEVEL variable at startup (or 0). */
336 unsigned int makelevel;
338 /* First file defined in the makefile whose name does not
339 start with `.'. This is the default to remake if the
340 command line does not specify. */
342 struct file *default_goal_file;
344 /* Pointer to structure for the file .DEFAULT
345 whose commands are used for any file that has none of its own.
346 This is zero if the makefiles do not define .DEFAULT. */
348 struct file *default_file;
350 /* Nonzero if we have seen the magic `.POSIX' target.
351 This turns on pedantic compliance with POSIX.2. */
353 int posix_pedantic;
355 /* Mask of signals that are being caught with fatal_error_signal. */
357 #ifdef POSIX
358 sigset_t fatal_signal_set;
359 #else
360 #ifdef HAVE_SIGSETMASK
361 int fatal_signal_mask;
362 #endif
363 #endif
365 static struct file *
366 enter_command_line_file (name)
367 char *name;
369 if (name[0] == '\0')
370 fatal ("empty string invalid as file name");
372 if (name[0] == '~')
374 char *expanded = tilde_expand (name);
375 if (expanded != 0)
376 name = expanded; /* Memory leak; I don't care. */
379 /* This is also done in parse_file_seq, so this is redundant
380 for names read from makefiles. It is here for names passed
381 on the command line. */
382 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
384 name += 2;
385 while (*name == '/')
386 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
387 ++name;
390 if (*name == '\0')
392 /* It was all slashes! Move back to the dot and truncate
393 it after the first slash, so it becomes just "./". */
395 --name;
396 while (name[0] != '.');
397 name[2] = '\0';
400 return enter_file (savestring (name, strlen (name)));
403 /* Toggle -d on receipt of SIGUSR1. */
405 static RETSIGTYPE
406 debug_signal_handler (sig)
407 int sig;
409 debug_flag = ! debug_flag;
413 main (argc, argv, envp)
414 int argc;
415 char **argv;
416 char **envp;
418 extern RETSIGTYPE fatal_error_signal (), child_handler ();
419 register struct file *f;
420 register unsigned int i;
421 char **p;
422 struct dep *read_makefiles;
423 PATH_VAR (current_directory);
425 default_goal_file = 0;
426 reading_filename = 0;
427 reading_lineno_ptr = 0;
429 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
430 signame_init ();
431 #endif
433 #ifdef POSIX
434 sigemptyset (&fatal_signal_set);
435 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
436 #else
437 #ifdef HAVE_SIGSETMASK
438 fatal_signal_mask = 0;
439 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
440 #else
441 #define ADD_SIG(sig)
442 #endif
443 #endif
445 #define FATAL_SIG(sig) \
446 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
447 (void) signal ((sig), SIG_IGN); \
448 else \
449 ADD_SIG (sig);
451 FATAL_SIG (SIGHUP);
452 FATAL_SIG (SIGQUIT);
453 FATAL_SIG (SIGINT);
454 FATAL_SIG (SIGTERM);
456 #ifdef SIGDANGER
457 FATAL_SIG (SIGDANGER);
458 #endif
459 #ifdef SIGXCPU
460 FATAL_SIG (SIGXCPU);
461 #endif
462 #ifdef SIGXFSZ
463 FATAL_SIG (SIGXFSZ);
464 #endif
466 #undef FATAL_SIG
468 /* Make sure stdout is line-buffered. */
470 #ifdef HAVE_SETLINEBUF
471 setlinebuf (stdout);
472 #else
473 #ifndef SETVBUF_REVERSED
474 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
475 #else /* setvbuf not reversed. */
476 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
477 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
478 #endif /* setvbuf reversed. */
479 #endif /* setlinebuf missing. */
481 /* Figure out where this program lives. */
483 if (argv[0] == 0)
484 argv[0] = "";
485 if (argv[0][0] == '\0')
486 program = "make";
487 else
489 program = rindex (argv[0], '/');
490 #ifdef __MSDOS__
491 if (program == 0)
492 program = rindex (argv[0], '\\');
493 if (program == 0)
494 program = rindex (argv[0], ':');
495 #endif
496 if (program == 0)
497 program = argv[0];
498 else
499 ++program;
502 /* Set up to access user data (files). */
503 user_access ();
505 /* Figure out where we are. */
507 if (getcwd (current_directory, GET_PATH_MAX) == 0)
509 #ifdef HAVE_GETCWD
510 perror_with_name ("getcwd: ", "");
511 #else
512 error ("getwd: %s", current_directory);
513 #endif
514 current_directory[0] = '\0';
515 directory_before_chdir = 0;
517 else
518 directory_before_chdir = savestring (current_directory,
519 strlen (current_directory));
521 /* Read in variables from the environment. It is important that this be
522 done before $(MAKE) is are figured out so its definitions will not be
523 one from the environment. */
525 for (i = 0; envp[i] != 0; ++i)
527 register char *ep = envp[i];
528 while (*ep != '=')
529 ++ep;
530 /* The result of pointer arithmetic is cast to unsigned int for
531 machines where ptrdiff_t is a different size that doesn't widen
532 the same. */
533 define_variable (envp[i], (unsigned int) (ep - envp[i]),
534 ep + 1, o_env, 1)
535 /* Force exportation of every variable culled from the environment.
536 We used to rely on target_environment's v_default code to do this.
537 But that does not work for the case where an environment variable
538 is redefined in a makefile with `override'; it should then still
539 be exported, because it was originally in the environment. */
540 ->export = v_export;
543 /* Decode the switches. */
545 decode_env_switches ("MAKEFLAGS", 9);
546 #if 0
547 /* People write things like:
548 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
549 and we set the -p, -i and -e switches. Doesn't seem quite right. */
550 decode_env_switches ("MFLAGS", 6);
551 #endif
552 decode_switches (argc, argv, 0);
554 /* Print version information. */
556 if (print_version_flag || print_data_base_flag || debug_flag)
557 print_version ();
559 /* `make --version' is supposed to just print the version and exit. */
560 if (print_version_flag)
561 die (0);
563 #ifndef __MSDOS__
564 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
565 (If it is a relative pathname with a slash, prepend our directory name
566 so the result will run the same program regardless of the current dir.
567 If it is a name with no slash, we can only hope that PATH did not
568 find it in the current directory.) */
570 if (current_directory[0] != '\0'
571 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
572 argv[0] = concat (current_directory, "/", argv[0]);
573 #endif
575 /* The extra indirection through $(MAKE_COMMAND) is done
576 for hysterical raisins. */
577 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
578 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
580 if (command_variables != 0)
582 struct command_variable *cv;
583 struct variable *v;
584 unsigned int len = 0;
585 char *value, *p;
587 /* Figure out how much space will be taken up by the command-line
588 variable definitions. */
589 for (cv = command_variables; cv != 0; cv = cv->next)
591 v = cv->variable;
592 len += 2 * strlen (v->name);
593 if (! v->recursive)
594 ++len;
595 ++len;
596 len += 2 * strlen (v->value);
599 /* Now allocate a buffer big enough and fill it. */
600 p = value = (char *) alloca (len);
601 for (cv = command_variables; cv != 0; cv = cv->next)
603 v = cv->variable;
604 p = quote_as_word (p, v->name, 0);
605 if (! v->recursive)
606 *p++ = ':';
607 *p++ = '=';
608 p = quote_as_word (p, v->value, 0);
609 *p++ = ' ';
611 p[-1] = '\0'; /* Kill the final space and terminate. */
613 /* Define an unchangeable variable with a name that no POSIX.2
614 makefile could validly use for its own variable. */
615 (void) define_variable ("-*-command-variables-*-", 23,
616 value, o_automatic, 0);
618 /* Define the variable; this will not override any user definition.
619 Normally a reference to this variable is written into the value of
620 MAKEFLAGS, allowing the user to override this value to affect the
621 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
622 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
623 a reference to this hidden variable is written instead. */
624 (void) define_variable ("MAKEOVERRIDES", 13,
625 "${-*-command-variables-*-}", o_env, 1);
628 /* If there were -C flags, move ourselves about. */
629 if (directories != 0)
630 for (i = 0; directories->list[i] != 0; ++i)
632 char *dir = directories->list[i];
633 if (dir[0] == '~')
635 char *expanded = tilde_expand (dir);
636 if (expanded != 0)
637 dir = expanded;
639 if (chdir (dir) < 0)
640 pfatal_with_name (dir);
641 if (dir != directories->list[i])
642 free (dir);
645 /* Figure out the level of recursion. */
647 struct variable *v = lookup_variable ("MAKELEVEL", 9);
648 if (v != 0 && *v->value != '\0' && *v->value != '-')
649 makelevel = (unsigned int) atoi (v->value);
650 else
651 makelevel = 0;
654 /* Except under -s, always do -w in sub-makes and under -C. */
655 if (!silent_flag && (directories != 0 || makelevel > 0))
656 print_directory_flag = 1;
658 /* Let the user disable that with --no-print-directory. */
659 if (inhibit_print_directory_flag)
660 print_directory_flag = 0;
662 /* Construct the list of include directories to search. */
664 construct_include_path (include_directories == 0 ? (char **) 0
665 : include_directories->list);
667 /* Figure out where we are now, after chdir'ing. */
668 if (directories == 0)
669 /* We didn't move, so we're still in the same place. */
670 starting_directory = current_directory;
671 else
673 if (getcwd (current_directory, GET_PATH_MAX) == 0)
675 #ifdef HAVE_GETCWD
676 perror_with_name ("getcwd: ", "");
677 #else
678 error ("getwd: %s", current_directory);
679 #endif
680 starting_directory = 0;
682 else
683 starting_directory = current_directory;
687 /* Read any stdin makefiles into temporary files. */
689 if (makefiles != 0)
691 register unsigned int i;
692 for (i = 0; i < makefiles->idx; ++i)
693 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
695 /* This makefile is standard input. Since we may re-exec
696 and thus re-read the makefiles, we read standard input
697 into a temporary file and read from that. */
698 FILE *outfile;
700 /* Make a unique filename. */
701 #ifdef HAVE_MKTEMP
702 static char name[] = "/tmp/GmXXXXXX";
703 (void) mktemp (name);
704 #else
705 static char name[L_tmpnam];
706 (void) tmpnam (name);
707 #endif
709 outfile = fopen (name, "w");
710 if (outfile == 0)
711 pfatal_with_name ("fopen (temporary file)");
712 while (!feof (stdin))
714 char buf[2048];
715 int n = fread (buf, 1, sizeof(buf), stdin);
716 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
717 pfatal_with_name ("fwrite (temporary file)");
719 /* Try to make sure we won't remake the temporary
720 file when we are re-exec'd. Kludge-o-matic! */
721 fprintf (outfile, "%s:;\n", name);
722 (void) fclose (outfile);
724 /* Replace the name that read_all_makefiles will
725 see with the name of the temporary file. */
727 char *temp;
728 /* SGI compiler requires alloca's result be assigned simply. */
729 temp = (char *) alloca (sizeof (name));
730 bcopy (name, temp, sizeof (name));
731 makefiles->list[i] = temp;
734 /* Make sure the temporary file will not be remade. */
735 f = enter_file (savestring (name, sizeof name - 1));
736 f->updated = 1;
737 f->update_status = 0;
738 f->command_state = cs_finished;
739 /* Let it be removed when we're done. */
740 f->intermediate = 1;
741 /* But don't mention it. */
742 f->dontcare = 1;
746 /* Set up to handle children dying. This must be done before
747 reading in the makefiles so that `shell' function calls will work. */
749 #ifdef SIGCHLD
750 (void) signal (SIGCHLD, child_handler);
751 #endif
752 #ifdef SIGCLD
753 (void) signal (SIGCLD, child_handler);
754 #endif
756 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
757 #ifdef SIGUSR1
758 (void) signal (SIGUSR1, debug_signal_handler);
759 #endif
761 /* Define the initial list of suffixes for old-style rules. */
763 set_default_suffixes ();
765 /* Define the file rules for the built-in suffix rules. These will later
766 be converted into pattern rules. We used to do this in
767 install_default_implicit_rules, but since that happens after reading
768 makefiles, it results in the built-in pattern rules taking precedence
769 over makefile-specified suffix rules, which is wrong. */
771 install_default_suffix_rules ();
773 /* Define some internal and special variables. */
775 define_automatic_variables ();
777 /* Set up the MAKEFLAGS and MFLAGS variables
778 so makefiles can look at them. */
780 define_makeflags (0, 0);
782 /* Define the default variables. */
783 define_default_variables ();
785 /* Read all the makefiles. */
787 default_file = enter_file (".DEFAULT");
789 read_makefiles
790 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
792 /* Decode switches again, in case the variables were set by the makefile. */
793 decode_env_switches ("MAKEFLAGS", 9);
794 #if 0
795 decode_env_switches ("MFLAGS", 6);
796 #endif
798 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
800 define_makeflags (1, 0);
802 /* Make each `struct dep' point at the `struct file' for the file
803 depended on. Also do magic for special targets. */
805 snap_deps ();
807 /* Convert old-style suffix rules to pattern rules. It is important to
808 do this before installing the built-in pattern rules below, so that
809 makefile-specified suffix rules take precedence over built-in pattern
810 rules. */
812 convert_to_pattern ();
814 /* Install the default implicit pattern rules.
815 This used to be done before reading the makefiles.
816 But in that case, built-in pattern rules were in the chain
817 before user-defined ones, so they matched first. */
819 install_default_implicit_rules ();
821 /* Compute implicit rule limits. */
823 count_implicit_rule_limits ();
825 /* Construct the listings of directories in VPATH lists. */
827 build_vpath_lists ();
829 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
830 and as having been updated already, and files given with -W flags as
831 brand new (time-stamp as far as possible into the future). */
833 if (old_files != 0)
834 for (p = old_files->list; *p != 0; ++p)
836 f = enter_command_line_file (*p);
837 f->last_mtime = (time_t) 1;
838 f->updated = 1;
839 f->update_status = 0;
840 f->command_state = cs_finished;
843 if (new_files != 0)
845 for (p = new_files->list; *p != 0; ++p)
847 f = enter_command_line_file (*p);
848 f->last_mtime = NEW_MTIME;
852 if (read_makefiles != 0)
854 /* Update any makefiles if necessary. */
856 time_t *makefile_mtimes = 0;
857 unsigned int mm_idx = 0;
859 if (debug_flag)
860 puts ("Updating makefiles....");
862 /* Remove any makefiles we don't want to try to update.
863 Also record the current modtimes so we can compare them later. */
865 register struct dep *d, *last;
866 last = 0;
867 d = read_makefiles;
868 while (d != 0)
870 register struct file *f = d->file;
871 if (f->double_colon)
872 for (f = f->double_colon; f != NULL; f = f->prev)
874 if (f->deps == 0 && f->cmds != 0)
876 /* This makefile is a :: target with commands, but
877 no dependencies. So, it will always be remade.
878 This might well cause an infinite loop, so don't
879 try to remake it. (This will only happen if
880 your makefiles are written exceptionally
881 stupidly; but if you work for Athena, that's how
882 you write your makefiles.) */
884 if (debug_flag)
885 printf ("Makefile `%s' might loop; not remaking it.\n",
886 f->name);
888 if (last == 0)
889 read_makefiles = d->next;
890 else
891 last->next = d->next;
893 /* Free the storage. */
894 free ((char *) d);
896 d = last == 0 ? 0 : last->next;
898 break;
901 if (f == NULL || !f->double_colon)
903 if (makefile_mtimes == 0)
904 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
905 else
906 makefile_mtimes = (time_t *)
907 xrealloc ((char *) makefile_mtimes,
908 (mm_idx + 1) * sizeof (time_t));
909 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
910 last = d;
911 d = d->next;
916 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
917 define_makeflags (1, 1);
919 switch (update_goal_chain (read_makefiles, 1))
921 case 1:
922 default:
923 #define BOGUS_UPDATE_STATUS 0
924 assert (BOGUS_UPDATE_STATUS);
925 break;
927 case -1:
928 /* Did nothing. */
929 break;
931 case 2:
932 /* Failed to update. Figure out if we care. */
934 /* Nonzero if any makefile was successfully remade. */
935 int any_remade = 0;
936 /* Nonzero if any makefile we care about failed
937 in updating or could not be found at all. */
938 int any_failed = 0;
939 register unsigned int i;
941 for (i = 0; read_makefiles != 0; ++i)
943 struct dep *d = read_makefiles;
944 read_makefiles = d->next;
945 if (d->file->updated)
947 /* This makefile was updated. */
948 if (d->file->update_status == 0)
950 /* It was successfully updated. */
951 any_remade |= (file_mtime_no_search (d->file)
952 != makefile_mtimes[i]);
954 else if (! (d->changed & RM_DONTCARE))
956 time_t mtime;
957 /* The update failed and this makefile was not
958 from the MAKEFILES variable, so we care. */
959 error ("Failed to remake makefile `%s'.",
960 d->file->name);
961 mtime = file_mtime_no_search (d->file);
962 any_remade |= (mtime != (time_t) -1
963 && mtime != makefile_mtimes[i]);
966 else
967 /* This makefile was not found at all. */
968 if (! (d->changed & RM_DONTCARE))
970 /* This is a makefile we care about. See how much. */
971 if (d->changed & RM_INCLUDED)
972 /* An included makefile. We don't need
973 to die, but we do want to complain. */
974 error ("Included makefile `%s' was not found.",
975 dep_name (d));
976 else
978 /* A normal makefile. We must die later. */
979 error ("Makefile `%s' was not found", dep_name (d));
980 any_failed = 1;
984 free ((char *) d);
987 if (any_remade)
988 goto re_exec;
989 else if (any_failed)
990 die (2);
991 else
992 break;
995 case 0:
996 re_exec:
997 /* Updated successfully. Re-exec ourselves. */
999 remove_intermediates (0);
1001 if (print_data_base_flag)
1002 print_data_base ();
1004 log_working_directory (0);
1006 if (makefiles != 0)
1008 /* These names might have changed. */
1009 register unsigned int i, j = 0;
1010 for (i = 1; i < argc; ++i)
1011 if (!strcmp (argv[i], "-f")) /* XXX */
1013 char *p = &argv[i][2];
1014 if (*p == '\0')
1015 argv[++i] = makefiles->list[j];
1016 else
1017 argv[i] = concat ("-f", makefiles->list[j], "");
1018 ++j;
1022 if (directories != 0 && directories->idx > 0)
1024 char bad;
1025 if (directory_before_chdir != 0)
1027 if (chdir (directory_before_chdir) < 0)
1029 perror_with_name ("chdir", "");
1030 bad = 1;
1032 else
1033 bad = 0;
1035 else
1036 bad = 1;
1037 if (bad)
1038 fatal ("Couldn't change back to original directory.");
1041 for (p = environ; *p != 0; ++p)
1042 if (!strncmp (*p, "MAKELEVEL=", 10))
1044 /* The SGI compiler apparently can't understand
1045 the concept of storing the result of a function
1046 in something other than a local variable. */
1047 char *sgi_loses;
1048 sgi_loses = (char *) alloca (40);
1049 *p = sgi_loses;
1050 sprintf (*p, "MAKELEVEL=%u", makelevel);
1051 break;
1054 if (debug_flag)
1056 char **p;
1057 fputs ("Re-executing:", stdout);
1058 for (p = argv; *p != 0; ++p)
1059 printf (" %s", *p);
1060 puts ("");
1063 fflush (stdout);
1064 fflush (stderr);
1066 exec_command (argv, environ);
1067 /* NOTREACHED */
1071 /* Set up `MAKEFLAGS' again for the normal targets. */
1072 define_makeflags (1, 0);
1075 int status;
1077 /* If there were no command-line goals, use the default. */
1078 if (goals == 0)
1080 if (default_goal_file != 0)
1082 goals = (struct dep *) xmalloc (sizeof (struct dep));
1083 goals->next = 0;
1084 goals->name = 0;
1085 goals->file = default_goal_file;
1088 else
1089 lastgoal->next = 0;
1091 if (goals != 0)
1093 /* Update the goals. */
1095 if (debug_flag)
1096 puts ("Updating goal targets....");
1098 switch (update_goal_chain (goals, 0))
1100 case -1:
1101 /* Nothing happened. */
1102 case 0:
1103 /* Updated successfully. */
1104 status = 0;
1105 break;
1106 case 2:
1107 /* Updating failed. */
1108 status = 2;
1109 break;
1110 case 1:
1111 /* We are under -q and would run some commands. */
1112 status = 1;
1113 break;
1114 default:
1115 abort ();
1118 else
1120 if (read_makefiles == 0)
1121 fatal ("No targets specified and no makefile found");
1122 else
1123 fatal ("No targets");
1126 /* Exit. */
1127 die (status);
1130 return 0;
1133 /* Parsing of arguments, decoding of switches. */
1135 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1136 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1137 (sizeof (long_option_aliases) /
1138 sizeof (long_option_aliases[0]))];
1140 /* Fill in the string and vector for getopt. */
1141 static void
1142 init_switches ()
1144 register char *p;
1145 register int c;
1146 register unsigned int i;
1148 if (options[0] != '\0')
1149 /* Already done. */
1150 return;
1152 p = options;
1154 /* Return switch and non-switch args in order, regardless of
1155 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1156 *p++ = '-';
1158 for (i = 0; switches[i].c != '\0'; ++i)
1160 long_options[i].name = (switches[i].long_name == 0 ? "" :
1161 switches[i].long_name);
1162 long_options[i].flag = 0;
1163 long_options[i].val = switches[i].c;
1164 if (isalnum (switches[i].c))
1165 *p++ = switches[i].c;
1166 switch (switches[i].type)
1168 case flag:
1169 case flag_off:
1170 case ignore:
1171 long_options[i].has_arg = no_argument;
1172 break;
1174 case string:
1175 case positive_int:
1176 case floating:
1177 if (isalnum (switches[i].c))
1178 *p++ = ':';
1179 if (switches[i].noarg_value != 0)
1181 if (isalnum (switches[i].c))
1182 *p++ = ':';
1183 long_options[i].has_arg = optional_argument;
1185 else
1186 long_options[i].has_arg = required_argument;
1187 break;
1190 *p = '\0';
1191 for (c = 0; c < (sizeof (long_option_aliases) /
1192 sizeof (long_option_aliases[0]));
1193 ++c)
1194 long_options[i++] = long_option_aliases[c];
1195 long_options[i].name = 0;
1198 static void
1199 handle_non_switch_argument (arg, env)
1200 char *arg;
1201 int env;
1203 /* Non-option argument. It might be a variable definition. */
1204 struct variable *v;
1205 if (arg[0] == '-' && arg[1] == '\0')
1206 /* Ignore plain `-' for compatibility. */
1207 return;
1208 v = try_variable_definition ((char *) 0, 0, arg, o_command);
1209 if (v != 0)
1211 /* It is indeed a variable definition. Record a pointer to
1212 the variable for later use in define_makeflags. */
1213 struct command_variable *cv
1214 = (struct command_variable *) xmalloc (sizeof (*cv));
1215 cv->variable = v;
1216 cv->next = command_variables;
1217 command_variables = cv;
1219 else if (! env)
1221 /* Not an option or variable definition; it must be a goal
1222 target! Enter it as a file and add it to the dep chain of
1223 goals. */
1224 struct file *f = enter_command_line_file (arg);
1225 f->cmd_target = 1;
1227 if (goals == 0)
1229 goals = (struct dep *) xmalloc (sizeof (struct dep));
1230 lastgoal = goals;
1232 else
1234 lastgoal->next
1235 = (struct dep *) xmalloc (sizeof (struct dep));
1236 lastgoal = lastgoal->next;
1238 lastgoal->name = 0;
1239 lastgoal->file = f;
1243 /* Decode switches from ARGC and ARGV.
1244 They came from the environment if ENV is nonzero. */
1246 static void
1247 decode_switches (argc, argv, env)
1248 int argc;
1249 char **argv;
1250 int env;
1252 int bad = 0;
1253 register const struct command_switch *cs;
1254 register struct stringlist *sl;
1255 register int c;
1257 /* getopt does most of the parsing for us.
1258 First, get its vectors set up. */
1260 init_switches ();
1262 /* Let getopt produce error messages for the command line,
1263 but not for options from the environment. */
1264 opterr = !env;
1265 /* Reset getopt's state. */
1266 optind = 0;
1268 while (optind < argc)
1270 /* Parse the next argument. */
1271 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1272 if (c == EOF)
1273 /* End of arguments, or "--" marker seen. */
1274 break;
1275 else if (c == 1)
1276 /* An argument not starting with a dash. */
1277 handle_non_switch_argument (optarg, env);
1278 else if (c == '?')
1279 /* Bad option. We will print a usage message and die later.
1280 But continue to parse the other options so the user can
1281 see all he did wrong. */
1282 bad = 1;
1283 else
1284 for (cs = switches; cs->c != '\0'; ++cs)
1285 if (cs->c == c)
1287 /* Whether or not we will actually do anything with
1288 this switch. We test this individually inside the
1289 switch below rather than just once outside it, so that
1290 options which are to be ignored still consume args. */
1291 int doit = !env || cs->env;
1293 switch (cs->type)
1295 default:
1296 abort ();
1298 case ignore:
1299 break;
1301 case flag:
1302 case flag_off:
1303 if (doit)
1304 *(int *) cs->value_ptr = cs->type == flag;
1305 break;
1307 case string:
1308 if (!doit)
1309 break;
1311 if (optarg == 0)
1312 optarg = cs->noarg_value;
1314 sl = *(struct stringlist **) cs->value_ptr;
1315 if (sl == 0)
1317 sl = (struct stringlist *)
1318 xmalloc (sizeof (struct stringlist));
1319 sl->max = 5;
1320 sl->idx = 0;
1321 sl->list = (char **) xmalloc (5 * sizeof (char *));
1322 *(struct stringlist **) cs->value_ptr = sl;
1324 else if (sl->idx == sl->max - 1)
1326 sl->max += 5;
1327 sl->list = (char **)
1328 xrealloc ((char *) sl->list,
1329 sl->max * sizeof (char *));
1331 sl->list[sl->idx++] = optarg;
1332 sl->list[sl->idx] = 0;
1333 break;
1335 case positive_int:
1336 if (optarg == 0 && argc > optind
1337 && isdigit (argv[optind][0]))
1338 optarg = argv[optind++];
1340 if (!doit)
1341 break;
1343 if (optarg != 0)
1345 int i = atoi (optarg);
1346 if (i < 1)
1348 if (doit)
1349 error ("the `-%c' option requires a \
1350 positive integral argument",
1351 cs->c);
1352 bad = 1;
1354 else
1355 *(unsigned int *) cs->value_ptr = i;
1357 else
1358 *(unsigned int *) cs->value_ptr
1359 = *(unsigned int *) cs->noarg_value;
1360 break;
1362 case floating:
1363 if (optarg == 0 && optind < argc
1364 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1365 optarg = argv[optind++];
1367 if (doit)
1368 *(double *) cs->value_ptr
1369 = (optarg != 0 ? atof (optarg)
1370 : *(double *) cs->noarg_value);
1372 break;
1375 /* We've found the switch. Stop looking. */
1376 break;
1380 /* There are no more options according to getting getopt, but there may
1381 be some arguments left. Since we have asked for non-option arguments
1382 to be returned in order, this only happens when there is a "--"
1383 argument to prevent later arguments from being options. */
1384 while (optind < argc)
1385 handle_non_switch_argument (argv[optind++], env);
1388 if (!env && (bad || print_usage_flag))
1390 /* Print a nice usage message. */
1391 FILE *usageto;
1393 if (print_version_flag)
1394 print_version ();
1396 usageto = bad ? stderr : stdout;
1398 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1400 fputs ("Options:\n", usageto);
1401 for (cs = switches; cs->c != '\0'; ++cs)
1403 char buf[1024], shortarg[50], longarg[50], *p;
1405 if (cs->description[0] == '-')
1406 continue;
1408 switch (long_options[cs - switches].has_arg)
1410 case no_argument:
1411 shortarg[0] = longarg[0] = '\0';
1412 break;
1413 case required_argument:
1414 sprintf (longarg, "=%s", cs->argdesc);
1415 sprintf (shortarg, " %s", cs->argdesc);
1416 break;
1417 case optional_argument:
1418 sprintf (longarg, "[=%s]", cs->argdesc);
1419 sprintf (shortarg, " [%s]", cs->argdesc);
1420 break;
1423 p = buf;
1425 if (isalnum (cs->c))
1427 sprintf (buf, " -%c%s", cs->c, shortarg);
1428 p += strlen (p);
1430 if (cs->long_name != 0)
1432 unsigned int i;
1433 sprintf (p, "%s--%s%s",
1434 !isalnum (cs->c) ? " " : ", ",
1435 cs->long_name, longarg);
1436 p += strlen (p);
1437 for (i = 0; i < (sizeof (long_option_aliases) /
1438 sizeof (long_option_aliases[0]));
1439 ++i)
1440 if (long_option_aliases[i].val == cs->c)
1442 sprintf (p, ", --%s%s",
1443 long_option_aliases[i].name, longarg);
1444 p += strlen (p);
1448 const struct command_switch *ncs = cs;
1449 while ((++ncs)->c != '\0')
1450 if (ncs->description[0] == '-' &&
1451 ncs->description[1] == cs->c)
1453 /* This is another switch that does the same
1454 one as the one we are processing. We want
1455 to list them all together on one line. */
1456 sprintf (p, ", -%c%s", ncs->c, shortarg);
1457 p += strlen (p);
1458 if (ncs->long_name != 0)
1460 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1461 p += strlen (p);
1466 if (p - buf > DESCRIPTION_COLUMN - 2)
1467 /* The list of option names is too long to fit on the same
1468 line with the description, leaving at least two spaces.
1469 Print it on its own line instead. */
1471 fprintf (usageto, "%s\n", buf);
1472 buf[0] = '\0';
1475 fprintf (usageto, "%*s%s.\n",
1476 - DESCRIPTION_COLUMN,
1477 buf, cs->description);
1480 die (bad ? 2 : 0);
1484 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1485 We do this by chopping the value into a vector of words, prepending a
1486 dash to the first word if it lacks one, and passing the vector to
1487 decode_switches. */
1489 static void
1490 decode_env_switches (envar, len)
1491 char *envar;
1492 unsigned int len;
1494 char *varref = (char *) alloca (2 + len + 2);
1495 char *value, *p;
1496 int argc;
1497 char **argv;
1499 /* Get the variable's value. */
1500 varref[0] = '$';
1501 varref[1] = '(';
1502 bcopy (envar, &varref[2], len);
1503 varref[2 + len] = ')';
1504 varref[2 + len + 1] = '\0';
1505 value = variable_expand (varref);
1507 /* Skip whitespace, and check for an empty value. */
1508 value = next_token (value);
1509 len = strlen (value);
1510 if (len == 0)
1511 return;
1513 /* Allocate a vector that is definitely big enough. */
1514 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1516 /* Allocate a buffer to copy the value into while we split it into words
1517 and unquote it. We must use permanent storage for this because
1518 decode_switches may store pointers into the passed argument words. */
1519 p = (char *) xmalloc (2 * len);
1521 /* getopt will look at the arguments starting at ARGV[1].
1522 Prepend a spacer word. */
1523 argv[0] = 0;
1524 argc = 1;
1525 argv[argc] = p;
1526 while (*value != '\0')
1528 if (*value == '\\')
1529 ++value; /* Skip the backslash. */
1530 else if (isblank (*value))
1532 /* End of the word. */
1533 *p++ = '\0';
1534 argv[++argc] = p;
1536 ++value;
1537 while (isblank (*value));
1538 continue;
1540 *p++ = *value++;
1542 *p = '\0';
1543 argv[++argc] = 0;
1545 if (argc == 2 && argv[1][0] != '-')
1547 /* There is just one word in the value, and it is not a switch.
1548 Either this is the single-word form and we should prepend a dash
1549 before calling decode_switches, or this is the multi-word form and
1550 there is no dash because it is a variable definition. */
1551 struct variable *v;
1552 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1553 if (v != 0)
1555 /* It was indeed a variable definition, and now it has been
1556 processed. There is nothing for decode_switches to do.
1557 Record a pointer to the variable for later use in
1558 define_makeflags. */
1559 struct command_variable *cv
1560 = (struct command_variable *) xmalloc (sizeof (*cv));
1561 cv->variable = v;
1562 cv->next = command_variables;
1563 command_variables = cv;
1564 return;
1567 /* It wasn't a variable definition, so it's some switches without a
1568 leading dash. Add one and pass it along to decode_switches. We
1569 need permanent storage for this in case decode_switches saves
1570 pointers into the value. */
1571 argv[1] = concat ("-", argv[1], "");
1574 /* Parse those words. */
1575 decode_switches (argc, argv, 1);
1578 /* Quote the string IN so that it will be interpreted as a single word with
1579 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1580 signs to avoid variable expansion in make itself. Write the result into
1581 OUT, returning the address of the next character to be written.
1582 Allocating space for OUT twice the length of IN (thrice if
1583 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1585 static char *
1586 quote_as_word (out, in, double_dollars)
1587 char *out, *in;
1588 int double_dollars;
1590 while (*in != '\0')
1592 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1593 *out++ = '\\';
1594 if (double_dollars && *in == '$')
1595 *out++ = '$';
1596 *out++ = *in++;
1599 return out;
1602 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1603 command switches. Include options with args if ALL is nonzero.
1604 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1606 static void
1607 define_makeflags (all, makefile)
1608 int all, makefile;
1610 static const char ref[] = "$(MAKEOVERRIDES)";
1611 static const char posixref[] = "$(-*-command-variables-*-)";
1612 register const struct command_switch *cs;
1613 char *flagstring;
1614 register char *p;
1615 unsigned int words;
1616 struct variable *v;
1618 /* We will construct a linked list of `struct flag's describing
1619 all the flags which need to go in MAKEFLAGS. Then, once we
1620 know how many there are and their lengths, we can put them all
1621 together in a string. */
1623 struct flag
1625 struct flag *next;
1626 const struct command_switch *cs;
1627 char *arg;
1629 struct flag *flags = 0;
1630 unsigned int flagslen = 0;
1631 #define ADD_FLAG(ARG, LEN) \
1632 do { \
1633 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1634 new->cs = cs; \
1635 new->arg = (ARG); \
1636 new->next = flags; \
1637 flags = new; \
1638 if (new->arg == 0) \
1639 ++flagslen; /* Just a single flag letter. */ \
1640 else \
1641 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1642 if (!isalnum (cs->c)) \
1643 /* This switch has no single-letter version, so we use the long. */ \
1644 flagslen += 2 + strlen (cs->long_name); \
1645 } while (0)
1647 for (cs = switches; cs->c != '\0'; ++cs)
1648 if (cs->toenv && (!makefile || !cs->no_makefile))
1649 switch (cs->type)
1651 default:
1652 abort ();
1654 case ignore:
1655 break;
1657 case flag:
1658 case flag_off:
1659 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1660 && (cs->default_value == 0
1661 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1662 ADD_FLAG (0, 0);
1663 break;
1665 case positive_int:
1666 if (all)
1668 if ((cs->default_value != 0
1669 && (*(unsigned int *) cs->value_ptr
1670 == *(unsigned int *) cs->default_value)))
1671 break;
1672 else if (cs->noarg_value != 0
1673 && (*(unsigned int *) cs->value_ptr ==
1674 *(unsigned int *) cs->noarg_value))
1675 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1676 else if (cs->c == 'j')
1677 /* Special case for `-j'. */
1678 ADD_FLAG ("1", 1);
1679 else
1681 char *buf = (char *) alloca (30);
1682 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1683 ADD_FLAG (buf, strlen (buf));
1686 break;
1688 case floating:
1689 if (all)
1691 if (cs->default_value != 0
1692 && (*(double *) cs->value_ptr
1693 == *(double *) cs->default_value))
1694 break;
1695 else if (cs->noarg_value != 0
1696 && (*(double *) cs->value_ptr
1697 == *(double *) cs->noarg_value))
1698 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1699 else
1701 char *buf = (char *) alloca (100);
1702 sprintf (buf, "%g", *(double *) cs->value_ptr);
1703 ADD_FLAG (buf, strlen (buf));
1706 break;
1708 case string:
1709 if (all)
1711 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1712 if (sl != 0)
1714 /* Add the elements in reverse order, because
1715 all the flags get reversed below; and the order
1716 matters for some switches (like -I). */
1717 register unsigned int i = sl->idx;
1718 while (i-- > 0)
1719 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1722 break;
1725 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1727 #undef ADD_FLAG
1729 /* Construct the value in FLAGSTRING.
1730 We allocate enough space for a preceding dash and trailing null. */
1731 flagstring = (char *) alloca (1 + flagslen + 1);
1732 p = flagstring;
1733 words = 1;
1734 *p++ = '-';
1735 while (flags != 0)
1737 /* Add the flag letter or name to the string. */
1738 if (!isalnum (flags->cs->c))
1740 *p++ = '-';
1741 strcpy (p, flags->cs->long_name);
1742 p += strlen (p);
1744 else
1745 *p++ = flags->cs->c;
1746 if (flags->arg != 0)
1748 /* A flag that takes an optional argument which in this case is
1749 omitted is specified by ARG being "". We must distinguish
1750 because a following flag appended without an intervening " -"
1751 is considered the arg for the first. */
1752 if (flags->arg[0] != '\0')
1754 /* Add its argument too. */
1755 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1756 p = quote_as_word (p, flags->arg, 1);
1758 ++words;
1759 /* Write a following space and dash, for the next flag. */
1760 *p++ = ' ';
1761 *p++ = '-';
1763 else if (!isalnum (flags->cs->c))
1765 ++words;
1766 /* Long options must each go in their own word,
1767 so we write the following space and dash. */
1768 *p++ = ' ';
1769 *p++ = '-';
1771 flags = flags->next;
1775 /* Define MFLAGS before appending variable definitions. */
1777 if (p == &flagstring[1])
1778 /* No flags. */
1779 flagstring[0] = '\0';
1780 else if (p[-1] == '-')
1781 /* Kill the final space and dash. */
1782 p[-2] = '\0';
1783 else
1784 /* Terminate the string. */
1785 *p = '\0';
1787 /* Since MFLAGS is not parsed for flags, there is no reason to
1788 override any makefile redefinition. */
1789 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1792 if (all && command_variables != 0)
1794 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1795 command-line variable definitions. */
1797 if (p == &flagstring[1])
1798 /* No flags written, so elide the leading dash already written. */
1799 p = flagstring;
1800 else
1802 /* Separate the variables from the switches with a "--" arg. */
1803 if (p[-1] != '-')
1805 /* We did not already write a trailing " -". */
1806 *p++ = ' ';
1807 *p++ = '-';
1809 /* There is a trailing " -"; fill it out to " -- ". */
1810 *p++ = '-';
1811 *p++ = ' ';
1814 /* Copy in the string. */
1815 if (posix_pedantic)
1817 bcopy (posixref, p, sizeof posixref - 1);
1818 p += sizeof posixref - 1;
1820 else
1822 bcopy (ref, p, sizeof ref - 1);
1823 p += sizeof ref - 1;
1826 else if (p == &flagstring[1])
1828 words = 0;
1829 --p;
1831 else if (p[-1] == '-')
1832 /* Kill the final space and dash. */
1833 p -= 2;
1834 /* Terminate the string. */
1835 *p = '\0';
1837 v = define_variable ("MAKEFLAGS", 9,
1838 /* If there is just a single word of switches,
1839 omit the leading dash unless it is a single
1840 long option with two leading dashes. */
1841 &flagstring[(words == 1 && command_variables == 0
1842 && flagstring[1] != '-')
1843 ? 1 : 0],
1844 /* This used to use o_env, but that lost when a
1845 makefile defined MAKEFLAGS. Makefiles set
1846 MAKEFLAGS to add switches, but we still want
1847 to redefine its value with the full set of
1848 switches. Of course, an override or command
1849 definition will still take precedence. */
1850 o_file, 1);
1851 if (! all)
1852 /* The first time we are called, set MAKEFLAGS to always be exported.
1853 We should not do this again on the second call, because that is
1854 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1855 v->export = v_export;
1858 /* Print version information. */
1860 static void
1861 print_version ()
1863 static int printed_version = 0;
1865 char *precede = print_data_base_flag ? "# " : "";
1867 if (printed_version)
1868 /* Do it only once. */
1869 return;
1871 printf ("%sGNU Make version %s", precede, version_string);
1872 if (remote_description != 0 && *remote_description != '\0')
1873 printf ("-%s", remote_description);
1875 printf (", by Richard Stallman and Roland McGrath.\n\
1876 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
1877 %sThis is free software; see the source for copying conditions.\n\
1878 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1879 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1881 printed_version = 1;
1883 /* Flush stdout so the user doesn't have to wait to see the
1884 version information while things are thought about. */
1885 fflush (stdout);
1888 /* Print a bunch of information about this and that. */
1890 static void
1891 print_data_base ()
1893 extern char *ctime ();
1894 time_t when;
1896 when = time ((time_t *) 0);
1897 printf ("\n# Make data base, printed on %s", ctime (&when));
1899 print_variable_data_base ();
1900 print_dir_data_base ();
1901 print_rule_data_base ();
1902 print_file_data_base ();
1903 print_vpath_data_base ();
1905 when = time ((time_t *) 0);
1906 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1909 /* Exit with STATUS, cleaning up as necessary. */
1911 void
1912 die (status)
1913 int status;
1915 static char dying = 0;
1917 if (!dying)
1919 int err;
1921 dying = 1;
1923 /* Try to move back to the original directory. This is essential on
1924 MS-DOS (where there is really only one process), and on Unix it
1925 puts core files in the original directory instead of the -C
1926 directory. */
1927 if (directory_before_chdir != 0)
1928 chdir (directory_before_chdir);
1930 if (print_version_flag)
1931 print_version ();
1933 /* Wait for children to die. */
1934 for (err = status != 0; job_slots_used > 0; err = 0)
1935 reap_children (1, err);
1937 /* Remove the intermediate files. */
1938 remove_intermediates (0);
1940 if (print_data_base_flag)
1941 print_data_base ();
1943 log_working_directory (0);
1946 exit (status);
1949 /* Write a message indicating that we've just entered or
1950 left (according to ENTERING) the current directory. */
1952 void
1953 log_working_directory (entering)
1954 int entering;
1956 static int entered = 0;
1957 char *message = entering ? "Entering" : "Leaving";
1959 /* Print nothing without the flag. Don't print the entering message
1960 again if we already have. Don't print the leaving message if we
1961 haven't printed the entering message. */
1962 if (! print_directory_flag || entering == entered)
1963 return;
1965 entered = entering;
1967 if (print_data_base_flag)
1968 fputs ("# ", stdout);
1970 if (makelevel == 0)
1971 printf ("%s: %s ", program, message);
1972 else
1973 printf ("%s[%u]: %s ", program, makelevel, message);
1975 if (starting_directory == 0)
1976 puts ("an unknown directory");
1977 else
1978 printf ("directory `%s'\n", starting_directory);