.
[make.git] / main.c
blob809839d0480b90fba02637d87fe4f4bd05661b2a
1 /* Argument parsing and main program of GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 94, 1995 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "commands.h"
21 #include "dep.h"
22 #include "file.h"
23 #include "variable.h"
24 #include "job.h"
25 #include "getopt.h"
26 #include <assert.h>
29 extern void print_variable_data_base ();
30 extern void print_dir_data_base ();
31 extern void print_rule_data_base ();
32 extern void print_file_data_base ();
33 extern void print_vpath_data_base ();
35 #ifndef HAVE_UNISTD_H
36 extern int chdir ();
37 #endif
38 #ifndef STDC_HEADERS
39 #ifndef sun /* Sun has an incorrect decl in a header. */
40 extern void exit ();
41 #endif
42 extern double atof ();
43 #endif
44 extern char *mktemp ();
46 static void print_data_base (), print_version ();
47 static void decode_switches (), decode_env_switches ();
48 static void define_makeflags ();
49 static char *quote_as_word ();
51 /* The structure that describes an accepted command switch. */
53 struct command_switch
55 char c; /* The switch character. */
57 enum /* Type of the value. */
59 flag, /* Turn int flag on. */
60 flag_off, /* Turn int flag off. */
61 string, /* One string per switch. */
62 positive_int, /* A positive integer. */
63 floating, /* A floating-point number (double). */
64 ignore /* Ignored. */
65 } type;
67 char *value_ptr; /* Pointer to the value-holding variable. */
69 unsigned int env:1; /* Can come from MAKEFLAGS. */
70 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
71 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
73 char *noarg_value; /* Pointer to value used if no argument is given. */
74 char *default_value;/* Pointer to default value. */
76 char *long_name; /* Long option name. */
77 char *argdesc; /* Descriptive word for argument. */
78 char *description; /* Description for usage message. */
82 /* The structure used to hold the list of strings given
83 in command switches of a type that takes string arguments. */
85 struct stringlist
87 char **list; /* Nil-terminated list of strings. */
88 unsigned int idx; /* Index into above. */
89 unsigned int max; /* Number of pointers allocated. */
93 /* The recognized command switches. */
95 /* Nonzero means do not print commands to be executed (-s). */
97 int silent_flag;
99 /* Nonzero means just touch the files
100 that would appear to need remaking (-t) */
102 int touch_flag;
104 /* Nonzero means just print what commands would need to be executed,
105 don't actually execute them (-n). */
107 int just_print_flag;
109 /* Print debugging trace info (-d). */
111 int debug_flag = 0;
113 /* Environment variables override makefile definitions. */
115 int env_overrides = 0;
117 /* Nonzero means ignore status codes returned by commands
118 executed to remake files. Just treat them all as successful (-i). */
120 int ignore_errors_flag = 0;
122 /* Nonzero means don't remake anything, just print the data base
123 that results from reading the makefile (-p). */
125 int print_data_base_flag = 0;
127 /* Nonzero means don't remake anything; just return a nonzero status
128 if the specified targets are not up to date (-q). */
130 int question_flag = 0;
132 /* Nonzero means do not use any of the builtin rules (-r). */
134 int no_builtin_rules_flag = 0;
136 /* Nonzero means keep going even if remaking some file fails (-k). */
138 int keep_going_flag;
139 int default_keep_going_flag = 0;
141 /* Nonzero means print directory before starting and when done (-w). */
143 int print_directory_flag = 0;
145 /* Nonzero means ignore print_directory_flag and never print the directory.
146 This is necessary because print_directory_flag is set implicitly. */
148 int inhibit_print_directory_flag = 0;
150 /* Nonzero means print version information. */
152 int print_version_flag = 0;
154 /* List of makefiles given with -f switches. */
156 static struct stringlist *makefiles = 0;
159 /* Number of job slots (commands that can be run at once). */
161 unsigned int job_slots = 1;
162 unsigned int default_job_slots = 1;
164 /* Value of job_slots that means no limit. */
166 static unsigned int inf_jobs = 0;
168 /* Maximum load average at which multiple jobs will be run.
169 Negative values mean unlimited, while zero means limit to
170 zero load (which could be useful to start infinite jobs remotely
171 but one at a time locally). */
173 double max_load_average = -1.0;
174 double default_load_average = -1.0;
176 /* List of directories given with -C switches. */
178 static struct stringlist *directories = 0;
180 /* List of include directories given with -I switches. */
182 static struct stringlist *include_directories = 0;
184 /* List of files given with -o switches. */
186 static struct stringlist *old_files = 0;
188 /* List of files given with -W switches. */
190 static struct stringlist *new_files = 0;
192 /* If nonzero, we should just print usage and exit. */
194 static int print_usage_flag = 0;
196 /* If nonzero, we should print a warning message
197 for each reference to an undefined variable. */
199 int warn_undefined_variables_flag;
201 /* The table of command switches. */
203 static const struct command_switch switches[] =
205 { 'b', ignore, 0, 0, 0, 0, 0, 0,
206 0, 0,
207 "Ignored for compatibility" },
208 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
209 "directory", "DIRECTORY",
210 "Change to DIRECTORY before doing anything" },
211 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
212 "debug", 0,
213 "Print lots of debugging information" },
214 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
215 "environment-overrides", 0,
216 "Environment variables override makefiles" },
217 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
218 "file", "FILE",
219 "Read FILE as a makefile" },
220 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
221 "help", 0,
222 "Print this message and exit" },
223 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
224 "ignore-errors", 0,
225 "Ignore errors from commands" },
226 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
227 "include-dir", "DIRECTORY",
228 "Search DIRECTORY for included makefiles" },
229 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
230 (char *) &inf_jobs, (char *) &default_job_slots,
231 "jobs", "N",
232 "Allow N jobs at once; infinite jobs with no arg" },
233 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
234 0, (char *) &default_keep_going_flag,
235 "keep-going", 0,
236 "Keep going when some targets can't be made" },
237 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
238 (char *) &default_load_average, (char *) &default_load_average,
239 "load-average", "N",
240 "Don't start multiple jobs unless load is below N" },
241 { 'm', ignore, 0, 0, 0, 0, 0, 0,
242 0, 0,
243 "-b" },
244 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
245 "just-print", 0,
246 "Don't actually run any commands; just print them" },
247 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
248 "old-file", "FILE",
249 "Consider FILE to be very old and don't remake it" },
250 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
251 "print-data-base", 0,
252 "Print make's internal database" },
253 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
254 "question", 0,
255 "Run no commands; exit status says if up to date" },
256 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
257 "no-builtin-rules", 0,
258 "Disable the built-in implicit rules" },
259 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
260 "silent", 0,
261 "Don't echo commands" },
262 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
263 0, (char *) &default_keep_going_flag,
264 "no-keep-going", 0,
265 "Turns off -k" },
266 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
267 "touch", 0,
268 "Touch targets instead of remaking them" },
269 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
270 "version", 0,
271 "Print the version number of make and exit" },
272 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
273 "print-directory", 0,
274 "Print the current directory" },
275 { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
276 "no-print-directory", 0,
277 "Turn off -w, even if it was turned on implicitly" },
278 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
279 "what-if", "FILE",
280 "Consider FILE to be infinitely new" },
281 { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
282 "warn-undefined-variables", 0,
283 "Warn when an undefined variable is referenced" },
284 { '\0', }
287 /* Secondary long names for options. */
289 static struct option long_option_aliases[] =
291 { "quiet", no_argument, 0, 's' },
292 { "stop", no_argument, 0, 'S' },
293 { "new-file", required_argument, 0, 'W' },
294 { "assume-new", required_argument, 0, 'W' },
295 { "assume-old", required_argument, 0, 'o' },
296 { "max-load", optional_argument, 0, 'l' },
297 { "dry-run", no_argument, 0, 'n' },
298 { "recon", no_argument, 0, 'n' },
299 { "makefile", required_argument, 0, 'f' },
302 /* The usage message prints the descriptions of options starting in
303 this column. Make sure it leaves enough room for the longest
304 description to fit in less than 80 characters. */
306 #define DESCRIPTION_COLUMN 30
308 /* List of goal targets. */
310 static struct dep *goals, *lastgoal;
312 /* List of variables which were defined on the command line
313 (or, equivalently, in MAKEFLAGS). */
315 struct command_variable
317 struct command_variable *next;
318 struct variable *variable;
320 static struct command_variable *command_variables;
322 /* The name we were invoked with. */
324 char *program;
326 /* Our current directory before processing any -C options. */
328 char *directory_before_chdir;
330 /* Our current directory after processing all -C options. */
332 char *starting_directory;
334 /* Value of the MAKELEVEL variable at startup (or 0). */
336 unsigned int makelevel;
338 /* First file defined in the makefile whose name does not
339 start with `.'. This is the default to remake if the
340 command line does not specify. */
342 struct file *default_goal_file;
344 /* Pointer to structure for the file .DEFAULT
345 whose commands are used for any file that has none of its own.
346 This is zero if the makefiles do not define .DEFAULT. */
348 struct file *default_file;
350 /* Nonzero if we have seen the magic `.POSIX' target.
351 This turns on pedantic compliance with POSIX.2. */
353 int posix_pedantic;
355 /* Mask of signals that are being caught with fatal_error_signal. */
357 #ifdef POSIX
358 sigset_t fatal_signal_set;
359 #else
360 #ifdef HAVE_SIGSETMASK
361 int fatal_signal_mask;
362 #endif
363 #endif
365 static struct file *
366 enter_command_line_file (name)
367 char *name;
369 if (name[0] == '\0')
370 fatal ("empty string invalid as file name");
372 if (name[0] == '~')
374 char *expanded = tilde_expand (name);
375 if (expanded != 0)
376 name = expanded; /* Memory leak; I don't care. */
379 /* This is also done in parse_file_seq, so this is redundant
380 for names read from makefiles. It is here for names passed
381 on the command line. */
382 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
384 name += 2;
385 while (*name == '/')
386 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
387 ++name;
390 if (*name == '\0')
392 /* It was all slashes! Move back to the dot and truncate
393 it after the first slash, so it becomes just "./". */
395 --name;
396 while (name[0] != '.');
397 name[2] = '\0';
400 return enter_file (savestring (name, strlen (name)));
403 /* Toggle -d on receipt of SIGUSR1. */
405 static RETSIGTYPE
406 debug_signal_handler (sig)
407 int sig;
409 debug_flag = ! debug_flag;
413 main (argc, argv, envp)
414 int argc;
415 char **argv;
416 char **envp;
418 extern void init_dir ();
419 extern RETSIGTYPE fatal_error_signal (), child_handler ();
420 register struct file *f;
421 register unsigned int i;
422 char **p;
423 struct dep *read_makefiles;
424 PATH_VAR (current_directory);
426 default_goal_file = 0;
427 reading_filename = 0;
428 reading_lineno_ptr = 0;
430 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
431 signame_init ();
432 #endif
434 #ifdef POSIX
435 sigemptyset (&fatal_signal_set);
436 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
437 #else
438 #ifdef HAVE_SIGSETMASK
439 fatal_signal_mask = 0;
440 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
441 #else
442 #define ADD_SIG(sig)
443 #endif
444 #endif
446 #define FATAL_SIG(sig) \
447 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
448 (void) signal ((sig), SIG_IGN); \
449 else \
450 ADD_SIG (sig);
452 FATAL_SIG (SIGHUP);
453 FATAL_SIG (SIGQUIT);
454 FATAL_SIG (SIGINT);
455 FATAL_SIG (SIGTERM);
457 #ifdef SIGDANGER
458 FATAL_SIG (SIGDANGER);
459 #endif
460 #ifdef SIGXCPU
461 FATAL_SIG (SIGXCPU);
462 #endif
463 #ifdef SIGXFSZ
464 FATAL_SIG (SIGXFSZ);
465 #endif
467 #undef FATAL_SIG
469 /* Make sure stdout is line-buffered. */
471 #ifdef HAVE_SETLINEBUF
472 setlinebuf (stdout);
473 #else
474 #ifndef SETVBUF_REVERSED
475 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
476 #else /* setvbuf not reversed. */
477 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
478 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
479 #endif /* setvbuf reversed. */
480 #endif /* setlinebuf missing. */
482 /* Initialize the directory hashing code. */
483 init_dir ();
485 /* Figure out where this program lives. */
487 if (argv[0] == 0)
488 argv[0] = "";
489 if (argv[0][0] == '\0')
490 program = "make";
491 else
493 program = rindex (argv[0], '/');
494 #ifdef __MSDOS__
495 if (program == 0)
496 program = rindex (argv[0], '\\');
497 if (program == 0)
498 program = rindex (argv[0], ':');
499 #endif
500 if (program == 0)
501 program = argv[0];
502 else
503 ++program;
506 /* Set up to access user data (files). */
507 user_access ();
509 /* Figure out where we are. */
511 if (getcwd (current_directory, GET_PATH_MAX) == 0)
513 #ifdef HAVE_GETCWD
514 perror_with_name ("getcwd: ", "");
515 #else
516 error ("getwd: %s", current_directory);
517 #endif
518 current_directory[0] = '\0';
519 directory_before_chdir = 0;
521 else
522 directory_before_chdir = savestring (current_directory,
523 strlen (current_directory));
525 /* Read in variables from the environment. It is important that this be
526 done before $(MAKE) is are figured out so its definitions will not be
527 one from the environment. */
529 for (i = 0; envp[i] != 0; ++i)
531 register char *ep = envp[i];
532 while (*ep != '=')
533 ++ep;
534 /* The result of pointer arithmetic is cast to unsigned int for
535 machines where ptrdiff_t is a different size that doesn't widen
536 the same. */
537 define_variable (envp[i], (unsigned int) (ep - envp[i]),
538 ep + 1, o_env, 1)
539 /* Force exportation of every variable culled from the environment.
540 We used to rely on target_environment's v_default code to do this.
541 But that does not work for the case where an environment variable
542 is redefined in a makefile with `override'; it should then still
543 be exported, because it was originally in the environment. */
544 ->export = v_export;
547 /* Decode the switches. */
549 decode_env_switches ("MAKEFLAGS", 9);
550 #if 0
551 /* People write things like:
552 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
553 and we set the -p, -i and -e switches. Doesn't seem quite right. */
554 decode_env_switches ("MFLAGS", 6);
555 #endif
556 decode_switches (argc, argv, 0);
558 /* Print version information. */
560 if (print_version_flag || print_data_base_flag || debug_flag)
561 print_version ();
563 /* `make --version' is supposed to just print the version and exit. */
564 if (print_version_flag)
565 die (0);
567 #ifndef __MSDOS__
568 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
569 (If it is a relative pathname with a slash, prepend our directory name
570 so the result will run the same program regardless of the current dir.
571 If it is a name with no slash, we can only hope that PATH did not
572 find it in the current directory.) */
574 if (current_directory[0] != '\0'
575 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
576 argv[0] = concat (current_directory, "/", argv[0]);
577 #endif
579 /* The extra indirection through $(MAKE_COMMAND) is done
580 for hysterical raisins. */
581 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
582 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
584 if (command_variables != 0)
586 struct command_variable *cv;
587 struct variable *v;
588 unsigned int len = 0;
589 char *value, *p;
591 /* Figure out how much space will be taken up by the command-line
592 variable definitions. */
593 for (cv = command_variables; cv != 0; cv = cv->next)
595 v = cv->variable;
596 len += 2 * strlen (v->name);
597 if (! v->recursive)
598 ++len;
599 ++len;
600 len += 2 * strlen (v->value);
603 /* Now allocate a buffer big enough and fill it. */
604 p = value = (char *) alloca (len);
605 for (cv = command_variables; cv != 0; cv = cv->next)
607 v = cv->variable;
608 p = quote_as_word (p, v->name, 0);
609 if (! v->recursive)
610 *p++ = ':';
611 *p++ = '=';
612 p = quote_as_word (p, v->value, 0);
613 *p++ = ' ';
615 p[-1] = '\0'; /* Kill the final space and terminate. */
617 /* Define an unchangeable variable with a name that no POSIX.2
618 makefile could validly use for its own variable. */
619 (void) define_variable ("-*-command-variables-*-", 23,
620 value, o_automatic, 0);
622 /* Define the variable; this will not override any user definition.
623 Normally a reference to this variable is written into the value of
624 MAKEFLAGS, allowing the user to override this value to affect the
625 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
626 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
627 a reference to this hidden variable is written instead. */
628 (void) define_variable ("MAKEOVERRIDES", 13,
629 "${-*-command-variables-*-}", o_env, 1);
632 /* If there were -C flags, move ourselves about. */
633 if (directories != 0)
634 for (i = 0; directories->list[i] != 0; ++i)
636 char *dir = directories->list[i];
637 if (dir[0] == '~')
639 char *expanded = tilde_expand (dir);
640 if (expanded != 0)
641 dir = expanded;
643 if (chdir (dir) < 0)
644 pfatal_with_name (dir);
645 if (dir != directories->list[i])
646 free (dir);
649 /* Figure out the level of recursion. */
651 struct variable *v = lookup_variable ("MAKELEVEL", 9);
652 if (v != 0 && *v->value != '\0' && *v->value != '-')
653 makelevel = (unsigned int) atoi (v->value);
654 else
655 makelevel = 0;
658 /* Except under -s, always do -w in sub-makes and under -C. */
659 if (!silent_flag && (directories != 0 || makelevel > 0))
660 print_directory_flag = 1;
662 /* Let the user disable that with --no-print-directory. */
663 if (inhibit_print_directory_flag)
664 print_directory_flag = 0;
666 /* Construct the list of include directories to search. */
668 construct_include_path (include_directories == 0 ? (char **) 0
669 : include_directories->list);
671 /* Figure out where we are now, after chdir'ing. */
672 if (directories == 0)
673 /* We didn't move, so we're still in the same place. */
674 starting_directory = current_directory;
675 else
677 if (getcwd (current_directory, GET_PATH_MAX) == 0)
679 #ifdef HAVE_GETCWD
680 perror_with_name ("getcwd: ", "");
681 #else
682 error ("getwd: %s", current_directory);
683 #endif
684 starting_directory = 0;
686 else
687 starting_directory = current_directory;
691 /* Read any stdin makefiles into temporary files. */
693 if (makefiles != 0)
695 register unsigned int i;
696 for (i = 0; i < makefiles->idx; ++i)
697 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
699 /* This makefile is standard input. Since we may re-exec
700 and thus re-read the makefiles, we read standard input
701 into a temporary file and read from that. */
702 FILE *outfile;
704 /* Make a unique filename. */
705 #ifdef HAVE_MKTEMP
706 static char name[] = "/tmp/GmXXXXXX";
707 (void) mktemp (name);
708 #else
709 static char name[L_tmpnam];
710 (void) tmpnam (name);
711 #endif
713 outfile = fopen (name, "w");
714 if (outfile == 0)
715 pfatal_with_name ("fopen (temporary file)");
716 while (!feof (stdin))
718 char buf[2048];
719 int n = fread (buf, 1, sizeof(buf), stdin);
720 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
721 pfatal_with_name ("fwrite (temporary file)");
723 /* Try to make sure we won't remake the temporary
724 file when we are re-exec'd. Kludge-o-matic! */
725 fprintf (outfile, "%s:;\n", name);
726 (void) fclose (outfile);
728 /* Replace the name that read_all_makefiles will
729 see with the name of the temporary file. */
731 char *temp;
732 /* SGI compiler requires alloca's result be assigned simply. */
733 temp = (char *) alloca (sizeof (name));
734 bcopy (name, temp, sizeof (name));
735 makefiles->list[i] = temp;
738 /* Make sure the temporary file will not be remade. */
739 f = enter_file (savestring (name, sizeof name - 1));
740 f->updated = 1;
741 f->update_status = 0;
742 f->command_state = cs_finished;
743 /* Let it be removed when we're done. */
744 f->intermediate = 1;
745 /* But don't mention it. */
746 f->dontcare = 1;
750 /* Set up to handle children dying. This must be done before
751 reading in the makefiles so that `shell' function calls will work. */
753 #ifdef SIGCHLD
754 (void) signal (SIGCHLD, child_handler);
755 #endif
756 #ifdef SIGCLD
757 (void) signal (SIGCLD, child_handler);
758 #endif
760 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
761 #ifdef SIGUSR1
762 (void) signal (SIGUSR1, debug_signal_handler);
763 #endif
765 /* Define the initial list of suffixes for old-style rules. */
767 set_default_suffixes ();
769 /* Define the file rules for the built-in suffix rules. These will later
770 be converted into pattern rules. We used to do this in
771 install_default_implicit_rules, but since that happens after reading
772 makefiles, it results in the built-in pattern rules taking precedence
773 over makefile-specified suffix rules, which is wrong. */
775 install_default_suffix_rules ();
777 /* Define some internal and special variables. */
779 define_automatic_variables ();
781 /* Set up the MAKEFLAGS and MFLAGS variables
782 so makefiles can look at them. */
784 define_makeflags (0, 0);
786 /* Define the default variables. */
787 define_default_variables ();
789 /* Read all the makefiles. */
791 default_file = enter_file (".DEFAULT");
793 read_makefiles
794 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
796 /* Decode switches again, in case the variables were set by the makefile. */
797 decode_env_switches ("MAKEFLAGS", 9);
798 #if 0
799 decode_env_switches ("MFLAGS", 6);
800 #endif
802 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
804 define_makeflags (1, 0);
806 /* Make each `struct dep' point at the `struct file' for the file
807 depended on. Also do magic for special targets. */
809 snap_deps ();
811 /* Convert old-style suffix rules to pattern rules. It is important to
812 do this before installing the built-in pattern rules below, so that
813 makefile-specified suffix rules take precedence over built-in pattern
814 rules. */
816 convert_to_pattern ();
818 /* Install the default implicit pattern rules.
819 This used to be done before reading the makefiles.
820 But in that case, built-in pattern rules were in the chain
821 before user-defined ones, so they matched first. */
823 install_default_implicit_rules ();
825 /* Compute implicit rule limits. */
827 count_implicit_rule_limits ();
829 /* Construct the listings of directories in VPATH lists. */
831 build_vpath_lists ();
833 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
834 and as having been updated already, and files given with -W flags as
835 brand new (time-stamp as far as possible into the future). */
837 if (old_files != 0)
838 for (p = old_files->list; *p != 0; ++p)
840 f = enter_command_line_file (*p);
841 f->last_mtime = (time_t) 1;
842 f->updated = 1;
843 f->update_status = 0;
844 f->command_state = cs_finished;
847 if (new_files != 0)
849 for (p = new_files->list; *p != 0; ++p)
851 f = enter_command_line_file (*p);
852 f->last_mtime = NEW_MTIME;
856 if (read_makefiles != 0)
858 /* Update any makefiles if necessary. */
860 time_t *makefile_mtimes = 0;
861 unsigned int mm_idx = 0;
863 if (debug_flag)
864 puts ("Updating makefiles....");
866 /* Remove any makefiles we don't want to try to update.
867 Also record the current modtimes so we can compare them later. */
869 register struct dep *d, *last;
870 last = 0;
871 d = read_makefiles;
872 while (d != 0)
874 register struct file *f = d->file;
875 if (f->double_colon)
876 for (f = f->double_colon; f != NULL; f = f->prev)
878 if (f->deps == 0 && f->cmds != 0)
880 /* This makefile is a :: target with commands, but
881 no dependencies. So, it will always be remade.
882 This might well cause an infinite loop, so don't
883 try to remake it. (This will only happen if
884 your makefiles are written exceptionally
885 stupidly; but if you work for Athena, that's how
886 you write your makefiles.) */
888 if (debug_flag)
889 printf ("Makefile `%s' might loop; not remaking it.\n",
890 f->name);
892 if (last == 0)
893 read_makefiles = d->next;
894 else
895 last->next = d->next;
897 /* Free the storage. */
898 free ((char *) d);
900 d = last == 0 ? 0 : last->next;
902 break;
905 if (f == NULL || !f->double_colon)
907 if (makefile_mtimes == 0)
908 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
909 else
910 makefile_mtimes = (time_t *)
911 xrealloc ((char *) makefile_mtimes,
912 (mm_idx + 1) * sizeof (time_t));
913 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
914 last = d;
915 d = d->next;
920 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
921 define_makeflags (1, 1);
923 switch (update_goal_chain (read_makefiles, 1))
925 case 1:
926 default:
927 #define BOGUS_UPDATE_STATUS 0
928 assert (BOGUS_UPDATE_STATUS);
929 break;
931 case -1:
932 /* Did nothing. */
933 break;
935 case 2:
936 /* Failed to update. Figure out if we care. */
938 /* Nonzero if any makefile was successfully remade. */
939 int any_remade = 0;
940 /* Nonzero if any makefile we care about failed
941 in updating or could not be found at all. */
942 int any_failed = 0;
943 register unsigned int i;
945 for (i = 0; read_makefiles != 0; ++i)
947 struct dep *d = read_makefiles;
948 read_makefiles = d->next;
949 if (d->file->updated)
951 /* This makefile was updated. */
952 if (d->file->update_status == 0)
954 /* It was successfully updated. */
955 any_remade |= (file_mtime_no_search (d->file)
956 != makefile_mtimes[i]);
958 else if (! (d->changed & RM_DONTCARE))
960 time_t mtime;
961 /* The update failed and this makefile was not
962 from the MAKEFILES variable, so we care. */
963 error ("Failed to remake makefile `%s'.",
964 d->file->name);
965 mtime = file_mtime_no_search (d->file);
966 any_remade |= (mtime != (time_t) -1
967 && mtime != makefile_mtimes[i]);
970 else
971 /* This makefile was not found at all. */
972 if (! (d->changed & RM_DONTCARE))
974 /* This is a makefile we care about. See how much. */
975 if (d->changed & RM_INCLUDED)
976 /* An included makefile. We don't need
977 to die, but we do want to complain. */
978 error ("Included makefile `%s' was not found.",
979 dep_name (d));
980 else
982 /* A normal makefile. We must die later. */
983 error ("Makefile `%s' was not found", dep_name (d));
984 any_failed = 1;
988 free ((char *) d);
991 if (any_remade)
992 goto re_exec;
993 else if (any_failed)
994 die (2);
995 else
996 break;
999 case 0:
1000 re_exec:
1001 /* Updated successfully. Re-exec ourselves. */
1003 remove_intermediates (0);
1005 if (print_data_base_flag)
1006 print_data_base ();
1008 log_working_directory (0);
1010 if (makefiles != 0)
1012 /* These names might have changed. */
1013 register unsigned int i, j = 0;
1014 for (i = 1; i < argc; ++i)
1015 if (!strcmp (argv[i], "-f")) /* XXX */
1017 char *p = &argv[i][2];
1018 if (*p == '\0')
1019 argv[++i] = makefiles->list[j];
1020 else
1021 argv[i] = concat ("-f", makefiles->list[j], "");
1022 ++j;
1026 if (directories != 0 && directories->idx > 0)
1028 char bad;
1029 if (directory_before_chdir != 0)
1031 if (chdir (directory_before_chdir) < 0)
1033 perror_with_name ("chdir", "");
1034 bad = 1;
1036 else
1037 bad = 0;
1039 else
1040 bad = 1;
1041 if (bad)
1042 fatal ("Couldn't change back to original directory.");
1045 for (p = environ; *p != 0; ++p)
1046 if (!strncmp (*p, "MAKELEVEL=", 10))
1048 /* The SGI compiler apparently can't understand
1049 the concept of storing the result of a function
1050 in something other than a local variable. */
1051 char *sgi_loses;
1052 sgi_loses = (char *) alloca (40);
1053 *p = sgi_loses;
1054 sprintf (*p, "MAKELEVEL=%u", makelevel);
1055 break;
1058 if (debug_flag)
1060 char **p;
1061 fputs ("Re-executing:", stdout);
1062 for (p = argv; *p != 0; ++p)
1063 printf (" %s", *p);
1064 puts ("");
1067 fflush (stdout);
1068 fflush (stderr);
1070 exec_command (argv, environ);
1071 /* NOTREACHED */
1075 /* Set up `MAKEFLAGS' again for the normal targets. */
1076 define_makeflags (1, 0);
1079 int status;
1081 /* If there were no command-line goals, use the default. */
1082 if (goals == 0)
1084 if (default_goal_file != 0)
1086 goals = (struct dep *) xmalloc (sizeof (struct dep));
1087 goals->next = 0;
1088 goals->name = 0;
1089 goals->file = default_goal_file;
1092 else
1093 lastgoal->next = 0;
1095 if (goals != 0)
1097 /* Update the goals. */
1099 if (debug_flag)
1100 puts ("Updating goal targets....");
1102 switch (update_goal_chain (goals, 0))
1104 case -1:
1105 /* Nothing happened. */
1106 case 0:
1107 /* Updated successfully. */
1108 status = 0;
1109 break;
1110 case 2:
1111 /* Updating failed. */
1112 status = 2;
1113 break;
1114 case 1:
1115 /* We are under -q and would run some commands. */
1116 status = 1;
1117 break;
1118 default:
1119 abort ();
1122 else
1124 if (read_makefiles == 0)
1125 fatal ("No targets specified and no makefile found");
1126 else
1127 fatal ("No targets");
1130 /* Exit. */
1131 die (status);
1134 return 0;
1137 /* Parsing of arguments, decoding of switches. */
1139 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1140 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1141 (sizeof (long_option_aliases) /
1142 sizeof (long_option_aliases[0]))];
1144 /* Fill in the string and vector for getopt. */
1145 static void
1146 init_switches ()
1148 register char *p;
1149 register int c;
1150 register unsigned int i;
1152 if (options[0] != '\0')
1153 /* Already done. */
1154 return;
1156 p = options;
1158 /* Return switch and non-switch args in order, regardless of
1159 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1160 *p++ = '-';
1162 for (i = 0; switches[i].c != '\0'; ++i)
1164 long_options[i].name = (switches[i].long_name == 0 ? "" :
1165 switches[i].long_name);
1166 long_options[i].flag = 0;
1167 long_options[i].val = switches[i].c;
1168 if (isalnum (switches[i].c))
1169 *p++ = switches[i].c;
1170 switch (switches[i].type)
1172 case flag:
1173 case flag_off:
1174 case ignore:
1175 long_options[i].has_arg = no_argument;
1176 break;
1178 case string:
1179 case positive_int:
1180 case floating:
1181 if (isalnum (switches[i].c))
1182 *p++ = ':';
1183 if (switches[i].noarg_value != 0)
1185 if (isalnum (switches[i].c))
1186 *p++ = ':';
1187 long_options[i].has_arg = optional_argument;
1189 else
1190 long_options[i].has_arg = required_argument;
1191 break;
1194 *p = '\0';
1195 for (c = 0; c < (sizeof (long_option_aliases) /
1196 sizeof (long_option_aliases[0]));
1197 ++c)
1198 long_options[i++] = long_option_aliases[c];
1199 long_options[i].name = 0;
1202 static void
1203 handle_non_switch_argument (arg, env)
1204 char *arg;
1205 int env;
1207 /* Non-option argument. It might be a variable definition. */
1208 struct variable *v;
1209 if (arg[0] == '-' && arg[1] == '\0')
1210 /* Ignore plain `-' for compatibility. */
1211 return;
1212 v = try_variable_definition ((char *) 0, 0, arg, o_command);
1213 if (v != 0)
1215 /* It is indeed a variable definition. Record a pointer to
1216 the variable for later use in define_makeflags. */
1217 struct command_variable *cv
1218 = (struct command_variable *) xmalloc (sizeof (*cv));
1219 cv->variable = v;
1220 cv->next = command_variables;
1221 command_variables = cv;
1223 else if (! env)
1225 /* Not an option or variable definition; it must be a goal
1226 target! Enter it as a file and add it to the dep chain of
1227 goals. */
1228 struct file *f = enter_command_line_file (arg);
1229 f->cmd_target = 1;
1231 if (goals == 0)
1233 goals = (struct dep *) xmalloc (sizeof (struct dep));
1234 lastgoal = goals;
1236 else
1238 lastgoal->next
1239 = (struct dep *) xmalloc (sizeof (struct dep));
1240 lastgoal = lastgoal->next;
1242 lastgoal->name = 0;
1243 lastgoal->file = f;
1247 /* Decode switches from ARGC and ARGV.
1248 They came from the environment if ENV is nonzero. */
1250 static void
1251 decode_switches (argc, argv, env)
1252 int argc;
1253 char **argv;
1254 int env;
1256 int bad = 0;
1257 register const struct command_switch *cs;
1258 register struct stringlist *sl;
1259 register int c;
1261 /* getopt does most of the parsing for us.
1262 First, get its vectors set up. */
1264 init_switches ();
1266 /* Let getopt produce error messages for the command line,
1267 but not for options from the environment. */
1268 opterr = !env;
1269 /* Reset getopt's state. */
1270 optind = 0;
1272 while (optind < argc)
1274 /* Parse the next argument. */
1275 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1276 if (c == EOF)
1277 /* End of arguments, or "--" marker seen. */
1278 break;
1279 else if (c == 1)
1280 /* An argument not starting with a dash. */
1281 handle_non_switch_argument (optarg, env);
1282 else if (c == '?')
1283 /* Bad option. We will print a usage message and die later.
1284 But continue to parse the other options so the user can
1285 see all he did wrong. */
1286 bad = 1;
1287 else
1288 for (cs = switches; cs->c != '\0'; ++cs)
1289 if (cs->c == c)
1291 /* Whether or not we will actually do anything with
1292 this switch. We test this individually inside the
1293 switch below rather than just once outside it, so that
1294 options which are to be ignored still consume args. */
1295 int doit = !env || cs->env;
1297 switch (cs->type)
1299 default:
1300 abort ();
1302 case ignore:
1303 break;
1305 case flag:
1306 case flag_off:
1307 if (doit)
1308 *(int *) cs->value_ptr = cs->type == flag;
1309 break;
1311 case string:
1312 if (!doit)
1313 break;
1315 if (optarg == 0)
1316 optarg = cs->noarg_value;
1318 sl = *(struct stringlist **) cs->value_ptr;
1319 if (sl == 0)
1321 sl = (struct stringlist *)
1322 xmalloc (sizeof (struct stringlist));
1323 sl->max = 5;
1324 sl->idx = 0;
1325 sl->list = (char **) xmalloc (5 * sizeof (char *));
1326 *(struct stringlist **) cs->value_ptr = sl;
1328 else if (sl->idx == sl->max - 1)
1330 sl->max += 5;
1331 sl->list = (char **)
1332 xrealloc ((char *) sl->list,
1333 sl->max * sizeof (char *));
1335 sl->list[sl->idx++] = optarg;
1336 sl->list[sl->idx] = 0;
1337 break;
1339 case positive_int:
1340 if (optarg == 0 && argc > optind
1341 && isdigit (argv[optind][0]))
1342 optarg = argv[optind++];
1344 if (!doit)
1345 break;
1347 if (optarg != 0)
1349 int i = atoi (optarg);
1350 if (i < 1)
1352 if (doit)
1353 error ("the `-%c' option requires a \
1354 positive integral argument",
1355 cs->c);
1356 bad = 1;
1358 else
1359 *(unsigned int *) cs->value_ptr = i;
1361 else
1362 *(unsigned int *) cs->value_ptr
1363 = *(unsigned int *) cs->noarg_value;
1364 break;
1366 case floating:
1367 if (optarg == 0 && optind < argc
1368 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1369 optarg = argv[optind++];
1371 if (doit)
1372 *(double *) cs->value_ptr
1373 = (optarg != 0 ? atof (optarg)
1374 : *(double *) cs->noarg_value);
1376 break;
1379 /* We've found the switch. Stop looking. */
1380 break;
1384 /* There are no more options according to getting getopt, but there may
1385 be some arguments left. Since we have asked for non-option arguments
1386 to be returned in order, this only happens when there is a "--"
1387 argument to prevent later arguments from being options. */
1388 while (optind < argc)
1389 handle_non_switch_argument (argv[optind++], env);
1392 if (!env && (bad || print_usage_flag))
1394 /* Print a nice usage message. */
1395 FILE *usageto;
1397 if (print_version_flag)
1398 print_version ();
1400 usageto = bad ? stderr : stdout;
1402 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1404 fputs ("Options:\n", usageto);
1405 for (cs = switches; cs->c != '\0'; ++cs)
1407 char buf[1024], shortarg[50], longarg[50], *p;
1409 if (cs->description[0] == '-')
1410 continue;
1412 switch (long_options[cs - switches].has_arg)
1414 case no_argument:
1415 shortarg[0] = longarg[0] = '\0';
1416 break;
1417 case required_argument:
1418 sprintf (longarg, "=%s", cs->argdesc);
1419 sprintf (shortarg, " %s", cs->argdesc);
1420 break;
1421 case optional_argument:
1422 sprintf (longarg, "[=%s]", cs->argdesc);
1423 sprintf (shortarg, " [%s]", cs->argdesc);
1424 break;
1427 p = buf;
1429 if (isalnum (cs->c))
1431 sprintf (buf, " -%c%s", cs->c, shortarg);
1432 p += strlen (p);
1434 if (cs->long_name != 0)
1436 unsigned int i;
1437 sprintf (p, "%s--%s%s",
1438 !isalnum (cs->c) ? " " : ", ",
1439 cs->long_name, longarg);
1440 p += strlen (p);
1441 for (i = 0; i < (sizeof (long_option_aliases) /
1442 sizeof (long_option_aliases[0]));
1443 ++i)
1444 if (long_option_aliases[i].val == cs->c)
1446 sprintf (p, ", --%s%s",
1447 long_option_aliases[i].name, longarg);
1448 p += strlen (p);
1452 const struct command_switch *ncs = cs;
1453 while ((++ncs)->c != '\0')
1454 if (ncs->description[0] == '-' &&
1455 ncs->description[1] == cs->c)
1457 /* This is another switch that does the same
1458 one as the one we are processing. We want
1459 to list them all together on one line. */
1460 sprintf (p, ", -%c%s", ncs->c, shortarg);
1461 p += strlen (p);
1462 if (ncs->long_name != 0)
1464 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1465 p += strlen (p);
1470 if (p - buf > DESCRIPTION_COLUMN - 2)
1471 /* The list of option names is too long to fit on the same
1472 line with the description, leaving at least two spaces.
1473 Print it on its own line instead. */
1475 fprintf (usageto, "%s\n", buf);
1476 buf[0] = '\0';
1479 fprintf (usageto, "%*s%s.\n",
1480 - DESCRIPTION_COLUMN,
1481 buf, cs->description);
1484 die (bad ? 2 : 0);
1488 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1489 We do this by chopping the value into a vector of words, prepending a
1490 dash to the first word if it lacks one, and passing the vector to
1491 decode_switches. */
1493 static void
1494 decode_env_switches (envar, len)
1495 char *envar;
1496 unsigned int len;
1498 char *varref = (char *) alloca (2 + len + 2);
1499 char *value, *p;
1500 int argc;
1501 char **argv;
1503 /* Get the variable's value. */
1504 varref[0] = '$';
1505 varref[1] = '(';
1506 bcopy (envar, &varref[2], len);
1507 varref[2 + len] = ')';
1508 varref[2 + len + 1] = '\0';
1509 value = variable_expand (varref);
1511 /* Skip whitespace, and check for an empty value. */
1512 value = next_token (value);
1513 len = strlen (value);
1514 if (len == 0)
1515 return;
1517 /* Allocate a vector that is definitely big enough. */
1518 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1520 /* Allocate a buffer to copy the value into while we split it into words
1521 and unquote it. We must use permanent storage for this because
1522 decode_switches may store pointers into the passed argument words. */
1523 p = (char *) xmalloc (2 * len);
1525 /* getopt will look at the arguments starting at ARGV[1].
1526 Prepend a spacer word. */
1527 argv[0] = 0;
1528 argc = 1;
1529 argv[argc] = p;
1530 while (*value != '\0')
1532 if (*value == '\\')
1533 ++value; /* Skip the backslash. */
1534 else if (isblank (*value))
1536 /* End of the word. */
1537 *p++ = '\0';
1538 argv[++argc] = p;
1540 ++value;
1541 while (isblank (*value));
1542 continue;
1544 *p++ = *value++;
1546 *p = '\0';
1547 argv[++argc] = 0;
1549 if (argc == 2 && argv[1][0] != '-')
1551 /* There is just one word in the value, and it is not a switch.
1552 Either this is the single-word form and we should prepend a dash
1553 before calling decode_switches, or this is the multi-word form and
1554 there is no dash because it is a variable definition. */
1555 struct variable *v;
1556 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1557 if (v != 0)
1559 /* It was indeed a variable definition, and now it has been
1560 processed. There is nothing for decode_switches to do.
1561 Record a pointer to the variable for later use in
1562 define_makeflags. */
1563 struct command_variable *cv
1564 = (struct command_variable *) xmalloc (sizeof (*cv));
1565 cv->variable = v;
1566 cv->next = command_variables;
1567 command_variables = cv;
1568 return;
1571 /* It wasn't a variable definition, so it's some switches without a
1572 leading dash. Add one and pass it along to decode_switches. We
1573 need permanent storage for this in case decode_switches saves
1574 pointers into the value. */
1575 argv[1] = concat ("-", argv[1], "");
1578 /* Parse those words. */
1579 decode_switches (argc, argv, 1);
1582 /* Quote the string IN so that it will be interpreted as a single word with
1583 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1584 signs to avoid variable expansion in make itself. Write the result into
1585 OUT, returning the address of the next character to be written.
1586 Allocating space for OUT twice the length of IN (thrice if
1587 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1589 static char *
1590 quote_as_word (out, in, double_dollars)
1591 char *out, *in;
1592 int double_dollars;
1594 while (*in != '\0')
1596 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1597 *out++ = '\\';
1598 if (double_dollars && *in == '$')
1599 *out++ = '$';
1600 *out++ = *in++;
1603 return out;
1606 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1607 command switches. Include options with args if ALL is nonzero.
1608 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1610 static void
1611 define_makeflags (all, makefile)
1612 int all, makefile;
1614 static const char ref[] = "$(MAKEOVERRIDES)";
1615 static const char posixref[] = "$(-*-command-variables-*-)";
1616 register const struct command_switch *cs;
1617 char *flagstring;
1618 register char *p;
1619 unsigned int words;
1620 struct variable *v;
1622 /* We will construct a linked list of `struct flag's describing
1623 all the flags which need to go in MAKEFLAGS. Then, once we
1624 know how many there are and their lengths, we can put them all
1625 together in a string. */
1627 struct flag
1629 struct flag *next;
1630 const struct command_switch *cs;
1631 char *arg;
1633 struct flag *flags = 0;
1634 unsigned int flagslen = 0;
1635 #define ADD_FLAG(ARG, LEN) \
1636 do { \
1637 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1638 new->cs = cs; \
1639 new->arg = (ARG); \
1640 new->next = flags; \
1641 flags = new; \
1642 if (new->arg == 0) \
1643 ++flagslen; /* Just a single flag letter. */ \
1644 else \
1645 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1646 if (!isalnum (cs->c)) \
1647 /* This switch has no single-letter version, so we use the long. */ \
1648 flagslen += 2 + strlen (cs->long_name); \
1649 } while (0)
1651 for (cs = switches; cs->c != '\0'; ++cs)
1652 if (cs->toenv && (!makefile || !cs->no_makefile))
1653 switch (cs->type)
1655 default:
1656 abort ();
1658 case ignore:
1659 break;
1661 case flag:
1662 case flag_off:
1663 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1664 && (cs->default_value == 0
1665 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1666 ADD_FLAG (0, 0);
1667 break;
1669 case positive_int:
1670 if (all)
1672 if ((cs->default_value != 0
1673 && (*(unsigned int *) cs->value_ptr
1674 == *(unsigned int *) cs->default_value)))
1675 break;
1676 else if (cs->noarg_value != 0
1677 && (*(unsigned int *) cs->value_ptr ==
1678 *(unsigned int *) cs->noarg_value))
1679 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1680 else if (cs->c == 'j')
1681 /* Special case for `-j'. */
1682 ADD_FLAG ("1", 1);
1683 else
1685 char *buf = (char *) alloca (30);
1686 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1687 ADD_FLAG (buf, strlen (buf));
1690 break;
1692 case floating:
1693 if (all)
1695 if (cs->default_value != 0
1696 && (*(double *) cs->value_ptr
1697 == *(double *) cs->default_value))
1698 break;
1699 else if (cs->noarg_value != 0
1700 && (*(double *) cs->value_ptr
1701 == *(double *) cs->noarg_value))
1702 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1703 else
1705 char *buf = (char *) alloca (100);
1706 sprintf (buf, "%g", *(double *) cs->value_ptr);
1707 ADD_FLAG (buf, strlen (buf));
1710 break;
1712 case string:
1713 if (all)
1715 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1716 if (sl != 0)
1718 /* Add the elements in reverse order, because
1719 all the flags get reversed below; and the order
1720 matters for some switches (like -I). */
1721 register unsigned int i = sl->idx;
1722 while (i-- > 0)
1723 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1726 break;
1729 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1731 #undef ADD_FLAG
1733 /* Construct the value in FLAGSTRING.
1734 We allocate enough space for a preceding dash and trailing null. */
1735 flagstring = (char *) alloca (1 + flagslen + 1);
1736 p = flagstring;
1737 words = 1;
1738 *p++ = '-';
1739 while (flags != 0)
1741 /* Add the flag letter or name to the string. */
1742 if (!isalnum (flags->cs->c))
1744 *p++ = '-';
1745 strcpy (p, flags->cs->long_name);
1746 p += strlen (p);
1748 else
1749 *p++ = flags->cs->c;
1750 if (flags->arg != 0)
1752 /* A flag that takes an optional argument which in this case is
1753 omitted is specified by ARG being "". We must distinguish
1754 because a following flag appended without an intervening " -"
1755 is considered the arg for the first. */
1756 if (flags->arg[0] != '\0')
1758 /* Add its argument too. */
1759 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1760 p = quote_as_word (p, flags->arg, 1);
1762 ++words;
1763 /* Write a following space and dash, for the next flag. */
1764 *p++ = ' ';
1765 *p++ = '-';
1767 else if (!isalnum (flags->cs->c))
1769 ++words;
1770 /* Long options must each go in their own word,
1771 so we write the following space and dash. */
1772 *p++ = ' ';
1773 *p++ = '-';
1775 flags = flags->next;
1779 /* Define MFLAGS before appending variable definitions. */
1781 if (p == &flagstring[1])
1782 /* No flags. */
1783 flagstring[0] = '\0';
1784 else if (p[-1] == '-')
1785 /* Kill the final space and dash. */
1786 p[-2] = '\0';
1787 else
1788 /* Terminate the string. */
1789 *p = '\0';
1791 /* Since MFLAGS is not parsed for flags, there is no reason to
1792 override any makefile redefinition. */
1793 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1796 if (all && command_variables != 0)
1798 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1799 command-line variable definitions. */
1801 if (p == &flagstring[1])
1802 /* No flags written, so elide the leading dash already written. */
1803 p = flagstring;
1804 else
1806 /* Separate the variables from the switches with a "--" arg. */
1807 if (p[-1] != '-')
1809 /* We did not already write a trailing " -". */
1810 *p++ = ' ';
1811 *p++ = '-';
1813 /* There is a trailing " -"; fill it out to " -- ". */
1814 *p++ = '-';
1815 *p++ = ' ';
1818 /* Copy in the string. */
1819 if (posix_pedantic)
1821 bcopy (posixref, p, sizeof posixref - 1);
1822 p += sizeof posixref - 1;
1824 else
1826 bcopy (ref, p, sizeof ref - 1);
1827 p += sizeof ref - 1;
1830 else if (p == &flagstring[1])
1832 words = 0;
1833 --p;
1835 else if (p[-1] == '-')
1836 /* Kill the final space and dash. */
1837 p -= 2;
1838 /* Terminate the string. */
1839 *p = '\0';
1841 v = define_variable ("MAKEFLAGS", 9,
1842 /* If there is just a single word of switches,
1843 omit the leading dash unless it is a single
1844 long option with two leading dashes. */
1845 &flagstring[(words == 1 && command_variables == 0
1846 && flagstring[1] != '-')
1847 ? 1 : 0],
1848 /* This used to use o_env, but that lost when a
1849 makefile defined MAKEFLAGS. Makefiles set
1850 MAKEFLAGS to add switches, but we still want
1851 to redefine its value with the full set of
1852 switches. Of course, an override or command
1853 definition will still take precedence. */
1854 o_file, 1);
1855 if (! all)
1856 /* The first time we are called, set MAKEFLAGS to always be exported.
1857 We should not do this again on the second call, because that is
1858 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1859 v->export = v_export;
1862 /* Print version information. */
1864 static void
1865 print_version ()
1867 static int printed_version = 0;
1869 char *precede = print_data_base_flag ? "# " : "";
1871 if (printed_version)
1872 /* Do it only once. */
1873 return;
1875 printf ("%sGNU Make version %s", precede, version_string);
1876 if (remote_description != 0 && *remote_description != '\0')
1877 printf ("-%s", remote_description);
1879 printf (", by Richard Stallman and Roland McGrath.\n\
1880 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
1881 %sThis is free software; see the source for copying conditions.\n\
1882 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1883 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1885 printed_version = 1;
1887 /* Flush stdout so the user doesn't have to wait to see the
1888 version information while things are thought about. */
1889 fflush (stdout);
1892 /* Print a bunch of information about this and that. */
1894 static void
1895 print_data_base ()
1897 extern char *ctime ();
1898 time_t when;
1900 when = time ((time_t *) 0);
1901 printf ("\n# Make data base, printed on %s", ctime (&when));
1903 print_variable_data_base ();
1904 print_dir_data_base ();
1905 print_rule_data_base ();
1906 print_file_data_base ();
1907 print_vpath_data_base ();
1909 when = time ((time_t *) 0);
1910 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1913 /* Exit with STATUS, cleaning up as necessary. */
1915 void
1916 die (status)
1917 int status;
1919 static char dying = 0;
1921 if (!dying)
1923 int err;
1925 dying = 1;
1927 /* Try to move back to the original directory. This is essential on
1928 MS-DOS (where there is really only one process), and on Unix it
1929 puts core files in the original directory instead of the -C
1930 directory. */
1931 if (directory_before_chdir != 0)
1932 chdir (directory_before_chdir);
1934 if (print_version_flag)
1935 print_version ();
1937 /* Wait for children to die. */
1938 for (err = status != 0; job_slots_used > 0; err = 0)
1939 reap_children (1, err);
1941 /* Remove the intermediate files. */
1942 remove_intermediates (0);
1944 if (print_data_base_flag)
1945 print_data_base ();
1947 log_working_directory (0);
1950 exit (status);
1953 /* Write a message indicating that we've just entered or
1954 left (according to ENTERING) the current directory. */
1956 void
1957 log_working_directory (entering)
1958 int entering;
1960 static int entered = 0;
1961 char *message = entering ? "Entering" : "Leaving";
1963 /* Print nothing without the flag. Don't print the entering message
1964 again if we already have. Don't print the leaving message if we
1965 haven't printed the entering message. */
1966 if (! print_directory_flag || entering == entered)
1967 return;
1969 entered = entering;
1971 if (print_data_base_flag)
1972 fputs ("# ", stdout);
1974 if (makelevel == 0)
1975 printf ("%s: %s ", program, message);
1976 else
1977 printf ("%s[%u]: %s ", program, makelevel, message);
1979 if (starting_directory == 0)
1980 puts ("an unknown directory");
1981 else
1982 printf ("directory `%s'\n", starting_directory);