[__MSDOS__]: Fixed typo.
[make.git] / main.c
blob10f7eca1dcf990b568f08ab73c226e368b26a4dd
1 /* Argument parsing and main program of GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 94 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"
28 extern void print_variable_data_base ();
29 extern void print_dir_data_base ();
30 extern void print_rule_data_base ();
31 extern void print_file_data_base ();
32 extern void print_vpath_data_base ();
34 #ifndef HAVE_UNISTD_H
35 extern int chdir ();
36 #endif
37 #ifndef STDC_HEADERS
38 #ifndef sun /* Sun has an incorrect decl in a header. */
39 extern void exit ();
40 #endif
41 extern double atof ();
42 #endif
43 extern char *mktemp ();
45 static void log_working_directory ();
46 static void print_data_base (), print_version ();
47 static void decode_switches (), decode_env_switches ();
48 static void define_makeflags ();
50 /* The structure that describes an accepted command switch. */
52 struct command_switch
54 char c; /* The switch character. */
56 enum /* Type of the value. */
58 flag, /* Turn int flag on. */
59 flag_off, /* Turn int flag off. */
60 string, /* One string per switch. */
61 positive_int, /* A positive integer. */
62 floating, /* A floating-point number (double). */
63 ignore /* Ignored. */
64 } type;
66 char *value_ptr; /* Pointer to the value-holding variable. */
68 unsigned int env:1; /* Can come from MAKEFLAGS. */
69 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
70 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
72 char *noarg_value; /* Pointer to value used if no argument is given. */
73 char *default_value;/* Pointer to default value. */
75 char *long_name; /* Long option name. */
76 char *argdesc; /* Descriptive word for argument. */
77 char *description; /* Description for usage message. */
81 /* The structure used to hold the list of strings given
82 in command switches of a type that takes string arguments. */
84 struct stringlist
86 char **list; /* Nil-terminated list of strings. */
87 unsigned int idx; /* Index into above. */
88 unsigned int max; /* Number of pointers allocated. */
92 /* The recognized command switches. */
94 /* Nonzero means do not print commands to be executed (-s). */
96 int silent_flag;
98 /* Nonzero means just touch the files
99 that would appear to need remaking (-t) */
101 int touch_flag;
103 /* Nonzero means just print what commands would need to be executed,
104 don't actually execute them (-n). */
106 int just_print_flag;
108 /* Print debugging trace info (-d). */
110 int debug_flag = 0;
112 /* Environment variables override makefile definitions. */
114 int env_overrides = 0;
116 /* Nonzero means ignore status codes returned by commands
117 executed to remake files. Just treat them all as successful (-i). */
119 int ignore_errors_flag = 0;
121 /* Nonzero means don't remake anything, just print the data base
122 that results from reading the makefile (-p). */
124 int print_data_base_flag = 0;
126 /* Nonzero means don't remake anything; just return a nonzero status
127 if the specified targets are not up to date (-q). */
129 int question_flag = 0;
131 /* Nonzero means do not use any of the builtin rules (-r). */
133 int no_builtin_rules_flag = 0;
135 /* Nonzero means keep going even if remaking some file fails (-k). */
137 int keep_going_flag;
138 int default_keep_going_flag = 0;
140 /* Nonzero means print directory before starting and when done (-w). */
142 int print_directory_flag = 0;
144 /* Nonzero means ignore print_directory_flag and never print the directory.
145 This is necessary because print_directory_flag is set implicitly. */
147 int inhibit_print_directory_flag = 0;
149 /* Nonzero means print version information. */
151 int print_version_flag = 0;
153 /* List of makefiles given with -f switches. */
155 static struct stringlist *makefiles = 0;
158 /* Number of job slots (commands that can be run at once). */
160 unsigned int job_slots = 1;
161 unsigned int default_job_slots = 1;
163 /* Value of job_slots that means no limit. */
165 static unsigned int inf_jobs = 0;
167 /* Maximum load average at which multiple jobs will be run.
168 Negative values mean unlimited, while zero means limit to
169 zero load (which could be useful to start infinite jobs remotely
170 but one at a time locally). */
172 double max_load_average = -1.0;
173 double default_load_average = -1.0;
175 /* List of directories given with -C switches. */
177 static struct stringlist *directories = 0;
179 /* List of include directories given with -I switches. */
181 static struct stringlist *include_directories = 0;
183 /* List of files given with -o switches. */
185 static struct stringlist *old_files = 0;
187 /* List of files given with -W switches. */
189 static struct stringlist *new_files = 0;
191 /* If nonzero, we should just print usage and exit. */
193 static int print_usage_flag = 0;
195 /* If nonzero, we should print a warning message
196 for each reference to an undefined variable. */
198 int warn_undefined_variables_flag;
200 /* The table of command switches. */
202 static const struct command_switch switches[] =
204 { 'b', ignore, 0, 0, 0, 0, 0, 0,
205 0, 0,
206 "Ignored for compatibility" },
207 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
208 "directory", "DIRECTORY",
209 "Change to DIRECTORY before doing anything" },
210 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
211 "debug", 0,
212 "Print lots of debugging information" },
213 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
214 "environment-overrides", 0,
215 "Environment variables override makefiles" },
216 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
217 "file", "FILE",
218 "Read FILE as a makefile" },
219 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
220 "help", 0,
221 "Print this message and exit" },
222 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
223 "ignore-errors", 0,
224 "Ignore errors from commands" },
225 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
226 "include-dir", "DIRECTORY",
227 "Search DIRECTORY for included makefiles" },
228 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
229 (char *) &inf_jobs, (char *) &default_job_slots,
230 "jobs", "N",
231 "Allow N jobs at once; infinite jobs with no arg" },
232 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
233 0, (char *) &default_keep_going_flag,
234 "keep-going", 0,
235 "Keep going when some targets can't be made" },
236 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
237 (char *) &default_load_average, (char *) &default_load_average,
238 "load-average", "N",
239 "Don't start multiple jobs unless load is below N" },
240 { 'm', ignore, 0, 0, 0, 0, 0, 0,
241 0, 0,
242 "-b" },
243 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
244 "just-print", 0,
245 "Don't actually run any commands; just print them" },
246 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
247 "old-file", "FILE",
248 "Consider FILE to be very old and don't remake it" },
249 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
250 "print-data-base", 0,
251 "Print make's internal database" },
252 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
253 "question", 0,
254 "Run no commands; exit status says if up to date" },
255 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
256 "no-builtin-rules", 0,
257 "Disable the built-in implicit rules" },
258 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
259 "silent", 0,
260 "Don't echo commands" },
261 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
262 0, (char *) &default_keep_going_flag,
263 "no-keep-going", 0,
264 "Turns off -k" },
265 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
266 "touch", 0,
267 "Touch targets instead of remaking them" },
268 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
269 "version", 0,
270 "Print the version number of make and exit" },
271 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
272 "print-directory", 0,
273 "Print the current directory" },
274 { 1, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
275 "no-print-directory", 0,
276 "Turn off -w, even if it was turned on implicitly" },
277 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
278 "what-if", "FILE",
279 "Consider FILE to be infinitely new" },
280 { 2, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
281 "warn-undefined-variables", 0,
282 "Warn when an undefined variable is referenced" },
283 { '\0', }
286 /* Secondary long names for options. */
288 static struct option long_option_aliases[] =
290 { "quiet", no_argument, 0, 's' },
291 { "stop", no_argument, 0, 'S' },
292 { "new-file", required_argument, 0, 'W' },
293 { "assume-new", required_argument, 0, 'W' },
294 { "assume-old", required_argument, 0, 'o' },
295 { "max-load", optional_argument, 0, 'l' },
296 { "dry-run", no_argument, 0, 'n' },
297 { "recon", no_argument, 0, 'n' },
298 { "makefile", required_argument, 0, 'f' },
301 /* The usage message prints the descriptions of options starting in
302 this column. Make sure it leaves enough room for the longest
303 description to fit in less than 80 characters. */
305 #define DESCRIPTION_COLUMN 30
307 /* List of goal targets. */
309 static struct dep *goals, *lastgoal;
311 /* List of variables which were defined on the command line
312 (or, equivalently, in MAKEFLAGS). */
314 struct command_variable
316 struct command_variable *next;
317 struct variable *variable;
319 static struct command_variable *command_variables;
321 /* The name we were invoked with. */
323 char *program;
325 /* Our current directory before processing any -C options. */
327 char *directory_before_chdir;
329 /* Our current directory after processing all -C options. */
331 char *starting_directory;
333 /* Value of the MAKELEVEL variable at startup (or 0). */
335 unsigned int makelevel;
337 /* First file defined in the makefile whose name does not
338 start with `.'. This is the default to remake if the
339 command line does not specify. */
341 struct file *default_goal_file;
343 /* Pointer to structure for the file .DEFAULT
344 whose commands are used for any file that has none of its own.
345 This is zero if the makefiles do not define .DEFAULT. */
347 struct file *default_file;
349 /* Mask of signals that are being caught with fatal_error_signal. */
351 #ifdef POSIX
352 sigset_t fatal_signal_set;
353 #else
354 #ifdef HAVE_SIGSETMASK
355 int fatal_signal_mask;
356 #endif
357 #endif
359 static struct file *
360 enter_command_line_file (name)
361 char *name;
363 if (name[0] == '~')
365 char *expanded = tilde_expand (name);
366 if (expanded != 0)
367 name = expanded; /* Memory leak; I don't care. */
370 /* This is also done in parse_file_seq, so this is redundant
371 for names read from makefiles. It is here for names passed
372 on the command line. */
373 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
375 name += 2;
376 while (*name == '/')
377 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
378 ++name;
381 if (*name == '\0')
383 /* It was all slashes! Move back to the dot and truncate
384 it after the first slash, so it becomes just "./". */
386 --name;
387 while (name[0] != '.');
388 name[2] = '\0';
391 return enter_file (savestring (name, strlen (name)));
395 main (argc, argv, envp)
396 int argc;
397 char **argv;
398 char **envp;
400 extern void init_dir ();
401 extern RETSIGTYPE fatal_error_signal (), child_handler ();
402 register struct file *f;
403 register unsigned int i;
404 char **p;
405 struct dep *read_makefiles;
406 PATH_VAR (current_directory);
408 default_goal_file = 0;
409 reading_filename = 0;
410 reading_lineno_ptr = 0;
412 #ifndef HAVE_SYS_SIGLIST
413 signame_init ();
414 #endif
416 #ifdef POSIX
417 sigemptyset (&fatal_signal_set);
418 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
419 #else
420 #ifdef HAVE_SIGSETMASK
421 fatal_signal_mask = 0;
422 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
423 #else
424 #define ADD_SIG(sig)
425 #endif
426 #endif
428 #define FATAL_SIG(sig) \
429 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
430 (void) signal ((sig), SIG_IGN); \
431 else \
432 ADD_SIG (sig);
434 FATAL_SIG (SIGHUP);
435 FATAL_SIG (SIGQUIT);
436 FATAL_SIG (SIGINT);
437 FATAL_SIG (SIGTERM);
439 #ifdef SIGDANGER
440 FATAL_SIG (SIGDANGER);
441 #endif
442 #ifdef SIGXCPU
443 FATAL_SIG (SIGXCPU);
444 #endif
445 #ifdef SIGXFSZ
446 FATAL_SIG (SIGXFSZ);
447 #endif
449 #undef FATAL_SIG
451 /* Make sure stdout is line-buffered. */
453 #ifdef HAVE_SETLINEBUF
454 setlinebuf (stdout);
455 #else
456 #ifndef SETVBUF_REVERSED
457 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
458 #else /* setvbuf not reversed. */
459 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
460 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
461 #endif /* setvbuf reversed. */
462 #endif /* setlinebuf missing. */
464 /* Initialize the directory hashing code. */
465 init_dir ();
467 /* Figure out where this program lives. */
469 if (argv[0] == 0)
470 argv[0] = "";
471 if (argv[0][0] == '\0')
472 program = "make";
473 else
475 program = rindex (argv[0], '/');
476 #ifdef __MSDOS__
477 if (program == 0)
478 program = rindex (argv[0], '\\');
479 if (program == 0)
480 program = rindex (argv[0], ':');
481 #endif
482 if (program == 0)
483 program = argv[0];
484 else
485 ++program;
488 /* Set up to access user data (files). */
489 user_access ();
491 /* Figure out where we are. */
493 if (getcwd (current_directory, GET_PATH_MAX) == 0)
495 #ifdef HAVE_GETCWD
496 perror_with_name ("getcwd: ", "");
497 #else
498 error ("getwd: %s", current_directory);
499 #endif
500 current_directory[0] = '\0';
501 directory_before_chdir = 0;
503 else
504 directory_before_chdir = savestring (current_directory,
505 strlen (current_directory));
507 /* Read in variables from the environment. It is important that this be
508 done before $(MAKE) is are figured out so its definitions will not be
509 one from the environment. */
511 for (i = 0; envp[i] != 0; ++i)
513 register char *ep = envp[i];
514 while (*ep != '=')
515 ++ep;
516 /* The result of pointer arithmetic is cast to unsigned int for
517 machines where ptrdiff_t is a different size that doesn't widen
518 the same. */
519 define_variable (envp[i], (unsigned int) (ep - envp[i]),
520 ep + 1, o_env, 1)
521 /* Force exportation of every variable culled from the environment.
522 We used to rely on target_environment's v_default code to do this.
523 But that does not work for the case where an environment variable
524 is redefined in a makefile with `override'; it should then still
525 be exported, because it was originally in the environment. */
526 ->export = v_export;
529 /* Decode the switches. */
531 decode_env_switches ("MAKEFLAGS", 9);
532 #if 0
533 /* People write things like:
534 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
535 and we set the -p, -i and -e switches. Doesn't seem quite right. */
536 decode_env_switches ("MFLAGS", 6);
537 #endif
538 decode_switches (argc, argv, 0);
540 /* Print version information. */
542 if (print_version_flag || print_data_base_flag || debug_flag)
543 print_version ();
545 /* `make --version' is supposed to just print the version and exit. */
546 if (print_version_flag)
547 die (0);
549 #ifndef __MSDOS__
550 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
551 (If it is a relative pathname with a slash, prepend our directory name
552 so the result will run the same program regardless of the current dir.
553 If it is a name with no slash, we can only hope that PATH did not
554 find it in the current directory.) */
556 if (current_directory[0] != '\0'
557 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
558 argv[0] = concat (current_directory, "/", argv[0]);
559 #endif
561 /* The extra indirection through $(MAKE_COMMAND) is done
562 for hysterical raisins. */
563 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
564 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
567 /* If there were -C flags, move ourselves about. */
568 if (directories != 0)
569 for (i = 0; directories->list[i] != 0; ++i)
571 char *dir = directories->list[i];
572 if (dir[0] == '~')
574 char *expanded = tilde_expand (dir);
575 if (expanded != 0)
576 dir = expanded;
578 if (chdir (dir) < 0)
579 pfatal_with_name (dir);
580 if (dir != directories->list[i])
581 free (dir);
584 /* Figure out the level of recursion. */
586 struct variable *v = lookup_variable ("MAKELEVEL", 9);
587 if (v != 0 && *v->value != '\0' && *v->value != '-')
588 makelevel = (unsigned int) atoi (v->value);
589 else
590 makelevel = 0;
593 /* Except under -s, always do -w in sub-makes and under -C. */
594 if (!silent_flag && (directories != 0 || makelevel > 0))
595 print_directory_flag = 1;
597 /* Let the user disable that with --no-print-directory. */
598 if (inhibit_print_directory_flag)
599 print_directory_flag = 0;
601 /* Construct the list of include directories to search. */
603 construct_include_path (include_directories == 0 ? (char **) 0
604 : include_directories->list);
606 /* Figure out where we are now, after chdir'ing. */
607 if (directories == 0)
608 /* We didn't move, so we're still in the same place. */
609 starting_directory = current_directory;
610 else
612 if (getcwd (current_directory, GET_PATH_MAX) == 0)
614 #ifdef HAVE_GETCWD
615 perror_with_name ("getcwd: ", "");
616 #else
617 error ("getwd: %s", current_directory);
618 #endif
619 starting_directory = 0;
621 else
622 starting_directory = current_directory;
625 /* Tell the user where he is. */
627 if (print_directory_flag)
628 log_working_directory (1);
630 /* Read any stdin makefiles into temporary files. */
632 if (makefiles != 0)
634 register unsigned int i;
635 for (i = 0; i < makefiles->idx; ++i)
636 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
638 /* This makefile is standard input. Since we may re-exec
639 and thus re-read the makefiles, we read standard input
640 into a temporary file and read from that. */
641 static char name[] = "/tmp/GmXXXXXX";
642 FILE *outfile;
644 /* Make a unique filename. */
645 (void) mktemp (name);
647 outfile = fopen (name, "w");
648 if (outfile == 0)
649 pfatal_with_name ("fopen (temporary file)");
650 while (!feof (stdin))
652 char buf[2048];
653 int n = fread (buf, 1, sizeof(buf), stdin);
654 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
655 pfatal_with_name ("fwrite (temporary file)");
657 /* Try to make sure we won't remake the temporary
658 file when we are re-exec'd. Kludge-o-matic! */
659 fprintf (outfile, "%s:;\n", name);
660 (void) fclose (outfile);
662 /* Replace the name that read_all_makefiles will
663 see with the name of the temporary file. */
665 char *temp;
666 /* SGI compiler requires alloca's result be assigned simply. */
667 temp = (char *) alloca (sizeof (name));
668 bcopy (name, temp, sizeof (name));
669 makefiles->list[i] = temp;
672 /* Make sure the temporary file will not be remade. */
673 f = enter_file (savestring (name, sizeof name - 1));
674 f->updated = 1;
675 f->update_status = 0;
676 f->command_state = cs_finished;
677 /* Let it be removed when we're done. */
678 f->intermediate = 1;
679 /* But don't mention it. */
680 f->dontcare = 1;
684 /* Set up to handle children dying. This must be done before
685 reading in the makefiles so that `shell' function calls will work. */
687 #ifdef SIGCHLD
688 (void) signal (SIGCHLD, child_handler);
689 #endif
690 #ifdef SIGCLD
691 (void) signal (SIGCLD, child_handler);
692 #endif
694 /* Define the initial list of suffixes for old-style rules. */
696 set_default_suffixes ();
698 /* Define the file rules for the built-in suffix rules. These will later
699 be converted into pattern rules. We used to do this in
700 install_default_implicit_rules, but since that happens after reading
701 makefiles, it results in the built-in pattern rules taking precedence
702 over makefile-specified suffix rules, which is wrong. */
704 install_default_suffix_rules ();
706 /* Define some internal and special variables. */
708 define_automatic_variables ();
710 /* Set up the MAKEFLAGS and MFLAGS variables
711 so makefiles can look at them. */
713 define_makeflags (0, 0);
715 /* Define the default variables. */
716 define_default_variables ();
718 /* Read all the makefiles. */
720 default_file = enter_file (".DEFAULT");
722 read_makefiles
723 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
725 /* Decode switches again, in case the variables were set by the makefile. */
726 decode_env_switches ("MAKEFLAGS", 9);
727 #if 0
728 decode_env_switches ("MFLAGS", 6);
729 #endif
731 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
733 define_makeflags (1, 0);
735 ignore_errors_flag |= lookup_file (".IGNORE") != 0;
737 silent_flag |= lookup_file (".SILENT") != 0;
739 /* Make each `struct dep' point at the
740 `struct file' for the file depended on. */
742 snap_deps ();
744 /* Convert old-style suffix rules to pattern rules. It is important to
745 do this before installing the built-in pattern rules below, so that
746 makefile-specified suffix rules take precedence over built-in pattern
747 rules. */
749 convert_to_pattern ();
751 /* Install the default implicit pattern rules.
752 This used to be done before reading the makefiles.
753 But in that case, built-in pattern rules were in the chain
754 before user-defined ones, so they matched first. */
756 install_default_implicit_rules ();
758 /* Compute implicit rule limits. */
760 count_implicit_rule_limits ();
762 /* Construct the listings of directories in VPATH lists. */
764 build_vpath_lists ();
766 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
767 and as having been updated already, and files given with -W flags as
768 brand new (time-stamp as far as possible into the future). */
770 if (old_files != 0)
771 for (p = old_files->list; *p != 0; ++p)
773 f = enter_command_line_file (*p);
774 f->last_mtime = (time_t) 1;
775 f->updated = 1;
776 f->update_status = 0;
777 f->command_state = cs_finished;
780 if (new_files != 0)
782 for (p = new_files->list; *p != 0; ++p)
784 f = enter_command_line_file (*p);
785 f->last_mtime = NEW_MTIME;
789 if (read_makefiles != 0)
791 /* Update any makefiles if necessary. */
793 time_t *makefile_mtimes = 0;
794 unsigned int mm_idx = 0;
796 if (debug_flag)
797 puts ("Updating makefiles....");
799 /* Remove any makefiles we don't want to try to update.
800 Also record the current modtimes so we can compare them later. */
802 register struct dep *d, *last;
803 last = 0;
804 d = read_makefiles;
805 while (d != 0)
807 register struct file *f = d->file;
808 if (f->double_colon)
809 for (f = f->double_colon; f != NULL; f = f->prev)
811 if (f->deps == 0 && f->cmds != 0)
813 /* This makefile is a :: target with commands, but
814 no dependencies. So, it will always be remade.
815 This might well cause an infinite loop, so don't
816 try to remake it. (This will only happen if
817 your makefiles are written exceptionally
818 stupidly; but if you work for Athena, that's how
819 you write your makefiles.) */
821 if (debug_flag)
822 printf ("Makefile `%s' might loop; not remaking it.\n",
823 f->name);
825 if (last == 0)
826 read_makefiles = d->next;
827 else
828 last->next = d->next;
830 /* Free the storage. */
831 free ((char *) d);
833 d = last == 0 ? 0 : last->next;
835 break;
838 if (f == NULL || !f->double_colon)
840 if (makefile_mtimes == 0)
841 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
842 else
843 makefile_mtimes = (time_t *)
844 xrealloc ((char *) makefile_mtimes,
845 (mm_idx + 1) * sizeof (time_t));
846 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
847 last = d;
848 d = d->next;
853 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
854 define_makeflags (1, 1);
856 switch (update_goal_chain (read_makefiles, 1))
858 default:
859 abort ();
861 case -1:
862 /* Did nothing. */
863 break;
865 case 1:
866 /* Failed to update. Figure out if we care. */
868 /* Nonzero if any makefile was successfully remade. */
869 int any_remade = 0;
870 /* Nonzero if any makefile we care about failed
871 in updating or could not be found at all. */
872 int any_failed = 0;
873 register unsigned int i;
875 for (i = 0; read_makefiles != 0; ++i)
877 struct dep *d = read_makefiles;
878 read_makefiles = d->next;
879 if (d->file->updated)
881 /* This makefile was updated. */
882 if (d->file->update_status == 0)
884 /* It was successfully updated. */
885 any_remade |= (file_mtime_no_search (d->file)
886 != makefile_mtimes[i]);
888 else if (! (d->changed & RM_DONTCARE))
890 time_t mtime;
891 /* The update failed and this makefile was not
892 from the MAKEFILES variable, so we care. */
893 error ("Failed to remake makefile `%s'.",
894 d->file->name);
895 mtime = file_mtime_no_search (d->file);
896 any_remade |= (mtime != (time_t) -1
897 && mtime != makefile_mtimes[i]);
900 else
901 /* This makefile was not found at all. */
902 if (! (d->changed & RM_DONTCARE))
904 /* This is a makefile we care about. See how much. */
905 if (d->changed & RM_INCLUDED)
906 /* An included makefile. We don't need
907 to die, but we do want to complain. */
908 error ("Included makefile `%s' was not found.",
909 dep_name (d));
910 else
912 /* A normal makefile. We must die later. */
913 error ("Makefile `%s' was not found", dep_name (d));
914 any_failed = 1;
918 free ((char *) d);
921 if (any_remade)
922 goto re_exec;
923 else if (any_failed)
924 die (2);
925 else
926 break;
929 case 0:
930 re_exec:
931 /* Updated successfully. Re-exec ourselves. */
933 remove_intermediates (0);
935 if (print_data_base_flag)
936 print_data_base ();
938 if (print_directory_flag)
939 log_working_directory (0);
941 if (makefiles != 0)
943 /* These names might have changed. */
944 register unsigned int i, j = 0;
945 for (i = 1; i < argc; ++i)
946 if (!strcmp (argv[i], "-f")) /* XXX */
948 char *p = &argv[i][2];
949 if (*p == '\0')
950 argv[++i] = makefiles->list[j];
951 else
952 argv[i] = concat ("-f", makefiles->list[j], "");
953 ++j;
957 if (directories != 0 && directories->idx > 0)
959 char bad;
960 if (directory_before_chdir != 0)
962 if (chdir (directory_before_chdir) < 0)
964 perror_with_name ("chdir", "");
965 bad = 1;
967 else
968 bad = 0;
970 else
971 bad = 1;
972 if (bad)
973 fatal ("Couldn't change back to original directory.");
976 for (p = environ; *p != 0; ++p)
977 if (!strncmp (*p, "MAKELEVEL=", 10))
979 /* The SGI compiler apparently can't understand
980 the concept of storing the result of a function
981 in something other than a local variable. */
982 char *sgi_loses;
983 sgi_loses = (char *) alloca (40);
984 *p = sgi_loses;
985 sprintf (*p, "MAKELEVEL=%u", makelevel);
986 break;
989 if (debug_flag)
991 char **p;
992 fputs ("Re-executing:", stdout);
993 for (p = argv; *p != 0; ++p)
994 printf (" %s", *p);
995 puts ("");
998 fflush (stdout);
999 fflush (stderr);
1001 exec_command (argv, environ);
1002 /* NOTREACHED */
1006 /* Set up `MAKEFLAGS' again for the normal targets. */
1007 define_makeflags (1, 0);
1010 int status;
1012 /* If there were no command-line goals, use the default. */
1013 if (goals == 0)
1015 if (default_goal_file != 0)
1017 goals = (struct dep *) xmalloc (sizeof (struct dep));
1018 goals->next = 0;
1019 goals->name = 0;
1020 goals->file = default_goal_file;
1023 else
1024 lastgoal->next = 0;
1026 if (goals != 0)
1028 /* Update the goals. */
1030 if (debug_flag)
1031 puts ("Updating goal targets....");
1033 switch (update_goal_chain (goals, 0))
1035 case -1:
1036 /* Nothing happened. */
1037 case 0:
1038 /* Updated successfully. */
1039 status = 0;
1040 break;
1041 case 2:
1042 /* Updating failed. */
1043 status = 2;
1044 break;
1045 case 1:
1046 /* We are under -q and would run some commands. */
1047 status = 1;
1048 break;
1049 default:
1050 abort ();
1053 else
1055 if (read_makefiles == 0)
1056 fatal ("No targets specified and no makefile found");
1057 else
1058 fatal ("No targets");
1061 /* Exit. */
1062 die (status);
1065 return 0;
1068 /* Parsing of arguments, decoding of switches. */
1070 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1071 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1072 (sizeof (long_option_aliases) /
1073 sizeof (long_option_aliases[0]))];
1075 /* Fill in the string and vector for getopt. */
1076 static void
1077 init_switches ()
1079 register char *p;
1080 register int c;
1081 register unsigned int i;
1083 if (options[0] != '\0')
1084 /* Already done. */
1085 return;
1087 p = options;
1089 /* Return switch and non-switch args in order, regardless of
1090 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1091 *p++ = '-';
1093 for (i = 0; switches[i].c != '\0'; ++i)
1095 long_options[i].name = (switches[i].long_name == 0 ? "" :
1096 switches[i].long_name);
1097 long_options[i].flag = 0;
1098 long_options[i].val = switches[i].c;
1099 if (isalnum (switches[i].c))
1100 *p++ = switches[i].c;
1101 switch (switches[i].type)
1103 case flag:
1104 case flag_off:
1105 case ignore:
1106 long_options[i].has_arg = no_argument;
1107 break;
1109 case string:
1110 case positive_int:
1111 case floating:
1112 if (isalnum (switches[i].c))
1113 *p++ = ':';
1114 if (switches[i].noarg_value != 0)
1116 if (isalnum (switches[i].c))
1117 *p++ = ':';
1118 long_options[i].has_arg = optional_argument;
1120 else
1121 long_options[i].has_arg = required_argument;
1122 break;
1125 *p = '\0';
1126 for (c = 0; c < (sizeof (long_option_aliases) /
1127 sizeof (long_option_aliases[0]));
1128 ++c)
1129 long_options[i++] = long_option_aliases[c];
1130 long_options[i].name = 0;
1133 /* Decode switches from ARGC and ARGV.
1134 They came from the environment if ENV is nonzero. */
1136 static void
1137 decode_switches (argc, argv, env)
1138 int argc;
1139 char **argv;
1140 int env;
1142 int bad = 0;
1143 register const struct command_switch *cs;
1144 register struct stringlist *sl;
1145 register int c;
1147 /* getopt does most of the parsing for us.
1148 First, get its vectors set up. */
1150 init_switches ();
1152 /* Let getopt produce error messages for the command line,
1153 but not for options from the environment. */
1154 opterr = !env;
1155 /* Reset getopt's state. */
1156 optind = 0;
1158 c = 0;
1159 while (optind < argc)
1161 if (c == EOF)
1163 /* There are no more options according to getting getopt, but
1164 there are some arguments left. Since we have asked for
1165 non-option arguments to be returned in order, I think this
1166 only happens when there is a "--" argument to prevent later
1167 argument from being options. Since getopt has finished its
1168 job, just update its state variables for the next argument and
1169 set C as if it had returned 1, indicating a non-option
1170 argument. */
1171 optarg = argv[optind++];
1172 c = 1;
1174 else
1175 /* Parse the next argument. */
1176 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1178 if (c == 1)
1180 /* Non-option argument. It might be a variable definition. */
1181 struct variable *v;
1182 v = try_variable_definition ((char *) 0, 0, optarg, o_command);
1183 if (v != 0)
1185 /* It is indeed a variable definition. Record a pointer to
1186 the variable for later use in define_makeflags. */
1187 struct command_variable *cv
1188 = (struct command_variable *) xmalloc (sizeof (*cv));
1189 cv->variable = v;
1190 cv->next = command_variables;
1191 command_variables = cv;
1193 else if (! env)
1195 /* Not an option or variable definition; it must be a goal
1196 target! Enter it as a file and add it to the dep chain of
1197 goals. */
1198 struct file *f = enter_command_line_file (optarg);
1199 f->cmd_target = 1;
1201 if (goals == 0)
1203 goals = (struct dep *) xmalloc (sizeof (struct dep));
1204 lastgoal = goals;
1206 else
1208 lastgoal->next
1209 = (struct dep *) xmalloc (sizeof (struct dep));
1210 lastgoal = lastgoal->next;
1212 lastgoal->name = 0;
1213 lastgoal->file = f;
1216 else if (c == '?')
1217 /* Bad option. We will print a usage message and die later.
1218 But continue to parse the other options so the user can
1219 see all he did wrong. */
1220 bad = 1;
1221 else
1222 for (cs = switches; cs->c != '\0'; ++cs)
1223 if (cs->c == c)
1225 /* Whether or not we will actually do anything with
1226 this switch. We test this individually inside the
1227 switch below rather than just once outside it, so that
1228 options which are to be ignored still consume args. */
1229 int doit = !env || cs->env;
1231 switch (cs->type)
1233 default:
1234 abort ();
1236 case ignore:
1237 break;
1239 case flag:
1240 case flag_off:
1241 if (doit)
1242 *(int *) cs->value_ptr = cs->type == flag;
1243 break;
1245 case string:
1246 if (!doit)
1247 break;
1249 if (optarg == 0)
1250 optarg = cs->noarg_value;
1252 sl = *(struct stringlist **) cs->value_ptr;
1253 if (sl == 0)
1255 sl = (struct stringlist *)
1256 xmalloc (sizeof (struct stringlist));
1257 sl->max = 5;
1258 sl->idx = 0;
1259 sl->list = (char **) xmalloc (5 * sizeof (char *));
1260 *(struct stringlist **) cs->value_ptr = sl;
1262 else if (sl->idx == sl->max - 1)
1264 sl->max += 5;
1265 sl->list = (char **)
1266 xrealloc ((char *) sl->list,
1267 sl->max * sizeof (char *));
1269 sl->list[sl->idx++] = optarg;
1270 sl->list[sl->idx] = 0;
1271 break;
1273 case positive_int:
1274 if (optarg == 0 && argc > optind
1275 && isdigit (argv[optind][0]))
1276 optarg = argv[optind++];
1278 if (!doit)
1279 break;
1281 if (optarg != 0)
1283 int i = atoi (optarg);
1284 if (i < 1)
1286 if (doit)
1287 error ("the `-%c' option requires a \
1288 positive integral argument",
1289 cs->c);
1290 bad = 1;
1292 else
1293 *(unsigned int *) cs->value_ptr = i;
1295 else
1296 *(unsigned int *) cs->value_ptr
1297 = *(unsigned int *) cs->noarg_value;
1298 break;
1300 case floating:
1301 if (optarg == 0 && optind < argc
1302 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1303 optarg = argv[optind++];
1305 if (doit)
1306 *(double *) cs->value_ptr
1307 = (optarg != 0 ? atof (optarg)
1308 : *(double *) cs->noarg_value);
1310 break;
1313 /* We've found the switch. Stop looking. */
1314 break;
1318 if (!env && (bad || print_usage_flag))
1320 /* Print a nice usage message. */
1322 if (print_version_flag)
1323 print_version ();
1325 fprintf (stderr, "Usage: %s [options] [target] ...\n", program);
1327 fputs ("Options:\n", stderr);
1328 for (cs = switches; cs->c != '\0'; ++cs)
1330 char buf[1024], shortarg[50], longarg[50], *p;
1332 if (cs->description[0] == '-')
1333 continue;
1335 switch (long_options[cs - switches].has_arg)
1337 case no_argument:
1338 shortarg[0] = longarg[0] = '\0';
1339 break;
1340 case required_argument:
1341 sprintf (longarg, "=%s", cs->argdesc);
1342 sprintf (shortarg, " %s", cs->argdesc);
1343 break;
1344 case optional_argument:
1345 sprintf (longarg, "[=%s]", cs->argdesc);
1346 sprintf (shortarg, " [%s]", cs->argdesc);
1347 break;
1350 p = buf;
1352 if (isalnum (cs->c))
1354 sprintf (buf, " -%c%s", cs->c, shortarg);
1355 p += strlen (p);
1357 if (cs->long_name != 0)
1359 unsigned int i;
1360 sprintf (p, "%s--%s%s",
1361 !isalnum (cs->c) ? " " : ", ",
1362 cs->long_name, longarg);
1363 p += strlen (p);
1364 for (i = 0; i < (sizeof (long_option_aliases) /
1365 sizeof (long_option_aliases[0]));
1366 ++i)
1367 if (long_option_aliases[i].val == cs->c)
1369 sprintf (p, ", --%s%s",
1370 long_option_aliases[i].name, longarg);
1371 p += strlen (p);
1375 const struct command_switch *ncs = cs;
1376 while ((++ncs)->c != '\0')
1377 if (ncs->description[0] == '-' &&
1378 ncs->description[1] == cs->c)
1380 /* This is another switch that does the same
1381 one as the one we are processing. We want
1382 to list them all together on one line. */
1383 sprintf (p, ", -%c%s", ncs->c, shortarg);
1384 p += strlen (p);
1385 if (ncs->long_name != 0)
1387 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1388 p += strlen (p);
1393 if (p - buf > DESCRIPTION_COLUMN - 2)
1394 /* The list of option names is too long to fit on the same
1395 line with the description, leaving at least two spaces.
1396 Print it on its own line instead. */
1398 fprintf (stderr, "%s\n", buf);
1399 buf[0] = '\0';
1402 fprintf (stderr, "%*s%s.\n",
1403 - DESCRIPTION_COLUMN,
1404 buf, cs->description);
1407 die (bad ? 2 : 0);
1411 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1412 We do this by chopping the value into a vector of words, prepending a
1413 dash to the first word if it lacks one, and passing the vector to
1414 decode_switches. */
1416 static void
1417 decode_env_switches (envar, len)
1418 char *envar;
1419 unsigned int len;
1421 char *varref = (char *) alloca (2 + len + 2);
1422 char *value, *p;
1423 int argc;
1424 char **argv;
1426 /* Get the variable's value. */
1427 varref[0] = '$';
1428 varref[1] = '(';
1429 bcopy (envar, &varref[2], len);
1430 varref[2 + len] = ')';
1431 varref[2 + len + 1] = '\0';
1432 value = variable_expand (varref);
1434 /* Skip whitespace, and check for an empty value. */
1435 value = next_token (value);
1436 len = strlen (value);
1437 if (len == 0)
1438 return;
1440 /* Allocate a vector that is definitely big enough. */
1441 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1443 /* Allocate a buffer to copy the value into while we split it into words
1444 and unquote it. We must use permanent storage for this because
1445 decode_switches may store pointers into the passed argument words. */
1446 p = (char *) xmalloc (2 * len);
1448 /* getopt will look at the arguments starting at ARGV[1].
1449 Prepend a spacer word. */
1450 argv[0] = 0;
1451 argc = 1;
1452 argv[argc] = p;
1453 while (*value != '\0')
1455 if (*value == '\\')
1456 ++value; /* Skip the backslash. */
1457 else if (isblank (*value))
1459 /* End of the word. */
1460 *p++ = '\0';
1461 argv[++argc] = p;
1463 ++value;
1464 while (isblank (*value));
1465 continue;
1467 *p++ = *value++;
1469 *p = '\0';
1470 argv[++argc] = 0;
1472 if (argc == 2 && argv[1][0] != '-')
1474 /* There is just one word in the value, and it is not a switch.
1475 Either this is the single-word form and we should prepend a dash
1476 before calling decode_switches, or this is the multi-word form and
1477 there is no dash because it is a variable definition. */
1478 struct variable *v;
1479 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1480 if (v != 0)
1482 /* It was indeed a variable definition, and now it has been
1483 processed. There is nothing for decode_switches to do.
1484 Record a pointer to the variable for later use in
1485 define_makeflags. */
1486 struct command_variable *cv
1487 = (struct command_variable *) xmalloc (sizeof (*cv));
1488 cv->variable = v;
1489 cv->next = command_variables;
1490 command_variables = cv;
1491 return;
1494 /* It wasn't a variable definition, so it's some switches without a
1495 leading dash. Add one and pass it along to decode_switches. We
1496 need permanent storage for this in case decode_switches saves
1497 pointers into the value. */
1498 argv[1] = concat ("-", argv[1], "");
1501 /* Parse those words. */
1502 decode_switches (argc, argv, 1);
1505 /* Quote the string IN so that it will be interpreted as a single word with
1506 no magic by the shell. Write the result into OUT, returning the address
1507 of the next character to be written. Allocating space for OUT twice the
1508 length of IN is always sufficient. */
1510 static
1511 #ifdef __GNUC__
1512 __inline__
1513 #endif
1514 char *
1515 shell_quote (out, in)
1516 char *out, *in;
1518 while (*in != '\0')
1520 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1521 *out++ = '\\';
1522 *out++ = *in++;
1525 return out;
1528 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1529 command switches. Include options with args if ALL is nonzero.
1530 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1532 static void
1533 define_makeflags (all, makefile)
1534 int all, makefile;
1536 register const struct command_switch *cs;
1537 char *flagstring;
1538 register char *p;
1539 unsigned int words;
1540 struct variable *v;
1541 struct command_variable *cv;
1543 /* We will construct a linked list of `struct flag's describing
1544 all the flags which need to go in MAKEFLAGS. Then, once we
1545 know how many there are and their lengths, we can put them all
1546 together in a string. */
1548 struct flag
1550 struct flag *next;
1551 const struct command_switch *cs;
1552 char *arg;
1553 unsigned int arglen;
1555 struct flag *flags = 0;
1556 unsigned int flagslen = 0;
1557 #define ADD_FLAG(ARG, LEN) \
1558 do { \
1559 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1560 new->cs = cs; \
1561 new->arg = (ARG); \
1562 new->arglen = (LEN); \
1563 new->next = flags; \
1564 flags = new; \
1565 if (new->arg == 0) \
1566 ++flagslen; /* Just a single flag letter. */ \
1567 else \
1568 flagslen += 1 + 1 + 1 + 1 + 2 * new->arglen; /* " -x foo" */ \
1569 if (!isalnum (cs->c)) \
1570 /* This switch has no single-letter version, so we use the long. */ \
1571 flagslen += 2 + strlen (cs->long_name); \
1572 } while (0)
1574 for (cs = switches; cs->c != '\0'; ++cs)
1575 if (cs->toenv && (!makefile || !cs->no_makefile))
1576 switch (cs->type)
1578 default:
1579 abort ();
1581 case ignore:
1582 break;
1584 case flag:
1585 case flag_off:
1586 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1587 && (cs->default_value == 0
1588 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1589 ADD_FLAG (0, 0);
1590 break;
1592 case positive_int:
1593 if (all)
1595 if ((cs->default_value != 0
1596 && (*(unsigned int *) cs->value_ptr
1597 == *(unsigned int *) cs->default_value)))
1598 break;
1599 else if (cs->noarg_value != 0
1600 && (*(unsigned int *) cs->value_ptr ==
1601 *(unsigned int *) cs->noarg_value))
1602 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1603 else if (cs->c == 'j')
1604 /* Special case for `-j'. */
1605 ADD_FLAG ("1", 1);
1606 else
1608 char *buf = (char *) alloca (30);
1609 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1610 ADD_FLAG (buf, strlen (buf));
1613 break;
1615 case floating:
1616 if (all)
1618 if (cs->default_value != 0
1619 && (*(double *) cs->value_ptr
1620 == *(double *) cs->default_value))
1621 break;
1622 else if (cs->noarg_value != 0
1623 && (*(double *) cs->value_ptr
1624 == *(double *) cs->noarg_value))
1625 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1626 else
1628 char *buf = (char *) alloca (100);
1629 sprintf (buf, "%g", *(double *) cs->value_ptr);
1630 ADD_FLAG (buf, strlen (buf));
1633 break;
1635 case string:
1636 if (all)
1638 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1639 if (sl != 0)
1641 /* Add the elements in reverse order, because
1642 all the flags get reversed below; and the order
1643 matters for some switches (like -I). */
1644 register unsigned int i = sl->idx;
1645 while (i-- > 0)
1646 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1649 break;
1652 #undef ADD_FLAG
1654 if (all && command_variables != 0)
1656 /* Now figure out how much space will be taken up
1657 by the command-line variable definitions. */
1658 flagslen += 4; /* For the possible " -- ". */
1659 for (cv = command_variables; cv != 0; cv = cv->next)
1661 v = cv->variable;
1662 flagslen += 2 * strlen (v->name);
1663 if (! v->recursive)
1664 ++flagslen;
1665 ++flagslen;
1666 flagslen += 2 * strlen (v->value);
1670 /* Construct the value in FLAGSTRING.
1671 We allocate enough space for a preceding dash and trailing null. */
1672 flagstring = (char *) alloca (1 + flagslen + 1);
1673 p = flagstring;
1674 words = 1;
1675 *p++ = '-';
1676 while (flags != 0)
1678 /* Add the flag letter or name to the string. */
1679 if (!isalnum (flags->cs->c))
1681 *p++ = '-';
1682 strcpy (p, flags->cs->long_name);
1683 p += strlen (p);
1685 else
1686 *p++ = flags->cs->c;
1687 if (flags->arg != 0)
1689 /* A flag that takes an optional argument which in this case
1690 is omitted is specified by ARG being "" and ARGLEN being 0.
1691 We must distinguish because a following flag appended without
1692 an intervening " -" is considered the arg for the first. */
1693 if (flags->arglen > 0)
1695 /* Add its argument too. */
1696 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1697 bcopy (flags->arg, p, flags->arglen);
1698 p += flags->arglen;
1700 ++words;
1701 /* Write a following space and dash, for the next flag. */
1702 *p++ = ' ';
1703 *p++ = '-';
1705 else if (!isalnum (flags->cs->c))
1707 ++words;
1708 /* Long options must each go in their own word,
1709 so we write the following space and dash. */
1710 *p++ = ' ';
1711 *p++ = '-';
1713 flags = flags->next;
1716 if (all && command_variables != 0)
1718 /* Now write all the command-line variable definitions. */
1719 if (p == &flagstring[1])
1720 /* No flags written, so elide the leading dash already written. */
1721 p = flagstring;
1722 else
1724 /* Separate the variables from the switches with a "--" arg. */
1725 if (p[-1] != '-')
1727 /* We did not already write a trailing " -". */
1728 *p++ = ' ';
1729 *p++ = '-';
1731 /* There is a trailing " -"; fill it out to " -- ". */
1732 *p++ = '-';
1733 *p++ = ' ';
1735 for (cv = command_variables; cv != 0; cv = cv->next)
1737 v = cv->variable;
1738 p = shell_quote (p, v->name);
1739 if (! v->recursive)
1740 *p++ = ':';
1741 *p++ = '=';
1742 p = shell_quote (p, v->value);
1743 *p++ = ' ';
1744 ++words;
1746 --p; /* Kill the final space. */
1748 else if (p[-1] == '-')
1749 /* Kill the final space and dash. */
1750 p -= 2;
1752 /* Terminate the string. */
1753 *p = '\0';
1755 v = define_variable ("MAKEFLAGS", 9,
1756 /* If there is just a single word of switches,
1757 omit the leading dash unless it is a single
1758 long option with two leading dashes. */
1759 &flagstring[(words == 1 && command_variables == 0
1760 && flagstring[1] != '-')
1761 ? 1 : 0],
1762 /* This used to use o_env, but that lost when a
1763 makefile defined MAKEFLAGS. Makefiles set
1764 MAKEFLAGS to add switches, but we still want
1765 to redefine its value with the full set of
1766 switches. Of course, an override or command
1767 definition will still take precedence. */
1768 o_file, 0);
1769 if (! all)
1770 /* The first time we are called, set MAKEFLAGS to always be exported.
1771 We should not do this again on the second call, because that is
1772 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1773 v->export = v_export;
1774 /* Since MFLAGS is not parsed for flags, there is no reason to
1775 override any makefile redefinition. */
1776 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 0);
1779 /* Print version information. */
1781 static void
1782 print_version ()
1784 static int printed_version = 0;
1786 char *precede = print_data_base_flag ? "# " : "";
1788 if (printed_version)
1789 /* Do it only once. */
1790 return;
1792 printf ("%sGNU Make version %s", precede, version_string);
1793 if (remote_description != 0 && *remote_description != '\0')
1794 printf ("-%s", remote_description);
1796 printf (", by Richard Stallman and Roland McGrath.\n\
1797 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.\n\
1798 %sThis is free software; see the source for copying conditions.\n\
1799 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1800 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1802 printed_version = 1;
1804 /* Flush stdout so the user doesn't have to wait to see the
1805 version information while things are thought about. */
1806 fflush (stdout);
1809 /* Print a bunch of information about this and that. */
1811 static void
1812 print_data_base ()
1814 extern char *ctime ();
1815 time_t when;
1817 when = time ((time_t *) 0);
1818 printf ("\n# Make data base, printed on %s", ctime (&when));
1820 print_variable_data_base ();
1821 print_dir_data_base ();
1822 print_rule_data_base ();
1823 print_file_data_base ();
1824 print_vpath_data_base ();
1826 when = time ((time_t *) 0);
1827 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1830 /* Exit with STATUS, cleaning up as necessary. */
1832 void
1833 die (status)
1834 int status;
1836 static char dying = 0;
1838 if (!dying)
1840 int err;
1842 dying = 1;
1844 /* Try to move back to the original directory. This is essential on
1845 MS-DOS (where there is really only one process), and on Unix it
1846 puts core files in the original directory instead of the -C
1847 directory. */
1848 if (directory_before_chdir != 0)
1849 chdir (directory_before_chdir);
1851 if (print_version_flag)
1852 print_version ();
1854 /* Wait for children to die. */
1855 for (err = status != 0; job_slots_used > 0; err = 0)
1856 reap_children (1, err);
1858 /* Remove the intermediate files. */
1859 remove_intermediates (0);
1861 if (print_data_base_flag)
1862 print_data_base ();
1864 if (print_directory_flag)
1865 log_working_directory (0);
1868 exit (status);
1871 /* Write a message indicating that we've just entered or
1872 left (according to ENTERING) the current directory. */
1874 static void
1875 log_working_directory (entering)
1876 int entering;
1878 static int entered = 0;
1879 char *message = entering ? "Entering" : "Leaving";
1881 if (entering)
1882 entered = 1;
1883 else if (!entered)
1884 /* Don't print the leaving message if we
1885 haven't printed the entering message. */
1886 return;
1888 if (print_data_base_flag)
1889 fputs ("# ", stdout);
1891 if (makelevel == 0)
1892 printf ("%s: %s ", program, message);
1893 else
1894 printf ("%s[%u]: %s ", program, makelevel, message);
1896 if (starting_directory == 0)
1897 puts ("an unknown directory");
1898 else
1899 printf ("directory `%s'\n", starting_directory);