If a file declared .INTERMEDIATE already exists before make starts, we
[make.git] / main.c
blob4bae4151f7562c46f72fe4f8c8c4e7bd0b65f878
1 /* Argument parsing and main program of GNU Make.
2 Copyright (C) 1988,89,90,91,94,95,96,97,98 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, Inc., 59 Temple Place - Suite 330, Boston,
18 MA 02111-1307, USA. */
20 #include "make.h"
21 #include "dep.h"
22 #include "filedef.h"
23 #include "variable.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "rule.h"
27 #include "getopt.h"
28 #include <assert.h>
29 #ifdef _AMIGA
30 # include <dos/dos.h>
31 # include <proto/dos.h>
32 #endif
33 #ifdef WINDOWS32
34 #include <windows.h>
35 #include "pathstuff.h"
36 #endif
38 #ifdef _AMIGA
39 int __stack = 20000; /* Make sure we have 20K of stack space */
40 #endif
42 extern void init_dir PARAMS ((void));
43 extern void remote_setup PARAMS ((void));
44 extern void remote_cleanup PARAMS ((void));
45 extern RETSIGTYPE fatal_error_signal PARAMS ((int sig));
47 extern void print_variable_data_base PARAMS ((void));
48 extern void print_dir_data_base PARAMS ((void));
49 extern void print_rule_data_base PARAMS ((void));
50 extern void print_file_data_base PARAMS ((void));
51 extern void print_vpath_data_base PARAMS ((void));
53 #if defined HAVE_WAITPID || defined HAVE_WAIT3
54 # define HAVE_WAIT_NOHANG
55 #endif
57 #ifndef HAVE_UNISTD_H
58 extern int chdir ();
59 #endif
60 #ifndef STDC_HEADERS
61 #ifndef sun /* Sun has an incorrect decl in a header. */
62 extern void exit PARAMS ((int)) __attribute__ ((noreturn));
63 #endif
64 extern double atof ();
65 #endif
66 extern char *mktemp ();
68 static void print_data_base PARAMS ((void));
69 static void print_version PARAMS ((void));
70 static void decode_switches PARAMS ((int argc, char **argv, int env));
71 static void decode_env_switches PARAMS ((char *envar, unsigned int len));
72 static void define_makeflags PARAMS ((int all, int makefile));
73 static char *quote_as_word PARAMS ((char *out, char *in, int double_dollars));
75 /* The structure that describes an accepted command switch. */
77 struct command_switch
79 char c; /* The switch character. */
81 enum /* Type of the value. */
83 flag, /* Turn int flag on. */
84 flag_off, /* Turn int flag off. */
85 string, /* One string per switch. */
86 positive_int, /* A positive integer. */
87 floating, /* A floating-point number (double). */
88 ignore /* Ignored. */
89 } type;
91 char *value_ptr; /* Pointer to the value-holding variable. */
93 unsigned int env:1; /* Can come from MAKEFLAGS. */
94 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
95 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
97 char *noarg_value; /* Pointer to value used if no argument is given. */
98 char *default_value;/* Pointer to default value. */
100 char *long_name; /* Long option name. */
101 char *argdesc; /* Descriptive word for argument. */
102 char *description; /* Description for usage message. */
106 /* The structure used to hold the list of strings given
107 in command switches of a type that takes string arguments. */
109 struct stringlist
111 char **list; /* Nil-terminated list of strings. */
112 unsigned int idx; /* Index into above. */
113 unsigned int max; /* Number of pointers allocated. */
117 /* The recognized command switches. */
119 /* Nonzero means do not print commands to be executed (-s). */
121 int silent_flag;
123 /* Nonzero means just touch the files
124 that would appear to need remaking (-t) */
126 int touch_flag;
128 /* Nonzero means just print what commands would need to be executed,
129 don't actually execute them (-n). */
131 int just_print_flag;
133 /* Print debugging trace info (-d). */
135 int debug_flag = 0;
137 #ifdef WINDOWS32
138 /* Suspend make in main for a short time to allow debugger to attach */
140 int suspend_flag = 0;
141 #endif
143 /* Environment variables override makefile definitions. */
145 int env_overrides = 0;
147 /* Nonzero means ignore status codes returned by commands
148 executed to remake files. Just treat them all as successful (-i). */
150 int ignore_errors_flag = 0;
152 /* Nonzero means don't remake anything, just print the data base
153 that results from reading the makefile (-p). */
155 int print_data_base_flag = 0;
157 /* Nonzero means don't remake anything; just return a nonzero status
158 if the specified targets are not up to date (-q). */
160 int question_flag = 0;
162 /* Nonzero means do not use any of the builtin rules (-r). */
164 int no_builtin_rules_flag = 0;
166 /* Nonzero means keep going even if remaking some file fails (-k). */
168 int keep_going_flag;
169 int default_keep_going_flag = 0;
171 /* Nonzero means print directory before starting and when done (-w). */
173 int print_directory_flag = 0;
175 /* Nonzero means ignore print_directory_flag and never print the directory.
176 This is necessary because print_directory_flag is set implicitly. */
178 int inhibit_print_directory_flag = 0;
180 /* Nonzero means print version information. */
182 int print_version_flag = 0;
184 /* List of makefiles given with -f switches. */
186 static struct stringlist *makefiles = 0;
188 /* Number of job slots (commands that can be run at once). */
190 unsigned int job_slots = 1;
191 unsigned int default_job_slots = 1;
193 /* Value of job_slots that means no limit. */
195 static unsigned int inf_jobs = 0;
197 /* Maximum load average at which multiple jobs will be run.
198 Negative values mean unlimited, while zero means limit to
199 zero load (which could be useful to start infinite jobs remotely
200 but one at a time locally). */
201 #ifndef NO_FLOAT
202 double max_load_average = -1.0;
203 double default_load_average = -1.0;
204 #else
205 int max_load_average = -1;
206 int default_load_average = -1;
207 #endif
209 /* List of directories given with -C switches. */
211 static struct stringlist *directories = 0;
213 /* List of include directories given with -I switches. */
215 static struct stringlist *include_directories = 0;
217 /* List of files given with -o switches. */
219 static struct stringlist *old_files = 0;
221 /* List of files given with -W switches. */
223 static struct stringlist *new_files = 0;
225 /* If nonzero, we should just print usage and exit. */
227 static int print_usage_flag = 0;
229 /* If nonzero, we should print a warning message
230 for each reference to an undefined variable. */
232 int warn_undefined_variables_flag;
234 /* The table of command switches. */
236 static const struct command_switch switches[] =
238 { 'b', ignore, 0, 0, 0, 0, 0, 0,
239 0, 0,
240 "Ignored for compatibility" },
241 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
242 "directory", "DIRECTORY",
243 "Change to DIRECTORY before doing anything" },
244 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
245 "debug", 0,
246 "Print lots of debugging information" },
247 #ifdef WINDOWS32
248 { 'D', flag, (char *) &suspend_flag, 1, 1, 0, 0, 0,
249 "suspend-for-debug", 0,
250 "Suspend process to allow a debugger to attach" },
251 #endif
252 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
253 "environment-overrides", 0,
254 "Environment variables override makefiles" },
255 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
256 "file", "FILE",
257 "Read FILE as a makefile" },
258 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
259 "help", 0,
260 "Print this message and exit" },
261 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
262 "ignore-errors", 0,
263 "Ignore errors from commands" },
264 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
265 "include-dir", "DIRECTORY",
266 "Search DIRECTORY for included makefiles" },
267 { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
268 (char *) &inf_jobs, (char *) &default_job_slots,
269 "jobs", "N",
270 "Allow N jobs at once; infinite jobs with no arg" },
271 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
272 0, (char *) &default_keep_going_flag,
273 "keep-going", 0,
274 "Keep going when some targets can't be made" },
275 #ifndef NO_FLOAT
276 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
277 (char *) &default_load_average, (char *) &default_load_average,
278 "load-average", "N",
279 "Don't start multiple jobs unless load is below N" },
280 #else
281 { 'l', positive_int, (char *) &max_load_average, 1, 1, 0,
282 (char *) &default_load_average, (char *) &default_load_average,
283 "load-average", "N",
284 "Don't start multiple jobs unless load is below N" },
285 #endif
286 { 'm', ignore, 0, 0, 0, 0, 0, 0,
287 0, 0,
288 "-b" },
289 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
290 "just-print", 0,
291 "Don't actually run any commands; just print them" },
292 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
293 "old-file", "FILE",
294 "Consider FILE to be very old and don't remake it" },
295 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
296 "print-data-base", 0,
297 "Print make's internal database" },
298 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
299 "question", 0,
300 "Run no commands; exit status says if up to date" },
301 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
302 "no-builtin-rules", 0,
303 "Disable the built-in implicit rules" },
304 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
305 "silent", 0,
306 "Don't echo commands" },
307 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
308 0, (char *) &default_keep_going_flag,
309 "no-keep-going", 0,
310 "Turns off -k" },
311 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
312 "touch", 0,
313 "Touch targets instead of remaking them" },
314 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
315 "version", 0,
316 "Print the version number of make and exit" },
317 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
318 "print-directory", 0,
319 "Print the current directory" },
320 { 2, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
321 "no-print-directory", 0,
322 "Turn off -w, even if it was turned on implicitly" },
323 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
324 "what-if", "FILE",
325 "Consider FILE to be infinitely new" },
326 { 3, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
327 "warn-undefined-variables", 0,
328 "Warn when an undefined variable is referenced" },
329 { '\0', }
332 /* Secondary long names for options. */
334 static struct option long_option_aliases[] =
336 { "quiet", no_argument, 0, 's' },
337 { "stop", no_argument, 0, 'S' },
338 { "new-file", required_argument, 0, 'W' },
339 { "assume-new", required_argument, 0, 'W' },
340 { "assume-old", required_argument, 0, 'o' },
341 { "max-load", optional_argument, 0, 'l' },
342 { "dry-run", no_argument, 0, 'n' },
343 { "recon", no_argument, 0, 'n' },
344 { "makefile", required_argument, 0, 'f' },
347 /* The usage message prints the descriptions of options starting in
348 this column. Make sure it leaves enough room for the longest
349 description to fit in less than 80 characters. */
351 #define DESCRIPTION_COLUMN 30
353 /* List of goal targets. */
355 static struct dep *goals, *lastgoal;
357 /* List of variables which were defined on the command line
358 (or, equivalently, in MAKEFLAGS). */
360 struct command_variable
362 struct command_variable *next;
363 struct variable *variable;
365 static struct command_variable *command_variables;
367 /* The name we were invoked with. */
369 char *program;
371 /* Our current directory before processing any -C options. */
373 char *directory_before_chdir;
375 /* Our current directory after processing all -C options. */
377 char *starting_directory;
379 /* Value of the MAKELEVEL variable at startup (or 0). */
381 unsigned int makelevel;
383 /* First file defined in the makefile whose name does not
384 start with `.'. This is the default to remake if the
385 command line does not specify. */
387 struct file *default_goal_file;
389 /* Pointer to structure for the file .DEFAULT
390 whose commands are used for any file that has none of its own.
391 This is zero if the makefiles do not define .DEFAULT. */
393 struct file *default_file;
395 /* Nonzero if we have seen the magic `.POSIX' target.
396 This turns on pedantic compliance with POSIX.2. */
398 int posix_pedantic;
400 /* Nonzero if some rule detected clock skew; we keep track so (a) we only
401 print one warning about it during the run, and (b) we can print a final
402 warning at the end of the run. */
404 int clock_skew_detected;
406 /* Mask of signals that are being caught with fatal_error_signal. */
408 #ifdef POSIX
409 sigset_t fatal_signal_set;
410 #else
411 #ifdef HAVE_SIGSETMASK
412 int fatal_signal_mask;
413 #endif
414 #endif
416 static struct file *
417 enter_command_line_file (name)
418 char *name;
420 if (name[0] == '\0')
421 fatal (NILF, "empty string invalid as file name");
423 if (name[0] == '~')
425 char *expanded = tilde_expand (name);
426 if (expanded != 0)
427 name = expanded; /* Memory leak; I don't care. */
430 /* This is also done in parse_file_seq, so this is redundant
431 for names read from makefiles. It is here for names passed
432 on the command line. */
433 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
435 name += 2;
436 while (*name == '/')
437 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
438 ++name;
441 if (*name == '\0')
443 /* It was all slashes! Move back to the dot and truncate
444 it after the first slash, so it becomes just "./". */
446 --name;
447 while (name[0] != '.');
448 name[2] = '\0';
451 return enter_file (savestring (name, strlen (name)));
454 /* Toggle -d on receipt of SIGUSR1. */
456 static RETSIGTYPE
457 debug_signal_handler (sig)
458 int sig;
460 debug_flag = ! debug_flag;
463 #ifdef WINDOWS32
465 * HANDLE runtime exceptions by avoiding a requestor on the GUI. Capture
466 * exception and print it to stderr instead.
468 * If debug_flag not set, just print a simple message and exit.
469 * If debug_flag set, print a more verbose message.
470 * If compiled for DEBUG, let exception pass through to GUI so that
471 * debuggers can attach.
473 LONG WINAPI
474 handle_runtime_exceptions( struct _EXCEPTION_POINTERS *exinfo )
476 PEXCEPTION_RECORD exrec = exinfo->ExceptionRecord;
477 LPSTR cmdline = GetCommandLine();
478 LPSTR prg = strtok(cmdline, " ");
479 CHAR errmsg[1024];
480 #ifdef USE_EVENT_LOG
481 HANDLE hEventSource;
482 LPTSTR lpszStrings[1];
483 #endif
485 if (!debug_flag)
487 sprintf(errmsg, "%s: Interrupt/Exception caught ", prg);
488 sprintf(&errmsg[strlen(errmsg)],
489 "(code = 0x%x, addr = 0x%x)\r\n",
490 exrec->ExceptionCode, exrec->ExceptionAddress);
491 fprintf(stderr, errmsg);
492 exit(255);
495 sprintf(errmsg,
496 "\r\nUnhandled exception filter called from program %s\r\n", prg);
497 sprintf(&errmsg[strlen(errmsg)], "ExceptionCode = %x\r\n",
498 exrec->ExceptionCode);
499 sprintf(&errmsg[strlen(errmsg)], "ExceptionFlags = %x\r\n",
500 exrec->ExceptionFlags);
501 sprintf(&errmsg[strlen(errmsg)], "ExceptionAddress = %x\r\n",
502 exrec->ExceptionAddress);
504 if (exrec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION
505 && exrec->NumberParameters >= 2)
506 sprintf(&errmsg[strlen(errmsg)],
507 "Access violation: %s operation at address %x\r\n",
508 exrec->ExceptionInformation[0] ? "write": "read",
509 exrec->ExceptionInformation[1]);
511 /* turn this on if we want to put stuff in the event log too */
512 #ifdef USE_EVENT_LOG
513 hEventSource = RegisterEventSource(NULL, "GNU Make");
514 lpszStrings[0] = errmsg;
516 if (hEventSource != NULL)
518 ReportEvent(hEventSource, /* handle of event source */
519 EVENTLOG_ERROR_TYPE, /* event type */
520 0, /* event category */
521 0, /* event ID */
522 NULL, /* current user's SID */
523 1, /* strings in lpszStrings */
524 0, /* no bytes of raw data */
525 lpszStrings, /* array of error strings */
526 NULL); /* no raw data */
528 (VOID) DeregisterEventSource(hEventSource);
530 #endif
532 /* Write the error to stderr too */
533 fprintf(stderr, errmsg);
535 #ifdef DEBUG
536 return EXCEPTION_CONTINUE_SEARCH;
537 #else
538 exit(255);
539 return (255); /* not reached */
540 #endif
544 * On WIN32 systems we don't have the luxury of a /bin directory that
545 * is mapped globally to every drive mounted to the system. Since make could
546 * be invoked from any drive, and we don't want to propogate /bin/sh
547 * to every single drive. Allow ourselves a chance to search for
548 * a value for default shell here (if the default path does not exist).
552 find_and_set_default_shell(char *token)
554 int sh_found = 0;
555 char* search_token;
556 PATH_VAR(sh_path);
557 extern char *default_shell;
559 if (!token)
560 search_token = default_shell;
561 else
562 search_token = token;
564 if (!no_default_sh_exe &&
565 (token == NULL || !strcmp(search_token, default_shell))) {
566 /* no new information, path already set or known */
567 sh_found = 1;
568 } else if (file_exists_p(search_token)) {
569 /* search token path was found */
570 sprintf(sh_path, "%s", search_token);
571 default_shell = xstrdup(w32ify(sh_path,0));
572 if (debug_flag)
573 printf("find_and_set_shell setting default_shell = %s\n", default_shell);
574 sh_found = 1;
575 } else {
576 char *p;
577 struct variable *v = lookup_variable ("Path", 4);
580 * Search Path for shell
582 if (v && v->value) {
583 char *ep;
585 p = v->value;
586 ep = strchr(p, PATH_SEPARATOR_CHAR);
588 while (ep && *ep) {
589 *ep = '\0';
591 if (dir_file_exists_p(p, search_token)) {
592 sprintf(sh_path, "%s/%s", p, search_token);
593 default_shell = xstrdup(w32ify(sh_path,0));
594 sh_found = 1;
595 *ep = PATH_SEPARATOR_CHAR;
597 /* terminate loop */
598 p += strlen(p);
599 } else {
600 *ep = PATH_SEPARATOR_CHAR;
601 p = ++ep;
604 ep = strchr(p, PATH_SEPARATOR_CHAR);
607 /* be sure to check last element of Path */
608 if (p && *p && dir_file_exists_p(p, search_token)) {
609 sprintf(sh_path, "%s/%s", p, search_token);
610 default_shell = xstrdup(w32ify(sh_path,0));
611 sh_found = 1;
614 if (debug_flag && sh_found)
615 printf("find_and_set_shell path search set default_shell = %s\n", default_shell);
619 /* naive test */
620 if (!unixy_shell && sh_found &&
621 (strstr(default_shell, "sh") || strstr(default_shell, "SH"))) {
622 unixy_shell = 1;
623 batch_mode_shell = 0;
626 #ifdef BATCH_MODE_ONLY_SHELL
627 batch_mode_shell = 1;
628 #endif
630 return (sh_found);
632 #endif /* WINDOWS32 */
634 #ifdef __MSDOS__
636 static void
637 msdos_return_to_initial_directory ()
639 if (directory_before_chdir)
640 chdir (directory_before_chdir);
642 #endif
644 #ifndef _AMIGA
646 main (argc, argv, envp)
647 int argc;
648 char **argv;
649 char **envp;
650 #else
651 int main (int argc, char ** argv)
652 #endif
654 static char *stdin_nm = 0;
655 register struct file *f;
656 register unsigned int i;
657 char **p;
658 struct dep *read_makefiles;
659 PATH_VAR (current_directory);
660 #ifdef WINDOWS32
661 char *unix_path = NULL;
662 char *windows32_path = NULL;
664 SetUnhandledExceptionFilter(handle_runtime_exceptions);
666 /* start off assuming we have no shell */
667 unixy_shell = 0;
668 no_default_sh_exe = 1;
669 #endif
671 default_goal_file = 0;
672 reading_file = 0;
674 #if defined (__MSDOS__) && !defined (_POSIX_SOURCE)
675 /* Request the most powerful version of `system', to
676 make up for the dumb default shell. */
677 __system_flags = (__system_redirect
678 | __system_use_shell
679 | __system_allow_multiple_cmds
680 | __system_allow_long_cmds
681 | __system_handle_null_commands
682 | __system_emulate_chdir);
684 #endif
686 #if !defined (HAVE_STRSIGNAL) && !defined (HAVE_SYS_SIGLIST)
687 signame_init ();
688 #endif
690 #ifdef POSIX
691 sigemptyset (&fatal_signal_set);
692 #define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
693 #else
694 #ifdef HAVE_SIGSETMASK
695 fatal_signal_mask = 0;
696 #define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
697 #else
698 #define ADD_SIG(sig)
699 #endif
700 #endif
702 #define FATAL_SIG(sig) \
703 if (signal ((sig), fatal_error_signal) == SIG_IGN) \
704 (void) signal ((sig), SIG_IGN); \
705 else \
706 ADD_SIG (sig);
708 #ifdef SIGHUP
709 FATAL_SIG (SIGHUP);
710 #endif
711 #ifdef SIGQUIT
712 FATAL_SIG (SIGQUIT);
713 #endif
714 FATAL_SIG (SIGINT);
715 FATAL_SIG (SIGTERM);
717 #ifdef SIGDANGER
718 FATAL_SIG (SIGDANGER);
719 #endif
720 #ifdef SIGXCPU
721 FATAL_SIG (SIGXCPU);
722 #endif
723 #ifdef SIGXFSZ
724 FATAL_SIG (SIGXFSZ);
725 #endif
727 #undef FATAL_SIG
729 /* Do not ignore the child-death signal. This must be done before
730 any children could possibly be created; otherwise, the wait
731 functions won't work on systems with the SVR4 ECHILD brain
732 damage, if our invoker is ignoring this signal. */
734 #ifdef HAVE_WAIT_NOHANG
735 # if defined SIGCHLD
736 (void) signal (SIGCHLD, SIG_DFL);
737 # endif
738 # if defined SIGCLD && SIGCLD != SIGCHLD
739 (void) signal (SIGCLD, SIG_DFL);
740 # endif
741 #endif
743 /* Make sure stdout is line-buffered. */
745 #ifdef HAVE_SETLINEBUF
746 setlinebuf (stdout);
747 #else
748 #ifndef SETVBUF_REVERSED
749 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
750 #else /* setvbuf not reversed. */
751 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
752 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
753 #endif /* setvbuf reversed. */
754 #endif /* setlinebuf missing. */
756 /* Figure out where this program lives. */
758 if (argv[0] == 0)
759 argv[0] = "";
760 if (argv[0][0] == '\0')
761 program = "make";
762 else
764 #ifdef VMS
765 program = rindex (argv[0], ']');
766 #else
767 program = rindex (argv[0], '/');
768 #endif
769 #ifdef __MSDOS__
770 if (program == 0)
771 program = rindex (argv[0], '\\');
772 else
774 /* Some weird environments might pass us argv[0] with
775 both kinds of slashes; we must find the rightmost. */
776 char *p = rindex (argv[0], '\\');
777 if (p && p > program)
778 program = p;
780 if (program == 0 && argv[0][1] == ':')
781 program = argv[0] + 1;
782 #endif
783 if (program == 0)
784 program = argv[0];
785 else
786 ++program;
789 /* Set up to access user data (files). */
790 user_access ();
792 /* Figure out where we are. */
794 #ifdef WINDOWS32
795 if (getcwd_fs (current_directory, GET_PATH_MAX) == 0)
796 #else
797 if (getcwd (current_directory, GET_PATH_MAX) == 0)
798 #endif
800 #ifdef HAVE_GETCWD
801 perror_with_name ("getcwd: ", "");
802 #else
803 error (NILF, "getwd: %s", current_directory);
804 #endif
805 current_directory[0] = '\0';
806 directory_before_chdir = 0;
808 else
809 directory_before_chdir = savestring (current_directory,
810 strlen (current_directory));
811 #ifdef __MSDOS__
812 /* Make sure we will return to the initial directory, come what may. */
813 atexit (msdos_return_to_initial_directory);
814 #endif
816 /* Read in variables from the environment. It is important that this be
817 done before $(MAKE) is figured out so its definitions will not be
818 from the environment. */
820 #ifndef _AMIGA
821 for (i = 0; envp[i] != 0; ++i)
823 int do_not_define;
824 register char *ep = envp[i];
826 /* by default, everything gets defined and exported */
827 do_not_define = 0;
829 while (*ep != '=')
830 ++ep;
831 #ifdef WINDOWS32
832 if (!unix_path && !strncmp(envp[i], "PATH=", 5))
833 unix_path = ep+1;
834 else if (!windows32_path && !strnicmp(envp[i], "Path=", 5)) {
835 do_not_define = 1; /* it gets defined after loop exits */
836 windows32_path = ep+1;
838 #endif
839 /* The result of pointer arithmetic is cast to unsigned int for
840 machines where ptrdiff_t is a different size that doesn't widen
841 the same. */
842 if (!do_not_define)
843 define_variable (envp[i], (unsigned int) (ep - envp[i]),
844 ep + 1, o_env, 1)
845 /* Force exportation of every variable culled from the environment.
846 We used to rely on target_environment's v_default code to do this.
847 But that does not work for the case where an environment variable
848 is redefined in a makefile with `override'; it should then still
849 be exported, because it was originally in the environment. */
850 ->export = v_export;
852 #ifdef WINDOWS32
854 * Make sure that this particular spelling of 'Path' is available
856 if (windows32_path)
857 define_variable("Path", 4, windows32_path, o_env, 1)->export = v_export;
858 else if (unix_path)
859 define_variable("Path", 4, unix_path, o_env, 1)->export = v_export;
860 else
861 define_variable("Path", 4, "", o_env, 1)->export = v_export;
864 * PATH defaults to Path iff PATH not found and Path is found.
866 if (!unix_path && windows32_path)
867 define_variable("PATH", 4, windows32_path, o_env, 1)->export = v_export;
868 #endif
869 #else /* For Amiga, read the ENV: device, ignoring all dirs */
871 BPTR env, file, old;
872 char buffer[1024];
873 int len;
874 __aligned struct FileInfoBlock fib;
876 env = Lock ("ENV:", ACCESS_READ);
877 if (env)
879 old = CurrentDir (DupLock(env));
880 Examine (env, &fib);
882 while (ExNext (env, &fib))
884 if (fib.fib_DirEntryType < 0) /* File */
886 /* Define an empty variable. It will be filled in
887 variable_lookup(). Makes startup quite a bit
888 faster. */
889 define_variable (fib.fib_FileName,
890 strlen (fib.fib_FileName),
891 "", o_env, 1)->export = v_export;
894 UnLock (env);
895 UnLock(CurrentDir(old));
898 #endif
900 /* Decode the switches. */
902 decode_env_switches ("MAKEFLAGS", 9);
903 #if 0
904 /* People write things like:
905 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
906 and we set the -p, -i and -e switches. Doesn't seem quite right. */
907 decode_env_switches ("MFLAGS", 6);
908 #endif
909 decode_switches (argc, argv, 0);
910 #ifdef WINDOWS32
911 if (suspend_flag) {
912 fprintf(stderr, "%s (pid = %d)\n", argv[0], GetCurrentProcessId());
913 fprintf(stderr, "%s is suspending for 30 seconds...", argv[0]);
914 Sleep(30 * 1000);
915 fprintf(stderr, "done sleep(30). Continuing.\n");
917 #endif
919 /* Print version information. */
921 if (print_version_flag || print_data_base_flag || debug_flag)
922 print_version ();
924 /* `make --version' is supposed to just print the version and exit. */
925 if (print_version_flag)
926 die (0);
928 #if !defined(__MSDOS__) && !defined(VMS)
929 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
930 (If it is a relative pathname with a slash, prepend our directory name
931 so the result will run the same program regardless of the current dir.
932 If it is a name with no slash, we can only hope that PATH did not
933 find it in the current directory.) */
934 #ifdef WINDOWS32
936 * Convert from backslashes to forward slashes for
937 * programs like sh which don't like them. Shouldn't
938 * matter if the path is one way or the other for
939 * CreateProcess().
941 if (strpbrk(argv[0], "/:\\") ||
942 strstr(argv[0], "..") ||
943 !strncmp(argv[0], "//", 2))
944 argv[0] = xstrdup(w32ify(argv[0],1));
945 #else /* WINDOWS32 */
946 if (current_directory[0] != '\0'
947 && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
948 argv[0] = concat (current_directory, "/", argv[0]);
949 #endif /* WINDOWS32 */
950 #endif
952 /* The extra indirection through $(MAKE_COMMAND) is done
953 for hysterical raisins. */
954 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
955 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
957 if (command_variables != 0)
959 struct command_variable *cv;
960 struct variable *v;
961 unsigned int len = 0;
962 char *value, *p;
964 /* Figure out how much space will be taken up by the command-line
965 variable definitions. */
966 for (cv = command_variables; cv != 0; cv = cv->next)
968 v = cv->variable;
969 len += 2 * strlen (v->name);
970 if (! v->recursive)
971 ++len;
972 ++len;
973 len += 3 * strlen (v->value);
976 /* Now allocate a buffer big enough and fill it. */
977 p = value = (char *) alloca (len);
978 for (cv = command_variables; cv != 0; cv = cv->next)
980 v = cv->variable;
981 p = quote_as_word (p, v->name, 0);
982 if (! v->recursive)
983 *p++ = ':';
984 *p++ = '=';
985 p = quote_as_word (p, v->value, 0);
986 *p++ = ' ';
988 p[-1] = '\0'; /* Kill the final space and terminate. */
990 /* Define an unchangeable variable with a name that no POSIX.2
991 makefile could validly use for its own variable. */
992 (void) define_variable ("-*-command-variables-*-", 23,
993 value, o_automatic, 0);
995 /* Define the variable; this will not override any user definition.
996 Normally a reference to this variable is written into the value of
997 MAKEFLAGS, allowing the user to override this value to affect the
998 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
999 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
1000 a reference to this hidden variable is written instead. */
1001 (void) define_variable ("MAKEOVERRIDES", 13,
1002 "${-*-command-variables-*-}", o_env, 1);
1005 /* If there were -C flags, move ourselves about. */
1006 if (directories != 0)
1007 for (i = 0; directories->list[i] != 0; ++i)
1009 char *dir = directories->list[i];
1010 if (dir[0] == '~')
1012 char *expanded = tilde_expand (dir);
1013 if (expanded != 0)
1014 dir = expanded;
1016 if (chdir (dir) < 0)
1017 pfatal_with_name (dir);
1018 if (dir != directories->list[i])
1019 free (dir);
1022 #ifdef WINDOWS32
1024 * THIS BLOCK OF CODE MUST COME AFTER chdir() CALL ABOVE IN ORDER
1025 * TO NOT CONFUSE THE DEPENDENCY CHECKING CODE IN implicit.c.
1027 * The functions in dir.c can incorrectly cache information for "."
1028 * before we have changed directory and this can cause file
1029 * lookups to fail because the current directory (.) was pointing
1030 * at the wrong place when it was first evaluated.
1032 no_default_sh_exe = !find_and_set_default_shell(NULL);
1034 #endif /* WINDOWS32 */
1035 /* Figure out the level of recursion. */
1037 struct variable *v = lookup_variable ("MAKELEVEL", 9);
1038 if (v != 0 && *v->value != '\0' && *v->value != '-')
1039 makelevel = (unsigned int) atoi (v->value);
1040 else
1041 makelevel = 0;
1044 /* Except under -s, always do -w in sub-makes and under -C. */
1045 if (!silent_flag && (directories != 0 || makelevel > 0))
1046 print_directory_flag = 1;
1048 /* Let the user disable that with --no-print-directory. */
1049 if (inhibit_print_directory_flag)
1050 print_directory_flag = 0;
1052 /* Construct the list of include directories to search. */
1054 construct_include_path (include_directories == 0 ? (char **) 0
1055 : include_directories->list);
1057 /* Figure out where we are now, after chdir'ing. */
1058 if (directories == 0)
1059 /* We didn't move, so we're still in the same place. */
1060 starting_directory = current_directory;
1061 else
1063 #ifdef WINDOWS32
1064 if (getcwd_fs (current_directory, GET_PATH_MAX) == 0)
1065 #else
1066 if (getcwd (current_directory, GET_PATH_MAX) == 0)
1067 #endif
1069 #ifdef HAVE_GETCWD
1070 perror_with_name ("getcwd: ", "");
1071 #else
1072 error (NILF, "getwd: %s", current_directory);
1073 #endif
1074 starting_directory = 0;
1076 else
1077 starting_directory = current_directory;
1080 (void) define_variable ("CURDIR", 6, current_directory, o_default, 0);
1082 /* Read any stdin makefiles into temporary files. */
1084 if (makefiles != 0)
1086 register unsigned int i;
1087 for (i = 0; i < makefiles->idx; ++i)
1088 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
1090 /* This makefile is standard input. Since we may re-exec
1091 and thus re-read the makefiles, we read standard input
1092 into a temporary file and read from that. */
1093 FILE *outfile;
1095 /* Make a unique filename. */
1096 #ifdef HAVE_MKTEMP
1098 #ifdef VMS
1099 static char name[] = "sys$scratch:GmXXXXXX";
1100 #else
1101 static char name[] = "/tmp/GmXXXXXX";
1102 #endif
1103 (void) mktemp (name);
1104 #else
1105 static char name[L_tmpnam];
1106 (void) tmpnam (name);
1107 #endif
1109 if (stdin_nm)
1110 fatal (NILF, "Makefile from standard input specified twice.");
1112 outfile = fopen (name, "w");
1113 if (outfile == 0)
1114 pfatal_with_name ("fopen (temporary file)");
1115 while (!feof (stdin))
1117 char buf[2048];
1118 unsigned int n = fread (buf, 1, sizeof(buf), stdin);
1119 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
1120 pfatal_with_name ("fwrite (temporary file)");
1122 (void) fclose (outfile);
1124 /* Replace the name that read_all_makefiles will
1125 see with the name of the temporary file. */
1127 char *temp;
1128 /* SGI compiler requires alloca's result be assigned simply. */
1129 temp = (char *) alloca (sizeof (name));
1130 bcopy (name, temp, sizeof (name));
1131 makefiles->list[i] = temp;
1134 /* Make sure the temporary file will not be remade. */
1135 stdin_nm = savestring (name, sizeof(name) -1);
1136 f = enter_file (stdin_nm);
1137 f->updated = 1;
1138 f->update_status = 0;
1139 f->command_state = cs_finished;
1140 /* Can't be intermediate, or it'll be removed too early for
1141 make re-exec. */
1142 f->intermediate = 0;
1143 f->dontcare = 0;
1147 #ifndef HAVE_WAIT_NOHANG
1149 extern RETSIGTYPE child_handler PARAMS ((int sig));
1151 /* Set up to handle children dying. This must be done before
1152 reading in the makefiles so that `shell' function calls will work.
1153 Note we only do this if we have to. */
1154 # if defined SIGCHLD
1155 (void) signal (SIGCHLD, child_handler);
1156 # endif
1157 # if defined SIGCLD && SIGCLD != SIGCHLD
1158 (void) signal (SIGCLD, child_handler);
1159 # endif
1161 #endif
1163 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
1164 #ifdef SIGUSR1
1165 (void) signal (SIGUSR1, debug_signal_handler);
1166 #endif
1168 /* Define the initial list of suffixes for old-style rules. */
1170 set_default_suffixes ();
1172 /* Define the file rules for the built-in suffix rules. These will later
1173 be converted into pattern rules. We used to do this in
1174 install_default_implicit_rules, but since that happens after reading
1175 makefiles, it results in the built-in pattern rules taking precedence
1176 over makefile-specified suffix rules, which is wrong. */
1178 install_default_suffix_rules ();
1180 /* Define some internal and special variables. */
1182 define_automatic_variables ();
1184 /* Set up the MAKEFLAGS and MFLAGS variables
1185 so makefiles can look at them. */
1187 define_makeflags (0, 0);
1189 /* Define the default variables. */
1190 define_default_variables ();
1192 /* Read all the makefiles. */
1194 default_file = enter_file (".DEFAULT");
1196 read_makefiles
1197 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
1199 #ifdef WINDOWS32
1200 /* look one last time after reading all Makefiles */
1201 if (no_default_sh_exe)
1202 no_default_sh_exe = !find_and_set_default_shell(NULL);
1204 if (no_default_sh_exe && job_slots != 1) {
1205 error (NILF, "Do not specify -j or --jobs if sh.exe is not available.");
1206 error (NILF, "Resetting make for single job mode.");
1207 job_slots = 1;
1209 #endif /* WINDOWS32 */
1211 #ifdef __MSDOS__
1212 /* We need to know what kind of shell we will be using. */
1214 extern int _is_unixy_shell (const char *_path);
1215 struct variable *shv = lookup_variable("SHELL", 5);
1216 extern int unixy_shell;
1217 extern char *default_shell;
1219 if (shv && *shv->value)
1221 char *shell_path = recursively_expand(shv);
1223 if (shell_path && _is_unixy_shell (shell_path))
1224 unixy_shell = 1;
1225 else
1226 unixy_shell = 0;
1227 if (shell_path)
1228 default_shell = shell_path;
1231 #endif /* __MSDOS__ */
1233 /* Decode switches again, in case the variables were set by the makefile. */
1234 decode_env_switches ("MAKEFLAGS", 9);
1235 #if 0
1236 decode_env_switches ("MFLAGS", 6);
1237 #endif
1239 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
1241 define_makeflags (1, 0);
1243 /* Make each `struct dep' point at the `struct file' for the file
1244 depended on. Also do magic for special targets. */
1246 snap_deps ();
1248 /* Convert old-style suffix rules to pattern rules. It is important to
1249 do this before installing the built-in pattern rules below, so that
1250 makefile-specified suffix rules take precedence over built-in pattern
1251 rules. */
1253 convert_to_pattern ();
1255 /* Install the default implicit pattern rules.
1256 This used to be done before reading the makefiles.
1257 But in that case, built-in pattern rules were in the chain
1258 before user-defined ones, so they matched first. */
1260 install_default_implicit_rules ();
1262 /* Compute implicit rule limits. */
1264 count_implicit_rule_limits ();
1266 /* Construct the listings of directories in VPATH lists. */
1268 build_vpath_lists ();
1270 /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
1271 and as having been updated already, and files given with -W flags as
1272 brand new (time-stamp as far as possible into the future). */
1274 if (old_files != 0)
1275 for (p = old_files->list; *p != 0; ++p)
1277 f = enter_command_line_file (*p);
1278 f->last_mtime = (FILE_TIMESTAMP) 1;
1279 f->updated = 1;
1280 f->update_status = 0;
1281 f->command_state = cs_finished;
1284 if (new_files != 0)
1286 for (p = new_files->list; *p != 0; ++p)
1288 f = enter_command_line_file (*p);
1289 f->last_mtime = NEW_MTIME;
1293 /* Initialize the remote job module. */
1294 remote_setup ();
1296 if (read_makefiles != 0)
1298 /* Update any makefiles if necessary. */
1300 FILE_TIMESTAMP *makefile_mtimes = 0;
1301 unsigned int mm_idx = 0;
1302 char **nargv = argv;
1303 int nargc = argc;
1305 if (debug_flag)
1306 puts ("Updating makefiles....");
1308 /* Remove any makefiles we don't want to try to update.
1309 Also record the current modtimes so we can compare them later. */
1311 register struct dep *d, *last;
1312 last = 0;
1313 d = read_makefiles;
1314 while (d != 0)
1316 register struct file *f = d->file;
1317 if (f->double_colon)
1318 for (f = f->double_colon; f != NULL; f = f->prev)
1320 if (f->deps == 0 && f->cmds != 0)
1322 /* This makefile is a :: target with commands, but
1323 no dependencies. So, it will always be remade.
1324 This might well cause an infinite loop, so don't
1325 try to remake it. (This will only happen if
1326 your makefiles are written exceptionally
1327 stupidly; but if you work for Athena, that's how
1328 you write your makefiles.) */
1330 if (debug_flag)
1331 printf ("Makefile `%s' might loop; not remaking it.\n",
1332 f->name);
1334 if (last == 0)
1335 read_makefiles = d->next;
1336 else
1337 last->next = d->next;
1339 /* Free the storage. */
1340 free ((char *) d);
1342 d = last == 0 ? read_makefiles : last->next;
1344 break;
1347 if (f == NULL || !f->double_colon)
1349 if (makefile_mtimes == 0)
1350 makefile_mtimes = (FILE_TIMESTAMP *)
1351 xmalloc (sizeof (FILE_TIMESTAMP));
1352 else
1353 makefile_mtimes = (FILE_TIMESTAMP *)
1354 xrealloc ((char *) makefile_mtimes,
1355 (mm_idx + 1) * sizeof (FILE_TIMESTAMP));
1356 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
1357 last = d;
1358 d = d->next;
1363 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
1364 define_makeflags (1, 1);
1366 switch (update_goal_chain (read_makefiles, 1))
1368 case 1:
1369 default:
1370 #define BOGUS_UPDATE_STATUS 0
1371 assert (BOGUS_UPDATE_STATUS);
1372 break;
1374 case -1:
1375 /* Did nothing. */
1376 break;
1378 case 2:
1379 /* Failed to update. Figure out if we care. */
1381 /* Nonzero if any makefile was successfully remade. */
1382 int any_remade = 0;
1383 /* Nonzero if any makefile we care about failed
1384 in updating or could not be found at all. */
1385 int any_failed = 0;
1386 register unsigned int i;
1387 struct dep *d;
1389 for (i = 0, d = read_makefiles; d != 0; ++i, d = d->next)
1390 if (d->file->updated)
1392 /* This makefile was updated. */
1393 if (d->file->update_status == 0)
1395 /* It was successfully updated. */
1396 any_remade |= (file_mtime_no_search (d->file)
1397 != makefile_mtimes[i]);
1399 else if (! (d->changed & RM_DONTCARE))
1401 FILE_TIMESTAMP mtime;
1402 /* The update failed and this makefile was not
1403 from the MAKEFILES variable, so we care. */
1404 error (NILF, "Failed to remake makefile `%s'.",
1405 d->file->name);
1406 mtime = file_mtime_no_search (d->file);
1407 any_remade |= (mtime != (FILE_TIMESTAMP) -1
1408 && mtime != makefile_mtimes[i]);
1411 else
1412 /* This makefile was not found at all. */
1413 if (! (d->changed & RM_DONTCARE))
1415 /* This is a makefile we care about. See how much. */
1416 if (d->changed & RM_INCLUDED)
1417 /* An included makefile. We don't need
1418 to die, but we do want to complain. */
1419 error (NILF, "Included makefile `%s' was not found.",
1420 dep_name (d));
1421 else
1423 /* A normal makefile. We must die later. */
1424 error (NILF, "Makefile `%s' was not found", dep_name (d));
1425 any_failed = 1;
1429 if (any_remade)
1430 goto re_exec;
1431 else if (any_failed)
1432 die (2);
1433 else
1434 break;
1437 case 0:
1438 re_exec:
1439 /* Updated successfully. Re-exec ourselves. */
1441 remove_intermediates (0);
1443 if (print_data_base_flag)
1444 print_data_base ();
1446 log_working_directory (0);
1448 if (makefiles != 0)
1450 /* These names might have changed. */
1451 register unsigned int i, j = 0;
1452 for (i = 1; i < argc; ++i)
1453 if (!strncmp (argv[i], "-f", 2)) /* XXX */
1455 char *p = &argv[i][2];
1456 if (*p == '\0')
1457 argv[++i] = makefiles->list[j];
1458 else
1459 argv[i] = concat ("-f", makefiles->list[j], "");
1460 ++j;
1464 /* Add -o option for the stdin temporary file, if necessary. */
1465 if (stdin_nm)
1467 nargv = (char **)xmalloc((nargc + 2) * sizeof(char *));
1468 bcopy(argv, nargv, argc * sizeof(char *));
1469 nargv[nargc++] = concat("-o", stdin_nm, "");
1470 nargv[nargc] = 0;
1473 if (directories != 0 && directories->idx > 0)
1475 char bad;
1476 if (directory_before_chdir != 0)
1478 if (chdir (directory_before_chdir) < 0)
1480 perror_with_name ("chdir", "");
1481 bad = 1;
1483 else
1484 bad = 0;
1486 else
1487 bad = 1;
1488 if (bad)
1489 fatal (NILF, "Couldn't change back to original directory.");
1492 #ifndef _AMIGA
1493 for (p = environ; *p != 0; ++p)
1494 if (!strncmp (*p, "MAKELEVEL=", 10))
1496 /* The SGI compiler apparently can't understand
1497 the concept of storing the result of a function
1498 in something other than a local variable. */
1499 char *sgi_loses;
1500 sgi_loses = (char *) alloca (40);
1501 *p = sgi_loses;
1502 sprintf (*p, "MAKELEVEL=%u", makelevel);
1503 break;
1505 #else /* AMIGA */
1507 char buffer[256];
1508 int len;
1510 len = GetVar ("MAKELEVEL", buffer, sizeof (buffer), GVF_GLOBAL_ONLY);
1512 if (len != -1)
1514 sprintf (buffer, "%u", makelevel);
1515 SetVar ("MAKELEVEL", buffer, -1, GVF_GLOBAL_ONLY);
1518 #endif
1520 if (debug_flag)
1522 char **p;
1523 fputs ("Re-executing:", stdout);
1524 for (p = nargv; *p != 0; ++p)
1525 printf (" %s", *p);
1526 puts ("");
1529 fflush (stdout);
1530 fflush (stderr);
1532 #ifndef _AMIGA
1533 exec_command (nargv, environ);
1534 #else
1535 exec_command (nargv);
1536 exit (0);
1537 #endif
1538 /* NOTREACHED */
1542 /* Set up `MAKEFLAGS' again for the normal targets. */
1543 define_makeflags (1, 0);
1545 /* If there is a temp file from reading a makefile from stdin, get rid of
1546 it now. */
1547 if (stdin_nm && unlink(stdin_nm) < 0 && errno != ENOENT)
1548 perror_with_name("unlink (temporary file): ", stdin_nm);
1551 int status;
1553 /* If there were no command-line goals, use the default. */
1554 if (goals == 0)
1556 if (default_goal_file != 0)
1558 goals = (struct dep *) xmalloc (sizeof (struct dep));
1559 goals->next = 0;
1560 goals->name = 0;
1561 goals->file = default_goal_file;
1564 else
1565 lastgoal->next = 0;
1567 if (goals != 0)
1569 /* Update the goals. */
1571 if (debug_flag)
1572 puts ("Updating goal targets....");
1574 switch (update_goal_chain (goals, 0))
1576 case -1:
1577 /* Nothing happened. */
1578 case 0:
1579 /* Updated successfully. */
1580 status = EXIT_SUCCESS;
1581 break;
1582 case 2:
1583 /* Updating failed. POSIX.2 specifies exit status >1 for this;
1584 but in VMS, there is only success and failure. */
1585 status = EXIT_FAILURE ? 2 : EXIT_FAILURE;
1586 break;
1587 case 1:
1588 /* We are under -q and would run some commands. */
1589 status = EXIT_FAILURE;
1590 break;
1591 default:
1592 abort ();
1595 else
1597 if (read_makefiles == 0)
1598 fatal (NILF, "No targets specified and no makefile found");
1599 else
1600 fatal (NILF, "No targets");
1603 /* If we detected some clock skew, generate one last warning */
1604 if (clock_skew_detected)
1605 error (NILF, "*** Warning: Clock skew detected. Your build may be incomplete.");
1607 /* Exit. */
1608 die (status);
1611 return 0;
1614 /* Parsing of arguments, decoding of switches. */
1616 static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
1617 static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
1618 (sizeof (long_option_aliases) /
1619 sizeof (long_option_aliases[0]))];
1621 /* Fill in the string and vector for getopt. */
1622 static void
1623 init_switches ()
1625 register char *p;
1626 register int c;
1627 register unsigned int i;
1629 if (options[0] != '\0')
1630 /* Already done. */
1631 return;
1633 p = options;
1635 /* Return switch and non-switch args in order, regardless of
1636 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
1637 *p++ = '-';
1639 for (i = 0; switches[i].c != '\0'; ++i)
1641 long_options[i].name = (switches[i].long_name == 0 ? "" :
1642 switches[i].long_name);
1643 long_options[i].flag = 0;
1644 long_options[i].val = switches[i].c;
1645 if (isalnum (switches[i].c))
1646 *p++ = switches[i].c;
1647 switch (switches[i].type)
1649 case flag:
1650 case flag_off:
1651 case ignore:
1652 long_options[i].has_arg = no_argument;
1653 break;
1655 case string:
1656 case positive_int:
1657 case floating:
1658 if (isalnum (switches[i].c))
1659 *p++ = ':';
1660 if (switches[i].noarg_value != 0)
1662 if (isalnum (switches[i].c))
1663 *p++ = ':';
1664 long_options[i].has_arg = optional_argument;
1666 else
1667 long_options[i].has_arg = required_argument;
1668 break;
1671 *p = '\0';
1672 for (c = 0; c < (sizeof (long_option_aliases) /
1673 sizeof (long_option_aliases[0]));
1674 ++c)
1675 long_options[i++] = long_option_aliases[c];
1676 long_options[i].name = 0;
1679 static void
1680 handle_non_switch_argument (arg, env)
1681 char *arg;
1682 int env;
1684 /* Non-option argument. It might be a variable definition. */
1685 struct variable *v;
1686 if (arg[0] == '-' && arg[1] == '\0')
1687 /* Ignore plain `-' for compatibility. */
1688 return;
1689 v = try_variable_definition (0, arg, o_command);
1690 if (v != 0)
1692 /* It is indeed a variable definition. Record a pointer to
1693 the variable for later use in define_makeflags. */
1694 struct command_variable *cv
1695 = (struct command_variable *) xmalloc (sizeof (*cv));
1696 cv->variable = v;
1697 cv->next = command_variables;
1698 command_variables = cv;
1700 else if (! env)
1702 /* Not an option or variable definition; it must be a goal
1703 target! Enter it as a file and add it to the dep chain of
1704 goals. */
1705 struct file *f = enter_command_line_file (arg);
1706 f->cmd_target = 1;
1708 if (goals == 0)
1710 goals = (struct dep *) xmalloc (sizeof (struct dep));
1711 lastgoal = goals;
1713 else
1715 lastgoal->next
1716 = (struct dep *) xmalloc (sizeof (struct dep));
1717 lastgoal = lastgoal->next;
1719 lastgoal->name = 0;
1720 lastgoal->file = f;
1723 /* Add this target name to the MAKECMDGOALS variable. */
1724 struct variable *v;
1725 char *value;
1727 v = lookup_variable("MAKECMDGOALS", 12);
1728 if (v == 0)
1729 value = f->name;
1730 else
1732 /* Paste the old and new values together */
1733 unsigned int oldlen, newlen;
1735 oldlen = strlen(v->value);
1736 newlen = strlen(f->name);
1737 value = (char *)alloca(oldlen + 1 + newlen + 1);
1738 bcopy(v->value, value, oldlen);
1739 value[oldlen] = ' ';
1740 bcopy(f->name, &value[oldlen + 1], newlen + 1);
1742 define_variable("MAKECMDGOALS", 12, value, o_default, 0);
1747 /* Decode switches from ARGC and ARGV.
1748 They came from the environment if ENV is nonzero. */
1750 static void
1751 decode_switches (argc, argv, env)
1752 int argc;
1753 char **argv;
1754 int env;
1756 int bad = 0;
1757 register const struct command_switch *cs;
1758 register struct stringlist *sl;
1759 register int c;
1761 /* getopt does most of the parsing for us.
1762 First, get its vectors set up. */
1764 init_switches ();
1766 /* Let getopt produce error messages for the command line,
1767 but not for options from the environment. */
1768 opterr = !env;
1769 /* Reset getopt's state. */
1770 optind = 0;
1772 while (optind < argc)
1774 /* Parse the next argument. */
1775 c = getopt_long (argc, argv, options, long_options, (int *) 0);
1776 if (c == EOF)
1777 /* End of arguments, or "--" marker seen. */
1778 break;
1779 else if (c == 1)
1780 /* An argument not starting with a dash. */
1781 handle_non_switch_argument (optarg, env);
1782 else if (c == '?')
1783 /* Bad option. We will print a usage message and die later.
1784 But continue to parse the other options so the user can
1785 see all he did wrong. */
1786 bad = 1;
1787 else
1788 for (cs = switches; cs->c != '\0'; ++cs)
1789 if (cs->c == c)
1791 /* Whether or not we will actually do anything with
1792 this switch. We test this individually inside the
1793 switch below rather than just once outside it, so that
1794 options which are to be ignored still consume args. */
1795 int doit = !env || cs->env;
1797 switch (cs->type)
1799 default:
1800 abort ();
1802 case ignore:
1803 break;
1805 case flag:
1806 case flag_off:
1807 if (doit)
1808 *(int *) cs->value_ptr = cs->type == flag;
1809 break;
1811 case string:
1812 if (!doit)
1813 break;
1815 if (optarg == 0)
1816 optarg = cs->noarg_value;
1818 sl = *(struct stringlist **) cs->value_ptr;
1819 if (sl == 0)
1821 sl = (struct stringlist *)
1822 xmalloc (sizeof (struct stringlist));
1823 sl->max = 5;
1824 sl->idx = 0;
1825 sl->list = (char **) xmalloc (5 * sizeof (char *));
1826 *(struct stringlist **) cs->value_ptr = sl;
1828 else if (sl->idx == sl->max - 1)
1830 sl->max += 5;
1831 sl->list = (char **)
1832 xrealloc ((char *) sl->list,
1833 sl->max * sizeof (char *));
1835 sl->list[sl->idx++] = optarg;
1836 sl->list[sl->idx] = 0;
1837 break;
1839 case positive_int:
1840 if (optarg == 0 && argc > optind
1841 && isdigit (argv[optind][0]))
1842 optarg = argv[optind++];
1844 if (!doit)
1845 break;
1847 if (optarg != 0)
1849 int i = atoi (optarg);
1850 if (i < 1)
1852 if (doit)
1853 error (NILF, "the `-%c' option requires a \
1854 positive integral argument",
1855 cs->c);
1856 bad = 1;
1858 else
1859 *(unsigned int *) cs->value_ptr = i;
1861 else
1862 *(unsigned int *) cs->value_ptr
1863 = *(unsigned int *) cs->noarg_value;
1864 break;
1866 #ifndef NO_FLOAT
1867 case floating:
1868 if (optarg == 0 && optind < argc
1869 && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
1870 optarg = argv[optind++];
1872 if (doit)
1873 *(double *) cs->value_ptr
1874 = (optarg != 0 ? atof (optarg)
1875 : *(double *) cs->noarg_value);
1877 break;
1878 #endif
1881 /* We've found the switch. Stop looking. */
1882 break;
1886 /* There are no more options according to getting getopt, but there may
1887 be some arguments left. Since we have asked for non-option arguments
1888 to be returned in order, this only happens when there is a "--"
1889 argument to prevent later arguments from being options. */
1890 while (optind < argc)
1891 handle_non_switch_argument (argv[optind++], env);
1894 if (!env && (bad || print_usage_flag))
1896 /* Print a nice usage message. */
1897 FILE *usageto;
1899 if (print_version_flag)
1900 print_version ();
1902 usageto = bad ? stderr : stdout;
1904 fprintf (usageto, "Usage: %s [options] [target] ...\n", program);
1906 fputs ("Options:\n", usageto);
1907 for (cs = switches; cs->c != '\0'; ++cs)
1909 char buf[1024], shortarg[50], longarg[50], *p;
1911 if (cs->description[0] == '-')
1912 continue;
1914 switch (long_options[cs - switches].has_arg)
1916 case no_argument:
1917 shortarg[0] = longarg[0] = '\0';
1918 break;
1919 case required_argument:
1920 sprintf (longarg, "=%s", cs->argdesc);
1921 sprintf (shortarg, " %s", cs->argdesc);
1922 break;
1923 case optional_argument:
1924 sprintf (longarg, "[=%s]", cs->argdesc);
1925 sprintf (shortarg, " [%s]", cs->argdesc);
1926 break;
1929 p = buf;
1931 if (isalnum (cs->c))
1933 sprintf (buf, " -%c%s", cs->c, shortarg);
1934 p += strlen (p);
1936 if (cs->long_name != 0)
1938 unsigned int i;
1939 sprintf (p, "%s--%s%s",
1940 !isalnum (cs->c) ? " " : ", ",
1941 cs->long_name, longarg);
1942 p += strlen (p);
1943 for (i = 0; i < (sizeof (long_option_aliases) /
1944 sizeof (long_option_aliases[0]));
1945 ++i)
1946 if (long_option_aliases[i].val == cs->c)
1948 sprintf (p, ", --%s%s",
1949 long_option_aliases[i].name, longarg);
1950 p += strlen (p);
1954 const struct command_switch *ncs = cs;
1955 while ((++ncs)->c != '\0')
1956 if (ncs->description[0] == '-' &&
1957 ncs->description[1] == cs->c)
1959 /* This is another switch that does the same
1960 one as the one we are processing. We want
1961 to list them all together on one line. */
1962 sprintf (p, ", -%c%s", ncs->c, shortarg);
1963 p += strlen (p);
1964 if (ncs->long_name != 0)
1966 sprintf (p, ", --%s%s", ncs->long_name, longarg);
1967 p += strlen (p);
1972 if (p - buf > DESCRIPTION_COLUMN - 2)
1973 /* The list of option names is too long to fit on the same
1974 line with the description, leaving at least two spaces.
1975 Print it on its own line instead. */
1977 fprintf (usageto, "%s\n", buf);
1978 buf[0] = '\0';
1981 fprintf (usageto, "%*s%s.\n",
1982 - DESCRIPTION_COLUMN,
1983 buf, cs->description);
1986 die (bad ? 2 : 0);
1990 /* Decode switches from environment variable ENVAR (which is LEN chars long).
1991 We do this by chopping the value into a vector of words, prepending a
1992 dash to the first word if it lacks one, and passing the vector to
1993 decode_switches. */
1995 static void
1996 decode_env_switches (envar, len)
1997 char *envar;
1998 unsigned int len;
2000 char *varref = (char *) alloca (2 + len + 2);
2001 char *value, *p;
2002 int argc;
2003 char **argv;
2005 /* Get the variable's value. */
2006 varref[0] = '$';
2007 varref[1] = '(';
2008 bcopy (envar, &varref[2], len);
2009 varref[2 + len] = ')';
2010 varref[2 + len + 1] = '\0';
2011 value = variable_expand (varref);
2013 /* Skip whitespace, and check for an empty value. */
2014 value = next_token (value);
2015 len = strlen (value);
2016 if (len == 0)
2017 return;
2019 /* Allocate a vector that is definitely big enough. */
2020 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
2022 /* Allocate a buffer to copy the value into while we split it into words
2023 and unquote it. We must use permanent storage for this because
2024 decode_switches may store pointers into the passed argument words. */
2025 p = (char *) xmalloc (2 * len);
2027 /* getopt will look at the arguments starting at ARGV[1].
2028 Prepend a spacer word. */
2029 argv[0] = 0;
2030 argc = 1;
2031 argv[argc] = p;
2032 while (*value != '\0')
2034 if (*value == '\\')
2035 ++value; /* Skip the backslash. */
2036 else if (isblank (*value))
2038 /* End of the word. */
2039 *p++ = '\0';
2040 argv[++argc] = p;
2042 ++value;
2043 while (isblank (*value));
2044 continue;
2046 *p++ = *value++;
2048 *p = '\0';
2049 argv[++argc] = 0;
2051 if (argv[1][0] != '-' && index (argv[1], '=') == 0)
2052 /* The first word doesn't start with a dash and isn't a variable
2053 definition. Add a dash and pass it along to decode_switches. We
2054 need permanent storage for this in case decode_switches saves
2055 pointers into the value. */
2056 argv[1] = concat ("-", argv[1], "");
2058 /* Parse those words. */
2059 decode_switches (argc, argv, 1);
2062 /* Quote the string IN so that it will be interpreted as a single word with
2063 no magic by the shell; if DOUBLE_DOLLARS is nonzero, also double dollar
2064 signs to avoid variable expansion in make itself. Write the result into
2065 OUT, returning the address of the next character to be written.
2066 Allocating space for OUT twice the length of IN (thrice if
2067 DOUBLE_DOLLARS is nonzero) is always sufficient. */
2069 static char *
2070 quote_as_word (out, in, double_dollars)
2071 char *out, *in;
2072 int double_dollars;
2074 while (*in != '\0')
2076 #ifdef VMS
2077 if (index ("^;'\"*?$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
2078 #else
2079 if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *in) != 0)
2080 #endif
2081 *out++ = '\\';
2082 if (double_dollars && *in == '$')
2083 *out++ = '$';
2084 *out++ = *in++;
2087 return out;
2090 /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
2091 command switches. Include options with args if ALL is nonzero.
2092 Don't include options with the `no_makefile' flag set if MAKEFILE. */
2094 static void
2095 define_makeflags (all, makefile)
2096 int all, makefile;
2098 static const char ref[] = "$(MAKEOVERRIDES)";
2099 static const char posixref[] = "$(-*-command-variables-*-)";
2100 register const struct command_switch *cs;
2101 char *flagstring;
2102 register char *p;
2103 unsigned int words;
2104 struct variable *v;
2106 /* We will construct a linked list of `struct flag's describing
2107 all the flags which need to go in MAKEFLAGS. Then, once we
2108 know how many there are and their lengths, we can put them all
2109 together in a string. */
2111 struct flag
2113 struct flag *next;
2114 const struct command_switch *cs;
2115 char *arg;
2117 struct flag *flags = 0;
2118 unsigned int flagslen = 0;
2119 #define ADD_FLAG(ARG, LEN) \
2120 do { \
2121 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
2122 new->cs = cs; \
2123 new->arg = (ARG); \
2124 new->next = flags; \
2125 flags = new; \
2126 if (new->arg == 0) \
2127 ++flagslen; /* Just a single flag letter. */ \
2128 else \
2129 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
2130 if (!isalnum (cs->c)) \
2131 /* This switch has no single-letter version, so we use the long. */ \
2132 flagslen += 2 + strlen (cs->long_name); \
2133 } while (0)
2135 for (cs = switches; cs->c != '\0'; ++cs)
2136 if (cs->toenv && (!makefile || !cs->no_makefile))
2137 switch (cs->type)
2139 default:
2140 abort ();
2142 case ignore:
2143 break;
2145 case flag:
2146 case flag_off:
2147 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
2148 && (cs->default_value == 0
2149 || *(int *) cs->value_ptr != *(int *) cs->default_value))
2150 ADD_FLAG (0, 0);
2151 break;
2153 case positive_int:
2154 if (all)
2156 if ((cs->default_value != 0
2157 && (*(unsigned int *) cs->value_ptr
2158 == *(unsigned int *) cs->default_value)))
2159 break;
2160 else if (cs->noarg_value != 0
2161 && (*(unsigned int *) cs->value_ptr ==
2162 *(unsigned int *) cs->noarg_value))
2163 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
2164 else if (cs->c == 'j')
2165 /* Special case for `-j'. */
2166 ADD_FLAG ("1", 1);
2167 else
2169 char *buf = (char *) alloca (30);
2170 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
2171 ADD_FLAG (buf, strlen (buf));
2174 break;
2176 #ifndef NO_FLOAT
2177 case floating:
2178 if (all)
2180 if (cs->default_value != 0
2181 && (*(double *) cs->value_ptr
2182 == *(double *) cs->default_value))
2183 break;
2184 else if (cs->noarg_value != 0
2185 && (*(double *) cs->value_ptr
2186 == *(double *) cs->noarg_value))
2187 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
2188 else
2190 char *buf = (char *) alloca (100);
2191 sprintf (buf, "%g", *(double *) cs->value_ptr);
2192 ADD_FLAG (buf, strlen (buf));
2195 break;
2196 #endif
2198 case string:
2199 if (all)
2201 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
2202 if (sl != 0)
2204 /* Add the elements in reverse order, because
2205 all the flags get reversed below; and the order
2206 matters for some switches (like -I). */
2207 register unsigned int i = sl->idx;
2208 while (i-- > 0)
2209 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
2212 break;
2215 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
2217 #undef ADD_FLAG
2219 /* Construct the value in FLAGSTRING.
2220 We allocate enough space for a preceding dash and trailing null. */
2221 flagstring = (char *) alloca (1 + flagslen + 1);
2222 p = flagstring;
2223 words = 1;
2224 *p++ = '-';
2225 while (flags != 0)
2227 /* Add the flag letter or name to the string. */
2228 if (!isalnum (flags->cs->c))
2230 *p++ = '-';
2231 strcpy (p, flags->cs->long_name);
2232 p += strlen (p);
2234 else
2235 *p++ = flags->cs->c;
2236 if (flags->arg != 0)
2238 /* A flag that takes an optional argument which in this case is
2239 omitted is specified by ARG being "". We must distinguish
2240 because a following flag appended without an intervening " -"
2241 is considered the arg for the first. */
2242 if (flags->arg[0] != '\0')
2244 /* Add its argument too. */
2245 *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
2246 p = quote_as_word (p, flags->arg, 1);
2248 ++words;
2249 /* Write a following space and dash, for the next flag. */
2250 *p++ = ' ';
2251 *p++ = '-';
2253 else if (!isalnum (flags->cs->c))
2255 ++words;
2256 /* Long options must each go in their own word,
2257 so we write the following space and dash. */
2258 *p++ = ' ';
2259 *p++ = '-';
2261 flags = flags->next;
2264 /* Define MFLAGS before appending variable definitions. */
2266 if (p == &flagstring[1])
2267 /* No flags. */
2268 flagstring[0] = '\0';
2269 else if (p[-1] == '-')
2271 /* Kill the final space and dash. */
2272 p -= 2;
2273 *p = '\0';
2275 else
2276 /* Terminate the string. */
2277 *p = '\0';
2279 /* Since MFLAGS is not parsed for flags, there is no reason to
2280 override any makefile redefinition. */
2281 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
2283 if (all && command_variables != 0)
2285 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
2286 command-line variable definitions. */
2288 if (p == &flagstring[1])
2289 /* No flags written, so elide the leading dash already written. */
2290 p = flagstring;
2291 else
2293 /* Separate the variables from the switches with a "--" arg. */
2294 if (p[-1] != '-')
2296 /* We did not already write a trailing " -". */
2297 *p++ = ' ';
2298 *p++ = '-';
2300 /* There is a trailing " -"; fill it out to " -- ". */
2301 *p++ = '-';
2302 *p++ = ' ';
2305 /* Copy in the string. */
2306 if (posix_pedantic)
2308 bcopy (posixref, p, sizeof posixref - 1);
2309 p += sizeof posixref - 1;
2311 else
2313 bcopy (ref, p, sizeof ref - 1);
2314 p += sizeof ref - 1;
2317 else if (p == &flagstring[1])
2319 words = 0;
2320 --p;
2322 else if (p[-1] == '-')
2323 /* Kill the final space and dash. */
2324 p -= 2;
2325 /* Terminate the string. */
2326 *p = '\0';
2328 v = define_variable ("MAKEFLAGS", 9,
2329 /* If there are switches, omit the leading dash
2330 unless it is a single long option with two
2331 leading dashes. */
2332 &flagstring[(flagstring[0] == '-'
2333 && flagstring[1] != '-')
2334 ? 1 : 0],
2335 /* This used to use o_env, but that lost when a
2336 makefile defined MAKEFLAGS. Makefiles set
2337 MAKEFLAGS to add switches, but we still want
2338 to redefine its value with the full set of
2339 switches. Of course, an override or command
2340 definition will still take precedence. */
2341 o_file, 1);
2342 if (! all)
2343 /* The first time we are called, set MAKEFLAGS to always be exported.
2344 We should not do this again on the second call, because that is
2345 after reading makefiles which might have done `unexport MAKEFLAGS'. */
2346 v->export = v_export;
2349 /* Print version information. */
2351 static void
2352 print_version ()
2354 static int printed_version = 0;
2356 char *precede = print_data_base_flag ? "# " : "";
2358 if (printed_version)
2359 /* Do it only once. */
2360 return;
2362 printf ("%sGNU Make version %s", precede, version_string);
2363 if (remote_description != 0 && *remote_description != '\0')
2364 printf ("-%s", remote_description);
2366 printf (", by Richard Stallman and Roland McGrath.\n\
2367 %sCopyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99\n\
2368 %s\tFree Software Foundation, Inc.\n\
2369 %sThis is free software; see the source for copying conditions.\n\
2370 %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
2371 %sPARTICULAR PURPOSE.\n\n\
2372 %sReport bugs to <bug-make@gnu.org>.\n\n",
2373 precede, precede, precede, precede, precede, precede);
2375 printed_version = 1;
2377 /* Flush stdout so the user doesn't have to wait to see the
2378 version information while things are thought about. */
2379 fflush (stdout);
2382 /* Print a bunch of information about this and that. */
2384 static void
2385 print_data_base ()
2387 time_t when;
2389 when = time ((time_t *) 0);
2390 printf ("\n# Make data base, printed on %s", ctime (&when));
2392 print_variable_data_base ();
2393 print_dir_data_base ();
2394 print_rule_data_base ();
2395 print_file_data_base ();
2396 print_vpath_data_base ();
2398 when = time ((time_t *) 0);
2399 printf ("\n# Finished Make data base on %s\n", ctime (&when));
2402 /* Exit with STATUS, cleaning up as necessary. */
2404 void
2405 die (status)
2406 int status;
2408 static char dying = 0;
2410 if (!dying)
2412 int err;
2414 dying = 1;
2416 if (print_version_flag)
2417 print_version ();
2419 /* Wait for children to die. */
2420 for (err = status != 0; job_slots_used > 0; err = 0)
2421 reap_children (1, err);
2423 /* Let the remote job module clean up its state. */
2424 remote_cleanup ();
2426 /* Remove the intermediate files. */
2427 remove_intermediates (0);
2429 if (print_data_base_flag)
2430 print_data_base ();
2432 /* Try to move back to the original directory. This is essential on
2433 MS-DOS (where there is really only one process), and on Unix it
2434 puts core files in the original directory instead of the -C
2435 directory. Must wait until after remove_intermediates(), or unlinks
2436 of relative pathnames fail. */
2437 if (directory_before_chdir != 0)
2438 chdir (directory_before_chdir);
2440 log_working_directory (0);
2443 exit (status);
2446 /* Write a message indicating that we've just entered or
2447 left (according to ENTERING) the current directory. */
2449 void
2450 log_working_directory (entering)
2451 int entering;
2453 static int entered = 0;
2454 char *msg = entering ? "Entering" : "Leaving";
2456 /* Print nothing without the flag. Don't print the entering message
2457 again if we already have. Don't print the leaving message if we
2458 haven't printed the entering message. */
2459 if (! print_directory_flag || entering == entered)
2460 return;
2462 entered = entering;
2464 if (print_data_base_flag)
2465 fputs ("# ", stdout);
2467 if (makelevel == 0)
2468 printf ("%s: %s ", program, msg);
2469 else
2470 printf ("%s[%u]: %s ", program, makelevel, msg);
2472 if (starting_directory == 0)
2473 puts ("an unknown directory");
2474 else
2475 printf ("directory `%s'\n", starting_directory);