Thu May 9 13:54:49 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[make.git] / main.c
blobd16e7f440ef66724ca068476f0fc83a0343938d3
1 /* Argument parsing and main program of GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 94, 1995, 1996 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). */
177 #ifndef NO_FLOAT
178 double max_load_average = -1.0;
179 double default_load_average = -1.0;
180 #else
181 int max_load_average = -1;
182 int default_load_average = -1;
183 #endif
185 /* List of directories given with -C switches. */
187 static struct stringlist *directories = 0;
189 /* List of include directories given with -I switches. */
191 static struct stringlist *include_directories = 0;
193 /* List of files given with -o switches. */
195 static struct stringlist *old_files = 0;
197 /* List of files given with -W switches. */
199 static struct stringlist *new_files = 0;
201 /* If nonzero, we should just print usage and exit. */
203 static int print_usage_flag = 0;
205 /* If nonzero, we should print a warning message
206 for each reference to an undefined variable. */
208 int warn_undefined_variables_flag;
210 /* The table of command switches. */
212 static const struct command_switch switches[] =
214 { 'b', ignore, 0, 0, 0, 0, 0, 0,
215 0, 0,
216 "Ignored for compatibility" },
217 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
218 "directory", "DIRECTORY",
219 "Change to DIRECTORY before doing anything" },
220 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
221 "debug", 0,
222 "Print lots of debugging information" },
223 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
224 "environment-overrides", 0,
225 "Environment variables override makefiles" },
226 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
227 "file", "FILE",
228 "Read FILE as a makefile" },
229 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
230 "help", 0,
231 "Print this message and exit" },
232 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
233 "ignore-errors", 0,
234 "Ignore errors from commands" },
235 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
236 "include-dir", "DIRECTORY",
237 "Search DIRECTORY for included makefiles" },
238 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
239 (char *) &inf_jobs, (char *) &default_job_slots,
240 "jobs", "N",
241 "Allow N jobs at once; infinite jobs with no arg" },
242 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
243 0, (char *) &default_keep_going_flag,
244 "keep-going", 0,
245 "Keep going when some targets can't be made" },
246 #ifndef NO_FLOAT
247 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
248 (char *) &default_load_average, (char *) &default_load_average,
249 "load-average", "N",
250 "Don't start multiple jobs unless load is below N" },
251 #else
252 { 'l', positive_int, (char *) &max_load_average, 1, 1, 0,
253 (char *) &default_load_average, (char *) &default_load_average,
254 "load-average", "N",
255 "Don't start multiple jobs unless load is below N" },
256 #endif
257 { 'm', ignore, 0, 0, 0, 0, 0, 0,
258 0, 0,
259 "-b" },
260 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
261 "just-print", 0,
262 "Don't actually run any commands; just print them" },
263 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
264 "old-file", "FILE",
265 "Consider FILE to be very old and don't remake it" },
266 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
267 "print-data-base", 0,
268 "Print make's internal database" },
269 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
270 "question", 0,
271 "Run no commands; exit status says if up to date" },
272 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
273 "no-builtin-rules", 0,
274 "Disable the built-in implicit rules" },
275 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
276 "silent", 0,
277 "Don't echo commands" },
278 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
279 0, (char *) &default_keep_going_flag,
280 "no-keep-going", 0,
281 "Turns off -k" },
282 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
283 "touch", 0,
284 "Touch targets instead of remaking them" },
285 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
286 "version", 0,
287 "Print the version number of make and exit" },
288 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
289 "print-directory", 0,
290 "Print the current directory" },
291 { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
292 "no-print-directory", 0,
293 "Turn off -w, even if it was turned on implicitly" },
294 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
295 "what-if", "FILE",
296 "Consider FILE to be infinitely new" },
297 { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
298 "warn-undefined-variables", 0,
299 "Warn when an undefined variable is referenced" },
300 { '\0', }
303 /* Secondary long names for options. */
305 static struct option long_option_aliases[] =
307 { "quiet", no_argument, 0, 's' },
308 { "stop", no_argument, 0, 'S' },
309 { "new-file", required_argument, 0, 'W' },
310 { "assume-new", required_argument, 0, 'W' },
311 { "assume-old", required_argument, 0, 'o' },
312 { "max-load", optional_argument, 0, 'l' },
313 { "dry-run", no_argument, 0, 'n' },
314 { "recon", no_argument, 0, 'n' },
315 { "makefile", required_argument, 0, 'f' },
318 /* The usage message prints the descriptions of options starting in
319 this column. Make sure it leaves enough room for the longest
320 description to fit in less than 80 characters. */
322 #define DESCRIPTION_COLUMN 30
324 /* List of goal targets. */
326 static struct dep *goals, *lastgoal;
328 /* List of variables which were defined on the command line
329 (or, equivalently, in MAKEFLAGS). */
331 struct command_variable
333 struct command_variable *next;
334 struct variable *variable;
336 static struct command_variable *command_variables;
338 /* The name we were invoked with. */
340 char *program;
342 /* Our current directory before processing any -C options. */
344 char *directory_before_chdir;
346 /* Our current directory after processing all -C options. */
348 char *starting_directory;
350 /* Value of the MAKELEVEL variable at startup (or 0). */
352 unsigned int makelevel;
354 /* First file defined in the makefile whose name does not
355 start with `.'. This is the default to remake if the
356 command line does not specify. */
358 struct file *default_goal_file;
360 /* Pointer to structure for the file .DEFAULT
361 whose commands are used for any file that has none of its own.
362 This is zero if the makefiles do not define .DEFAULT. */
364 struct file *default_file;
366 /* Nonzero if we have seen the magic `.POSIX' target.
367 This turns on pedantic compliance with POSIX.2. */
369 int posix_pedantic;
371 /* Mask of signals that are being caught with fatal_error_signal. */
373 #ifdef POSIX
374 sigset_t fatal_signal_set;
375 #else
376 #ifdef HAVE_SIGSETMASK
377 int fatal_signal_mask;
378 #endif
379 #endif
381 static struct file *
382 enter_command_line_file (name)
383 char *name;
385 if (name[0] == '\0')
386 fatal ("empty string invalid as file name");
388 if (name[0] == '~')
390 char *expanded = tilde_expand (name);
391 if (expanded != 0)
392 name = expanded; /* Memory leak; I don't care. */
395 /* This is also done in parse_file_seq, so this is redundant
396 for names read from makefiles. It is here for names passed
397 on the command line. */
398 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
400 name += 2;
401 while (*name == '/')
402 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
403 ++name;
406 if (*name == '\0')
408 /* It was all slashes! Move back to the dot and truncate
409 it after the first slash, so it becomes just "./". */
411 --name;
412 while (name[0] != '.');
413 name[2] = '\0';
416 return enter_file (savestring (name, strlen (name)));
419 /* Toggle -d on receipt of SIGUSR1. */
421 static RETSIGTYPE
422 debug_signal_handler (sig)
423 int sig;
425 debug_flag = ! debug_flag;
428 #ifndef _AMIGA
430 main (argc, argv, envp)
431 int argc;
432 char **argv;
433 char **envp;
434 #else
435 int main (int argc, char ** argv)
436 #endif
438 register struct file *f;
439 register unsigned int i;
440 char **p;
441 struct dep *read_makefiles;
442 PATH_VAR (current_directory);
444 default_goal_file = 0;
445 reading_filename = 0;
446 reading_lineno_ptr = 0;
448 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
449 signame_init ();
450 #endif
452 #ifdef POSIX
453 sigemptyset (&fatal_signal_set);
454 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
455 #else
456 #ifdef HAVE_SIGSETMASK
457 fatal_signal_mask = 0;
458 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
459 #else
460 #define ADD_SIG(sig)
461 #endif
462 #endif
464 #define FATAL_SIG(sig) \
465 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
466 (void) signal ((sig), SIG_IGN); \
467 else \
468 ADD_SIG (sig);
470 #ifdef SIGHUP
471 FATAL_SIG (SIGHUP);
472 #endif
473 #ifdef SIGQUIT
474 FATAL_SIG (SIGQUIT);
475 #endif
476 FATAL_SIG (SIGINT);
477 FATAL_SIG (SIGTERM);
479 #ifdef SIGDANGER
480 FATAL_SIG (SIGDANGER);
481 #endif
482 #ifdef SIGXCPU
483 FATAL_SIG (SIGXCPU);
484 #endif
485 #ifdef SIGXFSZ
486 FATAL_SIG (SIGXFSZ);
487 #endif
489 #undef FATAL_SIG
491 /* Make sure stdout is line-buffered. */
493 #ifdef HAVE_SETLINEBUF
494 setlinebuf (stdout);
495 #else
496 #ifndef SETVBUF_REVERSED
497 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
498 #else /* setvbuf not reversed. */
499 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
500 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
501 #endif /* setvbuf reversed. */
502 #endif /* setlinebuf missing. */
504 /* Figure out where this program lives. */
506 if (argv[0] == 0)
507 argv[0] = "";
508 if (argv[0][0] == '\0')
509 program = "make";
510 else
512 #ifdef VMS
513 program = rindex (argv[0], ']');
514 #else
515 program = rindex (argv[0], '/');
516 #endif
517 #ifdef __MSDOS__
518 if (program == 0)
519 program = rindex (argv[0], '\\');
520 if (program == 0)
521 program = rindex (argv[0], ':');
522 #endif
523 if (program == 0)
524 program = argv[0];
525 else
526 ++program;
529 /* Set up to access user data (files). */
530 user_access ();
532 /* Figure out where we are. */
534 if (getcwd (current_directory, GET_PATH_MAX) == 0)
536 #ifdef HAVE_GETCWD
537 perror_with_name ("getcwd: ", "");
538 #else
539 error ("getwd: %s", current_directory);
540 #endif
541 current_directory[0] = '\0';
542 directory_before_chdir = 0;
544 else
545 directory_before_chdir = savestring (current_directory,
546 strlen (current_directory));
548 /* Read in variables from the environment. It is important that this be
549 done before $(MAKE) is are figured out so its definitions will not be
550 one from the environment. */
552 #ifndef _AMIGA
553 for (i = 0; envp[i] != 0; ++i)
555 register char *ep = envp[i];
556 while (*ep != '=')
557 ++ep;
558 /* The result of pointer arithmetic is cast to unsigned int for
559 machines where ptrdiff_t is a different size that doesn't widen
560 the same. */
561 define_variable (envp[i], (unsigned int) (ep - envp[i]),
562 ep + 1, o_env, 1)
563 /* Force exportation of every variable culled from the environment.
564 We used to rely on target_environment's v_default code to do this.
565 But that does not work for the case where an environment variable
566 is redefined in a makefile with `override'; it should then still
567 be exported, because it was originally in the environment. */
568 ->export = v_export;
570 #else /* For Amiga, read the ENV: device, ignoring all dirs */
572 BPTR env, file, old;
573 char buffer[1024];
574 int len;
575 __aligned struct FileInfoBlock fib;
577 env = Lock ("ENV:", ACCESS_READ);
578 if (env)
580 old = CurrentDir (DupLock(env));
581 Examine (env, &fib);
583 while (ExNext (env, &fib))
585 if (fib.fib_DirEntryType < 0) /* File */
587 file = Open (fib.fib_FileName, MODE_OLDFILE);
589 if (file)
591 len = Read (file, buffer, sizeof (buffer)-1);
592 buffer[len] = 0;
594 define_variable (fib.fib_FileName,
595 strlen (fib.fib_FileName),
596 buffer, o_env, 1)->export = v_export;
600 UnLock (env);
601 UnLock(CurrentDir(old));
604 #endif
606 /* Decode the switches. */
608 decode_env_switches ("MAKEFLAGS", 9);
609 #if 0
610 /* People write things like:
611 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
612 and we set the -p, -i and -e switches. Doesn't seem quite right. */
613 decode_env_switches ("MFLAGS", 6);
614 #endif
615 decode_switches (argc, argv, 0);
617 /* Print version information. */
619 if (print_version_flag || print_data_base_flag || debug_flag)
620 print_version ();
622 /* `make --version' is supposed to just print the version and exit. */
623 if (print_version_flag)
624 die (0);
626 #if !defined(__MSDOS__) && !defined(VMS)
627 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
628 (If it is a relative pathname with a slash, prepend our directory name
629 so the result will run the same program regardless of the current dir.
630 If it is a name with no slash, we can only hope that PATH did not
631 find it in the current directory.) */
633 if (current_directory[0] != '\0'
634 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
635 argv[0] = concat (current_directory, "/", argv[0]);
636 #endif
638 /* The extra indirection through $(MAKE_COMMAND) is done
639 for hysterical raisins. */
640 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
641 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
643 if (command_variables != 0)
645 struct command_variable *cv;
646 struct variable *v;
647 unsigned int len = 0;
648 char *value, *p;
650 /* Figure out how much space will be taken up by the command-line
651 variable definitions. */
652 for (cv = command_variables; cv != 0; cv = cv->next)
654 v = cv->variable;
655 len += 2 * strlen (v->name);
656 if (! v->recursive)
657 ++len;
658 ++len;
659 len += 2 * strlen (v->value);
662 /* Now allocate a buffer big enough and fill it. */
663 p = value = (char *) alloca (len);
664 for (cv = command_variables; cv != 0; cv = cv->next)
666 v = cv->variable;
667 p = quote_as_word (p, v->name, 0);
668 if (! v->recursive)
669 *p++ = ':';
670 *p++ = '=';
671 p = quote_as_word (p, v->value, 0);
672 *p++ = ' ';
674 p[-1] = '\0'; /* Kill the final space and terminate. */
676 /* Define an unchangeable variable with a name that no POSIX.2
677 makefile could validly use for its own variable. */
678 (void) define_variable ("-*-command-variables-*-", 23,
679 value, o_automatic, 0);
681 /* Define the variable; this will not override any user definition.
682 Normally a reference to this variable is written into the value of
683 MAKEFLAGS, allowing the user to override this value to affect the
684 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
685 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
686 a reference to this hidden variable is written instead. */
687 (void) define_variable ("MAKEOVERRIDES", 13,
688 "${-*-command-variables-*-}", o_env, 1);
691 /* If there were -C flags, move ourselves about. */
692 if (directories != 0)
693 for (i = 0; directories->list[i] != 0; ++i)
695 char *dir = directories->list[i];
696 if (dir[0] == '~')
698 char *expanded = tilde_expand (dir);
699 if (expanded != 0)
700 dir = expanded;
702 if (chdir (dir) < 0)
703 pfatal_with_name (dir);
704 if (dir != directories->list[i])
705 free (dir);
708 /* Figure out the level of recursion. */
710 struct variable *v = lookup_variable ("MAKELEVEL", 9);
711 if (v != 0 && *v->value != '\0' && *v->value != '-')
712 makelevel = (unsigned int) atoi (v->value);
713 else
714 makelevel = 0;
717 /* Except under -s, always do -w in sub-makes and under -C. */
718 if (!silent_flag && (directories != 0 || makelevel > 0))
719 print_directory_flag = 1;
721 /* Let the user disable that with --no-print-directory. */
722 if (inhibit_print_directory_flag)
723 print_directory_flag = 0;
725 /* Construct the list of include directories to search. */
727 construct_include_path (include_directories == 0 ? (char **) 0
728 : include_directories->list);
730 /* Figure out where we are now, after chdir'ing. */
731 if (directories == 0)
732 /* We didn't move, so we're still in the same place. */
733 starting_directory = current_directory;
734 else
736 if (getcwd (current_directory, GET_PATH_MAX) == 0)
738 #ifdef HAVE_GETCWD
739 perror_with_name ("getcwd: ", "");
740 #else
741 error ("getwd: %s", current_directory);
742 #endif
743 starting_directory = 0;
745 else
746 starting_directory = current_directory;
750 /* Read any stdin makefiles into temporary files. */
752 if (makefiles != 0)
754 register unsigned int i;
755 for (i = 0; i < makefiles->idx; ++i)
756 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
758 /* This makefile is standard input. Since we may re-exec
759 and thus re-read the makefiles, we read standard input
760 into a temporary file and read from that. */
761 FILE *outfile;
763 /* Make a unique filename. */
764 #ifdef HAVE_MKTEMP
766 #ifdef VMS
767 static char name[] = "sys$scratch:GmXXXXXX";
768 #else
769 static char name[] = "/tmp/GmXXXXXX";
770 #endif
771 (void) mktemp (name);
772 #else
773 static char name[L_tmpnam];
774 (void) tmpnam (name);
775 #endif
777 outfile = fopen (name, "w");
778 if (outfile == 0)
779 pfatal_with_name ("fopen (temporary file)");
780 while (!feof (stdin))
782 char buf[2048];
783 unsigned int n = fread (buf, 1, sizeof(buf), stdin);
784 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
785 pfatal_with_name ("fwrite (temporary file)");
787 /* Try to make sure we won't remake the temporary
788 file when we are re-exec'd. Kludge-o-matic! */
789 fprintf (outfile, "%s:;\n", name);
790 (void) fclose (outfile);
792 /* Replace the name that read_all_makefiles will
793 see with the name of the temporary file. */
795 char *temp;
796 /* SGI compiler requires alloca's result be assigned simply. */
797 temp = (char *) alloca (sizeof (name));
798 bcopy (name, temp, sizeof (name));
799 makefiles->list[i] = temp;
802 /* Make sure the temporary file will not be remade. */
803 f = enter_file (savestring (name, sizeof name - 1));
804 f->updated = 1;
805 f->update_status = 0;
806 f->command_state = cs_finished;
807 /* Let it be removed when we're done. */
808 f->intermediate = 1;
809 /* But don't mention it. */
810 f->dontcare = 1;
814 /* Set up to handle children dying. This must be done before
815 reading in the makefiles so that `shell' function calls will work. */
817 #ifdef SIGCHLD
818 (void) signal (SIGCHLD, child_handler);
819 #endif
820 #ifdef SIGCLD
821 (void) signal (SIGCLD, child_handler);
822 #endif
824 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
825 #ifdef SIGUSR1
826 (void) signal (SIGUSR1, debug_signal_handler);
827 #endif
829 /* Define the initial list of suffixes for old-style rules. */
831 set_default_suffixes ();
833 /* Define the file rules for the built-in suffix rules. These will later
834 be converted into pattern rules. We used to do this in
835 install_default_implicit_rules, but since that happens after reading
836 makefiles, it results in the built-in pattern rules taking precedence
837 over makefile-specified suffix rules, which is wrong. */
839 install_default_suffix_rules ();
841 /* Define some internal and special variables. */
843 define_automatic_variables ();
845 /* Set up the MAKEFLAGS and MFLAGS variables
846 so makefiles can look at them. */
848 define_makeflags (0, 0);
850 /* Define the default variables. */
851 define_default_variables ();
853 /* Read all the makefiles. */
855 default_file = enter_file (".DEFAULT");
857 read_makefiles
858 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
860 /* Decode switches again, in case the variables were set by the makefile. */
861 decode_env_switches ("MAKEFLAGS", 9);
862 #if 0
863 decode_env_switches ("MFLAGS", 6);
864 #endif
866 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
868 define_makeflags (1, 0);
870 /* Make each `struct dep' point at the `struct file' for the file
871 depended on. Also do magic for special targets. */
873 snap_deps ();
875 /* Convert old-style suffix rules to pattern rules. It is important to
876 do this before installing the built-in pattern rules below, so that
877 makefile-specified suffix rules take precedence over built-in pattern
878 rules. */
880 convert_to_pattern ();
882 /* Install the default implicit pattern rules.
883 This used to be done before reading the makefiles.
884 But in that case, built-in pattern rules were in the chain
885 before user-defined ones, so they matched first. */
887 install_default_implicit_rules ();
889 /* Compute implicit rule limits. */
891 count_implicit_rule_limits ();
893 /* Construct the listings of directories in VPATH lists. */
895 build_vpath_lists ();
897 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
898 and as having been updated already, and files given with -W flags as
899 brand new (time-stamp as far as possible into the future). */
901 if (old_files != 0)
902 for (p = old_files->list; *p != 0; ++p)
904 f = enter_command_line_file (*p);
905 f->last_mtime = (time_t) 1;
906 f->updated = 1;
907 f->update_status = 0;
908 f->command_state = cs_finished;
911 if (new_files != 0)
913 for (p = new_files->list; *p != 0; ++p)
915 f = enter_command_line_file (*p);
916 f->last_mtime = NEW_MTIME;
920 if (read_makefiles != 0)
922 /* Update any makefiles if necessary. */
924 time_t *makefile_mtimes = 0;
925 unsigned int mm_idx = 0;
927 if (debug_flag)
928 puts ("Updating makefiles....");
930 /* Remove any makefiles we don't want to try to update.
931 Also record the current modtimes so we can compare them later. */
933 register struct dep *d, *last;
934 last = 0;
935 d = read_makefiles;
936 while (d != 0)
938 register struct file *f = d->file;
939 if (f->double_colon)
940 for (f = f->double_colon; f != NULL; f = f->prev)
942 if (f->deps == 0 && f->cmds != 0)
944 /* This makefile is a :: target with commands, but
945 no dependencies. So, it will always be remade.
946 This might well cause an infinite loop, so don't
947 try to remake it. (This will only happen if
948 your makefiles are written exceptionally
949 stupidly; but if you work for Athena, that's how
950 you write your makefiles.) */
952 if (debug_flag)
953 printf ("Makefile `%s' might loop; not remaking it.\n",
954 f->name);
956 if (last == 0)
957 read_makefiles = d->next;
958 else
959 last->next = d->next;
961 /* Free the storage. */
962 free ((char *) d);
964 d = last == 0 ? 0 : last->next;
966 break;
969 if (f == NULL || !f->double_colon)
971 if (makefile_mtimes == 0)
972 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
973 else
974 makefile_mtimes = (time_t *)
975 xrealloc ((char *) makefile_mtimes,
976 (mm_idx + 1) * sizeof (time_t));
977 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
978 last = d;
979 d = d->next;
984 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
985 define_makeflags (1, 1);
987 switch (update_goal_chain (read_makefiles, 1))
989 case 1:
990 default:
991 #define BOGUS_UPDATE_STATUS 0
992 assert (BOGUS_UPDATE_STATUS);
993 break;
995 case -1:
996 /* Did nothing. */
997 break;
999 case 2:
1000 /* Failed to update. Figure out if we care. */
1002 /* Nonzero if any makefile was successfully remade. */
1003 int any_remade = 0;
1004 /* Nonzero if any makefile we care about failed
1005 in updating or could not be found at all. */
1006 int any_failed = 0;
1007 register unsigned int i;
1009 for (i = 0; read_makefiles != 0; ++i)
1011 struct dep *d = read_makefiles;
1012 read_makefiles = d->next;
1013 if (d->file->updated)
1015 /* This makefile was updated. */
1016 if (d->file->update_status == 0)
1018 /* It was successfully updated. */
1019 any_remade |= (file_mtime_no_search (d->file)
1020 != makefile_mtimes[i]);
1022 else if (! (d->changed & RM_DONTCARE))
1024 time_t mtime;
1025 /* The update failed and this makefile was not
1026 from the MAKEFILES variable, so we care. */
1027 error ("Failed to remake makefile `%s'.",
1028 d->file->name);
1029 mtime = file_mtime_no_search (d->file);
1030 any_remade |= (mtime != (time_t) -1
1031 && mtime != makefile_mtimes[i]);
1034 else
1035 /* This makefile was not found at all. */
1036 if (! (d->changed & RM_DONTCARE))
1038 /* This is a makefile we care about. See how much. */
1039 if (d->changed & RM_INCLUDED)
1040 /* An included makefile. We don't need
1041 to die, but we do want to complain. */
1042 error ("Included makefile `%s' was not found.",
1043 dep_name (d));
1044 else
1046 /* A normal makefile. We must die later. */
1047 error ("Makefile `%s' was not found", dep_name (d));
1048 any_failed = 1;
1052 free ((char *) d);
1055 if (any_remade)
1056 goto re_exec;
1057 else if (any_failed)
1058 die (2);
1059 else
1060 break;
1063 case 0:
1064 re_exec:
1065 /* Updated successfully. Re-exec ourselves. */
1067 remove_intermediates (0);
1069 if (print_data_base_flag)
1070 print_data_base ();
1072 log_working_directory (0);
1074 if (makefiles != 0)
1076 /* These names might have changed. */
1077 register unsigned int i, j = 0;
1078 for (i = 1; i < argc; ++i)
1079 if (!strcmp (argv[i], "-f")) /* XXX */
1081 char *p = &argv[i][2];
1082 if (*p == '\0')
1083 argv[++i] = makefiles->list[j];
1084 else
1085 argv[i] = concat ("-f", makefiles->list[j], "");
1086 ++j;
1090 if (directories != 0 && directories->idx > 0)
1092 char bad;
1093 if (directory_before_chdir != 0)
1095 if (chdir (directory_before_chdir) < 0)
1097 perror_with_name ("chdir", "");
1098 bad = 1;
1100 else
1101 bad = 0;
1103 else
1104 bad = 1;
1105 if (bad)
1106 fatal ("Couldn't change back to original directory.");
1109 #ifndef _AMIGA
1110 for (p = environ; *p != 0; ++p)
1111 if (!strncmp (*p, "MAKELEVEL=", 10))
1113 /* The SGI compiler apparently can't understand
1114 the concept of storing the result of a function
1115 in something other than a local variable. */
1116 char *sgi_loses;
1117 sgi_loses = (char *) alloca (40);
1118 *p = sgi_loses;
1119 sprintf (*p, "MAKELEVEL=%u", makelevel);
1120 break;
1122 #else /* AMIGA */
1123 # include <dos/dos.h>
1124 # include <proto/dos.h>
1126 char buffer[256];
1127 int len;
1129 sprintf (buffer, "%u", makelevel);
1130 SetVar ("MAKELEVEL", buffer, -1, GVF_LOCAL_ONLY);
1132 #endif
1134 if (debug_flag)
1136 char **p;
1137 fputs ("Re-executing:", stdout);
1138 for (p = argv; *p != 0; ++p)
1139 printf (" %s", *p);
1140 puts ("");
1143 fflush (stdout);
1144 fflush (stderr);
1146 #ifndef _AMIGA
1147 exec_command (argv, environ);
1148 #else
1149 exec_command (argv);
1150 exit (0);
1151 #endif
1152 /* NOTREACHED */
1156 /* Set up `MAKEFLAGS' again for the normal targets. */
1157 define_makeflags (1, 0);
1160 int status;
1162 /* If there were no command-line goals, use the default. */
1163 if (goals == 0)
1165 if (default_goal_file != 0)
1167 goals = (struct dep *) xmalloc (sizeof (struct dep));
1168 goals->next = 0;
1169 goals->name = 0;
1170 goals->file = default_goal_file;
1173 else
1174 lastgoal->next = 0;
1176 if (goals != 0)
1178 /* Update the goals. */
1180 if (debug_flag)
1181 puts ("Updating goal targets....");
1183 switch (update_goal_chain (goals, 0))
1185 case -1:
1186 /* Nothing happened. */
1187 case 0:
1188 /* Updated successfully. */
1189 status = EXIT_SUCCESS;
1190 break;
1191 case 2:
1192 /* Updating failed. */
1193 status = EXIT_FAILURE;
1194 break;
1195 case 1:
1196 /* We are under -q and would run some commands. */
1197 status = EXIT_FAILURE;
1198 break;
1199 default:
1200 abort ();
1203 else
1205 if (read_makefiles == 0)
1206 fatal ("No targets specified and no makefile found");
1207 else
1208 fatal ("No targets");
1211 /* Exit. */
1212 die (status);
1215 return 0;
1218 /* Parsing of arguments, decoding of switches. */
1220 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1221 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1222 (sizeof (long_option_aliases) /
1223 sizeof (long_option_aliases[0]))];
1225 /* Fill in the string and vector for getopt. */
1226 static void
1227 init_switches ()
1229 register char *p;
1230 register int c;
1231 register unsigned int i;
1233 if (options[0] != '\0')
1234 /* Already done. */
1235 return;
1237 p = options;
1239 /* Return switch and non-switch args in order, regardless of
1240 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1241 *p++ = '-';
1243 for (i = 0; switches[i].c != '\0'; ++i)
1245 long_options[i].name = (switches[i].long_name == 0 ? "" :
1246 switches[i].long_name);
1247 long_options[i].flag = 0;
1248 long_options[i].val = switches[i].c;
1249 if (isalnum (switches[i].c))
1250 *p++ = switches[i].c;
1251 switch (switches[i].type)
1253 case flag:
1254 case flag_off:
1255 case ignore:
1256 long_options[i].has_arg = no_argument;
1257 break;
1259 case string:
1260 case positive_int:
1261 case floating:
1262 if (isalnum (switches[i].c))
1263 *p++ = ':';
1264 if (switches[i].noarg_value != 0)
1266 if (isalnum (switches[i].c))
1267 *p++ = ':';
1268 long_options[i].has_arg = optional_argument;
1270 else
1271 long_options[i].has_arg = required_argument;
1272 break;
1275 *p = '\0';
1276 for (c = 0; c < (sizeof (long_option_aliases) /
1277 sizeof (long_option_aliases[0]));
1278 ++c)
1279 long_options[i++] = long_option_aliases[c];
1280 long_options[i].name = 0;
1283 static void
1284 handle_non_switch_argument (arg, env)
1285 char *arg;
1286 int env;
1288 /* Non-option argument. It might be a variable definition. */
1289 struct variable *v;
1290 if (arg[0] == '-' && arg[1] == '\0')
1291 /* Ignore plain `-' for compatibility. */
1292 return;
1293 v = try_variable_definition ((char *) 0, 0, arg, o_command);
1294 if (v != 0)
1296 /* It is indeed a variable definition. Record a pointer to
1297 the variable for later use in define_makeflags. */
1298 struct command_variable *cv
1299 = (struct command_variable *) xmalloc (sizeof (*cv));
1300 cv->variable = v;
1301 cv->next = command_variables;
1302 command_variables = cv;
1304 else if (! env)
1306 /* Not an option or variable definition; it must be a goal
1307 target! Enter it as a file and add it to the dep chain of
1308 goals. */
1309 struct file *f = enter_command_line_file (arg);
1310 f->cmd_target = 1;
1312 if (goals == 0)
1314 goals = (struct dep *) xmalloc (sizeof (struct dep));
1315 lastgoal = goals;
1317 else
1319 lastgoal->next
1320 = (struct dep *) xmalloc (sizeof (struct dep));
1321 lastgoal = lastgoal->next;
1323 lastgoal->name = 0;
1324 lastgoal->file = f;
1328 /* Decode switches from ARGC and ARGV.
1329 They came from the environment if ENV is nonzero. */
1331 static void
1332 decode_switches (argc, argv, env)
1333 int argc;
1334 char **argv;
1335 int env;
1337 int bad = 0;
1338 register const struct command_switch *cs;
1339 register struct stringlist *sl;
1340 register int c;
1342 /* getopt does most of the parsing for us.
1343 First, get its vectors set up. */
1345 init_switches ();
1347 /* Let getopt produce error messages for the command line,
1348 but not for options from the environment. */
1349 opterr = !env;
1350 /* Reset getopt's state. */
1351 optind = 0;
1353 while (optind < argc)
1355 /* Parse the next argument. */
1356 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1357 if (c == EOF)
1358 /* End of arguments, or "--" marker seen. */
1359 break;
1360 else if (c == 1)
1361 /* An argument not starting with a dash. */
1362 handle_non_switch_argument (optarg, env);
1363 else if (c == '?')
1364 /* Bad option. We will print a usage message and die later.
1365 But continue to parse the other options so the user can
1366 see all he did wrong. */
1367 bad = 1;
1368 else
1369 for (cs = switches; cs->c != '\0'; ++cs)
1370 if (cs->c == c)
1372 /* Whether or not we will actually do anything with
1373 this switch. We test this individually inside the
1374 switch below rather than just once outside it, so that
1375 options which are to be ignored still consume args. */
1376 int doit = !env || cs->env;
1378 switch (cs->type)
1380 default:
1381 abort ();
1383 case ignore:
1384 break;
1386 case flag:
1387 case flag_off:
1388 if (doit)
1389 *(int *) cs->value_ptr = cs->type == flag;
1390 break;
1392 case string:
1393 if (!doit)
1394 break;
1396 if (optarg == 0)
1397 optarg = cs->noarg_value;
1399 sl = *(struct stringlist **) cs->value_ptr;
1400 if (sl == 0)
1402 sl = (struct stringlist *)
1403 xmalloc (sizeof (struct stringlist));
1404 sl->max = 5;
1405 sl->idx = 0;
1406 sl->list = (char **) xmalloc (5 * sizeof (char *));
1407 *(struct stringlist **) cs->value_ptr = sl;
1409 else if (sl->idx == sl->max - 1)
1411 sl->max += 5;
1412 sl->list = (char **)
1413 xrealloc ((char *) sl->list,
1414 sl->max * sizeof (char *));
1416 sl->list[sl->idx++] = optarg;
1417 sl->list[sl->idx] = 0;
1418 break;
1420 case positive_int:
1421 if (optarg == 0 && argc > optind
1422 && isdigit (argv[optind][0]))
1423 optarg = argv[optind++];
1425 if (!doit)
1426 break;
1428 if (optarg != 0)
1430 int i = atoi (optarg);
1431 if (i < 1)
1433 if (doit)
1434 error ("the `-%c' option requires a \
1435 positive integral argument",
1436 cs->c);
1437 bad = 1;
1439 else
1440 *(unsigned int *) cs->value_ptr = i;
1442 else
1443 *(unsigned int *) cs->value_ptr
1444 = *(unsigned int *) cs->noarg_value;
1445 break;
1447 #ifndef NO_FLOAT
1448 case floating:
1449 if (optarg == 0 && optind < argc
1450 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1451 optarg = argv[optind++];
1453 if (doit)
1454 *(double *) cs->value_ptr
1455 = (optarg != 0 ? atof (optarg)
1456 : *(double *) cs->noarg_value);
1458 break;
1459 #endif
1462 /* We've found the switch. Stop looking. */
1463 break;
1467 /* There are no more options according to getting getopt, but there may
1468 be some arguments left. Since we have asked for non-option arguments
1469 to be returned in order, this only happens when there is a "--"
1470 argument to prevent later arguments from being options. */
1471 while (optind < argc)
1472 handle_non_switch_argument (argv[optind++], env);
1475 if (!env && (bad || print_usage_flag))
1477 /* Print a nice usage message. */
1478 FILE *usageto;
1480 if (print_version_flag)
1481 print_version ();
1483 usageto = bad ? stderr : stdout;
1485 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1487 fputs ("Options:\n", usageto);
1488 for (cs = switches; cs->c != '\0'; ++cs)
1490 char buf[1024], shortarg[50], longarg[50], *p;
1492 if (cs->description[0] == '-')
1493 continue;
1495 switch (long_options[cs - switches].has_arg)
1497 case no_argument:
1498 shortarg[0] = longarg[0] = '\0';
1499 break;
1500 case required_argument:
1501 sprintf (longarg, "=%s", cs->argdesc);
1502 sprintf (shortarg, " %s", cs->argdesc);
1503 break;
1504 case optional_argument:
1505 sprintf (longarg, "[=%s]", cs->argdesc);
1506 sprintf (shortarg, " [%s]", cs->argdesc);
1507 break;
1510 p = buf;
1512 if (isalnum (cs->c))
1514 sprintf (buf, " -%c%s", cs->c, shortarg);
1515 p += strlen (p);
1517 if (cs->long_name != 0)
1519 unsigned int i;
1520 sprintf (p, "%s--%s%s",
1521 !isalnum (cs->c) ? " " : ", ",
1522 cs->long_name, longarg);
1523 p += strlen (p);
1524 for (i = 0; i < (sizeof (long_option_aliases) /
1525 sizeof (long_option_aliases[0]));
1526 ++i)
1527 if (long_option_aliases[i].val == cs->c)
1529 sprintf (p, ", --%s%s",
1530 long_option_aliases[i].name, longarg);
1531 p += strlen (p);
1535 const struct command_switch *ncs = cs;
1536 while ((++ncs)->c != '\0')
1537 if (ncs->description[0] == '-' &&
1538 ncs->description[1] == cs->c)
1540 /* This is another switch that does the same
1541 one as the one we are processing. We want
1542 to list them all together on one line. */
1543 sprintf (p, ", -%c%s", ncs->c, shortarg);
1544 p += strlen (p);
1545 if (ncs->long_name != 0)
1547 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1548 p += strlen (p);
1553 if (p - buf > DESCRIPTION_COLUMN - 2)
1554 /* The list of option names is too long to fit on the same
1555 line with the description, leaving at least two spaces.
1556 Print it on its own line instead. */
1558 fprintf (usageto, "%s\n", buf);
1559 buf[0] = '\0';
1562 fprintf (usageto, "%*s%s.\n",
1563 - DESCRIPTION_COLUMN,
1564 buf, cs->description);
1567 die (bad ? 2 : 0);
1571 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1572 We do this by chopping the value into a vector of words, prepending a
1573 dash to the first word if it lacks one, and passing the vector to
1574 decode_switches. */
1576 static void
1577 decode_env_switches (envar, len)
1578 char *envar;
1579 unsigned int len;
1581 char *varref = (char *) alloca (2 + len + 2);
1582 char *value, *p;
1583 int argc;
1584 char **argv;
1586 /* Get the variable's value. */
1587 varref[0] = '$';
1588 varref[1] = '(';
1589 bcopy (envar, &varref[2], len);
1590 varref[2 + len] = ')';
1591 varref[2 + len + 1] = '\0';
1592 value = variable_expand (varref);
1594 /* Skip whitespace, and check for an empty value. */
1595 value = next_token (value);
1596 len = strlen (value);
1597 if (len == 0)
1598 return;
1600 /* Allocate a vector that is definitely big enough. */
1601 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1603 /* Allocate a buffer to copy the value into while we split it into words
1604 and unquote it. We must use permanent storage for this because
1605 decode_switches may store pointers into the passed argument words. */
1606 p = (char *) xmalloc (2 * len);
1608 /* getopt will look at the arguments starting at ARGV[1].
1609 Prepend a spacer word. */
1610 argv[0] = 0;
1611 argc = 1;
1612 argv[argc] = p;
1613 while (*value != '\0')
1615 if (*value == '\\')
1616 ++value; /* Skip the backslash. */
1617 else if (isblank (*value))
1619 /* End of the word. */
1620 *p++ = '\0';
1621 argv[++argc] = p;
1623 ++value;
1624 while (isblank (*value));
1625 continue;
1627 *p++ = *value++;
1629 *p = '\0';
1630 argv[++argc] = 0;
1632 if (argc == 2 && argv[1][0] != '-')
1634 /* There is just one word in the value, and it is not a switch.
1635 Either this is the single-word form and we should prepend a dash
1636 before calling decode_switches, or this is the multi-word form and
1637 there is no dash because it is a variable definition. */
1638 struct variable *v;
1639 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1640 if (v != 0)
1642 /* It was indeed a variable definition, and now it has been
1643 processed. There is nothing for decode_switches to do.
1644 Record a pointer to the variable for later use in
1645 define_makeflags. */
1646 struct command_variable *cv
1647 = (struct command_variable *) xmalloc (sizeof (*cv));
1648 cv->variable = v;
1649 cv->next = command_variables;
1650 command_variables = cv;
1651 return;
1654 /* It wasn't a variable definition, so it's some switches without a
1655 leading dash. Add one and pass it along to decode_switches. We
1656 need permanent storage for this in case decode_switches saves
1657 pointers into the value. */
1658 argv[1] = concat ("-", argv[1], "");
1661 /* Parse those words. */
1662 decode_switches (argc, argv, 1);
1665 /* Quote the string IN so that it will be interpreted as a single word with
1666 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1667 signs to avoid variable expansion in make itself. Write the result into
1668 OUT, returning the address of the next character to be written.
1669 Allocating space for OUT twice the length of IN (thrice if
1670 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1672 static char *
1673 quote_as_word (out, in, double_dollars)
1674 char *out, *in;
1675 int double_dollars;
1677 while (*in != '\0')
1679 #ifdef VMS
1680 if (index ("^;'\"*?$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1681 #else
1682 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1683 #endif
1684 *out++ = '\\';
1685 if (double_dollars && *in == '$')
1686 *out++ = '$';
1687 *out++ = *in++;
1690 return out;
1693 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1694 command switches. Include options with args if ALL is nonzero.
1695 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1697 static void
1698 define_makeflags (all, makefile)
1699 int all, makefile;
1701 static const char ref[] = "$(MAKEOVERRIDES)";
1702 static const char posixref[] = "$(-*-command-variables-*-)";
1703 register const struct command_switch *cs;
1704 char *flagstring;
1705 register char *p;
1706 unsigned int words;
1707 struct variable *v;
1709 /* We will construct a linked list of `struct flag's describing
1710 all the flags which need to go in MAKEFLAGS. Then, once we
1711 know how many there are and their lengths, we can put them all
1712 together in a string. */
1714 struct flag
1716 struct flag *next;
1717 const struct command_switch *cs;
1718 char *arg;
1720 struct flag *flags = 0;
1721 unsigned int flagslen = 0;
1722 #define ADD_FLAG(ARG, LEN) \
1723 do { \
1724 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1725 new->cs = cs; \
1726 new->arg = (ARG); \
1727 new->next = flags; \
1728 flags = new; \
1729 if (new->arg == 0) \
1730 ++flagslen; /* Just a single flag letter. */ \
1731 else \
1732 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1733 if (!isalnum (cs->c)) \
1734 /* This switch has no single-letter version, so we use the long. */ \
1735 flagslen += 2 + strlen (cs->long_name); \
1736 } while (0)
1738 for (cs = switches; cs->c != '\0'; ++cs)
1739 if (cs->toenv && (!makefile || !cs->no_makefile))
1740 switch (cs->type)
1742 default:
1743 abort ();
1745 case ignore:
1746 break;
1748 case flag:
1749 case flag_off:
1750 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1751 && (cs->default_value == 0
1752 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1753 ADD_FLAG (0, 0);
1754 break;
1756 case positive_int:
1757 if (all)
1759 if ((cs->default_value != 0
1760 && (*(unsigned int *) cs->value_ptr
1761 == *(unsigned int *) cs->default_value)))
1762 break;
1763 else if (cs->noarg_value != 0
1764 && (*(unsigned int *) cs->value_ptr ==
1765 *(unsigned int *) cs->noarg_value))
1766 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1767 else if (cs->c == 'j')
1768 /* Special case for `-j'. */
1769 ADD_FLAG ("1", 1);
1770 else
1772 char *buf = (char *) alloca (30);
1773 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1774 ADD_FLAG (buf, strlen (buf));
1777 break;
1779 #ifndef NO_FLOAT
1780 case floating:
1781 if (all)
1783 if (cs->default_value != 0
1784 && (*(double *) cs->value_ptr
1785 == *(double *) cs->default_value))
1786 break;
1787 else if (cs->noarg_value != 0
1788 && (*(double *) cs->value_ptr
1789 == *(double *) cs->noarg_value))
1790 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1791 else
1793 char *buf = (char *) alloca (100);
1794 sprintf (buf, "%g", *(double *) cs->value_ptr);
1795 ADD_FLAG (buf, strlen (buf));
1798 break;
1799 #endif
1801 case string:
1802 if (all)
1804 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1805 if (sl != 0)
1807 /* Add the elements in reverse order, because
1808 all the flags get reversed below; and the order
1809 matters for some switches (like -I). */
1810 register unsigned int i = sl->idx;
1811 while (i-- > 0)
1812 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1815 break;
1818 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1820 #undef ADD_FLAG
1822 /* Construct the value in FLAGSTRING.
1823 We allocate enough space for a preceding dash and trailing null. */
1824 flagstring = (char *) alloca (1 + flagslen + 1);
1825 p = flagstring;
1826 words = 1;
1827 *p++ = '-';
1828 while (flags != 0)
1830 /* Add the flag letter or name to the string. */
1831 if (!isalnum (flags->cs->c))
1833 *p++ = '-';
1834 strcpy (p, flags->cs->long_name);
1835 p += strlen (p);
1837 else
1838 *p++ = flags->cs->c;
1839 if (flags->arg != 0)
1841 /* A flag that takes an optional argument which in this case is
1842 omitted is specified by ARG being "". We must distinguish
1843 because a following flag appended without an intervening " -"
1844 is considered the arg for the first. */
1845 if (flags->arg[0] != '\0')
1847 /* Add its argument too. */
1848 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1849 p = quote_as_word (p, flags->arg, 1);
1851 ++words;
1852 /* Write a following space and dash, for the next flag. */
1853 *p++ = ' ';
1854 *p++ = '-';
1856 else if (!isalnum (flags->cs->c))
1858 ++words;
1859 /* Long options must each go in their own word,
1860 so we write the following space and dash. */
1861 *p++ = ' ';
1862 *p++ = '-';
1864 flags = flags->next;
1868 /* Define MFLAGS before appending variable definitions. */
1870 if (p == &flagstring[1])
1871 /* No flags. */
1872 flagstring[0] = '\0';
1873 else if (p[-1] == '-')
1874 /* Kill the final space and dash. */
1875 p[-2] = '\0';
1876 else
1877 /* Terminate the string. */
1878 *p = '\0';
1880 /* Since MFLAGS is not parsed for flags, there is no reason to
1881 override any makefile redefinition. */
1882 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1885 if (all && command_variables != 0)
1887 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1888 command-line variable definitions. */
1890 if (p == &flagstring[1])
1891 /* No flags written, so elide the leading dash already written. */
1892 p = flagstring;
1893 else
1895 /* Separate the variables from the switches with a "--" arg. */
1896 if (p[-1] != '-')
1898 /* We did not already write a trailing " -". */
1899 *p++ = ' ';
1900 *p++ = '-';
1902 /* There is a trailing " -"; fill it out to " -- ". */
1903 *p++ = '-';
1904 *p++ = ' ';
1907 /* Copy in the string. */
1908 if (posix_pedantic)
1910 bcopy (posixref, p, sizeof posixref - 1);
1911 p += sizeof posixref - 1;
1913 else
1915 bcopy (ref, p, sizeof ref - 1);
1916 p += sizeof ref - 1;
1919 else if (p == &flagstring[1])
1921 words = 0;
1922 --p;
1924 else if (p[-1] == '-')
1925 /* Kill the final space and dash. */
1926 p -= 2;
1927 /* Terminate the string. */
1928 *p = '\0';
1930 v = define_variable ("MAKEFLAGS", 9,
1931 /* If there is just a single word of switches,
1932 omit the leading dash unless it is a single
1933 long option with two leading dashes. */
1934 &flagstring[(words == 1 && command_variables == 0
1935 && flagstring[1] != '-')
1936 ? 1 : 0],
1937 /* This used to use o_env, but that lost when a
1938 makefile defined MAKEFLAGS. Makefiles set
1939 MAKEFLAGS to add switches, but we still want
1940 to redefine its value with the full set of
1941 switches. Of course, an override or command
1942 definition will still take precedence. */
1943 o_file, 1);
1944 if (! all)
1945 /* The first time we are called, set MAKEFLAGS to always be exported.
1946 We should not do this again on the second call, because that is
1947 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1948 v->export = v_export;
1951 /* Print version information. */
1953 static void
1954 print_version ()
1956 static int printed_version = 0;
1958 char *precede = print_data_base_flag ? "# " : "";
1960 if (printed_version)
1961 /* Do it only once. */
1962 return;
1964 printf ("%sGNU Make version %s", precede, version_string);
1965 if (remote_description != 0 && *remote_description != '\0')
1966 printf ("-%s", remote_description);
1968 printf (", by Richard Stallman and Roland McGrath.\n\
1969 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
1970 %sThis is free software; see the source for copying conditions.\n\
1971 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1972 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1974 printed_version = 1;
1976 /* Flush stdout so the user doesn't have to wait to see the
1977 version information while things are thought about. */
1978 fflush (stdout);
1981 /* Print a bunch of information about this and that. */
1983 static void
1984 print_data_base ()
1986 extern char *ctime ();
1987 time_t when;
1989 when = time ((time_t *) 0);
1990 printf ("\n# Make data base, printed on %s", ctime (&when));
1992 print_variable_data_base ();
1993 print_dir_data_base ();
1994 print_rule_data_base ();
1995 print_file_data_base ();
1996 print_vpath_data_base ();
1998 when = time ((time_t *) 0);
1999 printf ("\n# Finished Make data base on %s\n", ctime (&when));
2002 /* Exit with STATUS, cleaning up as necessary. */
2004 void
2005 die (status)
2006 int status;
2008 static char dying = 0;
2010 if (!dying)
2012 int err;
2014 dying = 1;
2016 /* Try to move back to the original directory. This is essential on
2017 MS-DOS (where there is really only one process), and on Unix it
2018 puts core files in the original directory instead of the -C
2019 directory. */
2020 if (directory_before_chdir != 0)
2021 chdir (directory_before_chdir);
2023 if (print_version_flag)
2024 print_version ();
2026 /* Wait for children to die. */
2027 for (err = status != 0; job_slots_used > 0; err = 0)
2028 reap_children (1, err);
2030 /* Remove the intermediate files. */
2031 remove_intermediates (0);
2033 if (print_data_base_flag)
2034 print_data_base ();
2036 log_working_directory (0);
2039 exit (status);
2042 /* Write a message indicating that we've just entered or
2043 left (according to ENTERING) the current directory. */
2045 void
2046 log_working_directory (entering)
2047 int entering;
2049 static int entered = 0;
2050 char *message = entering ? "Entering" : "Leaving";
2052 /* Print nothing without the flag. Don't print the entering message
2053 again if we already have. Don't print the leaving message if we
2054 haven't printed the entering message. */
2055 if (! print_directory_flag || entering == entered)
2056 return;
2058 entered = entering;
2060 if (print_data_base_flag)
2061 fputs ("# ", stdout);
2063 if (makelevel == 0)
2064 printf ("%s: %s ", program, message);
2065 else
2066 printf ("%s[%u]: %s ", program, makelevel, message);
2068 if (starting_directory == 0)
2069 puts ("an unknown directory");
2070 else
2071 printf ("directory `%s'\n", starting_directory);