VirtualBox

source: kBuild/trunk/src/kmk/main.c@ 1523

Last change on this file since 1523 was 1518, checked in by bird, 17 years ago

MSC doesn't grok #ifdefs inside macros invocations.

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

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