VirtualBox

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

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

Cleaning up the modifications. Changes are now either configurable or marked, and dead stuff has been removed (dll shell).

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