VirtualBox

source: kBuild/trunk/src/gmake/main.c@ 311

Last change on this file since 311 was 287, checked in by bird, 20 years ago

join + optimizations.

  • Property svn:eol-style set to native
File size: 86.9 KB
Line 
1/* Argument parsing and main program of GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
32002, 2003, 2005 Free Software Foundation, Inc.
4This file is part of GNU Make.
5
6GNU Make is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Make is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Make; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19MA 02111-1307, USA. */
20
21#include "make.h"
22#include "dep.h"
23#include "filedef.h"
24#include "variable.h"
25#include "job.h"
26#include "commands.h"
27#include "rule.h"
28#include "debug.h"
29#include "getopt.h"
30
31#include <assert.h>
32#ifdef _AMIGA
33# include <dos/dos.h>
34# include <proto/dos.h>
35#endif
36#ifdef WINDOWS32
37#include <windows.h>
38#include <io.h>
39#include "pathstuff.h"
40#endif
41#ifdef __EMX__
42# include <sys/types.h>
43# include <sys/wait.h>
44#endif
45#ifdef HAVE_FCNTL_H
46# include <fcntl.h>
47#endif
48
49#if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
50# define SET_STACK_SIZE
51#endif
52
53#ifdef SET_STACK_SIZE
54# include <sys/resource.h>
55#endif
56
57#ifdef _AMIGA
58int __stack = 20000; /* Make sure we have 20K of stack space */
59#endif
60
61extern void init_dir PARAMS ((void));
62extern void remote_setup PARAMS ((void));
63extern void remote_cleanup PARAMS ((void));
64extern RETSIGTYPE fatal_error_signal PARAMS ((int sig));
65
66extern void print_variable_data_base PARAMS ((void));
67extern void print_dir_data_base PARAMS ((void));
68extern void print_rule_data_base PARAMS ((void));
69extern void print_file_data_base PARAMS ((void));
70extern void print_vpath_data_base PARAMS ((void));
71
72#if defined HAVE_WAITPID || defined HAVE_WAIT3
73# define HAVE_WAIT_NOHANG
74#endif
75
76#ifndef HAVE_UNISTD_H
77extern int chdir ();
78#endif
79#ifndef STDC_HEADERS
80# ifndef sun /* Sun has an incorrect decl in a header. */
81extern void exit PARAMS ((int)) __attribute__ ((noreturn));
82# endif
83extern double atof ();
84#endif
85
86static void print_data_base PARAMS ((void));
87static void print_version PARAMS ((void));
88static void decode_switches PARAMS ((int argc, char **argv, int env));
89static void decode_env_switches PARAMS ((char *envar, unsigned int len));
90static void define_makeflags PARAMS ((int all, int makefile));
91static char *quote_for_env PARAMS ((char *out, char *in));
92static void initialize_global_hash_tables PARAMS ((void));
93
94
95
96/* The structure that describes an accepted command switch. */
97
98struct command_switch
99 {
100 int c; /* The switch character. */
101
102 enum /* Type of the value. */
103 {
104 flag, /* Turn int flag on. */
105 flag_off, /* Turn int flag off. */
106 string, /* One string per switch. */
107 positive_int, /* A positive integer. */
108 floating, /* A floating-point number (double). */
109 ignore /* Ignored. */
110 } type;
111
112 char *value_ptr; /* Pointer to the value-holding variable. */
113
114 unsigned int env:1; /* Can come from MAKEFLAGS. */
115 unsigned int toenv:1; /* Should be put in MAKEFLAGS. */
116 unsigned int no_makefile:1; /* Don't propagate when remaking makefiles. */
117
118 char *noarg_value; /* Pointer to value used if no argument is given. */
119 char *default_value;/* Pointer to default value. */
120
121 char *long_name; /* Long option name. */
122 };
123
124/* True if C is a switch value that corresponds to a short option. */
125
126#define short_option(c) ((c) <= CHAR_MAX)
127
128/* The structure used to hold the list of strings given
129 in command switches of a type that takes string arguments. */
130
131struct stringlist
132 {
133 char **list; /* Nil-terminated list of strings. */
134 unsigned int idx; /* Index into above. */
135 unsigned int max; /* Number of pointers allocated. */
136 };
137
138
139/* The recognized command switches. */
140
141/* Nonzero means do not print commands to be executed (-s). */
142
143int silent_flag;
144
145/* Nonzero means just touch the files
146 that would appear to need remaking (-t) */
147
148int touch_flag;
149
150/* Nonzero means just print what commands would need to be executed,
151 don't actually execute them (-n). */
152
153int just_print_flag;
154
155/* Print debugging info (--debug). */
156
157static struct stringlist *db_flags;
158static int debug_flag = 0;
159
160int db_level = 0;
161
162#ifdef WINDOWS32
163/* Suspend make in main for a short time to allow debugger to attach */
164
165int suspend_flag = 0;
166#endif
167
168/* Environment variables override makefile definitions. */
169
170int env_overrides = 0;
171
172/* Nonzero means ignore status codes returned by commands
173 executed to remake files. Just treat them all as successful (-i). */
174
175int ignore_errors_flag = 0;
176
177/* Nonzero means don't remake anything, just print the data base
178 that results from reading the makefile (-p). */
179
180int print_data_base_flag = 0;
181
182/* Nonzero means don't remake anything; just return a nonzero status
183 if the specified targets are not up to date (-q). */
184
185int question_flag = 0;
186
187/* Nonzero means do not use any of the builtin rules (-r) / variables (-R). */
188
189int no_builtin_rules_flag = 0;
190int no_builtin_variables_flag = 0;
191
192/* Nonzero means keep going even if remaking some file fails (-k). */
193
194int keep_going_flag;
195int default_keep_going_flag = 0;
196
197/* Nonzero means check symlink mtimes. */
198
199int check_symlink_flag = 0;
200
201/* Nonzero means print directory before starting and when done (-w). */
202
203int print_directory_flag = 0;
204
205/* Nonzero means ignore print_directory_flag and never print the directory.
206 This is necessary because print_directory_flag is set implicitly. */
207
208int inhibit_print_directory_flag = 0;
209
210/* Nonzero means print version information. */
211
212int print_version_flag = 0;
213
214/* List of makefiles given with -f switches. */
215
216static struct stringlist *makefiles = 0;
217
218/* Number of job slots (commands that can be run at once). */
219
220unsigned int job_slots = 1;
221unsigned int default_job_slots = 1;
222static unsigned int master_job_slots = 0;
223
224/* Value of job_slots that means no limit. */
225
226static unsigned int inf_jobs = 0;
227
228/* File descriptors for the jobs pipe. */
229
230static struct stringlist *jobserver_fds = 0;
231
232int job_fds[2] = { -1, -1 };
233int job_rfd = -1;
234
235/* Maximum load average at which multiple jobs will be run.
236 Negative values mean unlimited, while zero means limit to
237 zero load (which could be useful to start infinite jobs remotely
238 but one at a time locally). */
239#ifndef NO_FLOAT
240double max_load_average = -1.0;
241double default_load_average = -1.0;
242#else
243int max_load_average = -1;
244int default_load_average = -1;
245#endif
246
247/* List of directories given with -C switches. */
248
249static struct stringlist *directories = 0;
250
251/* List of include directories given with -I switches. */
252
253static struct stringlist *include_directories = 0;
254
255/* List of files given with -o switches. */
256
257static struct stringlist *old_files = 0;
258
259/* List of files given with -W switches. */
260
261static struct stringlist *new_files = 0;
262
263/* If nonzero, we should just print usage and exit. */
264
265static int print_usage_flag = 0;
266
267/* If nonzero, we should print a warning message
268 for each reference to an undefined variable. */
269
270int warn_undefined_variables_flag;
271
272/* If nonzero, always build all targets, regardless of whether
273 they appear out of date or not. */
274
275int always_make_flag = 0;
276
277/* If nonzero, we're in the "try to rebuild makefiles" phase. */
278
279int rebuilding_makefiles = 0;
280
281/* Remember the original value of the SHELL variable, from the environment. */
282
283struct variable shell_var;
284
285
286
287/* The usage output. We write it this way to make life easier for the
288 translators, especially those trying to translate to right-to-left
289 languages like Hebrew. */
290
291static const char *const usage[] =
292 {
293 N_("Options:\n"),
294 N_("\
295 -b, -m Ignored for compatibility.\n"),
296 N_("\
297 -B, --always-make Unconditionally make all targets.\n"),
298 N_("\
299 -C DIRECTORY, --directory=DIRECTORY\n\
300 Change to DIRECTORY before doing anything.\n"),
301 N_("\
302 -d Print lots of debugging information.\n"),
303 N_("\
304 --debug[=FLAGS] Print various types of debugging information.\n"),
305 N_("\
306 -e, --environment-overrides\n\
307 Environment variables override makefiles.\n"),
308 N_("\
309 -f FILE, --file=FILE, --makefile=FILE\n\
310 Read FILE as a makefile.\n"),
311 N_("\
312 -h, --help Print this message and exit.\n"),
313 N_("\
314 -i, --ignore-errors Ignore errors from commands.\n"),
315 N_("\
316 -I DIRECTORY, --include-dir=DIRECTORY\n\
317 Search DIRECTORY for included makefiles.\n"),
318 N_("\
319 -j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg.\n"),
320 N_("\
321 -k, --keep-going Keep going when some targets can't be made.\n"),
322 N_("\
323 -l [N], --load-average[=N], --max-load[=N]\n\
324 Don't start multiple jobs unless load is below N.\n"),
325 N_("\
326 -L, --check-symlink-times Use the latest mtime between symlinks and target.\n"),
327 N_("\
328 -n, --just-print, --dry-run, --recon\n\
329 Don't actually run any commands; just print them.\n"),
330 N_("\
331 -o FILE, --old-file=FILE, --assume-old=FILE\n\
332 Consider FILE to be very old and don't remake it.\n"),
333 N_("\
334 -p, --print-data-base Print make's internal database.\n"),
335 N_("\
336 -q, --question Run no commands; exit status says if up to date.\n"),
337 N_("\
338 -r, --no-builtin-rules Disable the built-in implicit rules.\n"),
339 N_("\
340 -R, --no-builtin-variables Disable the built-in variable settings.\n"),
341 N_("\
342 -s, --silent, --quiet Don't echo commands.\n"),
343 N_("\
344 -S, --no-keep-going, --stop\n\
345 Turns off -k.\n"),
346 N_("\
347 -t, --touch Touch targets instead of remaking them.\n"),
348 N_("\
349 -v, --version Print the version number of make and exit.\n"),
350 N_("\
351 -w, --print-directory Print the current directory.\n"),
352 N_("\
353 --no-print-directory Turn off -w, even if it was turned on implicitly.\n"),
354 N_("\
355 -W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE\n\
356 Consider FILE to be infinitely new.\n"),
357 N_("\
358 --warn-undefined-variables Warn when an undefined variable is referenced.\n"),
359 NULL
360 };
361
362/* The table of command switches. */
363
364static const struct command_switch switches[] =
365 {
366 { 'b', ignore, 0, 0, 0, 0, 0, 0, 0 },
367 { 'B', flag, (char *) &always_make_flag, 1, 1, 0, 0, 0, "always-make" },
368 { 'C', string, (char *) &directories, 0, 0, 0, 0, 0, "directory" },
369 { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0, 0 },
370 { CHAR_MAX+1, string, (char *) &db_flags, 1, 1, 0, "basic", 0, "debug" },
371#ifdef WINDOWS32
372 { 'D', flag, (char *) &suspend_flag, 1, 1, 0, 0, 0, "suspend-for-debug" },
373#endif
374 { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
375 "environment-overrides", },
376 { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0, "file" },
377 { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0, "help" },
378 { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
379 "ignore-errors" },
380 { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
381 "include-dir" },
382 { 'j', positive_int, (char *) &job_slots, 1, 1, 0, (char *) &inf_jobs,
383 (char *) &default_job_slots, "jobs" },
384 { CHAR_MAX+2, string, (char *) &jobserver_fds, 1, 1, 0, 0, 0,
385 "jobserver-fds" },
386 { 'k', flag, (char *) &keep_going_flag, 1, 1, 0, 0,
387 (char *) &default_keep_going_flag, "keep-going" },
388#ifndef NO_FLOAT
389 { 'l', floating, (char *) &max_load_average, 1, 1, 0,
390 (char *) &default_load_average, (char *) &default_load_average,
391 "load-average" },
392#else
393 { 'l', positive_int, (char *) &max_load_average, 1, 1, 0,
394 (char *) &default_load_average, (char *) &default_load_average,
395 "load-average" },
396#endif
397 { 'L', flag, (char *) &check_symlink_flag, 1, 1, 0, 0, 0,
398 "check-symlink-times" },
399 { 'm', ignore, 0, 0, 0, 0, 0, 0, 0 },
400 { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0, "just-print" },
401 { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0, "old-file" },
402 { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
403 "print-data-base" },
404 { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0, "question" },
405 { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
406 "no-builtin-rules" },
407 { 'R', flag, (char *) &no_builtin_variables_flag, 1, 1, 0, 0, 0,
408 "no-builtin-variables" },
409 { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0, "silent" },
410 { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0, 0,
411 (char *) &default_keep_going_flag, "no-keep-going" },
412 { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0, "touch" },
413 { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0, "version" },
414 { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
415 "print-directory" },
416 { CHAR_MAX+3, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
417 "no-print-directory" },
418 { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0, "what-if" },
419 { CHAR_MAX+4, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
420 "warn-undefined-variables" },
421 { 0 }
422 };
423
424/* Secondary long names for options. */
425
426static struct option long_option_aliases[] =
427 {
428 { "quiet", no_argument, 0, 's' },
429 { "stop", no_argument, 0, 'S' },
430 { "new-file", required_argument, 0, 'W' },
431 { "assume-new", required_argument, 0, 'W' },
432 { "assume-old", required_argument, 0, 'o' },
433 { "max-load", optional_argument, 0, 'l' },
434 { "dry-run", no_argument, 0, 'n' },
435 { "recon", no_argument, 0, 'n' },
436 { "makefile", required_argument, 0, 'f' },
437 };
438
439/* List of goal targets. */
440
441static struct dep *goals, *lastgoal;
442
443/* List of variables which were defined on the command line
444 (or, equivalently, in MAKEFLAGS). */
445
446struct command_variable
447 {
448 struct command_variable *next;
449 struct variable *variable;
450 };
451static struct command_variable *command_variables;
452
453
454/* The name we were invoked with. */
455
456char *program;
457
458/* Our current directory before processing any -C options. */
459
460char *directory_before_chdir;
461
462/* Our current directory after processing all -C options. */
463
464char *starting_directory;
465
466/* Value of the MAKELEVEL variable at startup (or 0). */
467
468unsigned int makelevel;
469
470/* First file defined in the makefile whose name does not
471 start with `.'. This is the default to remake if the
472 command line does not specify. */
473
474struct file *default_goal_file;
475
476/* Pointer to the value of the .DEFAULT_GOAL special
477 variable. */
478char ** default_goal_name;
479
480/* Pointer to structure for the file .DEFAULT
481 whose commands are used for any file that has none of its own.
482 This is zero if the makefiles do not define .DEFAULT. */
483
484struct file *default_file;
485
486/* Nonzero if we have seen the magic `.POSIX' target.
487 This turns on pedantic compliance with POSIX.2. */
488
489int posix_pedantic;
490
491/* Negative if we have seen the `.NOTPARALLEL' target with empty dependency list.
492 Zero if no `.NOTPARALLEL' or no file in the dependency list is being executed.
493 Positive when a file in `.NOTPARALLEL' is being made.
494 Nonzero values have the effect of disabeling parallel building. */
495
496int not_parallel;
497
498/* Nonzero if some rule detected clock skew; we keep track so (a) we only
499 print one warning about it during the run, and (b) we can print a final
500 warning at the end of the run. */
501
502int clock_skew_detected;
503
504
505/* Mask of signals that are being caught with fatal_error_signal. */
506
507#ifdef POSIX
508sigset_t fatal_signal_set;
509#else
510# ifdef HAVE_SIGSETMASK
511int fatal_signal_mask;
512# endif
513#endif
514
515#if !defined HAVE_BSD_SIGNAL && !defined bsd_signal
516# if !defined HAVE_SIGACTION
517# define bsd_signal signal
518# else
519typedef RETSIGTYPE (*bsd_signal_ret_t) ();
520
521static bsd_signal_ret_t
522bsd_signal (int sig, bsd_signal_ret_t func)
523{
524 struct sigaction act, oact;
525 act.sa_handler = func;
526 act.sa_flags = SA_RESTART;
527 sigemptyset (&act.sa_mask);
528 sigaddset (&act.sa_mask, sig);
529 if (sigaction (sig, &act, &oact) != 0)
530 return SIG_ERR;
531 return oact.sa_handler;
532}
533# endif
534#endif
535
536static void
537initialize_global_hash_tables (void)
538{
539 init_hash_global_variable_set ();
540 init_hash_files ();
541 hash_init_directories ();
542 hash_init_function_table ();
543}
544
545static struct file *
546enter_command_line_file (char *name)
547{
548 if (name[0] == '\0')
549 fatal (NILF, _("empty string invalid as file name"));
550
551 if (name[0] == '~')
552 {
553 char *expanded = tilde_expand (name);
554 if (expanded != 0)
555 name = expanded; /* Memory leak; I don't care. */
556 }
557
558 /* This is also done in parse_file_seq, so this is redundant
559 for names read from makefiles. It is here for names passed
560 on the command line. */
561 while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
562 {
563 name += 2;
564 while (*name == '/')
565 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
566 ++name;
567 }
568
569 if (*name == '\0')
570 {
571 /* It was all slashes! Move back to the dot and truncate
572 it after the first slash, so it becomes just "./". */
573 do
574 --name;
575 while (name[0] != '.');
576 name[2] = '\0';
577 }
578
579 return enter_file (xstrdup (name));
580}
581
582/* Toggle -d on receipt of SIGUSR1. */
583
584#ifdef SIGUSR1
585static RETSIGTYPE
586debug_signal_handler (int sig UNUSED)
587{
588 db_level = db_level ? DB_NONE : DB_BASIC;
589}
590#endif
591
592static void
593decode_debug_flags (void)
594{
595 char **pp;
596
597 if (debug_flag)
598 db_level = DB_ALL;
599
600 if (!db_flags)
601 return;
602
603 for (pp=db_flags->list; *pp; ++pp)
604 {
605 const char *p = *pp;
606
607 while (1)
608 {
609 switch (tolower (p[0]))
610 {
611 case 'a':
612 db_level |= DB_ALL;
613 break;
614 case 'b':
615 db_level |= DB_BASIC;
616 break;
617 case 'i':
618 db_level |= DB_BASIC | DB_IMPLICIT;
619 break;
620 case 'j':
621 db_level |= DB_JOBS;
622 break;
623 case 'm':
624 db_level |= DB_BASIC | DB_MAKEFILES;
625 break;
626 case 'v':
627 db_level |= DB_BASIC | DB_VERBOSE;
628 break;
629 case 'k':
630 db_level |= DB_KMK;
631 break;
632 default:
633 fatal (NILF, _("unknown debug level specification `%s'"), p);
634 }
635
636 while (*(++p) != '\0')
637 if (*p == ',' || *p == ' ')
638 break;
639
640 if (*p == '\0')
641 break;
642
643 ++p;
644 }
645 }
646}
647
648#ifdef WINDOWS32
649/*
650 * HANDLE runtime exceptions by avoiding a requestor on the GUI. Capture
651 * exception and print it to stderr instead.
652 *
653 * If ! DB_VERBOSE, just print a simple message and exit.
654 * If DB_VERBOSE, print a more verbose message.
655 * If compiled for DEBUG, let exception pass through to GUI so that
656 * debuggers can attach.
657 */
658LONG WINAPI
659handle_runtime_exceptions( struct _EXCEPTION_POINTERS *exinfo )
660{
661 PEXCEPTION_RECORD exrec = exinfo->ExceptionRecord;
662 LPSTR cmdline = GetCommandLine();
663 LPSTR prg = strtok(cmdline, " ");
664 CHAR errmsg[1024];
665#ifdef USE_EVENT_LOG
666 HANDLE hEventSource;
667 LPTSTR lpszStrings[1];
668#endif
669
670 if (! ISDB (DB_VERBOSE))
671 {
672 sprintf(errmsg,
673 _("%s: Interrupt/Exception caught (code = 0x%x, addr = 0x%x)\n"),
674 prg, exrec->ExceptionCode, exrec->ExceptionAddress);
675 fprintf(stderr, errmsg);
676 exit(255);
677 }
678
679 sprintf(errmsg,
680 _("\nUnhandled exception filter called from program %s\nExceptionCode = %x\nExceptionFlags = %x\nExceptionAddress = %x\n"),
681 prg, exrec->ExceptionCode, exrec->ExceptionFlags,
682 exrec->ExceptionAddress);
683
684 if (exrec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION
685 && exrec->NumberParameters >= 2)
686 sprintf(&errmsg[strlen(errmsg)],
687 (exrec->ExceptionInformation[0]
688 ? _("Access violation: write operation at address %x\n")
689 : _("Access violation: read operation at address %x\n")),
690 exrec->ExceptionInformation[1]);
691
692 /* turn this on if we want to put stuff in the event log too */
693#ifdef USE_EVENT_LOG
694 hEventSource = RegisterEventSource(NULL, "GNU Make");
695 lpszStrings[0] = errmsg;
696
697 if (hEventSource != NULL)
698 {
699 ReportEvent(hEventSource, /* handle of event source */
700 EVENTLOG_ERROR_TYPE, /* event type */
701 0, /* event category */
702 0, /* event ID */
703 NULL, /* current user's SID */
704 1, /* strings in lpszStrings */
705 0, /* no bytes of raw data */
706 lpszStrings, /* array of error strings */
707 NULL); /* no raw data */
708
709 (VOID) DeregisterEventSource(hEventSource);
710 }
711#endif
712
713 /* Write the error to stderr too */
714 fprintf(stderr, errmsg);
715
716#ifdef DEBUG
717 return EXCEPTION_CONTINUE_SEARCH;
718#else
719 exit(255);
720 return (255); /* not reached */
721#endif
722}
723
724/*
725 * On WIN32 systems we don't have the luxury of a /bin directory that
726 * is mapped globally to every drive mounted to the system. Since make could
727 * be invoked from any drive, and we don't want to propogate /bin/sh
728 * to every single drive. Allow ourselves a chance to search for
729 * a value for default shell here (if the default path does not exist).
730 */
731
732int
733find_and_set_default_shell (char *token)
734{
735 int sh_found = 0;
736 char *search_token;
737 char *tokend;
738 PATH_VAR(sh_path);
739 extern char *default_shell;
740
741 if (!token)
742 search_token = default_shell;
743 else
744 search_token = token;
745
746
747 /* If the user explicitly requests the DOS cmd shell, obey that request.
748 However, make sure that's what they really want by requiring the value
749 of SHELL either equal, or have a final path element of, "cmd" or
750 "cmd.exe" case-insensitive. */
751 tokend = search_token + strlen (search_token) - 3;
752 if (((tokend == search_token
753 || (tokend > search_token
754 && (tokend[-1] == '/' || tokend[-1] == '\\')))
755 && !strcmpi (tokend, "cmd"))
756 || ((tokend - 4 == search_token
757 || (tokend - 4 > search_token
758 && (tokend[-5] == '/' || tokend[-5] == '\\')))
759 && !strcmpi (tokend - 4, "cmd.exe"))) {
760 batch_mode_shell = 1;
761 unixy_shell = 0;
762 sh_found = 0;
763 } else if (!no_default_sh_exe &&
764 (token == NULL || !strcmp (search_token, default_shell))) {
765 /* no new information, path already set or known */
766 sh_found = 1;
767 } else if (file_exists_p(search_token)) {
768 /* search token path was found */
769 sprintf(sh_path, "%s", search_token);
770 default_shell = xstrdup(w32ify(sh_path,0));
771 DB (DB_VERBOSE,
772 (_("find_and_set_shell setting default_shell = %s\n"), default_shell));
773 sh_found = 1;
774 } else {
775 char *p;
776 struct variable *v = lookup_variable ("PATH", 4);
777
778 /* Search Path for shell */
779 if (v && v->value) {
780 char *ep;
781
782 p = v->value;
783 ep = strchr(p, PATH_SEPARATOR_CHAR);
784
785 while (ep && *ep) {
786 *ep = '\0';
787
788 if (dir_file_exists_p(p, search_token)) {
789 sprintf(sh_path, "%s/%s", p, search_token);
790 default_shell = xstrdup(w32ify(sh_path,0));
791 sh_found = 1;
792 *ep = PATH_SEPARATOR_CHAR;
793
794 /* terminate loop */
795 p += strlen(p);
796 } else {
797 *ep = PATH_SEPARATOR_CHAR;
798 p = ++ep;
799 }
800
801 ep = strchr(p, PATH_SEPARATOR_CHAR);
802 }
803
804 /* be sure to check last element of Path */
805 if (p && *p && dir_file_exists_p(p, search_token)) {
806 sprintf(sh_path, "%s/%s", p, search_token);
807 default_shell = xstrdup(w32ify(sh_path,0));
808 sh_found = 1;
809 }
810
811 if (sh_found)
812 DB (DB_VERBOSE,
813 (_("find_and_set_shell path search set default_shell = %s\n"),
814 default_shell));
815 }
816 }
817
818 /* naive test */
819 if (!unixy_shell && sh_found &&
820 (strstr(default_shell, "sh") || strstr(default_shell, "SH"))) {
821 unixy_shell = 1;
822 batch_mode_shell = 0;
823 }
824
825#ifdef BATCH_MODE_ONLY_SHELL
826 batch_mode_shell = 1;
827#endif
828
829 return (sh_found);
830}
831#endif /* WINDOWS32 */
832
833#ifdef __MSDOS__
834
835static void
836msdos_return_to_initial_directory (void)
837{
838 if (directory_before_chdir)
839 chdir (directory_before_chdir);
840}
841#endif
842
843extern char *mktemp PARAMS ((char *template));
844extern int mkstemp PARAMS ((char *template));
845
846FILE *
847open_tmpfile(char **name, const char *template)
848{
849 int fd;
850
851#if defined HAVE_MKSTEMP || defined HAVE_MKTEMP
852# define TEMPLATE_LEN strlen (template)
853#else
854# define TEMPLATE_LEN L_tmpnam
855#endif
856 *name = xmalloc (TEMPLATE_LEN + 1);
857 strcpy (*name, template);
858
859#if defined HAVE_MKSTEMP && defined HAVE_FDOPEN
860 /* It's safest to use mkstemp(), if we can. */
861 fd = mkstemp (*name);
862 if (fd == -1)
863 return 0;
864 return fdopen (fd, "w");
865#else
866# ifdef HAVE_MKTEMP
867 (void) mktemp (*name);
868# else
869 (void) tmpnam (*name);
870# endif
871
872# ifdef HAVE_FDOPEN
873 /* Can't use mkstemp(), but guard against a race condition. */
874 fd = open (*name, O_CREAT|O_EXCL|O_WRONLY, 0600);
875 if (fd == -1)
876 return 0;
877 return fdopen (fd, "w");
878# else
879 /* Not secure, but what can we do? */
880 return fopen (*name, "w");
881# endif
882#endif
883}
884
885
886#ifdef _AMIGA
887int
888main (int argc, char **argv)
889#else
890int
891main (int argc, char **argv, char **envp)
892#endif
893{
894 static char *stdin_nm = 0;
895 struct file *f;
896 int i;
897 int makefile_status = MAKE_SUCCESS;
898 char **p;
899 struct dep *read_makefiles;
900 PATH_VAR (current_directory);
901#ifdef WINDOWS32
902 char *unix_path = NULL;
903 char *windows32_path = NULL;
904
905 SetUnhandledExceptionFilter(handle_runtime_exceptions);
906
907 /* start off assuming we have no shell */
908 unixy_shell = 0;
909 no_default_sh_exe = 1;
910#endif
911
912#ifdef SET_STACK_SIZE
913 /* Get rid of any avoidable limit on stack size. */
914 {
915 struct rlimit rlim;
916
917 /* Set the stack limit huge so that alloca does not fail. */
918 if (getrlimit (RLIMIT_STACK, &rlim) == 0)
919 {
920 rlim.rlim_cur = rlim.rlim_max;
921 setrlimit (RLIMIT_STACK, &rlim);
922 }
923 }
924#endif
925
926 /* Needed for OS/2 */
927 initialize_main(&argc, &argv);
928
929 default_goal_file = 0;
930 reading_file = 0;
931
932#if defined (__MSDOS__) && !defined (_POSIX_SOURCE)
933 /* Request the most powerful version of `system', to
934 make up for the dumb default shell. */
935 __system_flags = (__system_redirect
936 | __system_use_shell
937 | __system_allow_multiple_cmds
938 | __system_allow_long_cmds
939 | __system_handle_null_commands
940 | __system_emulate_chdir);
941
942#endif
943
944 /* Set up gettext/internationalization support. */
945 setlocale (LC_ALL, "");
946 bindtextdomain (PACKAGE, LOCALEDIR);
947 textdomain (PACKAGE);
948
949#ifdef POSIX
950 sigemptyset (&fatal_signal_set);
951#define ADD_SIG(sig) sigaddset (&fatal_signal_set, sig)
952#else
953#ifdef HAVE_SIGSETMASK
954 fatal_signal_mask = 0;
955#define ADD_SIG(sig) fatal_signal_mask |= sigmask (sig)
956#else
957#define ADD_SIG(sig)
958#endif
959#endif
960
961#define FATAL_SIG(sig) \
962 if (bsd_signal (sig, fatal_error_signal) == SIG_IGN) \
963 bsd_signal (sig, SIG_IGN); \
964 else \
965 ADD_SIG (sig);
966
967#ifdef SIGHUP
968 FATAL_SIG (SIGHUP);
969#endif
970#ifdef SIGQUIT
971 FATAL_SIG (SIGQUIT);
972#endif
973 FATAL_SIG (SIGINT);
974 FATAL_SIG (SIGTERM);
975
976#ifdef __MSDOS__
977 /* Windows 9X delivers FP exceptions in child programs to their
978 parent! We don't want Make to die when a child divides by zero,
979 so we work around that lossage by catching SIGFPE. */
980 FATAL_SIG (SIGFPE);
981#endif
982
983#ifdef SIGDANGER
984 FATAL_SIG (SIGDANGER);
985#endif
986#ifdef SIGXCPU
987 FATAL_SIG (SIGXCPU);
988#endif
989#ifdef SIGXFSZ
990 FATAL_SIG (SIGXFSZ);
991#endif
992
993#undef FATAL_SIG
994
995 /* Do not ignore the child-death signal. This must be done before
996 any children could possibly be created; otherwise, the wait
997 functions won't work on systems with the SVR4 ECHILD brain
998 damage, if our invoker is ignoring this signal. */
999
1000#ifdef HAVE_WAIT_NOHANG
1001# if defined SIGCHLD
1002 (void) bsd_signal (SIGCHLD, SIG_DFL);
1003# endif
1004# if defined SIGCLD && SIGCLD != SIGCHLD
1005 (void) bsd_signal (SIGCLD, SIG_DFL);
1006# endif
1007#endif
1008
1009 /* Make sure stdout is line-buffered. */
1010
1011#ifdef HAVE_SETVBUF
1012# ifdef SETVBUF_REVERSED
1013 setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
1014# else /* setvbuf not reversed. */
1015 /* Some buggy systems lose if we pass 0 instead of allocating ourselves. */
1016 setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
1017# endif /* setvbuf reversed. */
1018#elif HAVE_SETLINEBUF
1019 setlinebuf (stdout);
1020#endif /* setlinebuf missing. */
1021
1022 /* Figure out where this program lives. */
1023
1024 if (argv[0] == 0)
1025 argv[0] = "";
1026 if (argv[0][0] == '\0')
1027 program = "make";
1028 else
1029 {
1030#ifdef VMS
1031 program = strrchr (argv[0], ']');
1032#else
1033 program = strrchr (argv[0], '/');
1034#endif
1035#if defined(__MSDOS__) || defined(__EMX__)
1036 if (program == 0)
1037 program = strrchr (argv[0], '\\');
1038 else
1039 {
1040 /* Some weird environments might pass us argv[0] with
1041 both kinds of slashes; we must find the rightmost. */
1042 char *p = strrchr (argv[0], '\\');
1043 if (p && p > program)
1044 program = p;
1045 }
1046 if (program == 0 && argv[0][1] == ':')
1047 program = argv[0] + 1;
1048#endif
1049#ifdef WINDOWS32
1050 if (program == 0)
1051 {
1052 /* Extract program from full path */
1053 int argv0_len;
1054 char *p = strrchr (argv[0], '\\');
1055 if (!p)
1056 p = argv[0];
1057 argv0_len = strlen(p);
1058 if (argv0_len > 4
1059 && streq (&p[argv0_len - 4], ".exe"))
1060 {
1061 /* Remove .exe extension */
1062 p[argv0_len - 4] = '\0';
1063 /* Increment past the initial '\' */
1064 program = p + 1;
1065 }
1066 }
1067#endif
1068 if (program == 0)
1069 program = argv[0];
1070 else
1071 ++program;
1072 }
1073
1074 /* Set up to access user data (files). */
1075 user_access ();
1076
1077 initialize_global_hash_tables ();
1078
1079 /* Figure out where we are. */
1080
1081#ifdef WINDOWS32
1082 if (getcwd_fs (current_directory, GET_PATH_MAX) == 0)
1083#else
1084 if (getcwd (current_directory, GET_PATH_MAX) == 0)
1085#endif
1086 {
1087#ifdef HAVE_GETCWD
1088 perror_with_name ("getcwd", "");
1089#else
1090 error (NILF, "getwd: %s", current_directory);
1091#endif
1092 current_directory[0] = '\0';
1093 directory_before_chdir = 0;
1094 }
1095 else
1096 directory_before_chdir = xstrdup (current_directory);
1097#ifdef __MSDOS__
1098 /* Make sure we will return to the initial directory, come what may. */
1099 atexit (msdos_return_to_initial_directory);
1100#endif
1101
1102 /* Initialize the special variables. */
1103 define_variable (".VARIABLES", 10, "", o_default, 0)->special = 1;
1104 /* define_variable (".TARGETS", 8, "", o_default, 0)->special = 1; */
1105
1106 /* Set up .FEATURES */
1107 define_variable (".FEATURES", 9,
1108 "target-specific order-only second-expansion",
1109 o_default, 0);
1110#ifdef MAKE_JOBSERVER
1111 do_variable_definition (NILF, ".FEATURES", "jobserver",
1112 o_default, f_append, 0);
1113#endif
1114#ifdef MAKE_SYMLINKS
1115 do_variable_definition (NILF, ".FEATURES", "check-symlink",
1116 o_default, f_append, 0);
1117#endif
1118
1119 /* Read in variables from the environment. It is important that this be
1120 done before $(MAKE) is figured out so its definitions will not be
1121 from the environment. */
1122
1123#ifndef _AMIGA
1124 for (i = 0; envp[i] != 0; ++i)
1125 {
1126 int do_not_define = 0;
1127 char *ep = envp[i];
1128
1129 while (*ep != '\0' && *ep != '=')
1130 ++ep;
1131#ifdef WINDOWS32
1132 if (!unix_path && strneq(envp[i], "PATH=", 5))
1133 unix_path = ep+1;
1134 else if (!strnicmp(envp[i], "Path=", 5)) {
1135 do_not_define = 1; /* it gets defined after loop exits */
1136 if (!windows32_path)
1137 windows32_path = ep+1;
1138 }
1139#endif
1140 /* The result of pointer arithmetic is cast to unsigned int for
1141 machines where ptrdiff_t is a different size that doesn't widen
1142 the same. */
1143 if (!do_not_define)
1144 {
1145 struct variable *v;
1146
1147 v = define_variable (envp[i], (unsigned int) (ep - envp[i]),
1148 ep + 1, o_env, 1);
1149 /* Force exportation of every variable culled from the environment.
1150 We used to rely on target_environment's v_default code to do this.
1151 But that does not work for the case where an environment variable
1152 is redefined in a makefile with `override'; it should then still
1153 be exported, because it was originally in the environment. */
1154 v->export = v_export;
1155
1156 /* Another wrinkle is that POSIX says the value of SHELL set in the
1157 makefile should not change the value of SHELL given to
1158 subprocesses, which seems silly to me but... */
1159 if (strncmp (envp[i], "SHELL=", 6) == 0)
1160 {
1161#ifndef __MSDOS__
1162 v->export = v_noexport;
1163#endif
1164 shell_var.name = "SHELL";
1165 shell_var.value = xstrdup (ep + 1);
1166 }
1167 }
1168 }
1169#ifdef WINDOWS32
1170 /* If we didn't find a correctly spelled PATH we define PATH as
1171 * either the first mispelled value or an empty string
1172 */
1173 if (!unix_path)
1174 define_variable("PATH", 4,
1175 windows32_path ? windows32_path : "",
1176 o_env, 1)->export = v_export;
1177#endif
1178#else /* For Amiga, read the ENV: device, ignoring all dirs */
1179 {
1180 BPTR env, file, old;
1181 char buffer[1024];
1182 int len;
1183 __aligned struct FileInfoBlock fib;
1184
1185 env = Lock ("ENV:", ACCESS_READ);
1186 if (env)
1187 {
1188 old = CurrentDir (DupLock(env));
1189 Examine (env, &fib);
1190
1191 while (ExNext (env, &fib))
1192 {
1193 if (fib.fib_DirEntryType < 0) /* File */
1194 {
1195 /* Define an empty variable. It will be filled in
1196 variable_lookup(). Makes startup quite a bit
1197 faster. */
1198 define_variable (fib.fib_FileName,
1199 strlen (fib.fib_FileName),
1200 "", o_env, 1)->export = v_export;
1201 }
1202 }
1203 UnLock (env);
1204 UnLock(CurrentDir(old));
1205 }
1206 }
1207#endif
1208
1209 /* Decode the switches. */
1210
1211 decode_env_switches ("MAKEFLAGS", 9);
1212#if 0
1213 /* People write things like:
1214 MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
1215 and we set the -p, -i and -e switches. Doesn't seem quite right. */
1216 decode_env_switches ("MFLAGS", 6);
1217#endif
1218 decode_switches (argc, argv, 0);
1219#ifdef WINDOWS32
1220 if (suspend_flag) {
1221 fprintf(stderr, "%s (pid = %d)\n", argv[0], GetCurrentProcessId());
1222 fprintf(stderr, _("%s is suspending for 30 seconds..."), argv[0]);
1223 Sleep(30 * 1000);
1224 fprintf(stderr, _("done sleep(30). Continuing.\n"));
1225 }
1226#endif
1227
1228 decode_debug_flags ();
1229
1230 /* Print version information. */
1231
1232 if (print_version_flag || print_data_base_flag || db_level)
1233 print_version ();
1234
1235 /* `make --version' is supposed to just print the version and exit. */
1236 if (print_version_flag)
1237 die (0);
1238
1239#ifndef VMS
1240 /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
1241 (If it is a relative pathname with a slash, prepend our directory name
1242 so the result will run the same program regardless of the current dir.
1243 If it is a name with no slash, we can only hope that PATH did not
1244 find it in the current directory.) */
1245#ifdef WINDOWS32
1246 /*
1247 * Convert from backslashes to forward slashes for
1248 * programs like sh which don't like them. Shouldn't
1249 * matter if the path is one way or the other for
1250 * CreateProcess().
1251 */
1252 if (strpbrk(argv[0], "/:\\") ||
1253 strstr(argv[0], "..") ||
1254 strneq(argv[0], "//", 2))
1255 argv[0] = xstrdup(w32ify(argv[0],1));
1256#else /* WINDOWS32 */
1257#if defined (__MSDOS__) || defined (__EMX__)
1258 if (strchr (argv[0], '\\'))
1259 {
1260 char *p;
1261
1262 argv[0] = xstrdup (argv[0]);
1263 for (p = argv[0]; *p; p++)
1264 if (*p == '\\')
1265 *p = '/';
1266 }
1267 /* If argv[0] is not in absolute form, prepend the current
1268 directory. This can happen when Make is invoked by another DJGPP
1269 program that uses a non-absolute name. */
1270 if (current_directory[0] != '\0'
1271 && argv[0] != 0
1272 && (argv[0][0] != '/' && (argv[0][0] == '\0' || argv[0][1] != ':'))
1273#ifdef __EMX__
1274 /* do not prepend cwd if argv[0] contains no '/', e.g. "make" */
1275 && (strchr (argv[0], '/') != 0 || strchr (argv[0], '\\') != 0)
1276# endif
1277 )
1278 argv[0] = concat (current_directory, "/", argv[0]);
1279#else /* !__MSDOS__ */
1280 if (current_directory[0] != '\0'
1281 && argv[0] != 0 && argv[0][0] != '/' && strchr (argv[0], '/') != 0)
1282 argv[0] = concat (current_directory, "/", argv[0]);
1283#endif /* !__MSDOS__ */
1284#endif /* WINDOWS32 */
1285#endif
1286
1287 /* The extra indirection through $(MAKE_COMMAND) is done
1288 for hysterical raisins. */
1289 (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
1290 (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
1291
1292 if (command_variables != 0)
1293 {
1294 struct command_variable *cv;
1295 struct variable *v;
1296 unsigned int len = 0;
1297 char *value, *p;
1298
1299 /* Figure out how much space will be taken up by the command-line
1300 variable definitions. */
1301 for (cv = command_variables; cv != 0; cv = cv->next)
1302 {
1303 v = cv->variable;
1304 len += 2 * strlen (v->name);
1305 if (! v->recursive)
1306 ++len;
1307 ++len;
1308 len += 2 * strlen (v->value);
1309 ++len;
1310 }
1311
1312 /* Now allocate a buffer big enough and fill it. */
1313 p = value = (char *) alloca (len);
1314 for (cv = command_variables; cv != 0; cv = cv->next)
1315 {
1316 v = cv->variable;
1317 p = quote_for_env (p, v->name);
1318 if (! v->recursive)
1319 *p++ = ':';
1320 *p++ = '=';
1321 p = quote_for_env (p, v->value);
1322 *p++ = ' ';
1323 }
1324 p[-1] = '\0'; /* Kill the final space and terminate. */
1325
1326 /* Define an unchangeable variable with a name that no POSIX.2
1327 makefile could validly use for its own variable. */
1328 (void) define_variable ("-*-command-variables-*-", 23,
1329 value, o_automatic, 0);
1330
1331 /* Define the variable; this will not override any user definition.
1332 Normally a reference to this variable is written into the value of
1333 MAKEFLAGS, allowing the user to override this value to affect the
1334 exported value of MAKEFLAGS. In POSIX-pedantic mode, we cannot
1335 allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
1336 a reference to this hidden variable is written instead. */
1337 (void) define_variable ("MAKEOVERRIDES", 13,
1338 "${-*-command-variables-*-}", o_env, 1);
1339 }
1340
1341 /* If there were -C flags, move ourselves about. */
1342 if (directories != 0)
1343 for (i = 0; directories->list[i] != 0; ++i)
1344 {
1345 char *dir = directories->list[i];
1346 char *expanded = 0;
1347 if (dir[0] == '~')
1348 {
1349 expanded = tilde_expand (dir);
1350 if (expanded != 0)
1351 dir = expanded;
1352 }
1353#ifdef WINDOWS32
1354 /* WINDOWS32 chdir() doesn't work if the directory has a trailing '/'
1355 But allow -C/ just in case someone wants that. */
1356 {
1357 char *p = dir + strlen (dir) - 1;
1358 while (p > dir && (p[0] == '/' || p[0] == '\\'))
1359 --p;
1360 p[1] = '\0';
1361 }
1362#endif
1363 if (chdir (dir) < 0)
1364 pfatal_with_name (dir);
1365 if (expanded)
1366 free (expanded);
1367 }
1368
1369#ifdef WINDOWS32
1370 /*
1371 * THIS BLOCK OF CODE MUST COME AFTER chdir() CALL ABOVE IN ORDER
1372 * TO NOT CONFUSE THE DEPENDENCY CHECKING CODE IN implicit.c.
1373 *
1374 * The functions in dir.c can incorrectly cache information for "."
1375 * before we have changed directory and this can cause file
1376 * lookups to fail because the current directory (.) was pointing
1377 * at the wrong place when it was first evaluated.
1378 */
1379 no_default_sh_exe = !find_and_set_default_shell(NULL);
1380
1381#endif /* WINDOWS32 */
1382 /* Figure out the level of recursion. */
1383 {
1384 struct variable *v = lookup_variable (MAKELEVEL_NAME, MAKELEVEL_LENGTH);
1385 if (v != 0 && v->value[0] != '\0' && v->value[0] != '-')
1386 makelevel = (unsigned int) atoi (v->value);
1387 else
1388 makelevel = 0;
1389 }
1390
1391 /* Except under -s, always do -w in sub-makes and under -C. */
1392 if (!silent_flag && (directories != 0 || makelevel > 0))
1393 print_directory_flag = 1;
1394
1395 /* Let the user disable that with --no-print-directory. */
1396 if (inhibit_print_directory_flag)
1397 print_directory_flag = 0;
1398
1399 /* If -R was given, set -r too (doesn't make sense otherwise!) */
1400 if (no_builtin_variables_flag)
1401 no_builtin_rules_flag = 1;
1402
1403 /* Construct the list of include directories to search. */
1404
1405 construct_include_path (include_directories == 0 ? (char **) 0
1406 : include_directories->list);
1407
1408 /* Figure out where we are now, after chdir'ing. */
1409 if (directories == 0)
1410 /* We didn't move, so we're still in the same place. */
1411 starting_directory = current_directory;
1412 else
1413 {
1414#ifdef WINDOWS32
1415 if (getcwd_fs (current_directory, GET_PATH_MAX) == 0)
1416#else
1417 if (getcwd (current_directory, GET_PATH_MAX) == 0)
1418#endif
1419 {
1420#ifdef HAVE_GETCWD
1421 perror_with_name ("getcwd", "");
1422#else
1423 error (NILF, "getwd: %s", current_directory);
1424#endif
1425 starting_directory = 0;
1426 }
1427 else
1428 starting_directory = current_directory;
1429 }
1430
1431 (void) define_variable ("CURDIR", 6, current_directory, o_default, 0);
1432
1433 /* Read any stdin makefiles into temporary files. */
1434
1435 if (makefiles != 0)
1436 {
1437 register unsigned int i;
1438 for (i = 0; i < makefiles->idx; ++i)
1439 if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
1440 {
1441 /* This makefile is standard input. Since we may re-exec
1442 and thus re-read the makefiles, we read standard input
1443 into a temporary file and read from that. */
1444 FILE *outfile;
1445 char *template, *tmpdir;
1446
1447 if (stdin_nm)
1448 fatal (NILF, _("Makefile from standard input specified twice."));
1449
1450#ifdef VMS
1451# define DEFAULT_TMPDIR "sys$scratch:"
1452#else
1453# ifdef P_tmpdir
1454# define DEFAULT_TMPDIR P_tmpdir
1455# else
1456# define DEFAULT_TMPDIR "/tmp"
1457# endif
1458#endif
1459#define DEFAULT_TMPFILE "GmXXXXXX"
1460
1461 if (((tmpdir = getenv ("TMPDIR")) == NULL || *tmpdir == '\0')
1462#if defined (__MSDOS__) || defined (WINDOWS32) || defined (__EMX__)
1463 /* These are also used commonly on these platforms. */
1464 && ((tmpdir = getenv ("TEMP")) == NULL || *tmpdir == '\0')
1465 && ((tmpdir = getenv ("TMP")) == NULL || *tmpdir == '\0')
1466#endif
1467 )
1468 tmpdir = DEFAULT_TMPDIR;
1469
1470 template = (char *) alloca (strlen (tmpdir)
1471 + sizeof (DEFAULT_TMPFILE) + 1);
1472 strcpy (template, tmpdir);
1473
1474#ifdef HAVE_DOS_PATHS
1475 if (strchr ("/\\", template[strlen (template) - 1]) == NULL)
1476 strcat (template, "/");
1477#else
1478# ifndef VMS
1479 if (template[strlen (template) - 1] != '/')
1480 strcat (template, "/");
1481# endif /* !VMS */
1482#endif /* !HAVE_DOS_PATHS */
1483
1484 strcat (template, DEFAULT_TMPFILE);
1485 outfile = open_tmpfile (&stdin_nm, template);
1486 if (outfile == 0)
1487 pfatal_with_name (_("fopen (temporary file)"));
1488 while (!feof (stdin) && ! ferror (stdin))
1489 {
1490 char buf[2048];
1491 unsigned int n = fread (buf, 1, sizeof (buf), stdin);
1492 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
1493 pfatal_with_name (_("fwrite (temporary file)"));
1494 }
1495 (void) fclose (outfile);
1496
1497 /* Replace the name that read_all_makefiles will
1498 see with the name of the temporary file. */
1499 makefiles->list[i] = xstrdup (stdin_nm);
1500
1501 /* Make sure the temporary file will not be remade. */
1502 f = enter_file (stdin_nm);
1503 f->updated = 1;
1504 f->update_status = 0;
1505 f->command_state = cs_finished;
1506 /* Can't be intermediate, or it'll be removed too early for
1507 make re-exec. */
1508 f->intermediate = 0;
1509 f->dontcare = 0;
1510 }
1511 }
1512
1513#ifndef __EMX__ /* Don't use a SIGCHLD handler for OS/2 */
1514#if defined(MAKE_JOBSERVER) || !defined(HAVE_WAIT_NOHANG)
1515 /* Set up to handle children dying. This must be done before
1516 reading in the makefiles so that `shell' function calls will work.
1517
1518 If we don't have a hanging wait we have to fall back to old, broken
1519 functionality here and rely on the signal handler and counting
1520 children.
1521
1522 If we're using the jobs pipe we need a signal handler so that
1523 SIGCHLD is not ignored; we need it to interrupt the read(2) of the
1524 jobserver pipe in job.c if we're waiting for a token.
1525
1526 If none of these are true, we don't need a signal handler at all. */
1527 {
1528 extern RETSIGTYPE child_handler PARAMS ((int sig));
1529# if defined SIGCHLD
1530 bsd_signal (SIGCHLD, child_handler);
1531# endif
1532# if defined SIGCLD && SIGCLD != SIGCHLD
1533 bsd_signal (SIGCLD, child_handler);
1534# endif
1535 }
1536#endif
1537#endif
1538
1539 /* Let the user send us SIGUSR1 to toggle the -d flag during the run. */
1540#ifdef SIGUSR1
1541 bsd_signal (SIGUSR1, debug_signal_handler);
1542#endif
1543
1544 /* Define the initial list of suffixes for old-style rules. */
1545
1546 set_default_suffixes ();
1547
1548 /* Define the file rules for the built-in suffix rules. These will later
1549 be converted into pattern rules. We used to do this in
1550 install_default_implicit_rules, but since that happens after reading
1551 makefiles, it results in the built-in pattern rules taking precedence
1552 over makefile-specified suffix rules, which is wrong. */
1553
1554 install_default_suffix_rules ();
1555
1556 /* Define some internal and special variables. */
1557
1558 define_automatic_variables ();
1559
1560 /* Set up the MAKEFLAGS and MFLAGS variables
1561 so makefiles can look at them. */
1562
1563 define_makeflags (0, 0);
1564
1565 /* Define the default variables. */
1566 define_default_variables ();
1567
1568 default_file = enter_file (".DEFAULT");
1569
1570 {
1571 struct variable *v = define_variable (".DEFAULT_GOAL", 13, "", o_file, 0);
1572 default_goal_name = &v->value;
1573 }
1574
1575 /* Read all the makefiles. */
1576
1577 read_makefiles
1578 = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
1579
1580#ifdef WINDOWS32
1581 /* look one last time after reading all Makefiles */
1582 if (no_default_sh_exe)
1583 no_default_sh_exe = !find_and_set_default_shell(NULL);
1584
1585 if (no_default_sh_exe && job_slots != 1) {
1586 error (NILF, _("Do not specify -j or --jobs if sh.exe is not available."));
1587 error (NILF, _("Resetting make for single job mode."));
1588 job_slots = 1;
1589 }
1590#endif /* WINDOWS32 */
1591
1592#if defined (__MSDOS__) || defined (__EMX__)
1593 /* We need to know what kind of shell we will be using. */
1594 {
1595 extern int _is_unixy_shell (const char *_path);
1596 struct variable *shv = lookup_variable ("SHELL", 5);
1597 extern int unixy_shell;
1598 extern char *default_shell;
1599
1600 if (shv && *shv->value)
1601 {
1602 char *shell_path = recursively_expand(shv);
1603
1604 if (shell_path && _is_unixy_shell (shell_path))
1605 unixy_shell = 1;
1606 else
1607 unixy_shell = 0;
1608 if (shell_path)
1609 default_shell = shell_path;
1610 }
1611 }
1612#endif /* __MSDOS__ || __EMX__ */
1613
1614 /* Decode switches again, in case the variables were set by the makefile. */
1615 decode_env_switches ("MAKEFLAGS", 9);
1616#if 0
1617 decode_env_switches ("MFLAGS", 6);
1618#endif
1619
1620#if defined (__MSDOS__) || defined (__EMX__)
1621 if (job_slots != 1
1622# ifdef __EMX__
1623 && _osmode != OS2_MODE /* turn off -j if we are in DOS mode */
1624# endif
1625 )
1626 {
1627 error (NILF,
1628 _("Parallel jobs (-j) are not supported on this platform."));
1629 error (NILF, _("Resetting to single job (-j1) mode."));
1630 job_slots = 1;
1631 }
1632#endif
1633
1634#ifdef MAKE_JOBSERVER
1635 /* If the jobserver-fds option is seen, make sure that -j is reasonable. */
1636
1637 if (jobserver_fds)
1638 {
1639 char *cp;
1640 unsigned int ui;
1641
1642 for (ui=1; ui < jobserver_fds->idx; ++ui)
1643 if (!streq (jobserver_fds->list[0], jobserver_fds->list[ui]))
1644 fatal (NILF, _("internal error: multiple --jobserver-fds options"));
1645
1646 /* Now parse the fds string and make sure it has the proper format. */
1647
1648 cp = jobserver_fds->list[0];
1649
1650 if (sscanf (cp, "%d,%d", &job_fds[0], &job_fds[1]) != 2)
1651 fatal (NILF,
1652 _("internal error: invalid --jobserver-fds string `%s'"), cp);
1653
1654 /* The combination of a pipe + !job_slots means we're using the
1655 jobserver. If !job_slots and we don't have a pipe, we can start
1656 infinite jobs. If we see both a pipe and job_slots >0 that means the
1657 user set -j explicitly. This is broken; in this case obey the user
1658 (ignore the jobserver pipe for this make) but print a message. */
1659
1660 if (job_slots > 0)
1661 error (NILF,
1662 _("warning: -jN forced in submake: disabling jobserver mode."));
1663
1664 /* Create a duplicate pipe, that will be closed in the SIGCHLD
1665 handler. If this fails with EBADF, the parent has closed the pipe
1666 on us because it didn't think we were a submake. If so, print a
1667 warning then default to -j1. */
1668
1669 else if ((job_rfd = dup (job_fds[0])) < 0)
1670 {
1671 if (errno != EBADF)
1672 pfatal_with_name (_("dup jobserver"));
1673
1674 error (NILF,
1675 _("warning: jobserver unavailable: using -j1. Add `+' to parent make rule."));
1676 job_slots = 1;
1677 }
1678
1679 if (job_slots > 0)
1680 {
1681 close (job_fds[0]);
1682 close (job_fds[1]);
1683 job_fds[0] = job_fds[1] = -1;
1684 free (jobserver_fds->list);
1685 free (jobserver_fds);
1686 jobserver_fds = 0;
1687 }
1688 }
1689
1690 /* If we have >1 slot but no jobserver-fds, then we're a top-level make.
1691 Set up the pipe and install the fds option for our children. */
1692
1693 if (job_slots > 1)
1694 {
1695 char c = '+';
1696
1697 if (pipe (job_fds) < 0 || (job_rfd = dup (job_fds[0])) < 0)
1698 pfatal_with_name (_("creating jobs pipe"));
1699
1700 /* Every make assumes that it always has one job it can run. For the
1701 submakes it's the token they were given by their parent. For the
1702 top make, we just subtract one from the number the user wants. We
1703 want job_slots to be 0 to indicate we're using the jobserver. */
1704
1705 master_job_slots = job_slots;
1706
1707 while (--job_slots)
1708 {
1709 int r;
1710
1711 EINTRLOOP (r, write (job_fds[1], &c, 1));
1712 if (r != 1)
1713 pfatal_with_name (_("init jobserver pipe"));
1714 }
1715
1716 /* Fill in the jobserver_fds struct for our children. */
1717
1718 jobserver_fds = (struct stringlist *)
1719 xmalloc (sizeof (struct stringlist));
1720 jobserver_fds->list = (char **) xmalloc (sizeof (char *));
1721 jobserver_fds->list[0] = xmalloc ((sizeof ("1024")*2)+1);
1722
1723 sprintf (jobserver_fds->list[0], "%d,%d", job_fds[0], job_fds[1]);
1724 jobserver_fds->idx = 1;
1725 jobserver_fds->max = 1;
1726 }
1727#endif
1728
1729#ifndef MAKE_SYMLINKS
1730 if (check_symlink_flag)
1731 {
1732 error (NILF, _("Symbolic links not supported: disabling -L."));
1733 check_symlink_flag = 0;
1734 }
1735#endif
1736
1737 /* Set up MAKEFLAGS and MFLAGS again, so they will be right. */
1738
1739 define_makeflags (1, 0);
1740
1741 /* Make each `struct dep' point at the `struct file' for the file
1742 depended on. Also do magic for special targets. */
1743
1744 snap_deps ();
1745
1746 /* Convert old-style suffix rules to pattern rules. It is important to
1747 do this before installing the built-in pattern rules below, so that
1748 makefile-specified suffix rules take precedence over built-in pattern
1749 rules. */
1750
1751 convert_to_pattern ();
1752
1753 /* Install the default implicit pattern rules.
1754 This used to be done before reading the makefiles.
1755 But in that case, built-in pattern rules were in the chain
1756 before user-defined ones, so they matched first. */
1757
1758 install_default_implicit_rules ();
1759
1760 /* Compute implicit rule limits. */
1761
1762 count_implicit_rule_limits ();
1763
1764 /* Construct the listings of directories in VPATH lists. */
1765
1766 build_vpath_lists ();
1767
1768 /* Mark files given with -o flags as very old
1769 and as having been updated already, and files given with -W flags as
1770 brand new (time-stamp as far as possible into the future). */
1771
1772 if (old_files != 0)
1773 for (p = old_files->list; *p != 0; ++p)
1774 {
1775 f = enter_command_line_file (*p);
1776 f->last_mtime = f->mtime_before_update = OLD_MTIME;
1777 f->updated = 1;
1778 f->update_status = 0;
1779 f->command_state = cs_finished;
1780 }
1781
1782 if (new_files != 0)
1783 {
1784 for (p = new_files->list; *p != 0; ++p)
1785 {
1786 f = enter_command_line_file (*p);
1787 f->last_mtime = f->mtime_before_update = NEW_MTIME;
1788 }
1789 }
1790
1791 /* Initialize the remote job module. */
1792 remote_setup ();
1793
1794 if (read_makefiles != 0)
1795 {
1796 /* Update any makefiles if necessary. */
1797
1798 FILE_TIMESTAMP *makefile_mtimes = 0;
1799 unsigned int mm_idx = 0;
1800 char **nargv = argv;
1801 int nargc = argc;
1802 int orig_db_level = db_level;
1803 int status;
1804
1805 if (! ISDB (DB_MAKEFILES))
1806 db_level = DB_NONE;
1807
1808 DB (DB_BASIC, (_("Updating makefiles....\n")));
1809
1810 /* Remove any makefiles we don't want to try to update.
1811 Also record the current modtimes so we can compare them later. */
1812 {
1813 register struct dep *d, *last;
1814 last = 0;
1815 d = read_makefiles;
1816 while (d != 0)
1817 {
1818 register struct file *f = d->file;
1819 if (f->double_colon)
1820 for (f = f->double_colon; f != NULL; f = f->prev)
1821 {
1822 if (f->deps == 0 && f->cmds != 0)
1823 {
1824 /* This makefile is a :: target with commands, but
1825 no dependencies. So, it will always be remade.
1826 This might well cause an infinite loop, so don't
1827 try to remake it. (This will only happen if
1828 your makefiles are written exceptionally
1829 stupidly; but if you work for Athena, that's how
1830 you write your makefiles.) */
1831
1832 DB (DB_VERBOSE,
1833 (_("Makefile `%s' might loop; not remaking it.\n"),
1834 f->name));
1835
1836 if (last == 0)
1837 read_makefiles = d->next;
1838 else
1839 last->next = d->next;
1840
1841 /* Free the storage. */
1842 free ((char *) d);
1843
1844 d = last == 0 ? read_makefiles : last->next;
1845
1846 break;
1847 }
1848 }
1849 if (f == NULL || !f->double_colon)
1850 {
1851 makefile_mtimes = (FILE_TIMESTAMP *)
1852 xrealloc ((char *) makefile_mtimes,
1853 (mm_idx + 1) * sizeof (FILE_TIMESTAMP));
1854 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
1855 last = d;
1856 d = d->next;
1857 }
1858 }
1859 }
1860
1861 /* Set up `MAKEFLAGS' specially while remaking makefiles. */
1862 define_makeflags (1, 1);
1863
1864 rebuilding_makefiles = 1;
1865 status = update_goal_chain (read_makefiles);
1866 rebuilding_makefiles = 0;
1867
1868 switch (status)
1869 {
1870 case 1:
1871 /* The only way this can happen is if the user specified -q and asked
1872 * for one of the makefiles to be remade as a target on the command
1873 * line. Since we're not actually updating anything with -q we can
1874 * treat this as "did nothing".
1875 */
1876
1877 case -1:
1878 /* Did nothing. */
1879 break;
1880
1881 case 2:
1882 /* Failed to update. Figure out if we care. */
1883 {
1884 /* Nonzero if any makefile was successfully remade. */
1885 int any_remade = 0;
1886 /* Nonzero if any makefile we care about failed
1887 in updating or could not be found at all. */
1888 int any_failed = 0;
1889 unsigned int i;
1890 struct dep *d;
1891
1892 for (i = 0, d = read_makefiles; d != 0; ++i, d = d->next)
1893 {
1894 /* Reset the considered flag; we may need to look at the file
1895 again to print an error. */
1896 d->file->considered = 0;
1897
1898 if (d->file->updated)
1899 {
1900 /* This makefile was updated. */
1901 if (d->file->update_status == 0)
1902 {
1903 /* It was successfully updated. */
1904 any_remade |= (file_mtime_no_search (d->file)
1905 != makefile_mtimes[i]);
1906 }
1907 else if (! (d->changed & RM_DONTCARE))
1908 {
1909 FILE_TIMESTAMP mtime;
1910 /* The update failed and this makefile was not
1911 from the MAKEFILES variable, so we care. */
1912 error (NILF, _("Failed to remake makefile `%s'."),
1913 d->file->name);
1914 mtime = file_mtime_no_search (d->file);
1915 any_remade |= (mtime != NONEXISTENT_MTIME
1916 && mtime != makefile_mtimes[i]);
1917 makefile_status = MAKE_FAILURE;
1918 }
1919 }
1920 else
1921 /* This makefile was not found at all. */
1922 if (! (d->changed & RM_DONTCARE))
1923 {
1924 /* This is a makefile we care about. See how much. */
1925 if (d->changed & RM_INCLUDED)
1926 /* An included makefile. We don't need
1927 to die, but we do want to complain. */
1928 error (NILF,
1929 _("Included makefile `%s' was not found."),
1930 dep_name (d));
1931 else
1932 {
1933 /* A normal makefile. We must die later. */
1934 error (NILF, _("Makefile `%s' was not found"),
1935 dep_name (d));
1936 any_failed = 1;
1937 }
1938 }
1939 }
1940 /* Reset this to empty so we get the right error message below. */
1941 read_makefiles = 0;
1942
1943 if (any_remade)
1944 goto re_exec;
1945 if (any_failed)
1946 die (2);
1947 break;
1948 }
1949
1950 case 0:
1951 re_exec:
1952 /* Updated successfully. Re-exec ourselves. */
1953
1954 remove_intermediates (0);
1955
1956 if (print_data_base_flag)
1957 print_data_base ();
1958
1959 log_working_directory (0);
1960
1961 if (makefiles != 0)
1962 {
1963 /* These names might have changed. */
1964 int i, j = 0;
1965 for (i = 1; i < argc; ++i)
1966 if (strneq (argv[i], "-f", 2)) /* XXX */
1967 {
1968 char *p = &argv[i][2];
1969 if (*p == '\0')
1970 argv[++i] = makefiles->list[j];
1971 else
1972 argv[i] = concat ("-f", makefiles->list[j], "");
1973 ++j;
1974 }
1975 }
1976
1977 /* Add -o option for the stdin temporary file, if necessary. */
1978 if (stdin_nm)
1979 {
1980 nargv = (char **) xmalloc ((nargc + 2) * sizeof (char *));
1981 bcopy ((char *) argv, (char *) nargv, argc * sizeof (char *));
1982 nargv[nargc++] = concat ("-o", stdin_nm, "");
1983 nargv[nargc] = 0;
1984 }
1985
1986 if (directories != 0 && directories->idx > 0)
1987 {
1988 char bad;
1989 if (directory_before_chdir != 0)
1990 {
1991 if (chdir (directory_before_chdir) < 0)
1992 {
1993 perror_with_name ("chdir", "");
1994 bad = 1;
1995 }
1996 else
1997 bad = 0;
1998 }
1999 else
2000 bad = 1;
2001 if (bad)
2002 fatal (NILF, _("Couldn't change back to original directory."));
2003 }
2004
2005#ifndef _AMIGA
2006 for (p = environ; *p != 0; ++p)
2007 if (strneq (*p, MAKELEVEL_NAME, MAKELEVEL_LENGTH)
2008 && (*p)[MAKELEVEL_LENGTH] == '=')
2009 {
2010 /* The SGI compiler apparently can't understand
2011 the concept of storing the result of a function
2012 in something other than a local variable. */
2013 char *sgi_loses;
2014 sgi_loses = (char *) alloca (40);
2015 *p = sgi_loses;
2016 sprintf (*p, "%s=%u", MAKELEVEL_NAME, makelevel);
2017 break;
2018 }
2019#else /* AMIGA */
2020 {
2021 char buffer[256];
2022 int len;
2023
2024 len = GetVar (MAKELEVEL_NAME, buffer, sizeof (buffer), GVF_GLOBAL_ONLY);
2025
2026 if (len != -1)
2027 {
2028 sprintf (buffer, "%u", makelevel);
2029 SetVar (MAKELEVEL_NAME, buffer, -1, GVF_GLOBAL_ONLY);
2030 }
2031 }
2032#endif
2033
2034 if (ISDB (DB_BASIC))
2035 {
2036 char **p;
2037 fputs (_("Re-executing:"), stdout);
2038 for (p = nargv; *p != 0; ++p)
2039 printf (" %s", *p);
2040 putchar ('\n');
2041 }
2042
2043 fflush (stdout);
2044 fflush (stderr);
2045
2046 /* Close the dup'd jobserver pipe if we opened one. */
2047 if (job_rfd >= 0)
2048 close (job_rfd);
2049
2050#ifdef _AMIGA
2051 exec_command (nargv);
2052 exit (0);
2053#elif defined (__EMX__)
2054 {
2055 /* It is not possible to use execve() here because this
2056 would cause the parent process to be terminated with
2057 exit code 0 before the child process has been terminated.
2058 Therefore it may be the best solution simply to spawn the
2059 child process including all file handles and to wait for its
2060 termination. */
2061 int pid;
2062 int status;
2063 pid = child_execute_job (0, 1, nargv, environ, NULL);
2064
2065 /* is this loop really necessary? */
2066 do {
2067 pid = wait (&status);
2068 } while (pid <= 0);
2069 /* use the exit code of the child process */
2070 exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
2071 }
2072#else
2073 exec_command (nargv, environ);
2074#endif
2075 /* NOTREACHED */
2076
2077 default:
2078#define BOGUS_UPDATE_STATUS 0
2079 assert (BOGUS_UPDATE_STATUS);
2080 break;
2081 }
2082
2083 db_level = orig_db_level;
2084
2085 /* Free the makefile mtimes (if we allocated any). */
2086 if (makefile_mtimes)
2087 free ((char *) makefile_mtimes);
2088 }
2089
2090 /* Set up `MAKEFLAGS' again for the normal targets. */
2091 define_makeflags (1, 0);
2092
2093 /* If there is a temp file from reading a makefile from stdin, get rid of
2094 it now. */
2095 if (stdin_nm && unlink (stdin_nm) < 0 && errno != ENOENT)
2096 perror_with_name (_("unlink (temporary file): "), stdin_nm);
2097
2098 {
2099 int status;
2100
2101 /* If there were no command-line goals, use the default. */
2102 if (goals == 0)
2103 {
2104 if (**default_goal_name != '\0')
2105 {
2106 if (default_goal_file == 0 ||
2107 strcmp (*default_goal_name, default_goal_file->name) != 0)
2108 {
2109 default_goal_file = lookup_file (*default_goal_name);
2110
2111 /* In case user set .DEFAULT_GOAL to a non-existent target
2112 name let's just enter this name into the table and let
2113 the standard logic sort it out. */
2114 if (default_goal_file == 0)
2115 {
2116 struct nameseq *ns;
2117 char *p = *default_goal_name;
2118
2119 ns = multi_glob (
2120 parse_file_seq (&p, '\0', sizeof (struct nameseq), 1),
2121 sizeof (struct nameseq));
2122
2123 /* .DEFAULT_GOAL should contain one target. */
2124 if (ns->next != 0)
2125 fatal (NILF, _(".DEFAULT_GOAL contains more than one target"));
2126
2127 default_goal_file = enter_file (ns->name);
2128
2129 ns->name = 0; /* It was reused by enter_file(). */
2130 free_ns_chain (ns);
2131 }
2132 }
2133
2134 goals = (struct dep *) xmalloc (sizeof (struct dep));
2135 goals->next = 0;
2136 goals->name = 0;
2137 goals->ignore_mtime = 0;
2138 goals->need_2nd_expansion = 0;
2139 goals->file = default_goal_file;
2140 }
2141 }
2142 else
2143 lastgoal->next = 0;
2144
2145
2146 if (!goals)
2147 {
2148 if (read_makefiles == 0)
2149 fatal (NILF, _("No targets specified and no makefile found"));
2150
2151 fatal (NILF, _("No targets"));
2152 }
2153
2154 /* Update the goals. */
2155
2156 DB (DB_BASIC, (_("Updating goal targets....\n")));
2157
2158 switch (update_goal_chain (goals))
2159 {
2160 case -1:
2161 /* Nothing happened. */
2162 case 0:
2163 /* Updated successfully. */
2164 status = makefile_status;
2165 break;
2166 case 1:
2167 /* We are under -q and would run some commands. */
2168 status = MAKE_TROUBLE;
2169 break;
2170 case 2:
2171 /* Updating failed. POSIX.2 specifies exit status >1 for this;
2172 but in VMS, there is only success and failure. */
2173 status = MAKE_FAILURE;
2174 break;
2175 default:
2176 abort ();
2177 }
2178
2179 /* If we detected some clock skew, generate one last warning */
2180 if (clock_skew_detected)
2181 error (NILF,
2182 _("warning: Clock skew detected. Your build may be incomplete."));
2183
2184 /* Exit. */
2185 die (status);
2186 }
2187
2188 return 0;
2189}
2190
2191
2192/* Parsing of arguments, decoding of switches. */
2193
2194static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
2195static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
2196 (sizeof (long_option_aliases) /
2197 sizeof (long_option_aliases[0]))];
2198
2199/* Fill in the string and vector for getopt. */
2200static void
2201init_switches (void)
2202{
2203 char *p;
2204 unsigned int c;
2205 unsigned int i;
2206
2207 if (options[0] != '\0')
2208 /* Already done. */
2209 return;
2210
2211 p = options;
2212
2213 /* Return switch and non-switch args in order, regardless of
2214 POSIXLY_CORRECT. Non-switch args are returned as option 1. */
2215 *p++ = '-';
2216
2217 for (i = 0; switches[i].c != '\0'; ++i)
2218 {
2219 long_options[i].name = (switches[i].long_name == 0 ? "" :
2220 switches[i].long_name);
2221 long_options[i].flag = 0;
2222 long_options[i].val = switches[i].c;
2223 if (short_option (switches[i].c))
2224 *p++ = switches[i].c;
2225 switch (switches[i].type)
2226 {
2227 case flag:
2228 case flag_off:
2229 case ignore:
2230 long_options[i].has_arg = no_argument;
2231 break;
2232
2233 case string:
2234 case positive_int:
2235 case floating:
2236 if (short_option (switches[i].c))
2237 *p++ = ':';
2238 if (switches[i].noarg_value != 0)
2239 {
2240 if (short_option (switches[i].c))
2241 *p++ = ':';
2242 long_options[i].has_arg = optional_argument;
2243 }
2244 else
2245 long_options[i].has_arg = required_argument;
2246 break;
2247 }
2248 }
2249 *p = '\0';
2250 for (c = 0; c < (sizeof (long_option_aliases) /
2251 sizeof (long_option_aliases[0]));
2252 ++c)
2253 long_options[i++] = long_option_aliases[c];
2254 long_options[i].name = 0;
2255}
2256
2257static void
2258handle_non_switch_argument (char *arg, int env)
2259{
2260 /* Non-option argument. It might be a variable definition. */
2261 struct variable *v;
2262 if (arg[0] == '-' && arg[1] == '\0')
2263 /* Ignore plain `-' for compatibility. */
2264 return;
2265 v = try_variable_definition (0, arg, o_command, 0);
2266 if (v != 0)
2267 {
2268 /* It is indeed a variable definition. If we don't already have this
2269 one, record a pointer to the variable for later use in
2270 define_makeflags. */
2271 struct command_variable *cv;
2272
2273 for (cv = command_variables; cv != 0; cv = cv->next)
2274 if (cv->variable == v)
2275 break;
2276
2277 if (! cv) {
2278 cv = (struct command_variable *) xmalloc (sizeof (*cv));
2279 cv->variable = v;
2280 cv->next = command_variables;
2281 command_variables = cv;
2282 }
2283 }
2284 else if (! env)
2285 {
2286 /* Not an option or variable definition; it must be a goal
2287 target! Enter it as a file and add it to the dep chain of
2288 goals. */
2289 struct file *f = enter_command_line_file (arg);
2290 f->cmd_target = 1;
2291
2292 if (goals == 0)
2293 {
2294 goals = (struct dep *) xmalloc (sizeof (struct dep));
2295 lastgoal = goals;
2296 }
2297 else
2298 {
2299 lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
2300 lastgoal = lastgoal->next;
2301 }
2302 lastgoal->name = 0;
2303 lastgoal->file = f;
2304 lastgoal->ignore_mtime = 0;
2305 lastgoal->need_2nd_expansion = 0;
2306
2307 {
2308 /* Add this target name to the MAKECMDGOALS variable. */
2309 struct variable *v;
2310 char *value;
2311
2312 v = lookup_variable ("MAKECMDGOALS", 12);
2313 if (v == 0)
2314 value = f->name;
2315 else
2316 {
2317 /* Paste the old and new values together */
2318 unsigned int oldlen, newlen;
2319
2320 oldlen = strlen (v->value);
2321 newlen = strlen (f->name);
2322 value = (char *) alloca (oldlen + 1 + newlen + 1);
2323 bcopy (v->value, value, oldlen);
2324 value[oldlen] = ' ';
2325 bcopy (f->name, &value[oldlen + 1], newlen + 1);
2326 }
2327 define_variable ("MAKECMDGOALS", 12, value, o_default, 0);
2328 }
2329 }
2330}
2331
2332/* Print a nice usage method. */
2333
2334static void
2335print_usage (int bad)
2336{
2337 const char *const *cpp;
2338 FILE *usageto;
2339
2340 if (print_version_flag)
2341 print_version ();
2342
2343 usageto = bad ? stderr : stdout;
2344
2345 fprintf (usageto, _("Usage: %s [options] [target] ...\n"), program);
2346
2347 for (cpp = usage; *cpp; ++cpp)
2348 fputs (_(*cpp), usageto);
2349
2350 if (!remote_description || *remote_description == '\0')
2351 fprintf (usageto, _("\nThis program built for %s\n"), make_host);
2352 else
2353 fprintf (usageto, _("\nThis program built for %s (%s)\n"),
2354 make_host, remote_description);
2355
2356 fprintf (usageto, _("Report bugs to <[email protected]>\n"));
2357}
2358
2359/* Decode switches from ARGC and ARGV.
2360 They came from the environment if ENV is nonzero. */
2361
2362static void
2363decode_switches (int argc, char **argv, int env)
2364{
2365 int bad = 0;
2366 register const struct command_switch *cs;
2367 register struct stringlist *sl;
2368 register int c;
2369
2370 /* getopt does most of the parsing for us.
2371 First, get its vectors set up. */
2372
2373 init_switches ();
2374
2375 /* Let getopt produce error messages for the command line,
2376 but not for options from the environment. */
2377 opterr = !env;
2378 /* Reset getopt's state. */
2379 optind = 0;
2380
2381 while (optind < argc)
2382 {
2383 /* Parse the next argument. */
2384 c = getopt_long (argc, argv, options, long_options, (int *) 0);
2385 if (c == EOF)
2386 /* End of arguments, or "--" marker seen. */
2387 break;
2388 else if (c == 1)
2389 /* An argument not starting with a dash. */
2390 handle_non_switch_argument (optarg, env);
2391 else if (c == '?')
2392 /* Bad option. We will print a usage message and die later.
2393 But continue to parse the other options so the user can
2394 see all he did wrong. */
2395 bad = 1;
2396 else
2397 for (cs = switches; cs->c != '\0'; ++cs)
2398 if (cs->c == c)
2399 {
2400 /* Whether or not we will actually do anything with
2401 this switch. We test this individually inside the
2402 switch below rather than just once outside it, so that
2403 options which are to be ignored still consume args. */
2404 int doit = !env || cs->env;
2405
2406 switch (cs->type)
2407 {
2408 default:
2409 abort ();
2410
2411 case ignore:
2412 break;
2413
2414 case flag:
2415 case flag_off:
2416 if (doit)
2417 *(int *) cs->value_ptr = cs->type == flag;
2418 break;
2419
2420 case string:
2421 if (!doit)
2422 break;
2423
2424 if (optarg == 0)
2425 optarg = cs->noarg_value;
2426 else if (*optarg == '\0')
2427 {
2428 error (NILF, _("the `-%c' option requires a non-empty string argument"),
2429 cs->c);
2430 bad = 1;
2431 }
2432
2433 sl = *(struct stringlist **) cs->value_ptr;
2434 if (sl == 0)
2435 {
2436 sl = (struct stringlist *)
2437 xmalloc (sizeof (struct stringlist));
2438 sl->max = 5;
2439 sl->idx = 0;
2440 sl->list = (char **) xmalloc (5 * sizeof (char *));
2441 *(struct stringlist **) cs->value_ptr = sl;
2442 }
2443 else if (sl->idx == sl->max - 1)
2444 {
2445 sl->max += 5;
2446 sl->list = (char **)
2447 xrealloc ((char *) sl->list,
2448 sl->max * sizeof (char *));
2449 }
2450 sl->list[sl->idx++] = optarg;
2451 sl->list[sl->idx] = 0;
2452 break;
2453
2454 case positive_int:
2455 /* See if we have an option argument; if we do require that
2456 it's all digits, not something like "10foo". */
2457 if (optarg == 0 && argc > optind)
2458 {
2459 const char *cp;
2460 for (cp=argv[optind]; ISDIGIT (cp[0]); ++cp)
2461 ;
2462 if (cp[0] == '\0')
2463 optarg = argv[optind++];
2464 }
2465
2466 if (!doit)
2467 break;
2468
2469 if (optarg != 0)
2470 {
2471 int i = atoi (optarg);
2472 const char *cp;
2473
2474 /* Yes, I realize we're repeating this in some cases. */
2475 for (cp = optarg; ISDIGIT (cp[0]); ++cp)
2476 ;
2477
2478 if (i < 1 || cp[0] != '\0')
2479 {
2480 error (NILF, _("the `-%c' option requires a positive integral argument"),
2481 cs->c);
2482 bad = 1;
2483 }
2484 else
2485 *(unsigned int *) cs->value_ptr = i;
2486 }
2487 else
2488 *(unsigned int *) cs->value_ptr
2489 = *(unsigned int *) cs->noarg_value;
2490 break;
2491
2492#ifndef NO_FLOAT
2493 case floating:
2494 if (optarg == 0 && optind < argc
2495 && (ISDIGIT (argv[optind][0]) || argv[optind][0] == '.'))
2496 optarg = argv[optind++];
2497
2498 if (doit)
2499 *(double *) cs->value_ptr
2500 = (optarg != 0 ? atof (optarg)
2501 : *(double *) cs->noarg_value);
2502
2503 break;
2504#endif
2505 }
2506
2507 /* We've found the switch. Stop looking. */
2508 break;
2509 }
2510 }
2511
2512 /* There are no more options according to getting getopt, but there may
2513 be some arguments left. Since we have asked for non-option arguments
2514 to be returned in order, this only happens when there is a "--"
2515 argument to prevent later arguments from being options. */
2516 while (optind < argc)
2517 handle_non_switch_argument (argv[optind++], env);
2518
2519
2520 if (!env && (bad || print_usage_flag))
2521 {
2522 print_usage (bad);
2523 die (bad ? 2 : 0);
2524 }
2525}
2526
2527/* Decode switches from environment variable ENVAR (which is LEN chars long).
2528 We do this by chopping the value into a vector of words, prepending a
2529 dash to the first word if it lacks one, and passing the vector to
2530 decode_switches. */
2531
2532static void
2533decode_env_switches (char *envar, unsigned int len)
2534{
2535 char *varref = (char *) alloca (2 + len + 2);
2536 char *value, *p;
2537 int argc;
2538 char **argv;
2539
2540 /* Get the variable's value. */
2541 varref[0] = '$';
2542 varref[1] = '(';
2543 bcopy (envar, &varref[2], len);
2544 varref[2 + len] = ')';
2545 varref[2 + len + 1] = '\0';
2546 value = variable_expand (varref);
2547
2548 /* Skip whitespace, and check for an empty value. */
2549 value = next_token (value);
2550 len = strlen (value);
2551 if (len == 0)
2552 return;
2553
2554 /* Allocate a vector that is definitely big enough. */
2555 argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
2556
2557 /* Allocate a buffer to copy the value into while we split it into words
2558 and unquote it. We must use permanent storage for this because
2559 decode_switches may store pointers into the passed argument words. */
2560 p = (char *) xmalloc (2 * len);
2561
2562 /* getopt will look at the arguments starting at ARGV[1].
2563 Prepend a spacer word. */
2564 argv[0] = 0;
2565 argc = 1;
2566 argv[argc] = p;
2567 while (*value != '\0')
2568 {
2569 if (*value == '\\' && value[1] != '\0')
2570 ++value; /* Skip the backslash. */
2571 else if (isblank ((unsigned char)*value))
2572 {
2573 /* End of the word. */
2574 *p++ = '\0';
2575 argv[++argc] = p;
2576 do
2577 ++value;
2578 while (isblank ((unsigned char)*value));
2579 continue;
2580 }
2581 *p++ = *value++;
2582 }
2583 *p = '\0';
2584 argv[++argc] = 0;
2585
2586 if (argv[1][0] != '-' && strchr (argv[1], '=') == 0)
2587 /* The first word doesn't start with a dash and isn't a variable
2588 definition. Add a dash and pass it along to decode_switches. We
2589 need permanent storage for this in case decode_switches saves
2590 pointers into the value. */
2591 argv[1] = concat ("-", argv[1], "");
2592
2593 /* Parse those words. */
2594 decode_switches (argc, argv, 1);
2595}
2596
2597
2598/* Quote the string IN so that it will be interpreted as a single word with
2599 no magic by decode_env_switches; also double dollar signs to avoid
2600 variable expansion in make itself. Write the result into OUT, returning
2601 the address of the next character to be written.
2602 Allocating space for OUT twice the length of IN is always sufficient. */
2603
2604static char *
2605quote_for_env (char *out, char *in)
2606{
2607 while (*in != '\0')
2608 {
2609 if (*in == '$')
2610 *out++ = '$';
2611 else if (isblank ((unsigned char)*in) || *in == '\\')
2612 *out++ = '\\';
2613 *out++ = *in++;
2614 }
2615
2616 return out;
2617}
2618
2619/* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
2620 command switches. Include options with args if ALL is nonzero.
2621 Don't include options with the `no_makefile' flag set if MAKEFILE. */
2622
2623static void
2624define_makeflags (int all, int makefile)
2625{
2626 static const char ref[] = "$(MAKEOVERRIDES)";
2627 static const char posixref[] = "$(-*-command-variables-*-)";
2628 register const struct command_switch *cs;
2629 char *flagstring;
2630 register char *p;
2631 unsigned int words;
2632 struct variable *v;
2633
2634 /* We will construct a linked list of `struct flag's describing
2635 all the flags which need to go in MAKEFLAGS. Then, once we
2636 know how many there are and their lengths, we can put them all
2637 together in a string. */
2638
2639 struct flag
2640 {
2641 struct flag *next;
2642 const struct command_switch *cs;
2643 char *arg;
2644 };
2645 struct flag *flags = 0;
2646 unsigned int flagslen = 0;
2647#define ADD_FLAG(ARG, LEN) \
2648 do { \
2649 struct flag *new = (struct flag *) alloca (sizeof (struct flag)); \
2650 new->cs = cs; \
2651 new->arg = (ARG); \
2652 new->next = flags; \
2653 flags = new; \
2654 if (new->arg == 0) \
2655 ++flagslen; /* Just a single flag letter. */ \
2656 else \
2657 flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */ \
2658 if (!short_option (cs->c)) \
2659 /* This switch has no single-letter version, so we use the long. */ \
2660 flagslen += 2 + strlen (cs->long_name); \
2661 } while (0)
2662
2663 for (cs = switches; cs->c != '\0'; ++cs)
2664 if (cs->toenv && (!makefile || !cs->no_makefile))
2665 switch (cs->type)
2666 {
2667 default:
2668 abort ();
2669
2670 case ignore:
2671 break;
2672
2673 case flag:
2674 case flag_off:
2675 if (!*(int *) cs->value_ptr == (cs->type == flag_off)
2676 && (cs->default_value == 0
2677 || *(int *) cs->value_ptr != *(int *) cs->default_value))
2678 ADD_FLAG (0, 0);
2679 break;
2680
2681 case positive_int:
2682 if (all)
2683 {
2684 if ((cs->default_value != 0
2685 && (*(unsigned int *) cs->value_ptr
2686 == *(unsigned int *) cs->default_value)))
2687 break;
2688 else if (cs->noarg_value != 0
2689 && (*(unsigned int *) cs->value_ptr ==
2690 *(unsigned int *) cs->noarg_value))
2691 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
2692 else if (cs->c == 'j')
2693 /* Special case for `-j'. */
2694 ADD_FLAG ("1", 1);
2695 else
2696 {
2697 char *buf = (char *) alloca (30);
2698 sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
2699 ADD_FLAG (buf, strlen (buf));
2700 }
2701 }
2702 break;
2703
2704#ifndef NO_FLOAT
2705 case floating:
2706 if (all)
2707 {
2708 if (cs->default_value != 0
2709 && (*(double *) cs->value_ptr
2710 == *(double *) cs->default_value))
2711 break;
2712 else if (cs->noarg_value != 0
2713 && (*(double *) cs->value_ptr
2714 == *(double *) cs->noarg_value))
2715 ADD_FLAG ("", 0); /* Optional value omitted; see below. */
2716 else
2717 {
2718 char *buf = (char *) alloca (100);
2719 sprintf (buf, "%g", *(double *) cs->value_ptr);
2720 ADD_FLAG (buf, strlen (buf));
2721 }
2722 }
2723 break;
2724#endif
2725
2726 case string:
2727 if (all)
2728 {
2729 struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
2730 if (sl != 0)
2731 {
2732 /* Add the elements in reverse order, because
2733 all the flags get reversed below; and the order
2734 matters for some switches (like -I). */
2735 register unsigned int i = sl->idx;
2736 while (i-- > 0)
2737 ADD_FLAG (sl->list[i], strlen (sl->list[i]));
2738 }
2739 }
2740 break;
2741 }
2742
2743 flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ". */
2744
2745#undef ADD_FLAG
2746
2747 /* Construct the value in FLAGSTRING.
2748 We allocate enough space for a preceding dash and trailing null. */
2749 flagstring = (char *) alloca (1 + flagslen + 1);
2750 bzero (flagstring, 1 + flagslen + 1);
2751 p = flagstring;
2752 words = 1;
2753 *p++ = '-';
2754 while (flags != 0)
2755 {
2756 /* Add the flag letter or name to the string. */
2757 if (short_option (flags->cs->c))
2758 *p++ = flags->cs->c;
2759 else
2760 {
2761 if (*p != '-')
2762 {
2763 *p++ = ' ';
2764 *p++ = '-';
2765 }
2766 *p++ = '-';
2767 strcpy (p, flags->cs->long_name);
2768 p += strlen (p);
2769 }
2770 if (flags->arg != 0)
2771 {
2772 /* A flag that takes an optional argument which in this case is
2773 omitted is specified by ARG being "". We must distinguish
2774 because a following flag appended without an intervening " -"
2775 is considered the arg for the first. */
2776 if (flags->arg[0] != '\0')
2777 {
2778 /* Add its argument too. */
2779 *p++ = !short_option (flags->cs->c) ? '=' : ' ';
2780 p = quote_for_env (p, flags->arg);
2781 }
2782 ++words;
2783 /* Write a following space and dash, for the next flag. */
2784 *p++ = ' ';
2785 *p++ = '-';
2786 }
2787 else if (!short_option (flags->cs->c))
2788 {
2789 ++words;
2790 /* Long options must each go in their own word,
2791 so we write the following space and dash. */
2792 *p++ = ' ';
2793 *p++ = '-';
2794 }
2795 flags = flags->next;
2796 }
2797
2798 /* Define MFLAGS before appending variable definitions. */
2799
2800 if (p == &flagstring[1])
2801 /* No flags. */
2802 flagstring[0] = '\0';
2803 else if (p[-1] == '-')
2804 {
2805 /* Kill the final space and dash. */
2806 p -= 2;
2807 *p = '\0';
2808 }
2809 else
2810 /* Terminate the string. */
2811 *p = '\0';
2812
2813 /* Since MFLAGS is not parsed for flags, there is no reason to
2814 override any makefile redefinition. */
2815 (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
2816
2817 if (all && command_variables != 0)
2818 {
2819 /* Now write a reference to $(MAKEOVERRIDES), which contains all the
2820 command-line variable definitions. */
2821
2822 if (p == &flagstring[1])
2823 /* No flags written, so elide the leading dash already written. */
2824 p = flagstring;
2825 else
2826 {
2827 /* Separate the variables from the switches with a "--" arg. */
2828 if (p[-1] != '-')
2829 {
2830 /* We did not already write a trailing " -". */
2831 *p++ = ' ';
2832 *p++ = '-';
2833 }
2834 /* There is a trailing " -"; fill it out to " -- ". */
2835 *p++ = '-';
2836 *p++ = ' ';
2837 }
2838
2839 /* Copy in the string. */
2840 if (posix_pedantic)
2841 {
2842 bcopy (posixref, p, sizeof posixref - 1);
2843 p += sizeof posixref - 1;
2844 }
2845 else
2846 {
2847 bcopy (ref, p, sizeof ref - 1);
2848 p += sizeof ref - 1;
2849 }
2850 }
2851 else if (p == &flagstring[1])
2852 {
2853 words = 0;
2854 --p;
2855 }
2856 else if (p[-1] == '-')
2857 /* Kill the final space and dash. */
2858 p -= 2;
2859 /* Terminate the string. */
2860 *p = '\0';
2861
2862 v = define_variable ("MAKEFLAGS", 9,
2863 /* If there are switches, omit the leading dash
2864 unless it is a single long option with two
2865 leading dashes. */
2866 &flagstring[(flagstring[0] == '-'
2867 && flagstring[1] != '-')
2868 ? 1 : 0],
2869 /* This used to use o_env, but that lost when a
2870 makefile defined MAKEFLAGS. Makefiles set
2871 MAKEFLAGS to add switches, but we still want
2872 to redefine its value with the full set of
2873 switches. Of course, an override or command
2874 definition will still take precedence. */
2875 o_file, 1);
2876 if (! all)
2877 /* The first time we are called, set MAKEFLAGS to always be exported.
2878 We should not do this again on the second call, because that is
2879 after reading makefiles which might have done `unexport MAKEFLAGS'. */
2880 v->export = v_export;
2881}
2882
2883
2884/* Print version information. */
2885
2886static void
2887print_version (void)
2888{
2889 static int printed_version = 0;
2890
2891 char *precede = print_data_base_flag ? "# " : "";
2892
2893 if (printed_version)
2894 /* Do it only once. */
2895 return;
2896
2897 /* Print this untranslated. The coding standards recommend translating the
2898 (C) to the copyright symbol, but this string is going to change every
2899 year, and none of the rest of it should be translated (including the
2900 word "Copyright", so it hardly seems worth it. */
2901
2902 printf ("%sGNU Make %s\n\
2903%sCopyright (C) 2003 Free Software Foundation, Inc.\n",
2904 precede, version_string, precede);
2905
2906 printf (_("%sThis is free software; see the source for copying conditions.\n\
2907%sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
2908%sPARTICULAR PURPOSE.\n"),
2909 precede, precede, precede);
2910
2911 if (!remote_description || *remote_description == '\0')
2912 printf (_("\n%sThis program built for %s\n"), precede, make_host);
2913 else
2914 printf (_("\n%sThis program built for %s (%s)\n"),
2915 precede, make_host, remote_description);
2916
2917 printed_version = 1;
2918
2919 /* Flush stdout so the user doesn't have to wait to see the
2920 version information while things are thought about. */
2921 fflush (stdout);
2922}
2923
2924/* Print a bunch of information about this and that. */
2925
2926static void
2927print_data_base (void)
2928{
2929 time_t when;
2930
2931 when = time ((time_t *) 0);
2932 printf (_("\n# Make data base, printed on %s"), ctime (&when));
2933
2934 print_variable_data_base ();
2935 print_dir_data_base ();
2936 print_rule_data_base ();
2937 print_file_data_base ();
2938 print_vpath_data_base ();
2939
2940 when = time ((time_t *) 0);
2941 printf (_("\n# Finished Make data base on %s\n"), ctime (&when));
2942}
2943
2944
2945/* Exit with STATUS, cleaning up as necessary. */
2946
2947void
2948die (int status)
2949{
2950 static char dying = 0;
2951
2952 if (!dying)
2953 {
2954 char token = '+';
2955 int err;
2956
2957 dying = 1;
2958
2959 if (print_version_flag)
2960 print_version ();
2961
2962 /* Wait for children to die. */
2963 for (err = (status != 0); job_slots_used > 0; err = 0)
2964 reap_children (1, err);
2965
2966 /* Let the remote job module clean up its state. */
2967 remote_cleanup ();
2968
2969 /* Remove the intermediate files. */
2970 remove_intermediates (0);
2971
2972 if (print_data_base_flag)
2973 print_data_base ();
2974
2975 /* Sanity: have we written all our jobserver tokens back? If our
2976 exit status is 2 that means some kind of syntax error; we might not
2977 have written all our tokens so do that now. If tokens are left
2978 after any other error code, that's bad. */
2979
2980 if (job_fds[0] != -1 && jobserver_tokens)
2981 {
2982 if (status != 2)
2983 error (NILF,
2984 "INTERNAL: Exiting with %u jobserver tokens (should be 0)!",
2985 jobserver_tokens);
2986 else
2987 while (jobserver_tokens--)
2988 {
2989 int r;
2990
2991 EINTRLOOP (r, write (job_fds[1], &token, 1));
2992 if (r != 1)
2993 perror_with_name ("write", "");
2994 }
2995 }
2996
2997
2998 /* Sanity: If we're the master, were all the tokens written back? */
2999
3000 if (master_job_slots)
3001 {
3002 /* We didn't write one for ourself, so start at 1. */
3003 unsigned int tcnt = 1;
3004
3005 /* Close the write side, so the read() won't hang. */
3006 close (job_fds[1]);
3007
3008 while ((err = read (job_fds[0], &token, 1)) == 1)
3009 ++tcnt;
3010
3011 if (tcnt != master_job_slots)
3012 error (NILF,
3013 "INTERNAL: Exiting with %u jobserver tokens available; should be %u!",
3014 tcnt, master_job_slots);
3015 }
3016
3017 /* Try to move back to the original directory. This is essential on
3018 MS-DOS (where there is really only one process), and on Unix it
3019 puts core files in the original directory instead of the -C
3020 directory. Must wait until after remove_intermediates(), or unlinks
3021 of relative pathnames fail. */
3022 if (directory_before_chdir != 0)
3023 chdir (directory_before_chdir);
3024
3025 log_working_directory (0);
3026 }
3027
3028 exit (status);
3029}
3030
3031
3032/* Write a message indicating that we've just entered or
3033 left (according to ENTERING) the current directory. */
3034
3035void
3036log_working_directory (int entering)
3037{
3038 static int entered = 0;
3039
3040 /* Print nothing without the flag. Don't print the entering message
3041 again if we already have. Don't print the leaving message if we
3042 haven't printed the entering message. */
3043 if (! print_directory_flag || entering == entered)
3044 return;
3045
3046 entered = entering;
3047
3048 if (print_data_base_flag)
3049 fputs ("# ", stdout);
3050
3051 /* Use entire sentences to give the translators a fighting chance. */
3052
3053 if (makelevel == 0)
3054 if (starting_directory == 0)
3055 if (entering)
3056 printf (_("%s: Entering an unknown directory\n"), program);
3057 else
3058 printf (_("%s: Leaving an unknown directory\n"), program);
3059 else
3060 if (entering)
3061 printf (_("%s: Entering directory `%s'\n"),
3062 program, starting_directory);
3063 else
3064 printf (_("%s: Leaving directory `%s'\n"),
3065 program, starting_directory);
3066 else
3067 if (starting_directory == 0)
3068 if (entering)
3069 printf (_("%s[%u]: Entering an unknown directory\n"),
3070 program, makelevel);
3071 else
3072 printf (_("%s[%u]: Leaving an unknown directory\n"),
3073 program, makelevel);
3074 else
3075 if (entering)
3076 printf (_("%s[%u]: Entering directory `%s'\n"),
3077 program, makelevel, starting_directory);
3078 else
3079 printf (_("%s[%u]: Leaving directory `%s'\n"),
3080 program, makelevel, starting_directory);
3081
3082 /* Flush stdout to be sure this comes before any stderr output. */
3083 fflush (stdout);
3084}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette