moved to filedef.h
[make.git] / main.c
blobefbd70ee6c40d80045d3dd5f8c51d4bc71de0b4f
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 "dep.h"
21 #include "filedef.h"
22 #include "variable.h"
23 #include "job.h"
24 #include "commands.h"
25 #include "getopt.h"
26 #include <assert.h>
28 extern void init_dir PARAMS ((void));
29 extern RETSIGTYPE fatal_error_signal PARAMS ((int sig));
30 extern RETSIGTYPE child_handler PARAMS ((int sig));
32 extern void print_variable_data_base PARAMS ((void));
33 extern void print_dir_data_base PARAMS ((void));
34 extern void print_rule_data_base PARAMS ((void));
35 extern void print_file_data_base PARAMS ((void));
36 extern void print_vpath_data_base PARAMS ((void));
38 #ifndef HAVE_UNISTD_H
39 extern int chdir ();
40 #endif
41 #ifndef STDC_HEADERS
42 #ifndef sun /* Sun has an incorrect decl in a header. */
43 extern void exit ();
44 #endif
45 extern double atof ();
46 #endif
47 extern char *mktemp ();
49 static void print_data_base PARAMS((void));
50 static void print_version PARAMS ((void));
51 static void decode_switches PARAMS ((int argc, char **argv, int env));
52 static void decode_env_switches PARAMS ((char *envar, unsigned int len));
53 static void define_makeflags PARAMS ((int all, int makefile));
54 static char *quote_as_word PARAMS ((char *out, char *in, int double_dollars));
56 /* The structure that describes an accepted command switch. */
58 struct command_switch
60 char c; /* The switch character. */
62 enum /* Type of the value. */
64 flag, /* Turn int flag on. */
65 flag_off, /* Turn int flag off. */
66 string, /* One string per switch. */
67 positive_int, /* A positive integer. */
68 floating, /* A floating-point number (double). */
69 ignore /* Ignored. */
70 } type;
72 char *value_ptr; /* Pointer to the value-holding variable. */
74 unsigned int env:1; /* Can come from MAKEFLAGS. */
75 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
76 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
78 char *noarg_value; /* Pointer to value used if no argument is given. */
79 char *default_value;/* Pointer to default value. */
81 char *long_name; /* Long option name. */
82 char *argdesc; /* Descriptive word for argument. */
83 char *description; /* Description for usage message. */
87 /* The structure used to hold the list of strings given
88 in command switches of a type that takes string arguments. */
90 struct stringlist
92 char **list; /* Nil-terminated list of strings. */
93 unsigned int idx; /* Index into above. */
94 unsigned int max; /* Number of pointers allocated. */
98 /* The recognized command switches. */
100 /* Nonzero means do not print commands to be executed (-s). */
102 int silent_flag;
104 /* Nonzero means just touch the files
105 that would appear to need remaking (-t) */
107 int touch_flag;
109 /* Nonzero means just print what commands would need to be executed,
110 don't actually execute them (-n). */
112 int just_print_flag;
114 /* Print debugging trace info (-d). */
116 int debug_flag = 0;
118 /* Environment variables override makefile definitions. */
120 int env_overrides = 0;
122 /* Nonzero means ignore status codes returned by commands
123 executed to remake files. Just treat them all as successful (-i). */
125 int ignore_errors_flag = 0;
127 /* Nonzero means don't remake anything, just print the data base
128 that results from reading the makefile (-p). */
130 int print_data_base_flag = 0;
132 /* Nonzero means don't remake anything; just return a nonzero status
133 if the specified targets are not up to date (-q). */
135 int question_flag = 0;
137 /* Nonzero means do not use any of the builtin rules (-r). */
139 int no_builtin_rules_flag = 0;
141 /* Nonzero means keep going even if remaking some file fails (-k). */
143 int keep_going_flag;
144 int default_keep_going_flag = 0;
146 /* Nonzero means print directory before starting and when done (-w). */
148 int print_directory_flag = 0;
150 /* Nonzero means ignore print_directory_flag and never print the directory.
151 This is necessary because print_directory_flag is set implicitly. */
153 int inhibit_print_directory_flag = 0;
155 /* Nonzero means print version information. */
157 int print_version_flag = 0;
159 /* List of makefiles given with -f switches. */
161 static struct stringlist *makefiles = 0;
164 /* Number of job slots (commands that can be run at once). */
166 unsigned int job_slots = 1;
167 unsigned int default_job_slots = 1;
169 /* Value of job_slots that means no limit. */
171 static unsigned int inf_jobs = 0;
173 /* Maximum load average at which multiple jobs will be run.
174 Negative values mean unlimited, while zero means limit to
175 zero load (which could be useful to start infinite jobs remotely
176 but one at a time locally). */
178 double max_load_average = -1.0;
179 double default_load_average = -1.0;
181 /* List of directories given with -C switches. */
183 static struct stringlist *directories = 0;
185 /* List of include directories given with -I switches. */
187 static struct stringlist *include_directories = 0;
189 /* List of files given with -o switches. */
191 static struct stringlist *old_files = 0;
193 /* List of files given with -W switches. */
195 static struct stringlist *new_files = 0;
197 /* If nonzero, we should just print usage and exit. */
199 static int print_usage_flag = 0;
201 /* If nonzero, we should print a warning message
202 for each reference to an undefined variable. */
204 int warn_undefined_variables_flag;
206 /* The table of command switches. */
208 static const struct command_switch switches[] =
210 { 'b', ignore, 0, 0, 0, 0, 0, 0,
211 0, 0,
212 "Ignored for compatibility" },
213 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
214 "directory", "DIRECTORY",
215 "Change to DIRECTORY before doing anything" },
216 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
217 "debug", 0,
218 "Print lots of debugging information" },
219 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
220 "environment-overrides", 0,
221 "Environment variables override makefiles" },
222 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
223 "file", "FILE",
224 "Read FILE as a makefile" },
225 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
226 "help", 0,
227 "Print this message and exit" },
228 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
229 "ignore-errors", 0,
230 "Ignore errors from commands" },
231 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
232 "include-dir", "DIRECTORY",
233 "Search DIRECTORY for included makefiles" },
234 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
235 (char *) &inf_jobs, (char *) &default_job_slots,
236 "jobs", "N",
237 "Allow N jobs at once; infinite jobs with no arg" },
238 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
239 0, (char *) &default_keep_going_flag,
240 "keep-going", 0,
241 "Keep going when some targets can't be made" },
242 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
243 (char *) &default_load_average, (char *) &default_load_average,
244 "load-average", "N",
245 "Don't start multiple jobs unless load is below N" },
246 { 'm', ignore, 0, 0, 0, 0, 0, 0,
247 0, 0,
248 "-b" },
249 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
250 "just-print", 0,
251 "Don't actually run any commands; just print them" },
252 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
253 "old-file", "FILE",
254 "Consider FILE to be very old and don't remake it" },
255 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
256 "print-data-base", 0,
257 "Print make's internal database" },
258 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
259 "question", 0,
260 "Run no commands; exit status says if up to date" },
261 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
262 "no-builtin-rules", 0,
263 "Disable the built-in implicit rules" },
264 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
265 "silent", 0,
266 "Don't echo commands" },
267 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
268 0, (char *) &default_keep_going_flag,
269 "no-keep-going", 0,
270 "Turns off -k" },
271 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
272 "touch", 0,
273 "Touch targets instead of remaking them" },
274 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
275 "version", 0,
276 "Print the version number of make and exit" },
277 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
278 "print-directory", 0,
279 "Print the current directory" },
280 { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
281 "no-print-directory", 0,
282 "Turn off -w, even if it was turned on implicitly" },
283 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
284 "what-if", "FILE",
285 "Consider FILE to be infinitely new" },
286 { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
287 "warn-undefined-variables", 0,
288 "Warn when an undefined variable is referenced" },
289 { '\0', }
292 /* Secondary long names for options. */
294 static struct option long_option_aliases[] =
296 { "quiet", no_argument, 0, 's' },
297 { "stop", no_argument, 0, 'S' },
298 { "new-file", required_argument, 0, 'W' },
299 { "assume-new", required_argument, 0, 'W' },
300 { "assume-old", required_argument, 0, 'o' },
301 { "max-load", optional_argument, 0, 'l' },
302 { "dry-run", no_argument, 0, 'n' },
303 { "recon", no_argument, 0, 'n' },
304 { "makefile", required_argument, 0, 'f' },
307 /* The usage message prints the descriptions of options starting in
308 this column. Make sure it leaves enough room for the longest
309 description to fit in less than 80 characters. */
311 #define DESCRIPTION_COLUMN 30
313 /* List of goal targets. */
315 static struct dep *goals, *lastgoal;
317 /* List of variables which were defined on the command line
318 (or, equivalently, in MAKEFLAGS). */
320 struct command_variable
322 struct command_variable *next;
323 struct variable *variable;
325 static struct command_variable *command_variables;
327 /* The name we were invoked with. */
329 char *program;
331 /* Our current directory before processing any -C options. */
333 char *directory_before_chdir;
335 /* Our current directory after processing all -C options. */
337 char *starting_directory;
339 /* Value of the MAKELEVEL variable at startup (or 0). */
341 unsigned int makelevel;
343 /* First file defined in the makefile whose name does not
344 start with `.'. This is the default to remake if the
345 command line does not specify. */
347 struct file *default_goal_file;
349 /* Pointer to structure for the file .DEFAULT
350 whose commands are used for any file that has none of its own.
351 This is zero if the makefiles do not define .DEFAULT. */
353 struct file *default_file;
355 /* Nonzero if we have seen the magic `.POSIX' target.
356 This turns on pedantic compliance with POSIX.2. */
358 int posix_pedantic;
360 /* Mask of signals that are being caught with fatal_error_signal. */
362 #ifdef POSIX
363 sigset_t fatal_signal_set;
364 #else
365 #ifdef HAVE_SIGSETMASK
366 int fatal_signal_mask;
367 #endif
368 #endif
370 static struct file *
371 enter_command_line_file (name)
372 char *name;
374 if (name[0] == '\0')
375 fatal ("empty string invalid as file name");
377 if (name[0] == '~')
379 char *expanded = tilde_expand (name);
380 if (expanded != 0)
381 name = expanded; /* Memory leak; I don't care. */
384 /* This is also done in parse_file_seq, so this is redundant
385 for names read from makefiles. It is here for names passed
386 on the command line. */
387 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
389 name += 2;
390 while (*name == '/')
391 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
392 ++name;
395 if (*name == '\0')
397 /* It was all slashes! Move back to the dot and truncate
398 it after the first slash, so it becomes just "./". */
400 --name;
401 while (name[0] != '.');
402 name[2] = '\0';
405 return enter_file (savestring (name, strlen (name)));
408 /* Toggle -d on receipt of SIGUSR1. */
410 static RETSIGTYPE
411 debug_signal_handler (sig)
412 int sig;
414 debug_flag = ! debug_flag;
418 main (argc, argv, envp)
419 int argc;
420 char **argv;
421 char **envp;
423 register struct file *f;
424 register unsigned int i;
425 char **p;
426 struct dep *read_makefiles;
427 PATH_VAR (current_directory);
429 default_goal_file = 0;
430 reading_filename = 0;
431 reading_lineno_ptr = 0;
433 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
434 signame_init ();
435 #endif
437 #ifdef POSIX
438 sigemptyset (&fatal_signal_set);
439 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
440 #else
441 #ifdef HAVE_SIGSETMASK
442 fatal_signal_mask = 0;
443 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
444 #else
445 #define ADD_SIG(sig)
446 #endif
447 #endif
449 #define FATAL_SIG(sig) \
450 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
451 (void) signal ((sig), SIG_IGN); \
452 else \
453 ADD_SIG (sig);
455 FATAL_SIG (SIGHUP);
456 FATAL_SIG (SIGQUIT);
457 FATAL_SIG (SIGINT);
458 FATAL_SIG (SIGTERM);
460 #ifdef SIGDANGER
461 FATAL_SIG (SIGDANGER);
462 #endif
463 #ifdef SIGXCPU
464 FATAL_SIG (SIGXCPU);
465 #endif
466 #ifdef SIGXFSZ
467 FATAL_SIG (SIGXFSZ);
468 #endif
470 #undef FATAL_SIG
472 /* Make sure stdout is line-buffered. */
474 #ifdef HAVE_SETLINEBUF
475 setlinebuf (stdout);
476 #else
477 #ifndef SETVBUF_REVERSED
478 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
479 #else /* setvbuf not reversed. */
480 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
481 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
482 #endif /* setvbuf reversed. */
483 #endif /* setlinebuf missing. */
485 /* Figure out where this program lives. */
487 if (argv[0] == 0)
488 argv[0] = "";
489 if (argv[0][0] == '\0')
490 program = "make";
491 else
493 #ifdef VMS
494 program = rindex (argv[0], ']');
495 #else
496 program = rindex (argv[0], '/');
497 #endif
498 #ifdef __MSDOS__
499 if (program == 0)
500 program = rindex (argv[0], '\\');
501 if (program == 0)
502 program = rindex (argv[0], ':');
503 #endif
504 if (program == 0)
505 program = argv[0];
506 else
507 ++program;
510 /* Set up to access user data (files). */
511 user_access ();
513 /* Figure out where we are. */
515 if (getcwd (current_directory, GET_PATH_MAX) == 0)
517 #ifdef HAVE_GETCWD
518 perror_with_name ("getcwd: ", "");
519 #else
520 error ("getwd: %s", current_directory);
521 #endif
522 current_directory[0] = '\0';
523 directory_before_chdir = 0;
525 else
526 directory_before_chdir = savestring (current_directory,
527 strlen (current_directory));
529 /* Read in variables from the environment. It is important that this be
530 done before $(MAKE) is are figured out so its definitions will not be
531 one from the environment. */
533 for (i = 0; envp[i] != 0; ++i)
535 register char *ep = envp[i];
536 while (*ep != '=')
537 ++ep;
538 /* The result of pointer arithmetic is cast to unsigned int for
539 machines where ptrdiff_t is a different size that doesn't widen
540 the same. */
541 define_variable (envp[i], (unsigned int) (ep - envp[i]),
542 ep + 1, o_env, 1)
543 /* Force exportation of every variable culled from the environment.
544 We used to rely on target_environment's v_default code to do this.
545 But that does not work for the case where an environment variable
546 is redefined in a makefile with `override'; it should then still
547 be exported, because it was originally in the environment. */
548 ->export = v_export;
551 /* Decode the switches. */
553 decode_env_switches ("MAKEFLAGS", 9);
554 #if 0
555 /* People write things like:
556 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
557 and we set the -p, -i and -e switches. Doesn't seem quite right. */
558 decode_env_switches ("MFLAGS", 6);
559 #endif
560 decode_switches (argc, argv, 0);
562 /* Print version information. */
564 if (print_version_flag || print_data_base_flag || debug_flag)
565 print_version ();
567 /* `make --version' is supposed to just print the version and exit. */
568 if (print_version_flag)
569 die (0);
571 #if !defined(__MSDOS__) && !defined(VMS)
572 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
573 (If it is a relative pathname with a slash, prepend our directory name
574 so the result will run the same program regardless of the current dir.
575 If it is a name with no slash, we can only hope that PATH did not
576 find it in the current directory.) */
578 if (current_directory[0] != '\0'
579 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
580 argv[0] = concat (current_directory, "/", argv[0]);
581 #endif
583 /* The extra indirection through $(MAKE_COMMAND) is done
584 for hysterical raisins. */
585 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
586 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
588 if (command_variables != 0)
590 struct command_variable *cv;
591 struct variable *v;
592 unsigned int len = 0;
593 char *value, *p;
595 /* Figure out how much space will be taken up by the command-line
596 variable definitions. */
597 for (cv = command_variables; cv != 0; cv = cv->next)
599 v = cv->variable;
600 len += 2 * strlen (v->name);
601 if (! v->recursive)
602 ++len;
603 ++len;
604 len += 2 * strlen (v->value);
607 /* Now allocate a buffer big enough and fill it. */
608 p = value = (char *) alloca (len);
609 for (cv = command_variables; cv != 0; cv = cv->next)
611 v = cv->variable;
612 p = quote_as_word (p, v->name, 0);
613 if (! v->recursive)
614 *p++ = ':';
615 *p++ = '=';
616 p = quote_as_word (p, v->value, 0);
617 *p++ = ' ';
619 p[-1] = '\0'; /* Kill the final space and terminate. */
621 /* Define an unchangeable variable with a name that no POSIX.2
622 makefile could validly use for its own variable. */
623 (void) define_variable ("-*-command-variables-*-", 23,
624 value, o_automatic, 0);
626 /* Define the variable; this will not override any user definition.
627 Normally a reference to this variable is written into the value of
628 MAKEFLAGS, allowing the user to override this value to affect the
629 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
630 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
631 a reference to this hidden variable is written instead. */
632 (void) define_variable ("MAKEOVERRIDES", 13,
633 "${-*-command-variables-*-}", o_env, 1);
636 /* If there were -C flags, move ourselves about. */
637 if (directories != 0)
638 for (i = 0; directories->list[i] != 0; ++i)
640 char *dir = directories->list[i];
641 if (dir[0] == '~')
643 char *expanded = tilde_expand (dir);
644 if (expanded != 0)
645 dir = expanded;
647 if (chdir (dir) < 0)
648 pfatal_with_name (dir);
649 if (dir != directories->list[i])
650 free (dir);
653 /* Figure out the level of recursion. */
655 struct variable *v = lookup_variable ("MAKELEVEL", 9);
656 if (v != 0 && *v->value != '\0' && *v->value != '-')
657 makelevel = (unsigned int) atoi (v->value);
658 else
659 makelevel = 0;
662 /* Except under -s, always do -w in sub-makes and under -C. */
663 if (!silent_flag && (directories != 0 || makelevel > 0))
664 print_directory_flag = 1;
666 /* Let the user disable that with --no-print-directory. */
667 if (inhibit_print_directory_flag)
668 print_directory_flag = 0;
670 /* Construct the list of include directories to search. */
672 construct_include_path (include_directories == 0 ? (char **) 0
673 : include_directories->list);
675 /* Figure out where we are now, after chdir'ing. */
676 if (directories == 0)
677 /* We didn't move, so we're still in the same place. */
678 starting_directory = current_directory;
679 else
681 if (getcwd (current_directory, GET_PATH_MAX) == 0)
683 #ifdef HAVE_GETCWD
684 perror_with_name ("getcwd: ", "");
685 #else
686 error ("getwd: %s", current_directory);
687 #endif
688 starting_directory = 0;
690 else
691 starting_directory = current_directory;
695 /* Read any stdin makefiles into temporary files. */
697 if (makefiles != 0)
699 register unsigned int i;
700 for (i = 0; i < makefiles->idx; ++i)
701 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
703 /* This makefile is standard input. Since we may re-exec
704 and thus re-read the makefiles, we read standard input
705 into a temporary file and read from that. */
706 FILE *outfile;
708 /* Make a unique filename. */
709 #ifdef HAVE_MKTEMP
711 #ifdef VMS
712 static char name[] = "sys$scratch:GmXXXXXX";
713 #else
714 static char name[] = "/tmp/GmXXXXXX";
715 #endif
716 (void) mktemp (name);
717 #else
718 static char name[L_tmpnam];
719 (void) tmpnam (name);
720 #endif
722 outfile = fopen (name, "w");
723 if (outfile == 0)
724 pfatal_with_name ("fopen (temporary file)");
725 while (!feof (stdin))
727 char buf[2048];
728 int n = fread (buf, 1, sizeof(buf), stdin);
729 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
730 pfatal_with_name ("fwrite (temporary file)");
732 /* Try to make sure we won't remake the temporary
733 file when we are re-exec'd. Kludge-o-matic! */
734 fprintf (outfile, "%s:;\n", name);
735 (void) fclose (outfile);
737 /* Replace the name that read_all_makefiles will
738 see with the name of the temporary file. */
740 char *temp;
741 /* SGI compiler requires alloca's result be assigned simply. */
742 temp = (char *) alloca (sizeof (name));
743 bcopy (name, temp, sizeof (name));
744 makefiles->list[i] = temp;
747 /* Make sure the temporary file will not be remade. */
748 f = enter_file (savestring (name, sizeof name - 1));
749 f->updated = 1;
750 f->update_status = 0;
751 f->command_state = cs_finished;
752 /* Let it be removed when we're done. */
753 f->intermediate = 1;
754 /* But don't mention it. */
755 f->dontcare = 1;
759 /* Set up to handle children dying. This must be done before
760 reading in the makefiles so that `shell' function calls will work. */
762 #ifdef SIGCHLD
763 (void) signal (SIGCHLD, child_handler);
764 #endif
765 #ifdef SIGCLD
766 (void) signal (SIGCLD, child_handler);
767 #endif
769 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
770 #ifdef SIGUSR1
771 (void) signal (SIGUSR1, debug_signal_handler);
772 #endif
774 /* Define the initial list of suffixes for old-style rules. */
776 set_default_suffixes ();
778 /* Define the file rules for the built-in suffix rules. These will later
779 be converted into pattern rules. We used to do this in
780 install_default_implicit_rules, but since that happens after reading
781 makefiles, it results in the built-in pattern rules taking precedence
782 over makefile-specified suffix rules, which is wrong. */
784 install_default_suffix_rules ();
786 /* Define some internal and special variables. */
788 define_automatic_variables ();
790 /* Set up the MAKEFLAGS and MFLAGS variables
791 so makefiles can look at them. */
793 define_makeflags (0, 0);
795 /* Define the default variables. */
796 define_default_variables ();
798 /* Read all the makefiles. */
800 default_file = enter_file (".DEFAULT");
802 read_makefiles
803 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
805 /* Decode switches again, in case the variables were set by the makefile. */
806 decode_env_switches ("MAKEFLAGS", 9);
807 #if 0
808 decode_env_switches ("MFLAGS", 6);
809 #endif
811 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
813 define_makeflags (1, 0);
815 /* Make each `struct dep' point at the `struct file' for the file
816 depended on. Also do magic for special targets. */
818 snap_deps ();
820 /* Convert old-style suffix rules to pattern rules. It is important to
821 do this before installing the built-in pattern rules below, so that
822 makefile-specified suffix rules take precedence over built-in pattern
823 rules. */
825 convert_to_pattern ();
827 /* Install the default implicit pattern rules.
828 This used to be done before reading the makefiles.
829 But in that case, built-in pattern rules were in the chain
830 before user-defined ones, so they matched first. */
832 install_default_implicit_rules ();
834 /* Compute implicit rule limits. */
836 count_implicit_rule_limits ();
838 /* Construct the listings of directories in VPATH lists. */
840 build_vpath_lists ();
842 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
843 and as having been updated already, and files given with -W flags as
844 brand new (time-stamp as far as possible into the future). */
846 if (old_files != 0)
847 for (p = old_files->list; *p != 0; ++p)
849 f = enter_command_line_file (*p);
850 f->last_mtime = (time_t) 1;
851 f->updated = 1;
852 f->update_status = 0;
853 f->command_state = cs_finished;
856 if (new_files != 0)
858 for (p = new_files->list; *p != 0; ++p)
860 f = enter_command_line_file (*p);
861 f->last_mtime = NEW_MTIME;
865 if (read_makefiles != 0)
867 /* Update any makefiles if necessary. */
869 time_t *makefile_mtimes = 0;
870 unsigned int mm_idx = 0;
872 if (debug_flag)
873 puts ("Updating makefiles....");
875 /* Remove any makefiles we don't want to try to update.
876 Also record the current modtimes so we can compare them later. */
878 register struct dep *d, *last;
879 last = 0;
880 d = read_makefiles;
881 while (d != 0)
883 register struct file *f = d->file;
884 if (f->double_colon)
885 for (f = f->double_colon; f != NULL; f = f->prev)
887 if (f->deps == 0 && f->cmds != 0)
889 /* This makefile is a :: target with commands, but
890 no dependencies. So, it will always be remade.
891 This might well cause an infinite loop, so don't
892 try to remake it. (This will only happen if
893 your makefiles are written exceptionally
894 stupidly; but if you work for Athena, that's how
895 you write your makefiles.) */
897 if (debug_flag)
898 printf ("Makefile `%s' might loop; not remaking it.\n",
899 f->name);
901 if (last == 0)
902 read_makefiles = d->next;
903 else
904 last->next = d->next;
906 /* Free the storage. */
907 free ((char *) d);
909 d = last == 0 ? 0 : last->next;
911 break;
914 if (f == NULL || !f->double_colon)
916 if (makefile_mtimes == 0)
917 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
918 else
919 makefile_mtimes = (time_t *)
920 xrealloc ((char *) makefile_mtimes,
921 (mm_idx + 1) * sizeof (time_t));
922 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
923 last = d;
924 d = d->next;
929 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
930 define_makeflags (1, 1);
932 switch (update_goal_chain (read_makefiles, 1))
934 case 1:
935 default:
936 #define BOGUS_UPDATE_STATUS 0
937 assert (BOGUS_UPDATE_STATUS);
938 break;
940 case -1:
941 /* Did nothing. */
942 break;
944 case 2:
945 /* Failed to update. Figure out if we care. */
947 /* Nonzero if any makefile was successfully remade. */
948 int any_remade = 0;
949 /* Nonzero if any makefile we care about failed
950 in updating or could not be found at all. */
951 int any_failed = 0;
952 register unsigned int i;
954 for (i = 0; read_makefiles != 0; ++i)
956 struct dep *d = read_makefiles;
957 read_makefiles = d->next;
958 if (d->file->updated)
960 /* This makefile was updated. */
961 if (d->file->update_status == 0)
963 /* It was successfully updated. */
964 any_remade |= (file_mtime_no_search (d->file)
965 != makefile_mtimes[i]);
967 else if (! (d->changed & RM_DONTCARE))
969 time_t mtime;
970 /* The update failed and this makefile was not
971 from the MAKEFILES variable, so we care. */
972 error ("Failed to remake makefile `%s'.",
973 d->file->name);
974 mtime = file_mtime_no_search (d->file);
975 any_remade |= (mtime != (time_t) -1
976 && mtime != makefile_mtimes[i]);
979 else
980 /* This makefile was not found at all. */
981 if (! (d->changed & RM_DONTCARE))
983 /* This is a makefile we care about. See how much. */
984 if (d->changed & RM_INCLUDED)
985 /* An included makefile. We don't need
986 to die, but we do want to complain. */
987 error ("Included makefile `%s' was not found.",
988 dep_name (d));
989 else
991 /* A normal makefile. We must die later. */
992 error ("Makefile `%s' was not found", dep_name (d));
993 any_failed = 1;
997 free ((char *) d);
1000 if (any_remade)
1001 goto re_exec;
1002 else if (any_failed)
1003 die (2);
1004 else
1005 break;
1008 case 0:
1009 re_exec:
1010 /* Updated successfully. Re-exec ourselves. */
1012 remove_intermediates (0);
1014 if (print_data_base_flag)
1015 print_data_base ();
1017 log_working_directory (0);
1019 if (makefiles != 0)
1021 /* These names might have changed. */
1022 register unsigned int i, j = 0;
1023 for (i = 1; i < argc; ++i)
1024 if (!strcmp (argv[i], "-f")) /* XXX */
1026 char *p = &argv[i][2];
1027 if (*p == '\0')
1028 argv[++i] = makefiles->list[j];
1029 else
1030 argv[i] = concat ("-f", makefiles->list[j], "");
1031 ++j;
1035 if (directories != 0 && directories->idx > 0)
1037 char bad;
1038 if (directory_before_chdir != 0)
1040 if (chdir (directory_before_chdir) < 0)
1042 perror_with_name ("chdir", "");
1043 bad = 1;
1045 else
1046 bad = 0;
1048 else
1049 bad = 1;
1050 if (bad)
1051 fatal ("Couldn't change back to original directory.");
1054 for (p = environ; *p != 0; ++p)
1055 if (!strncmp (*p, "MAKELEVEL=", 10))
1057 /* The SGI compiler apparently can't understand
1058 the concept of storing the result of a function
1059 in something other than a local variable. */
1060 char *sgi_loses;
1061 sgi_loses = (char *) alloca (40);
1062 *p = sgi_loses;
1063 sprintf (*p, "MAKELEVEL=%u", makelevel);
1064 break;
1067 if (debug_flag)
1069 char **p;
1070 fputs ("Re-executing:", stdout);
1071 for (p = argv; *p != 0; ++p)
1072 printf (" %s", *p);
1073 puts ("");
1076 fflush (stdout);
1077 fflush (stderr);
1079 exec_command (argv, environ);
1080 /* NOTREACHED */
1084 /* Set up `MAKEFLAGS' again for the normal targets. */
1085 define_makeflags (1, 0);
1088 int status;
1090 /* If there were no command-line goals, use the default. */
1091 if (goals == 0)
1093 if (default_goal_file != 0)
1095 goals = (struct dep *) xmalloc (sizeof (struct dep));
1096 goals->next = 0;
1097 goals->name = 0;
1098 goals->file = default_goal_file;
1101 else
1102 lastgoal->next = 0;
1104 if (goals != 0)
1106 /* Update the goals. */
1108 if (debug_flag)
1109 puts ("Updating goal targets....");
1111 switch (update_goal_chain (goals, 0))
1113 case -1:
1114 /* Nothing happened. */
1115 case 0:
1116 /* Updated successfully. */
1117 status = EXIT_SUCCESS;
1118 break;
1119 case 2:
1120 /* Updating failed. */
1121 status = EXIT_FAILURE;
1122 break;
1123 case 1:
1124 /* We are under -q and would run some commands. */
1125 status = EXIT_FAILURE;
1126 break;
1127 default:
1128 abort ();
1131 else
1133 if (read_makefiles == 0)
1134 fatal ("No targets specified and no makefile found");
1135 else
1136 fatal ("No targets");
1139 /* Exit. */
1140 die (status);
1143 return 0;
1146 /* Parsing of arguments, decoding of switches. */
1148 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1149 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1150 (sizeof (long_option_aliases) /
1151 sizeof (long_option_aliases[0]))];
1153 /* Fill in the string and vector for getopt. */
1154 static void
1155 init_switches ()
1157 register char *p;
1158 register int c;
1159 register unsigned int i;
1161 if (options[0] != '\0')
1162 /* Already done. */
1163 return;
1165 p = options;
1167 /* Return switch and non-switch args in order, regardless of
1168 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1169 *p++ = '-';
1171 for (i = 0; switches[i].c != '\0'; ++i)
1173 long_options[i].name = (switches[i].long_name == 0 ? "" :
1174 switches[i].long_name);
1175 long_options[i].flag = 0;
1176 long_options[i].val = switches[i].c;
1177 if (isalnum (switches[i].c))
1178 *p++ = switches[i].c;
1179 switch (switches[i].type)
1181 case flag:
1182 case flag_off:
1183 case ignore:
1184 long_options[i].has_arg = no_argument;
1185 break;
1187 case string:
1188 case positive_int:
1189 case floating:
1190 if (isalnum (switches[i].c))
1191 *p++ = ':';
1192 if (switches[i].noarg_value != 0)
1194 if (isalnum (switches[i].c))
1195 *p++ = ':';
1196 long_options[i].has_arg = optional_argument;
1198 else
1199 long_options[i].has_arg = required_argument;
1200 break;
1203 *p = '\0';
1204 for (c = 0; c < (sizeof (long_option_aliases) /
1205 sizeof (long_option_aliases[0]));
1206 ++c)
1207 long_options[i++] = long_option_aliases[c];
1208 long_options[i].name = 0;
1211 static void
1212 handle_non_switch_argument (arg, env)
1213 char *arg;
1214 int env;
1216 /* Non-option argument. It might be a variable definition. */
1217 struct variable *v;
1218 if (arg[0] == '-' && arg[1] == '\0')
1219 /* Ignore plain `-' for compatibility. */
1220 return;
1221 v = try_variable_definition ((char *) 0, 0, arg, o_command);
1222 if (v != 0)
1224 /* It is indeed a variable definition. Record a pointer to
1225 the variable for later use in define_makeflags. */
1226 struct command_variable *cv
1227 = (struct command_variable *) xmalloc (sizeof (*cv));
1228 cv->variable = v;
1229 cv->next = command_variables;
1230 command_variables = cv;
1232 else if (! env)
1234 /* Not an option or variable definition; it must be a goal
1235 target! Enter it as a file and add it to the dep chain of
1236 goals. */
1237 struct file *f = enter_command_line_file (arg);
1238 f->cmd_target = 1;
1240 if (goals == 0)
1242 goals = (struct dep *) xmalloc (sizeof (struct dep));
1243 lastgoal = goals;
1245 else
1247 lastgoal->next
1248 = (struct dep *) xmalloc (sizeof (struct dep));
1249 lastgoal = lastgoal->next;
1251 lastgoal->name = 0;
1252 lastgoal->file = f;
1256 /* Decode switches from ARGC and ARGV.
1257 They came from the environment if ENV is nonzero. */
1259 static void
1260 decode_switches (argc, argv, env)
1261 int argc;
1262 char **argv;
1263 int env;
1265 int bad = 0;
1266 register const struct command_switch *cs;
1267 register struct stringlist *sl;
1268 register int c;
1270 /* getopt does most of the parsing for us.
1271 First, get its vectors set up. */
1273 init_switches ();
1275 /* Let getopt produce error messages for the command line,
1276 but not for options from the environment. */
1277 opterr = !env;
1278 /* Reset getopt's state. */
1279 optind = 0;
1281 while (optind < argc)
1283 /* Parse the next argument. */
1284 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1285 if (c == EOF)
1286 /* End of arguments, or "--" marker seen. */
1287 break;
1288 else if (c == 1)
1289 /* An argument not starting with a dash. */
1290 handle_non_switch_argument (optarg, env);
1291 else if (c == '?')
1292 /* Bad option. We will print a usage message and die later.
1293 But continue to parse the other options so the user can
1294 see all he did wrong. */
1295 bad = 1;
1296 else
1297 for (cs = switches; cs->c != '\0'; ++cs)
1298 if (cs->c == c)
1300 /* Whether or not we will actually do anything with
1301 this switch. We test this individually inside the
1302 switch below rather than just once outside it, so that
1303 options which are to be ignored still consume args. */
1304 int doit = !env || cs->env;
1306 switch (cs->type)
1308 default:
1309 abort ();
1311 case ignore:
1312 break;
1314 case flag:
1315 case flag_off:
1316 if (doit)
1317 *(int *) cs->value_ptr = cs->type == flag;
1318 break;
1320 case string:
1321 if (!doit)
1322 break;
1324 if (optarg == 0)
1325 optarg = cs->noarg_value;
1327 sl = *(struct stringlist **) cs->value_ptr;
1328 if (sl == 0)
1330 sl = (struct stringlist *)
1331 xmalloc (sizeof (struct stringlist));
1332 sl->max = 5;
1333 sl->idx = 0;
1334 sl->list = (char **) xmalloc (5 * sizeof (char *));
1335 *(struct stringlist **) cs->value_ptr = sl;
1337 else if (sl->idx == sl->max - 1)
1339 sl->max += 5;
1340 sl->list = (char **)
1341 xrealloc ((char *) sl->list,
1342 sl->max * sizeof (char *));
1344 sl->list[sl->idx++] = optarg;
1345 sl->list[sl->idx] = 0;
1346 break;
1348 case positive_int:
1349 if (optarg == 0 && argc > optind
1350 && isdigit (argv[optind][0]))
1351 optarg = argv[optind++];
1353 if (!doit)
1354 break;
1356 if (optarg != 0)
1358 int i = atoi (optarg);
1359 if (i < 1)
1361 if (doit)
1362 error ("the `-%c' option requires a \
1363 positive integral argument",
1364 cs->c);
1365 bad = 1;
1367 else
1368 *(unsigned int *) cs->value_ptr = i;
1370 else
1371 *(unsigned int *) cs->value_ptr
1372 = *(unsigned int *) cs->noarg_value;
1373 break;
1375 case floating:
1376 if (optarg == 0 && optind < argc
1377 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1378 optarg = argv[optind++];
1380 if (doit)
1381 *(double *) cs->value_ptr
1382 = (optarg != 0 ? atof (optarg)
1383 : *(double *) cs->noarg_value);
1385 break;
1388 /* We've found the switch. Stop looking. */
1389 break;
1393 /* There are no more options according to getting getopt, but there may
1394 be some arguments left. Since we have asked for non-option arguments
1395 to be returned in order, this only happens when there is a "--"
1396 argument to prevent later arguments from being options. */
1397 while (optind < argc)
1398 handle_non_switch_argument (argv[optind++], env);
1401 if (!env && (bad || print_usage_flag))
1403 /* Print a nice usage message. */
1404 FILE *usageto;
1406 if (print_version_flag)
1407 print_version ();
1409 usageto = bad ? stderr : stdout;
1411 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1413 fputs ("Options:\n", usageto);
1414 for (cs = switches; cs->c != '\0'; ++cs)
1416 char buf[1024], shortarg[50], longarg[50], *p;
1418 if (cs->description[0] == '-')
1419 continue;
1421 switch (long_options[cs - switches].has_arg)
1423 case no_argument:
1424 shortarg[0] = longarg[0] = '\0';
1425 break;
1426 case required_argument:
1427 sprintf (longarg, "=%s", cs->argdesc);
1428 sprintf (shortarg, " %s", cs->argdesc);
1429 break;
1430 case optional_argument:
1431 sprintf (longarg, "[=%s]", cs->argdesc);
1432 sprintf (shortarg, " [%s]", cs->argdesc);
1433 break;
1436 p = buf;
1438 if (isalnum (cs->c))
1440 sprintf (buf, " -%c%s", cs->c, shortarg);
1441 p += strlen (p);
1443 if (cs->long_name != 0)
1445 unsigned int i;
1446 sprintf (p, "%s--%s%s",
1447 !isalnum (cs->c) ? " " : ", ",
1448 cs->long_name, longarg);
1449 p += strlen (p);
1450 for (i = 0; i < (sizeof (long_option_aliases) /
1451 sizeof (long_option_aliases[0]));
1452 ++i)
1453 if (long_option_aliases[i].val == cs->c)
1455 sprintf (p, ", --%s%s",
1456 long_option_aliases[i].name, longarg);
1457 p += strlen (p);
1461 const struct command_switch *ncs = cs;
1462 while ((++ncs)->c != '\0')
1463 if (ncs->description[0] == '-' &&
1464 ncs->description[1] == cs->c)
1466 /* This is another switch that does the same
1467 one as the one we are processing. We want
1468 to list them all together on one line. */
1469 sprintf (p, ", -%c%s", ncs->c, shortarg);
1470 p += strlen (p);
1471 if (ncs->long_name != 0)
1473 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1474 p += strlen (p);
1479 if (p - buf > DESCRIPTION_COLUMN - 2)
1480 /* The list of option names is too long to fit on the same
1481 line with the description, leaving at least two spaces.
1482 Print it on its own line instead. */
1484 fprintf (usageto, "%s\n", buf);
1485 buf[0] = '\0';
1488 fprintf (usageto, "%*s%s.\n",
1489 - DESCRIPTION_COLUMN,
1490 buf, cs->description);
1493 die (bad ? 2 : 0);
1497 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1498 We do this by chopping the value into a vector of words, prepending a
1499 dash to the first word if it lacks one, and passing the vector to
1500 decode_switches. */
1502 static void
1503 decode_env_switches (envar, len)
1504 char *envar;
1505 unsigned int len;
1507 char *varref = (char *) alloca (2 + len + 2);
1508 char *value, *p;
1509 int argc;
1510 char **argv;
1512 /* Get the variable's value. */
1513 varref[0] = '$';
1514 varref[1] = '(';
1515 bcopy (envar, &varref[2], len);
1516 varref[2 + len] = ')';
1517 varref[2 + len + 1] = '\0';
1518 value = variable_expand (varref);
1520 /* Skip whitespace, and check for an empty value. */
1521 value = next_token (value);
1522 len = strlen (value);
1523 if (len == 0)
1524 return;
1526 /* Allocate a vector that is definitely big enough. */
1527 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1529 /* Allocate a buffer to copy the value into while we split it into words
1530 and unquote it. We must use permanent storage for this because
1531 decode_switches may store pointers into the passed argument words. */
1532 p = (char *) xmalloc (2 * len);
1534 /* getopt will look at the arguments starting at ARGV[1].
1535 Prepend a spacer word. */
1536 argv[0] = 0;
1537 argc = 1;
1538 argv[argc] = p;
1539 while (*value != '\0')
1541 if (*value == '\\')
1542 ++value; /* Skip the backslash. */
1543 else if (isblank (*value))
1545 /* End of the word. */
1546 *p++ = '\0';
1547 argv[++argc] = p;
1549 ++value;
1550 while (isblank (*value));
1551 continue;
1553 *p++ = *value++;
1555 *p = '\0';
1556 argv[++argc] = 0;
1558 if (argc == 2 && argv[1][0] != '-')
1560 /* There is just one word in the value, and it is not a switch.
1561 Either this is the single-word form and we should prepend a dash
1562 before calling decode_switches, or this is the multi-word form and
1563 there is no dash because it is a variable definition. */
1564 struct variable *v;
1565 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1566 if (v != 0)
1568 /* It was indeed a variable definition, and now it has been
1569 processed. There is nothing for decode_switches to do.
1570 Record a pointer to the variable for later use in
1571 define_makeflags. */
1572 struct command_variable *cv
1573 = (struct command_variable *) xmalloc (sizeof (*cv));
1574 cv->variable = v;
1575 cv->next = command_variables;
1576 command_variables = cv;
1577 return;
1580 /* It wasn't a variable definition, so it's some switches without a
1581 leading dash. Add one and pass it along to decode_switches. We
1582 need permanent storage for this in case decode_switches saves
1583 pointers into the value. */
1584 argv[1] = concat ("-", argv[1], "");
1587 /* Parse those words. */
1588 decode_switches (argc, argv, 1);
1591 /* Quote the string IN so that it will be interpreted as a single word with
1592 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1593 signs to avoid variable expansion in make itself. Write the result into
1594 OUT, returning the address of the next character to be written.
1595 Allocating space for OUT twice the length of IN (thrice if
1596 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1598 static char *
1599 quote_as_word (out, in, double_dollars)
1600 char *out, *in;
1601 int double_dollars;
1603 while (*in != '\0')
1605 #ifdef VMS
1606 if (index ("^;'\"*?$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1607 #else
1608 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1609 #endif
1610 *out++ = '\\';
1611 if (double_dollars && *in == '$')
1612 *out++ = '$';
1613 *out++ = *in++;
1616 return out;
1619 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1620 command switches. Include options with args if ALL is nonzero.
1621 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1623 static void
1624 define_makeflags (all, makefile)
1625 int all, makefile;
1627 static const char ref[] = "$(MAKEOVERRIDES)";
1628 static const char posixref[] = "$(-*-command-variables-*-)";
1629 register const struct command_switch *cs;
1630 char *flagstring;
1631 register char *p;
1632 unsigned int words;
1633 struct variable *v;
1635 /* We will construct a linked list of `struct flag's describing
1636 all the flags which need to go in MAKEFLAGS. Then, once we
1637 know how many there are and their lengths, we can put them all
1638 together in a string. */
1640 struct flag
1642 struct flag *next;
1643 const struct command_switch *cs;
1644 char *arg;
1646 struct flag *flags = 0;
1647 unsigned int flagslen = 0;
1648 #define ADD_FLAG(ARG, LEN) \
1649 do { \
1650 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1651 new->cs = cs; \
1652 new->arg = (ARG); \
1653 new->next = flags; \
1654 flags = new; \
1655 if (new->arg == 0) \
1656 ++flagslen; /* Just a single flag letter. */ \
1657 else \
1658 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1659 if (!isalnum (cs->c)) \
1660 /* This switch has no single-letter version, so we use the long. */ \
1661 flagslen += 2 + strlen (cs->long_name); \
1662 } while (0)
1664 for (cs = switches; cs->c != '\0'; ++cs)
1665 if (cs->toenv && (!makefile || !cs->no_makefile))
1666 switch (cs->type)
1668 default:
1669 abort ();
1671 case ignore:
1672 break;
1674 case flag:
1675 case flag_off:
1676 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1677 && (cs->default_value == 0
1678 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1679 ADD_FLAG (0, 0);
1680 break;
1682 case positive_int:
1683 if (all)
1685 if ((cs->default_value != 0
1686 && (*(unsigned int *) cs->value_ptr
1687 == *(unsigned int *) cs->default_value)))
1688 break;
1689 else if (cs->noarg_value != 0
1690 && (*(unsigned int *) cs->value_ptr ==
1691 *(unsigned int *) cs->noarg_value))
1692 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1693 else if (cs->c == 'j')
1694 /* Special case for `-j'. */
1695 ADD_FLAG ("1", 1);
1696 else
1698 char *buf = (char *) alloca (30);
1699 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1700 ADD_FLAG (buf, strlen (buf));
1703 break;
1705 case floating:
1706 if (all)
1708 if (cs->default_value != 0
1709 && (*(double *) cs->value_ptr
1710 == *(double *) cs->default_value))
1711 break;
1712 else if (cs->noarg_value != 0
1713 && (*(double *) cs->value_ptr
1714 == *(double *) cs->noarg_value))
1715 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1716 else
1718 char *buf = (char *) alloca (100);
1719 sprintf (buf, "%g", *(double *) cs->value_ptr);
1720 ADD_FLAG (buf, strlen (buf));
1723 break;
1725 case string:
1726 if (all)
1728 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1729 if (sl != 0)
1731 /* Add the elements in reverse order, because
1732 all the flags get reversed below; and the order
1733 matters for some switches (like -I). */
1734 register unsigned int i = sl->idx;
1735 while (i-- > 0)
1736 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1739 break;
1742 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1744 #undef ADD_FLAG
1746 /* Construct the value in FLAGSTRING.
1747 We allocate enough space for a preceding dash and trailing null. */
1748 flagstring = (char *) alloca (1 + flagslen + 1);
1749 p = flagstring;
1750 words = 1;
1751 *p++ = '-';
1752 while (flags != 0)
1754 /* Add the flag letter or name to the string. */
1755 if (!isalnum (flags->cs->c))
1757 *p++ = '-';
1758 strcpy (p, flags->cs->long_name);
1759 p += strlen (p);
1761 else
1762 *p++ = flags->cs->c;
1763 if (flags->arg != 0)
1765 /* A flag that takes an optional argument which in this case is
1766 omitted is specified by ARG being "". We must distinguish
1767 because a following flag appended without an intervening " -"
1768 is considered the arg for the first. */
1769 if (flags->arg[0] != '\0')
1771 /* Add its argument too. */
1772 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1773 p = quote_as_word (p, flags->arg, 1);
1775 ++words;
1776 /* Write a following space and dash, for the next flag. */
1777 *p++ = ' ';
1778 *p++ = '-';
1780 else if (!isalnum (flags->cs->c))
1782 ++words;
1783 /* Long options must each go in their own word,
1784 so we write the following space and dash. */
1785 *p++ = ' ';
1786 *p++ = '-';
1788 flags = flags->next;
1792 /* Define MFLAGS before appending variable definitions. */
1794 if (p == &flagstring[1])
1795 /* No flags. */
1796 flagstring[0] = '\0';
1797 else if (p[-1] == '-')
1798 /* Kill the final space and dash. */
1799 p[-2] = '\0';
1800 else
1801 /* Terminate the string. */
1802 *p = '\0';
1804 /* Since MFLAGS is not parsed for flags, there is no reason to
1805 override any makefile redefinition. */
1806 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1809 if (all && command_variables != 0)
1811 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1812 command-line variable definitions. */
1814 if (p == &flagstring[1])
1815 /* No flags written, so elide the leading dash already written. */
1816 p = flagstring;
1817 else
1819 /* Separate the variables from the switches with a "--" arg. */
1820 if (p[-1] != '-')
1822 /* We did not already write a trailing " -". */
1823 *p++ = ' ';
1824 *p++ = '-';
1826 /* There is a trailing " -"; fill it out to " -- ". */
1827 *p++ = '-';
1828 *p++ = ' ';
1831 /* Copy in the string. */
1832 if (posix_pedantic)
1834 bcopy (posixref, p, sizeof posixref - 1);
1835 p += sizeof posixref - 1;
1837 else
1839 bcopy (ref, p, sizeof ref - 1);
1840 p += sizeof ref - 1;
1843 else if (p == &flagstring[1])
1845 words = 0;
1846 --p;
1848 else if (p[-1] == '-')
1849 /* Kill the final space and dash. */
1850 p -= 2;
1851 /* Terminate the string. */
1852 *p = '\0';
1854 v = define_variable ("MAKEFLAGS", 9,
1855 /* If there is just a single word of switches,
1856 omit the leading dash unless it is a single
1857 long option with two leading dashes. */
1858 &flagstring[(words == 1 && command_variables == 0
1859 && flagstring[1] != '-')
1860 ? 1 : 0],
1861 /* This used to use o_env, but that lost when a
1862 makefile defined MAKEFLAGS. Makefiles set
1863 MAKEFLAGS to add switches, but we still want
1864 to redefine its value with the full set of
1865 switches. Of course, an override or command
1866 definition will still take precedence. */
1867 o_file, 1);
1868 if (! all)
1869 /* The first time we are called, set MAKEFLAGS to always be exported.
1870 We should not do this again on the second call, because that is
1871 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1872 v->export = v_export;
1875 /* Print version information. */
1877 static void
1878 print_version ()
1880 static int printed_version = 0;
1882 char *precede = print_data_base_flag ? "# " : "";
1884 if (printed_version)
1885 /* Do it only once. */
1886 return;
1888 printf ("%sGNU Make version %s", precede, version_string);
1889 if (remote_description != 0 && *remote_description != '\0')
1890 printf ("-%s", remote_description);
1892 printf (", by Richard Stallman and Roland McGrath.\n\
1893 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
1894 %sThis is free software; see the source for copying conditions.\n\
1895 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1896 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1898 printed_version = 1;
1900 /* Flush stdout so the user doesn't have to wait to see the
1901 version information while things are thought about. */
1902 fflush (stdout);
1905 /* Print a bunch of information about this and that. */
1907 static void
1908 print_data_base ()
1910 extern char *ctime ();
1911 time_t when;
1913 when = time ((time_t *) 0);
1914 printf ("\n# Make data base, printed on %s", ctime (&when));
1916 print_variable_data_base ();
1917 print_dir_data_base ();
1918 print_rule_data_base ();
1919 print_file_data_base ();
1920 print_vpath_data_base ();
1922 when = time ((time_t *) 0);
1923 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1926 /* Exit with STATUS, cleaning up as necessary. */
1928 void
1929 die (status)
1930 int status;
1932 static char dying = 0;
1934 if (!dying)
1936 int err;
1938 dying = 1;
1940 /* Try to move back to the original directory. This is essential on
1941 MS-DOS (where there is really only one process), and on Unix it
1942 puts core files in the original directory instead of the -C
1943 directory. */
1944 if (directory_before_chdir != 0)
1945 chdir (directory_before_chdir);
1947 if (print_version_flag)
1948 print_version ();
1950 /* Wait for children to die. */
1951 for (err = status != 0; job_slots_used > 0; err = 0)
1952 reap_children (1, err);
1954 /* Remove the intermediate files. */
1955 remove_intermediates (0);
1957 if (print_data_base_flag)
1958 print_data_base ();
1960 log_working_directory (0);
1963 exit (status);
1966 /* Write a message indicating that we've just entered or
1967 left (according to ENTERING) the current directory. */
1969 void
1970 log_working_directory (entering)
1971 int entering;
1973 static int entered = 0;
1974 char *message = entering ? "Entering" : "Leaving";
1976 /* Print nothing without the flag. Don't print the entering message
1977 again if we already have. Don't print the leaving message if we
1978 haven't printed the entering message. */
1979 if (! print_directory_flag || entering == entered)
1980 return;
1982 entered = entering;
1984 if (print_data_base_flag)
1985 fputs ("# ", stdout);
1987 if (makelevel == 0)
1988 printf ("%s: %s ", program, message);
1989 else
1990 printf ("%s[%u]: %s ", program, makelevel, message);
1992 if (starting_directory == 0)
1993 puts ("an unknown directory");
1994 else
1995 printf ("directory `%s'\n", starting_directory);