(touch_file): Use message instead of printf.
[make.git] / main.c
blob17cdb5c9aceafa86d63071cc2d8a50f3689c3d41
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] == '\0')
371 fatal ("empty string invalid as file name");
373 if (name[0] == '~')
375 char *expanded = tilde_expand (name);
376 if (expanded != 0)
377 name = expanded; /* Memory leak; I don't care. */
380 /* This is also done in parse_file_seq, so this is redundant
381 for names read from makefiles. It is here for names passed
382 on the command line. */
383 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
385 name += 2;
386 while (*name == '/')
387 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
388 ++name;
391 if (*name == '\0')
393 /* It was all slashes! Move back to the dot and truncate
394 it after the first slash, so it becomes just "./". */
396 --name;
397 while (name[0] != '.');
398 name[2] = '\0';
401 return enter_file (savestring (name, strlen (name)));
404 /* Toggle -d on receipt of SIGUSR1. */
406 static RETSIGTYPE
407 debug_signal_handler (sig)
408 int sig;
410 debug_flag = ! debug_flag;
414 main (argc, argv, envp)
415 int argc;
416 char **argv;
417 char **envp;
419 extern void init_dir ();
420 extern RETSIGTYPE fatal_error_signal (), child_handler ();
421 register struct file *f;
422 register unsigned int i;
423 char **p;
424 struct dep *read_makefiles;
425 PATH_VAR (current_directory);
427 default_goal_file = 0;
428 reading_filename = 0;
429 reading_lineno_ptr = 0;
431 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
432 signame_init ();
433 #endif
435 #ifdef POSIX
436 sigemptyset (&fatal_signal_set);
437 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
438 #else
439 #ifdef HAVE_SIGSETMASK
440 fatal_signal_mask = 0;
441 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
442 #else
443 #define ADD_SIG(sig)
444 #endif
445 #endif
447 #define FATAL_SIG(sig) \
448 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
449 (void) signal ((sig), SIG_IGN); \
450 else \
451 ADD_SIG (sig);
453 FATAL_SIG (SIGHUP);
454 FATAL_SIG (SIGQUIT);
455 FATAL_SIG (SIGINT);
456 FATAL_SIG (SIGTERM);
458 #ifdef SIGDANGER
459 FATAL_SIG (SIGDANGER);
460 #endif
461 #ifdef SIGXCPU
462 FATAL_SIG (SIGXCPU);
463 #endif
464 #ifdef SIGXFSZ
465 FATAL_SIG (SIGXFSZ);
466 #endif
468 #undef FATAL_SIG
470 /* Make sure stdout is line-buffered. */
472 #ifdef HAVE_SETLINEBUF
473 setlinebuf (stdout);
474 #else
475 #ifndef SETVBUF_REVERSED
476 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
477 #else /* setvbuf not reversed. */
478 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
479 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
480 #endif /* setvbuf reversed. */
481 #endif /* setlinebuf missing. */
483 /* Initialize the directory hashing code. */
484 init_dir ();
486 /* Figure out where this program lives. */
488 if (argv[0] == 0)
489 argv[0] = "";
490 if (argv[0][0] == '\0')
491 program = "make";
492 else
494 program = rindex (argv[0], '/');
495 #ifdef __MSDOS__
496 if (program == 0)
497 program = rindex (argv[0], '\\');
498 if (program == 0)
499 program = rindex (argv[0], ':');
500 #endif
501 if (program == 0)
502 program = argv[0];
503 else
504 ++program;
507 /* Set up to access user data (files). */
508 user_access ();
510 /* Figure out where we are. */
512 if (getcwd (current_directory, GET_PATH_MAX) == 0)
514 #ifdef HAVE_GETCWD
515 perror_with_name ("getcwd: ", "");
516 #else
517 error ("getwd: %s", current_directory);
518 #endif
519 current_directory[0] = '\0';
520 directory_before_chdir = 0;
522 else
523 directory_before_chdir = savestring (current_directory,
524 strlen (current_directory));
526 /* Read in variables from the environment. It is important that this be
527 done before $(MAKE) is are figured out so its definitions will not be
528 one from the environment. */
530 for (i = 0; envp[i] != 0; ++i)
532 register char *ep = envp[i];
533 while (*ep != '=')
534 ++ep;
535 /* The result of pointer arithmetic is cast to unsigned int for
536 machines where ptrdiff_t is a different size that doesn't widen
537 the same. */
538 define_variable (envp[i], (unsigned int) (ep - envp[i]),
539 ep + 1, o_env, 1)
540 /* Force exportation of every variable culled from the environment.
541 We used to rely on target_environment's v_default code to do this.
542 But that does not work for the case where an environment variable
543 is redefined in a makefile with `override'; it should then still
544 be exported, because it was originally in the environment. */
545 ->export = v_export;
548 /* Decode the switches. */
550 decode_env_switches ("MAKEFLAGS", 9);
551 #if 0
552 /* People write things like:
553 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
554 and we set the -p, -i and -e switches. Doesn't seem quite right. */
555 decode_env_switches ("MFLAGS", 6);
556 #endif
557 decode_switches (argc, argv, 0);
559 /* Print version information. */
561 if (print_version_flag || print_data_base_flag || debug_flag)
562 print_version ();
564 /* `make --version' is supposed to just print the version and exit. */
565 if (print_version_flag)
566 die (0);
568 #ifndef __MSDOS__
569 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
570 (If it is a relative pathname with a slash, prepend our directory name
571 so the result will run the same program regardless of the current dir.
572 If it is a name with no slash, we can only hope that PATH did not
573 find it in the current directory.) */
575 if (current_directory[0] != '\0'
576 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
577 argv[0] = concat (current_directory, "/", argv[0]);
578 #endif
580 /* The extra indirection through $(MAKE_COMMAND) is done
581 for hysterical raisins. */
582 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
583 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
585 if (command_variables != 0)
587 struct command_variable *cv;
588 struct variable *v;
589 unsigned int len = 0;
590 char *value, *p;
592 /* Figure out how much space will be taken up by the command-line
593 variable definitions. */
594 for (cv = command_variables; cv != 0; cv = cv->next)
596 v = cv->variable;
597 len += 2 * strlen (v->name);
598 if (! v->recursive)
599 ++len;
600 ++len;
601 len += 2 * strlen (v->value);
604 /* Now allocate a buffer big enough and fill it. */
605 p = value = (char *) alloca (len);
606 for (cv = command_variables; cv != 0; cv = cv->next)
608 v = cv->variable;
609 p = quote_as_word (p, v->name, 0);
610 if (! v->recursive)
611 *p++ = ':';
612 *p++ = '=';
613 p = quote_as_word (p, v->value, 0);
614 *p++ = ' ';
616 p[-1] = '\0'; /* Kill the final space and terminate. */
618 /* Define an unchangeable variable with a name that no POSIX.2
619 makefile could validly use for its own variable. */
620 (void) define_variable ("-*-command-variables-*-", 23,
621 value, o_automatic, 0);
623 /* Define the variable; this will not override any user definition.
624 Normally a reference to this variable is written into the value of
625 MAKEFLAGS, allowing the user to override this value to affect the
626 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
627 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
628 a reference to this hidden variable is written instead. */
629 (void) define_variable ("MAKEOVERRIDES", 13,
630 "${-*-command-variables-*-}", o_env, 1);
633 /* If there were -C flags, move ourselves about. */
634 if (directories != 0)
635 for (i = 0; directories->list[i] != 0; ++i)
637 char *dir = directories->list[i];
638 if (dir[0] == '~')
640 char *expanded = tilde_expand (dir);
641 if (expanded != 0)
642 dir = expanded;
644 if (chdir (dir) < 0)
645 pfatal_with_name (dir);
646 if (dir != directories->list[i])
647 free (dir);
650 /* Figure out the level of recursion. */
652 struct variable *v = lookup_variable ("MAKELEVEL", 9);
653 if (v != 0 && *v->value != '\0' && *v->value != '-')
654 makelevel = (unsigned int) atoi (v->value);
655 else
656 makelevel = 0;
659 /* Except under -s, always do -w in sub-makes and under -C. */
660 if (!silent_flag && (directories != 0 || makelevel > 0))
661 print_directory_flag = 1;
663 /* Let the user disable that with --no-print-directory. */
664 if (inhibit_print_directory_flag)
665 print_directory_flag = 0;
667 /* Construct the list of include directories to search. */
669 construct_include_path (include_directories == 0 ? (char **) 0
670 : include_directories->list);
672 /* Figure out where we are now, after chdir'ing. */
673 if (directories == 0)
674 /* We didn't move, so we're still in the same place. */
675 starting_directory = current_directory;
676 else
678 if (getcwd (current_directory, GET_PATH_MAX) == 0)
680 #ifdef HAVE_GETCWD
681 perror_with_name ("getcwd: ", "");
682 #else
683 error ("getwd: %s", current_directory);
684 #endif
685 starting_directory = 0;
687 else
688 starting_directory = current_directory;
691 /* Tell the user where he is. */
693 if (print_directory_flag)
694 log_working_directory (1);
696 /* Read any stdin makefiles into temporary files. */
698 if (makefiles != 0)
700 register unsigned int i;
701 for (i = 0; i < makefiles->idx; ++i)
702 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
704 /* This makefile is standard input. Since we may re-exec
705 and thus re-read the makefiles, we read standard input
706 into a temporary file and read from that. */
707 FILE *outfile;
709 /* Make a unique filename. */
710 #ifdef HAVE_MKTEMP
711 static char name[] = "/tmp/GmXXXXXX";
712 (void) mktemp (name);
713 #else
714 static char name[L_tmpnam];
715 (void) tmpnam (name);
716 #endif
718 outfile = fopen (name, "w");
719 if (outfile == 0)
720 pfatal_with_name ("fopen (temporary file)");
721 while (!feof (stdin))
723 char buf[2048];
724 int n = fread (buf, 1, sizeof(buf), stdin);
725 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
726 pfatal_with_name ("fwrite (temporary file)");
728 /* Try to make sure we won't remake the temporary
729 file when we are re-exec'd. Kludge-o-matic! */
730 fprintf (outfile, "%s:;\n", name);
731 (void) fclose (outfile);
733 /* Replace the name that read_all_makefiles will
734 see with the name of the temporary file. */
736 char *temp;
737 /* SGI compiler requires alloca's result be assigned simply. */
738 temp = (char *) alloca (sizeof (name));
739 bcopy (name, temp, sizeof (name));
740 makefiles->list[i] = temp;
743 /* Make sure the temporary file will not be remade. */
744 f = enter_file (savestring (name, sizeof name - 1));
745 f->updated = 1;
746 f->update_status = 0;
747 f->command_state = cs_finished;
748 /* Let it be removed when we're done. */
749 f->intermediate = 1;
750 /* But don't mention it. */
751 f->dontcare = 1;
755 /* Set up to handle children dying. This must be done before
756 reading in the makefiles so that `shell' function calls will work. */
758 #ifdef SIGCHLD
759 (void) signal (SIGCHLD, child_handler);
760 #endif
761 #ifdef SIGCLD
762 (void) signal (SIGCLD, child_handler);
763 #endif
765 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
766 #ifdef SIGUSR1
767 (void) signal (SIGUSR1, debug_signal_handler);
768 #endif
770 /* Define the initial list of suffixes for old-style rules. */
772 set_default_suffixes ();
774 /* Define the file rules for the built-in suffix rules. These will later
775 be converted into pattern rules. We used to do this in
776 install_default_implicit_rules, but since that happens after reading
777 makefiles, it results in the built-in pattern rules taking precedence
778 over makefile-specified suffix rules, which is wrong. */
780 install_default_suffix_rules ();
782 /* Define some internal and special variables. */
784 define_automatic_variables ();
786 /* Set up the MAKEFLAGS and MFLAGS variables
787 so makefiles can look at them. */
789 define_makeflags (0, 0);
791 /* Define the default variables. */
792 define_default_variables ();
794 /* Read all the makefiles. */
796 default_file = enter_file (".DEFAULT");
798 read_makefiles
799 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
801 /* Decode switches again, in case the variables were set by the makefile. */
802 decode_env_switches ("MAKEFLAGS", 9);
803 #if 0
804 decode_env_switches ("MFLAGS", 6);
805 #endif
807 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
809 define_makeflags (1, 0);
811 /* Make each `struct dep' point at the `struct file' for the file
812 depended on. Also do magic for special targets. */
814 snap_deps ();
816 /* Convert old-style suffix rules to pattern rules. It is important to
817 do this before installing the built-in pattern rules below, so that
818 makefile-specified suffix rules take precedence over built-in pattern
819 rules. */
821 convert_to_pattern ();
823 /* Install the default implicit pattern rules.
824 This used to be done before reading the makefiles.
825 But in that case, built-in pattern rules were in the chain
826 before user-defined ones, so they matched first. */
828 install_default_implicit_rules ();
830 /* Compute implicit rule limits. */
832 count_implicit_rule_limits ();
834 /* Construct the listings of directories in VPATH lists. */
836 build_vpath_lists ();
838 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
839 and as having been updated already, and files given with -W flags as
840 brand new (time-stamp as far as possible into the future). */
842 if (old_files != 0)
843 for (p = old_files->list; *p != 0; ++p)
845 f = enter_command_line_file (*p);
846 f->last_mtime = (time_t) 1;
847 f->updated = 1;
848 f->update_status = 0;
849 f->command_state = cs_finished;
852 if (new_files != 0)
854 for (p = new_files->list; *p != 0; ++p)
856 f = enter_command_line_file (*p);
857 f->last_mtime = NEW_MTIME;
861 if (read_makefiles != 0)
863 /* Update any makefiles if necessary. */
865 time_t *makefile_mtimes = 0;
866 unsigned int mm_idx = 0;
868 if (debug_flag)
869 puts ("Updating makefiles....");
871 /* Remove any makefiles we don't want to try to update.
872 Also record the current modtimes so we can compare them later. */
874 register struct dep *d, *last;
875 last = 0;
876 d = read_makefiles;
877 while (d != 0)
879 register struct file *f = d->file;
880 if (f->double_colon)
881 for (f = f->double_colon; f != NULL; f = f->prev)
883 if (f->deps == 0 && f->cmds != 0)
885 /* This makefile is a :: target with commands, but
886 no dependencies. So, it will always be remade.
887 This might well cause an infinite loop, so don't
888 try to remake it. (This will only happen if
889 your makefiles are written exceptionally
890 stupidly; but if you work for Athena, that's how
891 you write your makefiles.) */
893 if (debug_flag)
894 printf ("Makefile `%s' might loop; not remaking it.\n",
895 f->name);
897 if (last == 0)
898 read_makefiles = d->next;
899 else
900 last->next = d->next;
902 /* Free the storage. */
903 free ((char *) d);
905 d = last == 0 ? 0 : last->next;
907 break;
910 if (f == NULL || !f->double_colon)
912 if (makefile_mtimes == 0)
913 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
914 else
915 makefile_mtimes = (time_t *)
916 xrealloc ((char *) makefile_mtimes,
917 (mm_idx + 1) * sizeof (time_t));
918 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
919 last = d;
920 d = d->next;
925 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
926 define_makeflags (1, 1);
928 switch (update_goal_chain (read_makefiles, 1))
930 case 1:
931 default:
932 #define BOGUS_UPDATE_STATUS 0
933 assert (BOGUS_UPDATE_STATUS);
934 break;
936 case -1:
937 /* Did nothing. */
938 break;
940 case 2:
941 /* Failed to update. Figure out if we care. */
943 /* Nonzero if any makefile was successfully remade. */
944 int any_remade = 0;
945 /* Nonzero if any makefile we care about failed
946 in updating or could not be found at all. */
947 int any_failed = 0;
948 register unsigned int i;
950 for (i = 0; read_makefiles != 0; ++i)
952 struct dep *d = read_makefiles;
953 read_makefiles = d->next;
954 if (d->file->updated)
956 /* This makefile was updated. */
957 if (d->file->update_status == 0)
959 /* It was successfully updated. */
960 any_remade |= (file_mtime_no_search (d->file)
961 != makefile_mtimes[i]);
963 else if (! (d->changed & RM_DONTCARE))
965 time_t mtime;
966 /* The update failed and this makefile was not
967 from the MAKEFILES variable, so we care. */
968 error ("Failed to remake makefile `%s'.",
969 d->file->name);
970 mtime = file_mtime_no_search (d->file);
971 any_remade |= (mtime != (time_t) -1
972 && mtime != makefile_mtimes[i]);
975 else
976 /* This makefile was not found at all. */
977 if (! (d->changed & RM_DONTCARE))
979 /* This is a makefile we care about. See how much. */
980 if (d->changed & RM_INCLUDED)
981 /* An included makefile. We don't need
982 to die, but we do want to complain. */
983 error ("Included makefile `%s' was not found.",
984 dep_name (d));
985 else
987 /* A normal makefile. We must die later. */
988 error ("Makefile `%s' was not found", dep_name (d));
989 any_failed = 1;
993 free ((char *) d);
996 if (any_remade)
997 goto re_exec;
998 else if (any_failed)
999 die (2);
1000 else
1001 break;
1004 case 0:
1005 re_exec:
1006 /* Updated successfully. Re-exec ourselves. */
1008 remove_intermediates (0);
1010 if (print_data_base_flag)
1011 print_data_base ();
1013 if (print_directory_flag)
1014 log_working_directory (0);
1016 if (makefiles != 0)
1018 /* These names might have changed. */
1019 register unsigned int i, j = 0;
1020 for (i = 1; i < argc; ++i)
1021 if (!strcmp (argv[i], "-f")) /* XXX */
1023 char *p = &argv[i][2];
1024 if (*p == '\0')
1025 argv[++i] = makefiles->list[j];
1026 else
1027 argv[i] = concat ("-f", makefiles->list[j], "");
1028 ++j;
1032 if (directories != 0 && directories->idx > 0)
1034 char bad;
1035 if (directory_before_chdir != 0)
1037 if (chdir (directory_before_chdir) < 0)
1039 perror_with_name ("chdir", "");
1040 bad = 1;
1042 else
1043 bad = 0;
1045 else
1046 bad = 1;
1047 if (bad)
1048 fatal ("Couldn't change back to original directory.");
1051 for (p = environ; *p != 0; ++p)
1052 if (!strncmp (*p, "MAKELEVEL=", 10))
1054 /* The SGI compiler apparently can't understand
1055 the concept of storing the result of a function
1056 in something other than a local variable. */
1057 char *sgi_loses;
1058 sgi_loses = (char *) alloca (40);
1059 *p = sgi_loses;
1060 sprintf (*p, "MAKELEVEL=%u", makelevel);
1061 break;
1064 if (debug_flag)
1066 char **p;
1067 fputs ("Re-executing:", stdout);
1068 for (p = argv; *p != 0; ++p)
1069 printf (" %s", *p);
1070 puts ("");
1073 fflush (stdout);
1074 fflush (stderr);
1076 exec_command (argv, environ);
1077 /* NOTREACHED */
1081 /* Set up `MAKEFLAGS' again for the normal targets. */
1082 define_makeflags (1, 0);
1085 int status;
1087 /* If there were no command-line goals, use the default. */
1088 if (goals == 0)
1090 if (default_goal_file != 0)
1092 goals = (struct dep *) xmalloc (sizeof (struct dep));
1093 goals->next = 0;
1094 goals->name = 0;
1095 goals->file = default_goal_file;
1098 else
1099 lastgoal->next = 0;
1101 if (goals != 0)
1103 /* Update the goals. */
1105 if (debug_flag)
1106 puts ("Updating goal targets....");
1108 switch (update_goal_chain (goals, 0))
1110 case -1:
1111 /* Nothing happened. */
1112 case 0:
1113 /* Updated successfully. */
1114 status = 0;
1115 break;
1116 case 2:
1117 /* Updating failed. */
1118 status = 2;
1119 break;
1120 case 1:
1121 /* We are under -q and would run some commands. */
1122 status = 1;
1123 break;
1124 default:
1125 abort ();
1128 else
1130 if (read_makefiles == 0)
1131 fatal ("No targets specified and no makefile found");
1132 else
1133 fatal ("No targets");
1136 /* Exit. */
1137 die (status);
1140 return 0;
1143 /* Parsing of arguments, decoding of switches. */
1145 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1146 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1147 (sizeof (long_option_aliases) /
1148 sizeof (long_option_aliases[0]))];
1150 /* Fill in the string and vector for getopt. */
1151 static void
1152 init_switches ()
1154 register char *p;
1155 register int c;
1156 register unsigned int i;
1158 if (options[0] != '\0')
1159 /* Already done. */
1160 return;
1162 p = options;
1164 /* Return switch and non-switch args in order, regardless of
1165 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1166 *p++ = '-';
1168 for (i = 0; switches[i].c != '\0'; ++i)
1170 long_options[i].name = (switches[i].long_name == 0 ? "" :
1171 switches[i].long_name);
1172 long_options[i].flag = 0;
1173 long_options[i].val = switches[i].c;
1174 if (isalnum (switches[i].c))
1175 *p++ = switches[i].c;
1176 switch (switches[i].type)
1178 case flag:
1179 case flag_off:
1180 case ignore:
1181 long_options[i].has_arg = no_argument;
1182 break;
1184 case string:
1185 case positive_int:
1186 case floating:
1187 if (isalnum (switches[i].c))
1188 *p++ = ':';
1189 if (switches[i].noarg_value != 0)
1191 if (isalnum (switches[i].c))
1192 *p++ = ':';
1193 long_options[i].has_arg = optional_argument;
1195 else
1196 long_options[i].has_arg = required_argument;
1197 break;
1200 *p = '\0';
1201 for (c = 0; c < (sizeof (long_option_aliases) /
1202 sizeof (long_option_aliases[0]));
1203 ++c)
1204 long_options[i++] = long_option_aliases[c];
1205 long_options[i].name = 0;
1208 static void
1209 handle_non_switch_argument (arg, env)
1210 char *arg;
1211 int env;
1213 /* Non-option argument. It might be a variable definition. */
1214 struct variable *v;
1215 if (arg[0] == '-' && arg[1] == '\0')
1216 /* Ignore plain `-' for compatibility. */
1217 return;
1218 v = try_variable_definition ((char *) 0, 0, arg, o_command);
1219 if (v != 0)
1221 /* It is indeed a variable definition. Record a pointer to
1222 the variable for later use in define_makeflags. */
1223 struct command_variable *cv
1224 = (struct command_variable *) xmalloc (sizeof (*cv));
1225 cv->variable = v;
1226 cv->next = command_variables;
1227 command_variables = cv;
1229 else if (! env)
1231 /* Not an option or variable definition; it must be a goal
1232 target! Enter it as a file and add it to the dep chain of
1233 goals. */
1234 struct file *f = enter_command_line_file (arg);
1235 f->cmd_target = 1;
1237 if (goals == 0)
1239 goals = (struct dep *) xmalloc (sizeof (struct dep));
1240 lastgoal = goals;
1242 else
1244 lastgoal->next
1245 = (struct dep *) xmalloc (sizeof (struct dep));
1246 lastgoal = lastgoal->next;
1248 lastgoal->name = 0;
1249 lastgoal->file = f;
1253 /* Decode switches from ARGC and ARGV.
1254 They came from the environment if ENV is nonzero. */
1256 static void
1257 decode_switches (argc, argv, env)
1258 int argc;
1259 char **argv;
1260 int env;
1262 int bad = 0;
1263 register const struct command_switch *cs;
1264 register struct stringlist *sl;
1265 register int c;
1267 /* getopt does most of the parsing for us.
1268 First, get its vectors set up. */
1270 init_switches ();
1272 /* Let getopt produce error messages for the command line,
1273 but not for options from the environment. */
1274 opterr = !env;
1275 /* Reset getopt's state. */
1276 optind = 0;
1278 while (optind < argc)
1280 /* Parse the next argument. */
1281 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1282 if (c == EOF)
1283 /* End of arguments, or "--" marker seen. */
1284 break;
1285 else if (c == 1)
1286 /* An argument not starting with a dash. */
1287 handle_non_switch_argument (optarg, env);
1288 else if (c == '?')
1289 /* Bad option. We will print a usage message and die later.
1290 But continue to parse the other options so the user can
1291 see all he did wrong. */
1292 bad = 1;
1293 else
1294 for (cs = switches; cs->c != '\0'; ++cs)
1295 if (cs->c == c)
1297 /* Whether or not we will actually do anything with
1298 this switch. We test this individually inside the
1299 switch below rather than just once outside it, so that
1300 options which are to be ignored still consume args. */
1301 int doit = !env || cs->env;
1303 switch (cs->type)
1305 default:
1306 abort ();
1308 case ignore:
1309 break;
1311 case flag:
1312 case flag_off:
1313 if (doit)
1314 *(int *) cs->value_ptr = cs->type == flag;
1315 break;
1317 case string:
1318 if (!doit)
1319 break;
1321 if (optarg == 0)
1322 optarg = cs->noarg_value;
1324 sl = *(struct stringlist **) cs->value_ptr;
1325 if (sl == 0)
1327 sl = (struct stringlist *)
1328 xmalloc (sizeof (struct stringlist));
1329 sl->max = 5;
1330 sl->idx = 0;
1331 sl->list = (char **) xmalloc (5 * sizeof (char *));
1332 *(struct stringlist **) cs->value_ptr = sl;
1334 else if (sl->idx == sl->max - 1)
1336 sl->max += 5;
1337 sl->list = (char **)
1338 xrealloc ((char *) sl->list,
1339 sl->max * sizeof (char *));
1341 sl->list[sl->idx++] = optarg;
1342 sl->list[sl->idx] = 0;
1343 break;
1345 case positive_int:
1346 if (optarg == 0 && argc > optind
1347 && isdigit (argv[optind][0]))
1348 optarg = argv[optind++];
1350 if (!doit)
1351 break;
1353 if (optarg != 0)
1355 int i = atoi (optarg);
1356 if (i < 1)
1358 if (doit)
1359 error ("the `-%c' option requires a \
1360 positive integral argument",
1361 cs->c);
1362 bad = 1;
1364 else
1365 *(unsigned int *) cs->value_ptr = i;
1367 else
1368 *(unsigned int *) cs->value_ptr
1369 = *(unsigned int *) cs->noarg_value;
1370 break;
1372 case floating:
1373 if (optarg == 0 && optind < argc
1374 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1375 optarg = argv[optind++];
1377 if (doit)
1378 *(double *) cs->value_ptr
1379 = (optarg != 0 ? atof (optarg)
1380 : *(double *) cs->noarg_value);
1382 break;
1385 /* We've found the switch. Stop looking. */
1386 break;
1390 /* There are no more options according to getting getopt, but there may
1391 be some arguments left. Since we have asked for non-option arguments
1392 to be returned in order, this only happens when there is a "--"
1393 argument to prevent later arguments from being options. */
1394 while (optind < argc)
1395 handle_non_switch_argument (argv[optind++], env);
1398 if (!env && (bad || print_usage_flag))
1400 /* Print a nice usage message. */
1401 FILE *usageto;
1403 if (print_version_flag)
1404 print_version ();
1406 usageto = bad ? stderr : stdout;
1408 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1410 fputs ("Options:\n", usageto);
1411 for (cs = switches; cs->c != '\0'; ++cs)
1413 char buf[1024], shortarg[50], longarg[50], *p;
1415 if (cs->description[0] == '-')
1416 continue;
1418 switch (long_options[cs - switches].has_arg)
1420 case no_argument:
1421 shortarg[0] = longarg[0] = '\0';
1422 break;
1423 case required_argument:
1424 sprintf (longarg, "=%s", cs->argdesc);
1425 sprintf (shortarg, " %s", cs->argdesc);
1426 break;
1427 case optional_argument:
1428 sprintf (longarg, "[=%s]", cs->argdesc);
1429 sprintf (shortarg, " [%s]", cs->argdesc);
1430 break;
1433 p = buf;
1435 if (isalnum (cs->c))
1437 sprintf (buf, " -%c%s", cs->c, shortarg);
1438 p += strlen (p);
1440 if (cs->long_name != 0)
1442 unsigned int i;
1443 sprintf (p, "%s--%s%s",
1444 !isalnum (cs->c) ? " " : ", ",
1445 cs->long_name, longarg);
1446 p += strlen (p);
1447 for (i = 0; i < (sizeof (long_option_aliases) /
1448 sizeof (long_option_aliases[0]));
1449 ++i)
1450 if (long_option_aliases[i].val == cs->c)
1452 sprintf (p, ", --%s%s",
1453 long_option_aliases[i].name, longarg);
1454 p += strlen (p);
1458 const struct command_switch *ncs = cs;
1459 while ((++ncs)->c != '\0')
1460 if (ncs->description[0] == '-' &&
1461 ncs->description[1] == cs->c)
1463 /* This is another switch that does the same
1464 one as the one we are processing. We want
1465 to list them all together on one line. */
1466 sprintf (p, ", -%c%s", ncs->c, shortarg);
1467 p += strlen (p);
1468 if (ncs->long_name != 0)
1470 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1471 p += strlen (p);
1476 if (p - buf > DESCRIPTION_COLUMN - 2)
1477 /* The list of option names is too long to fit on the same
1478 line with the description, leaving at least two spaces.
1479 Print it on its own line instead. */
1481 fprintf (usageto, "%s\n", buf);
1482 buf[0] = '\0';
1485 fprintf (usageto, "%*s%s.\n",
1486 - DESCRIPTION_COLUMN,
1487 buf, cs->description);
1490 die (bad ? 2 : 0);
1494 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1495 We do this by chopping the value into a vector of words, prepending a
1496 dash to the first word if it lacks one, and passing the vector to
1497 decode_switches. */
1499 static void
1500 decode_env_switches (envar, len)
1501 char *envar;
1502 unsigned int len;
1504 char *varref = (char *) alloca (2 + len + 2);
1505 char *value, *p;
1506 int argc;
1507 char **argv;
1509 /* Get the variable's value. */
1510 varref[0] = '$';
1511 varref[1] = '(';
1512 bcopy (envar, &varref[2], len);
1513 varref[2 + len] = ')';
1514 varref[2 + len + 1] = '\0';
1515 value = variable_expand (varref);
1517 /* Skip whitespace, and check for an empty value. */
1518 value = next_token (value);
1519 len = strlen (value);
1520 if (len == 0)
1521 return;
1523 /* Allocate a vector that is definitely big enough. */
1524 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1526 /* Allocate a buffer to copy the value into while we split it into words
1527 and unquote it. We must use permanent storage for this because
1528 decode_switches may store pointers into the passed argument words. */
1529 p = (char *) xmalloc (2 * len);
1531 /* getopt will look at the arguments starting at ARGV[1].
1532 Prepend a spacer word. */
1533 argv[0] = 0;
1534 argc = 1;
1535 argv[argc] = p;
1536 while (*value != '\0')
1538 if (*value == '\\')
1539 ++value; /* Skip the backslash. */
1540 else if (isblank (*value))
1542 /* End of the word. */
1543 *p++ = '\0';
1544 argv[++argc] = p;
1546 ++value;
1547 while (isblank (*value));
1548 continue;
1550 *p++ = *value++;
1552 *p = '\0';
1553 argv[++argc] = 0;
1555 if (argc == 2 && argv[1][0] != '-')
1557 /* There is just one word in the value, and it is not a switch.
1558 Either this is the single-word form and we should prepend a dash
1559 before calling decode_switches, or this is the multi-word form and
1560 there is no dash because it is a variable definition. */
1561 struct variable *v;
1562 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1563 if (v != 0)
1565 /* It was indeed a variable definition, and now it has been
1566 processed. There is nothing for decode_switches to do.
1567 Record a pointer to the variable for later use in
1568 define_makeflags. */
1569 struct command_variable *cv
1570 = (struct command_variable *) xmalloc (sizeof (*cv));
1571 cv->variable = v;
1572 cv->next = command_variables;
1573 command_variables = cv;
1574 return;
1577 /* It wasn't a variable definition, so it's some switches without a
1578 leading dash. Add one and pass it along to decode_switches. We
1579 need permanent storage for this in case decode_switches saves
1580 pointers into the value. */
1581 argv[1] = concat ("-", argv[1], "");
1584 /* Parse those words. */
1585 decode_switches (argc, argv, 1);
1588 /* Quote the string IN so that it will be interpreted as a single word with
1589 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1590 signs to avoid variable expansion in make itself. Write the result into
1591 OUT, returning the address of the next character to be written.
1592 Allocating space for OUT twice the length of IN (thrice if
1593 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1595 static char *
1596 quote_as_word (out, in, double_dollars)
1597 char *out, *in;
1598 int double_dollars;
1600 while (*in != '\0')
1602 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1603 *out++ = '\\';
1604 if (double_dollars && *in == '$')
1605 *out++ = '$';
1606 *out++ = *in++;
1609 return out;
1612 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1613 command switches. Include options with args if ALL is nonzero.
1614 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1616 static void
1617 define_makeflags (all, makefile)
1618 int all, makefile;
1620 static const char ref[] = "$(MAKEOVERRIDES)";
1621 static const char posixref[] = "$(-*-command-variables-*-)";
1622 register const struct command_switch *cs;
1623 char *flagstring;
1624 register char *p;
1625 unsigned int words;
1626 struct variable *v;
1628 /* We will construct a linked list of `struct flag's describing
1629 all the flags which need to go in MAKEFLAGS. Then, once we
1630 know how many there are and their lengths, we can put them all
1631 together in a string. */
1633 struct flag
1635 struct flag *next;
1636 const struct command_switch *cs;
1637 char *arg;
1639 struct flag *flags = 0;
1640 unsigned int flagslen = 0;
1641 #define ADD_FLAG(ARG, LEN) \
1642 do { \
1643 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1644 new->cs = cs; \
1645 new->arg = (ARG); \
1646 new->next = flags; \
1647 flags = new; \
1648 if (new->arg == 0) \
1649 ++flagslen; /* Just a single flag letter. */ \
1650 else \
1651 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1652 if (!isalnum (cs->c)) \
1653 /* This switch has no single-letter version, so we use the long. */ \
1654 flagslen += 2 + strlen (cs->long_name); \
1655 } while (0)
1657 for (cs = switches; cs->c != '\0'; ++cs)
1658 if (cs->toenv && (!makefile || !cs->no_makefile))
1659 switch (cs->type)
1661 default:
1662 abort ();
1664 case ignore:
1665 break;
1667 case flag:
1668 case flag_off:
1669 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1670 && (cs->default_value == 0
1671 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1672 ADD_FLAG (0, 0);
1673 break;
1675 case positive_int:
1676 if (all)
1678 if ((cs->default_value != 0
1679 && (*(unsigned int *) cs->value_ptr
1680 == *(unsigned int *) cs->default_value)))
1681 break;
1682 else if (cs->noarg_value != 0
1683 && (*(unsigned int *) cs->value_ptr ==
1684 *(unsigned int *) cs->noarg_value))
1685 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1686 else if (cs->c == 'j')
1687 /* Special case for `-j'. */
1688 ADD_FLAG ("1", 1);
1689 else
1691 char *buf = (char *) alloca (30);
1692 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1693 ADD_FLAG (buf, strlen (buf));
1696 break;
1698 case floating:
1699 if (all)
1701 if (cs->default_value != 0
1702 && (*(double *) cs->value_ptr
1703 == *(double *) cs->default_value))
1704 break;
1705 else if (cs->noarg_value != 0
1706 && (*(double *) cs->value_ptr
1707 == *(double *) cs->noarg_value))
1708 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1709 else
1711 char *buf = (char *) alloca (100);
1712 sprintf (buf, "%g", *(double *) cs->value_ptr);
1713 ADD_FLAG (buf, strlen (buf));
1716 break;
1718 case string:
1719 if (all)
1721 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1722 if (sl != 0)
1724 /* Add the elements in reverse order, because
1725 all the flags get reversed below; and the order
1726 matters for some switches (like -I). */
1727 register unsigned int i = sl->idx;
1728 while (i-- > 0)
1729 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1732 break;
1735 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1737 #undef ADD_FLAG
1739 /* Construct the value in FLAGSTRING.
1740 We allocate enough space for a preceding dash and trailing null. */
1741 flagstring = (char *) alloca (1 + flagslen + 1);
1742 p = flagstring;
1743 words = 1;
1744 *p++ = '-';
1745 while (flags != 0)
1747 /* Add the flag letter or name to the string. */
1748 if (!isalnum (flags->cs->c))
1750 *p++ = '-';
1751 strcpy (p, flags->cs->long_name);
1752 p += strlen (p);
1754 else
1755 *p++ = flags->cs->c;
1756 if (flags->arg != 0)
1758 /* A flag that takes an optional argument which in this case is
1759 omitted is specified by ARG being "". We must distinguish
1760 because a following flag appended without an intervening " -"
1761 is considered the arg for the first. */
1762 if (flags->arg[0] != '\0')
1764 /* Add its argument too. */
1765 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1766 p = quote_as_word (p, flags->arg, 1);
1768 ++words;
1769 /* Write a following space and dash, for the next flag. */
1770 *p++ = ' ';
1771 *p++ = '-';
1773 else if (!isalnum (flags->cs->c))
1775 ++words;
1776 /* Long options must each go in their own word,
1777 so we write the following space and dash. */
1778 *p++ = ' ';
1779 *p++ = '-';
1781 flags = flags->next;
1785 /* Define MFLAGS before appending variable definitions. */
1787 if (p == &flagstring[1])
1788 /* No flags. */
1789 flagstring[0] = '\0';
1790 else if (p[-1] == '-')
1791 /* Kill the final space and dash. */
1792 p[-2] = '\0';
1793 else
1794 /* Terminate the string. */
1795 *p = '\0';
1797 /* Since MFLAGS is not parsed for flags, there is no reason to
1798 override any makefile redefinition. */
1799 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1802 if (all && command_variables != 0)
1804 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1805 command-line variable definitions. */
1807 if (p == &flagstring[1])
1808 /* No flags written, so elide the leading dash already written. */
1809 p = flagstring;
1810 else
1812 /* Separate the variables from the switches with a "--" arg. */
1813 if (p[-1] != '-')
1815 /* We did not already write a trailing " -". */
1816 *p++ = ' ';
1817 *p++ = '-';
1819 /* There is a trailing " -"; fill it out to " -- ". */
1820 *p++ = '-';
1821 *p++ = ' ';
1824 /* Copy in the string. */
1825 if (posix_pedantic)
1827 bcopy (posixref, p, sizeof posixref - 1);
1828 p += sizeof posixref - 1;
1830 else
1832 bcopy (ref, p, sizeof ref - 1);
1833 p += sizeof ref - 1;
1836 else if (p == &flagstring[1])
1838 words = 0;
1839 --p;
1841 else if (p[-1] == '-')
1842 /* Kill the final space and dash. */
1843 p -= 2;
1844 /* Terminate the string. */
1845 *p = '\0';
1847 v = define_variable ("MAKEFLAGS", 9,
1848 /* If there is just a single word of switches,
1849 omit the leading dash unless it is a single
1850 long option with two leading dashes. */
1851 &flagstring[(words == 1 && command_variables == 0
1852 && flagstring[1] != '-')
1853 ? 1 : 0],
1854 /* This used to use o_env, but that lost when a
1855 makefile defined MAKEFLAGS. Makefiles set
1856 MAKEFLAGS to add switches, but we still want
1857 to redefine its value with the full set of
1858 switches. Of course, an override or command
1859 definition will still take precedence. */
1860 o_file, 1);
1861 if (! all)
1862 /* The first time we are called, set MAKEFLAGS to always be exported.
1863 We should not do this again on the second call, because that is
1864 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1865 v->export = v_export;
1868 /* Print version information. */
1870 static void
1871 print_version ()
1873 static int printed_version = 0;
1875 char *precede = print_data_base_flag ? "# " : "";
1877 if (printed_version)
1878 /* Do it only once. */
1879 return;
1881 printf ("%sGNU Make version %s", precede, version_string);
1882 if (remote_description != 0 && *remote_description != '\0')
1883 printf ("-%s", remote_description);
1885 printf (", by Richard Stallman and Roland McGrath.\n\
1886 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
1887 %sThis is free software; see the source for copying conditions.\n\
1888 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1889 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1891 printed_version = 1;
1893 /* Flush stdout so the user doesn't have to wait to see the
1894 version information while things are thought about. */
1895 fflush (stdout);
1898 /* Print a bunch of information about this and that. */
1900 static void
1901 print_data_base ()
1903 extern char *ctime ();
1904 time_t when;
1906 when = time ((time_t *) 0);
1907 printf ("\n# Make data base, printed on %s", ctime (&when));
1909 print_variable_data_base ();
1910 print_dir_data_base ();
1911 print_rule_data_base ();
1912 print_file_data_base ();
1913 print_vpath_data_base ();
1915 when = time ((time_t *) 0);
1916 printf ("\n# Finished Make data base on %s\n", ctime (&when));
1919 /* Exit with STATUS, cleaning up as necessary. */
1921 void
1922 die (status)
1923 int status;
1925 static char dying = 0;
1927 if (!dying)
1929 int err;
1931 dying = 1;
1933 /* Try to move back to the original directory. This is essential on
1934 MS-DOS (where there is really only one process), and on Unix it
1935 puts core files in the original directory instead of the -C
1936 directory. */
1937 if (directory_before_chdir != 0)
1938 chdir (directory_before_chdir);
1940 if (print_version_flag)
1941 print_version ();
1943 /* Wait for children to die. */
1944 for (err = status != 0; job_slots_used > 0; err = 0)
1945 reap_children (1, err);
1947 /* Remove the intermediate files. */
1948 remove_intermediates (0);
1950 if (print_data_base_flag)
1951 print_data_base ();
1953 if (print_directory_flag)
1954 log_working_directory (0);
1957 exit (status);
1960 /* Write a message indicating that we've just entered or
1961 left (according to ENTERING) the current directory. */
1963 static void
1964 log_working_directory (entering)
1965 int entering;
1967 static int entered = 0;
1968 char *message = entering ? "Entering" : "Leaving";
1970 if (entering)
1971 entered = 1;
1972 else if (!entered)
1973 /* Don't print the leaving message if we
1974 haven't printed the entering message. */
1975 return;
1977 if (print_data_base_flag)
1978 fputs ("# ", stdout);
1980 if (makelevel == 0)
1981 printf ("%s: %s ", program, message);
1982 else
1983 printf ("%s[%u]: %s ", program, makelevel, message);
1985 if (starting_directory == 0)
1986 puts ("an unknown directory");
1987 else
1988 printf ("directory `%s'\n", starting_directory);