Regenerated for 3.73
[make.git] / main.c
blob2c92793ce25ff2f81516ec71c8cdd97036c4155a
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 log_working_directory ();
47 static void print_data_base (), print_version ();
48 static void decode_switches (), decode_env_switches ();
49 static void define_makeflags ();
50 static char *quote_as_word ();
52 /* The structure that describes an accepted command switch. */
54 struct command_switch
56 char c; /* The switch character. */
58 enum /* Type of the value. */
60 flag, /* Turn int flag on. */
61 flag_off, /* Turn int flag off. */
62 string, /* One string per switch. */
63 positive_int, /* A positive integer. */
64 floating, /* A floating-point number (double). */
65 ignore /* Ignored. */
66 } type;
68 char *value_ptr; /* Pointer to the value-holding variable. */
70 unsigned int env:1; /* Can come from MAKEFLAGS. */
71 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
72 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
74 char *noarg_value; /* Pointer to value used if no argument is given. */
75 char *default_value;/* Pointer to default value. */
77 char *long_name; /* Long option name. */
78 char *argdesc; /* Descriptive word for argument. */
79 char *description; /* Description for usage message. */
83 /* The structure used to hold the list of strings given
84 in command switches of a type that takes string arguments. */
86 struct stringlist
88 char **list; /* Nil-terminated list of strings. */
89 unsigned int idx; /* Index into above. */
90 unsigned int max; /* Number of pointers allocated. */
94 /* The recognized command switches. */
96 /* Nonzero means do not print commands to be executed (-s). */
98 int silent_flag;
100 /* Nonzero means just touch the files
101 that would appear to need remaking (-t) */
103 int touch_flag;
105 /* Nonzero means just print what commands would need to be executed,
106 don't actually execute them (-n). */
108 int just_print_flag;
110 /* Print debugging trace info (-d). */
112 int debug_flag = 0;
114 /* Environment variables override makefile definitions. */
116 int env_overrides = 0;
118 /* Nonzero means ignore status codes returned by commands
119 executed to remake files. Just treat them all as successful (-i). */
121 int ignore_errors_flag = 0;
123 /* Nonzero means don't remake anything, just print the data base
124 that results from reading the makefile (-p). */
126 int print_data_base_flag = 0;
128 /* Nonzero means don't remake anything; just return a nonzero status
129 if the specified targets are not up to date (-q). */
131 int question_flag = 0;
133 /* Nonzero means do not use any of the builtin rules (-r). */
135 int no_builtin_rules_flag = 0;
137 /* Nonzero means keep going even if remaking some file fails (-k). */
139 int keep_going_flag;
140 int default_keep_going_flag = 0;
142 /* Nonzero means print directory before starting and when done (-w). */
144 int print_directory_flag = 0;
146 /* Nonzero means ignore print_directory_flag and never print the directory.
147 This is necessary because print_directory_flag is set implicitly. */
149 int inhibit_print_directory_flag = 0;
151 /* Nonzero means print version information. */
153 int print_version_flag = 0;
155 /* List of makefiles given with -f switches. */
157 static struct stringlist *makefiles = 0;
160 /* Number of job slots (commands that can be run at once). */
162 unsigned int job_slots = 1;
163 unsigned int default_job_slots = 1;
165 /* Value of job_slots that means no limit. */
167 static unsigned int inf_jobs = 0;
169 /* Maximum load average at which multiple jobs will be run.
170 Negative values mean unlimited, while zero means limit to
171 zero load (which could be useful to start infinite jobs remotely
172 but one at a time locally). */
174 double max_load_average = -1.0;
175 double default_load_average = -1.0;
177 /* List of directories given with -C switches. */
179 static struct stringlist *directories = 0;
181 /* List of include directories given with -I switches. */
183 static struct stringlist *include_directories = 0;
185 /* List of files given with -o switches. */
187 static struct stringlist *old_files = 0;
189 /* List of files given with -W switches. */
191 static struct stringlist *new_files = 0;
193 /* If nonzero, we should just print usage and exit. */
195 static int print_usage_flag = 0;
197 /* If nonzero, we should print a warning message
198 for each reference to an undefined variable. */
200 int warn_undefined_variables_flag;
202 /* The table of command switches. */
204 static const struct command_switch switches[] =
206 { 'b', ignore, 0, 0, 0, 0, 0, 0,
207 0, 0,
208 "Ignored for compatibility" },
209 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
210 "directory", "DIRECTORY",
211 "Change to DIRECTORY before doing anything" },
212 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
213 "debug", 0,
214 "Print lots of debugging information" },
215 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
216 "environment-overrides", 0,
217 "Environment variables override makefiles" },
218 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
219 "file", "FILE",
220 "Read FILE as a makefile" },
221 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
222 "help", 0,
223 "Print this message and exit" },
224 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
225 "ignore-errors", 0,
226 "Ignore errors from commands" },
227 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
228 "include-dir", "DIRECTORY",
229 "Search DIRECTORY for included makefiles" },
230 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
231 (char *) &inf_jobs, (char *) &default_job_slots,
232 "jobs", "N",
233 "Allow N jobs at once; infinite jobs with no arg" },
234 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
235 0, (char *) &default_keep_going_flag,
236 "keep-going", 0,
237 "Keep going when some targets can't be made" },
238 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
239 (char *) &default_load_average, (char *) &default_load_average,
240 "load-average", "N",
241 "Don't start multiple jobs unless load is below N" },
242 { 'm', ignore, 0, 0, 0, 0, 0, 0,
243 0, 0,
244 "-b" },
245 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
246 "just-print", 0,
247 "Don't actually run any commands; just print them" },
248 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
249 "old-file", "FILE",
250 "Consider FILE to be very old and don't remake it" },
251 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
252 "print-data-base", 0,
253 "Print make's internal database" },
254 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
255 "question", 0,
256 "Run no commands; exit status says if up to date" },
257 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
258 "no-builtin-rules", 0,
259 "Disable the built-in implicit rules" },
260 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
261 "silent", 0,
262 "Don't echo commands" },
263 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
264 0, (char *) &default_keep_going_flag,
265 "no-keep-going", 0,
266 "Turns off -k" },
267 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
268 "touch", 0,
269 "Touch targets instead of remaking them" },
270 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
271 "version", 0,
272 "Print the version number of make and exit" },
273 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
274 "print-directory", 0,
275 "Print the current directory" },
276 { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
277 "no-print-directory", 0,
278 "Turn off -w, even if it was turned on implicitly" },
279 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
280 "what-if", "FILE",
281 "Consider FILE to be infinitely new" },
282 { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
283 "warn-undefined-variables", 0,
284 "Warn when an undefined variable is referenced" },
285 { '\0', }
288 /* Secondary long names for options. */
290 static struct option long_option_aliases[] =
292 { "quiet", no_argument, 0, 's' },
293 { "stop", no_argument, 0, 'S' },
294 { "new-file", required_argument, 0, 'W' },
295 { "assume-new", required_argument, 0, 'W' },
296 { "assume-old", required_argument, 0, 'o' },
297 { "max-load", optional_argument, 0, 'l' },
298 { "dry-run", no_argument, 0, 'n' },
299 { "recon", no_argument, 0, 'n' },
300 { "makefile", required_argument, 0, 'f' },
303 /* The usage message prints the descriptions of options starting in
304 this column. Make sure it leaves enough room for the longest
305 description to fit in less than 80 characters. */
307 #define DESCRIPTION_COLUMN 30
309 /* List of goal targets. */
311 static struct dep *goals, *lastgoal;
313 /* List of variables which were defined on the command line
314 (or, equivalently, in MAKEFLAGS). */
316 struct command_variable
318 struct command_variable *next;
319 struct variable *variable;
321 static struct command_variable *command_variables;
323 /* The name we were invoked with. */
325 char *program;
327 /* Our current directory before processing any -C options. */
329 char *directory_before_chdir;
331 /* Our current directory after processing all -C options. */
333 char *starting_directory;
335 /* Value of the MAKELEVEL variable at startup (or 0). */
337 unsigned int makelevel;
339 /* First file defined in the makefile whose name does not
340 start with `.'. This is the default to remake if the
341 command line does not specify. */
343 struct file *default_goal_file;
345 /* Pointer to structure for the file .DEFAULT
346 whose commands are used for any file that has none of its own.
347 This is zero if the makefiles do not define .DEFAULT. */
349 struct file *default_file;
351 /* Nonzero if we have seen the magic `.POSIX' target.
352 This turns on pedantic compliance with POSIX.2. */
354 int posix_pedantic;
356 /* Mask of signals that are being caught with fatal_error_signal. */
358 #ifdef POSIX
359 sigset_t fatal_signal_set;
360 #else
361 #ifdef HAVE_SIGSETMASK
362 int fatal_signal_mask;
363 #endif
364 #endif
366 static struct file *
367 enter_command_line_file (name)
368 char *name;
370 if (name[0] == '~')
372 char *expanded = tilde_expand (name);
373 if (expanded != 0)
374 name = expanded; /* Memory leak; I don't care. */
377 /* This is also done in parse_file_seq, so this is redundant
378 for names read from makefiles. It is here for names passed
379 on the command line. */
380 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
382 name += 2;
383 while (*name == '/')
384 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
385 ++name;
388 if (*name == '\0')
390 /* It was all slashes! Move back to the dot and truncate
391 it after the first slash, so it becomes just "./". */
393 --name;
394 while (name[0] != '.');
395 name[2] = '\0';
398 return enter_file (savestring (name, strlen (name)));
401 /* Toggle -d on receipt of SIGUSR1. */
403 static RETSIGTYPE
404 debug_signal_handler (sig)
405 int sig;
407 debug_flag = ! debug_flag;
411 main (argc, argv, envp)
412 int argc;
413 char **argv;
414 char **envp;
416 extern void init_dir ();
417 extern RETSIGTYPE fatal_error_signal (), child_handler ();
418 register struct file *f;
419 register unsigned int i;
420 char **p;
421 struct dep *read_makefiles;
422 PATH_VAR (current_directory);
424 default_goal_file = 0;
425 reading_filename = 0;
426 reading_lineno_ptr = 0;
428 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
429 signame_init ();
430 #endif
432 #ifdef POSIX
433 sigemptyset (&fatal_signal_set);
434 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
435 #else
436 #ifdef HAVE_SIGSETMASK
437 fatal_signal_mask = 0;
438 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
439 #else
440 #define ADD_SIG(sig)
441 #endif
442 #endif
444 #define FATAL_SIG(sig) \
445 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
446 (void) signal ((sig), SIG_IGN); \
447 else \
448 ADD_SIG (sig);
450 FATAL_SIG (SIGHUP);
451 FATAL_SIG (SIGQUIT);
452 FATAL_SIG (SIGINT);
453 FATAL_SIG (SIGTERM);
455 #ifdef SIGDANGER
456 FATAL_SIG (SIGDANGER);
457 #endif
458 #ifdef SIGXCPU
459 FATAL_SIG (SIGXCPU);
460 #endif
461 #ifdef SIGXFSZ
462 FATAL_SIG (SIGXFSZ);
463 #endif
465 #undef FATAL_SIG
467 /* Make sure stdout is line-buffered. */
469 #ifdef HAVE_SETLINEBUF
470 setlinebuf (stdout);
471 #else
472 #ifndef SETVBUF_REVERSED
473 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
474 #else /* setvbuf not reversed. */
475 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
476 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
477 #endif /* setvbuf reversed. */
478 #endif /* setlinebuf missing. */
480 /* Initialize the directory hashing code. */
481 init_dir ();
483 /* Figure out where this program lives. */
485 if (argv[0] == 0)
486 argv[0] = "";
487 if (argv[0][0] == '\0')
488 program = "make";
489 else
491 program = rindex (argv[0], '/');
492 #ifdef __MSDOS__
493 if (program == 0)
494 program = rindex (argv[0], '\\');
495 if (program == 0)
496 program = rindex (argv[0], ':');
497 #endif
498 if (program == 0)
499 program = argv[0];
500 else
501 ++program;
504 /* Set up to access user data (files). */
505 user_access ();
507 /* Figure out where we are. */
509 if (getcwd (current_directory, GET_PATH_MAX) == 0)
511 #ifdef HAVE_GETCWD
512 perror_with_name ("getcwd: ", "");
513 #else
514 error ("getwd: %s", current_directory);
515 #endif
516 current_directory[0] = '\0';
517 directory_before_chdir = 0;
519 else
520 directory_before_chdir = savestring (current_directory,
521 strlen (current_directory));
523 /* Read in variables from the environment. It is important that this be
524 done before $(MAKE) is are figured out so its definitions will not be
525 one from the environment. */
527 for (i = 0; envp[i] != 0; ++i)
529 register char *ep = envp[i];
530 while (*ep != '=')
531 ++ep;
532 /* The result of pointer arithmetic is cast to unsigned int for
533 machines where ptrdiff_t is a different size that doesn't widen
534 the same. */
535 define_variable (envp[i], (unsigned int) (ep - envp[i]),
536 ep + 1, o_env, 1)
537 /* Force exportation of every variable culled from the environment.
538 We used to rely on target_environment's v_default code to do this.
539 But that does not work for the case where an environment variable
540 is redefined in a makefile with `override'; it should then still
541 be exported, because it was originally in the environment. */
542 ->export = v_export;
545 /* Decode the switches. */
547 decode_env_switches ("MAKEFLAGS", 9);
548 #if 0
549 /* People write things like:
550 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
551 and we set the -p, -i and -e switches. Doesn't seem quite right. */
552 decode_env_switches ("MFLAGS", 6);
553 #endif
554 decode_switches (argc, argv, 0);
556 /* Print version information. */
558 if (print_version_flag || print_data_base_flag || debug_flag)
559 print_version ();
561 /* `make --version' is supposed to just print the version and exit. */
562 if (print_version_flag)
563 die (0);
565 #ifndef __MSDOS__
566 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
567 (If it is a relative pathname with a slash, prepend our directory name
568 so the result will run the same program regardless of the current dir.
569 If it is a name with no slash, we can only hope that PATH did not
570 find it in the current directory.) */
572 if (current_directory[0] != '\0'
573 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
574 argv[0] = concat (current_directory, "/", argv[0]);
575 #endif
577 /* The extra indirection through $(MAKE_COMMAND) is done
578 for hysterical raisins. */
579 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
580 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
582 if (command_variables != 0)
584 struct command_variable *cv;
585 struct variable *v;
586 unsigned int len = 0;
587 char *value, *p;
589 /* Figure out how much space will be taken up by the command-line
590 variable definitions. */
591 for (cv = command_variables; cv != 0; cv = cv->next)
593 v = cv->variable;
594 len += 2 * strlen (v->name);
595 if (! v->recursive)
596 ++len;
597 ++len;
598 len += 2 * strlen (v->value);
601 /* Now allocate a buffer big enough and fill it. */
602 p = value = (char *) alloca (len);
603 for (cv = command_variables; cv != 0; cv = cv->next)
605 v = cv->variable;
606 p = quote_as_word (p, v->name, 0);
607 if (! v->recursive)
608 *p++ = ':';
609 *p++ = '=';
610 p = quote_as_word (p, v->value, 0);
611 *p++ = ' ';
613 p[-1] = '\0'; /* Kill the final space and terminate. */
615 /* Define an unchangeable variable with a name that no POSIX.2
616 makefile could validly use for its own variable. */
617 (void) define_variable ("-*-command-variables-*-", 23,
618 value, o_automatic, 0);
620 /* Define the variable; this will not override any user definition.
621 Normally a reference to this variable is written into the value of
622 MAKEFLAGS, allowing the user to override this value to affect the
623 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
624 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
625 a reference to this hidden variable is written instead. */
626 (void) define_variable ("MAKEOVERRIDES", 13,
627 "${-*-command-variables-*-}", o_env, 1);
630 /* If there were -C flags, move ourselves about. */
631 if (directories != 0)
632 for (i = 0; directories->list[i] != 0; ++i)
634 char *dir = directories->list[i];
635 if (dir[0] == '~')
637 char *expanded = tilde_expand (dir);
638 if (expanded != 0)
639 dir = expanded;
641 if (chdir (dir) < 0)
642 pfatal_with_name (dir);
643 if (dir != directories->list[i])
644 free (dir);
647 /* Figure out the level of recursion. */
649 struct variable *v = lookup_variable ("MAKELEVEL", 9);
650 if (v != 0 && *v->value != '\0' && *v->value != '-')
651 makelevel = (unsigned int) atoi (v->value);
652 else
653 makelevel = 0;
656 /* Except under -s, always do -w in sub-makes and under -C. */
657 if (!silent_flag && (directories != 0 || makelevel > 0))
658 print_directory_flag = 1;
660 /* Let the user disable that with --no-print-directory. */
661 if (inhibit_print_directory_flag)
662 print_directory_flag = 0;
664 /* Construct the list of include directories to search. */
666 construct_include_path (include_directories == 0 ? (char **) 0
667 : include_directories->list);
669 /* Figure out where we are now, after chdir'ing. */
670 if (directories == 0)
671 /* We didn't move, so we're still in the same place. */
672 starting_directory = current_directory;
673 else
675 if (getcwd (current_directory, GET_PATH_MAX) == 0)
677 #ifdef HAVE_GETCWD
678 perror_with_name ("getcwd: ", "");
679 #else
680 error ("getwd: %s", current_directory);
681 #endif
682 starting_directory = 0;
684 else
685 starting_directory = current_directory;
688 /* Tell the user where he is. */
690 if (print_directory_flag)
691 log_working_directory (1);
693 /* Read any stdin makefiles into temporary files. */
695 if (makefiles != 0)
697 register unsigned int i;
698 for (i = 0; i < makefiles->idx; ++i)
699 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
701 /* This makefile is standard input. Since we may re-exec
702 and thus re-read the makefiles, we read standard input
703 into a temporary file and read from that. */
704 FILE *outfile;
706 /* Make a unique filename. */
707 #ifdef HAVE_MKTEMP
708 static char name[] = "/tmp/GmXXXXXX";
709 (void) mktemp (name);
710 #else
711 static char name[L_tmpnam];
712 (void) tmpnam (name);
713 #endif
715 outfile = fopen (name, "w");
716 if (outfile == 0)
717 pfatal_with_name ("fopen (temporary file)");
718 while (!feof (stdin))
720 char buf[2048];
721 int n = fread (buf, 1, sizeof(buf), stdin);
722 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
723 pfatal_with_name ("fwrite (temporary file)");
725 /* Try to make sure we won't remake the temporary
726 file when we are re-exec'd. Kludge-o-matic! */
727 fprintf (outfile, "%s:;\n", name);
728 (void) fclose (outfile);
730 /* Replace the name that read_all_makefiles will
731 see with the name of the temporary file. */
733 char *temp;
734 /* SGI compiler requires alloca's result be assigned simply. */
735 temp = (char *) alloca (sizeof (name));
736 bcopy (name, temp, sizeof (name));
737 makefiles->list[i] = temp;
740 /* Make sure the temporary file will not be remade. */
741 f = enter_file (savestring (name, sizeof name - 1));
742 f->updated = 1;
743 f->update_status = 0;
744 f->command_state = cs_finished;
745 /* Let it be removed when we're done. */
746 f->intermediate = 1;
747 /* But don't mention it. */
748 f->dontcare = 1;
752 /* Set up to handle children dying. This must be done before
753 reading in the makefiles so that `shell' function calls will work. */
755 #ifdef SIGCHLD
756 (void) signal (SIGCHLD, child_handler);
757 #endif
758 #ifdef SIGCLD
759 (void) signal (SIGCLD, child_handler);
760 #endif
762 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
763 #ifdef SIGUSR1
764 (void) signal (SIGUSR1, debug_signal_handler);
765 #endif
767 /* Define the initial list of suffixes for old-style rules. */
769 set_default_suffixes ();
771 /* Define the file rules for the built-in suffix rules. These will later
772 be converted into pattern rules. We used to do this in
773 install_default_implicit_rules, but since that happens after reading
774 makefiles, it results in the built-in pattern rules taking precedence
775 over makefile-specified suffix rules, which is wrong. */
777 install_default_suffix_rules ();
779 /* Define some internal and special variables. */
781 define_automatic_variables ();
783 /* Set up the MAKEFLAGS and MFLAGS variables
784 so makefiles can look at them. */
786 define_makeflags (0, 0);
788 /* Define the default variables. */
789 define_default_variables ();
791 /* Read all the makefiles. */
793 default_file = enter_file (".DEFAULT");
795 read_makefiles
796 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
798 /* Decode switches again, in case the variables were set by the makefile. */
799 decode_env_switches ("MAKEFLAGS", 9);
800 #if 0
801 decode_env_switches ("MFLAGS", 6);
802 #endif
804 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
806 define_makeflags (1, 0);
808 /* Make each `struct dep' point at the `struct file' for the file
809 depended on. Also do magic for special targets. */
811 snap_deps ();
813 /* Convert old-style suffix rules to pattern rules. It is important to
814 do this before installing the built-in pattern rules below, so that
815 makefile-specified suffix rules take precedence over built-in pattern
816 rules. */
818 convert_to_pattern ();
820 /* Install the default implicit pattern rules.
821 This used to be done before reading the makefiles.
822 But in that case, built-in pattern rules were in the chain
823 before user-defined ones, so they matched first. */
825 install_default_implicit_rules ();
827 /* Compute implicit rule limits. */
829 count_implicit_rule_limits ();
831 /* Construct the listings of directories in VPATH lists. */
833 build_vpath_lists ();
835 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
836 and as having been updated already, and files given with -W flags as
837 brand new (time-stamp as far as possible into the future). */
839 if (old_files != 0)
840 for (p = old_files->list; *p != 0; ++p)
842 f = enter_command_line_file (*p);
843 f->last_mtime = (time_t) 1;
844 f->updated = 1;
845 f->update_status = 0;
846 f->command_state = cs_finished;
849 if (new_files != 0)
851 for (p = new_files->list; *p != 0; ++p)
853 f = enter_command_line_file (*p);
854 f->last_mtime = NEW_MTIME;
858 if (read_makefiles != 0)
860 /* Update any makefiles if necessary. */
862 time_t *makefile_mtimes = 0;
863 unsigned int mm_idx = 0;
865 if (debug_flag)
866 puts ("Updating makefiles....");
868 /* Remove any makefiles we don't want to try to update.
869 Also record the current modtimes so we can compare them later. */
871 register struct dep *d, *last;
872 last = 0;
873 d = read_makefiles;
874 while (d != 0)
876 register struct file *f = d->file;
877 if (f->double_colon)
878 for (f = f->double_colon; f != NULL; f = f->prev)
880 if (f->deps == 0 && f->cmds != 0)
882 /* This makefile is a :: target with commands, but
883 no dependencies. So, it will always be remade.
884 This might well cause an infinite loop, so don't
885 try to remake it. (This will only happen if
886 your makefiles are written exceptionally
887 stupidly; but if you work for Athena, that's how
888 you write your makefiles.) */
890 if (debug_flag)
891 printf ("Makefile `%s' might loop; not remaking it.\n",
892 f->name);
894 if (last == 0)
895 read_makefiles = d->next;
896 else
897 last->next = d->next;
899 /* Free the storage. */
900 free ((char *) d);
902 d = last == 0 ? 0 : last->next;
904 break;
907 if (f == NULL || !f->double_colon)
909 if (makefile_mtimes == 0)
910 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
911 else
912 makefile_mtimes = (time_t *)
913 xrealloc ((char *) makefile_mtimes,
914 (mm_idx + 1) * sizeof (time_t));
915 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
916 last = d;
917 d = d->next;
922 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
923 define_makeflags (1, 1);
925 switch (update_goal_chain (read_makefiles, 1))
927 case 1:
928 default:
929 #define BOGUS_UPDATE_STATUS 0
930 assert (BOGUS_UPDATE_STATUS);
931 break;
933 case -1:
934 /* Did nothing. */
935 break;
937 case 2:
938 /* Failed to update. Figure out if we care. */
940 /* Nonzero if any makefile was successfully remade. */
941 int any_remade = 0;
942 /* Nonzero if any makefile we care about failed
943 in updating or could not be found at all. */
944 int any_failed = 0;
945 register unsigned int i;
947 for (i = 0; read_makefiles != 0; ++i)
949 struct dep *d = read_makefiles;
950 read_makefiles = d->next;
951 if (d->file->updated)
953 /* This makefile was updated. */
954 if (d->file->update_status == 0)
956 /* It was successfully updated. */
957 any_remade |= (file_mtime_no_search (d->file)
958 != makefile_mtimes[i]);
960 else if (! (d->changed & RM_DONTCARE))
962 time_t mtime;
963 /* The update failed and this makefile was not
964 from the MAKEFILES variable, so we care. */
965 error ("Failed to remake makefile `%s'.",
966 d->file->name);
967 mtime = file_mtime_no_search (d->file);
968 any_remade |= (mtime != (time_t) -1
969 && mtime != makefile_mtimes[i]);
972 else
973 /* This makefile was not found at all. */
974 if (! (d->changed & RM_DONTCARE))
976 /* This is a makefile we care about. See how much. */
977 if (d->changed & RM_INCLUDED)
978 /* An included makefile. We don't need
979 to die, but we do want to complain. */
980 error ("Included makefile `%s' was not found.",
981 dep_name (d));
982 else
984 /* A normal makefile. We must die later. */
985 error ("Makefile `%s' was not found", dep_name (d));
986 any_failed = 1;
990 free ((char *) d);
993 if (any_remade)
994 goto re_exec;
995 else if (any_failed)
996 die (2);
997 else
998 break;
1001 case 0:
1002 re_exec:
1003 /* Updated successfully. Re-exec ourselves. */
1005 remove_intermediates (0);
1007 if (print_data_base_flag)
1008 print_data_base ();
1010 if (print_directory_flag)
1011 log_working_directory (0);
1013 if (makefiles != 0)
1015 /* These names might have changed. */
1016 register unsigned int i, j = 0;
1017 for (i = 1; i < argc; ++i)
1018 if (!strcmp (argv[i], "-f")) /* XXX */
1020 char *p = &argv[i][2];
1021 if (*p == '\0')
1022 argv[++i] = makefiles->list[j];
1023 else
1024 argv[i] = concat ("-f", makefiles->list[j], "");
1025 ++j;
1029 if (directories != 0 && directories->idx > 0)
1031 char bad;
1032 if (directory_before_chdir != 0)
1034 if (chdir (directory_before_chdir) < 0)
1036 perror_with_name ("chdir", "");
1037 bad = 1;
1039 else
1040 bad = 0;
1042 else
1043 bad = 1;
1044 if (bad)
1045 fatal ("Couldn't change back to original directory.");
1048 for (p = environ; *p != 0; ++p)
1049 if (!strncmp (*p, "MAKELEVEL=", 10))
1051 /* The SGI compiler apparently can't understand
1052 the concept of storing the result of a function
1053 in something other than a local variable. */
1054 char *sgi_loses;
1055 sgi_loses = (char *) alloca (40);
1056 *p = sgi_loses;
1057 sprintf (*p, "MAKELEVEL=%u", makelevel);
1058 break;
1061 if (debug_flag)
1063 char **p;
1064 fputs ("Re-executing:", stdout);
1065 for (p = argv; *p != 0; ++p)
1066 printf (" %s", *p);
1067 puts ("");
1070 fflush (stdout);
1071 fflush (stderr);
1073 exec_command (argv, environ);
1074 /* NOTREACHED */
1078 /* Set up `MAKEFLAGS' again for the normal targets. */
1079 define_makeflags (1, 0);
1082 int status;
1084 /* If there were no command-line goals, use the default. */
1085 if (goals == 0)
1087 if (default_goal_file != 0)
1089 goals = (struct dep *) xmalloc (sizeof (struct dep));
1090 goals->next = 0;
1091 goals->name = 0;
1092 goals->file = default_goal_file;
1095 else
1096 lastgoal->next = 0;
1098 if (goals != 0)
1100 /* Update the goals. */
1102 if (debug_flag)
1103 puts ("Updating goal targets....");
1105 switch (update_goal_chain (goals, 0))
1107 case -1:
1108 /* Nothing happened. */
1109 case 0:
1110 /* Updated successfully. */
1111 status = 0;
1112 break;
1113 case 2:
1114 /* Updating failed. */
1115 status = 2;
1116 break;
1117 case 1:
1118 /* We are under -q and would run some commands. */
1119 status = 1;
1120 break;
1121 default:
1122 abort ();
1125 else
1127 if (read_makefiles == 0)
1128 fatal ("No targets specified and no makefile found");
1129 else
1130 fatal ("No targets");
1133 /* Exit. */
1134 die (status);
1137 return 0;
1140 /* Parsing of arguments, decoding of switches. */
1142 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1143 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1144 (sizeof (long_option_aliases) /
1145 sizeof (long_option_aliases[0]))];
1147 /* Fill in the string and vector for getopt. */
1148 static void
1149 init_switches ()
1151 register char *p;
1152 register int c;
1153 register unsigned int i;
1155 if (options[0] != '\0')
1156 /* Already done. */
1157 return;
1159 p = options;
1161 /* Return switch and non-switch args in order, regardless of
1162 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1163 *p++ = '-';
1165 for (i = 0; switches[i].c != '\0'; ++i)
1167 long_options[i].name = (switches[i].long_name == 0 ? "" :
1168 switches[i].long_name);
1169 long_options[i].flag = 0;
1170 long_options[i].val = switches[i].c;
1171 if (isalnum (switches[i].c))
1172 *p++ = switches[i].c;
1173 switch (switches[i].type)
1175 case flag:
1176 case flag_off:
1177 case ignore:
1178 long_options[i].has_arg = no_argument;
1179 break;
1181 case string:
1182 case positive_int:
1183 case floating:
1184 if (isalnum (switches[i].c))
1185 *p++ = ':';
1186 if (switches[i].noarg_value != 0)
1188 if (isalnum (switches[i].c))
1189 *p++ = ':';
1190 long_options[i].has_arg = optional_argument;
1192 else
1193 long_options[i].has_arg = required_argument;
1194 break;
1197 *p = '\0';
1198 for (c = 0; c < (sizeof (long_option_aliases) /
1199 sizeof (long_option_aliases[0]));
1200 ++c)
1201 long_options[i++] = long_option_aliases[c];
1202 long_options[i].name = 0;
1205 static void
1206 handle_non_switch_argument (arg, env)
1207 char *arg;
1208 int env;
1210 /* Non-option argument. It might be a variable definition. */
1211 struct variable *v;
1212 if (arg[0] == '-' && arg[1] == '\0')
1213 /* Ignore plain `-' for compatibility. */
1214 return;
1215 v = try_variable_definition ((char *) 0, 0, arg, o_command);
1216 if (v != 0)
1218 /* It is indeed a variable definition. Record a pointer to
1219 the variable for later use in define_makeflags. */
1220 struct command_variable *cv
1221 = (struct command_variable *) xmalloc (sizeof (*cv));
1222 cv->variable = v;
1223 cv->next = command_variables;
1224 command_variables = cv;
1226 else if (! env)
1228 /* Not an option or variable definition; it must be a goal
1229 target! Enter it as a file and add it to the dep chain of
1230 goals. */
1231 struct file *f = enter_command_line_file (arg);
1232 f->cmd_target = 1;
1234 if (goals == 0)
1236 goals = (struct dep *) xmalloc (sizeof (struct dep));
1237 lastgoal = goals;
1239 else
1241 lastgoal->next
1242 = (struct dep *) xmalloc (sizeof (struct dep));
1243 lastgoal = lastgoal->next;
1245 lastgoal->name = 0;
1246 lastgoal->file = f;
1250 /* Decode switches from ARGC and ARGV.
1251 They came from the environment if ENV is nonzero. */
1253 static void
1254 decode_switches (argc, argv, env)
1255 int argc;
1256 char **argv;
1257 int env;
1259 int bad = 0;
1260 register const struct command_switch *cs;
1261 register struct stringlist *sl;
1262 register int c;
1264 /* getopt does most of the parsing for us.
1265 First, get its vectors set up. */
1267 init_switches ();
1269 /* Let getopt produce error messages for the command line,
1270 but not for options from the environment. */
1271 opterr = !env;
1272 /* Reset getopt's state. */
1273 optind = 0;
1275 while (optind < argc)
1277 /* Parse the next argument. */
1278 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1279 if (c == EOF)
1280 /* End of arguments, or "--" marker seen. */
1281 break;
1282 else if (c == 1)
1283 /* An argument not starting with a dash. */
1284 handle_non_switch_argument (optarg, env);
1285 else if (c == '?')
1286 /* Bad option. We will print a usage message and die later.
1287 But continue to parse the other options so the user can
1288 see all he did wrong. */
1289 bad = 1;
1290 else
1291 for (cs = switches; cs->c != '\0'; ++cs)
1292 if (cs->c == c)
1294 /* Whether or not we will actually do anything with
1295 this switch. We test this individually inside the
1296 switch below rather than just once outside it, so that
1297 options which are to be ignored still consume args. */
1298 int doit = !env || cs->env;
1300 switch (cs->type)
1302 default:
1303 abort ();
1305 case ignore:
1306 break;
1308 case flag:
1309 case flag_off:
1310 if (doit)
1311 *(int *) cs->value_ptr = cs->type == flag;
1312 break;
1314 case string:
1315 if (!doit)
1316 break;
1318 if (optarg == 0)
1319 optarg = cs->noarg_value;
1321 sl = *(struct stringlist **) cs->value_ptr;
1322 if (sl == 0)
1324 sl = (struct stringlist *)
1325 xmalloc (sizeof (struct stringlist));
1326 sl->max = 5;
1327 sl->idx = 0;
1328 sl->list = (char **) xmalloc (5 * sizeof (char *));
1329 *(struct stringlist **) cs->value_ptr = sl;
1331 else if (sl->idx == sl->max - 1)
1333 sl->max += 5;
1334 sl->list = (char **)
1335 xrealloc ((char *) sl->list,
1336 sl->max * sizeof (char *));
1338 sl->list[sl->idx++] = optarg;
1339 sl->list[sl->idx] = 0;
1340 break;
1342 case positive_int:
1343 if (optarg == 0 && argc > optind
1344 && isdigit (argv[optind][0]))
1345 optarg = argv[optind++];
1347 if (!doit)
1348 break;
1350 if (optarg != 0)
1352 int i = atoi (optarg);
1353 if (i < 1)
1355 if (doit)
1356 error ("the `-%c' option requires a \
1357 positive integral argument",
1358 cs->c);
1359 bad = 1;
1361 else
1362 *(unsigned int *) cs->value_ptr = i;
1364 else
1365 *(unsigned int *) cs->value_ptr
1366 = *(unsigned int *) cs->noarg_value;
1367 break;
1369 case floating:
1370 if (optarg == 0 && optind < argc
1371 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1372 optarg = argv[optind++];
1374 if (doit)
1375 *(double *) cs->value_ptr
1376 = (optarg != 0 ? atof (optarg)
1377 : *(double *) cs->noarg_value);
1379 break;
1382 /* We've found the switch. Stop looking. */
1383 break;
1387 /* There are no more options according to getting getopt, but there may
1388 be some arguments left. Since we have asked for non-option arguments
1389 to be returned in order, this only happens when there is a "--"
1390 argument to prevent later arguments from being options. */
1391 while (optind < argc)
1392 handle_non_switch_argument (argv[optind++], env);
1395 if (!env && (bad || print_usage_flag))
1397 /* Print a nice usage message. */
1398 FILE *usageto;
1400 if (print_version_flag)
1401 print_version ();
1403 usageto = bad ? stderr : stdout;
1405 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1407 fputs ("Options:\n", usageto);
1408 for (cs = switches; cs->c != '\0'; ++cs)
1410 char buf[1024], shortarg[50], longarg[50], *p;
1412 if (cs->description[0] == '-')
1413 continue;
1415 switch (long_options[cs - switches].has_arg)
1417 case no_argument:
1418 shortarg[0] = longarg[0] = '\0';
1419 break;
1420 case required_argument:
1421 sprintf (longarg, "=%s", cs->argdesc);
1422 sprintf (shortarg, " %s", cs->argdesc);
1423 break;
1424 case optional_argument:
1425 sprintf (longarg, "[=%s]", cs->argdesc);
1426 sprintf (shortarg, " [%s]", cs->argdesc);
1427 break;
1430 p = buf;
1432 if (isalnum (cs->c))
1434 sprintf (buf, " -%c%s", cs->c, shortarg);
1435 p += strlen (p);
1437 if (cs->long_name != 0)
1439 unsigned int i;
1440 sprintf (p, "%s--%s%s",
1441 !isalnum (cs->c) ? " " : ", ",
1442 cs->long_name, longarg);
1443 p += strlen (p);
1444 for (i = 0; i < (sizeof (long_option_aliases) /
1445 sizeof (long_option_aliases[0]));
1446 ++i)
1447 if (long_option_aliases[i].val == cs->c)
1449 sprintf (p, ", --%s%s",
1450 long_option_aliases[i].name, longarg);
1451 p += strlen (p);
1455 const struct command_switch *ncs = cs;
1456 while ((++ncs)->c != '\0')
1457 if (ncs->description[0] == '-' &&
1458 ncs->description[1] == cs->c)
1460 /* This is another switch that does the same
1461 one as the one we are processing. We want
1462 to list them all together on one line. */
1463 sprintf (p, ", -%c%s", ncs->c, shortarg);
1464 p += strlen (p);
1465 if (ncs->long_name != 0)
1467 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1468 p += strlen (p);
1473 if (p - buf > DESCRIPTION_COLUMN - 2)
1474 /* The list of option names is too long to fit on the same
1475 line with the description, leaving at least two spaces.
1476 Print it on its own line instead. */
1478 fprintf (usageto, "%s\n", buf);
1479 buf[0] = '\0';
1482 fprintf (usageto, "%*s%s.\n",
1483 - DESCRIPTION_COLUMN,
1484 buf, cs->description);
1487 die (bad ? 2 : 0);
1491 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1492 We do this by chopping the value into a vector of words, prepending a
1493 dash to the first word if it lacks one, and passing the vector to
1494 decode_switches. */
1496 static void
1497 decode_env_switches (envar, len)
1498 char *envar;
1499 unsigned int len;
1501 char *varref = (char *) alloca (2 + len + 2);
1502 char *value, *p;
1503 int argc;
1504 char **argv;
1506 /* Get the variable's value. */
1507 varref[0] = '$';
1508 varref[1] = '(';
1509 bcopy (envar, &varref[2], len);
1510 varref[2 + len] = ')';
1511 varref[2 + len + 1] = '\0';
1512 value = variable_expand (varref);
1514 /* Skip whitespace, and check for an empty value. */
1515 value = next_token (value);
1516 len = strlen (value);
1517 if (len == 0)
1518 return;
1520 /* Allocate a vector that is definitely big enough. */
1521 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1523 /* Allocate a buffer to copy the value into while we split it into words
1524 and unquote it. We must use permanent storage for this because
1525 decode_switches may store pointers into the passed argument words. */
1526 p = (char *) xmalloc (2 * len);
1528 /* getopt will look at the arguments starting at ARGV[1].
1529 Prepend a spacer word. */
1530 argv[0] = 0;
1531 argc = 1;
1532 argv[argc] = p;
1533 while (*value != '\0')
1535 if (*value == '\\')
1536 ++value; /* Skip the backslash. */
1537 else if (isblank (*value))
1539 /* End of the word. */
1540 *p++ = '\0';
1541 argv[++argc] = p;
1543 ++value;
1544 while (isblank (*value));
1545 continue;
1547 *p++ = *value++;
1549 *p = '\0';
1550 argv[++argc] = 0;
1552 if (argc == 2 && argv[1][0] != '-')
1554 /* There is just one word in the value, and it is not a switch.
1555 Either this is the single-word form and we should prepend a dash
1556 before calling decode_switches, or this is the multi-word form and
1557 there is no dash because it is a variable definition. */
1558 struct variable *v;
1559 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1560 if (v != 0)
1562 /* It was indeed a variable definition, and now it has been
1563 processed. There is nothing for decode_switches to do.
1564 Record a pointer to the variable for later use in
1565 define_makeflags. */
1566 struct command_variable *cv
1567 = (struct command_variable *) xmalloc (sizeof (*cv));
1568 cv->variable = v;
1569 cv->next = command_variables;
1570 command_variables = cv;
1571 return;
1574 /* It wasn't a variable definition, so it's some switches without a
1575 leading dash. Add one and pass it along to decode_switches. We
1576 need permanent storage for this in case decode_switches saves
1577 pointers into the value. */
1578 argv[1] = concat ("-", argv[1], "");
1581 /* Parse those words. */
1582 decode_switches (argc, argv, 1);
1585 /* Quote the string IN so that it will be interpreted as a single word with
1586 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1587 signs to avoid variable expansion in make itself. Write the result into
1588 OUT, returning the address of the next character to be written.
1589 Allocating space for OUT twice the length of IN (thrice if
1590 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1592 static char *
1593 quote_as_word (out, in, double_dollars)
1594 char *out, *in;
1595 int double_dollars;
1597 while (*in != '\0')
1599 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1600 *out++ = '\\';
1601 if (double_dollars && *in == '$')
1602 *out++ = '$';
1603 *out++ = *in++;
1606 return out;
1609 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1610 command switches. Include options with args if ALL is nonzero.
1611 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1613 static void
1614 define_makeflags (all, makefile)
1615 int all, makefile;
1617 static const char ref[] = "$(MAKEOVERRIDES)";
1618 static const char posixref[] = "$(-*-command-variables-*-)";
1619 register const struct command_switch *cs;
1620 char *flagstring;
1621 register char *p;
1622 unsigned int words;
1623 struct variable *v;
1625 /* We will construct a linked list of `struct flag's describing
1626 all the flags which need to go in MAKEFLAGS. Then, once we
1627 know how many there are and their lengths, we can put them all
1628 together in a string. */
1630 struct flag
1632 struct flag *next;
1633 const struct command_switch *cs;
1634 char *arg;
1636 struct flag *flags = 0;
1637 unsigned int flagslen = 0;
1638 #define ADD_FLAG(ARG, LEN) \
1639 do { \
1640 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1641 new->cs = cs; \
1642 new->arg = (ARG); \
1643 new->next = flags; \
1644 flags = new; \
1645 if (new->arg == 0) \
1646 ++flagslen; /* Just a single flag letter. */ \
1647 else \
1648 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1649 if (!isalnum (cs->c)) \
1650 /* This switch has no single-letter version, so we use the long. */ \
1651 flagslen += 2 + strlen (cs->long_name); \
1652 } while (0)
1654 for (cs = switches; cs->c != '\0'; ++cs)
1655 if (cs->toenv && (!makefile || !cs->no_makefile))
1656 switch (cs->type)
1658 default:
1659 abort ();
1661 case ignore:
1662 break;
1664 case flag:
1665 case flag_off:
1666 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1667 && (cs->default_value == 0
1668 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1669 ADD_FLAG (0, 0);
1670 break;
1672 case positive_int:
1673 if (all)
1675 if ((cs->default_value != 0
1676 && (*(unsigned int *) cs->value_ptr
1677 == *(unsigned int *) cs->default_value)))
1678 break;
1679 else if (cs->noarg_value != 0
1680 && (*(unsigned int *) cs->value_ptr ==
1681 *(unsigned int *) cs->noarg_value))
1682 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1683 else if (cs->c == 'j')
1684 /* Special case for `-j'. */
1685 ADD_FLAG ("1", 1);
1686 else
1688 char *buf = (char *) alloca (30);
1689 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1690 ADD_FLAG (buf, strlen (buf));
1693 break;
1695 case floating:
1696 if (all)
1698 if (cs->default_value != 0
1699 && (*(double *) cs->value_ptr
1700 == *(double *) cs->default_value))
1701 break;
1702 else if (cs->noarg_value != 0
1703 && (*(double *) cs->value_ptr
1704 == *(double *) cs->noarg_value))
1705 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1706 else
1708 char *buf = (char *) alloca (100);
1709 sprintf (buf, "%g", *(double *) cs->value_ptr);
1710 ADD_FLAG (buf, strlen (buf));
1713 break;
1715 case string:
1716 if (all)
1718 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1719 if (sl != 0)
1721 /* Add the elements in reverse order, because
1722 all the flags get reversed below; and the order
1723 matters for some switches (like -I). */
1724 register unsigned int i = sl->idx;
1725 while (i-- > 0)
1726 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1729 break;
1732 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1734 #undef ADD_FLAG
1736 /* Construct the value in FLAGSTRING.
1737 We allocate enough space for a preceding dash and trailing null. */
1738 flagstring = (char *) alloca (1 + flagslen + 1);
1739 p = flagstring;
1740 words = 1;
1741 *p++ = '-';
1742 while (flags != 0)
1744 /* Add the flag letter or name to the string. */
1745 if (!isalnum (flags->cs->c))
1747 *p++ = '-';
1748 strcpy (p, flags->cs->long_name);
1749 p += strlen (p);
1751 else
1752 *p++ = flags->cs->c;
1753 if (flags->arg != 0)
1755 /* A flag that takes an optional argument which in this case is
1756 omitted is specified by ARG being "". We must distinguish
1757 because a following flag appended without an intervening " -"
1758 is considered the arg for the first. */
1759 if (flags->arg[0] != '\0')
1761 /* Add its argument too. */
1762 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1763 p = quote_as_word (p, flags->arg, 1);
1765 ++words;
1766 /* Write a following space and dash, for the next flag. */
1767 *p++ = ' ';
1768 *p++ = '-';
1770 else if (!isalnum (flags->cs->c))
1772 ++words;
1773 /* Long options must each go in their own word,
1774 so we write the following space and dash. */
1775 *p++ = ' ';
1776 *p++ = '-';
1778 flags = flags->next;
1781 if (all && command_variables != 0)
1783 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1784 command-line variable definitions. */
1786 if (p == &flagstring[1])
1787 /* No flags written, so elide the leading dash already written. */
1788 p = flagstring;
1789 else
1791 /* Separate the variables from the switches with a "--" arg. */
1792 if (p[-1] != '-')
1794 /* We did not already write a trailing " -". */
1795 *p++ = ' ';
1796 *p++ = '-';
1798 /* There is a trailing " -"; fill it out to " -- ". */
1799 *p++ = '-';
1800 *p++ = ' ';
1803 /* Copy in the string. */
1804 if (posix_pedantic)
1806 bcopy (posixref, p, sizeof posixref - 1);
1807 p += sizeof posixref - 1;
1809 else
1811 bcopy (ref, p, sizeof ref - 1);
1812 p += sizeof ref - 1;
1815 else if (p == &flagstring[1])
1817 words = 0;
1818 --p;
1820 else if (p[-1] == '-')
1821 /* Kill the final space and dash. */
1822 p -= 2;
1823 /* Terminate the string. */
1824 *p = '\0';
1826 v = define_variable ("MAKEFLAGS", 9,
1827 /* If there is just a single word of switches,
1828 omit the leading dash unless it is a single
1829 long option with two leading dashes. */
1830 &flagstring[(words == 1 && command_variables == 0
1831 && flagstring[1] != '-')
1832 ? 1 : 0],
1833 /* This used to use o_env, but that lost when a
1834 makefile defined MAKEFLAGS. Makefiles set
1835 MAKEFLAGS to add switches, but we still want
1836 to redefine its value with the full set of
1837 switches. Of course, an override or command
1838 definition will still take precedence. */
1839 o_file, 1);
1840 if (! all)
1841 /* The first time we are called, set MAKEFLAGS to always be exported.
1842 We should not do this again on the second call, because that is
1843 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1844 v->export = v_export;
1845 /* Since MFLAGS is not parsed for flags, there is no reason to
1846 override any makefile redefinition. */
1847 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1850 /* Print version information. */
1852 static void
1853 print_version ()
1855 static int printed_version = 0;
1857 char *precede = print_data_base_flag ? "# " : "";
1859 if (printed_version)
1860 /* Do it only once. */
1861 return;
1863 printf ("%sGNU Make version %s", precede, version_string);
1864 if (remote_description != 0 && *remote_description != '\0')
1865 printf ("-%s", remote_description);
1867 printf (", by Richard Stallman and Roland McGrath.\n\
1868 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
1869 %sThis is free software; see the source for copying conditions.\n\
1870 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1871 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1873 printed_version = 1;
1875 /* Flush stdout so the user doesn't have to wait to see the
1876 version information while things are thought about. */
1877 fflush (stdout);
1880 /* Print a bunch of information about this and that. */
1882 static void
1883 print_data_base ()
1885 extern char *ctime ();
1886 time_t when;
1888 when = time ((time_t *) 0);
1889 printf ("\n# Make data base, printed on %s", ctime (&when));
1891 print_variable_data_base ();
1892 print_dir_data_base ();
1893 print_rule_data_base ();
1894 print_file_data_base ();
1895 print_vpath_data_base ();
1897 when = time ((time_t *) 0);
1898 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1901 /* Exit with STATUS, cleaning up as necessary. */
1903 void
1904 die (status)
1905 int status;
1907 static char dying = 0;
1909 if (!dying)
1911 int err;
1913 dying = 1;
1915 /* Try to move back to the original directory. This is essential on
1916 MS-DOS (where there is really only one process), and on Unix it
1917 puts core files in the original directory instead of the -C
1918 directory. */
1919 if (directory_before_chdir != 0)
1920 chdir (directory_before_chdir);
1922 if (print_version_flag)
1923 print_version ();
1925 /* Wait for children to die. */
1926 for (err = status != 0; job_slots_used > 0; err = 0)
1927 reap_children (1, err);
1929 /* Remove the intermediate files. */
1930 remove_intermediates (0);
1932 if (print_data_base_flag)
1933 print_data_base ();
1935 if (print_directory_flag)
1936 log_working_directory (0);
1939 exit (status);
1942 /* Write a message indicating that we've just entered or
1943 left (according to ENTERING) the current directory. */
1945 static void
1946 log_working_directory (entering)
1947 int entering;
1949 static int entered = 0;
1950 char *message = entering ? "Entering" : "Leaving";
1952 if (entering)
1953 entered = 1;
1954 else if (!entered)
1955 /* Don't print the leaving message if we
1956 haven't printed the entering message. */
1957 return;
1959 if (print_data_base_flag)
1960 fputs ("# ", stdout);
1962 if (makelevel == 0)
1963 printf ("%s: %s ", program, message);
1964 else
1965 printf ("%s[%u]: %s ", program, makelevel, message);
1967 if (starting_directory == 0)
1968 puts ("an unknown directory");
1969 else
1970 printf ("directory `%s'\n", starting_directory);