Fix typo in comment.
[make.git] / main.c
blob18d5d7b9d5102efe76824596e8c6124e3dd978c9
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"
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 ();
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] == '~')
371 char *expanded = tilde_expand (name);
372 if (expanded != 0)
373 name = expanded; /* Memory leak; I don't care. */
376 /* This is also done in parse_file_seq, so this is redundant
377 for names read from makefiles. It is here for names passed
378 on the command line. */
379 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
381 name += 2;
382 while (*name == '/')
383 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
384 ++name;
387 if (*name == '\0')
389 /* It was all slashes! Move back to the dot and truncate
390 it after the first slash, so it becomes just "./". */
392 --name;
393 while (name[0] != '.');
394 name[2] = '\0';
397 return enter_file (savestring (name, strlen (name)));
401 main (argc, argv, envp)
402 int argc;
403 char **argv;
404 char **envp;
406 extern void init_dir ();
407 extern RETSIGTYPE fatal_error_signal (), child_handler ();
408 register struct file *f;
409 register unsigned int i;
410 char **p;
411 struct dep *read_makefiles;
412 PATH_VAR (current_directory);
414 default_goal_file = 0;
415 reading_filename = 0;
416 reading_lineno_ptr = 0;
418 #ifndef HAVE_SYS_SIGLIST
419 signame_init ();
420 #endif
422 #ifdef POSIX
423 sigemptyset (&fatal_signal_set);
424 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
425 #else
426 #ifdef HAVE_SIGSETMASK
427 fatal_signal_mask = 0;
428 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
429 #else
430 #define ADD_SIG(sig)
431 #endif
432 #endif
434 #define FATAL_SIG(sig) \
435 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
436 (void) signal ((sig), SIG_IGN); \
437 else \
438 ADD_SIG (sig);
440 FATAL_SIG (SIGHUP);
441 FATAL_SIG (SIGQUIT);
442 FATAL_SIG (SIGINT);
443 FATAL_SIG (SIGTERM);
445 #ifdef SIGDANGER
446 FATAL_SIG (SIGDANGER);
447 #endif
448 #ifdef SIGXCPU
449 FATAL_SIG (SIGXCPU);
450 #endif
451 #ifdef SIGXFSZ
452 FATAL_SIG (SIGXFSZ);
453 #endif
455 #undef FATAL_SIG
457 /* Make sure stdout is line-buffered. */
459 #ifdef HAVE_SETLINEBUF
460 setlinebuf (stdout);
461 #else
462 #ifndef SETVBUF_REVERSED
463 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
464 #else /* setvbuf not reversed. */
465 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
466 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
467 #endif /* setvbuf reversed. */
468 #endif /* setlinebuf missing. */
470 /* Initialize the directory hashing code. */
471 init_dir ();
473 /* Figure out where this program lives. */
475 if (argv[0] == 0)
476 argv[0] = "";
477 if (argv[0][0] == '\0')
478 program = "make";
479 else
481 program = rindex (argv[0], '/');
482 #ifdef __MSDOS__
483 if (program == 0)
484 program = rindex (argv[0], '\\');
485 if (program == 0)
486 program = rindex (argv[0], ':');
487 #endif
488 if (program == 0)
489 program = argv[0];
490 else
491 ++program;
494 /* Set up to access user data (files). */
495 user_access ();
497 /* Figure out where we are. */
499 if (getcwd (current_directory, GET_PATH_MAX) == 0)
501 #ifdef HAVE_GETCWD
502 perror_with_name ("getcwd: ", "");
503 #else
504 error ("getwd: %s", current_directory);
505 #endif
506 current_directory[0] = '\0';
507 directory_before_chdir = 0;
509 else
510 directory_before_chdir = savestring (current_directory,
511 strlen (current_directory));
513 /* Read in variables from the environment. It is important that this be
514 done before $(MAKE) is are figured out so its definitions will not be
515 one from the environment. */
517 for (i = 0; envp[i] != 0; ++i)
519 register char *ep = envp[i];
520 while (*ep != '=')
521 ++ep;
522 /* The result of pointer arithmetic is cast to unsigned int for
523 machines where ptrdiff_t is a different size that doesn't widen
524 the same. */
525 define_variable (envp[i], (unsigned int) (ep - envp[i]),
526 ep + 1, o_env, 1)
527 /* Force exportation of every variable culled from the environment.
528 We used to rely on target_environment's v_default code to do this.
529 But that does not work for the case where an environment variable
530 is redefined in a makefile with `override'; it should then still
531 be exported, because it was originally in the environment. */
532 ->export = v_export;
535 /* Decode the switches. */
537 decode_env_switches ("MAKEFLAGS", 9);
538 #if 0
539 /* People write things like:
540 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
541 and we set the -p, -i and -e switches. Doesn't seem quite right. */
542 decode_env_switches ("MFLAGS", 6);
543 #endif
544 decode_switches (argc, argv, 0);
546 /* Print version information. */
548 if (print_version_flag || print_data_base_flag || debug_flag)
549 print_version ();
551 /* `make --version' is supposed to just print the version and exit. */
552 if (print_version_flag)
553 die (0);
555 #ifndef __MSDOS__
556 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
557 (If it is a relative pathname with a slash, prepend our directory name
558 so the result will run the same program regardless of the current dir.
559 If it is a name with no slash, we can only hope that PATH did not
560 find it in the current directory.) */
562 if (current_directory[0] != '\0'
563 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
564 argv[0] = concat (current_directory, "/", argv[0]);
565 #endif
567 /* The extra indirection through $(MAKE_COMMAND) is done
568 for hysterical raisins. */
569 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
570 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
572 if (command_variables != 0)
574 struct command_variable *cv;
575 struct variable *v;
576 unsigned int len = 0;
577 char *value, *p;
579 /* Figure out how much space will be taken up by the command-line
580 variable definitions. */
581 for (cv = command_variables; cv != 0; cv = cv->next)
583 v = cv->variable;
584 len += 2 * strlen (v->name);
585 if (! v->recursive)
586 ++len;
587 ++len;
588 len += 2 * strlen (v->value);
591 /* Now allocate a buffer big enough and fill it. */
592 p = value = alloca (len);
593 for (cv = command_variables; cv != 0; cv = cv->next)
595 v = cv->variable;
596 p = quote_as_word (p, v->name, 0);
597 if (! v->recursive)
598 *p++ = ':';
599 *p++ = '=';
600 p = quote_as_word (p, v->value, 0);
601 *p++ = ' ';
603 p[-1] = '\0'; /* Kill the final space and terminate. */
605 /* Define an unchangeable variable with a name that no POSIX.2
606 makefile could validly use for its own variable. */
607 (void) define_variable ("-*-command-variables-*-", 23,
608 value, o_automatic, 0);
610 /* Define the variable; this will not override any user definition.
611 Normally a reference to this variable is written into the value of
612 MAKEFLAGS, allowing the user to override this value to affect the
613 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
614 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
615 a reference to this hidden variable is written instead. */
616 (void) define_variable ("MAKEOVERRIDES", 13,
617 "${-*-command-variables-*-}", o_env, 1);
620 /* If there were -C flags, move ourselves about. */
621 if (directories != 0)
622 for (i = 0; directories->list[i] != 0; ++i)
624 char *dir = directories->list[i];
625 if (dir[0] == '~')
627 char *expanded = tilde_expand (dir);
628 if (expanded != 0)
629 dir = expanded;
631 if (chdir (dir) < 0)
632 pfatal_with_name (dir);
633 if (dir != directories->list[i])
634 free (dir);
637 /* Figure out the level of recursion. */
639 struct variable *v = lookup_variable ("MAKELEVEL", 9);
640 if (v != 0 && *v->value != '\0' && *v->value != '-')
641 makelevel = (unsigned int) atoi (v->value);
642 else
643 makelevel = 0;
646 /* Except under -s, always do -w in sub-makes and under -C. */
647 if (!silent_flag && (directories != 0 || makelevel > 0))
648 print_directory_flag = 1;
650 /* Let the user disable that with --no-print-directory. */
651 if (inhibit_print_directory_flag)
652 print_directory_flag = 0;
654 /* Construct the list of include directories to search. */
656 construct_include_path (include_directories == 0 ? (char **) 0
657 : include_directories->list);
659 /* Figure out where we are now, after chdir'ing. */
660 if (directories == 0)
661 /* We didn't move, so we're still in the same place. */
662 starting_directory = current_directory;
663 else
665 if (getcwd (current_directory, GET_PATH_MAX) == 0)
667 #ifdef HAVE_GETCWD
668 perror_with_name ("getcwd: ", "");
669 #else
670 error ("getwd: %s", current_directory);
671 #endif
672 starting_directory = 0;
674 else
675 starting_directory = current_directory;
678 /* Tell the user where he is. */
680 if (print_directory_flag)
681 log_working_directory (1);
683 /* Read any stdin makefiles into temporary files. */
685 if (makefiles != 0)
687 register unsigned int i;
688 for (i = 0; i < makefiles->idx; ++i)
689 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
691 /* This makefile is standard input. Since we may re-exec
692 and thus re-read the makefiles, we read standard input
693 into a temporary file and read from that. */
694 static char name[] = "/tmp/GmXXXXXX";
695 FILE *outfile;
697 /* Make a unique filename. */
698 (void) mktemp (name);
700 outfile = fopen (name, "w");
701 if (outfile == 0)
702 pfatal_with_name ("fopen (temporary file)");
703 while (!feof (stdin))
705 char buf[2048];
706 int n = fread (buf, 1, sizeof(buf), stdin);
707 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
708 pfatal_with_name ("fwrite (temporary file)");
710 /* Try to make sure we won't remake the temporary
711 file when we are re-exec'd. Kludge-o-matic! */
712 fprintf (outfile, "%s:;\n", name);
713 (void) fclose (outfile);
715 /* Replace the name that read_all_makefiles will
716 see with the name of the temporary file. */
718 char *temp;
719 /* SGI compiler requires alloca's result be assigned simply. */
720 temp = (char *) alloca (sizeof (name));
721 bcopy (name, temp, sizeof (name));
722 makefiles->list[i] = temp;
725 /* Make sure the temporary file will not be remade. */
726 f = enter_file (savestring (name, sizeof name - 1));
727 f->updated = 1;
728 f->update_status = 0;
729 f->command_state = cs_finished;
730 /* Let it be removed when we're done. */
731 f->intermediate = 1;
732 /* But don't mention it. */
733 f->dontcare = 1;
737 /* Set up to handle children dying. This must be done before
738 reading in the makefiles so that `shell' function calls will work. */
740 #ifdef SIGCHLD
741 (void) signal (SIGCHLD, child_handler);
742 #endif
743 #ifdef SIGCLD
744 (void) signal (SIGCLD, child_handler);
745 #endif
747 /* Define the initial list of suffixes for old-style rules. */
749 set_default_suffixes ();
751 /* Define the file rules for the built-in suffix rules. These will later
752 be converted into pattern rules. We used to do this in
753 install_default_implicit_rules, but since that happens after reading
754 makefiles, it results in the built-in pattern rules taking precedence
755 over makefile-specified suffix rules, which is wrong. */
757 install_default_suffix_rules ();
759 /* Define some internal and special variables. */
761 define_automatic_variables ();
763 /* Set up the MAKEFLAGS and MFLAGS variables
764 so makefiles can look at them. */
766 define_makeflags (0, 0);
768 /* Define the default variables. */
769 define_default_variables ();
771 /* Read all the makefiles. */
773 default_file = enter_file (".DEFAULT");
775 read_makefiles
776 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
778 /* Decode switches again, in case the variables were set by the makefile. */
779 decode_env_switches ("MAKEFLAGS", 9);
780 #if 0
781 decode_env_switches ("MFLAGS", 6);
782 #endif
784 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
786 define_makeflags (1, 0);
788 /* Make each `struct dep' point at the `struct file' for the file
789 depended on. Also do magic for special targets. */
791 snap_deps ();
793 /* Convert old-style suffix rules to pattern rules. It is important to
794 do this before installing the built-in pattern rules below, so that
795 makefile-specified suffix rules take precedence over built-in pattern
796 rules. */
798 convert_to_pattern ();
800 /* Install the default implicit pattern rules.
801 This used to be done before reading the makefiles.
802 But in that case, built-in pattern rules were in the chain
803 before user-defined ones, so they matched first. */
805 install_default_implicit_rules ();
807 /* Compute implicit rule limits. */
809 count_implicit_rule_limits ();
811 /* Construct the listings of directories in VPATH lists. */
813 build_vpath_lists ();
815 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
816 and as having been updated already, and files given with -W flags as
817 brand new (time-stamp as far as possible into the future). */
819 if (old_files != 0)
820 for (p = old_files->list; *p != 0; ++p)
822 f = enter_command_line_file (*p);
823 f->last_mtime = (time_t) 1;
824 f->updated = 1;
825 f->update_status = 0;
826 f->command_state = cs_finished;
829 if (new_files != 0)
831 for (p = new_files->list; *p != 0; ++p)
833 f = enter_command_line_file (*p);
834 f->last_mtime = NEW_MTIME;
838 if (read_makefiles != 0)
840 /* Update any makefiles if necessary. */
842 time_t *makefile_mtimes = 0;
843 unsigned int mm_idx = 0;
845 if (debug_flag)
846 puts ("Updating makefiles....");
848 /* Remove any makefiles we don't want to try to update.
849 Also record the current modtimes so we can compare them later. */
851 register struct dep *d, *last;
852 last = 0;
853 d = read_makefiles;
854 while (d != 0)
856 register struct file *f = d->file;
857 if (f->double_colon)
858 for (f = f->double_colon; f != NULL; f = f->prev)
860 if (f->deps == 0 && f->cmds != 0)
862 /* This makefile is a :: target with commands, but
863 no dependencies. So, it will always be remade.
864 This might well cause an infinite loop, so don't
865 try to remake it. (This will only happen if
866 your makefiles are written exceptionally
867 stupidly; but if you work for Athena, that's how
868 you write your makefiles.) */
870 if (debug_flag)
871 printf ("Makefile `%s' might loop; not remaking it.\n",
872 f->name);
874 if (last == 0)
875 read_makefiles = d->next;
876 else
877 last->next = d->next;
879 /* Free the storage. */
880 free ((char *) d);
882 d = last == 0 ? 0 : last->next;
884 break;
887 if (f == NULL || !f->double_colon)
889 if (makefile_mtimes == 0)
890 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
891 else
892 makefile_mtimes = (time_t *)
893 xrealloc ((char *) makefile_mtimes,
894 (mm_idx + 1) * sizeof (time_t));
895 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
896 last = d;
897 d = d->next;
902 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
903 define_makeflags (1, 1);
905 switch (update_goal_chain (read_makefiles, 1))
907 default:
908 abort ();
910 case -1:
911 /* Did nothing. */
912 break;
914 case 1:
915 /* Failed to update. Figure out if we care. */
917 /* Nonzero if any makefile was successfully remade. */
918 int any_remade = 0;
919 /* Nonzero if any makefile we care about failed
920 in updating or could not be found at all. */
921 int any_failed = 0;
922 register unsigned int i;
924 for (i = 0; read_makefiles != 0; ++i)
926 struct dep *d = read_makefiles;
927 read_makefiles = d->next;
928 if (d->file->updated)
930 /* This makefile was updated. */
931 if (d->file->update_status == 0)
933 /* It was successfully updated. */
934 any_remade |= (file_mtime_no_search (d->file)
935 != makefile_mtimes[i]);
937 else if (! (d->changed & RM_DONTCARE))
939 time_t mtime;
940 /* The update failed and this makefile was not
941 from the MAKEFILES variable, so we care. */
942 error ("Failed to remake makefile `%s'.",
943 d->file->name);
944 mtime = file_mtime_no_search (d->file);
945 any_remade |= (mtime != (time_t) -1
946 && mtime != makefile_mtimes[i]);
949 else
950 /* This makefile was not found at all. */
951 if (! (d->changed & RM_DONTCARE))
953 /* This is a makefile we care about. See how much. */
954 if (d->changed & RM_INCLUDED)
955 /* An included makefile. We don't need
956 to die, but we do want to complain. */
957 error ("Included makefile `%s' was not found.",
958 dep_name (d));
959 else
961 /* A normal makefile. We must die later. */
962 error ("Makefile `%s' was not found", dep_name (d));
963 any_failed = 1;
967 free ((char *) d);
970 if (any_remade)
971 goto re_exec;
972 else if (any_failed)
973 die (2);
974 else
975 break;
978 case 0:
979 re_exec:
980 /* Updated successfully. Re-exec ourselves. */
982 remove_intermediates (0);
984 if (print_data_base_flag)
985 print_data_base ();
987 if (print_directory_flag)
988 log_working_directory (0);
990 if (makefiles != 0)
992 /* These names might have changed. */
993 register unsigned int i, j = 0;
994 for (i = 1; i < argc; ++i)
995 if (!strcmp (argv[i], "-f")) /* XXX */
997 char *p = &argv[i][2];
998 if (*p == '\0')
999 argv[++i] = makefiles->list[j];
1000 else
1001 argv[i] = concat ("-f", makefiles->list[j], "");
1002 ++j;
1006 if (directories != 0 && directories->idx > 0)
1008 char bad;
1009 if (directory_before_chdir != 0)
1011 if (chdir (directory_before_chdir) < 0)
1013 perror_with_name ("chdir", "");
1014 bad = 1;
1016 else
1017 bad = 0;
1019 else
1020 bad = 1;
1021 if (bad)
1022 fatal ("Couldn't change back to original directory.");
1025 for (p = environ; *p != 0; ++p)
1026 if (!strncmp (*p, "MAKELEVEL=", 10))
1028 /* The SGI compiler apparently can't understand
1029 the concept of storing the result of a function
1030 in something other than a local variable. */
1031 char *sgi_loses;
1032 sgi_loses = (char *) alloca (40);
1033 *p = sgi_loses;
1034 sprintf (*p, "MAKELEVEL=%u", makelevel);
1035 break;
1038 if (debug_flag)
1040 char **p;
1041 fputs ("Re-executing:", stdout);
1042 for (p = argv; *p != 0; ++p)
1043 printf (" %s", *p);
1044 puts ("");
1047 fflush (stdout);
1048 fflush (stderr);
1050 exec_command (argv, environ);
1051 /* NOTREACHED */
1055 /* Set up `MAKEFLAGS' again for the normal targets. */
1056 define_makeflags (1, 0);
1059 int status;
1061 /* If there were no command-line goals, use the default. */
1062 if (goals == 0)
1064 if (default_goal_file != 0)
1066 goals = (struct dep *) xmalloc (sizeof (struct dep));
1067 goals->next = 0;
1068 goals->name = 0;
1069 goals->file = default_goal_file;
1072 else
1073 lastgoal->next = 0;
1075 if (goals != 0)
1077 /* Update the goals. */
1079 if (debug_flag)
1080 puts ("Updating goal targets....");
1082 switch (update_goal_chain (goals, 0))
1084 case -1:
1085 /* Nothing happened. */
1086 case 0:
1087 /* Updated successfully. */
1088 status = 0;
1089 break;
1090 case 2:
1091 /* Updating failed. */
1092 status = 2;
1093 break;
1094 case 1:
1095 /* We are under -q and would run some commands. */
1096 status = 1;
1097 break;
1098 default:
1099 abort ();
1102 else
1104 if (read_makefiles == 0)
1105 fatal ("No targets specified and no makefile found");
1106 else
1107 fatal ("No targets");
1110 /* Exit. */
1111 die (status);
1114 return 0;
1117 /* Parsing of arguments, decoding of switches. */
1119 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1120 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1121 (sizeof (long_option_aliases) /
1122 sizeof (long_option_aliases[0]))];
1124 /* Fill in the string and vector for getopt. */
1125 static void
1126 init_switches ()
1128 register char *p;
1129 register int c;
1130 register unsigned int i;
1132 if (options[0] != '\0')
1133 /* Already done. */
1134 return;
1136 p = options;
1138 /* Return switch and non-switch args in order, regardless of
1139 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1140 *p++ = '-';
1142 for (i = 0; switches[i].c != '\0'; ++i)
1144 long_options[i].name = (switches[i].long_name == 0 ? "" :
1145 switches[i].long_name);
1146 long_options[i].flag = 0;
1147 long_options[i].val = switches[i].c;
1148 if (isalnum (switches[i].c))
1149 *p++ = switches[i].c;
1150 switch (switches[i].type)
1152 case flag:
1153 case flag_off:
1154 case ignore:
1155 long_options[i].has_arg = no_argument;
1156 break;
1158 case string:
1159 case positive_int:
1160 case floating:
1161 if (isalnum (switches[i].c))
1162 *p++ = ':';
1163 if (switches[i].noarg_value != 0)
1165 if (isalnum (switches[i].c))
1166 *p++ = ':';
1167 long_options[i].has_arg = optional_argument;
1169 else
1170 long_options[i].has_arg = required_argument;
1171 break;
1174 *p = '\0';
1175 for (c = 0; c < (sizeof (long_option_aliases) /
1176 sizeof (long_option_aliases[0]));
1177 ++c)
1178 long_options[i++] = long_option_aliases[c];
1179 long_options[i].name = 0;
1182 /* Decode switches from ARGC and ARGV.
1183 They came from the environment if ENV is nonzero. */
1185 static void
1186 decode_switches (argc, argv, env)
1187 int argc;
1188 char **argv;
1189 int env;
1191 int bad = 0;
1192 register const struct command_switch *cs;
1193 register struct stringlist *sl;
1194 register int c;
1196 /* getopt does most of the parsing for us.
1197 First, get its vectors set up. */
1199 init_switches ();
1201 /* Let getopt produce error messages for the command line,
1202 but not for options from the environment. */
1203 opterr = !env;
1204 /* Reset getopt's state. */
1205 optind = 0;
1207 c = 0;
1208 while (optind < argc)
1210 if (c == EOF)
1212 /* There are no more options according to getting getopt, but
1213 there are some arguments left. Since we have asked for
1214 non-option arguments to be returned in order, I think this
1215 only happens when there is a "--" argument to prevent later
1216 argument from being options. Since getopt has finished its
1217 job, just update its state variables for the next argument and
1218 set C as if it had returned 1, indicating a non-option
1219 argument. */
1220 optarg = argv[optind++];
1221 c = 1;
1223 else
1224 /* Parse the next argument. */
1225 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1227 if (c == 1)
1229 /* Non-option argument. It might be a variable definition. */
1230 struct variable *v;
1231 v = try_variable_definition ((char *) 0, 0, optarg, o_command);
1232 if (v != 0)
1234 /* It is indeed a variable definition. Record a pointer to
1235 the variable for later use in define_makeflags. */
1236 struct command_variable *cv
1237 = (struct command_variable *) xmalloc (sizeof (*cv));
1238 cv->variable = v;
1239 cv->next = command_variables;
1240 command_variables = cv;
1242 else if (! env)
1244 /* Not an option or variable definition; it must be a goal
1245 target! Enter it as a file and add it to the dep chain of
1246 goals. */
1247 struct file *f = enter_command_line_file (optarg);
1248 f->cmd_target = 1;
1250 if (goals == 0)
1252 goals = (struct dep *) xmalloc (sizeof (struct dep));
1253 lastgoal = goals;
1255 else
1257 lastgoal->next
1258 = (struct dep *) xmalloc (sizeof (struct dep));
1259 lastgoal = lastgoal->next;
1261 lastgoal->name = 0;
1262 lastgoal->file = f;
1265 else if (c == '?')
1266 /* Bad option. We will print a usage message and die later.
1267 But continue to parse the other options so the user can
1268 see all he did wrong. */
1269 bad = 1;
1270 else
1271 for (cs = switches; cs->c != '\0'; ++cs)
1272 if (cs->c == c)
1274 /* Whether or not we will actually do anything with
1275 this switch. We test this individually inside the
1276 switch below rather than just once outside it, so that
1277 options which are to be ignored still consume args. */
1278 int doit = !env || cs->env;
1280 switch (cs->type)
1282 default:
1283 abort ();
1285 case ignore:
1286 break;
1288 case flag:
1289 case flag_off:
1290 if (doit)
1291 *(int *) cs->value_ptr = cs->type == flag;
1292 break;
1294 case string:
1295 if (!doit)
1296 break;
1298 if (optarg == 0)
1299 optarg = cs->noarg_value;
1301 sl = *(struct stringlist **) cs->value_ptr;
1302 if (sl == 0)
1304 sl = (struct stringlist *)
1305 xmalloc (sizeof (struct stringlist));
1306 sl->max = 5;
1307 sl->idx = 0;
1308 sl->list = (char **) xmalloc (5 * sizeof (char *));
1309 *(struct stringlist **) cs->value_ptr = sl;
1311 else if (sl->idx == sl->max - 1)
1313 sl->max += 5;
1314 sl->list = (char **)
1315 xrealloc ((char *) sl->list,
1316 sl->max * sizeof (char *));
1318 sl->list[sl->idx++] = optarg;
1319 sl->list[sl->idx] = 0;
1320 break;
1322 case positive_int:
1323 if (optarg == 0 && argc > optind
1324 && isdigit (argv[optind][0]))
1325 optarg = argv[optind++];
1327 if (!doit)
1328 break;
1330 if (optarg != 0)
1332 int i = atoi (optarg);
1333 if (i < 1)
1335 if (doit)
1336 error ("the `-%c' option requires a \
1337 positive integral argument",
1338 cs->c);
1339 bad = 1;
1341 else
1342 *(unsigned int *) cs->value_ptr = i;
1344 else
1345 *(unsigned int *) cs->value_ptr
1346 = *(unsigned int *) cs->noarg_value;
1347 break;
1349 case floating:
1350 if (optarg == 0 && optind < argc
1351 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1352 optarg = argv[optind++];
1354 if (doit)
1355 *(double *) cs->value_ptr
1356 = (optarg != 0 ? atof (optarg)
1357 : *(double *) cs->noarg_value);
1359 break;
1362 /* We've found the switch. Stop looking. */
1363 break;
1367 if (!env && (bad || print_usage_flag))
1369 /* Print a nice usage message. */
1370 FILE *usageto;
1372 if (print_version_flag)
1373 print_version ();
1375 usageto = bad ? stderr : stdout;
1377 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1379 fputs ("Options:\n", usageto);
1380 for (cs = switches; cs->c != '\0'; ++cs)
1382 char buf[1024], shortarg[50], longarg[50], *p;
1384 if (cs->description[0] == '-')
1385 continue;
1387 switch (long_options[cs - switches].has_arg)
1389 case no_argument:
1390 shortarg[0] = longarg[0] = '\0';
1391 break;
1392 case required_argument:
1393 sprintf (longarg, "=%s", cs->argdesc);
1394 sprintf (shortarg, " %s", cs->argdesc);
1395 break;
1396 case optional_argument:
1397 sprintf (longarg, "[=%s]", cs->argdesc);
1398 sprintf (shortarg, " [%s]", cs->argdesc);
1399 break;
1402 p = buf;
1404 if (isalnum (cs->c))
1406 sprintf (buf, " -%c%s", cs->c, shortarg);
1407 p += strlen (p);
1409 if (cs->long_name != 0)
1411 unsigned int i;
1412 sprintf (p, "%s--%s%s",
1413 !isalnum (cs->c) ? " " : ", ",
1414 cs->long_name, longarg);
1415 p += strlen (p);
1416 for (i = 0; i < (sizeof (long_option_aliases) /
1417 sizeof (long_option_aliases[0]));
1418 ++i)
1419 if (long_option_aliases[i].val == cs->c)
1421 sprintf (p, ", --%s%s",
1422 long_option_aliases[i].name, longarg);
1423 p += strlen (p);
1427 const struct command_switch *ncs = cs;
1428 while ((++ncs)->c != '\0')
1429 if (ncs->description[0] == '-' &&
1430 ncs->description[1] == cs->c)
1432 /* This is another switch that does the same
1433 one as the one we are processing. We want
1434 to list them all together on one line. */
1435 sprintf (p, ", -%c%s", ncs->c, shortarg);
1436 p += strlen (p);
1437 if (ncs->long_name != 0)
1439 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1440 p += strlen (p);
1445 if (p - buf > DESCRIPTION_COLUMN - 2)
1446 /* The list of option names is too long to fit on the same
1447 line with the description, leaving at least two spaces.
1448 Print it on its own line instead. */
1450 fprintf (usageto, "%s\n", buf);
1451 buf[0] = '\0';
1454 fprintf (usageto, "%*s%s.\n",
1455 - DESCRIPTION_COLUMN,
1456 buf, cs->description);
1459 die (bad ? 2 : 0);
1463 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1464 We do this by chopping the value into a vector of words, prepending a
1465 dash to the first word if it lacks one, and passing the vector to
1466 decode_switches. */
1468 static void
1469 decode_env_switches (envar, len)
1470 char *envar;
1471 unsigned int len;
1473 char *varref = (char *) alloca (2 + len + 2);
1474 char *value, *p;
1475 int argc;
1476 char **argv;
1478 /* Get the variable's value. */
1479 varref[0] = '$';
1480 varref[1] = '(';
1481 bcopy (envar, &varref[2], len);
1482 varref[2 + len] = ')';
1483 varref[2 + len + 1] = '\0';
1484 value = variable_expand (varref);
1486 /* Skip whitespace, and check for an empty value. */
1487 value = next_token (value);
1488 len = strlen (value);
1489 if (len == 0)
1490 return;
1492 /* Allocate a vector that is definitely big enough. */
1493 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1495 /* Allocate a buffer to copy the value into while we split it into words
1496 and unquote it. We must use permanent storage for this because
1497 decode_switches may store pointers into the passed argument words. */
1498 p = (char *) xmalloc (2 * len);
1500 /* getopt will look at the arguments starting at ARGV[1].
1501 Prepend a spacer word. */
1502 argv[0] = 0;
1503 argc = 1;
1504 argv[argc] = p;
1505 while (*value != '\0')
1507 if (*value == '\\')
1508 ++value; /* Skip the backslash. */
1509 else if (isblank (*value))
1511 /* End of the word. */
1512 *p++ = '\0';
1513 argv[++argc] = p;
1515 ++value;
1516 while (isblank (*value));
1517 continue;
1519 *p++ = *value++;
1521 *p = '\0';
1522 argv[++argc] = 0;
1524 if (argc == 2 && argv[1][0] != '-')
1526 /* There is just one word in the value, and it is not a switch.
1527 Either this is the single-word form and we should prepend a dash
1528 before calling decode_switches, or this is the multi-word form and
1529 there is no dash because it is a variable definition. */
1530 struct variable *v;
1531 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1532 if (v != 0)
1534 /* It was indeed a variable definition, and now it has been
1535 processed. There is nothing for decode_switches to do.
1536 Record a pointer to the variable for later use in
1537 define_makeflags. */
1538 struct command_variable *cv
1539 = (struct command_variable *) xmalloc (sizeof (*cv));
1540 cv->variable = v;
1541 cv->next = command_variables;
1542 command_variables = cv;
1543 return;
1546 /* It wasn't a variable definition, so it's some switches without a
1547 leading dash. Add one and pass it along to decode_switches. We
1548 need permanent storage for this in case decode_switches saves
1549 pointers into the value. */
1550 argv[1] = concat ("-", argv[1], "");
1553 /* Parse those words. */
1554 decode_switches (argc, argv, 1);
1557 /* Quote the string IN so that it will be interpreted as a single word with
1558 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1559 signs to avoid variable expansion in make itself. Write the result into
1560 OUT, returning the address of the next character to be written.
1561 Allocating space for OUT twice the length of IN (thrice if
1562 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1564 static char *
1565 quote_as_word (out, in, double_dollars)
1566 char *out, *in;
1567 int double_dollars;
1569 while (*in != '\0')
1571 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1572 *out++ = '\\';
1573 if (double_dollars && *in == '$')
1574 *out++ = '$';
1575 *out++ = *in++;
1578 return out;
1581 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1582 command switches. Include options with args if ALL is nonzero.
1583 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1585 static void
1586 define_makeflags (all, makefile)
1587 int all, makefile;
1589 static const char ref[] = "$(MAKEOVERRIDES)";
1590 static const char posixref[] = "$(-*-command-variables-*-)";
1591 register const struct command_switch *cs;
1592 char *flagstring;
1593 register char *p;
1594 unsigned int words;
1595 struct variable *v;
1597 /* We will construct a linked list of `struct flag's describing
1598 all the flags which need to go in MAKEFLAGS. Then, once we
1599 know how many there are and their lengths, we can put them all
1600 together in a string. */
1602 struct flag
1604 struct flag *next;
1605 const struct command_switch *cs;
1606 char *arg;
1608 struct flag *flags = 0;
1609 unsigned int flagslen = 0;
1610 #define ADD_FLAG(ARG, LEN) \
1611 do { \
1612 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1613 new->cs = cs; \
1614 new->arg = (ARG); \
1615 new->next = flags; \
1616 flags = new; \
1617 if (new->arg == 0) \
1618 ++flagslen; /* Just a single flag letter. */ \
1619 else \
1620 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1621 if (!isalnum (cs->c)) \
1622 /* This switch has no single-letter version, so we use the long. */ \
1623 flagslen += 2 + strlen (cs->long_name); \
1624 } while (0)
1626 for (cs = switches; cs->c != '\0'; ++cs)
1627 if (cs->toenv && (!makefile || !cs->no_makefile))
1628 switch (cs->type)
1630 default:
1631 abort ();
1633 case ignore:
1634 break;
1636 case flag:
1637 case flag_off:
1638 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1639 && (cs->default_value == 0
1640 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1641 ADD_FLAG (0, 0);
1642 break;
1644 case positive_int:
1645 if (all)
1647 if ((cs->default_value != 0
1648 && (*(unsigned int *) cs->value_ptr
1649 == *(unsigned int *) cs->default_value)))
1650 break;
1651 else if (cs->noarg_value != 0
1652 && (*(unsigned int *) cs->value_ptr ==
1653 *(unsigned int *) cs->noarg_value))
1654 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1655 else if (cs->c == 'j')
1656 /* Special case for `-j'. */
1657 ADD_FLAG ("1", 1);
1658 else
1660 char *buf = (char *) alloca (30);
1661 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1662 ADD_FLAG (buf, strlen (buf));
1665 break;
1667 case floating:
1668 if (all)
1670 if (cs->default_value != 0
1671 && (*(double *) cs->value_ptr
1672 == *(double *) cs->default_value))
1673 break;
1674 else if (cs->noarg_value != 0
1675 && (*(double *) cs->value_ptr
1676 == *(double *) cs->noarg_value))
1677 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1678 else
1680 char *buf = (char *) alloca (100);
1681 sprintf (buf, "%g", *(double *) cs->value_ptr);
1682 ADD_FLAG (buf, strlen (buf));
1685 break;
1687 case string:
1688 if (all)
1690 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1691 if (sl != 0)
1693 /* Add the elements in reverse order, because
1694 all the flags get reversed below; and the order
1695 matters for some switches (like -I). */
1696 register unsigned int i = sl->idx;
1697 while (i-- > 0)
1698 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1701 break;
1704 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1706 #undef ADD_FLAG
1708 /* Construct the value in FLAGSTRING.
1709 We allocate enough space for a preceding dash and trailing null. */
1710 flagstring = (char *) alloca (1 + flagslen + 1);
1711 p = flagstring;
1712 words = 1;
1713 *p++ = '-';
1714 while (flags != 0)
1716 /* Add the flag letter or name to the string. */
1717 if (!isalnum (flags->cs->c))
1719 *p++ = '-';
1720 strcpy (p, flags->cs->long_name);
1721 p += strlen (p);
1723 else
1724 *p++ = flags->cs->c;
1725 if (flags->arg != 0)
1727 /* A flag that takes an optional argument which in this case is
1728 omitted is specified by ARG being "". We must distinguish
1729 because a following flag appended without an intervening " -"
1730 is considered the arg for the first. */
1731 if (flags->arg[0] != '\0')
1733 /* Add its argument too. */
1734 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1735 p = quote_as_word (p, flags->arg, 1);
1737 ++words;
1738 /* Write a following space and dash, for the next flag. */
1739 *p++ = ' ';
1740 *p++ = '-';
1742 else if (!isalnum (flags->cs->c))
1744 ++words;
1745 /* Long options must each go in their own word,
1746 so we write the following space and dash. */
1747 *p++ = ' ';
1748 *p++ = '-';
1750 flags = flags->next;
1753 if (all && command_variables != 0)
1755 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1756 command-line variable definitions. */
1758 if (p == &flagstring[1])
1759 /* No flags written, so elide the leading dash already written. */
1760 p = flagstring;
1761 else
1763 /* Separate the variables from the switches with a "--" arg. */
1764 if (p[-1] != '-')
1766 /* We did not already write a trailing " -". */
1767 *p++ = ' ';
1768 *p++ = '-';
1770 /* There is a trailing " -"; fill it out to " -- ". */
1771 *p++ = '-';
1772 *p++ = ' ';
1775 /* Copy in the string. */
1776 if (posix_pedantic)
1778 bcopy (posixref, p, sizeof posixref - 1);
1779 p += sizeof posixref - 1;
1781 else
1783 bcopy (ref, p, sizeof ref - 1);
1784 p += sizeof ref - 1;
1787 else if (p == &flagstring[1])
1789 words = 0;
1790 --p;
1792 else if (p[-1] == '-')
1793 /* Kill the final space and dash. */
1794 p -= 2;
1795 /* Terminate the string. */
1796 *p = '\0';
1798 v = define_variable ("MAKEFLAGS", 9,
1799 /* If there is just a single word of switches,
1800 omit the leading dash unless it is a single
1801 long option with two leading dashes. */
1802 &flagstring[(words == 1 && command_variables == 0
1803 && flagstring[1] != '-')
1804 ? 1 : 0],
1805 /* This used to use o_env, but that lost when a
1806 makefile defined MAKEFLAGS. Makefiles set
1807 MAKEFLAGS to add switches, but we still want
1808 to redefine its value with the full set of
1809 switches. Of course, an override or command
1810 definition will still take precedence. */
1811 o_file, 1);
1812 if (! all)
1813 /* The first time we are called, set MAKEFLAGS to always be exported.
1814 We should not do this again on the second call, because that is
1815 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1816 v->export = v_export;
1817 /* Since MFLAGS is not parsed for flags, there is no reason to
1818 override any makefile redefinition. */
1819 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1822 /* Print version information. */
1824 static void
1825 print_version ()
1827 static int printed_version = 0;
1829 char *precede = print_data_base_flag ? "# " : "";
1831 if (printed_version)
1832 /* Do it only once. */
1833 return;
1835 printf ("%sGNU Make version %s", precede, version_string);
1836 if (remote_description != 0 && *remote_description != '\0')
1837 printf ("-%s", remote_description);
1839 printf (", by Richard Stallman and Roland McGrath.\n\
1840 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.\n\
1841 %sThis is free software; see the source for copying conditions.\n\
1842 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1843 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1845 printed_version = 1;
1847 /* Flush stdout so the user doesn't have to wait to see the
1848 version information while things are thought about. */
1849 fflush (stdout);
1852 /* Print a bunch of information about this and that. */
1854 static void
1855 print_data_base ()
1857 extern char *ctime ();
1858 time_t when;
1860 when = time ((time_t *) 0);
1861 printf ("\n# Make data base, printed on %s", ctime (&when));
1863 print_variable_data_base ();
1864 print_dir_data_base ();
1865 print_rule_data_base ();
1866 print_file_data_base ();
1867 print_vpath_data_base ();
1869 when = time ((time_t *) 0);
1870 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1873 /* Exit with STATUS, cleaning up as necessary. */
1875 void
1876 die (status)
1877 int status;
1879 static char dying = 0;
1881 if (!dying)
1883 int err;
1885 dying = 1;
1887 /* Try to move back to the original directory. This is essential on
1888 MS-DOS (where there is really only one process), and on Unix it
1889 puts core files in the original directory instead of the -C
1890 directory. */
1891 if (directory_before_chdir != 0)
1892 chdir (directory_before_chdir);
1894 if (print_version_flag)
1895 print_version ();
1897 /* Wait for children to die. */
1898 for (err = status != 0; job_slots_used > 0; err = 0)
1899 reap_children (1, err);
1901 /* Remove the intermediate files. */
1902 remove_intermediates (0);
1904 if (print_data_base_flag)
1905 print_data_base ();
1907 if (print_directory_flag)
1908 log_working_directory (0);
1911 exit (status);
1914 /* Write a message indicating that we've just entered or
1915 left (according to ENTERING) the current directory. */
1917 static void
1918 log_working_directory (entering)
1919 int entering;
1921 static int entered = 0;
1922 char *message = entering ? "Entering" : "Leaving";
1924 if (entering)
1925 entered = 1;
1926 else if (!entered)
1927 /* Don't print the leaving message if we
1928 haven't printed the entering message. */
1929 return;
1931 if (print_data_base_flag)
1932 fputs ("# ", stdout);
1934 if (makelevel == 0)
1935 printf ("%s: %s ", program, message);
1936 else
1937 printf ("%s[%u]: %s ", program, makelevel, message);
1939 if (starting_directory == 0)
1940 puts ("an unknown directory");
1941 else
1942 printf ("directory `%s'\n", starting_directory);