Formerly remake.c.~51~
[make.git] / main.c
blob4e8251e1e407b68ef2d641825896be3c4a514659
1 /* Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
2 This file is part of GNU Make.
4 GNU Make is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 GNU Make is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GNU Make; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include "make.h"
19 #include "commands.h"
20 #include "dep.h"
21 #include "file.h"
22 #include "variable.h"
23 #include "job.h"
24 #include "getopt.h"
27 extern void print_variable_data_base ();
28 extern void print_dir_data_base ();
29 extern void print_rule_data_base ();
30 extern void print_file_data_base ();
31 extern void print_vpath_data_base ();
33 #ifndef HAVE_UNISTD_H
34 extern int chdir ();
35 #endif
36 #ifndef STDC_HEADERS
37 #ifndef sun /* Sun has an incorrect decl in a header. */
38 extern void exit ();
39 #endif
40 extern double atof ();
41 #endif
42 extern char *mktemp ();
44 static void log_working_directory ();
45 static void print_data_base (), print_version ();
46 static void decode_switches (), decode_env_switches ();
47 static void define_makeflags ();
49 /* The structure that describes an accepted command switch. */
51 struct command_switch
53 char c; /* The switch character. */
55 enum /* Type of the value. */
57 flag, /* Turn int flag on. */
58 flag_off, /* Turn int flag off. */
59 string, /* One string per switch. */
60 positive_int, /* A positive integer. */
61 floating, /* A floating-point number (double). */
62 ignore /* Ignored. */
63 } type;
65 char *value_ptr; /* Pointer to the value-holding variable. */
67 unsigned int env:1; /* Can come from MAKEFLAGS. */
68 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
69 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
71 char *noarg_value; /* Pointer to value used if no argument is given. */
72 char *default_value;/* Pointer to default value. */
74 char *long_name; /* Long option name. */
75 char *argdesc; /* Descriptive word for argument. */
76 char *description; /* Description for usage message. */
80 /* The structure used to hold the list of strings given
81 in command switches of a type that takes string arguments. */
83 struct stringlist
85 char **list; /* Nil-terminated list of strings. */
86 unsigned int idx; /* Index into above. */
87 unsigned int max; /* Number of pointers allocated. */
91 /* The recognized command switches. */
93 /* Nonzero means do not print commands to be executed (-s). */
95 int silent_flag;
97 /* Nonzero means just touch the files
98 that would appear to need remaking (-t) */
100 int touch_flag;
102 /* Nonzero means just print what commands would need to be executed,
103 don't actually execute them (-n). */
105 int just_print_flag;
107 /* Print debugging trace info (-d). */
109 int debug_flag = 0;
111 /* Environment variables override makefile definitions. */
113 int env_overrides = 0;
115 /* Nonzero means ignore status codes returned by commands
116 executed to remake files. Just treat them all as successful (-i). */
118 int ignore_errors_flag = 0;
120 /* Nonzero means don't remake anything, just print the data base
121 that results from reading the makefile (-p). */
123 int print_data_base_flag = 0;
125 /* Nonzero means don't remake anything; just return a nonzero status
126 if the specified targets are not up to date (-q). */
128 int question_flag = 0;
130 /* Nonzero means do not use any of the builtin rules (-r). */
132 int no_builtin_rules_flag = 0;
134 /* Nonzero means keep going even if remaking some file fails (-k). */
136 int keep_going_flag;
137 int default_keep_going_flag = 0;
139 /* Nonzero means print directory before starting and when done (-w). */
141 int print_directory_flag = 0;
143 /* Nonzero means ignore print_directory_flag and never print the directory.
144 This is necessary because print_directory_flag is set implicitly. */
146 int inhibit_print_directory_flag = 0;
148 /* Nonzero means print version information. */
150 int print_version_flag = 0;
152 /* List of makefiles given with -f switches. */
154 static struct stringlist *makefiles = 0;
157 /* Number of job slots (commands that can be run at once). */
159 unsigned int job_slots = 1;
160 unsigned int default_job_slots = 1;
162 /* Value of job_slots that means no limit. */
164 static unsigned int inf_jobs = 0;
166 /* Maximum load average at which multiple jobs will be run.
167 Negative values mean unlimited, while zero means limit to
168 zero load (which could be useful to start infinite jobs remotely
169 but one at a time locally). */
171 double max_load_average = -1.0;
172 double default_load_average = -1.0;
174 /* List of directories given with -C switches. */
176 static struct stringlist *directories = 0;
178 /* List of include directories given with -I switches. */
180 static struct stringlist *include_directories = 0;
182 /* List of files given with -o switches. */
184 static struct stringlist *old_files = 0;
186 /* List of files given with -W switches. */
188 static struct stringlist *new_files = 0;
190 /* If nonzero, we should just print usage and exit. */
192 static int print_usage_flag = 0;
194 /* If nonzero, we should print a warning message
195 for each reference to an undefined variable. */
197 int warn_undefined_variables_flag;
199 /* The table of command switches. */
201 static const struct command_switch switches[] =
203 { 'b', ignore, 0, 0, 0, 0, 0, 0,
204 0, 0,
205 "Ignored for compatibility" },
206 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
207 "directory", "DIRECTORY",
208 "Change to DIRECTORY before doing anything" },
209 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
210 "debug", 0,
211 "Print lots of debugging information" },
212 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
213 "environment-overrides", 0,
214 "Environment variables override makefiles" },
215 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
216 "file", "FILE",
217 "Read FILE as a makefile" },
218 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
219 "help", 0,
220 "Print this message and exit" },
221 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
222 "ignore-errors", 0,
223 "Ignore errors from commands" },
224 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
225 "include-dir", "DIRECTORY",
226 "Search DIRECTORY for included makefiles" },
227 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
228 (char *) &inf_jobs, (char *) &default_job_slots,
229 "jobs", "N",
230 "Allow N jobs at once; infinite jobs with no arg" },
231 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
232 0, (char *) &default_keep_going_flag,
233 "keep-going", 0,
234 "Keep going when some targets can't be made" },
235 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
236 (char *) &default_load_average, (char *) &default_load_average,
237 "load-average", "N",
238 "Don't start multiple jobs unless load is below N" },
239 { 'm', ignore, 0, 0, 0, 0, 0, 0,
240 0, 0,
241 "-b" },
242 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
243 "just-print", 0,
244 "Don't actually run any commands; just print them" },
245 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
246 "old-file", "FILE",
247 "Consider FILE to be very old and don't remake it" },
248 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
249 "print-data-base", 0,
250 "Print make's internal database" },
251 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
252 "question", 0,
253 "Run no commands; exit status says if up to date" },
254 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
255 "no-builtin-rules", 0,
256 "Disable the built-in implicit rules" },
257 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
258 "silent", 0,
259 "Don't echo commands" },
260 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
261 0, (char *) &default_keep_going_flag,
262 "no-keep-going", 0,
263 "Turns off -k" },
264 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
265 "touch", 0,
266 "Touch targets instead of remaking them" },
267 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
268 "version", 0,
269 "Print the version number of make and exit" },
270 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
271 "print-directory", 0,
272 "Print the current directory" },
273 { 1, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
274 "no-print-directory", 0,
275 "Turn off -w, even if it was turned on implicitly" },
276 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
277 "what-if", "FILE",
278 "Consider FILE to be infinitely new" },
279 { 2, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
280 "warn-undefined-variables", 0,
281 "Warn when an undefined variable is referenced" },
282 { '\0', }
285 /* Secondary long names for options. */
287 static struct option long_option_aliases[] =
289 { "quiet", no_argument, 0, 's' },
290 { "stop", no_argument, 0, 'S' },
291 { "new-file", required_argument, 0, 'W' },
292 { "assume-new", required_argument, 0, 'W' },
293 { "assume-old", required_argument, 0, 'o' },
294 { "max-load", optional_argument, 0, 'l' },
295 { "dry-run", no_argument, 0, 'n' },
296 { "recon", no_argument, 0, 'n' },
297 { "makefile", required_argument, 0, 'f' },
300 /* The usage message prints the descriptions of options starting in
301 this column. Make sure it leaves enough room for the longest
302 description to fit in less than 80 characters. */
304 #define DESCRIPTION_COLUMN 30
306 /* List of non-switch arguments. */
308 struct stringlist *other_args = 0;
310 /* The name we were invoked with. */
312 char *program;
314 /* Our current directory after processing all -C options. */
316 char *starting_directory;
318 /* Value of the MAKELEVEL variable at startup (or 0). */
320 unsigned int makelevel;
322 /* First file defined in the makefile whose name does not
323 start with `.'. This is the default to remake if the
324 command line does not specify. */
326 struct file *default_goal_file;
328 /* Pointer to structure for the file .DEFAULT
329 whose commands are used for any file that has none of its own.
330 This is zero if the makefiles do not define .DEFAULT. */
332 struct file *default_file;
334 /* Mask of signals that are being caught with fatal_error_signal. */
336 #ifdef POSIX
337 sigset_t fatal_signal_set;
338 #else
339 #ifdef HAVE_SIGSETMASK
340 int fatal_signal_mask;
341 #endif
342 #endif
344 static struct file *
345 enter_command_line_file (name)
346 char *name;
348 if (name[0] == '~')
350 char *expanded = tilde_expand (name);
351 if (expanded != 0)
352 name = expanded; /* Memory leak; I don't care. */
355 /* This is also done in parse_file_seq, so this is redundant
356 for names read from makefiles. It is here for names passed
357 on the command line. */
358 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
360 name += 2;
361 while (*name == '/')
362 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
363 ++name;
366 if (*name == '\0')
368 /* It was all slashes! Move back to the dot and truncate
369 it after the first slash, so it becomes just "./". */
371 --name;
372 while (name[0] != '.');
373 name[2] = '\0';
376 return enter_file (savestring (name, strlen (name)));
380 main (argc, argv, envp)
381 int argc;
382 char **argv;
383 char **envp;
385 extern void init_dir ();
386 extern RETSIGTYPE fatal_error_signal (), child_handler ();
387 register struct file *f;
388 register unsigned int i;
389 register char *cmd_defs;
390 register unsigned int cmd_defs_len, cmd_defs_idx;
391 char **p;
392 time_t now;
393 struct dep *goals = 0;
394 register struct dep *lastgoal;
395 struct dep *read_makefiles;
396 PATH_VAR (current_directory);
397 char *directory_before_chdir;
399 mcheck(0);
401 default_goal_file = 0;
402 reading_filename = 0;
403 reading_lineno_ptr = 0;
405 #ifndef HAVE_SYS_SIGLIST
406 signame_init ();
407 #endif
409 #ifdef POSIX
410 sigemptyset (&fatal_signal_set);
411 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
412 #else
413 #ifdef HAVE_SIGSETMASK
414 fatal_signal_mask = 0;
415 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
416 #else
417 #define ADD_SIG(sig)
418 #endif
419 #endif
421 #define FATAL_SIG(sig) \
422 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
423 (void) signal ((sig), SIG_IGN); \
424 else \
425 ADD_SIG (sig);
427 FATAL_SIG (SIGHUP);
428 FATAL_SIG (SIGQUIT);
429 FATAL_SIG (SIGINT);
430 FATAL_SIG (SIGTERM);
432 #ifdef SIGDANGER
433 FATAL_SIG (SIGDANGER);
434 #endif
435 #ifdef SIGXCPU
436 FATAL_SIG (SIGXCPU);
437 #endif
438 #ifdef SIGXFSZ
439 FATAL_SIG (SIGXFSZ);
440 #endif
442 #undef FATAL_SIG
444 /* Make sure stdout is line-buffered. */
446 #ifdef HAVE_SETLINEBUF
447 setlinebuf (stdout);
448 #else
449 #ifndef SETVBUF_REVERSED
450 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
451 #else /* setvbuf not reversed. */
452 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
453 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
454 #endif /* setvbuf reversed. */
455 #endif /* setlinebuf missing. */
457 /* Initialize the directory hashing code. */
458 init_dir ();
460 /* Set up to access user data (files). */
461 user_access ();
463 /* Figure out where this program lives. */
465 if (argv[0] == 0)
466 argv[0] = "";
467 if (argv[0][0] == '\0')
468 program = "make";
469 else
471 program = rindex (argv[0], '/');
472 if (program == 0)
473 program = argv[0];
474 else
475 ++program;
478 /* Figure out where we are. */
480 if (getcwd (current_directory, GET_PATH_MAX) == 0)
482 #ifdef HAVE_GETCWD
483 perror_with_name ("getcwd: ", "");
484 #else
485 error ("getwd: %s", current_directory);
486 #endif
487 current_directory[0] = '\0';
488 directory_before_chdir = 0;
490 else
491 directory_before_chdir = savestring (current_directory,
492 strlen (current_directory));
494 /* Read in variables from the environment. It is important that this be
495 done before `MAKE' and `MAKEOVERRIDES' are figured out so their
496 definitions will not be ones from the environment. */
498 for (i = 0; envp[i] != 0; ++i)
500 register char *ep = envp[i];
501 while (*ep != '=')
502 ++ep;
503 define_variable (envp[i], ep - envp[i], ep + 1, o_env, 1)
504 /* Force exportation of every variable culled from the environment.
505 We used to rely on target_environment's v_default code to do this.
506 But that does not work for the case where an environment variable
507 is redefined in a makefile with `override'; it should then still
508 be exported, because it was originally in the environment. */
509 ->export = v_export;
512 /* Decode the switches. */
514 decode_env_switches ("MAKEFLAGS", 9);
515 #if 0
516 /* People write things like:
517 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
518 and we set the -p, -i and -e switches. Doesn't seem quite right. */
519 decode_env_switches ("MFLAGS", 6);
520 #endif
521 decode_switches (argc, argv, 0);
523 /* Print version information. */
525 if (print_version_flag || print_data_base_flag || debug_flag)
526 print_version ();
528 /* `make --version' is supposed to just print the version and exit. */
529 if (print_version_flag)
530 die (0);
532 /* Search for command line arguments that define variables,
533 and do the definitions. Also save up the text of these
534 arguments in CMD_DEFS so we can put them into the values
535 of $(MAKEOVERRIDES) and $(MAKE). */
537 cmd_defs_len = 200;
538 cmd_defs = (char *) xmalloc (cmd_defs_len);
539 cmd_defs_idx = 0;
541 for (i = 1; other_args->list[i] != 0; ++i)
543 if (other_args->list[i][0] == '\0')
544 /* Ignore empty arguments, so they can't kill enter_file. */
545 continue;
547 /* Try a variable definition. */
548 if (try_variable_definition ((char *) 0, 0,
549 other_args->list[i], o_command))
551 /* It succeeded. The variable is already defined.
552 Backslash-quotify it and append it to CMD_DEFS, then clobber it
553 to 0 in the list so that it won't be taken for a goal target. */
554 register char *p = other_args->list[i];
555 unsigned int l = strlen (p);
556 if (cmd_defs_idx + (l * 2) + 1 > cmd_defs_len)
558 if (l * 2 > cmd_defs_len)
559 cmd_defs_len += l * 2;
560 else
561 cmd_defs_len *= 2;
562 cmd_defs = (char *) xrealloc (cmd_defs, cmd_defs_len);
565 while (*p != '\0')
567 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p) != 0)
568 cmd_defs[cmd_defs_idx++] = '\\';
569 cmd_defs[cmd_defs_idx++] = *p++;
571 cmd_defs[cmd_defs_idx++] = ' ';
573 else
575 /* It was not a variable definition, so it is a target to be made.
576 Enter it as a file and add it to the dep chain of goals. */
577 f = enter_command_line_file (other_args->list[i]);
578 f->cmd_target = 1;
580 if (goals == 0)
582 goals = (struct dep *) xmalloc (sizeof (struct dep));
583 lastgoal = goals;
585 else
587 lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
588 lastgoal = lastgoal->next;
590 lastgoal->name = 0;
591 lastgoal->file = f;
595 if (cmd_defs_idx > 0)
597 cmd_defs[cmd_defs_idx - 1] = '\0';
598 (void) define_variable ("MAKEOVERRIDES", 13, cmd_defs, o_default, 0);
600 free (cmd_defs);
602 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
603 (If it is a relative pathname with a slash, prepend our directory name
604 so the result will run the same program regardless of the current dir.
605 If it is a name with no slash, we can only hope that PATH did not
606 find it in the current directory.) */
608 if (current_directory[0] != '\0'
609 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
610 argv[0] = concat (current_directory, "/", argv[0]);
612 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
614 /* Append the command-line variable definitions gathered above
615 so sub-makes will get them as command-line definitions. */
617 (void) define_variable ("MAKE", 4,
618 "$(MAKE_COMMAND) $(MAKEOVERRIDES)", o_default, 1);
620 /* If there were -C flags, move ourselves about. */
622 if (directories != 0)
623 for (i = 0; directories->list[i] != 0; ++i)
625 char *dir = directories->list[i];
626 if (dir[0] == '~')
628 char *expanded = tilde_expand (dir);
629 if (expanded != 0)
630 dir = expanded;
632 if (chdir (dir) < 0)
633 pfatal_with_name (dir);
634 if (dir != directories->list[i])
635 free (dir);
638 /* Figure out the level of recursion. */
640 struct variable *v = lookup_variable ("MAKELEVEL", 9);
641 if (v != 0 && *v->value != '\0' && *v->value != '-')
642 makelevel = (unsigned int) atoi (v->value);
643 else
644 makelevel = 0;
647 /* Except under -s, always do -w in sub-makes and under -C. */
648 if (!silent_flag && (directories != 0 || makelevel > 0))
649 print_directory_flag = 1;
651 /* Let the user disable that with --no-print-directory. */
652 if (inhibit_print_directory_flag)
653 print_directory_flag = 0;
655 /* Construct the list of include directories to search. */
657 construct_include_path (include_directories == 0 ? (char **) 0
658 : include_directories->list);
660 /* Figure out where we are now, after chdir'ing. */
661 if (directories == 0)
662 /* We didn't move, so we're still in the same place. */
663 starting_directory = current_directory;
664 else
666 if (getcwd (current_directory, GET_PATH_MAX) == 0)
668 #ifdef HAVE_GETCWD
669 perror_with_name ("getcwd: ", "");
670 #else
671 error ("getwd: %s", current_directory);
672 #endif
673 starting_directory = 0;
675 else
676 starting_directory = current_directory;
679 /* Tell the user where he is. */
681 if (print_directory_flag)
682 log_working_directory (1);
684 /* Read any stdin makefiles into temporary files. */
686 if (makefiles != 0)
688 register unsigned int i;
689 for (i = 0; i < makefiles->idx; ++i)
690 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
692 /* This makefile is standard input. Since we may re-exec
693 and thus re-read the makefiles, we read standard input
694 into a temporary file and read from that. */
695 static char name[] = "/tmp/GmXXXXXX";
696 FILE *outfile;
698 /* Make a unique filename. */
699 (void) mktemp (name);
701 outfile = fopen (name, "w");
702 if (outfile == 0)
703 pfatal_with_name ("fopen (temporary file)");
704 while (!feof (stdin))
706 char buf[2048];
707 int n = fread (buf, 1, sizeof(buf), stdin);
708 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
709 pfatal_with_name ("fwrite (temporary file)");
711 /* Try to make sure we won't remake the temporary
712 file when we are re-exec'd. Kludge-o-matic! */
713 fprintf (outfile, "%s:;\n", name);
714 (void) fclose (outfile);
716 /* Replace the name that read_all_makefiles will
717 see with the name of the temporary file. */
719 char *temp;
720 /* SGI compiler requires alloca's result be assigned simply. */
721 temp = (char *) alloca (sizeof (name));
722 bcopy (name, temp, sizeof (name));
723 makefiles->list[i] = temp;
726 /* Make sure the temporary file will not be remade. */
727 f = enter_file (savestring (name, sizeof name - 1));
728 f->updated = 1;
729 f->update_status = 0;
730 f->command_state = cs_finished;
731 /* Let it be removed when we're done. */
732 f->intermediate = 1;
733 /* But don't mention it. */
734 f->dontcare = 1;
738 /* Set up to handle children dying. This must be done before
739 reading in the makefiles so that `shell' function calls will work. */
741 #ifdef SIGCHLD
742 (void) signal (SIGCHLD, child_handler);
743 #endif
744 #ifdef SIGCLD
745 (void) signal (SIGCLD, child_handler);
746 #endif
748 /* Define the initial list of suffixes for old-style rules. */
750 set_default_suffixes ();
752 /* Define the file rules for the built-in suffix rules. These will later
753 be converted into pattern rules. We used to do this in
754 install_default_implicit_rules, but since that happens after reading
755 makefiles, it results in the built-in pattern rules taking precedence
756 over makefile-specified suffix rules, which is wrong. */
758 install_default_suffix_rules ();
760 /* Define some internal and special variables. */
762 define_automatic_variables ();
764 /* Set up the MAKEFLAGS and MFLAGS variables
765 so makefiles can look at them. */
767 define_makeflags (0, 0);
769 /* Define the default variables. */
770 define_default_variables ();
772 /* Read all the makefiles. */
774 default_file = enter_file (".DEFAULT");
776 read_makefiles
777 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
779 /* Decode switches again, in case the variables were set by the makefile. */
780 decode_env_switches ("MAKEFLAGS", 9);
781 #if 0
782 decode_env_switches ("MFLAGS", 6);
783 #endif
785 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
787 define_makeflags (1, 0);
789 ignore_errors_flag |= lookup_file (".IGNORE") != 0;
791 silent_flag |= lookup_file (".SILENT") != 0;
793 /* Make each `struct dep' point at the
794 `struct file' for the file depended on. */
796 snap_deps ();
798 /* Convert old-style suffix rules to pattern rules. It is important to
799 do this before installing the built-in pattern rules below, so that
800 makefile-specified suffix rules take precedence over built-in pattern
801 rules. */
803 convert_to_pattern ();
805 /* Install the default implicit pattern rules.
806 This used to be done before reading the makefiles.
807 But in that case, built-in pattern rules were in the chain
808 before user-defined ones, so they matched first. */
810 install_default_implicit_rules ();
812 /* Compute implicit rule limits. */
814 count_implicit_rule_limits ();
816 /* Construct the listings of directories in VPATH lists. */
818 build_vpath_lists ();
820 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
821 and as having been updated already, and files given with -W flags
822 as brand new (time-stamp of now). */
824 if (old_files != 0)
825 for (p = old_files->list; *p != 0; ++p)
827 f = enter_command_line_file (*p);
828 f->last_mtime = (time_t) 1;
829 f->updated = 1;
830 f->update_status = 0;
831 f->command_state = cs_finished;
834 if (new_files != 0)
836 now = time ((time_t *) 0);
837 for (p = new_files->list; *p != 0; ++p)
839 f = enter_command_line_file (*p);
840 f->last_mtime = now;
844 if (read_makefiles != 0)
846 /* Update any makefiles if necessary. */
848 time_t *makefile_mtimes = 0;
849 unsigned int mm_idx = 0;
851 if (debug_flag)
852 puts ("Updating makefiles....");
854 /* Remove any makefiles we don't want to try to update.
855 Also record the current modtimes so we can compare them later. */
857 register struct dep *d, *last;
858 last = 0;
859 d = read_makefiles;
860 while (d != 0)
862 register struct file *f = d->file;
863 if (f->double_colon)
866 if (f->deps == 0 && f->cmds != 0)
868 /* This makefile is a :: target with commands, but
869 no dependencies. So, it will always be remade.
870 This might well cause an infinite loop, so don't
871 try to remake it. (This will only happen if
872 your makefiles are written exceptionally
873 stupidly; but if you work for Athena, that's how
874 you write your makefiles.) */
876 if (debug_flag)
877 printf ("Makefile `%s' might loop; not remaking it.\n",
878 f->name);
880 if (last == 0)
881 read_makefiles = d->next;
882 else
883 last->next = d->next;
885 /* Free the storage. */
886 free ((char *) d);
888 d = last == 0 ? 0 : last->next;
890 break;
892 f = f->prev;
894 while (f != NULL);
895 if (f == NULL || !f->double_colon)
897 if (makefile_mtimes == 0)
898 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
899 else
900 makefile_mtimes = (time_t *)
901 xrealloc ((char *) makefile_mtimes,
902 (mm_idx + 1) * sizeof (time_t));
903 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
904 last = d;
905 d = d->next;
910 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
911 define_makeflags (1, 1);
913 switch (update_goal_chain (read_makefiles, 1))
915 default:
916 abort ();
918 case -1:
919 /* Did nothing. */
920 break;
922 case 1:
923 /* Failed to update. Figure out if we care. */
925 /* Nonzero if any makefile was successfully remade. */
926 int any_remade = 0;
927 /* Nonzero if any makefile we care about failed
928 in updating or could not be found at all. */
929 int any_failed = 0;
930 register unsigned int i;
932 for (i = 0; read_makefiles != 0; ++i)
934 struct dep *d = read_makefiles;
935 read_makefiles = d->next;
936 if (d->file->updated)
938 /* This makefile was updated. */
939 if (d->file->update_status == 0)
941 /* It was successfully updated. */
942 any_remade |= (file_mtime_no_search (d->file)
943 != makefile_mtimes[i]);
945 else if (! (d->changed & RM_DONTCARE))
947 time_t mtime;
948 /* The update failed and this makefile was not
949 from the MAKEFILES variable, so we care. */
950 error ("Failed to remake makefile `%s'.",
951 d->file->name);
952 mtime = file_mtime_no_search (d->file);
953 any_remade |= (mtime != (time_t) -1
954 && mtime != makefile_mtimes[i]);
957 else
958 /* This makefile was not found at all. */
959 if (! (d->changed & RM_DONTCARE))
961 /* This is a makefile we care about. See how much. */
962 if (d->changed & RM_INCLUDED)
963 /* An included makefile. We don't need
964 to die, but we do want to complain. */
965 error ("Included makefile `%s' was not found.",
966 dep_name (d));
967 else
969 /* A normal makefile. We must die later. */
970 error ("Makefile `%s' was not found", dep_name (d));
971 any_failed = 1;
975 free ((char *) d);
978 if (any_remade)
979 goto re_exec;
980 else if (any_failed)
981 die (1);
982 else
983 break;
986 case 0:
987 re_exec:
988 /* Updated successfully. Re-exec ourselves. */
990 remove_intermediates (0);
992 if (print_data_base_flag)
993 print_data_base ();
995 if (print_directory_flag)
996 log_working_directory (0);
998 if (makefiles != 0)
1000 /* These names might have changed. */
1001 register unsigned int i, j = 0;
1002 for (i = 1; i < argc; ++i)
1003 if (!strcmp (argv[i], "-f")) /* XXX */
1005 char *p = &argv[i][2];
1006 if (*p == '\0')
1007 argv[++i] = makefiles->list[j];
1008 else
1009 argv[i] = concat ("-f", makefiles->list[j], "");
1010 ++j;
1014 if (directories != 0 && directories->idx > 0)
1016 char bad;
1017 if (directory_before_chdir != 0)
1019 if (chdir (directory_before_chdir) < 0)
1021 perror_with_name ("chdir", "");
1022 bad = 1;
1024 else
1025 bad = 0;
1027 else
1028 bad = 1;
1029 if (bad)
1030 fatal ("Couldn't change back to original directory.");
1033 for (p = environ; *p != 0; ++p)
1034 if (!strncmp (*p, "MAKELEVEL=", 10))
1036 /* The SGI compiler apparently can't understand
1037 the concept of storing the result of a function
1038 in something other than a local variable. */
1039 char *sgi_loses;
1040 sgi_loses = (char *) alloca (40);
1041 *p = sgi_loses;
1042 sprintf (*p, "MAKELEVEL=%u", makelevel);
1043 break;
1046 if (debug_flag)
1048 char **p;
1049 fputs ("Re-executing:", stdout);
1050 for (p = argv; *p != 0; ++p)
1051 printf (" %s", *p);
1052 puts ("");
1055 fflush (stdout);
1056 fflush (stderr);
1058 exec_command (argv, environ);
1059 /* NOTREACHED */
1063 /* Set up `MAKEFLAGS' again for the normal targets. */
1064 define_makeflags (1, 0);
1067 int status;
1069 /* If there were no command-line goals, use the default. */
1070 if (goals == 0)
1072 if (default_goal_file != 0)
1074 goals = (struct dep *) xmalloc (sizeof (struct dep));
1075 goals->next = 0;
1076 goals->name = 0;
1077 goals->file = default_goal_file;
1080 else
1081 lastgoal->next = 0;
1083 if (goals != 0)
1085 /* Update the goals. */
1087 if (debug_flag)
1088 puts ("Updating goal targets....");
1090 switch (update_goal_chain (goals, 0))
1092 case -1:
1093 /* Nothing happened. */
1094 case 0:
1095 /* Updated successfully. */
1096 status = 0;
1097 break;
1098 case 1:
1099 /* Updating failed. */
1100 status = 1;
1101 break;
1102 default:
1103 abort ();
1106 else
1108 if (read_makefiles == 0)
1109 fatal ("No targets specified and no makefile found");
1110 else
1111 fatal ("No targets");
1114 /* Exit. */
1115 die (status);
1118 return 0;
1121 /* Parsing of arguments, decoding of switches. */
1123 static char options[sizeof (switches) / sizeof (switches[0]) * 3];
1124 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1125 (sizeof (long_option_aliases) /
1126 sizeof (long_option_aliases[0]))];
1128 /* Fill in the string and vector for getopt. */
1129 static void
1130 init_switches ()
1132 register char *p;
1133 register int c;
1134 register unsigned int i;
1136 if (options[0] != '\0')
1137 /* Already done. */
1138 return;
1140 p = options;
1141 for (i = 0; switches[i].c != '\0'; ++i)
1143 long_options[i].name = (switches[i].long_name == 0 ? "" :
1144 switches[i].long_name);
1145 long_options[i].flag = 0;
1146 long_options[i].val = switches[i].c;
1147 if (isalnum (switches[i].c))
1148 *p++ = switches[i].c;
1149 switch (switches[i].type)
1151 case flag:
1152 case flag_off:
1153 case ignore:
1154 long_options[i].has_arg = no_argument;
1155 break;
1157 case string:
1158 case positive_int:
1159 case floating:
1160 if (isalnum (switches[i].c))
1161 *p++ = ':';
1162 if (switches[i].noarg_value != 0)
1164 if (isalnum (switches[i].c))
1165 *p++ = ':';
1166 long_options[i].has_arg = optional_argument;
1168 else
1169 long_options[i].has_arg = required_argument;
1170 break;
1173 *p = '\0';
1174 for (c = 0; c < (sizeof (long_option_aliases) /
1175 sizeof (long_option_aliases[0]));
1176 ++c)
1177 long_options[i++] = long_option_aliases[c];
1178 long_options[i].name = 0;
1181 /* Decode switches from ARGC and ARGV.
1182 They came from the environment if ENV is nonzero. */
1184 static void
1185 decode_switches (argc, argv, env)
1186 int argc;
1187 char **argv;
1188 int env;
1190 int bad = 0;
1191 register const struct command_switch *cs;
1192 register struct stringlist *sl;
1193 register int c;
1195 if (!env)
1197 other_args = (struct stringlist *) xmalloc (sizeof (struct stringlist));
1198 other_args->max = argc + 1;
1199 other_args->list = (char **) xmalloc ((argc + 1) * sizeof (char *));
1200 other_args->idx = 1;
1201 other_args->list[0] = argv[0];
1204 /* getopt does most of the parsing for us.
1205 First, get its vectors set up. */
1207 init_switches ();
1209 /* Let getopt produce error messages for the command line,
1210 but not for options from the environment. */
1211 opterr = !env;
1212 /* Reset getopt's state. */
1213 optind = 0;
1215 while ((c = getopt_long (argc, argv,
1216 options, long_options, (int *) 0)) != EOF)
1218 if (c == '?')
1219 /* Bad option. We will print a usage message and die later.
1220 But continue to parse the other options so the user can
1221 see all he did wrong. */
1222 bad = 1;
1223 else
1224 for (cs = switches; cs->c != '\0'; ++cs)
1225 if (cs->c == c)
1227 /* Whether or not we will actually do anything with
1228 this switch. We test this individually inside the
1229 switch below rather than just once outside it, so that
1230 options which are to be ignored still consume args. */
1231 int doit = !env || cs->env;
1233 switch (cs->type)
1235 default:
1236 abort ();
1238 case ignore:
1239 break;
1241 case flag:
1242 case flag_off:
1243 if (doit)
1244 *(int *) cs->value_ptr = cs->type == flag;
1245 break;
1247 case string:
1248 if (!doit)
1249 break;
1251 if (optarg == 0)
1252 optarg = cs->noarg_value;
1254 sl = *(struct stringlist **) cs->value_ptr;
1255 if (sl == 0)
1257 sl = (struct stringlist *)
1258 xmalloc (sizeof (struct stringlist));
1259 sl->max = 5;
1260 sl->idx = 0;
1261 sl->list = (char **) xmalloc (5 * sizeof (char *));
1262 *(struct stringlist **) cs->value_ptr = sl;
1264 else if (sl->idx == sl->max - 1)
1266 sl->max += 5;
1267 sl->list = (char **)
1268 xrealloc ((char *) sl->list,
1269 sl->max * sizeof (char *));
1271 sl->list[sl->idx++] = optarg;
1272 sl->list[sl->idx] = 0;
1273 break;
1275 case positive_int:
1276 if (optarg == 0 && argc > optind
1277 && isdigit (argv[optind][0]))
1278 optarg = argv[optind++];
1280 if (!doit)
1281 break;
1283 if (optarg != 0)
1285 int i = atoi (optarg);
1286 if (i < 1)
1288 if (doit)
1289 error ("the `-%c' option requires a \
1290 positive integral argument",
1291 cs->c);
1292 bad = 1;
1294 else
1295 *(unsigned int *) cs->value_ptr = i;
1297 else
1298 *(unsigned int *) cs->value_ptr
1299 = *(unsigned int *) cs->noarg_value;
1300 break;
1302 case floating:
1303 if (optarg == 0 && optind < argc
1304 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1305 optarg = argv[optind++];
1307 if (doit)
1308 *(double *) cs->value_ptr
1309 = (optarg != 0 ? atof (optarg)
1310 : *(double *) cs->noarg_value);
1312 break;
1315 /* We've found the switch. Stop looking. */
1316 break;
1320 if (!env)
1322 /* Collect the remaining args in the `other_args' string list. */
1324 while (optind < argc)
1326 char *arg = argv[optind++];
1327 if (arg[0] != '-' || arg[1] != '\0')
1328 other_args->list[other_args->idx++] = arg;
1330 other_args->list[other_args->idx] = 0;
1333 if (!env && (bad || print_usage_flag))
1335 /* Print a nice usage message. */
1337 if (print_version_flag)
1338 print_version ();
1340 fprintf (stderr, "Usage: %s [options] [target] ...\n", program);
1342 fputs ("Options:\n", stderr);
1343 for (cs = switches; cs->c != '\0'; ++cs)
1345 char buf[1024], shortarg[50], longarg[50], *p;
1347 if (cs->description[0] == '-')
1348 continue;
1350 switch (long_options[cs - switches].has_arg)
1352 case no_argument:
1353 shortarg[0] = longarg[0] = '\0';
1354 break;
1355 case required_argument:
1356 sprintf (longarg, "=%s", cs->argdesc);
1357 sprintf (shortarg, " %s", cs->argdesc);
1358 break;
1359 case optional_argument:
1360 sprintf (longarg, "[=%s]", cs->argdesc);
1361 sprintf (shortarg, " [%s]", cs->argdesc);
1362 break;
1365 p = buf;
1367 if (isalnum (cs->c))
1369 sprintf (buf, " -%c%s", cs->c, shortarg);
1370 p += strlen (p);
1372 if (cs->long_name != 0)
1374 unsigned int i;
1375 sprintf (p, "%s--%s%s",
1376 !isalnum (cs->c) ? " " : ", ",
1377 cs->long_name, longarg);
1378 p += strlen (p);
1379 for (i = 0; i < (sizeof (long_option_aliases) /
1380 sizeof (long_option_aliases[0]));
1381 ++i)
1382 if (long_option_aliases[i].val == cs->c)
1384 sprintf (p, ", --%s%s",
1385 long_option_aliases[i].name, longarg);
1386 p += strlen (p);
1390 const struct command_switch *ncs = cs;
1391 while ((++ncs)->c != '\0')
1392 if (ncs->description[0] == '-' &&
1393 ncs->description[1] == cs->c)
1395 /* This is another switch that does the same
1396 one as the one we are processing. We want
1397 to list them all together on one line. */
1398 sprintf (p, ", -%c%s", ncs->c, shortarg);
1399 p += strlen (p);
1400 if (ncs->long_name != 0)
1402 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1403 p += strlen (p);
1408 if (p - buf > DESCRIPTION_COLUMN - 2)
1409 /* The list of option names is too long to fit on the same
1410 line with the description, leaving at least two spaces.
1411 Print it on its own line instead. */
1413 fprintf (stderr, "%s\n", buf);
1414 buf[0] = '\0';
1417 fprintf (stderr, "%*s%s.\n",
1418 - DESCRIPTION_COLUMN,
1419 buf, cs->description);
1422 die (bad);
1426 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1427 We do this by chopping the value into a vector of words, prepending a
1428 dash to the first word if it lacks one, and passing the vector to
1429 decode_switches. */
1431 static void
1432 decode_env_switches (envar, len)
1433 char *envar;
1434 unsigned int len;
1436 char *varref = (char *) alloca (2 + len + 2);
1437 char *value, *args;
1438 int argc;
1439 char **argv;
1441 /* Get the variable's value. */
1442 varref[0] = '$';
1443 varref[1] = '(';
1444 bcopy (envar, &varref[2], len);
1445 varref[2 + len] = ')';
1446 varref[2 + len + 1] = '\0';
1447 value = variable_expand (varref);
1449 /* Skip whitespace, and check for an empty value. */
1450 value = next_token (value);
1451 len = strlen (value);
1452 if (len == 0)
1453 return;
1455 /* Make a copy of the value in ARGS, where we will munge it.
1456 If it does not begin with a dash, prepend one.
1457 We must allocate lasting storage for this (and we never free it) because
1458 decode_switches may save pointers into it for string-valued switches. */
1459 args = (char *) xmalloc (1 + len + 2);
1460 if (value[0] != '-')
1461 args[0] = '-';
1462 bcopy (value, value[0] == '-' ? args : &args[1], len + 1);
1463 /* Write an extra null terminator so our loop below will
1464 never be in danger of looking past the end of the string. */
1465 args[(value[0] == '-' ? 0 : 1) + len + 1] = '\0';
1467 /* Allocate a vector that is definitely big enough. */
1468 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1470 /* getopt will look at the arguments starting at ARGV[1].
1471 Prepend a spacer word. */
1472 argv[0] = 0;
1473 argc = 1;
1476 argv[argc++] = args;
1477 args = end_of_token (args);
1478 *args++ = '\0';
1479 } while (*args != '\0');
1480 argv[argc] = 0;
1482 /* Parse those words. */
1483 decode_switches (argc, argv, 1);
1486 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1487 command switches. Include options with args if ALL is nonzero.
1488 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1490 static void
1491 define_makeflags (all, makefile)
1492 int all, makefile;
1494 register const struct command_switch *cs;
1495 char *flagstring;
1497 /* We will construct a linked list of `struct flag's describing
1498 all the flags which need to go in MAKEFLAGS. Then, once we
1499 know how many there are and their lengths, we can put them all
1500 together in a string. */
1502 struct flag
1504 struct flag *next;
1505 const struct command_switch *cs;
1506 char *arg;
1507 unsigned int arglen;
1509 struct flag *flags = 0;
1510 unsigned int flagslen = 0;
1511 #define ADD_FLAG(ARG, LEN) \
1512 do { \
1513 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1514 new->cs = cs; \
1515 new->arg = (ARG); \
1516 new->arglen = (LEN); \
1517 new->next = flags; \
1518 flags = new; \
1519 if (new->arg == 0) \
1520 ++flagslen; /* Just a single flag letter. */ \
1521 else \
1522 flagslen += 1 + 1 + 1 + 1 + new->arglen; /* " -x foo" */ \
1523 if (!isalnum (cs->c)) \
1524 /* This switch has no single-letter version, so we use the long. */ \
1525 flagslen += 2 + strlen (cs->long_name); \
1526 } while (0)
1528 for (cs = switches; cs->c != '\0'; ++cs)
1529 if (cs->toenv && (!makefile || !cs->no_makefile))
1530 switch (cs->type)
1532 default:
1533 abort ();
1535 case ignore:
1536 break;
1538 case flag:
1539 case flag_off:
1540 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1541 && (cs->default_value == 0
1542 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1543 ADD_FLAG (0, 0);
1544 break;
1546 case positive_int:
1547 if (all)
1549 if ((cs->default_value != 0
1550 && (*(unsigned int *) cs->value_ptr
1551 == *(unsigned int *) cs->default_value)))
1552 break;
1553 else if (cs->noarg_value != 0
1554 && (*(unsigned int *) cs->value_ptr ==
1555 *(unsigned int *) cs->noarg_value))
1556 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1557 else if (cs->c == 'j')
1558 /* Special case for `-j'. */
1559 ADD_FLAG ("1", 1);
1560 else
1562 char *buf = (char *) alloca (30);
1563 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1564 ADD_FLAG (buf, strlen (buf));
1567 break;
1569 case floating:
1570 if (all)
1572 if (cs->default_value != 0
1573 && (*(double *) cs->value_ptr
1574 == *(double *) cs->default_value))
1575 break;
1576 else if (cs->noarg_value != 0
1577 && (*(double *) cs->value_ptr
1578 == *(double *) cs->noarg_value))
1579 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1580 else
1582 char *buf = (char *) alloca (100);
1583 sprintf (buf, "%g", *(double *) cs->value_ptr);
1584 ADD_FLAG (buf, strlen (buf));
1587 break;
1589 case string:
1590 if (all)
1592 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1593 if (sl != 0)
1595 /* Add the elements in reverse order, because
1596 all the flags get reversed below; and the order
1597 matters for some switches (like -I). */
1598 register unsigned int i = sl->idx;
1599 while (i-- > 0)
1600 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1603 break;
1606 #undef ADD_FLAG
1608 if (flags == 0)
1609 /* No flags. Use a string of two nulls so [1] works below. */
1610 flagstring = "\0";
1611 else
1613 /* Construct the value in FLAGSTRING.
1614 We allocate enough space for a preceding dash and trailing null. */
1615 register char *p;
1616 flagstring = (char *) alloca (1 + flagslen + 1);
1617 p = flagstring;
1618 *p++ = '-';
1621 /* Add the flag letter or name to the string. */
1622 if (!isalnum (flags->cs->c))
1624 *p++ = '-';
1625 strcpy (p, flags->cs->long_name);
1626 p += strlen (p);
1628 else
1629 *p++ = flags->cs->c;
1630 if (flags->arg != 0)
1632 /* A flag that takes an optional argument which in this case
1633 is omitted is specified by ARG being "" and ARGLEN being 0.
1634 We must distinguish because a following flag appended without
1635 an intervening " -" is considered the arg for the first. */
1636 if (flags->arglen > 0)
1638 /* Add its argument too. */
1639 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1640 bcopy (flags->arg, p, flags->arglen);
1641 p += flags->arglen;
1643 /* Write a following space and dash, for the next flag. */
1644 *p++ = ' ';
1645 *p++ = '-';
1647 else if (!isalnum (flags->cs->c))
1649 /* Long options must each go in their own word,
1650 so we write the following space and dash. */
1651 *p++ = ' ';
1652 *p++ = '-';
1654 flags = flags->next;
1655 } while (flags != 0);
1657 if (p[-1] == '-')
1658 /* Kill the final space and dash. */
1659 p -= 2;
1661 /* Terminate the string. */
1662 *p = '\0';
1665 define_variable ("MAKEFLAGS", 9,
1666 /* On Sun, the value of MFLAGS starts with a `-' but
1667 the value of MAKEFLAGS lacks the `-'.
1668 Be compatible with this unless FLAGSTRING starts
1669 with a long option `--foo', since removing the
1670 first dash would result in the bogus `-foo'. */
1671 flagstring[1] == '-' ? flagstring : &flagstring[1],
1672 /* This used to use o_env, but that lost when a
1673 makefile defined MAKEFLAGS. Makefiles set
1674 MAKEFLAGS to add switches, but we still want
1675 to redefine its value with the full set of
1676 switches. Of course, an override or command
1677 definition will still take precedence. */
1678 o_file, 0)
1679 /* Always export MAKEFLAGS. */
1680 ->export = v_export;
1681 /* Since MFLAGS is not parsed for flags, there is no reason to
1682 override any makefile redefinition. */
1683 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 0);
1686 /* Print version information. */
1688 static void
1689 print_version ()
1691 static int printed_version = 0;
1693 char *precede = print_data_base_flag ? "# " : "";
1695 if (printed_version)
1696 /* Do it only once. */
1697 return;
1699 printf ("%sGNU Make version %s", precede, version_string);
1700 if (remote_description != 0 && *remote_description != '\0')
1701 printf ("-%s", remote_description);
1703 printf (", by Richard Stallman and Roland McGrath.\n\
1704 %sCopyright (C) 1988, 89, 90, 91, 92, 93 Free Software Foundation, Inc.\n\
1705 %sThis is free software; see the source for copying conditions.\n\
1706 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1707 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1709 printed_version = 1;
1711 /* Flush stdout so the user doesn't have to wait to see the
1712 version information while things are thought about. */
1713 fflush (stdout);
1716 /* Print a bunch of information about this and that. */
1718 static void
1719 print_data_base ()
1721 extern char *ctime ();
1722 time_t when;
1724 when = time ((time_t *) 0);
1725 printf ("\n# Make data base, printed on %s", ctime (&when));
1727 print_variable_data_base ();
1728 print_dir_data_base ();
1729 print_rule_data_base ();
1730 print_file_data_base ();
1731 print_vpath_data_base ();
1733 when = time ((time_t *) 0);
1734 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1737 /* Exit with STATUS, cleaning up as necessary. */
1739 void
1740 die (status)
1741 int status;
1743 static char dying = 0;
1745 if (!dying)
1747 int err;
1749 dying = 1;
1751 if (print_version_flag)
1752 print_version ();
1754 /* Wait for children to die. */
1755 for (err = status != 0; job_slots_used > 0; err = 0)
1756 reap_children (1, err);
1758 /* Remove the intermediate files. */
1759 remove_intermediates (0);
1761 if (print_data_base_flag)
1762 print_data_base ();
1764 if (print_directory_flag)
1765 log_working_directory (0);
1768 exit (status);
1771 /* Write a message indicating that we've just entered or
1772 left (according to ENTERING) the current directory. */
1774 static void
1775 log_working_directory (entering)
1776 int entering;
1778 static int entered = 0;
1779 char *message = entering ? "Entering" : "Leaving";
1781 if (entering)
1782 entered = 1;
1783 else if (!entered)
1784 /* Don't print the leaving message if we
1785 haven't printed the entering message. */
1786 return;
1788 if (print_data_base_flag)
1789 fputs ("# ", stdout);
1791 if (makelevel == 0)
1792 printf ("%s: %s ", program, message);
1793 else
1794 printf ("%s[%u]: %s ", program, makelevel, message);
1796 if (starting_directory == 0)
1797 puts ("an unknown directory");
1798 else
1799 printf ("directory `%s'\n", starting_directory);