Sun May 12 19:19:43 1996 Aaron Digulla <digulla@fh-konstanz.de>
[make.git] / main.c
blobc3f8089c4a987d93c609ce27366e33fad3d629e9
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>
27 #ifdef _AMIGA
28 # include <dos/dos.h>
29 # include <proto/dos.h>
30 #endif
32 #ifdef _AMIGA
33 int __stack = 20000; /* Make sure we have 20K of stack space */
34 #endif
36 extern void init_dir PARAMS ((void));
37 extern RETSIGTYPE fatal_error_signal PARAMS ((int sig));
38 extern RETSIGTYPE child_handler PARAMS ((int sig));
40 extern void print_variable_data_base PARAMS ((void));
41 extern void print_dir_data_base PARAMS ((void));
42 extern void print_rule_data_base PARAMS ((void));
43 extern void print_file_data_base PARAMS ((void));
44 extern void print_vpath_data_base PARAMS ((void));
46 #ifndef HAVE_UNISTD_H
47 extern int chdir ();
48 #endif
49 #ifndef STDC_HEADERS
50 #ifndef sun /* Sun has an incorrect decl in a header. */
51 extern void exit ();
52 #endif
53 extern double atof ();
54 #endif
55 extern char *mktemp ();
57 static void print_data_base PARAMS((void));
58 static void print_version PARAMS ((void));
59 static void decode_switches PARAMS ((int argc, char **argv, int env));
60 static void decode_env_switches PARAMS ((char *envar, unsigned int len));
61 static void define_makeflags PARAMS ((int all, int makefile));
62 static char *quote_as_word PARAMS ((char *out, char *in, int double_dollars));
64 /* The structure that describes an accepted command switch. */
66 struct command_switch
68 char c; /* The switch character. */
70 enum /* Type of the value. */
72 flag, /* Turn int flag on. */
73 flag_off, /* Turn int flag off. */
74 string, /* One string per switch. */
75 positive_int, /* A positive integer. */
76 floating, /* A floating-point number (double). */
77 ignore /* Ignored. */
78 } type;
80 char *value_ptr; /* Pointer to the value-holding variable. */
82 unsigned int env:1; /* Can come from MAKEFLAGS. */
83 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
84 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
86 char *noarg_value; /* Pointer to value used if no argument is given. */
87 char *default_value;/* Pointer to default value. */
89 char *long_name; /* Long option name. */
90 char *argdesc; /* Descriptive word for argument. */
91 char *description; /* Description for usage message. */
95 /* The structure used to hold the list of strings given
96 in command switches of a type that takes string arguments. */
98 struct stringlist
100 char **list; /* Nil-terminated list of strings. */
101 unsigned int idx; /* Index into above. */
102 unsigned int max; /* Number of pointers allocated. */
106 /* The recognized command switches. */
108 /* Nonzero means do not print commands to be executed (-s). */
110 int silent_flag;
112 /* Nonzero means just touch the files
113 that would appear to need remaking (-t) */
115 int touch_flag;
117 /* Nonzero means just print what commands would need to be executed,
118 don't actually execute them (-n). */
120 int just_print_flag;
122 /* Print debugging trace info (-d). */
124 int debug_flag = 0;
126 /* Environment variables override makefile definitions. */
128 int env_overrides = 0;
130 /* Nonzero means ignore status codes returned by commands
131 executed to remake files. Just treat them all as successful (-i). */
133 int ignore_errors_flag = 0;
135 /* Nonzero means don't remake anything, just print the data base
136 that results from reading the makefile (-p). */
138 int print_data_base_flag = 0;
140 /* Nonzero means don't remake anything; just return a nonzero status
141 if the specified targets are not up to date (-q). */
143 int question_flag = 0;
145 /* Nonzero means do not use any of the builtin rules (-r). */
147 int no_builtin_rules_flag = 0;
149 /* Nonzero means keep going even if remaking some file fails (-k). */
151 int keep_going_flag;
152 int default_keep_going_flag = 0;
154 /* Nonzero means print directory before starting and when done (-w). */
156 int print_directory_flag = 0;
158 /* Nonzero means ignore print_directory_flag and never print the directory.
159 This is necessary because print_directory_flag is set implicitly. */
161 int inhibit_print_directory_flag = 0;
163 /* Nonzero means print version information. */
165 int print_version_flag = 0;
167 /* List of makefiles given with -f switches. */
169 static struct stringlist *makefiles = 0;
172 /* Number of job slots (commands that can be run at once). */
174 unsigned int job_slots = 1;
175 unsigned int default_job_slots = 1;
177 /* Value of job_slots that means no limit. */
179 static unsigned int inf_jobs = 0;
181 /* Maximum load average at which multiple jobs will be run.
182 Negative values mean unlimited, while zero means limit to
183 zero load (which could be useful to start infinite jobs remotely
184 but one at a time locally). */
185 #ifndef NO_FLOAT
186 double max_load_average = -1.0;
187 double default_load_average = -1.0;
188 #else
189 int max_load_average = -1;
190 int default_load_average = -1;
191 #endif
193 /* List of directories given with -C switches. */
195 static struct stringlist *directories = 0;
197 /* List of include directories given with -I switches. */
199 static struct stringlist *include_directories = 0;
201 /* List of files given with -o switches. */
203 static struct stringlist *old_files = 0;
205 /* List of files given with -W switches. */
207 static struct stringlist *new_files = 0;
209 /* If nonzero, we should just print usage and exit. */
211 static int print_usage_flag = 0;
213 /* If nonzero, we should print a warning message
214 for each reference to an undefined variable. */
216 int warn_undefined_variables_flag;
218 /* The table of command switches. */
220 static const struct command_switch switches[] =
222 { 'b', ignore, 0, 0, 0, 0, 0, 0,
223 0, 0,
224 "Ignored for compatibility" },
225 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
226 "directory", "DIRECTORY",
227 "Change to DIRECTORY before doing anything" },
228 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
229 "debug", 0,
230 "Print lots of debugging information" },
231 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
232 "environment-overrides", 0,
233 "Environment variables override makefiles" },
234 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
235 "file", "FILE",
236 "Read FILE as a makefile" },
237 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
238 "help", 0,
239 "Print this message and exit" },
240 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
241 "ignore-errors", 0,
242 "Ignore errors from commands" },
243 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
244 "include-dir", "DIRECTORY",
245 "Search DIRECTORY for included makefiles" },
246 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
247 (char *) &inf_jobs, (char *) &default_job_slots,
248 "jobs", "N",
249 "Allow N jobs at once; infinite jobs with no arg" },
250 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
251 0, (char *) &default_keep_going_flag,
252 "keep-going", 0,
253 "Keep going when some targets can't be made" },
254 #ifndef NO_FLOAT
255 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
256 (char *) &default_load_average, (char *) &default_load_average,
257 "load-average", "N",
258 "Don't start multiple jobs unless load is below N" },
259 #else
260 { 'l', positive_int, (char *) &max_load_average, 1, 1, 0,
261 (char *) &default_load_average, (char *) &default_load_average,
262 "load-average", "N",
263 "Don't start multiple jobs unless load is below N" },
264 #endif
265 { 'm', ignore, 0, 0, 0, 0, 0, 0,
266 0, 0,
267 "-b" },
268 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
269 "just-print", 0,
270 "Don't actually run any commands; just print them" },
271 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
272 "old-file", "FILE",
273 "Consider FILE to be very old and don't remake it" },
274 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
275 "print-data-base", 0,
276 "Print make's internal database" },
277 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
278 "question", 0,
279 "Run no commands; exit status says if up to date" },
280 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
281 "no-builtin-rules", 0,
282 "Disable the built-in implicit rules" },
283 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
284 "silent", 0,
285 "Don't echo commands" },
286 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
287 0, (char *) &default_keep_going_flag,
288 "no-keep-going", 0,
289 "Turns off -k" },
290 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
291 "touch", 0,
292 "Touch targets instead of remaking them" },
293 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
294 "version", 0,
295 "Print the version number of make and exit" },
296 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
297 "print-directory", 0,
298 "Print the current directory" },
299 { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
300 "no-print-directory", 0,
301 "Turn off -w, even if it was turned on implicitly" },
302 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
303 "what-if", "FILE",
304 "Consider FILE to be infinitely new" },
305 { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
306 "warn-undefined-variables", 0,
307 "Warn when an undefined variable is referenced" },
308 { '\0', }
311 /* Secondary long names for options. */
313 static struct option long_option_aliases[] =
315 { "quiet", no_argument, 0, 's' },
316 { "stop", no_argument, 0, 'S' },
317 { "new-file", required_argument, 0, 'W' },
318 { "assume-new", required_argument, 0, 'W' },
319 { "assume-old", required_argument, 0, 'o' },
320 { "max-load", optional_argument, 0, 'l' },
321 { "dry-run", no_argument, 0, 'n' },
322 { "recon", no_argument, 0, 'n' },
323 { "makefile", required_argument, 0, 'f' },
326 /* The usage message prints the descriptions of options starting in
327 this column. Make sure it leaves enough room for the longest
328 description to fit in less than 80 characters. */
330 #define DESCRIPTION_COLUMN 30
332 /* List of goal targets. */
334 static struct dep *goals, *lastgoal;
336 /* List of variables which were defined on the command line
337 (or, equivalently, in MAKEFLAGS). */
339 struct command_variable
341 struct command_variable *next;
342 struct variable *variable;
344 static struct command_variable *command_variables;
346 /* The name we were invoked with. */
348 char *program;
350 /* Our current directory before processing any -C options. */
352 char *directory_before_chdir;
354 /* Our current directory after processing all -C options. */
356 char *starting_directory;
358 /* Value of the MAKELEVEL variable at startup (or 0). */
360 unsigned int makelevel;
362 /* First file defined in the makefile whose name does not
363 start with `.'. This is the default to remake if the
364 command line does not specify. */
366 struct file *default_goal_file;
368 /* Pointer to structure for the file .DEFAULT
369 whose commands are used for any file that has none of its own.
370 This is zero if the makefiles do not define .DEFAULT. */
372 struct file *default_file;
374 /* Nonzero if we have seen the magic `.POSIX' target.
375 This turns on pedantic compliance with POSIX.2. */
377 int posix_pedantic;
379 /* Mask of signals that are being caught with fatal_error_signal. */
381 #ifdef POSIX
382 sigset_t fatal_signal_set;
383 #else
384 #ifdef HAVE_SIGSETMASK
385 int fatal_signal_mask;
386 #endif
387 #endif
389 static struct file *
390 enter_command_line_file (name)
391 char *name;
393 if (name[0] == '\0')
394 fatal ("empty string invalid as file name");
396 if (name[0] == '~')
398 char *expanded = tilde_expand (name);
399 if (expanded != 0)
400 name = expanded; /* Memory leak; I don't care. */
403 /* This is also done in parse_file_seq, so this is redundant
404 for names read from makefiles. It is here for names passed
405 on the command line. */
406 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
408 name += 2;
409 while (*name == '/')
410 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
411 ++name;
414 if (*name == '\0')
416 /* It was all slashes! Move back to the dot and truncate
417 it after the first slash, so it becomes just "./". */
419 --name;
420 while (name[0] != '.');
421 name[2] = '\0';
424 return enter_file (savestring (name, strlen (name)));
427 /* Toggle -d on receipt of SIGUSR1. */
429 static RETSIGTYPE
430 debug_signal_handler (sig)
431 int sig;
433 debug_flag = ! debug_flag;
436 #ifndef _AMIGA
438 main (argc, argv, envp)
439 int argc;
440 char **argv;
441 char **envp;
442 #else
443 int main (int argc, char ** argv)
444 #endif
446 register struct file *f;
447 register unsigned int i;
448 char **p;
449 struct dep *read_makefiles;
450 PATH_VAR (current_directory);
452 default_goal_file = 0;
453 reading_filename = 0;
454 reading_lineno_ptr = 0;
456 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
457 signame_init ();
458 #endif
460 #ifdef POSIX
461 sigemptyset (&fatal_signal_set);
462 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
463 #else
464 #ifdef HAVE_SIGSETMASK
465 fatal_signal_mask = 0;
466 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
467 #else
468 #define ADD_SIG(sig)
469 #endif
470 #endif
472 #define FATAL_SIG(sig) \
473 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
474 (void) signal ((sig), SIG_IGN); \
475 else \
476 ADD_SIG (sig);
478 #ifdef SIGHUP
479 FATAL_SIG (SIGHUP);
480 #endif
481 #ifdef SIGQUIT
482 FATAL_SIG (SIGQUIT);
483 #endif
484 FATAL_SIG (SIGINT);
485 FATAL_SIG (SIGTERM);
487 #ifdef SIGDANGER
488 FATAL_SIG (SIGDANGER);
489 #endif
490 #ifdef SIGXCPU
491 FATAL_SIG (SIGXCPU);
492 #endif
493 #ifdef SIGXFSZ
494 FATAL_SIG (SIGXFSZ);
495 #endif
497 #undef FATAL_SIG
499 /* Make sure stdout is line-buffered. */
501 #ifdef HAVE_SETLINEBUF
502 setlinebuf (stdout);
503 #else
504 #ifndef SETVBUF_REVERSED
505 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
506 #else /* setvbuf not reversed. */
507 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
508 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
509 #endif /* setvbuf reversed. */
510 #endif /* setlinebuf missing. */
512 /* Figure out where this program lives. */
514 if (argv[0] == 0)
515 argv[0] = "";
516 if (argv[0][0] == '\0')
517 program = "make";
518 else
520 #ifdef VMS
521 program = rindex (argv[0], ']');
522 #else
523 program = rindex (argv[0], '/');
524 #endif
525 #ifdef __MSDOS__
526 if (program == 0)
527 program = rindex (argv[0], '\\');
528 if (program == 0)
529 program = rindex (argv[0], ':');
530 #endif
531 if (program == 0)
532 program = argv[0];
533 else
534 ++program;
537 /* Set up to access user data (files). */
538 user_access ();
540 /* Figure out where we are. */
542 if (getcwd (current_directory, GET_PATH_MAX) == 0)
544 #ifdef HAVE_GETCWD
545 perror_with_name ("getcwd: ", "");
546 #else
547 error ("getwd: %s", current_directory);
548 #endif
549 current_directory[0] = '\0';
550 directory_before_chdir = 0;
552 else
553 directory_before_chdir = savestring (current_directory,
554 strlen (current_directory));
556 /* Read in variables from the environment. It is important that this be
557 done before $(MAKE) is are figured out so its definitions will not be
558 one from the environment. */
560 #ifndef _AMIGA
561 for (i = 0; envp[i] != 0; ++i)
563 register char *ep = envp[i];
564 while (*ep != '=')
565 ++ep;
566 /* The result of pointer arithmetic is cast to unsigned int for
567 machines where ptrdiff_t is a different size that doesn't widen
568 the same. */
569 define_variable (envp[i], (unsigned int) (ep - envp[i]),
570 ep + 1, o_env, 1)
571 /* Force exportation of every variable culled from the environment.
572 We used to rely on target_environment's v_default code to do this.
573 But that does not work for the case where an environment variable
574 is redefined in a makefile with `override'; it should then still
575 be exported, because it was originally in the environment. */
576 ->export = v_export;
578 #else /* For Amiga, read the ENV: device, ignoring all dirs */
580 BPTR env, file, old;
581 char buffer[1024];
582 int len;
583 __aligned struct FileInfoBlock fib;
585 env = Lock ("ENV:", ACCESS_READ);
586 if (env)
588 old = CurrentDir (DupLock(env));
589 Examine (env, &fib);
591 while (ExNext (env, &fib))
593 if (fib.fib_DirEntryType < 0) /* File */
595 /* Define an empty variable. It will be filled in
596 variable_lookup(). Makes startup quite a bit
597 faster. */
598 define_variable (fib.fib_FileName,
599 strlen (fib.fib_FileName),
600 "", o_env, 1)->export = v_export;
603 UnLock (env);
604 UnLock(CurrentDir(old));
607 #endif
609 /* Decode the switches. */
611 decode_env_switches ("MAKEFLAGS", 9);
612 #if 0
613 /* People write things like:
614 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
615 and we set the -p, -i and -e switches. Doesn't seem quite right. */
616 decode_env_switches ("MFLAGS", 6);
617 #endif
618 decode_switches (argc, argv, 0);
620 /* Print version information. */
622 if (print_version_flag || print_data_base_flag || debug_flag)
623 print_version ();
625 /* `make --version' is supposed to just print the version and exit. */
626 if (print_version_flag)
627 die (0);
629 #if !defined(__MSDOS__) && !defined(VMS)
630 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
631 (If it is a relative pathname with a slash, prepend our directory name
632 so the result will run the same program regardless of the current dir.
633 If it is a name with no slash, we can only hope that PATH did not
634 find it in the current directory.) */
636 if (current_directory[0] != '\0'
637 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
638 argv[0] = concat (current_directory, "/", argv[0]);
639 #endif
641 /* The extra indirection through $(MAKE_COMMAND) is done
642 for hysterical raisins. */
643 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
644 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
646 if (command_variables != 0)
648 struct command_variable *cv;
649 struct variable *v;
650 unsigned int len = 0;
651 char *value, *p;
653 /* Figure out how much space will be taken up by the command-line
654 variable definitions. */
655 for (cv = command_variables; cv != 0; cv = cv->next)
657 v = cv->variable;
658 len += 2 * strlen (v->name);
659 if (! v->recursive)
660 ++len;
661 ++len;
662 len += 2 * strlen (v->value);
665 /* Now allocate a buffer big enough and fill it. */
666 p = value = (char *) alloca (len);
667 for (cv = command_variables; cv != 0; cv = cv->next)
669 v = cv->variable;
670 p = quote_as_word (p, v->name, 0);
671 if (! v->recursive)
672 *p++ = ':';
673 *p++ = '=';
674 p = quote_as_word (p, v->value, 0);
675 *p++ = ' ';
677 p[-1] = '\0'; /* Kill the final space and terminate. */
679 /* Define an unchangeable variable with a name that no POSIX.2
680 makefile could validly use for its own variable. */
681 (void) define_variable ("-*-command-variables-*-", 23,
682 value, o_automatic, 0);
684 /* Define the variable; this will not override any user definition.
685 Normally a reference to this variable is written into the value of
686 MAKEFLAGS, allowing the user to override this value to affect the
687 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
688 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
689 a reference to this hidden variable is written instead. */
690 (void) define_variable ("MAKEOVERRIDES", 13,
691 "${-*-command-variables-*-}", o_env, 1);
694 /* If there were -C flags, move ourselves about. */
695 if (directories != 0)
696 for (i = 0; directories->list[i] != 0; ++i)
698 char *dir = directories->list[i];
699 if (dir[0] == '~')
701 char *expanded = tilde_expand (dir);
702 if (expanded != 0)
703 dir = expanded;
705 if (chdir (dir) < 0)
706 pfatal_with_name (dir);
707 if (dir != directories->list[i])
708 free (dir);
711 /* Figure out the level of recursion. */
713 struct variable *v = lookup_variable ("MAKELEVEL", 9);
714 if (v != 0 && *v->value != '\0' && *v->value != '-')
715 makelevel = (unsigned int) atoi (v->value);
716 else
717 makelevel = 0;
720 /* Except under -s, always do -w in sub-makes and under -C. */
721 if (!silent_flag && (directories != 0 || makelevel > 0))
722 print_directory_flag = 1;
724 /* Let the user disable that with --no-print-directory. */
725 if (inhibit_print_directory_flag)
726 print_directory_flag = 0;
728 /* Construct the list of include directories to search. */
730 construct_include_path (include_directories == 0 ? (char **) 0
731 : include_directories->list);
733 /* Figure out where we are now, after chdir'ing. */
734 if (directories == 0)
735 /* We didn't move, so we're still in the same place. */
736 starting_directory = current_directory;
737 else
739 if (getcwd (current_directory, GET_PATH_MAX) == 0)
741 #ifdef HAVE_GETCWD
742 perror_with_name ("getcwd: ", "");
743 #else
744 error ("getwd: %s", current_directory);
745 #endif
746 starting_directory = 0;
748 else
749 starting_directory = current_directory;
752 /* Read any stdin makefiles into temporary files. */
754 if (makefiles != 0)
756 register unsigned int i;
757 for (i = 0; i < makefiles->idx; ++i)
758 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
760 /* This makefile is standard input. Since we may re-exec
761 and thus re-read the makefiles, we read standard input
762 into a temporary file and read from that. */
763 FILE *outfile;
765 /* Make a unique filename. */
766 #ifdef HAVE_MKTEMP
768 #ifdef VMS
769 static char name[] = "sys$scratch:GmXXXXXX";
770 #else
771 static char name[] = "/tmp/GmXXXXXX";
772 #endif
773 (void) mktemp (name);
774 #else
775 static char name[L_tmpnam];
776 (void) tmpnam (name);
777 #endif
779 outfile = fopen (name, "w");
780 if (outfile == 0)
781 pfatal_with_name ("fopen (temporary file)");
782 while (!feof (stdin))
784 char buf[2048];
785 unsigned int n = fread (buf, 1, sizeof(buf), stdin);
786 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
787 pfatal_with_name ("fwrite (temporary file)");
789 /* Try to make sure we won't remake the temporary
790 file when we are re-exec'd. Kludge-o-matic! */
791 fprintf (outfile, "%s:;\n", name);
792 (void) fclose (outfile);
794 /* Replace the name that read_all_makefiles will
795 see with the name of the temporary file. */
797 char *temp;
798 /* SGI compiler requires alloca's result be assigned simply. */
799 temp = (char *) alloca (sizeof (name));
800 bcopy (name, temp, sizeof (name));
801 makefiles->list[i] = temp;
804 /* Make sure the temporary file will not be remade. */
805 f = enter_file (savestring (name, sizeof name - 1));
806 f->updated = 1;
807 f->update_status = 0;
808 f->command_state = cs_finished;
809 /* Let it be removed when we're done. */
810 f->intermediate = 1;
811 /* But don't mention it. */
812 f->dontcare = 1;
816 /* Set up to handle children dying. This must be done before
817 reading in the makefiles so that `shell' function calls will work. */
819 #ifdef SIGCHLD
820 (void) signal (SIGCHLD, child_handler);
821 #endif
822 #ifdef SIGCLD
823 (void) signal (SIGCLD, child_handler);
824 #endif
826 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
827 #ifdef SIGUSR1
828 (void) signal (SIGUSR1, debug_signal_handler);
829 #endif
831 /* Define the initial list of suffixes for old-style rules. */
833 set_default_suffixes ();
835 /* Define the file rules for the built-in suffix rules. These will later
836 be converted into pattern rules. We used to do this in
837 install_default_implicit_rules, but since that happens after reading
838 makefiles, it results in the built-in pattern rules taking precedence
839 over makefile-specified suffix rules, which is wrong. */
841 install_default_suffix_rules ();
843 /* Define some internal and special variables. */
845 define_automatic_variables ();
847 /* Set up the MAKEFLAGS and MFLAGS variables
848 so makefiles can look at them. */
850 define_makeflags (0, 0);
852 /* Define the default variables. */
853 define_default_variables ();
855 /* Read all the makefiles. */
857 default_file = enter_file (".DEFAULT");
859 read_makefiles
860 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
862 /* Decode switches again, in case the variables were set by the makefile. */
863 decode_env_switches ("MAKEFLAGS", 9);
864 #if 0
865 decode_env_switches ("MFLAGS", 6);
866 #endif
868 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
870 define_makeflags (1, 0);
872 /* Make each `struct dep' point at the `struct file' for the file
873 depended on. Also do magic for special targets. */
875 snap_deps ();
877 /* Convert old-style suffix rules to pattern rules. It is important to
878 do this before installing the built-in pattern rules below, so that
879 makefile-specified suffix rules take precedence over built-in pattern
880 rules. */
882 convert_to_pattern ();
884 /* Install the default implicit pattern rules.
885 This used to be done before reading the makefiles.
886 But in that case, built-in pattern rules were in the chain
887 before user-defined ones, so they matched first. */
889 install_default_implicit_rules ();
891 /* Compute implicit rule limits. */
893 count_implicit_rule_limits ();
895 /* Construct the listings of directories in VPATH lists. */
897 build_vpath_lists ();
899 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
900 and as having been updated already, and files given with -W flags as
901 brand new (time-stamp as far as possible into the future). */
903 if (old_files != 0)
904 for (p = old_files->list; *p != 0; ++p)
906 f = enter_command_line_file (*p);
907 f->last_mtime = (time_t) 1;
908 f->updated = 1;
909 f->update_status = 0;
910 f->command_state = cs_finished;
913 if (new_files != 0)
915 for (p = new_files->list; *p != 0; ++p)
917 f = enter_command_line_file (*p);
918 f->last_mtime = NEW_MTIME;
922 if (read_makefiles != 0)
924 /* Update any makefiles if necessary. */
926 time_t *makefile_mtimes = 0;
927 unsigned int mm_idx = 0;
929 if (debug_flag)
930 puts ("Updating makefiles....");
932 /* Remove any makefiles we don't want to try to update.
933 Also record the current modtimes so we can compare them later. */
935 register struct dep *d, *last;
936 last = 0;
937 d = read_makefiles;
938 while (d != 0)
940 register struct file *f = d->file;
941 if (f->double_colon)
942 for (f = f->double_colon; f != NULL; f = f->prev)
944 if (f->deps == 0 && f->cmds != 0)
946 /* This makefile is a :: target with commands, but
947 no dependencies. So, it will always be remade.
948 This might well cause an infinite loop, so don't
949 try to remake it. (This will only happen if
950 your makefiles are written exceptionally
951 stupidly; but if you work for Athena, that's how
952 you write your makefiles.) */
954 if (debug_flag)
955 printf ("Makefile `%s' might loop; not remaking it.\n",
956 f->name);
958 if (last == 0)
959 read_makefiles = d->next;
960 else
961 last->next = d->next;
963 /* Free the storage. */
964 free ((char *) d);
966 d = last == 0 ? 0 : last->next;
968 break;
971 if (f == NULL || !f->double_colon)
973 if (makefile_mtimes == 0)
974 makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
975 else
976 makefile_mtimes = (time_t *)
977 xrealloc ((char *) makefile_mtimes,
978 (mm_idx + 1) * sizeof (time_t));
979 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
980 last = d;
981 d = d->next;
986 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
987 define_makeflags (1, 1);
989 switch (update_goal_chain (read_makefiles, 1))
991 case 1:
992 default:
993 #define BOGUS_UPDATE_STATUS 0
994 assert (BOGUS_UPDATE_STATUS);
995 break;
997 case -1:
998 /* Did nothing. */
999 break;
1001 case 2:
1002 /* Failed to update. Figure out if we care. */
1004 /* Nonzero if any makefile was successfully remade. */
1005 int any_remade = 0;
1006 /* Nonzero if any makefile we care about failed
1007 in updating or could not be found at all. */
1008 int any_failed = 0;
1009 register unsigned int i;
1011 for (i = 0; read_makefiles != 0; ++i)
1013 struct dep *d = read_makefiles;
1014 read_makefiles = d->next;
1015 if (d->file->updated)
1017 /* This makefile was updated. */
1018 if (d->file->update_status == 0)
1020 /* It was successfully updated. */
1021 any_remade |= (file_mtime_no_search (d->file)
1022 != makefile_mtimes[i]);
1024 else if (! (d->changed & RM_DONTCARE))
1026 time_t mtime;
1027 /* The update failed and this makefile was not
1028 from the MAKEFILES variable, so we care. */
1029 error ("Failed to remake makefile `%s'.",
1030 d->file->name);
1031 mtime = file_mtime_no_search (d->file);
1032 any_remade |= (mtime != (time_t) -1
1033 && mtime != makefile_mtimes[i]);
1036 else
1037 /* This makefile was not found at all. */
1038 if (! (d->changed & RM_DONTCARE))
1040 /* This is a makefile we care about. See how much. */
1041 if (d->changed & RM_INCLUDED)
1042 /* An included makefile. We don't need
1043 to die, but we do want to complain. */
1044 error ("Included makefile `%s' was not found.",
1045 dep_name (d));
1046 else
1048 /* A normal makefile. We must die later. */
1049 error ("Makefile `%s' was not found", dep_name (d));
1050 any_failed = 1;
1054 free ((char *) d);
1057 if (any_remade)
1058 goto re_exec;
1059 else if (any_failed)
1060 die (2);
1061 else
1062 break;
1065 case 0:
1066 re_exec:
1067 /* Updated successfully. Re-exec ourselves. */
1069 remove_intermediates (0);
1071 if (print_data_base_flag)
1072 print_data_base ();
1074 log_working_directory (0);
1076 if (makefiles != 0)
1078 /* These names might have changed. */
1079 register unsigned int i, j = 0;
1080 for (i = 1; i < argc; ++i)
1081 if (!strcmp (argv[i], "-f")) /* XXX */
1083 char *p = &argv[i][2];
1084 if (*p == '\0')
1085 argv[++i] = makefiles->list[j];
1086 else
1087 argv[i] = concat ("-f", makefiles->list[j], "");
1088 ++j;
1092 if (directories != 0 && directories->idx > 0)
1094 char bad;
1095 if (directory_before_chdir != 0)
1097 if (chdir (directory_before_chdir) < 0)
1099 perror_with_name ("chdir", "");
1100 bad = 1;
1102 else
1103 bad = 0;
1105 else
1106 bad = 1;
1107 if (bad)
1108 fatal ("Couldn't change back to original directory.");
1111 #ifndef _AMIGA
1112 for (p = environ; *p != 0; ++p)
1113 if (!strncmp (*p, "MAKELEVEL=", 10))
1115 /* The SGI compiler apparently can't understand
1116 the concept of storing the result of a function
1117 in something other than a local variable. */
1118 char *sgi_loses;
1119 sgi_loses = (char *) alloca (40);
1120 *p = sgi_loses;
1121 sprintf (*p, "MAKELEVEL=%u", makelevel);
1122 break;
1124 #else /* AMIGA */
1126 char buffer[256];
1127 int len;
1129 len = GetVar ("MAKELEVEL", buffer, sizeof (buffer), GVF_GLOBAL_ONLY);
1131 if (len != -1)
1133 sprintf (buffer, "%u", makelevel);
1134 SetVar ("MAKELEVEL", buffer, -1, GVF_GLOBAL_ONLY);
1137 #endif
1139 if (debug_flag)
1141 char **p;
1142 fputs ("Re-executing:", stdout);
1143 for (p = argv; *p != 0; ++p)
1144 printf (" %s", *p);
1145 puts ("");
1148 fflush (stdout);
1149 fflush (stderr);
1151 #ifndef _AMIGA
1152 exec_command (argv, environ);
1153 #else
1154 exec_command (argv);
1155 exit (0);
1156 #endif
1157 /* NOTREACHED */
1161 /* Set up `MAKEFLAGS' again for the normal targets. */
1162 define_makeflags (1, 0);
1165 int status;
1167 /* If there were no command-line goals, use the default. */
1168 if (goals == 0)
1170 if (default_goal_file != 0)
1172 goals = (struct dep *) xmalloc (sizeof (struct dep));
1173 goals->next = 0;
1174 goals->name = 0;
1175 goals->file = default_goal_file;
1178 else
1179 lastgoal->next = 0;
1181 if (goals != 0)
1183 /* Update the goals. */
1185 if (debug_flag)
1186 puts ("Updating goal targets....");
1188 switch (update_goal_chain (goals, 0))
1190 case -1:
1191 /* Nothing happened. */
1192 case 0:
1193 /* Updated successfully. */
1194 status = EXIT_SUCCESS;
1195 break;
1196 case 2:
1197 /* Updating failed. */
1198 status = EXIT_FAILURE;
1199 break;
1200 case 1:
1201 /* We are under -q and would run some commands. */
1202 status = EXIT_FAILURE;
1203 break;
1204 default:
1205 abort ();
1208 else
1210 if (read_makefiles == 0)
1211 fatal ("No targets specified and no makefile found");
1212 else
1213 fatal ("No targets");
1216 /* Exit. */
1217 die (status);
1220 return 0;
1223 /* Parsing of arguments, decoding of switches. */
1225 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1226 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1227 (sizeof (long_option_aliases) /
1228 sizeof (long_option_aliases[0]))];
1230 /* Fill in the string and vector for getopt. */
1231 static void
1232 init_switches ()
1234 register char *p;
1235 register int c;
1236 register unsigned int i;
1238 if (options[0] != '\0')
1239 /* Already done. */
1240 return;
1242 p = options;
1244 /* Return switch and non-switch args in order, regardless of
1245 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1246 *p++ = '-';
1248 for (i = 0; switches[i].c != '\0'; ++i)
1250 long_options[i].name = (switches[i].long_name == 0 ? "" :
1251 switches[i].long_name);
1252 long_options[i].flag = 0;
1253 long_options[i].val = switches[i].c;
1254 if (isalnum (switches[i].c))
1255 *p++ = switches[i].c;
1256 switch (switches[i].type)
1258 case flag:
1259 case flag_off:
1260 case ignore:
1261 long_options[i].has_arg = no_argument;
1262 break;
1264 case string:
1265 case positive_int:
1266 case floating:
1267 if (isalnum (switches[i].c))
1268 *p++ = ':';
1269 if (switches[i].noarg_value != 0)
1271 if (isalnum (switches[i].c))
1272 *p++ = ':';
1273 long_options[i].has_arg = optional_argument;
1275 else
1276 long_options[i].has_arg = required_argument;
1277 break;
1280 *p = '\0';
1281 for (c = 0; c < (sizeof (long_option_aliases) /
1282 sizeof (long_option_aliases[0]));
1283 ++c)
1284 long_options[i++] = long_option_aliases[c];
1285 long_options[i].name = 0;
1288 static void
1289 handle_non_switch_argument (arg, env)
1290 char *arg;
1291 int env;
1293 /* Non-option argument. It might be a variable definition. */
1294 struct variable *v;
1295 if (arg[0] == '-' && arg[1] == '\0')
1296 /* Ignore plain `-' for compatibility. */
1297 return;
1298 v = try_variable_definition ((char *) 0, 0, arg, o_command);
1299 if (v != 0)
1301 /* It is indeed a variable definition. Record a pointer to
1302 the variable for later use in define_makeflags. */
1303 struct command_variable *cv
1304 = (struct command_variable *) xmalloc (sizeof (*cv));
1305 cv->variable = v;
1306 cv->next = command_variables;
1307 command_variables = cv;
1309 else if (! env)
1311 /* Not an option or variable definition; it must be a goal
1312 target! Enter it as a file and add it to the dep chain of
1313 goals. */
1314 struct file *f = enter_command_line_file (arg);
1315 f->cmd_target = 1;
1317 if (goals == 0)
1319 goals = (struct dep *) xmalloc (sizeof (struct dep));
1320 lastgoal = goals;
1322 else
1324 lastgoal->next
1325 = (struct dep *) xmalloc (sizeof (struct dep));
1326 lastgoal = lastgoal->next;
1328 lastgoal->name = 0;
1329 lastgoal->file = f;
1333 /* Decode switches from ARGC and ARGV.
1334 They came from the environment if ENV is nonzero. */
1336 static void
1337 decode_switches (argc, argv, env)
1338 int argc;
1339 char **argv;
1340 int env;
1342 int bad = 0;
1343 register const struct command_switch *cs;
1344 register struct stringlist *sl;
1345 register int c;
1347 /* getopt does most of the parsing for us.
1348 First, get its vectors set up. */
1350 init_switches ();
1352 /* Let getopt produce error messages for the command line,
1353 but not for options from the environment. */
1354 opterr = !env;
1355 /* Reset getopt's state. */
1356 optind = 0;
1358 while (optind < argc)
1360 /* Parse the next argument. */
1361 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1362 if (c == EOF)
1363 /* End of arguments, or "--" marker seen. */
1364 break;
1365 else if (c == 1)
1366 /* An argument not starting with a dash. */
1367 handle_non_switch_argument (optarg, env);
1368 else if (c == '?')
1369 /* Bad option. We will print a usage message and die later.
1370 But continue to parse the other options so the user can
1371 see all he did wrong. */
1372 bad = 1;
1373 else
1374 for (cs = switches; cs->c != '\0'; ++cs)
1375 if (cs->c == c)
1377 /* Whether or not we will actually do anything with
1378 this switch. We test this individually inside the
1379 switch below rather than just once outside it, so that
1380 options which are to be ignored still consume args. */
1381 int doit = !env || cs->env;
1383 switch (cs->type)
1385 default:
1386 abort ();
1388 case ignore:
1389 break;
1391 case flag:
1392 case flag_off:
1393 if (doit)
1394 *(int *) cs->value_ptr = cs->type == flag;
1395 break;
1397 case string:
1398 if (!doit)
1399 break;
1401 if (optarg == 0)
1402 optarg = cs->noarg_value;
1404 sl = *(struct stringlist **) cs->value_ptr;
1405 if (sl == 0)
1407 sl = (struct stringlist *)
1408 xmalloc (sizeof (struct stringlist));
1409 sl->max = 5;
1410 sl->idx = 0;
1411 sl->list = (char **) xmalloc (5 * sizeof (char *));
1412 *(struct stringlist **) cs->value_ptr = sl;
1414 else if (sl->idx == sl->max - 1)
1416 sl->max += 5;
1417 sl->list = (char **)
1418 xrealloc ((char *) sl->list,
1419 sl->max * sizeof (char *));
1421 sl->list[sl->idx++] = optarg;
1422 sl->list[sl->idx] = 0;
1423 break;
1425 case positive_int:
1426 if (optarg == 0 && argc > optind
1427 && isdigit (argv[optind][0]))
1428 optarg = argv[optind++];
1430 if (!doit)
1431 break;
1433 if (optarg != 0)
1435 int i = atoi (optarg);
1436 if (i < 1)
1438 if (doit)
1439 error ("the `-%c' option requires a \
1440 positive integral argument",
1441 cs->c);
1442 bad = 1;
1444 else
1445 *(unsigned int *) cs->value_ptr = i;
1447 else
1448 *(unsigned int *) cs->value_ptr
1449 = *(unsigned int *) cs->noarg_value;
1450 break;
1452 #ifndef NO_FLOAT
1453 case floating:
1454 if (optarg == 0 && optind < argc
1455 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1456 optarg = argv[optind++];
1458 if (doit)
1459 *(double *) cs->value_ptr
1460 = (optarg != 0 ? atof (optarg)
1461 : *(double *) cs->noarg_value);
1463 break;
1464 #endif
1467 /* We've found the switch. Stop looking. */
1468 break;
1472 /* There are no more options according to getting getopt, but there may
1473 be some arguments left. Since we have asked for non-option arguments
1474 to be returned in order, this only happens when there is a "--"
1475 argument to prevent later arguments from being options. */
1476 while (optind < argc)
1477 handle_non_switch_argument (argv[optind++], env);
1480 if (!env && (bad || print_usage_flag))
1482 /* Print a nice usage message. */
1483 FILE *usageto;
1485 if (print_version_flag)
1486 print_version ();
1488 usageto = bad ? stderr : stdout;
1490 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1492 fputs ("Options:\n", usageto);
1493 for (cs = switches; cs->c != '\0'; ++cs)
1495 char buf[1024], shortarg[50], longarg[50], *p;
1497 if (cs->description[0] == '-')
1498 continue;
1500 switch (long_options[cs - switches].has_arg)
1502 case no_argument:
1503 shortarg[0] = longarg[0] = '\0';
1504 break;
1505 case required_argument:
1506 sprintf (longarg, "=%s", cs->argdesc);
1507 sprintf (shortarg, " %s", cs->argdesc);
1508 break;
1509 case optional_argument:
1510 sprintf (longarg, "[=%s]", cs->argdesc);
1511 sprintf (shortarg, " [%s]", cs->argdesc);
1512 break;
1515 p = buf;
1517 if (isalnum (cs->c))
1519 sprintf (buf, " -%c%s", cs->c, shortarg);
1520 p += strlen (p);
1522 if (cs->long_name != 0)
1524 unsigned int i;
1525 sprintf (p, "%s--%s%s",
1526 !isalnum (cs->c) ? " " : ", ",
1527 cs->long_name, longarg);
1528 p += strlen (p);
1529 for (i = 0; i < (sizeof (long_option_aliases) /
1530 sizeof (long_option_aliases[0]));
1531 ++i)
1532 if (long_option_aliases[i].val == cs->c)
1534 sprintf (p, ", --%s%s",
1535 long_option_aliases[i].name, longarg);
1536 p += strlen (p);
1540 const struct command_switch *ncs = cs;
1541 while ((++ncs)->c != '\0')
1542 if (ncs->description[0] == '-' &&
1543 ncs->description[1] == cs->c)
1545 /* This is another switch that does the same
1546 one as the one we are processing. We want
1547 to list them all together on one line. */
1548 sprintf (p, ", -%c%s", ncs->c, shortarg);
1549 p += strlen (p);
1550 if (ncs->long_name != 0)
1552 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1553 p += strlen (p);
1558 if (p - buf > DESCRIPTION_COLUMN - 2)
1559 /* The list of option names is too long to fit on the same
1560 line with the description, leaving at least two spaces.
1561 Print it on its own line instead. */
1563 fprintf (usageto, "%s\n", buf);
1564 buf[0] = '\0';
1567 fprintf (usageto, "%*s%s.\n",
1568 - DESCRIPTION_COLUMN,
1569 buf, cs->description);
1572 die (bad ? 2 : 0);
1576 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1577 We do this by chopping the value into a vector of words, prepending a
1578 dash to the first word if it lacks one, and passing the vector to
1579 decode_switches. */
1581 static void
1582 decode_env_switches (envar, len)
1583 char *envar;
1584 unsigned int len;
1586 char *varref = (char *) alloca (2 + len + 2);
1587 char *value, *p;
1588 int argc;
1589 char **argv;
1591 /* Get the variable's value. */
1592 varref[0] = '$';
1593 varref[1] = '(';
1594 bcopy (envar, &varref[2], len);
1595 varref[2 + len] = ')';
1596 varref[2 + len + 1] = '\0';
1597 value = variable_expand (varref);
1599 /* Skip whitespace, and check for an empty value. */
1600 value = next_token (value);
1601 len = strlen (value);
1602 if (len == 0)
1603 return;
1605 /* Allocate a vector that is definitely big enough. */
1606 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
1608 /* Allocate a buffer to copy the value into while we split it into words
1609 and unquote it. We must use permanent storage for this because
1610 decode_switches may store pointers into the passed argument words. */
1611 p = (char *) xmalloc (2 * len);
1613 /* getopt will look at the arguments starting at ARGV[1].
1614 Prepend a spacer word. */
1615 argv[0] = 0;
1616 argc = 1;
1617 argv[argc] = p;
1618 while (*value != '\0')
1620 if (*value == '\\')
1621 ++value; /* Skip the backslash. */
1622 else if (isblank (*value))
1624 /* End of the word. */
1625 *p++ = '\0';
1626 argv[++argc] = p;
1628 ++value;
1629 while (isblank (*value));
1630 continue;
1632 *p++ = *value++;
1634 *p = '\0';
1635 argv[++argc] = 0;
1637 if (argc == 2 && argv[1][0] != '-')
1639 /* There is just one word in the value, and it is not a switch.
1640 Either this is the single-word form and we should prepend a dash
1641 before calling decode_switches, or this is the multi-word form and
1642 there is no dash because it is a variable definition. */
1643 struct variable *v;
1644 v = try_variable_definition ((char *) 0, 0, argv[1], o_command);
1645 if (v != 0)
1647 /* It was indeed a variable definition, and now it has been
1648 processed. There is nothing for decode_switches to do.
1649 Record a pointer to the variable for later use in
1650 define_makeflags. */
1651 struct command_variable *cv
1652 = (struct command_variable *) xmalloc (sizeof (*cv));
1653 cv->variable = v;
1654 cv->next = command_variables;
1655 command_variables = cv;
1656 return;
1659 /* It wasn't a variable definition, so it's some switches without a
1660 leading dash. Add one and pass it along to decode_switches. We
1661 need permanent storage for this in case decode_switches saves
1662 pointers into the value. */
1663 argv[1] = concat ("-", argv[1], "");
1666 /* Parse those words. */
1667 decode_switches (argc, argv, 1);
1670 /* Quote the string IN so that it will be interpreted as a single word with
1671 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
1672 signs to avoid variable expansion in make itself. Write the result into
1673 OUT, returning the address of the next character to be written.
1674 Allocating space for OUT twice the length of IN (thrice if
1675 DOUBLE_DOLLARS is nonzero) is always sufficient. */
1677 static char *
1678 quote_as_word (out, in, double_dollars)
1679 char *out, *in;
1680 int double_dollars;
1682 while (*in != '\0')
1684 #ifdef VMS
1685 if (index ("^;'\"*?$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1686 #else
1687 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
1688 #endif
1689 *out++ = '\\';
1690 if (double_dollars && *in == '$')
1691 *out++ = '$';
1692 *out++ = *in++;
1695 return out;
1698 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
1699 command switches. Include options with args if ALL is nonzero.
1700 Don't include options with the `no_makefile' flag set if MAKEFILE. */
1702 static void
1703 define_makeflags (all, makefile)
1704 int all, makefile;
1706 static const char ref[] = "$(MAKEOVERRIDES)";
1707 static const char posixref[] = "$(-*-command-variables-*-)";
1708 register const struct command_switch *cs;
1709 char *flagstring;
1710 register char *p;
1711 unsigned int words;
1712 struct variable *v;
1714 /* We will construct a linked list of `struct flag's describing
1715 all the flags which need to go in MAKEFLAGS. Then, once we
1716 know how many there are and their lengths, we can put them all
1717 together in a string. */
1719 struct flag
1721 struct flag *next;
1722 const struct command_switch *cs;
1723 char *arg;
1725 struct flag *flags = 0;
1726 unsigned int flagslen = 0;
1727 #define ADD_FLAG(ARG, LEN) \
1728 do { \
1729 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
1730 new->cs = cs; \
1731 new->arg = (ARG); \
1732 new->next = flags; \
1733 flags = new; \
1734 if (new->arg == 0) \
1735 ++flagslen; /* Just a single flag letter. */ \
1736 else \
1737 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
1738 if (!isalnum (cs->c)) \
1739 /* This switch has no single-letter version, so we use the long. */ \
1740 flagslen += 2 + strlen (cs->long_name); \
1741 } while (0)
1743 for (cs = switches; cs->c != '\0'; ++cs)
1744 if (cs->toenv && (!makefile || !cs->no_makefile))
1745 switch (cs->type)
1747 default:
1748 abort ();
1750 case ignore:
1751 break;
1753 case flag:
1754 case flag_off:
1755 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
1756 && (cs->default_value == 0
1757 || *(int *) cs->value_ptr != *(int *) cs->default_value))
1758 ADD_FLAG (0, 0);
1759 break;
1761 case positive_int:
1762 if (all)
1764 if ((cs->default_value != 0
1765 && (*(unsigned int *) cs->value_ptr
1766 == *(unsigned int *) cs->default_value)))
1767 break;
1768 else if (cs->noarg_value != 0
1769 && (*(unsigned int *) cs->value_ptr ==
1770 *(unsigned int *) cs->noarg_value))
1771 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1772 else if (cs->c == 'j')
1773 /* Special case for `-j'. */
1774 ADD_FLAG ("1", 1);
1775 else
1777 char *buf = (char *) alloca (30);
1778 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
1779 ADD_FLAG (buf, strlen (buf));
1782 break;
1784 #ifndef NO_FLOAT
1785 case floating:
1786 if (all)
1788 if (cs->default_value != 0
1789 && (*(double *) cs->value_ptr
1790 == *(double *) cs->default_value))
1791 break;
1792 else if (cs->noarg_value != 0
1793 && (*(double *) cs->value_ptr
1794 == *(double *) cs->noarg_value))
1795 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
1796 else
1798 char *buf = (char *) alloca (100);
1799 sprintf (buf, "%g", *(double *) cs->value_ptr);
1800 ADD_FLAG (buf, strlen (buf));
1803 break;
1804 #endif
1806 case string:
1807 if (all)
1809 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
1810 if (sl != 0)
1812 /* Add the elements in reverse order, because
1813 all the flags get reversed below; and the order
1814 matters for some switches (like -I). */
1815 register unsigned int i = sl->idx;
1816 while (i-- > 0)
1817 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
1820 break;
1823 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
1825 #undef ADD_FLAG
1827 /* Construct the value in FLAGSTRING.
1828 We allocate enough space for a preceding dash and trailing null. */
1829 flagstring = (char *) alloca (1 + flagslen + 1);
1830 p = flagstring;
1831 words = 1;
1832 *p++ = '-';
1833 while (flags != 0)
1835 /* Add the flag letter or name to the string. */
1836 if (!isalnum (flags->cs->c))
1838 *p++ = '-';
1839 strcpy (p, flags->cs->long_name);
1840 p += strlen (p);
1842 else
1843 *p++ = flags->cs->c;
1844 if (flags->arg != 0)
1846 /* A flag that takes an optional argument which in this case is
1847 omitted is specified by ARG being "". We must distinguish
1848 because a following flag appended without an intervening " -"
1849 is considered the arg for the first. */
1850 if (flags->arg[0] != '\0')
1852 /* Add its argument too. */
1853 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
1854 p = quote_as_word (p, flags->arg, 1);
1856 ++words;
1857 /* Write a following space and dash, for the next flag. */
1858 *p++ = ' ';
1859 *p++ = '-';
1861 else if (!isalnum (flags->cs->c))
1863 ++words;
1864 /* Long options must each go in their own word,
1865 so we write the following space and dash. */
1866 *p++ = ' ';
1867 *p++ = '-';
1869 flags = flags->next;
1872 /* Define MFLAGS before appending variable definitions. */
1874 if (p == &flagstring[1])
1875 /* No flags. */
1876 flagstring[0] = '\0';
1877 else if (p[-1] == '-')
1878 /* Kill the final space and dash. */
1879 p[-2] = '\0';
1880 else
1881 /* Terminate the string. */
1882 *p = '\0';
1884 /* Since MFLAGS is not parsed for flags, there is no reason to
1885 override any makefile redefinition. */
1886 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
1888 if (all && command_variables != 0)
1890 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
1891 command-line variable definitions. */
1893 if (p == &flagstring[1])
1894 /* No flags written, so elide the leading dash already written. */
1895 p = flagstring;
1896 else
1898 /* Separate the variables from the switches with a "--" arg. */
1899 if (p[-1] != '-')
1901 /* We did not already write a trailing " -". */
1902 *p++ = ' ';
1903 *p++ = '-';
1905 /* There is a trailing " -"; fill it out to " -- ". */
1906 *p++ = '-';
1907 *p++ = ' ';
1910 /* Copy in the string. */
1911 if (posix_pedantic)
1913 bcopy (posixref, p, sizeof posixref - 1);
1914 p += sizeof posixref - 1;
1916 else
1918 bcopy (ref, p, sizeof ref - 1);
1919 p += sizeof ref - 1;
1922 else if (p == &flagstring[1])
1924 words = 0;
1925 --p;
1927 else if (p[-1] == '-')
1928 /* Kill the final space and dash. */
1929 p -= 2;
1930 /* Terminate the string. */
1931 *p = '\0';
1933 v = define_variable ("MAKEFLAGS", 9,
1934 /* If there is just a single word of switches,
1935 omit the leading dash unless it is a single
1936 long option with two leading dashes. */
1937 &flagstring[(words == 1 && command_variables == 0
1938 && flagstring[1] != '-')
1939 ? 1 : 0],
1940 /* This used to use o_env, but that lost when a
1941 makefile defined MAKEFLAGS. Makefiles set
1942 MAKEFLAGS to add switches, but we still want
1943 to redefine its value with the full set of
1944 switches. Of course, an override or command
1945 definition will still take precedence. */
1946 o_file, 1);
1947 if (! all)
1948 /* The first time we are called, set MAKEFLAGS to always be exported.
1949 We should not do this again on the second call, because that is
1950 after reading makefiles which might have done `unexport MAKEFLAGS'. */
1951 v->export = v_export;
1954 /* Print version information. */
1956 static void
1957 print_version ()
1959 static int printed_version = 0;
1961 char *precede = print_data_base_flag ? "# " : "";
1963 if (printed_version)
1964 /* Do it only once. */
1965 return;
1967 printf ("%sGNU Make version %s", precede, version_string);
1968 if (remote_description != 0 && *remote_description != '\0')
1969 printf ("-%s", remote_description);
1971 printf (", by Richard Stallman and Roland McGrath.\n\
1972 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95 Free Software Foundation, Inc.\n\
1973 %sThis is free software; see the source for copying conditions.\n\
1974 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
1975 %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
1977 printed_version = 1;
1979 /* Flush stdout so the user doesn't have to wait to see the
1980 version information while things are thought about. */
1981 fflush (stdout);
1984 /* Print a bunch of information about this and that. */
1986 static void
1987 print_data_base ()
1989 extern char *ctime ();
1990 time_t when;
1992 when = time ((time_t *) 0);
1993 printf ("\n# Make data base, printed on %s", ctime (&when));
1995 print_variable_data_base ();
1996 print_dir_data_base ();
1997 print_rule_data_base ();
1998 print_file_data_base ();
1999 print_vpath_data_base ();
2001 when = time ((time_t *) 0);
2002 printf ("\n# Finished Make data base on %s\n", ctime (&when));
2005 /* Exit with STATUS, cleaning up as necessary. */
2007 void
2008 die (status)
2009 int status;
2011 static char dying = 0;
2013 if (!dying)
2015 int err;
2017 dying = 1;
2019 /* Try to move back to the original directory. This is essential on
2020 MS-DOS (where there is really only one process), and on Unix it
2021 puts core files in the original directory instead of the -C
2022 directory. */
2023 if (directory_before_chdir != 0)
2024 chdir (directory_before_chdir);
2026 if (print_version_flag)
2027 print_version ();
2029 /* Wait for children to die. */
2030 for (err = status != 0; job_slots_used > 0; err = 0)
2031 reap_children (1, err);
2033 /* Remove the intermediate files. */
2034 remove_intermediates (0);
2036 if (print_data_base_flag)
2037 print_data_base ();
2039 log_working_directory (0);
2042 exit (status);
2045 /* Write a message indicating that we've just entered or
2046 left (according to ENTERING) the current directory. */
2048 void
2049 log_working_directory (entering)
2050 int entering;
2052 static int entered = 0;
2053 char *message = entering ? "Entering" : "Leaving";
2055 /* Print nothing without the flag. Don't print the entering message
2056 again if we already have. Don't print the leaving message if we
2057 haven't printed the entering message. */
2058 if (! print_directory_flag || entering == entered)
2059 return;
2061 entered = entering;
2063 if (print_data_base_flag)
2064 fputs ("# ", stdout);
2066 if (makelevel == 0)
2067 printf ("%s: %s ", program, message);
2068 else
2069 printf ("%s[%u]: %s ", program, makelevel, message);
2071 if (starting_directory == 0)
2072 puts ("an unknown directory");
2073 else
2074 printf ("directory `%s'\n", starting_directory);