VirtualBox

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

Last change on this file since 522 was 522, checked in by bird, 19 years ago

kmk_ash wants batch files on windows or the double quotes will get screwed up.

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

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