VirtualBox

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

Last change on this file since 530 was 530, checked in by bird, 18 years ago

Added kBuild specific functions for speeding up source processing.

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

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