xref: /netbsd-src/external/gpl2/gmake/dist/main.c (revision d9c0982bb8dc3a2644bed9efa6c2d4101172b3fb)
169606e3fSchristos /* Argument parsing and main program of GNU Make.
269606e3fSchristos Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
369606e3fSchristos 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
469606e3fSchristos Foundation, Inc.
569606e3fSchristos This file is part of GNU Make.
669606e3fSchristos 
769606e3fSchristos GNU Make is free software; you can redistribute it and/or modify it under the
869606e3fSchristos terms of the GNU General Public License as published by the Free Software
969606e3fSchristos Foundation; either version 2, or (at your option) any later version.
1069606e3fSchristos 
1169606e3fSchristos GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
1269606e3fSchristos WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
1369606e3fSchristos A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
1469606e3fSchristos 
1569606e3fSchristos You should have received a copy of the GNU General Public License along with
1669606e3fSchristos GNU Make; see the file COPYING.  If not, write to the Free Software
1769606e3fSchristos Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.  */
1869606e3fSchristos 
1969606e3fSchristos #include "make.h"
2069606e3fSchristos #include "dep.h"
2169606e3fSchristos #include "filedef.h"
2269606e3fSchristos #include "variable.h"
2369606e3fSchristos #include "job.h"
2469606e3fSchristos #include "commands.h"
2569606e3fSchristos #include "rule.h"
2669606e3fSchristos #include "debug.h"
2769606e3fSchristos #include "getopt.h"
2869606e3fSchristos 
2969606e3fSchristos #include <assert.h>
3069606e3fSchristos #ifdef _AMIGA
3169606e3fSchristos # include <dos/dos.h>
3269606e3fSchristos # include <proto/dos.h>
3369606e3fSchristos #endif
3469606e3fSchristos #ifdef WINDOWS32
3569606e3fSchristos #include <windows.h>
3669606e3fSchristos #include <io.h>
3769606e3fSchristos #include "pathstuff.h"
3869606e3fSchristos #endif
3969606e3fSchristos #ifdef __EMX__
4069606e3fSchristos # include <sys/types.h>
4169606e3fSchristos # include <sys/wait.h>
4269606e3fSchristos #endif
4369606e3fSchristos #ifdef HAVE_FCNTL_H
4469606e3fSchristos # include <fcntl.h>
4569606e3fSchristos #endif
4669606e3fSchristos 
4769606e3fSchristos #if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT) && defined(HAVE_SETRLIMIT)
4869606e3fSchristos # define SET_STACK_SIZE
4969606e3fSchristos #endif
5069606e3fSchristos 
5169606e3fSchristos #ifdef SET_STACK_SIZE
5269606e3fSchristos # include <sys/resource.h>
5369606e3fSchristos #endif
5469606e3fSchristos 
5569606e3fSchristos #ifdef _AMIGA
5669606e3fSchristos int __stack = 20000; /* Make sure we have 20K of stack space */
5769606e3fSchristos #endif
5869606e3fSchristos 
5969606e3fSchristos extern void init_dir PARAMS ((void));
6069606e3fSchristos extern void remote_setup PARAMS ((void));
6169606e3fSchristos extern void remote_cleanup PARAMS ((void));
6269606e3fSchristos extern RETSIGTYPE fatal_error_signal PARAMS ((int sig));
6369606e3fSchristos 
6469606e3fSchristos extern void print_variable_data_base PARAMS ((void));
6569606e3fSchristos extern void print_dir_data_base PARAMS ((void));
6669606e3fSchristos extern void print_rule_data_base PARAMS ((void));
6769606e3fSchristos extern void print_file_data_base PARAMS ((void));
6869606e3fSchristos extern void print_vpath_data_base PARAMS ((void));
6969606e3fSchristos 
7069606e3fSchristos #if defined HAVE_WAITPID || defined HAVE_WAIT3
7169606e3fSchristos # define HAVE_WAIT_NOHANG
7269606e3fSchristos #endif
7369606e3fSchristos 
7469606e3fSchristos #ifndef	HAVE_UNISTD_H
7569606e3fSchristos extern int chdir ();
7669606e3fSchristos #endif
7769606e3fSchristos #ifndef	STDC_HEADERS
7869606e3fSchristos # ifndef sun			/* Sun has an incorrect decl in a header.  */
7969606e3fSchristos extern void exit PARAMS ((int)) __attribute__ ((noreturn));
8069606e3fSchristos # endif
8169606e3fSchristos extern double atof ();
8269606e3fSchristos #endif
8369606e3fSchristos 
8469606e3fSchristos static void clean_jobserver PARAMS ((int status));
8569606e3fSchristos static void print_data_base PARAMS ((void));
8669606e3fSchristos static void print_version PARAMS ((void));
8769606e3fSchristos static void decode_switches PARAMS ((int argc, char **argv, int env));
8869606e3fSchristos static void decode_env_switches PARAMS ((char *envar, unsigned int len));
8969606e3fSchristos static void define_makeflags PARAMS ((int all, int makefile));
9069606e3fSchristos static char *quote_for_env PARAMS ((char *out, char *in));
9169606e3fSchristos static void initialize_global_hash_tables PARAMS ((void));
9269606e3fSchristos 
9369606e3fSchristos 
9469606e3fSchristos /* The structure that describes an accepted command switch.  */
9569606e3fSchristos 
9669606e3fSchristos struct command_switch
9769606e3fSchristos   {
9869606e3fSchristos     int c;			/* The switch character.  */
9969606e3fSchristos 
10069606e3fSchristos     enum			/* Type of the value.  */
10169606e3fSchristos       {
10269606e3fSchristos 	flag,			/* Turn int flag on.  */
10369606e3fSchristos 	flag_off,		/* Turn int flag off.  */
10469606e3fSchristos 	string,			/* One string per switch.  */
10569606e3fSchristos 	positive_int,		/* A positive integer.  */
10669606e3fSchristos 	floating,		/* A floating-point number (double).  */
10769606e3fSchristos 	ignore			/* Ignored.  */
10869606e3fSchristos       } type;
10969606e3fSchristos 
11069606e3fSchristos     char *value_ptr;	/* Pointer to the value-holding variable.  */
11169606e3fSchristos 
11269606e3fSchristos     unsigned int env:1;		/* Can come from MAKEFLAGS.  */
11369606e3fSchristos     unsigned int toenv:1;	/* Should be put in MAKEFLAGS.  */
11469606e3fSchristos     unsigned int no_makefile:1;	/* Don't propagate when remaking makefiles.  */
11569606e3fSchristos 
11669606e3fSchristos     char *noarg_value;	/* Pointer to value used if no argument is given.  */
11769606e3fSchristos     char *default_value;/* Pointer to default value.  */
11869606e3fSchristos 
11969606e3fSchristos     char *long_name;		/* Long option name.  */
12069606e3fSchristos   };
12169606e3fSchristos 
12269606e3fSchristos /* True if C is a switch value that corresponds to a short option.  */
12369606e3fSchristos 
12469606e3fSchristos #define short_option(c) ((c) <= CHAR_MAX)
12569606e3fSchristos 
12669606e3fSchristos /* The structure used to hold the list of strings given
12769606e3fSchristos    in command switches of a type that takes string arguments.  */
12869606e3fSchristos 
12969606e3fSchristos struct stringlist
13069606e3fSchristos   {
13169606e3fSchristos     char **list;	/* Nil-terminated list of strings.  */
13269606e3fSchristos     unsigned int idx;	/* Index into above.  */
13369606e3fSchristos     unsigned int max;	/* Number of pointers allocated.  */
13469606e3fSchristos   };
13569606e3fSchristos 
13669606e3fSchristos 
13769606e3fSchristos /* The recognized command switches.  */
13869606e3fSchristos 
13969606e3fSchristos /* Nonzero means do not print commands to be executed (-s).  */
14069606e3fSchristos 
14169606e3fSchristos int silent_flag;
14269606e3fSchristos 
14369606e3fSchristos /* Nonzero means just touch the files
14469606e3fSchristos    that would appear to need remaking (-t)  */
14569606e3fSchristos 
14669606e3fSchristos int touch_flag;
14769606e3fSchristos 
14869606e3fSchristos /* Nonzero means just print what commands would need to be executed,
14969606e3fSchristos    don't actually execute them (-n).  */
15069606e3fSchristos 
15169606e3fSchristos int just_print_flag;
15269606e3fSchristos 
15369606e3fSchristos /* Print debugging info (--debug).  */
15469606e3fSchristos 
15569606e3fSchristos static struct stringlist *db_flags;
15669606e3fSchristos static int debug_flag = 0;
15769606e3fSchristos 
15869606e3fSchristos int db_level = 0;
15969606e3fSchristos 
16069606e3fSchristos #ifdef WINDOWS32
16169606e3fSchristos /* Suspend make in main for a short time to allow debugger to attach */
16269606e3fSchristos 
16369606e3fSchristos int suspend_flag = 0;
16469606e3fSchristos #endif
16569606e3fSchristos 
16669606e3fSchristos /* Environment variables override makefile definitions.  */
16769606e3fSchristos 
16869606e3fSchristos int env_overrides = 0;
16969606e3fSchristos 
17069606e3fSchristos /* Nonzero means ignore status codes returned by commands
17169606e3fSchristos    executed to remake files.  Just treat them all as successful (-i).  */
17269606e3fSchristos 
17369606e3fSchristos int ignore_errors_flag = 0;
17469606e3fSchristos 
17569606e3fSchristos /* Nonzero means don't remake anything, just print the data base
17669606e3fSchristos    that results from reading the makefile (-p).  */
17769606e3fSchristos 
17869606e3fSchristos int print_data_base_flag = 0;
17969606e3fSchristos 
18069606e3fSchristos /* Nonzero means don't remake anything; just return a nonzero status
18169606e3fSchristos    if the specified targets are not up to date (-q).  */
18269606e3fSchristos 
18369606e3fSchristos int question_flag = 0;
18469606e3fSchristos 
18569606e3fSchristos /* Nonzero means do not use any of the builtin rules (-r) / variables (-R).  */
18669606e3fSchristos 
18769606e3fSchristos int no_builtin_rules_flag = 0;
18869606e3fSchristos int no_builtin_variables_flag = 0;
18969606e3fSchristos 
19069606e3fSchristos /* Nonzero means keep going even if remaking some file fails (-k).  */
19169606e3fSchristos 
19269606e3fSchristos int keep_going_flag;
19369606e3fSchristos int default_keep_going_flag = 0;
19469606e3fSchristos 
19569606e3fSchristos /* Nonzero means check symlink mtimes.  */
19669606e3fSchristos 
19769606e3fSchristos int check_symlink_flag = 0;
19869606e3fSchristos 
19969606e3fSchristos /* Nonzero means print directory before starting and when done (-w).  */
20069606e3fSchristos 
20169606e3fSchristos int print_directory_flag = 0;
20269606e3fSchristos 
20369606e3fSchristos /* Nonzero means ignore print_directory_flag and never print the directory.
20469606e3fSchristos    This is necessary because print_directory_flag is set implicitly.  */
20569606e3fSchristos 
20669606e3fSchristos int inhibit_print_directory_flag = 0;
20769606e3fSchristos 
20869606e3fSchristos /* Nonzero means print version information.  */
20969606e3fSchristos 
21069606e3fSchristos int print_version_flag = 0;
21169606e3fSchristos 
21269606e3fSchristos /* List of makefiles given with -f switches.  */
21369606e3fSchristos 
21469606e3fSchristos static struct stringlist *makefiles = 0;
21569606e3fSchristos 
21669606e3fSchristos /* Number of job slots (commands that can be run at once).  */
21769606e3fSchristos 
21869606e3fSchristos unsigned int job_slots = 1;
21969606e3fSchristos unsigned int default_job_slots = 1;
22069606e3fSchristos static unsigned int master_job_slots = 0;
22169606e3fSchristos 
22269606e3fSchristos /* Value of job_slots that means no limit.  */
22369606e3fSchristos 
22469606e3fSchristos static unsigned int inf_jobs = 0;
22569606e3fSchristos 
22669606e3fSchristos /* File descriptors for the jobs pipe.  */
22769606e3fSchristos 
22869606e3fSchristos static struct stringlist *jobserver_fds = 0;
22969606e3fSchristos 
23069606e3fSchristos int job_fds[2] = { -1, -1 };
23169606e3fSchristos int job_rfd = -1;
23269606e3fSchristos 
23369606e3fSchristos /* Maximum load average at which multiple jobs will be run.
23469606e3fSchristos    Negative values mean unlimited, while zero means limit to
23569606e3fSchristos    zero load (which could be useful to start infinite jobs remotely
23669606e3fSchristos    but one at a time locally).  */
23769606e3fSchristos #ifndef NO_FLOAT
23869606e3fSchristos double max_load_average = -1.0;
23969606e3fSchristos double default_load_average = -1.0;
24069606e3fSchristos #else
24169606e3fSchristos int max_load_average = -1;
24269606e3fSchristos int default_load_average = -1;
24369606e3fSchristos #endif
24469606e3fSchristos 
24569606e3fSchristos /* List of directories given with -C switches.  */
24669606e3fSchristos 
24769606e3fSchristos static struct stringlist *directories = 0;
24869606e3fSchristos 
24969606e3fSchristos /* List of include directories given with -I switches.  */
25069606e3fSchristos 
25169606e3fSchristos static struct stringlist *include_directories = 0;
25269606e3fSchristos 
25369606e3fSchristos /* List of files given with -o switches.  */
25469606e3fSchristos 
25569606e3fSchristos static struct stringlist *old_files = 0;
25669606e3fSchristos 
25769606e3fSchristos /* List of files given with -W switches.  */
25869606e3fSchristos 
25969606e3fSchristos static struct stringlist *new_files = 0;
26069606e3fSchristos 
26169606e3fSchristos /* If nonzero, we should just print usage and exit.  */
26269606e3fSchristos 
26369606e3fSchristos static int print_usage_flag = 0;
26469606e3fSchristos 
26569606e3fSchristos /* If nonzero, we should print a warning message
26669606e3fSchristos    for each reference to an undefined variable.  */
26769606e3fSchristos 
26869606e3fSchristos int warn_undefined_variables_flag;
26969606e3fSchristos 
27069606e3fSchristos /* If nonzero, always build all targets, regardless of whether
27169606e3fSchristos    they appear out of date or not.  */
27269606e3fSchristos 
27369606e3fSchristos static int always_make_set = 0;
27469606e3fSchristos int always_make_flag = 0;
27569606e3fSchristos 
27669606e3fSchristos /* If nonzero, we're in the "try to rebuild makefiles" phase.  */
27769606e3fSchristos 
27869606e3fSchristos int rebuilding_makefiles = 0;
27969606e3fSchristos 
28069606e3fSchristos /* Remember the original value of the SHELL variable, from the environment.  */
28169606e3fSchristos 
28269606e3fSchristos struct variable shell_var;
28369606e3fSchristos 
28469606e3fSchristos 
28569606e3fSchristos /* The usage output.  We write it this way to make life easier for the
28669606e3fSchristos    translators, especially those trying to translate to right-to-left
28769606e3fSchristos    languages like Hebrew.  */
28869606e3fSchristos 
28969606e3fSchristos static const char *const usage[] =
29069606e3fSchristos   {
29169606e3fSchristos     N_("Options:\n"),
29269606e3fSchristos     N_("\
29369606e3fSchristos   -b, -m                      Ignored for compatibility.\n"),
29469606e3fSchristos     N_("\
29569606e3fSchristos   -B, --always-make           Unconditionally make all targets.\n"),
29669606e3fSchristos     N_("\
29769606e3fSchristos   -C DIRECTORY, --directory=DIRECTORY\n\
29869606e3fSchristos                               Change to DIRECTORY before doing anything.\n"),
29969606e3fSchristos     N_("\
30069606e3fSchristos   -d                          Print lots of debugging information.\n"),
30169606e3fSchristos     N_("\
30269606e3fSchristos   --debug[=FLAGS]             Print various types of debugging information.\n"),
30369606e3fSchristos     N_("\
30469606e3fSchristos   -e, --environment-overrides\n\
30569606e3fSchristos                               Environment variables override makefiles.\n"),
30669606e3fSchristos     N_("\
30769606e3fSchristos   -f FILE, --file=FILE, --makefile=FILE\n\
30869606e3fSchristos                               Read FILE as a makefile.\n"),
30969606e3fSchristos     N_("\
31069606e3fSchristos   -h, --help                  Print this message and exit.\n"),
31169606e3fSchristos     N_("\
31269606e3fSchristos   -i, --ignore-errors         Ignore errors from commands.\n"),
31369606e3fSchristos     N_("\
31469606e3fSchristos   -I DIRECTORY, --include-dir=DIRECTORY\n\
31569606e3fSchristos                               Search DIRECTORY for included makefiles.\n"),
31669606e3fSchristos     N_("\
31769606e3fSchristos   -j [N], --jobs[=N]          Allow N jobs at once; infinite jobs with no arg.\n"),
31869606e3fSchristos     N_("\
31969606e3fSchristos   -k, --keep-going            Keep going when some targets can't be made.\n"),
32069606e3fSchristos     N_("\
32169606e3fSchristos   -l [N], --load-average[=N], --max-load[=N]\n\
32269606e3fSchristos                               Don't start multiple jobs unless load is below N.\n"),
32369606e3fSchristos     N_("\
32469606e3fSchristos   -L, --check-symlink-times   Use the latest mtime between symlinks and target.\n"),
32569606e3fSchristos     N_("\
32669606e3fSchristos   -n, --just-print, --dry-run, --recon\n\
32769606e3fSchristos                               Don't actually run any commands; just print them.\n"),
32869606e3fSchristos     N_("\
32969606e3fSchristos   -o FILE, --old-file=FILE, --assume-old=FILE\n\
33069606e3fSchristos                               Consider FILE to be very old and don't remake it.\n"),
33169606e3fSchristos     N_("\
33269606e3fSchristos   -p, --print-data-base       Print make's internal database.\n"),
33369606e3fSchristos     N_("\
33469606e3fSchristos   -q, --question              Run no commands; exit status says if up to date.\n"),
33569606e3fSchristos     N_("\
33669606e3fSchristos   -r, --no-builtin-rules      Disable the built-in implicit rules.\n"),
33769606e3fSchristos     N_("\
33869606e3fSchristos   -R, --no-builtin-variables  Disable the built-in variable settings.\n"),
33969606e3fSchristos     N_("\
34069606e3fSchristos   -s, --silent, --quiet       Don't echo commands.\n"),
34169606e3fSchristos     N_("\
34269606e3fSchristos   -S, --no-keep-going, --stop\n\
34369606e3fSchristos                               Turns off -k.\n"),
34469606e3fSchristos     N_("\
34569606e3fSchristos   -t, --touch                 Touch targets instead of remaking them.\n"),
34669606e3fSchristos     N_("\
34769606e3fSchristos   -v, --version               Print the version number of make and exit.\n"),
34869606e3fSchristos     N_("\
34969606e3fSchristos   -w, --print-directory       Print the current directory.\n"),
35069606e3fSchristos     N_("\
35169606e3fSchristos   --no-print-directory        Turn off -w, even if it was turned on implicitly.\n"),
35269606e3fSchristos     N_("\
35369606e3fSchristos   -W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE\n\
35469606e3fSchristos                               Consider FILE to be infinitely new.\n"),
35569606e3fSchristos     N_("\
35669606e3fSchristos   --warn-undefined-variables  Warn when an undefined variable is referenced.\n"),
35769606e3fSchristos     NULL
35869606e3fSchristos   };
35969606e3fSchristos 
36069606e3fSchristos /* The table of command switches.  */
36169606e3fSchristos 
36269606e3fSchristos static const struct command_switch switches[] =
36369606e3fSchristos   {
36469606e3fSchristos     { 'b', ignore, 0, 0, 0, 0, 0, 0, 0 },
36569606e3fSchristos     { 'B', flag, (char *) &always_make_set, 1, 1, 0, 0, 0, "always-make" },
36669606e3fSchristos     { 'C', string, (char *) &directories, 0, 0, 0, 0, 0, "directory" },
36769606e3fSchristos     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0, 0 },
36869606e3fSchristos     { CHAR_MAX+1, string, (char *) &db_flags, 1, 1, 0, "basic", 0, "debug" },
36969606e3fSchristos #ifdef WINDOWS32
37069606e3fSchristos     { 'D', flag, (char *) &suspend_flag, 1, 1, 0, 0, 0, "suspend-for-debug" },
37169606e3fSchristos #endif
37269606e3fSchristos     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
37369606e3fSchristos       "environment-overrides", },
37469606e3fSchristos     { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0, "file" },
37569606e3fSchristos     { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0, "help" },
37669606e3fSchristos     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
37769606e3fSchristos       "ignore-errors" },
37869606e3fSchristos     { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
37969606e3fSchristos       "include-dir" },
38069606e3fSchristos     { 'j', positive_int, (char *) &job_slots, 1, 1, 0, (char *) &inf_jobs,
38169606e3fSchristos       (char *) &default_job_slots, "jobs" },
38269606e3fSchristos     { CHAR_MAX+2, string, (char *) &jobserver_fds, 1, 1, 0, 0, 0,
38369606e3fSchristos       "jobserver-fds" },
38469606e3fSchristos     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0, 0,
38569606e3fSchristos       (char *) &default_keep_going_flag, "keep-going" },
38669606e3fSchristos #ifndef NO_FLOAT
38769606e3fSchristos     { 'l', floating, (char *) &max_load_average, 1, 1, 0,
38869606e3fSchristos       (char *) &default_load_average, (char *) &default_load_average,
38969606e3fSchristos       "load-average" },
39069606e3fSchristos #else
39169606e3fSchristos     { 'l', positive_int, (char *) &max_load_average, 1, 1, 0,
39269606e3fSchristos       (char *) &default_load_average, (char *) &default_load_average,
39369606e3fSchristos       "load-average" },
39469606e3fSchristos #endif
39569606e3fSchristos     { 'L', flag, (char *) &check_symlink_flag, 1, 1, 0, 0, 0,
39669606e3fSchristos       "check-symlink-times" },
39769606e3fSchristos     { 'm', ignore, 0, 0, 0, 0, 0, 0, 0 },
39869606e3fSchristos     { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0, "just-print" },
39969606e3fSchristos     { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0, "old-file" },
40069606e3fSchristos     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
40169606e3fSchristos       "print-data-base" },
40269606e3fSchristos     { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0, "question" },
40369606e3fSchristos     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
40469606e3fSchristos       "no-builtin-rules" },
40569606e3fSchristos     { 'R', flag, (char *) &no_builtin_variables_flag, 1, 1, 0, 0, 0,
40669606e3fSchristos       "no-builtin-variables" },
40769606e3fSchristos     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0, "silent" },
40869606e3fSchristos     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0, 0,
40969606e3fSchristos       (char *) &default_keep_going_flag, "no-keep-going" },
41069606e3fSchristos     { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0, "touch" },
41169606e3fSchristos     { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0, "version" },
41269606e3fSchristos     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
41369606e3fSchristos       "print-directory" },
41469606e3fSchristos     { CHAR_MAX+3, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
41569606e3fSchristos       "no-print-directory" },
41669606e3fSchristos     { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0, "what-if" },
41769606e3fSchristos     { CHAR_MAX+4, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
41869606e3fSchristos       "warn-undefined-variables" },
41969606e3fSchristos     { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
42069606e3fSchristos   };
42169606e3fSchristos 
42269606e3fSchristos /* Secondary long names for options.  */
42369606e3fSchristos 
42469606e3fSchristos static struct option long_option_aliases[] =
42569606e3fSchristos   {
42669606e3fSchristos     { "quiet",		no_argument,		0, 's' },
42769606e3fSchristos     { "stop",		no_argument,		0, 'S' },
42869606e3fSchristos     { "new-file",	required_argument,	0, 'W' },
42969606e3fSchristos     { "assume-new",	required_argument,	0, 'W' },
43069606e3fSchristos     { "assume-old",	required_argument,	0, 'o' },
43169606e3fSchristos     { "max-load",	optional_argument,	0, 'l' },
43269606e3fSchristos     { "dry-run",	no_argument,		0, 'n' },
43369606e3fSchristos     { "recon",		no_argument,		0, 'n' },
43469606e3fSchristos     { "makefile",	required_argument,	0, 'f' },
43569606e3fSchristos   };
43669606e3fSchristos 
43769606e3fSchristos /* List of goal targets.  */
43869606e3fSchristos 
43969606e3fSchristos static struct dep *goals, *lastgoal;
44069606e3fSchristos 
44169606e3fSchristos /* List of variables which were defined on the command line
44269606e3fSchristos    (or, equivalently, in MAKEFLAGS).  */
44369606e3fSchristos 
44469606e3fSchristos struct command_variable
44569606e3fSchristos   {
44669606e3fSchristos     struct command_variable *next;
44769606e3fSchristos     struct variable *variable;
44869606e3fSchristos   };
44969606e3fSchristos static struct command_variable *command_variables;
45069606e3fSchristos 
45169606e3fSchristos /* The name we were invoked with.  */
45269606e3fSchristos 
45369606e3fSchristos char *program;
45469606e3fSchristos 
45569606e3fSchristos /* Our current directory before processing any -C options.  */
45669606e3fSchristos 
45769606e3fSchristos char *directory_before_chdir;
45869606e3fSchristos 
45969606e3fSchristos /* Our current directory after processing all -C options.  */
46069606e3fSchristos 
46169606e3fSchristos char *starting_directory;
46269606e3fSchristos 
46369606e3fSchristos /* Value of the MAKELEVEL variable at startup (or 0).  */
46469606e3fSchristos 
46569606e3fSchristos unsigned int makelevel;
46669606e3fSchristos 
46769606e3fSchristos /* First file defined in the makefile whose name does not
46869606e3fSchristos    start with `.'.  This is the default to remake if the
46969606e3fSchristos    command line does not specify.  */
47069606e3fSchristos 
47169606e3fSchristos struct file *default_goal_file;
47269606e3fSchristos 
47369606e3fSchristos /* Pointer to the value of the .DEFAULT_GOAL special
47469606e3fSchristos    variable.  */
47569606e3fSchristos char ** default_goal_name;
47669606e3fSchristos 
47769606e3fSchristos /* Pointer to structure for the file .DEFAULT
47869606e3fSchristos    whose commands are used for any file that has none of its own.
47969606e3fSchristos    This is zero if the makefiles do not define .DEFAULT.  */
48069606e3fSchristos 
48169606e3fSchristos struct file *default_file;
48269606e3fSchristos 
48369606e3fSchristos /* Nonzero if we have seen the magic `.POSIX' target.
48469606e3fSchristos    This turns on pedantic compliance with POSIX.2.  */
48569606e3fSchristos 
48669606e3fSchristos int posix_pedantic;
48769606e3fSchristos 
48869606e3fSchristos /* Nonzero if we have seen the '.SECONDEXPANSION' target.
48969606e3fSchristos    This turns on secondary expansion of prerequisites.  */
49069606e3fSchristos 
49169606e3fSchristos int second_expansion;
49269606e3fSchristos 
49369606e3fSchristos /* Nonzero if we have seen the `.NOTPARALLEL' target.
49469606e3fSchristos    This turns off parallel builds for this invocation of make.  */
49569606e3fSchristos 
49669606e3fSchristos int not_parallel;
49769606e3fSchristos 
49869606e3fSchristos /* Nonzero if some rule detected clock skew; we keep track so (a) we only
49969606e3fSchristos    print one warning about it during the run, and (b) we can print a final
50069606e3fSchristos    warning at the end of the run. */
50169606e3fSchristos 
50269606e3fSchristos int clock_skew_detected;
50369606e3fSchristos 
50469606e3fSchristos /* Mask of signals that are being caught with fatal_error_signal.  */
50569606e3fSchristos 
50669606e3fSchristos #ifdef	POSIX
50769606e3fSchristos sigset_t fatal_signal_set;
50869606e3fSchristos #else
50969606e3fSchristos # ifdef	HAVE_SIGSETMASK
51069606e3fSchristos int fatal_signal_mask;
51169606e3fSchristos # endif
51269606e3fSchristos #endif
51369606e3fSchristos 
514*d9c0982bSkre #undef HAVE_BSD_SIGNAL		/* PR lib/58674 .. tools build fails */
515*d9c0982bSkre #undef bsd_signal		/* bsd_signal() has a weird history. skip it */
516*d9c0982bSkre 
51769606e3fSchristos #if !defined HAVE_BSD_SIGNAL && !defined bsd_signal
51869606e3fSchristos # if !defined HAVE_SIGACTION
51969606e3fSchristos #  define bsd_signal signal
52069606e3fSchristos # else
52169606e3fSchristos typedef RETSIGTYPE (*bsd_signal_ret_t) ();
52269606e3fSchristos 
523*d9c0982bSkre /*static*/ bsd_signal_ret_t
52469606e3fSchristos bsd_signal (int sig, bsd_signal_ret_t func)
52569606e3fSchristos {
52669606e3fSchristos   struct sigaction act, oact;
52769606e3fSchristos   act.sa_handler = func;
52869606e3fSchristos   act.sa_flags = SA_RESTART;
52969606e3fSchristos   sigemptyset (&act.sa_mask);
53069606e3fSchristos   sigaddset (&act.sa_mask, sig);
53169606e3fSchristos   if (sigaction (sig, &act, &oact) != 0)
53269606e3fSchristos     return SIG_ERR;
53369606e3fSchristos   return oact.sa_handler;
53469606e3fSchristos }
53569606e3fSchristos # endif
53669606e3fSchristos #endif
53769606e3fSchristos 
53869606e3fSchristos static void
53969606e3fSchristos initialize_global_hash_tables (void)
54069606e3fSchristos {
54169606e3fSchristos   init_hash_global_variable_set ();
54269606e3fSchristos   strcache_init ();
54369606e3fSchristos   init_hash_files ();
54469606e3fSchristos   hash_init_directories ();
54569606e3fSchristos   hash_init_function_table ();
54669606e3fSchristos }
54769606e3fSchristos 
54869606e3fSchristos static struct file *
54969606e3fSchristos enter_command_line_file (char *name)
55069606e3fSchristos {
55169606e3fSchristos   if (name[0] == '\0')
55269606e3fSchristos     fatal (NILF, _("empty string invalid as file name"));
55369606e3fSchristos 
55469606e3fSchristos   if (name[0] == '~')
55569606e3fSchristos     {
55669606e3fSchristos       char *expanded = tilde_expand (name);
55769606e3fSchristos       if (expanded != 0)
55869606e3fSchristos 	name = expanded;	/* Memory leak; I don't care.  */
55969606e3fSchristos     }
56069606e3fSchristos 
56169606e3fSchristos   /* This is also done in parse_file_seq, so this is redundant
56269606e3fSchristos      for names read from makefiles.  It is here for names passed
56369606e3fSchristos      on the command line.  */
56469606e3fSchristos   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
56569606e3fSchristos     {
56669606e3fSchristos       name += 2;
56769606e3fSchristos       while (*name == '/')
56869606e3fSchristos 	/* Skip following slashes: ".//foo" is "foo", not "/foo".  */
56969606e3fSchristos 	++name;
57069606e3fSchristos     }
57169606e3fSchristos 
57269606e3fSchristos   if (*name == '\0')
57369606e3fSchristos     {
57469606e3fSchristos       /* It was all slashes!  Move back to the dot and truncate
57569606e3fSchristos 	 it after the first slash, so it becomes just "./".  */
57669606e3fSchristos       do
57769606e3fSchristos 	--name;
57869606e3fSchristos       while (name[0] != '.');
57969606e3fSchristos       name[2] = '\0';
58069606e3fSchristos     }
58169606e3fSchristos 
58269606e3fSchristos   return enter_file (xstrdup (name));
58369606e3fSchristos }
58469606e3fSchristos 
58569606e3fSchristos /* Toggle -d on receipt of SIGUSR1.  */
58669606e3fSchristos 
58769606e3fSchristos #ifdef SIGUSR1
58869606e3fSchristos static RETSIGTYPE
58969606e3fSchristos debug_signal_handler (int sig UNUSED)
59069606e3fSchristos {
59169606e3fSchristos   db_level = db_level ? DB_NONE : DB_BASIC;
59269606e3fSchristos }
59369606e3fSchristos #endif
59469606e3fSchristos 
59569606e3fSchristos static void
59669606e3fSchristos decode_debug_flags (void)
59769606e3fSchristos {
59869606e3fSchristos   char **pp;
59969606e3fSchristos 
60069606e3fSchristos   if (debug_flag)
60169606e3fSchristos     db_level = DB_ALL;
60269606e3fSchristos 
60369606e3fSchristos   if (!db_flags)
60469606e3fSchristos     return;
60569606e3fSchristos 
60669606e3fSchristos   for (pp=db_flags->list; *pp; ++pp)
60769606e3fSchristos     {
60869606e3fSchristos       const char *p = *pp;
60969606e3fSchristos 
61069606e3fSchristos       while (1)
61169606e3fSchristos         {
61269606e3fSchristos           switch (tolower (p[0]))
61369606e3fSchristos             {
61469606e3fSchristos             case 'a':
61569606e3fSchristos               db_level |= DB_ALL;
61669606e3fSchristos               break;
61769606e3fSchristos             case 'b':
61869606e3fSchristos               db_level |= DB_BASIC;
61969606e3fSchristos               break;
62069606e3fSchristos             case 'i':
62169606e3fSchristos               db_level |= DB_BASIC | DB_IMPLICIT;
62269606e3fSchristos               break;
62369606e3fSchristos             case 'j':
62469606e3fSchristos               db_level |= DB_JOBS;
62569606e3fSchristos               break;
62669606e3fSchristos             case 'm':
62769606e3fSchristos               db_level |= DB_BASIC | DB_MAKEFILES;
62869606e3fSchristos               break;
62969606e3fSchristos             case 'v':
63069606e3fSchristos               db_level |= DB_BASIC | DB_VERBOSE;
63169606e3fSchristos               break;
63269606e3fSchristos             default:
63369606e3fSchristos               fatal (NILF, _("unknown debug level specification `%s'"), p);
63469606e3fSchristos             }
63569606e3fSchristos 
63669606e3fSchristos           while (*(++p) != '\0')
63769606e3fSchristos             if (*p == ',' || *p == ' ')
63869606e3fSchristos               break;
63969606e3fSchristos 
64069606e3fSchristos           if (*p == '\0')
64169606e3fSchristos             break;
64269606e3fSchristos 
64369606e3fSchristos           ++p;
64469606e3fSchristos         }
64569606e3fSchristos     }
64669606e3fSchristos }
64769606e3fSchristos 
64869606e3fSchristos #ifdef WINDOWS32
64969606e3fSchristos /*
65069606e3fSchristos  * HANDLE runtime exceptions by avoiding a requestor on the GUI. Capture
65169606e3fSchristos  * exception and print it to stderr instead.
65269606e3fSchristos  *
65369606e3fSchristos  * If ! DB_VERBOSE, just print a simple message and exit.
65469606e3fSchristos  * If DB_VERBOSE, print a more verbose message.
65569606e3fSchristos  * If compiled for DEBUG, let exception pass through to GUI so that
65669606e3fSchristos  *   debuggers can attach.
65769606e3fSchristos  */
65869606e3fSchristos LONG WINAPI
65969606e3fSchristos handle_runtime_exceptions( struct _EXCEPTION_POINTERS *exinfo )
66069606e3fSchristos {
66169606e3fSchristos   PEXCEPTION_RECORD exrec = exinfo->ExceptionRecord;
66269606e3fSchristos   LPSTR cmdline = GetCommandLine();
66369606e3fSchristos   LPSTR prg = strtok(cmdline, " ");
66469606e3fSchristos   CHAR errmsg[1024];
66569606e3fSchristos #ifdef USE_EVENT_LOG
66669606e3fSchristos   HANDLE hEventSource;
66769606e3fSchristos   LPTSTR lpszStrings[1];
66869606e3fSchristos #endif
66969606e3fSchristos 
67069606e3fSchristos   if (! ISDB (DB_VERBOSE))
67169606e3fSchristos     {
67269606e3fSchristos       sprintf(errmsg,
67369606e3fSchristos               _("%s: Interrupt/Exception caught (code = 0x%lx, addr = 0x%lx)\n"),
67469606e3fSchristos               prg, exrec->ExceptionCode, (DWORD)exrec->ExceptionAddress);
67569606e3fSchristos       fprintf(stderr, errmsg);
67669606e3fSchristos       exit(255);
67769606e3fSchristos     }
67869606e3fSchristos 
67969606e3fSchristos   sprintf(errmsg,
68069606e3fSchristos           _("\nUnhandled exception filter called from program %s\nExceptionCode = %lx\nExceptionFlags = %lx\nExceptionAddress = %lx\n"),
68169606e3fSchristos           prg, exrec->ExceptionCode, exrec->ExceptionFlags,
68269606e3fSchristos           (DWORD)exrec->ExceptionAddress);
68369606e3fSchristos 
68469606e3fSchristos   if (exrec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION
68569606e3fSchristos       && exrec->NumberParameters >= 2)
68669606e3fSchristos     sprintf(&errmsg[strlen(errmsg)],
68769606e3fSchristos             (exrec->ExceptionInformation[0]
68869606e3fSchristos              ? _("Access violation: write operation at address %lx\n")
68969606e3fSchristos              : _("Access violation: read operation at address %lx\n")),
69069606e3fSchristos             exrec->ExceptionInformation[1]);
69169606e3fSchristos 
69269606e3fSchristos   /* turn this on if we want to put stuff in the event log too */
69369606e3fSchristos #ifdef USE_EVENT_LOG
69469606e3fSchristos   hEventSource = RegisterEventSource(NULL, "GNU Make");
69569606e3fSchristos   lpszStrings[0] = errmsg;
69669606e3fSchristos 
69769606e3fSchristos   if (hEventSource != NULL)
69869606e3fSchristos     {
69969606e3fSchristos       ReportEvent(hEventSource,         /* handle of event source */
70069606e3fSchristos                   EVENTLOG_ERROR_TYPE,  /* event type */
70169606e3fSchristos                   0,                    /* event category */
70269606e3fSchristos                   0,                    /* event ID */
70369606e3fSchristos                   NULL,                 /* current user's SID */
70469606e3fSchristos                   1,                    /* strings in lpszStrings */
70569606e3fSchristos                   0,                    /* no bytes of raw data */
70669606e3fSchristos                   lpszStrings,          /* array of error strings */
70769606e3fSchristos                   NULL);                /* no raw data */
70869606e3fSchristos 
70969606e3fSchristos       (VOID) DeregisterEventSource(hEventSource);
71069606e3fSchristos     }
71169606e3fSchristos #endif
71269606e3fSchristos 
71369606e3fSchristos   /* Write the error to stderr too */
71469606e3fSchristos   fprintf(stderr, errmsg);
71569606e3fSchristos 
71669606e3fSchristos #ifdef DEBUG
71769606e3fSchristos   return EXCEPTION_CONTINUE_SEARCH;
71869606e3fSchristos #else
71969606e3fSchristos   exit(255);
72069606e3fSchristos   return (255); /* not reached */
72169606e3fSchristos #endif
72269606e3fSchristos }
72369606e3fSchristos 
72469606e3fSchristos /*
72569606e3fSchristos  * On WIN32 systems we don't have the luxury of a /bin directory that
72669606e3fSchristos  * is mapped globally to every drive mounted to the system. Since make could
72769606e3fSchristos  * be invoked from any drive, and we don't want to propogate /bin/sh
72869606e3fSchristos  * to every single drive. Allow ourselves a chance to search for
72969606e3fSchristos  * a value for default shell here (if the default path does not exist).
73069606e3fSchristos  */
73169606e3fSchristos 
73269606e3fSchristos int
73369606e3fSchristos find_and_set_default_shell (char *token)
73469606e3fSchristos {
73569606e3fSchristos   int sh_found = 0;
73669606e3fSchristos   char *search_token;
73769606e3fSchristos   char *tokend;
73869606e3fSchristos   PATH_VAR(sh_path);
73969606e3fSchristos   extern char *default_shell;
74069606e3fSchristos 
74169606e3fSchristos   if (!token)
74269606e3fSchristos     search_token = default_shell;
74369606e3fSchristos   else
74469606e3fSchristos     search_token = token;
74569606e3fSchristos 
74669606e3fSchristos 
74769606e3fSchristos   /* If the user explicitly requests the DOS cmd shell, obey that request.
74869606e3fSchristos      However, make sure that's what they really want by requiring the value
74969606e3fSchristos      of SHELL either equal, or have a final path element of, "cmd" or
75069606e3fSchristos      "cmd.exe" case-insensitive.  */
75169606e3fSchristos   tokend = search_token + strlen (search_token) - 3;
75269606e3fSchristos   if (((tokend == search_token
75369606e3fSchristos         || (tokend > search_token
75469606e3fSchristos             && (tokend[-1] == '/' || tokend[-1] == '\\')))
75569606e3fSchristos        && !strcmpi (tokend, "cmd"))
75669606e3fSchristos       || ((tokend - 4 == search_token
75769606e3fSchristos            || (tokend - 4 > search_token
75869606e3fSchristos                && (tokend[-5] == '/' || tokend[-5] == '\\')))
75969606e3fSchristos           && !strcmpi (tokend - 4, "cmd.exe"))) {
76069606e3fSchristos     batch_mode_shell = 1;
76169606e3fSchristos     unixy_shell = 0;
76269606e3fSchristos     sprintf (sh_path, "%s", search_token);
76369606e3fSchristos     default_shell = xstrdup (w32ify (sh_path, 0));
76469606e3fSchristos     DB (DB_VERBOSE,
76569606e3fSchristos         (_("find_and_set_shell setting default_shell = %s\n"), default_shell));
76669606e3fSchristos     sh_found = 1;
76769606e3fSchristos   } else if (!no_default_sh_exe &&
76869606e3fSchristos              (token == NULL || !strcmp (search_token, default_shell))) {
76969606e3fSchristos     /* no new information, path already set or known */
77069606e3fSchristos     sh_found = 1;
77169606e3fSchristos   } else if (file_exists_p(search_token)) {
77269606e3fSchristos     /* search token path was found */
77369606e3fSchristos     sprintf(sh_path, "%s", search_token);
77469606e3fSchristos     default_shell = xstrdup(w32ify(sh_path,0));
77569606e3fSchristos     DB (DB_VERBOSE,
77669606e3fSchristos         (_("find_and_set_shell setting default_shell = %s\n"), default_shell));
77769606e3fSchristos     sh_found = 1;
77869606e3fSchristos   } else {
77969606e3fSchristos     char *p;
78069606e3fSchristos     struct variable *v = lookup_variable (STRING_SIZE_TUPLE ("PATH"));
78169606e3fSchristos 
78269606e3fSchristos     /* Search Path for shell */
78369606e3fSchristos     if (v && v->value) {
78469606e3fSchristos       char *ep;
78569606e3fSchristos 
78669606e3fSchristos       p  = v->value;
78769606e3fSchristos       ep = strchr(p, PATH_SEPARATOR_CHAR);
78869606e3fSchristos 
78969606e3fSchristos       while (ep && *ep) {
79069606e3fSchristos         *ep = '\0';
79169606e3fSchristos 
79269606e3fSchristos         if (dir_file_exists_p(p, search_token)) {
79369606e3fSchristos           sprintf(sh_path, "%s/%s", p, search_token);
79469606e3fSchristos           default_shell = xstrdup(w32ify(sh_path,0));
79569606e3fSchristos           sh_found = 1;
79669606e3fSchristos           *ep = PATH_SEPARATOR_CHAR;
79769606e3fSchristos 
79869606e3fSchristos           /* terminate loop */
79969606e3fSchristos           p += strlen(p);
80069606e3fSchristos         } else {
80169606e3fSchristos           *ep = PATH_SEPARATOR_CHAR;
80269606e3fSchristos            p = ++ep;
80369606e3fSchristos         }
80469606e3fSchristos 
80569606e3fSchristos         ep = strchr(p, PATH_SEPARATOR_CHAR);
80669606e3fSchristos       }
80769606e3fSchristos 
80869606e3fSchristos       /* be sure to check last element of Path */
80969606e3fSchristos       if (p && *p && dir_file_exists_p(p, search_token)) {
81069606e3fSchristos           sprintf(sh_path, "%s/%s", p, search_token);
81169606e3fSchristos           default_shell = xstrdup(w32ify(sh_path,0));
81269606e3fSchristos           sh_found = 1;
81369606e3fSchristos       }
81469606e3fSchristos 
81569606e3fSchristos       if (sh_found)
81669606e3fSchristos         DB (DB_VERBOSE,
81769606e3fSchristos             (_("find_and_set_shell path search set default_shell = %s\n"),
81869606e3fSchristos              default_shell));
81969606e3fSchristos     }
82069606e3fSchristos   }
82169606e3fSchristos 
82269606e3fSchristos   /* naive test */
82369606e3fSchristos   if (!unixy_shell && sh_found &&
82469606e3fSchristos       (strstr(default_shell, "sh") || strstr(default_shell, "SH"))) {
82569606e3fSchristos     unixy_shell = 1;
82669606e3fSchristos     batch_mode_shell = 0;
82769606e3fSchristos   }
82869606e3fSchristos 
82969606e3fSchristos #ifdef BATCH_MODE_ONLY_SHELL
83069606e3fSchristos   batch_mode_shell = 1;
83169606e3fSchristos #endif
83269606e3fSchristos 
83369606e3fSchristos   return (sh_found);
83469606e3fSchristos }
83569606e3fSchristos #endif  /* WINDOWS32 */
83669606e3fSchristos 
83769606e3fSchristos #ifdef  __MSDOS__
83869606e3fSchristos 
83969606e3fSchristos static void
84069606e3fSchristos msdos_return_to_initial_directory (void)
84169606e3fSchristos {
84269606e3fSchristos   if (directory_before_chdir)
84369606e3fSchristos     chdir (directory_before_chdir);
84469606e3fSchristos }
84569606e3fSchristos #endif
84669606e3fSchristos 
84769606e3fSchristos extern char *mktemp PARAMS ((char *template));
84869606e3fSchristos extern int mkstemp PARAMS ((char *template));
84969606e3fSchristos 
85069606e3fSchristos FILE *
85169606e3fSchristos open_tmpfile(char **name, const char *template)
85269606e3fSchristos {
85369606e3fSchristos #ifdef HAVE_FDOPEN
85469606e3fSchristos   int fd;
85569606e3fSchristos #endif
85669606e3fSchristos 
85769606e3fSchristos #if defined HAVE_MKSTEMP || defined HAVE_MKTEMP
85869606e3fSchristos # define TEMPLATE_LEN   strlen (template)
85969606e3fSchristos #else
86069606e3fSchristos # define TEMPLATE_LEN   L_tmpnam
86169606e3fSchristos #endif
86269606e3fSchristos   *name = xmalloc (TEMPLATE_LEN + 1);
86369606e3fSchristos   strcpy (*name, template);
86469606e3fSchristos 
86569606e3fSchristos #if defined HAVE_MKSTEMP && defined HAVE_FDOPEN
86669606e3fSchristos   /* It's safest to use mkstemp(), if we can.  */
86769606e3fSchristos   fd = mkstemp (*name);
86869606e3fSchristos   if (fd == -1)
86969606e3fSchristos     return 0;
87069606e3fSchristos   return fdopen (fd, "w");
87169606e3fSchristos #else
87269606e3fSchristos # ifdef HAVE_MKTEMP
87369606e3fSchristos   (void) mktemp (*name);
87469606e3fSchristos # else
87569606e3fSchristos   (void) tmpnam (*name);
87669606e3fSchristos # endif
87769606e3fSchristos 
87869606e3fSchristos # ifdef HAVE_FDOPEN
87969606e3fSchristos   /* Can't use mkstemp(), but guard against a race condition.  */
88069606e3fSchristos   fd = open (*name, O_CREAT|O_EXCL|O_WRONLY, 0600);
88169606e3fSchristos   if (fd == -1)
88269606e3fSchristos     return 0;
88369606e3fSchristos   return fdopen (fd, "w");
88469606e3fSchristos # else
88569606e3fSchristos   /* Not secure, but what can we do?  */
88669606e3fSchristos   return fopen (*name, "w");
88769606e3fSchristos # endif
88869606e3fSchristos #endif
88969606e3fSchristos }
89069606e3fSchristos 
89169606e3fSchristos 
89269606e3fSchristos #ifdef _AMIGA
89369606e3fSchristos int
89469606e3fSchristos main (int argc, char **argv)
89569606e3fSchristos #else
89669606e3fSchristos int
89769606e3fSchristos main (int argc, char **argv, char **envp)
89869606e3fSchristos #endif
89969606e3fSchristos {
90069606e3fSchristos   static char *stdin_nm = 0;
90169606e3fSchristos   struct file *f;
90269606e3fSchristos   int i;
90369606e3fSchristos   int makefile_status = MAKE_SUCCESS;
90469606e3fSchristos   char **p;
90569606e3fSchristos   struct dep *read_makefiles;
90669606e3fSchristos   PATH_VAR (current_directory);
90769606e3fSchristos   unsigned int restarts = 0;
90869606e3fSchristos #ifdef WINDOWS32
90969606e3fSchristos   char *unix_path = NULL;
91069606e3fSchristos   char *windows32_path = NULL;
91169606e3fSchristos 
91269606e3fSchristos   SetUnhandledExceptionFilter(handle_runtime_exceptions);
91369606e3fSchristos 
91469606e3fSchristos   /* start off assuming we have no shell */
91569606e3fSchristos   unixy_shell = 0;
91669606e3fSchristos   no_default_sh_exe = 1;
91769606e3fSchristos #endif
91869606e3fSchristos 
91969606e3fSchristos #ifdef SET_STACK_SIZE
92069606e3fSchristos  /* Get rid of any avoidable limit on stack size.  */
92169606e3fSchristos   {
92269606e3fSchristos     struct rlimit rlim;
92369606e3fSchristos 
92469606e3fSchristos     /* Set the stack limit huge so that alloca does not fail.  */
92569606e3fSchristos     if (getrlimit (RLIMIT_STACK, &rlim) == 0)
92669606e3fSchristos       {
92769606e3fSchristos         rlim.rlim_cur = rlim.rlim_max;
92869606e3fSchristos         setrlimit (RLIMIT_STACK, &rlim);
92969606e3fSchristos       }
93069606e3fSchristos   }
93169606e3fSchristos #endif
93269606e3fSchristos 
93369606e3fSchristos #ifdef HAVE_ATEXIT
93469606e3fSchristos   atexit (close_stdout);
93569606e3fSchristos #endif
93669606e3fSchristos 
93769606e3fSchristos   /* Needed for OS/2 */
93869606e3fSchristos   initialize_main(&argc, &argv);
93969606e3fSchristos 
94069606e3fSchristos   default_goal_file = 0;
94169606e3fSchristos   reading_file = 0;
94269606e3fSchristos 
94369606e3fSchristos #if defined (__MSDOS__) && !defined (_POSIX_SOURCE)
94469606e3fSchristos   /* Request the most powerful version of `system', to
94569606e3fSchristos      make up for the dumb default shell.  */
94669606e3fSchristos   __system_flags = (__system_redirect
94769606e3fSchristos 		    | __system_use_shell
94869606e3fSchristos 		    | __system_allow_multiple_cmds
94969606e3fSchristos 		    | __system_allow_long_cmds
95069606e3fSchristos 		    | __system_handle_null_commands
95169606e3fSchristos 		    | __system_emulate_chdir);
95269606e3fSchristos 
95369606e3fSchristos #endif
95469606e3fSchristos 
95569606e3fSchristos   /* Set up gettext/internationalization support.  */
95669606e3fSchristos   setlocale (LC_ALL, "");
95769606e3fSchristos   bindtextdomain (PACKAGE, LOCALEDIR);
95869606e3fSchristos   textdomain (PACKAGE);
95969606e3fSchristos 
96069606e3fSchristos #ifdef	POSIX
96169606e3fSchristos   sigemptyset (&fatal_signal_set);
96269606e3fSchristos #define	ADD_SIG(sig)	sigaddset (&fatal_signal_set, sig)
96369606e3fSchristos #else
96469606e3fSchristos #ifdef	HAVE_SIGSETMASK
96569606e3fSchristos   fatal_signal_mask = 0;
96669606e3fSchristos #define	ADD_SIG(sig)	fatal_signal_mask |= sigmask (sig)
96769606e3fSchristos #else
96869606e3fSchristos #define	ADD_SIG(sig)
96969606e3fSchristos #endif
97069606e3fSchristos #endif
97169606e3fSchristos 
97269606e3fSchristos #define	FATAL_SIG(sig)							      \
97369606e3fSchristos   if (bsd_signal (sig, fatal_error_signal) == SIG_IGN)			      \
97469606e3fSchristos     bsd_signal (sig, SIG_IGN);						      \
97569606e3fSchristos   else									      \
97669606e3fSchristos     ADD_SIG (sig);
97769606e3fSchristos 
97869606e3fSchristos #ifdef SIGHUP
97969606e3fSchristos   FATAL_SIG (SIGHUP);
98069606e3fSchristos #endif
98169606e3fSchristos #ifdef SIGQUIT
98269606e3fSchristos   FATAL_SIG (SIGQUIT);
98369606e3fSchristos #endif
98469606e3fSchristos   FATAL_SIG (SIGINT);
98569606e3fSchristos   FATAL_SIG (SIGTERM);
98669606e3fSchristos 
98769606e3fSchristos #ifdef __MSDOS__
98869606e3fSchristos   /* Windows 9X delivers FP exceptions in child programs to their
98969606e3fSchristos      parent!  We don't want Make to die when a child divides by zero,
99069606e3fSchristos      so we work around that lossage by catching SIGFPE.  */
99169606e3fSchristos   FATAL_SIG (SIGFPE);
99269606e3fSchristos #endif
99369606e3fSchristos 
99469606e3fSchristos #ifdef	SIGDANGER
99569606e3fSchristos   FATAL_SIG (SIGDANGER);
99669606e3fSchristos #endif
99769606e3fSchristos #ifdef SIGXCPU
99869606e3fSchristos   FATAL_SIG (SIGXCPU);
99969606e3fSchristos #endif
100069606e3fSchristos #ifdef SIGXFSZ
100169606e3fSchristos   FATAL_SIG (SIGXFSZ);
100269606e3fSchristos #endif
100369606e3fSchristos 
100469606e3fSchristos #undef	FATAL_SIG
100569606e3fSchristos 
100669606e3fSchristos   /* Do not ignore the child-death signal.  This must be done before
100769606e3fSchristos      any children could possibly be created; otherwise, the wait
100869606e3fSchristos      functions won't work on systems with the SVR4 ECHILD brain
100969606e3fSchristos      damage, if our invoker is ignoring this signal.  */
101069606e3fSchristos 
101169606e3fSchristos #ifdef HAVE_WAIT_NOHANG
101269606e3fSchristos # if defined SIGCHLD
101369606e3fSchristos   (void) bsd_signal (SIGCHLD, SIG_DFL);
101469606e3fSchristos # endif
101569606e3fSchristos # if defined SIGCLD && SIGCLD != SIGCHLD
101669606e3fSchristos   (void) bsd_signal (SIGCLD, SIG_DFL);
101769606e3fSchristos # endif
101869606e3fSchristos #endif
101969606e3fSchristos 
102069606e3fSchristos   /* Make sure stdout is line-buffered.  */
102169606e3fSchristos 
102269606e3fSchristos #ifdef HAVE_SETVBUF
102369606e3fSchristos # ifdef SETVBUF_REVERSED
102469606e3fSchristos   setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
102569606e3fSchristos # else	/* setvbuf not reversed.  */
102669606e3fSchristos   /* Some buggy systems lose if we pass 0 instead of allocating ourselves.  */
102769606e3fSchristos   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
102869606e3fSchristos # endif	/* setvbuf reversed.  */
102969606e3fSchristos #elif HAVE_SETLINEBUF
103069606e3fSchristos   setlinebuf (stdout);
103169606e3fSchristos #endif	/* setlinebuf missing.  */
103269606e3fSchristos 
103369606e3fSchristos   /* Figure out where this program lives.  */
103469606e3fSchristos 
103569606e3fSchristos   if (argv[0] == 0)
103669606e3fSchristos     argv[0] = "";
103769606e3fSchristos   if (argv[0][0] == '\0')
103869606e3fSchristos     program = "make";
103969606e3fSchristos   else
104069606e3fSchristos     {
104169606e3fSchristos #ifdef VMS
104269606e3fSchristos       program = strrchr (argv[0], ']');
104369606e3fSchristos #else
104469606e3fSchristos       program = strrchr (argv[0], '/');
104569606e3fSchristos #endif
104669606e3fSchristos #if defined(__MSDOS__) || defined(__EMX__)
104769606e3fSchristos       if (program == 0)
104869606e3fSchristos 	program = strrchr (argv[0], '\\');
104969606e3fSchristos       else
105069606e3fSchristos 	{
105169606e3fSchristos 	  /* Some weird environments might pass us argv[0] with
105269606e3fSchristos 	     both kinds of slashes; we must find the rightmost.  */
105369606e3fSchristos 	  char *p = strrchr (argv[0], '\\');
105469606e3fSchristos 	  if (p && p > program)
105569606e3fSchristos 	    program = p;
105669606e3fSchristos 	}
105769606e3fSchristos       if (program == 0 && argv[0][1] == ':')
105869606e3fSchristos 	program = argv[0] + 1;
105969606e3fSchristos #endif
106069606e3fSchristos #ifdef WINDOWS32
106169606e3fSchristos       if (program == 0)
106269606e3fSchristos         {
106369606e3fSchristos           /* Extract program from full path */
106469606e3fSchristos           int argv0_len;
106569606e3fSchristos           program = strrchr (argv[0], '\\');
106669606e3fSchristos           if (program)
106769606e3fSchristos             {
106869606e3fSchristos               argv0_len = strlen(program);
106969606e3fSchristos               if (argv0_len > 4 && streq (&program[argv0_len - 4], ".exe"))
107069606e3fSchristos                 /* Remove .exe extension */
107169606e3fSchristos                 program[argv0_len - 4] = '\0';
107269606e3fSchristos             }
107369606e3fSchristos         }
107469606e3fSchristos #endif
107569606e3fSchristos       if (program == 0)
107669606e3fSchristos 	program = argv[0];
107769606e3fSchristos       else
107869606e3fSchristos 	++program;
107969606e3fSchristos     }
108069606e3fSchristos 
108169606e3fSchristos   /* Set up to access user data (files).  */
108269606e3fSchristos   user_access ();
108369606e3fSchristos 
108469606e3fSchristos   initialize_global_hash_tables ();
108569606e3fSchristos 
108669606e3fSchristos   /* Figure out where we are.  */
108769606e3fSchristos 
108869606e3fSchristos #ifdef WINDOWS32
108969606e3fSchristos   if (getcwd_fs (current_directory, GET_PATH_MAX) == 0)
109069606e3fSchristos #else
109169606e3fSchristos   if (getcwd (current_directory, GET_PATH_MAX) == 0)
109269606e3fSchristos #endif
109369606e3fSchristos     {
109469606e3fSchristos #ifdef	HAVE_GETCWD
109569606e3fSchristos       perror_with_name ("getcwd", "");
109669606e3fSchristos #else
109769606e3fSchristos       error (NILF, "getwd: %s", current_directory);
109869606e3fSchristos #endif
109969606e3fSchristos       current_directory[0] = '\0';
110069606e3fSchristos       directory_before_chdir = 0;
110169606e3fSchristos     }
110269606e3fSchristos   else
110369606e3fSchristos     directory_before_chdir = xstrdup (current_directory);
110469606e3fSchristos #ifdef  __MSDOS__
110569606e3fSchristos   /* Make sure we will return to the initial directory, come what may.  */
110669606e3fSchristos   atexit (msdos_return_to_initial_directory);
110769606e3fSchristos #endif
110869606e3fSchristos 
110969606e3fSchristos   /* Initialize the special variables.  */
111069606e3fSchristos   define_variable (".VARIABLES", 10, "", o_default, 0)->special = 1;
111169606e3fSchristos   /* define_variable (".TARGETS", 8, "", o_default, 0)->special = 1; */
111269606e3fSchristos 
111369606e3fSchristos   /* Set up .FEATURES */
111469606e3fSchristos   define_variable (".FEATURES", 9,
111569606e3fSchristos                    "target-specific order-only second-expansion else-if",
111669606e3fSchristos                    o_default, 0);
111769606e3fSchristos #ifndef NO_ARCHIVES
111869606e3fSchristos   do_variable_definition (NILF, ".FEATURES", "archives",
111969606e3fSchristos                           o_default, f_append, 0);
112069606e3fSchristos #endif
112169606e3fSchristos #ifdef MAKE_JOBSERVER
112269606e3fSchristos   do_variable_definition (NILF, ".FEATURES", "jobserver",
112369606e3fSchristos                           o_default, f_append, 0);
112469606e3fSchristos #endif
112569606e3fSchristos #ifdef MAKE_SYMLINKS
112669606e3fSchristos   do_variable_definition (NILF, ".FEATURES", "check-symlink",
112769606e3fSchristos                           o_default, f_append, 0);
112869606e3fSchristos #endif
112969606e3fSchristos 
113069606e3fSchristos   /* Read in variables from the environment.  It is important that this be
113169606e3fSchristos      done before $(MAKE) is figured out so its definitions will not be
113269606e3fSchristos      from the environment.  */
113369606e3fSchristos 
113469606e3fSchristos #ifndef _AMIGA
113569606e3fSchristos   for (i = 0; envp[i] != 0; ++i)
113669606e3fSchristos     {
113769606e3fSchristos       int do_not_define = 0;
113869606e3fSchristos       char *ep = envp[i];
113969606e3fSchristos 
114069606e3fSchristos       while (*ep != '\0' && *ep != '=')
114169606e3fSchristos         ++ep;
114269606e3fSchristos #ifdef WINDOWS32
114369606e3fSchristos       if (!unix_path && strneq(envp[i], "PATH=", 5))
114469606e3fSchristos         unix_path = ep+1;
114569606e3fSchristos       else if (!strnicmp(envp[i], "Path=", 5)) {
114669606e3fSchristos         do_not_define = 1; /* it gets defined after loop exits */
114769606e3fSchristos         if (!windows32_path)
114869606e3fSchristos           windows32_path = ep+1;
114969606e3fSchristos       }
115069606e3fSchristos #endif
115169606e3fSchristos       /* The result of pointer arithmetic is cast to unsigned int for
115269606e3fSchristos 	 machines where ptrdiff_t is a different size that doesn't widen
115369606e3fSchristos 	 the same.  */
115469606e3fSchristos       if (!do_not_define)
115569606e3fSchristos         {
115669606e3fSchristos           struct variable *v;
115769606e3fSchristos 
115869606e3fSchristos           v = define_variable (envp[i], (unsigned int) (ep - envp[i]),
115969606e3fSchristos                                ep + 1, o_env, 1);
116069606e3fSchristos           /* Force exportation of every variable culled from the environment.
116169606e3fSchristos              We used to rely on target_environment's v_default code to do this.
116269606e3fSchristos              But that does not work for the case where an environment variable
116369606e3fSchristos              is redefined in a makefile with `override'; it should then still
116469606e3fSchristos              be exported, because it was originally in the environment.  */
116569606e3fSchristos           v->export = v_export;
116669606e3fSchristos 
116769606e3fSchristos           /* Another wrinkle is that POSIX says the value of SHELL set in the
116869606e3fSchristos              makefile won't change the value of SHELL given to subprocesses  */
116969606e3fSchristos           if (streq (v->name, "SHELL"))
117069606e3fSchristos             {
117169606e3fSchristos #ifndef __MSDOS__
117269606e3fSchristos               v->export = v_noexport;
117369606e3fSchristos #endif
117469606e3fSchristos               shell_var.name = "SHELL";
117569606e3fSchristos               shell_var.value = xstrdup (ep + 1);
117669606e3fSchristos             }
117769606e3fSchristos 
117869606e3fSchristos           /* If MAKE_RESTARTS is set, remember it but don't export it.  */
117969606e3fSchristos           if (streq (v->name, "MAKE_RESTARTS"))
118069606e3fSchristos             {
118169606e3fSchristos               v->export = v_noexport;
118269606e3fSchristos               restarts = (unsigned int) atoi (ep + 1);
118369606e3fSchristos             }
118469606e3fSchristos         }
118569606e3fSchristos     }
118669606e3fSchristos #ifdef WINDOWS32
118769606e3fSchristos     /* If we didn't find a correctly spelled PATH we define PATH as
118869606e3fSchristos      * either the first mispelled value or an empty string
118969606e3fSchristos      */
119069606e3fSchristos     if (!unix_path)
119169606e3fSchristos       define_variable("PATH", 4,
119269606e3fSchristos                       windows32_path ? windows32_path : "",
119369606e3fSchristos                       o_env, 1)->export = v_export;
119469606e3fSchristos #endif
119569606e3fSchristos #else /* For Amiga, read the ENV: device, ignoring all dirs */
119669606e3fSchristos     {
119769606e3fSchristos 	BPTR env, file, old;
119869606e3fSchristos 	char buffer[1024];
119969606e3fSchristos 	int len;
120069606e3fSchristos 	__aligned struct FileInfoBlock fib;
120169606e3fSchristos 
120269606e3fSchristos 	env = Lock ("ENV:", ACCESS_READ);
120369606e3fSchristos 	if (env)
120469606e3fSchristos 	{
120569606e3fSchristos 	    old = CurrentDir (DupLock(env));
120669606e3fSchristos 	    Examine (env, &fib);
120769606e3fSchristos 
120869606e3fSchristos 	    while (ExNext (env, &fib))
120969606e3fSchristos 	    {
121069606e3fSchristos 		if (fib.fib_DirEntryType < 0) /* File */
121169606e3fSchristos 		{
121269606e3fSchristos 		    /* Define an empty variable. It will be filled in
121369606e3fSchristos 			variable_lookup(). Makes startup quite a bit
121469606e3fSchristos 			faster. */
121569606e3fSchristos 			define_variable (fib.fib_FileName,
121669606e3fSchristos 			    strlen (fib.fib_FileName),
121769606e3fSchristos 			"", o_env, 1)->export = v_export;
121869606e3fSchristos 		}
121969606e3fSchristos 	    }
122069606e3fSchristos 	    UnLock (env);
122169606e3fSchristos 	    UnLock(CurrentDir(old));
122269606e3fSchristos 	}
122369606e3fSchristos     }
122469606e3fSchristos #endif
122569606e3fSchristos 
122669606e3fSchristos   /* Decode the switches.  */
122769606e3fSchristos 
122869606e3fSchristos   decode_env_switches (STRING_SIZE_TUPLE ("MAKEFLAGS"));
122969606e3fSchristos #if 0
123069606e3fSchristos   /* People write things like:
123169606e3fSchristos      	MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
123269606e3fSchristos      and we set the -p, -i and -e switches.  Doesn't seem quite right.  */
123369606e3fSchristos   decode_env_switches (STRING_SIZE_TUPLE ("MFLAGS"));
123469606e3fSchristos #endif
123569606e3fSchristos   decode_switches (argc, argv, 0);
123669606e3fSchristos #ifdef WINDOWS32
123769606e3fSchristos   if (suspend_flag) {
123869606e3fSchristos         fprintf(stderr, "%s (pid = %ld)\n", argv[0], GetCurrentProcessId());
123969606e3fSchristos         fprintf(stderr, _("%s is suspending for 30 seconds..."), argv[0]);
124069606e3fSchristos         Sleep(30 * 1000);
124169606e3fSchristos         fprintf(stderr, _("done sleep(30). Continuing.\n"));
124269606e3fSchristos   }
124369606e3fSchristos #endif
124469606e3fSchristos 
124569606e3fSchristos   decode_debug_flags ();
124669606e3fSchristos 
124769606e3fSchristos   /* Set always_make_flag if -B was given and we've not restarted already.  */
124869606e3fSchristos   always_make_flag = always_make_set && (restarts == 0);
124969606e3fSchristos 
125069606e3fSchristos   /* Print version information.  */
125169606e3fSchristos   if (print_version_flag || print_data_base_flag || db_level)
125269606e3fSchristos     {
125369606e3fSchristos       print_version ();
125469606e3fSchristos 
125569606e3fSchristos       /* `make --version' is supposed to just print the version and exit.  */
125669606e3fSchristos       if (print_version_flag)
125769606e3fSchristos         die (0);
125869606e3fSchristos     }
125969606e3fSchristos 
126069606e3fSchristos #ifndef VMS
126169606e3fSchristos   /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
126269606e3fSchristos      (If it is a relative pathname with a slash, prepend our directory name
126369606e3fSchristos      so the result will run the same program regardless of the current dir.
126469606e3fSchristos      If it is a name with no slash, we can only hope that PATH did not
126569606e3fSchristos      find it in the current directory.)  */
126669606e3fSchristos #ifdef WINDOWS32
126769606e3fSchristos   /*
126869606e3fSchristos    * Convert from backslashes to forward slashes for
126969606e3fSchristos    * programs like sh which don't like them. Shouldn't
127069606e3fSchristos    * matter if the path is one way or the other for
127169606e3fSchristos    * CreateProcess().
127269606e3fSchristos    */
127369606e3fSchristos   if (strpbrk(argv[0], "/:\\") ||
127469606e3fSchristos       strstr(argv[0], "..") ||
127569606e3fSchristos       strneq(argv[0], "//", 2))
127669606e3fSchristos     argv[0] = xstrdup(w32ify(argv[0],1));
127769606e3fSchristos #else /* WINDOWS32 */
127869606e3fSchristos #if defined (__MSDOS__) || defined (__EMX__)
127969606e3fSchristos   if (strchr (argv[0], '\\'))
128069606e3fSchristos     {
128169606e3fSchristos       char *p;
128269606e3fSchristos 
128369606e3fSchristos       argv[0] = xstrdup (argv[0]);
128469606e3fSchristos       for (p = argv[0]; *p; p++)
128569606e3fSchristos 	if (*p == '\\')
128669606e3fSchristos 	  *p = '/';
128769606e3fSchristos     }
128869606e3fSchristos   /* If argv[0] is not in absolute form, prepend the current
128969606e3fSchristos      directory.  This can happen when Make is invoked by another DJGPP
129069606e3fSchristos      program that uses a non-absolute name.  */
129169606e3fSchristos   if (current_directory[0] != '\0'
129269606e3fSchristos       && argv[0] != 0
129369606e3fSchristos       && (argv[0][0] != '/' && (argv[0][0] == '\0' || argv[0][1] != ':'))
129469606e3fSchristos #ifdef __EMX__
129569606e3fSchristos       /* do not prepend cwd if argv[0] contains no '/', e.g. "make" */
129669606e3fSchristos       && (strchr (argv[0], '/') != 0 || strchr (argv[0], '\\') != 0)
129769606e3fSchristos # endif
129869606e3fSchristos       )
129969606e3fSchristos     argv[0] = concat (current_directory, "/", argv[0]);
130069606e3fSchristos #else  /* !__MSDOS__ */
130169606e3fSchristos   if (current_directory[0] != '\0'
130269606e3fSchristos       && argv[0] != 0 && argv[0][0] != '/' && strchr (argv[0], '/') != 0)
130369606e3fSchristos     argv[0] = concat (current_directory, "/", argv[0]);
130469606e3fSchristos #endif /* !__MSDOS__ */
130569606e3fSchristos #endif /* WINDOWS32 */
130669606e3fSchristos #endif
130769606e3fSchristos 
130869606e3fSchristos   /* The extra indirection through $(MAKE_COMMAND) is done
130969606e3fSchristos      for hysterical raisins.  */
131069606e3fSchristos   (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
131169606e3fSchristos   (void) define_variable ("MAKE", 4, "$(MAKE_COMMAND)", o_default, 1);
131269606e3fSchristos 
131369606e3fSchristos   if (command_variables != 0)
131469606e3fSchristos     {
131569606e3fSchristos       struct command_variable *cv;
131669606e3fSchristos       struct variable *v;
131769606e3fSchristos       unsigned int len = 0;
131869606e3fSchristos       char *value, *p;
131969606e3fSchristos 
132069606e3fSchristos       /* Figure out how much space will be taken up by the command-line
132169606e3fSchristos 	 variable definitions.  */
132269606e3fSchristos       for (cv = command_variables; cv != 0; cv = cv->next)
132369606e3fSchristos 	{
132469606e3fSchristos 	  v = cv->variable;
132569606e3fSchristos 	  len += 2 * strlen (v->name);
132669606e3fSchristos 	  if (! v->recursive)
132769606e3fSchristos 	    ++len;
132869606e3fSchristos 	  ++len;
132969606e3fSchristos 	  len += 2 * strlen (v->value);
133069606e3fSchristos 	  ++len;
133169606e3fSchristos 	}
133269606e3fSchristos 
133369606e3fSchristos       /* Now allocate a buffer big enough and fill it.  */
133469606e3fSchristos       p = value = (char *) alloca (len);
133569606e3fSchristos       for (cv = command_variables; cv != 0; cv = cv->next)
133669606e3fSchristos 	{
133769606e3fSchristos 	  v = cv->variable;
133869606e3fSchristos 	  p = quote_for_env (p, v->name);
133969606e3fSchristos 	  if (! v->recursive)
134069606e3fSchristos 	    *p++ = ':';
134169606e3fSchristos 	  *p++ = '=';
134269606e3fSchristos 	  p = quote_for_env (p, v->value);
134369606e3fSchristos 	  *p++ = ' ';
134469606e3fSchristos 	}
134569606e3fSchristos       p[-1] = '\0';		/* Kill the final space and terminate.  */
134669606e3fSchristos 
134769606e3fSchristos       /* Define an unchangeable variable with a name that no POSIX.2
134869606e3fSchristos 	 makefile could validly use for its own variable.  */
134969606e3fSchristos       (void) define_variable ("-*-command-variables-*-", 23,
135069606e3fSchristos 			      value, o_automatic, 0);
135169606e3fSchristos 
135269606e3fSchristos       /* Define the variable; this will not override any user definition.
135369606e3fSchristos          Normally a reference to this variable is written into the value of
135469606e3fSchristos          MAKEFLAGS, allowing the user to override this value to affect the
135569606e3fSchristos          exported value of MAKEFLAGS.  In POSIX-pedantic mode, we cannot
135669606e3fSchristos          allow the user's setting of MAKEOVERRIDES to affect MAKEFLAGS, so
135769606e3fSchristos          a reference to this hidden variable is written instead. */
135869606e3fSchristos       (void) define_variable ("MAKEOVERRIDES", 13,
135969606e3fSchristos 			      "${-*-command-variables-*-}", o_env, 1);
136069606e3fSchristos     }
136169606e3fSchristos 
136269606e3fSchristos   /* If there were -C flags, move ourselves about.  */
136369606e3fSchristos   if (directories != 0)
136469606e3fSchristos     for (i = 0; directories->list[i] != 0; ++i)
136569606e3fSchristos       {
136669606e3fSchristos 	char *dir = directories->list[i];
136769606e3fSchristos         char *expanded = 0;
136869606e3fSchristos 	if (dir[0] == '~')
136969606e3fSchristos 	  {
137069606e3fSchristos             expanded = tilde_expand (dir);
137169606e3fSchristos 	    if (expanded != 0)
137269606e3fSchristos 	      dir = expanded;
137369606e3fSchristos 	  }
137469606e3fSchristos #ifdef WINDOWS32
137569606e3fSchristos         /* WINDOWS32 chdir() doesn't work if the directory has a trailing '/'
137669606e3fSchristos            But allow -C/ just in case someone wants that.  */
137769606e3fSchristos         {
137869606e3fSchristos           char *p = dir + strlen (dir) - 1;
137969606e3fSchristos           while (p > dir && (p[0] == '/' || p[0] == '\\'))
138069606e3fSchristos             --p;
138169606e3fSchristos           p[1] = '\0';
138269606e3fSchristos         }
138369606e3fSchristos #endif
138469606e3fSchristos 	if (chdir (dir) < 0)
138569606e3fSchristos 	  pfatal_with_name (dir);
138669606e3fSchristos 	if (expanded)
138769606e3fSchristos 	  free (expanded);
138869606e3fSchristos       }
138969606e3fSchristos 
139069606e3fSchristos #ifdef WINDOWS32
139169606e3fSchristos   /*
139269606e3fSchristos    * THIS BLOCK OF CODE MUST COME AFTER chdir() CALL ABOVE IN ORDER
139369606e3fSchristos    * TO NOT CONFUSE THE DEPENDENCY CHECKING CODE IN implicit.c.
139469606e3fSchristos    *
139569606e3fSchristos    * The functions in dir.c can incorrectly cache information for "."
139669606e3fSchristos    * before we have changed directory and this can cause file
139769606e3fSchristos    * lookups to fail because the current directory (.) was pointing
139869606e3fSchristos    * at the wrong place when it was first evaluated.
139969606e3fSchristos    */
140069606e3fSchristos    no_default_sh_exe = !find_and_set_default_shell(NULL);
140169606e3fSchristos 
140269606e3fSchristos #endif /* WINDOWS32 */
140369606e3fSchristos   /* Figure out the level of recursion.  */
140469606e3fSchristos   {
140569606e3fSchristos     struct variable *v = lookup_variable (STRING_SIZE_TUPLE (MAKELEVEL_NAME));
140669606e3fSchristos     if (v != 0 && v->value[0] != '\0' && v->value[0] != '-')
140769606e3fSchristos       makelevel = (unsigned int) atoi (v->value);
140869606e3fSchristos     else
140969606e3fSchristos       makelevel = 0;
141069606e3fSchristos   }
141169606e3fSchristos 
141269606e3fSchristos   /* Except under -s, always do -w in sub-makes and under -C.  */
141369606e3fSchristos   if (!silent_flag && (directories != 0 || makelevel > 0))
141469606e3fSchristos     print_directory_flag = 1;
141569606e3fSchristos 
141669606e3fSchristos   /* Let the user disable that with --no-print-directory.  */
141769606e3fSchristos   if (inhibit_print_directory_flag)
141869606e3fSchristos     print_directory_flag = 0;
141969606e3fSchristos 
142069606e3fSchristos   /* If -R was given, set -r too (doesn't make sense otherwise!)  */
142169606e3fSchristos   if (no_builtin_variables_flag)
142269606e3fSchristos     no_builtin_rules_flag = 1;
142369606e3fSchristos 
142469606e3fSchristos   /* Construct the list of include directories to search.  */
142569606e3fSchristos 
142669606e3fSchristos   construct_include_path (include_directories == 0 ? (char **) 0
142769606e3fSchristos 			  : include_directories->list);
142869606e3fSchristos 
142969606e3fSchristos   /* Figure out where we are now, after chdir'ing.  */
143069606e3fSchristos   if (directories == 0)
143169606e3fSchristos     /* We didn't move, so we're still in the same place.  */
143269606e3fSchristos     starting_directory = current_directory;
143369606e3fSchristos   else
143469606e3fSchristos     {
143569606e3fSchristos #ifdef WINDOWS32
143669606e3fSchristos       if (getcwd_fs (current_directory, GET_PATH_MAX) == 0)
143769606e3fSchristos #else
143869606e3fSchristos       if (getcwd (current_directory, GET_PATH_MAX) == 0)
143969606e3fSchristos #endif
144069606e3fSchristos 	{
144169606e3fSchristos #ifdef	HAVE_GETCWD
144269606e3fSchristos 	  perror_with_name ("getcwd", "");
144369606e3fSchristos #else
144469606e3fSchristos 	  error (NILF, "getwd: %s", current_directory);
144569606e3fSchristos #endif
144669606e3fSchristos 	  starting_directory = 0;
144769606e3fSchristos 	}
144869606e3fSchristos       else
144969606e3fSchristos 	starting_directory = current_directory;
145069606e3fSchristos     }
145169606e3fSchristos 
145269606e3fSchristos   (void) define_variable ("CURDIR", 6, current_directory, o_file, 0);
145369606e3fSchristos 
145469606e3fSchristos   /* Read any stdin makefiles into temporary files.  */
145569606e3fSchristos 
145669606e3fSchristos   if (makefiles != 0)
145769606e3fSchristos     {
145869606e3fSchristos       register unsigned int i;
145969606e3fSchristos       for (i = 0; i < makefiles->idx; ++i)
146069606e3fSchristos 	if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
146169606e3fSchristos 	  {
146269606e3fSchristos 	    /* This makefile is standard input.  Since we may re-exec
146369606e3fSchristos 	       and thus re-read the makefiles, we read standard input
146469606e3fSchristos 	       into a temporary file and read from that.  */
146569606e3fSchristos 	    FILE *outfile;
146669606e3fSchristos             char *template, *tmpdir;
146769606e3fSchristos 
146869606e3fSchristos             if (stdin_nm)
146969606e3fSchristos               fatal (NILF, _("Makefile from standard input specified twice."));
147069606e3fSchristos 
147169606e3fSchristos #ifdef VMS
147269606e3fSchristos # define DEFAULT_TMPDIR     "sys$scratch:"
147369606e3fSchristos #else
147469606e3fSchristos # ifdef P_tmpdir
147569606e3fSchristos #  define DEFAULT_TMPDIR    P_tmpdir
147669606e3fSchristos # else
147769606e3fSchristos #  define DEFAULT_TMPDIR    "/tmp"
147869606e3fSchristos # endif
147969606e3fSchristos #endif
148069606e3fSchristos #define DEFAULT_TMPFILE     "GmXXXXXX"
148169606e3fSchristos 
148269606e3fSchristos 	    if (((tmpdir = getenv ("TMPDIR")) == NULL || *tmpdir == '\0')
148369606e3fSchristos #if defined (__MSDOS__) || defined (WINDOWS32) || defined (__EMX__)
148469606e3fSchristos                 /* These are also used commonly on these platforms.  */
148569606e3fSchristos                 && ((tmpdir = getenv ("TEMP")) == NULL || *tmpdir == '\0')
148669606e3fSchristos                 && ((tmpdir = getenv ("TMP")) == NULL || *tmpdir == '\0')
148769606e3fSchristos #endif
148869606e3fSchristos                )
148969606e3fSchristos 	      tmpdir = DEFAULT_TMPDIR;
149069606e3fSchristos 
149169606e3fSchristos             template = (char *) alloca (strlen (tmpdir)
149269606e3fSchristos                                         + sizeof (DEFAULT_TMPFILE) + 1);
149369606e3fSchristos 	    strcpy (template, tmpdir);
149469606e3fSchristos 
149569606e3fSchristos #ifdef HAVE_DOS_PATHS
149669606e3fSchristos 	    if (strchr ("/\\", template[strlen (template) - 1]) == NULL)
149769606e3fSchristos 	      strcat (template, "/");
149869606e3fSchristos #else
149969606e3fSchristos # ifndef VMS
150069606e3fSchristos 	    if (template[strlen (template) - 1] != '/')
150169606e3fSchristos 	      strcat (template, "/");
150269606e3fSchristos # endif /* !VMS */
150369606e3fSchristos #endif /* !HAVE_DOS_PATHS */
150469606e3fSchristos 
150569606e3fSchristos 	    strcat (template, DEFAULT_TMPFILE);
150669606e3fSchristos 	    outfile = open_tmpfile (&stdin_nm, template);
150769606e3fSchristos 	    if (outfile == 0)
150869606e3fSchristos 	      pfatal_with_name (_("fopen (temporary file)"));
150969606e3fSchristos 	    while (!feof (stdin) && ! ferror (stdin))
151069606e3fSchristos 	      {
151169606e3fSchristos 		char buf[2048];
151269606e3fSchristos 		unsigned int n = fread (buf, 1, sizeof (buf), stdin);
151369606e3fSchristos 		if (n > 0 && fwrite (buf, 1, n, outfile) != n)
151469606e3fSchristos 		  pfatal_with_name (_("fwrite (temporary file)"));
151569606e3fSchristos 	      }
151669606e3fSchristos 	    (void) fclose (outfile);
151769606e3fSchristos 
151869606e3fSchristos 	    /* Replace the name that read_all_makefiles will
151969606e3fSchristos 	       see with the name of the temporary file.  */
152069606e3fSchristos             makefiles->list[i] = xstrdup (stdin_nm);
152169606e3fSchristos 
152269606e3fSchristos 	    /* Make sure the temporary file will not be remade.  */
152369606e3fSchristos 	    f = enter_file (stdin_nm);
152469606e3fSchristos 	    f->updated = 1;
152569606e3fSchristos 	    f->update_status = 0;
152669606e3fSchristos 	    f->command_state = cs_finished;
152769606e3fSchristos  	    /* Can't be intermediate, or it'll be removed too early for
152869606e3fSchristos                make re-exec.  */
152969606e3fSchristos  	    f->intermediate = 0;
153069606e3fSchristos 	    f->dontcare = 0;
153169606e3fSchristos 	  }
153269606e3fSchristos     }
153369606e3fSchristos 
153469606e3fSchristos #ifndef __EMX__ /* Don't use a SIGCHLD handler for OS/2 */
153569606e3fSchristos #if defined(MAKE_JOBSERVER) || !defined(HAVE_WAIT_NOHANG)
153669606e3fSchristos   /* Set up to handle children dying.  This must be done before
153769606e3fSchristos      reading in the makefiles so that `shell' function calls will work.
153869606e3fSchristos 
153969606e3fSchristos      If we don't have a hanging wait we have to fall back to old, broken
154069606e3fSchristos      functionality here and rely on the signal handler and counting
154169606e3fSchristos      children.
154269606e3fSchristos 
154369606e3fSchristos      If we're using the jobs pipe we need a signal handler so that
154469606e3fSchristos      SIGCHLD is not ignored; we need it to interrupt the read(2) of the
154569606e3fSchristos      jobserver pipe in job.c if we're waiting for a token.
154669606e3fSchristos 
154769606e3fSchristos      If none of these are true, we don't need a signal handler at all.  */
154869606e3fSchristos   {
154969606e3fSchristos     extern RETSIGTYPE child_handler PARAMS ((int sig));
155069606e3fSchristos # if defined SIGCHLD
155169606e3fSchristos     bsd_signal (SIGCHLD, child_handler);
155269606e3fSchristos # endif
155369606e3fSchristos # if defined SIGCLD && SIGCLD != SIGCHLD
155469606e3fSchristos     bsd_signal (SIGCLD, child_handler);
155569606e3fSchristos # endif
155669606e3fSchristos   }
155769606e3fSchristos #endif
155869606e3fSchristos #endif
155969606e3fSchristos 
156069606e3fSchristos   /* Let the user send us SIGUSR1 to toggle the -d flag during the run.  */
156169606e3fSchristos #ifdef SIGUSR1
156269606e3fSchristos   bsd_signal (SIGUSR1, debug_signal_handler);
156369606e3fSchristos #endif
156469606e3fSchristos 
156569606e3fSchristos   /* Define the initial list of suffixes for old-style rules.  */
156669606e3fSchristos 
156769606e3fSchristos   set_default_suffixes ();
156869606e3fSchristos 
156969606e3fSchristos   /* Define the file rules for the built-in suffix rules.  These will later
157069606e3fSchristos      be converted into pattern rules.  We used to do this in
157169606e3fSchristos      install_default_implicit_rules, but since that happens after reading
157269606e3fSchristos      makefiles, it results in the built-in pattern rules taking precedence
157369606e3fSchristos      over makefile-specified suffix rules, which is wrong.  */
157469606e3fSchristos 
157569606e3fSchristos   install_default_suffix_rules ();
157669606e3fSchristos 
157769606e3fSchristos   /* Define some internal and special variables.  */
157869606e3fSchristos 
157969606e3fSchristos   define_automatic_variables ();
158069606e3fSchristos 
158169606e3fSchristos   /* Set up the MAKEFLAGS and MFLAGS variables
158269606e3fSchristos      so makefiles can look at them.  */
158369606e3fSchristos 
158469606e3fSchristos   define_makeflags (0, 0);
158569606e3fSchristos 
158669606e3fSchristos   /* Define the default variables.  */
158769606e3fSchristos   define_default_variables ();
158869606e3fSchristos 
158969606e3fSchristos   default_file = enter_file (".DEFAULT");
159069606e3fSchristos 
159169606e3fSchristos   {
159269606e3fSchristos     struct variable *v = define_variable (".DEFAULT_GOAL", 13, "", o_file, 0);
159369606e3fSchristos     default_goal_name = &v->value;
159469606e3fSchristos   }
159569606e3fSchristos 
159669606e3fSchristos   /* Read all the makefiles.  */
159769606e3fSchristos 
159869606e3fSchristos   read_makefiles
159969606e3fSchristos     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
160069606e3fSchristos 
160169606e3fSchristos #ifdef WINDOWS32
160269606e3fSchristos   /* look one last time after reading all Makefiles */
160369606e3fSchristos   if (no_default_sh_exe)
160469606e3fSchristos     no_default_sh_exe = !find_and_set_default_shell(NULL);
160569606e3fSchristos #endif /* WINDOWS32 */
160669606e3fSchristos 
160769606e3fSchristos #if defined (__MSDOS__) || defined (__EMX__)
160869606e3fSchristos   /* We need to know what kind of shell we will be using.  */
160969606e3fSchristos   {
161069606e3fSchristos     extern int _is_unixy_shell (const char *_path);
161169606e3fSchristos     struct variable *shv = lookup_variable (STRING_SIZE_TUPLE ("SHELL"));
161269606e3fSchristos     extern int unixy_shell;
161369606e3fSchristos     extern char *default_shell;
161469606e3fSchristos 
161569606e3fSchristos     if (shv && *shv->value)
161669606e3fSchristos       {
161769606e3fSchristos 	char *shell_path = recursively_expand(shv);
161869606e3fSchristos 
161969606e3fSchristos 	if (shell_path && _is_unixy_shell (shell_path))
162069606e3fSchristos 	  unixy_shell = 1;
162169606e3fSchristos 	else
162269606e3fSchristos 	  unixy_shell = 0;
162369606e3fSchristos 	if (shell_path)
162469606e3fSchristos 	  default_shell = shell_path;
162569606e3fSchristos       }
162669606e3fSchristos   }
162769606e3fSchristos #endif /* __MSDOS__ || __EMX__ */
162869606e3fSchristos 
162969606e3fSchristos   /* Decode switches again, in case the variables were set by the makefile.  */
163069606e3fSchristos   decode_env_switches (STRING_SIZE_TUPLE ("MAKEFLAGS"));
163169606e3fSchristos #if 0
163269606e3fSchristos   decode_env_switches (STRING_SIZE_TUPLE ("MFLAGS"));
163369606e3fSchristos #endif
163469606e3fSchristos 
163569606e3fSchristos #if defined (__MSDOS__) || defined (__EMX__)
163669606e3fSchristos   if (job_slots != 1
163769606e3fSchristos # ifdef __EMX__
163869606e3fSchristos       && _osmode != OS2_MODE /* turn off -j if we are in DOS mode */
163969606e3fSchristos # endif
164069606e3fSchristos       )
164169606e3fSchristos     {
164269606e3fSchristos       error (NILF,
164369606e3fSchristos              _("Parallel jobs (-j) are not supported on this platform."));
164469606e3fSchristos       error (NILF, _("Resetting to single job (-j1) mode."));
164569606e3fSchristos       job_slots = 1;
164669606e3fSchristos     }
164769606e3fSchristos #endif
164869606e3fSchristos 
164969606e3fSchristos #ifdef MAKE_JOBSERVER
165069606e3fSchristos   /* If the jobserver-fds option is seen, make sure that -j is reasonable.  */
165169606e3fSchristos 
165269606e3fSchristos   if (jobserver_fds)
165369606e3fSchristos   {
165469606e3fSchristos     char *cp;
165569606e3fSchristos     unsigned int ui;
165669606e3fSchristos 
165769606e3fSchristos     for (ui=1; ui < jobserver_fds->idx; ++ui)
165869606e3fSchristos       if (!streq (jobserver_fds->list[0], jobserver_fds->list[ui]))
165969606e3fSchristos         fatal (NILF, _("internal error: multiple --jobserver-fds options"));
166069606e3fSchristos 
166169606e3fSchristos     /* Now parse the fds string and make sure it has the proper format.  */
166269606e3fSchristos 
166369606e3fSchristos     cp = jobserver_fds->list[0];
166469606e3fSchristos 
166569606e3fSchristos     if (sscanf (cp, "%d,%d", &job_fds[0], &job_fds[1]) != 2)
166669606e3fSchristos       fatal (NILF,
166769606e3fSchristos              _("internal error: invalid --jobserver-fds string `%s'"), cp);
166869606e3fSchristos 
166969606e3fSchristos     /* The combination of a pipe + !job_slots means we're using the
167069606e3fSchristos        jobserver.  If !job_slots and we don't have a pipe, we can start
167169606e3fSchristos        infinite jobs.  If we see both a pipe and job_slots >0 that means the
167269606e3fSchristos        user set -j explicitly.  This is broken; in this case obey the user
167369606e3fSchristos        (ignore the jobserver pipe for this make) but print a message.  */
167469606e3fSchristos 
167569606e3fSchristos     if (job_slots > 0)
167669606e3fSchristos       error (NILF,
167769606e3fSchristos              _("warning: -jN forced in submake: disabling jobserver mode."));
167869606e3fSchristos 
167969606e3fSchristos     /* Create a duplicate pipe, that will be closed in the SIGCHLD
168069606e3fSchristos        handler.  If this fails with EBADF, the parent has closed the pipe
168169606e3fSchristos        on us because it didn't think we were a submake.  If so, print a
168269606e3fSchristos        warning then default to -j1.  */
168369606e3fSchristos 
168469606e3fSchristos     else if ((job_rfd = dup (job_fds[0])) < 0)
168569606e3fSchristos       {
168669606e3fSchristos         if (errno != EBADF)
168769606e3fSchristos           pfatal_with_name (_("dup jobserver"));
168869606e3fSchristos 
168969606e3fSchristos         error (NILF,
169069606e3fSchristos                _("warning: jobserver unavailable: using -j1.  Add `+' to parent make rule."));
169169606e3fSchristos         job_slots = 1;
169269606e3fSchristos       }
169369606e3fSchristos 
169469606e3fSchristos     if (job_slots > 0)
169569606e3fSchristos       {
169669606e3fSchristos         close (job_fds[0]);
169769606e3fSchristos         close (job_fds[1]);
169869606e3fSchristos         job_fds[0] = job_fds[1] = -1;
169969606e3fSchristos         free (jobserver_fds->list);
170069606e3fSchristos         free (jobserver_fds);
170169606e3fSchristos         jobserver_fds = 0;
170269606e3fSchristos       }
170369606e3fSchristos   }
170469606e3fSchristos 
170569606e3fSchristos   /* If we have >1 slot but no jobserver-fds, then we're a top-level make.
170669606e3fSchristos      Set up the pipe and install the fds option for our children.  */
170769606e3fSchristos 
170869606e3fSchristos   if (job_slots > 1)
170969606e3fSchristos     {
171069606e3fSchristos       char c = '+';
171169606e3fSchristos 
171269606e3fSchristos       if (pipe (job_fds) < 0 || (job_rfd = dup (job_fds[0])) < 0)
171369606e3fSchristos 	pfatal_with_name (_("creating jobs pipe"));
171469606e3fSchristos 
171569606e3fSchristos       /* Every make assumes that it always has one job it can run.  For the
171669606e3fSchristos          submakes it's the token they were given by their parent.  For the
171769606e3fSchristos          top make, we just subtract one from the number the user wants.  We
171869606e3fSchristos          want job_slots to be 0 to indicate we're using the jobserver.  */
171969606e3fSchristos 
172069606e3fSchristos       master_job_slots = job_slots;
172169606e3fSchristos 
172269606e3fSchristos       while (--job_slots)
172369606e3fSchristos         {
172469606e3fSchristos           int r;
172569606e3fSchristos 
172669606e3fSchristos           EINTRLOOP (r, write (job_fds[1], &c, 1));
172769606e3fSchristos           if (r != 1)
172869606e3fSchristos             pfatal_with_name (_("init jobserver pipe"));
172969606e3fSchristos         }
173069606e3fSchristos 
173169606e3fSchristos       /* Fill in the jobserver_fds struct for our children.  */
173269606e3fSchristos 
173369606e3fSchristos       jobserver_fds = (struct stringlist *)
173469606e3fSchristos                         xmalloc (sizeof (struct stringlist));
173569606e3fSchristos       jobserver_fds->list = (char **) xmalloc (sizeof (char *));
173669606e3fSchristos       jobserver_fds->list[0] = xmalloc ((sizeof ("1024")*2)+1);
173769606e3fSchristos 
173869606e3fSchristos       sprintf (jobserver_fds->list[0], "%d,%d", job_fds[0], job_fds[1]);
173969606e3fSchristos       jobserver_fds->idx = 1;
174069606e3fSchristos       jobserver_fds->max = 1;
174169606e3fSchristos     }
174269606e3fSchristos #endif
174369606e3fSchristos 
174469606e3fSchristos #ifndef MAKE_SYMLINKS
174569606e3fSchristos   if (check_symlink_flag)
174669606e3fSchristos     {
174769606e3fSchristos       error (NILF, _("Symbolic links not supported: disabling -L."));
174869606e3fSchristos       check_symlink_flag = 0;
174969606e3fSchristos     }
175069606e3fSchristos #endif
175169606e3fSchristos 
175269606e3fSchristos   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */
175369606e3fSchristos 
175469606e3fSchristos   define_makeflags (1, 0);
175569606e3fSchristos 
175669606e3fSchristos   /* Make each `struct dep' point at the `struct file' for the file
175769606e3fSchristos      depended on.  Also do magic for special targets.  */
175869606e3fSchristos 
175969606e3fSchristos   snap_deps ();
176069606e3fSchristos 
176169606e3fSchristos   /* Convert old-style suffix rules to pattern rules.  It is important to
176269606e3fSchristos      do this before installing the built-in pattern rules below, so that
176369606e3fSchristos      makefile-specified suffix rules take precedence over built-in pattern
176469606e3fSchristos      rules.  */
176569606e3fSchristos 
176669606e3fSchristos   convert_to_pattern ();
176769606e3fSchristos 
176869606e3fSchristos   /* Install the default implicit pattern rules.
176969606e3fSchristos      This used to be done before reading the makefiles.
177069606e3fSchristos      But in that case, built-in pattern rules were in the chain
177169606e3fSchristos      before user-defined ones, so they matched first.  */
177269606e3fSchristos 
177369606e3fSchristos   install_default_implicit_rules ();
177469606e3fSchristos 
177569606e3fSchristos   /* Compute implicit rule limits.  */
177669606e3fSchristos 
177769606e3fSchristos   count_implicit_rule_limits ();
177869606e3fSchristos 
177969606e3fSchristos   /* Construct the listings of directories in VPATH lists.  */
178069606e3fSchristos 
178169606e3fSchristos   build_vpath_lists ();
178269606e3fSchristos 
178369606e3fSchristos   /* Mark files given with -o flags as very old and as having been updated
178469606e3fSchristos      already, and files given with -W flags as brand new (time-stamp as far
178569606e3fSchristos      as possible into the future).  If restarts is set we'll do -W later.  */
178669606e3fSchristos 
178769606e3fSchristos   if (old_files != 0)
178869606e3fSchristos     for (p = old_files->list; *p != 0; ++p)
178969606e3fSchristos       {
179069606e3fSchristos 	f = enter_command_line_file (*p);
179169606e3fSchristos 	f->last_mtime = f->mtime_before_update = OLD_MTIME;
179269606e3fSchristos 	f->updated = 1;
179369606e3fSchristos 	f->update_status = 0;
179469606e3fSchristos 	f->command_state = cs_finished;
179569606e3fSchristos       }
179669606e3fSchristos 
179769606e3fSchristos   if (!restarts && new_files != 0)
179869606e3fSchristos     {
179969606e3fSchristos       for (p = new_files->list; *p != 0; ++p)
180069606e3fSchristos 	{
180169606e3fSchristos 	  f = enter_command_line_file (*p);
180269606e3fSchristos 	  f->last_mtime = f->mtime_before_update = NEW_MTIME;
180369606e3fSchristos 	}
180469606e3fSchristos     }
180569606e3fSchristos 
180669606e3fSchristos   /* Initialize the remote job module.  */
180769606e3fSchristos   remote_setup ();
180869606e3fSchristos 
180969606e3fSchristos   if (read_makefiles != 0)
181069606e3fSchristos     {
181169606e3fSchristos       /* Update any makefiles if necessary.  */
181269606e3fSchristos 
181369606e3fSchristos       FILE_TIMESTAMP *makefile_mtimes = 0;
181469606e3fSchristos       unsigned int mm_idx = 0;
181569606e3fSchristos       char **nargv = argv;
181669606e3fSchristos       int nargc = argc;
181769606e3fSchristos       int orig_db_level = db_level;
181869606e3fSchristos       int status;
181969606e3fSchristos 
182069606e3fSchristos       if (! ISDB (DB_MAKEFILES))
182169606e3fSchristos         db_level = DB_NONE;
182269606e3fSchristos 
182369606e3fSchristos       DB (DB_BASIC, (_("Updating makefiles....\n")));
182469606e3fSchristos 
182569606e3fSchristos       /* Remove any makefiles we don't want to try to update.
182669606e3fSchristos 	 Also record the current modtimes so we can compare them later.  */
182769606e3fSchristos       {
182869606e3fSchristos 	register struct dep *d, *last;
182969606e3fSchristos 	last = 0;
183069606e3fSchristos 	d = read_makefiles;
183169606e3fSchristos 	while (d != 0)
183269606e3fSchristos 	  {
183369606e3fSchristos 	    register struct file *f = d->file;
183469606e3fSchristos 	    if (f->double_colon)
183569606e3fSchristos 	      for (f = f->double_colon; f != NULL; f = f->prev)
183669606e3fSchristos 		{
183769606e3fSchristos 		  if (f->deps == 0 && f->cmds != 0)
183869606e3fSchristos 		    {
183969606e3fSchristos 		      /* This makefile is a :: target with commands, but
184069606e3fSchristos 			 no dependencies.  So, it will always be remade.
184169606e3fSchristos 			 This might well cause an infinite loop, so don't
184269606e3fSchristos 			 try to remake it.  (This will only happen if
184369606e3fSchristos 			 your makefiles are written exceptionally
184469606e3fSchristos 			 stupidly; but if you work for Athena, that's how
184569606e3fSchristos 			 you write your makefiles.)  */
184669606e3fSchristos 
184769606e3fSchristos 		      DB (DB_VERBOSE,
184869606e3fSchristos                           (_("Makefile `%s' might loop; not remaking it.\n"),
184969606e3fSchristos                            f->name));
185069606e3fSchristos 
185169606e3fSchristos 		      if (last == 0)
185269606e3fSchristos 			read_makefiles = d->next;
185369606e3fSchristos 		      else
185469606e3fSchristos 			last->next = d->next;
185569606e3fSchristos 
185669606e3fSchristos 		      /* Free the storage.  */
185769606e3fSchristos                       free_dep (d);
185869606e3fSchristos 
185969606e3fSchristos 		      d = last == 0 ? read_makefiles : last->next;
186069606e3fSchristos 
186169606e3fSchristos 		      break;
186269606e3fSchristos 		    }
186369606e3fSchristos 		}
186469606e3fSchristos 	    if (f == NULL || !f->double_colon)
186569606e3fSchristos 	      {
186669606e3fSchristos                 makefile_mtimes = (FILE_TIMESTAMP *)
186769606e3fSchristos                   xrealloc ((char *) makefile_mtimes,
186869606e3fSchristos                             (mm_idx + 1) * sizeof (FILE_TIMESTAMP));
186969606e3fSchristos 		makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
187069606e3fSchristos 		last = d;
187169606e3fSchristos 		d = d->next;
187269606e3fSchristos 	      }
187369606e3fSchristos 	  }
187469606e3fSchristos       }
187569606e3fSchristos 
187669606e3fSchristos       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
187769606e3fSchristos       define_makeflags (1, 1);
187869606e3fSchristos 
187969606e3fSchristos       rebuilding_makefiles = 1;
188069606e3fSchristos       status = update_goal_chain (read_makefiles);
188169606e3fSchristos       rebuilding_makefiles = 0;
188269606e3fSchristos 
188369606e3fSchristos       switch (status)
188469606e3fSchristos 	{
188569606e3fSchristos 	case 1:
188669606e3fSchristos           /* The only way this can happen is if the user specified -q and asked
188769606e3fSchristos            * for one of the makefiles to be remade as a target on the command
188869606e3fSchristos            * line.  Since we're not actually updating anything with -q we can
188969606e3fSchristos            * treat this as "did nothing".
189069606e3fSchristos            */
189169606e3fSchristos 
189269606e3fSchristos 	case -1:
189369606e3fSchristos 	  /* Did nothing.  */
189469606e3fSchristos 	  break;
189569606e3fSchristos 
189669606e3fSchristos 	case 2:
189769606e3fSchristos 	  /* Failed to update.  Figure out if we care.  */
189869606e3fSchristos 	  {
189969606e3fSchristos 	    /* Nonzero if any makefile was successfully remade.  */
190069606e3fSchristos 	    int any_remade = 0;
190169606e3fSchristos 	    /* Nonzero if any makefile we care about failed
190269606e3fSchristos 	       in updating or could not be found at all.  */
190369606e3fSchristos 	    int any_failed = 0;
190469606e3fSchristos 	    unsigned int i;
190569606e3fSchristos             struct dep *d;
190669606e3fSchristos 
190769606e3fSchristos 	    for (i = 0, d = read_makefiles; d != 0; ++i, d = d->next)
190869606e3fSchristos               {
190969606e3fSchristos                 /* Reset the considered flag; we may need to look at the file
191069606e3fSchristos                    again to print an error.  */
191169606e3fSchristos                 d->file->considered = 0;
191269606e3fSchristos 
191369606e3fSchristos                 if (d->file->updated)
191469606e3fSchristos                   {
191569606e3fSchristos                     /* This makefile was updated.  */
191669606e3fSchristos                     if (d->file->update_status == 0)
191769606e3fSchristos                       {
191869606e3fSchristos                         /* It was successfully updated.  */
191969606e3fSchristos                         any_remade |= (file_mtime_no_search (d->file)
192069606e3fSchristos                                        != makefile_mtimes[i]);
192169606e3fSchristos                       }
192269606e3fSchristos                     else if (! (d->changed & RM_DONTCARE))
192369606e3fSchristos                       {
192469606e3fSchristos                         FILE_TIMESTAMP mtime;
192569606e3fSchristos                         /* The update failed and this makefile was not
192669606e3fSchristos                            from the MAKEFILES variable, so we care.  */
192769606e3fSchristos                         error (NILF, _("Failed to remake makefile `%s'."),
192869606e3fSchristos                                d->file->name);
192969606e3fSchristos                         mtime = file_mtime_no_search (d->file);
193069606e3fSchristos                         any_remade |= (mtime != NONEXISTENT_MTIME
193169606e3fSchristos                                        && mtime != makefile_mtimes[i]);
193269606e3fSchristos                         makefile_status = MAKE_FAILURE;
193369606e3fSchristos                       }
193469606e3fSchristos                   }
193569606e3fSchristos                 else
193669606e3fSchristos                   /* This makefile was not found at all.  */
193769606e3fSchristos                   if (! (d->changed & RM_DONTCARE))
193869606e3fSchristos                     {
193969606e3fSchristos                       /* This is a makefile we care about.  See how much.  */
194069606e3fSchristos                       if (d->changed & RM_INCLUDED)
194169606e3fSchristos                         /* An included makefile.  We don't need
194269606e3fSchristos                            to die, but we do want to complain.  */
194369606e3fSchristos                         error (NILF,
194469606e3fSchristos                                _("Included makefile `%s' was not found."),
194569606e3fSchristos                                dep_name (d));
194669606e3fSchristos                       else
194769606e3fSchristos                         {
194869606e3fSchristos                           /* A normal makefile.  We must die later.  */
194969606e3fSchristos                           error (NILF, _("Makefile `%s' was not found"),
195069606e3fSchristos                                  dep_name (d));
195169606e3fSchristos                           any_failed = 1;
195269606e3fSchristos                         }
195369606e3fSchristos                     }
195469606e3fSchristos               }
195569606e3fSchristos             /* Reset this to empty so we get the right error message below.  */
195669606e3fSchristos             read_makefiles = 0;
195769606e3fSchristos 
195869606e3fSchristos 	    if (any_remade)
195969606e3fSchristos 	      goto re_exec;
196069606e3fSchristos 	    if (any_failed)
196169606e3fSchristos 	      die (2);
196269606e3fSchristos             break;
196369606e3fSchristos 	  }
196469606e3fSchristos 
196569606e3fSchristos 	case 0:
196669606e3fSchristos 	re_exec:
196769606e3fSchristos 	  /* Updated successfully.  Re-exec ourselves.  */
196869606e3fSchristos 
196969606e3fSchristos 	  remove_intermediates (0);
197069606e3fSchristos 
197169606e3fSchristos 	  if (print_data_base_flag)
197269606e3fSchristos 	    print_data_base ();
197369606e3fSchristos 
197469606e3fSchristos 	  log_working_directory (0);
197569606e3fSchristos 
197669606e3fSchristos           clean_jobserver (0);
197769606e3fSchristos 
197869606e3fSchristos 	  if (makefiles != 0)
197969606e3fSchristos 	    {
198069606e3fSchristos 	      /* These names might have changed.  */
198169606e3fSchristos 	      int i, j = 0;
198269606e3fSchristos 	      for (i = 1; i < argc; ++i)
198369606e3fSchristos 		if (strneq (argv[i], "-f", 2)) /* XXX */
198469606e3fSchristos 		  {
198569606e3fSchristos 		    char *p = &argv[i][2];
198669606e3fSchristos 		    if (*p == '\0')
198769606e3fSchristos 		      argv[++i] = makefiles->list[j];
198869606e3fSchristos 		    else
198969606e3fSchristos 		      argv[i] = concat ("-f", makefiles->list[j], "");
199069606e3fSchristos 		    ++j;
199169606e3fSchristos 		  }
199269606e3fSchristos 	    }
199369606e3fSchristos 
199469606e3fSchristos           /* Add -o option for the stdin temporary file, if necessary.  */
199569606e3fSchristos           if (stdin_nm)
199669606e3fSchristos             {
199769606e3fSchristos               nargv = (char **) xmalloc ((nargc + 2) * sizeof (char *));
199869606e3fSchristos               bcopy ((char *) argv, (char *) nargv, argc * sizeof (char *));
199969606e3fSchristos               nargv[nargc++] = concat ("-o", stdin_nm, "");
200069606e3fSchristos               nargv[nargc] = 0;
200169606e3fSchristos             }
200269606e3fSchristos 
200369606e3fSchristos 	  if (directories != 0 && directories->idx > 0)
200469606e3fSchristos 	    {
200569606e3fSchristos 	      char bad;
200669606e3fSchristos 	      if (directory_before_chdir != 0)
200769606e3fSchristos 		{
200869606e3fSchristos 		  if (chdir (directory_before_chdir) < 0)
200969606e3fSchristos 		    {
201069606e3fSchristos 		      perror_with_name ("chdir", "");
201169606e3fSchristos 		      bad = 1;
201269606e3fSchristos 		    }
201369606e3fSchristos 		  else
201469606e3fSchristos 		    bad = 0;
201569606e3fSchristos 		}
201669606e3fSchristos 	      else
201769606e3fSchristos 		bad = 1;
201869606e3fSchristos 	      if (bad)
201969606e3fSchristos 		fatal (NILF, _("Couldn't change back to original directory."));
202069606e3fSchristos 	    }
202169606e3fSchristos 
202269606e3fSchristos           ++restarts;
202369606e3fSchristos 
202469606e3fSchristos 	  if (ISDB (DB_BASIC))
202569606e3fSchristos 	    {
202669606e3fSchristos 	      char **p;
202769606e3fSchristos 	      printf (_("Re-executing[%u]:"), restarts);
202869606e3fSchristos 	      for (p = nargv; *p != 0; ++p)
202969606e3fSchristos 		printf (" %s", *p);
203069606e3fSchristos 	      putchar ('\n');
203169606e3fSchristos 	    }
203269606e3fSchristos 
203369606e3fSchristos #ifndef _AMIGA
203469606e3fSchristos 	  for (p = environ; *p != 0; ++p)
203569606e3fSchristos             {
203669606e3fSchristos               if (strneq (*p, MAKELEVEL_NAME, MAKELEVEL_LENGTH)
203769606e3fSchristos                   && (*p)[MAKELEVEL_LENGTH] == '=')
203869606e3fSchristos                 {
203969606e3fSchristos                   /* The SGI compiler apparently can't understand
204069606e3fSchristos                      the concept of storing the result of a function
204169606e3fSchristos                      in something other than a local variable.  */
204269606e3fSchristos                   char *sgi_loses;
204369606e3fSchristos                   sgi_loses = (char *) alloca (40);
204469606e3fSchristos                   *p = sgi_loses;
204569606e3fSchristos                   sprintf (*p, "%s=%u", MAKELEVEL_NAME, makelevel);
204669606e3fSchristos                 }
204769606e3fSchristos               if (strneq (*p, "MAKE_RESTARTS=", 14))
204869606e3fSchristos                 {
204969606e3fSchristos                   char *sgi_loses;
205069606e3fSchristos                   sgi_loses = (char *) alloca (40);
205169606e3fSchristos                   *p = sgi_loses;
205269606e3fSchristos                   sprintf (*p, "MAKE_RESTARTS=%u", restarts);
205369606e3fSchristos                   restarts = 0;
205469606e3fSchristos                 }
205569606e3fSchristos             }
205669606e3fSchristos #else /* AMIGA */
205769606e3fSchristos 	  {
205869606e3fSchristos 	    char buffer[256];
205969606e3fSchristos 
206069606e3fSchristos             sprintf (buffer, "%u", makelevel);
206169606e3fSchristos             SetVar (MAKELEVEL_NAME, buffer, -1, GVF_GLOBAL_ONLY);
206269606e3fSchristos 
206369606e3fSchristos             sprintf (buffer, "%u", restarts);
206469606e3fSchristos             SetVar ("MAKE_RESTARTS", buffer, -1, GVF_GLOBAL_ONLY);
206569606e3fSchristos             restarts = 0;
206669606e3fSchristos 	  }
206769606e3fSchristos #endif
206869606e3fSchristos 
206969606e3fSchristos           /* If we didn't set the restarts variable yet, add it.  */
207069606e3fSchristos           if (restarts)
207169606e3fSchristos             {
207269606e3fSchristos               char *b = alloca (40);
207369606e3fSchristos               sprintf (b, "MAKE_RESTARTS=%u", restarts);
207469606e3fSchristos               putenv (b);
207569606e3fSchristos             }
207669606e3fSchristos 
207769606e3fSchristos 	  fflush (stdout);
207869606e3fSchristos 	  fflush (stderr);
207969606e3fSchristos 
208069606e3fSchristos           /* Close the dup'd jobserver pipe if we opened one.  */
208169606e3fSchristos           if (job_rfd >= 0)
208269606e3fSchristos             close (job_rfd);
208369606e3fSchristos 
208469606e3fSchristos #ifdef _AMIGA
208569606e3fSchristos 	  exec_command (nargv);
208669606e3fSchristos 	  exit (0);
208769606e3fSchristos #elif defined (__EMX__)
208869606e3fSchristos 	  {
208969606e3fSchristos 	    /* It is not possible to use execve() here because this
209069606e3fSchristos 	       would cause the parent process to be terminated with
209169606e3fSchristos 	       exit code 0 before the child process has been terminated.
209269606e3fSchristos 	       Therefore it may be the best solution simply to spawn the
209369606e3fSchristos 	       child process including all file handles and to wait for its
209469606e3fSchristos 	       termination. */
209569606e3fSchristos 	    int pid;
209669606e3fSchristos 	    int status;
209769606e3fSchristos 	    pid = child_execute_job (0, 1, nargv, environ);
209869606e3fSchristos 
209969606e3fSchristos 	    /* is this loop really necessary? */
210069606e3fSchristos 	    do {
210169606e3fSchristos 	      pid = wait (&status);
210269606e3fSchristos 	    } while (pid <= 0);
210369606e3fSchristos 	    /* use the exit code of the child process */
210469606e3fSchristos 	    exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
210569606e3fSchristos 	  }
210669606e3fSchristos #else
210769606e3fSchristos 	  exec_command (nargv, environ);
210869606e3fSchristos #endif
210969606e3fSchristos 	  /* NOTREACHED */
211069606e3fSchristos 
211169606e3fSchristos 	default:
211269606e3fSchristos #define BOGUS_UPDATE_STATUS 0
211369606e3fSchristos 	  assert (BOGUS_UPDATE_STATUS);
211469606e3fSchristos 	  break;
211569606e3fSchristos 	}
211669606e3fSchristos 
211769606e3fSchristos       db_level = orig_db_level;
211869606e3fSchristos 
211969606e3fSchristos       /* Free the makefile mtimes (if we allocated any).  */
212069606e3fSchristos       if (makefile_mtimes)
212169606e3fSchristos         free ((char *) makefile_mtimes);
212269606e3fSchristos     }
212369606e3fSchristos 
212469606e3fSchristos   /* Set up `MAKEFLAGS' again for the normal targets.  */
212569606e3fSchristos   define_makeflags (1, 0);
212669606e3fSchristos 
212769606e3fSchristos   /* Set always_make_flag if -B was given.  */
212869606e3fSchristos   always_make_flag = always_make_set;
212969606e3fSchristos 
213069606e3fSchristos   /* If restarts is set we haven't set up -W files yet, so do that now.  */
213169606e3fSchristos   if (restarts && new_files != 0)
213269606e3fSchristos     {
213369606e3fSchristos       for (p = new_files->list; *p != 0; ++p)
213469606e3fSchristos 	{
213569606e3fSchristos 	  f = enter_command_line_file (*p);
213669606e3fSchristos 	  f->last_mtime = f->mtime_before_update = NEW_MTIME;
213769606e3fSchristos 	}
213869606e3fSchristos     }
213969606e3fSchristos 
214069606e3fSchristos   /* If there is a temp file from reading a makefile from stdin, get rid of
214169606e3fSchristos      it now.  */
214269606e3fSchristos   if (stdin_nm && unlink (stdin_nm) < 0 && errno != ENOENT)
214369606e3fSchristos     perror_with_name (_("unlink (temporary file): "), stdin_nm);
214469606e3fSchristos 
214569606e3fSchristos   {
214669606e3fSchristos     int status;
214769606e3fSchristos 
214869606e3fSchristos     /* If there were no command-line goals, use the default.  */
214969606e3fSchristos     if (goals == 0)
215069606e3fSchristos       {
215169606e3fSchristos         if (**default_goal_name != '\0')
215269606e3fSchristos           {
215369606e3fSchristos             if (default_goal_file == 0 ||
215469606e3fSchristos                 strcmp (*default_goal_name, default_goal_file->name) != 0)
215569606e3fSchristos               {
215669606e3fSchristos                 default_goal_file = lookup_file (*default_goal_name);
215769606e3fSchristos 
215869606e3fSchristos                 /* In case user set .DEFAULT_GOAL to a non-existent target
215969606e3fSchristos                    name let's just enter this name into the table and let
216069606e3fSchristos                    the standard logic sort it out. */
216169606e3fSchristos                 if (default_goal_file == 0)
216269606e3fSchristos                   {
216369606e3fSchristos                     struct nameseq *ns;
216469606e3fSchristos                     char *p = *default_goal_name;
216569606e3fSchristos 
216669606e3fSchristos                     ns = multi_glob (
216769606e3fSchristos                       parse_file_seq (&p, '\0', sizeof (struct nameseq), 1),
216869606e3fSchristos                       sizeof (struct nameseq));
216969606e3fSchristos 
217069606e3fSchristos                     /* .DEFAULT_GOAL should contain one target. */
217169606e3fSchristos                     if (ns->next != 0)
217269606e3fSchristos                       fatal (NILF, _(".DEFAULT_GOAL contains more than one target"));
217369606e3fSchristos 
217469606e3fSchristos                     default_goal_file = enter_file (ns->name);
217569606e3fSchristos 
217669606e3fSchristos                     ns->name = 0; /* It was reused by enter_file(). */
217769606e3fSchristos                     free_ns_chain (ns);
217869606e3fSchristos                   }
217969606e3fSchristos               }
218069606e3fSchristos 
218169606e3fSchristos             goals = alloc_dep ();
218269606e3fSchristos             goals->file = default_goal_file;
218369606e3fSchristos           }
218469606e3fSchristos       }
218569606e3fSchristos     else
218669606e3fSchristos       lastgoal->next = 0;
218769606e3fSchristos 
218869606e3fSchristos 
218969606e3fSchristos     if (!goals)
219069606e3fSchristos       {
219169606e3fSchristos         if (read_makefiles == 0)
219269606e3fSchristos           fatal (NILF, _("No targets specified and no makefile found"));
219369606e3fSchristos 
219469606e3fSchristos         fatal (NILF, _("No targets"));
219569606e3fSchristos       }
219669606e3fSchristos 
219769606e3fSchristos     /* Update the goals.  */
219869606e3fSchristos 
219969606e3fSchristos     DB (DB_BASIC, (_("Updating goal targets....\n")));
220069606e3fSchristos 
220169606e3fSchristos     switch (update_goal_chain (goals))
220269606e3fSchristos     {
220369606e3fSchristos       case -1:
220469606e3fSchristos         /* Nothing happened.  */
220569606e3fSchristos       case 0:
220669606e3fSchristos         /* Updated successfully.  */
220769606e3fSchristos         status = makefile_status;
220869606e3fSchristos         break;
220969606e3fSchristos       case 1:
221069606e3fSchristos         /* We are under -q and would run some commands.  */
221169606e3fSchristos         status = MAKE_TROUBLE;
221269606e3fSchristos         break;
221369606e3fSchristos       case 2:
221469606e3fSchristos         /* Updating failed.  POSIX.2 specifies exit status >1 for this;
221569606e3fSchristos            but in VMS, there is only success and failure.  */
221669606e3fSchristos         status = MAKE_FAILURE;
221769606e3fSchristos         break;
221869606e3fSchristos       default:
221969606e3fSchristos         abort ();
222069606e3fSchristos     }
222169606e3fSchristos 
222269606e3fSchristos     /* If we detected some clock skew, generate one last warning */
222369606e3fSchristos     if (clock_skew_detected)
222469606e3fSchristos       error (NILF,
222569606e3fSchristos              _("warning:  Clock skew detected.  Your build may be incomplete."));
222669606e3fSchristos 
222769606e3fSchristos     /* Exit.  */
222869606e3fSchristos     die (status);
222969606e3fSchristos   }
223069606e3fSchristos 
223169606e3fSchristos   /* NOTREACHED */
223269606e3fSchristos   return 0;
223369606e3fSchristos }
223469606e3fSchristos 
223569606e3fSchristos /* Parsing of arguments, decoding of switches.  */
223669606e3fSchristos 
223769606e3fSchristos static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
223869606e3fSchristos static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
223969606e3fSchristos 				  (sizeof (long_option_aliases) /
224069606e3fSchristos 				   sizeof (long_option_aliases[0]))];
224169606e3fSchristos 
224269606e3fSchristos /* Fill in the string and vector for getopt.  */
224369606e3fSchristos static void
224469606e3fSchristos init_switches (void)
224569606e3fSchristos {
224669606e3fSchristos   char *p;
224769606e3fSchristos   unsigned int c;
224869606e3fSchristos   unsigned int i;
224969606e3fSchristos 
225069606e3fSchristos   if (options[0] != '\0')
225169606e3fSchristos     /* Already done.  */
225269606e3fSchristos     return;
225369606e3fSchristos 
225469606e3fSchristos   p = options;
225569606e3fSchristos 
225669606e3fSchristos   /* Return switch and non-switch args in order, regardless of
225769606e3fSchristos      POSIXLY_CORRECT.  Non-switch args are returned as option 1.  */
225869606e3fSchristos   *p++ = '-';
225969606e3fSchristos 
226069606e3fSchristos   for (i = 0; switches[i].c != '\0'; ++i)
226169606e3fSchristos     {
226269606e3fSchristos       long_options[i].name = (switches[i].long_name == 0 ? "" :
226369606e3fSchristos 			      switches[i].long_name);
226469606e3fSchristos       long_options[i].flag = 0;
226569606e3fSchristos       long_options[i].val = switches[i].c;
226669606e3fSchristos       if (short_option (switches[i].c))
226769606e3fSchristos 	*p++ = switches[i].c;
226869606e3fSchristos       switch (switches[i].type)
226969606e3fSchristos 	{
227069606e3fSchristos 	case flag:
227169606e3fSchristos 	case flag_off:
227269606e3fSchristos 	case ignore:
227369606e3fSchristos 	  long_options[i].has_arg = no_argument;
227469606e3fSchristos 	  break;
227569606e3fSchristos 
227669606e3fSchristos 	case string:
227769606e3fSchristos 	case positive_int:
227869606e3fSchristos 	case floating:
227969606e3fSchristos 	  if (short_option (switches[i].c))
228069606e3fSchristos 	    *p++ = ':';
228169606e3fSchristos 	  if (switches[i].noarg_value != 0)
228269606e3fSchristos 	    {
228369606e3fSchristos 	      if (short_option (switches[i].c))
228469606e3fSchristos 		*p++ = ':';
228569606e3fSchristos 	      long_options[i].has_arg = optional_argument;
228669606e3fSchristos 	    }
228769606e3fSchristos 	  else
228869606e3fSchristos 	    long_options[i].has_arg = required_argument;
228969606e3fSchristos 	  break;
229069606e3fSchristos 	}
229169606e3fSchristos     }
229269606e3fSchristos   *p = '\0';
229369606e3fSchristos   for (c = 0; c < (sizeof (long_option_aliases) /
229469606e3fSchristos 		   sizeof (long_option_aliases[0]));
229569606e3fSchristos        ++c)
229669606e3fSchristos     long_options[i++] = long_option_aliases[c];
229769606e3fSchristos   long_options[i].name = 0;
229869606e3fSchristos }
229969606e3fSchristos 
230069606e3fSchristos static void
230169606e3fSchristos handle_non_switch_argument (char *arg, int env)
230269606e3fSchristos {
230369606e3fSchristos   /* Non-option argument.  It might be a variable definition.  */
230469606e3fSchristos   struct variable *v;
230569606e3fSchristos   if (arg[0] == '-' && arg[1] == '\0')
230669606e3fSchristos     /* Ignore plain `-' for compatibility.  */
230769606e3fSchristos     return;
230869606e3fSchristos   v = try_variable_definition (0, arg, o_command, 0);
230969606e3fSchristos   if (v != 0)
231069606e3fSchristos     {
231169606e3fSchristos       /* It is indeed a variable definition.  If we don't already have this
231269606e3fSchristos 	 one, record a pointer to the variable for later use in
231369606e3fSchristos 	 define_makeflags.  */
231469606e3fSchristos       struct command_variable *cv;
231569606e3fSchristos 
231669606e3fSchristos       for (cv = command_variables; cv != 0; cv = cv->next)
231769606e3fSchristos         if (cv->variable == v)
231869606e3fSchristos           break;
231969606e3fSchristos 
232069606e3fSchristos       if (! cv) {
232169606e3fSchristos         cv = (struct command_variable *) xmalloc (sizeof (*cv));
232269606e3fSchristos         cv->variable = v;
232369606e3fSchristos         cv->next = command_variables;
232469606e3fSchristos         command_variables = cv;
232569606e3fSchristos       }
232669606e3fSchristos     }
232769606e3fSchristos   else if (! env)
232869606e3fSchristos     {
232969606e3fSchristos       /* Not an option or variable definition; it must be a goal
233069606e3fSchristos 	 target!  Enter it as a file and add it to the dep chain of
233169606e3fSchristos 	 goals.  */
233269606e3fSchristos       struct file *f = enter_command_line_file (arg);
233369606e3fSchristos       f->cmd_target = 1;
233469606e3fSchristos 
233569606e3fSchristos       if (goals == 0)
233669606e3fSchristos 	{
233769606e3fSchristos 	  goals = alloc_dep ();
233869606e3fSchristos 	  lastgoal = goals;
233969606e3fSchristos 	}
234069606e3fSchristos       else
234169606e3fSchristos 	{
234269606e3fSchristos 	  lastgoal->next = alloc_dep ();
234369606e3fSchristos 	  lastgoal = lastgoal->next;
234469606e3fSchristos 	}
234569606e3fSchristos 
234669606e3fSchristos       lastgoal->file = f;
234769606e3fSchristos 
234869606e3fSchristos       {
234969606e3fSchristos         /* Add this target name to the MAKECMDGOALS variable. */
235069606e3fSchristos         struct variable *v;
235169606e3fSchristos         char *value;
235269606e3fSchristos 
235369606e3fSchristos         v = lookup_variable (STRING_SIZE_TUPLE ("MAKECMDGOALS"));
235469606e3fSchristos         if (v == 0)
235569606e3fSchristos           value = f->name;
235669606e3fSchristos         else
235769606e3fSchristos           {
235869606e3fSchristos             /* Paste the old and new values together */
235969606e3fSchristos             unsigned int oldlen, newlen;
236069606e3fSchristos 
236169606e3fSchristos             oldlen = strlen (v->value);
236269606e3fSchristos             newlen = strlen (f->name);
236369606e3fSchristos             value = (char *) alloca (oldlen + 1 + newlen + 1);
236469606e3fSchristos             bcopy (v->value, value, oldlen);
236569606e3fSchristos             value[oldlen] = ' ';
236669606e3fSchristos             bcopy (f->name, &value[oldlen + 1], newlen + 1);
236769606e3fSchristos           }
236869606e3fSchristos         define_variable ("MAKECMDGOALS", 12, value, o_default, 0);
236969606e3fSchristos       }
237069606e3fSchristos     }
237169606e3fSchristos }
237269606e3fSchristos 
237369606e3fSchristos /* Print a nice usage method.  */
237469606e3fSchristos 
237569606e3fSchristos static void
237669606e3fSchristos print_usage (int bad)
237769606e3fSchristos {
237869606e3fSchristos   const char *const *cpp;
237969606e3fSchristos   FILE *usageto;
238069606e3fSchristos 
238169606e3fSchristos   if (print_version_flag)
238269606e3fSchristos     print_version ();
238369606e3fSchristos 
238469606e3fSchristos   usageto = bad ? stderr : stdout;
238569606e3fSchristos 
238669606e3fSchristos   fprintf (usageto, _("Usage: %s [options] [target] ...\n"), program);
238769606e3fSchristos 
238869606e3fSchristos   for (cpp = usage; *cpp; ++cpp)
238969606e3fSchristos     fputs (_(*cpp), usageto);
239069606e3fSchristos 
239169606e3fSchristos   if (!remote_description || *remote_description == '\0')
239269606e3fSchristos     fprintf (usageto, _("\nThis program built for %s\n"), make_host);
239369606e3fSchristos   else
239469606e3fSchristos     fprintf (usageto, _("\nThis program built for %s (%s)\n"),
239569606e3fSchristos              make_host, remote_description);
239669606e3fSchristos 
239769606e3fSchristos   fprintf (usageto, _("Report bugs to <bug-make@gnu.org>\n"));
239869606e3fSchristos }
239969606e3fSchristos 
240069606e3fSchristos /* Decode switches from ARGC and ARGV.
240169606e3fSchristos    They came from the environment if ENV is nonzero.  */
240269606e3fSchristos 
240369606e3fSchristos static void
240469606e3fSchristos decode_switches (int argc, char **argv, int env)
240569606e3fSchristos {
240669606e3fSchristos   int bad = 0;
240769606e3fSchristos   register const struct command_switch *cs;
240869606e3fSchristos   register struct stringlist *sl;
240969606e3fSchristos   register int c;
241069606e3fSchristos 
241169606e3fSchristos   /* getopt does most of the parsing for us.
241269606e3fSchristos      First, get its vectors set up.  */
241369606e3fSchristos 
241469606e3fSchristos   init_switches ();
241569606e3fSchristos 
241669606e3fSchristos   /* Let getopt produce error messages for the command line,
241769606e3fSchristos      but not for options from the environment.  */
241869606e3fSchristos   opterr = !env;
241969606e3fSchristos   /* Reset getopt's state.  */
242069606e3fSchristos   optind = 0;
242169606e3fSchristos 
242269606e3fSchristos   while (optind < argc)
242369606e3fSchristos     {
242469606e3fSchristos       /* Parse the next argument.  */
242569606e3fSchristos       c = getopt_long (argc, argv, options, long_options, (int *) 0);
242669606e3fSchristos       if (c == EOF)
242769606e3fSchristos 	/* End of arguments, or "--" marker seen.  */
242869606e3fSchristos 	break;
242969606e3fSchristos       else if (c == 1)
243069606e3fSchristos 	/* An argument not starting with a dash.  */
243169606e3fSchristos 	handle_non_switch_argument (optarg, env);
243269606e3fSchristos       else if (c == '?')
243369606e3fSchristos 	/* Bad option.  We will print a usage message and die later.
243469606e3fSchristos 	   But continue to parse the other options so the user can
243569606e3fSchristos 	   see all he did wrong.  */
243669606e3fSchristos 	bad = 1;
243769606e3fSchristos       else
243869606e3fSchristos 	for (cs = switches; cs->c != '\0'; ++cs)
243969606e3fSchristos 	  if (cs->c == c)
244069606e3fSchristos 	    {
244169606e3fSchristos 	      /* Whether or not we will actually do anything with
244269606e3fSchristos 		 this switch.  We test this individually inside the
244369606e3fSchristos 		 switch below rather than just once outside it, so that
244469606e3fSchristos 		 options which are to be ignored still consume args.  */
244569606e3fSchristos 	      int doit = !env || cs->env;
244669606e3fSchristos 
244769606e3fSchristos 	      switch (cs->type)
244869606e3fSchristos 		{
244969606e3fSchristos 		default:
245069606e3fSchristos 		  abort ();
245169606e3fSchristos 
245269606e3fSchristos 		case ignore:
245369606e3fSchristos 		  break;
245469606e3fSchristos 
245569606e3fSchristos 		case flag:
245669606e3fSchristos 		case flag_off:
245769606e3fSchristos 		  if (doit)
245869606e3fSchristos 		    *(int *) cs->value_ptr = cs->type == flag;
245969606e3fSchristos 		  break;
246069606e3fSchristos 
246169606e3fSchristos 		case string:
246269606e3fSchristos 		  if (!doit)
246369606e3fSchristos 		    break;
246469606e3fSchristos 
246569606e3fSchristos 		  if (optarg == 0)
246669606e3fSchristos 		    optarg = cs->noarg_value;
246769606e3fSchristos                   else if (*optarg == '\0')
246869606e3fSchristos                     {
246969606e3fSchristos                       error (NILF, _("the `-%c' option requires a non-empty string argument"),
247069606e3fSchristos                              cs->c);
247169606e3fSchristos                       bad = 1;
247269606e3fSchristos                     }
247369606e3fSchristos 
247469606e3fSchristos 		  sl = *(struct stringlist **) cs->value_ptr;
247569606e3fSchristos 		  if (sl == 0)
247669606e3fSchristos 		    {
247769606e3fSchristos 		      sl = (struct stringlist *)
247869606e3fSchristos 			xmalloc (sizeof (struct stringlist));
247969606e3fSchristos 		      sl->max = 5;
248069606e3fSchristos 		      sl->idx = 0;
248169606e3fSchristos 		      sl->list = (char **) xmalloc (5 * sizeof (char *));
248269606e3fSchristos 		      *(struct stringlist **) cs->value_ptr = sl;
248369606e3fSchristos 		    }
248469606e3fSchristos 		  else if (sl->idx == sl->max - 1)
248569606e3fSchristos 		    {
248669606e3fSchristos 		      sl->max += 5;
248769606e3fSchristos 		      sl->list = (char **)
248869606e3fSchristos 			xrealloc ((char *) sl->list,
248969606e3fSchristos 				  sl->max * sizeof (char *));
249069606e3fSchristos 		    }
249169606e3fSchristos 		  sl->list[sl->idx++] = optarg;
249269606e3fSchristos 		  sl->list[sl->idx] = 0;
249369606e3fSchristos 		  break;
249469606e3fSchristos 
249569606e3fSchristos 		case positive_int:
249669606e3fSchristos                   /* See if we have an option argument; if we do require that
249769606e3fSchristos                      it's all digits, not something like "10foo".  */
249869606e3fSchristos 		  if (optarg == 0 && argc > optind)
249969606e3fSchristos                     {
250069606e3fSchristos                       const char *cp;
250169606e3fSchristos                       for (cp=argv[optind]; ISDIGIT (cp[0]); ++cp)
250269606e3fSchristos                         ;
250369606e3fSchristos                       if (cp[0] == '\0')
250469606e3fSchristos                         optarg = argv[optind++];
250569606e3fSchristos                     }
250669606e3fSchristos 
250769606e3fSchristos 		  if (!doit)
250869606e3fSchristos 		    break;
250969606e3fSchristos 
251069606e3fSchristos 		  if (optarg != 0)
251169606e3fSchristos 		    {
251269606e3fSchristos 		      int i = atoi (optarg);
251369606e3fSchristos                       const char *cp;
251469606e3fSchristos 
251569606e3fSchristos                       /* Yes, I realize we're repeating this in some cases.  */
251669606e3fSchristos                       for (cp = optarg; ISDIGIT (cp[0]); ++cp)
251769606e3fSchristos                         ;
251869606e3fSchristos 
251969606e3fSchristos 		      if (i < 1 || cp[0] != '\0')
252069606e3fSchristos 			{
252169606e3fSchristos                           error (NILF, _("the `-%c' option requires a positive integral argument"),
252269606e3fSchristos                                  cs->c);
252369606e3fSchristos 			  bad = 1;
252469606e3fSchristos 			}
252569606e3fSchristos 		      else
252669606e3fSchristos 			*(unsigned int *) cs->value_ptr = i;
252769606e3fSchristos 		    }
252869606e3fSchristos 		  else
252969606e3fSchristos 		    *(unsigned int *) cs->value_ptr
253069606e3fSchristos 		      = *(unsigned int *) cs->noarg_value;
253169606e3fSchristos 		  break;
253269606e3fSchristos 
253369606e3fSchristos #ifndef NO_FLOAT
253469606e3fSchristos 		case floating:
253569606e3fSchristos 		  if (optarg == 0 && optind < argc
253669606e3fSchristos 		      && (ISDIGIT (argv[optind][0]) || argv[optind][0] == '.'))
253769606e3fSchristos 		    optarg = argv[optind++];
253869606e3fSchristos 
253969606e3fSchristos 		  if (doit)
254069606e3fSchristos 		    *(double *) cs->value_ptr
254169606e3fSchristos 		      = (optarg != 0 ? atof (optarg)
254269606e3fSchristos 			 : *(double *) cs->noarg_value);
254369606e3fSchristos 
254469606e3fSchristos 		  break;
254569606e3fSchristos #endif
254669606e3fSchristos 		}
254769606e3fSchristos 
254869606e3fSchristos 	      /* We've found the switch.  Stop looking.  */
254969606e3fSchristos 	      break;
255069606e3fSchristos 	    }
255169606e3fSchristos     }
255269606e3fSchristos 
255369606e3fSchristos   /* There are no more options according to getting getopt, but there may
255469606e3fSchristos      be some arguments left.  Since we have asked for non-option arguments
255569606e3fSchristos      to be returned in order, this only happens when there is a "--"
255669606e3fSchristos      argument to prevent later arguments from being options.  */
255769606e3fSchristos   while (optind < argc)
255869606e3fSchristos     handle_non_switch_argument (argv[optind++], env);
255969606e3fSchristos 
256069606e3fSchristos 
256169606e3fSchristos   if (!env && (bad || print_usage_flag))
256269606e3fSchristos     {
256369606e3fSchristos       print_usage (bad);
256469606e3fSchristos       die (bad ? 2 : 0);
256569606e3fSchristos     }
256669606e3fSchristos }
256769606e3fSchristos 
256869606e3fSchristos /* Decode switches from environment variable ENVAR (which is LEN chars long).
256969606e3fSchristos    We do this by chopping the value into a vector of words, prepending a
257069606e3fSchristos    dash to the first word if it lacks one, and passing the vector to
257169606e3fSchristos    decode_switches.  */
257269606e3fSchristos 
257369606e3fSchristos static void
257469606e3fSchristos decode_env_switches (char *envar, unsigned int len)
257569606e3fSchristos {
257669606e3fSchristos   char *varref = (char *) alloca (2 + len + 2);
257769606e3fSchristos   char *value, *p;
257869606e3fSchristos   int argc;
257969606e3fSchristos   char **argv;
258069606e3fSchristos 
258169606e3fSchristos   /* Get the variable's value.  */
258269606e3fSchristos   varref[0] = '$';
258369606e3fSchristos   varref[1] = '(';
258469606e3fSchristos   bcopy (envar, &varref[2], len);
258569606e3fSchristos   varref[2 + len] = ')';
258669606e3fSchristos   varref[2 + len + 1] = '\0';
258769606e3fSchristos   value = variable_expand (varref);
258869606e3fSchristos 
258969606e3fSchristos   /* Skip whitespace, and check for an empty value.  */
259069606e3fSchristos   value = next_token (value);
259169606e3fSchristos   len = strlen (value);
259269606e3fSchristos   if (len == 0)
259369606e3fSchristos     return;
259469606e3fSchristos 
259569606e3fSchristos   /* Allocate a vector that is definitely big enough.  */
259669606e3fSchristos   argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
259769606e3fSchristos 
259869606e3fSchristos   /* Allocate a buffer to copy the value into while we split it into words
259969606e3fSchristos      and unquote it.  We must use permanent storage for this because
260069606e3fSchristos      decode_switches may store pointers into the passed argument words.  */
260169606e3fSchristos   p = (char *) xmalloc (2 * len);
260269606e3fSchristos 
260369606e3fSchristos   /* getopt will look at the arguments starting at ARGV[1].
260469606e3fSchristos      Prepend a spacer word.  */
260569606e3fSchristos   argv[0] = 0;
260669606e3fSchristos   argc = 1;
260769606e3fSchristos   argv[argc] = p;
260869606e3fSchristos   while (*value != '\0')
260969606e3fSchristos     {
261069606e3fSchristos       if (*value == '\\' && value[1] != '\0')
261169606e3fSchristos 	++value;		/* Skip the backslash.  */
261269606e3fSchristos       else if (isblank ((unsigned char)*value))
261369606e3fSchristos 	{
261469606e3fSchristos 	  /* End of the word.  */
261569606e3fSchristos 	  *p++ = '\0';
261669606e3fSchristos 	  argv[++argc] = p;
261769606e3fSchristos 	  do
261869606e3fSchristos 	    ++value;
261969606e3fSchristos 	  while (isblank ((unsigned char)*value));
262069606e3fSchristos 	  continue;
262169606e3fSchristos 	}
262269606e3fSchristos       *p++ = *value++;
262369606e3fSchristos     }
262469606e3fSchristos   *p = '\0';
262569606e3fSchristos   argv[++argc] = 0;
262669606e3fSchristos 
262769606e3fSchristos   if (argv[1][0] != '-' && strchr (argv[1], '=') == 0)
262869606e3fSchristos     /* The first word doesn't start with a dash and isn't a variable
262969606e3fSchristos        definition.  Add a dash and pass it along to decode_switches.  We
263069606e3fSchristos        need permanent storage for this in case decode_switches saves
263169606e3fSchristos        pointers into the value.  */
263269606e3fSchristos     argv[1] = concat ("-", argv[1], "");
263369606e3fSchristos 
263469606e3fSchristos   /* Parse those words.  */
263569606e3fSchristos   decode_switches (argc, argv, 1);
263669606e3fSchristos }
263769606e3fSchristos 
263869606e3fSchristos /* Quote the string IN so that it will be interpreted as a single word with
263969606e3fSchristos    no magic by decode_env_switches; also double dollar signs to avoid
264069606e3fSchristos    variable expansion in make itself.  Write the result into OUT, returning
264169606e3fSchristos    the address of the next character to be written.
264269606e3fSchristos    Allocating space for OUT twice the length of IN is always sufficient.  */
264369606e3fSchristos 
264469606e3fSchristos static char *
264569606e3fSchristos quote_for_env (char *out, char *in)
264669606e3fSchristos {
264769606e3fSchristos   while (*in != '\0')
264869606e3fSchristos     {
264969606e3fSchristos       if (*in == '$')
265069606e3fSchristos 	*out++ = '$';
265169606e3fSchristos       else if (isblank ((unsigned char)*in) || *in == '\\')
265269606e3fSchristos         *out++ = '\\';
265369606e3fSchristos       *out++ = *in++;
265469606e3fSchristos     }
265569606e3fSchristos 
265669606e3fSchristos   return out;
265769606e3fSchristos }
265869606e3fSchristos 
265969606e3fSchristos /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
266069606e3fSchristos    command switches.  Include options with args if ALL is nonzero.
266169606e3fSchristos    Don't include options with the `no_makefile' flag set if MAKEFILE.  */
266269606e3fSchristos 
266369606e3fSchristos static void
266469606e3fSchristos define_makeflags (int all, int makefile)
266569606e3fSchristos {
266669606e3fSchristos   static const char ref[] = "$(MAKEOVERRIDES)";
266769606e3fSchristos   static const char posixref[] = "$(-*-command-variables-*-)";
266869606e3fSchristos   register const struct command_switch *cs;
266969606e3fSchristos   char *flagstring;
267069606e3fSchristos   register char *p;
267169606e3fSchristos   unsigned int words;
267269606e3fSchristos   struct variable *v;
267369606e3fSchristos 
267469606e3fSchristos   /* We will construct a linked list of `struct flag's describing
267569606e3fSchristos      all the flags which need to go in MAKEFLAGS.  Then, once we
267669606e3fSchristos      know how many there are and their lengths, we can put them all
267769606e3fSchristos      together in a string.  */
267869606e3fSchristos 
267969606e3fSchristos   struct flag
268069606e3fSchristos     {
268169606e3fSchristos       struct flag *next;
268269606e3fSchristos       const struct command_switch *cs;
268369606e3fSchristos       char *arg;
268469606e3fSchristos     };
268569606e3fSchristos   struct flag *flags = 0;
268669606e3fSchristos   unsigned int flagslen = 0;
268769606e3fSchristos #define	ADD_FLAG(ARG, LEN) \
268869606e3fSchristos   do {									      \
268969606e3fSchristos     struct flag *new = (struct flag *) alloca (sizeof (struct flag));	      \
269069606e3fSchristos     new->cs = cs;							      \
269169606e3fSchristos     new->arg = (ARG);							      \
269269606e3fSchristos     new->next = flags;							      \
269369606e3fSchristos     flags = new;							      \
269469606e3fSchristos     if (new->arg == 0)							      \
269569606e3fSchristos       ++flagslen;		/* Just a single flag letter.  */	      \
269669606e3fSchristos     else								      \
269769606e3fSchristos       flagslen += 1 + 1 + 1 + 1 + 3 * (LEN); /* " -x foo" */		      \
269869606e3fSchristos     if (!short_option (cs->c))						      \
269969606e3fSchristos       /* This switch has no single-letter version, so we use the long.  */    \
270069606e3fSchristos       flagslen += 2 + strlen (cs->long_name);				      \
270169606e3fSchristos   } while (0)
270269606e3fSchristos 
270369606e3fSchristos   for (cs = switches; cs->c != '\0'; ++cs)
270469606e3fSchristos     if (cs->toenv && (!makefile || !cs->no_makefile))
270569606e3fSchristos       switch (cs->type)
270669606e3fSchristos 	{
270769606e3fSchristos 	default:
270869606e3fSchristos 	  abort ();
270969606e3fSchristos 
271069606e3fSchristos 	case ignore:
271169606e3fSchristos 	  break;
271269606e3fSchristos 
271369606e3fSchristos 	case flag:
271469606e3fSchristos 	case flag_off:
271569606e3fSchristos 	  if (!*(int *) cs->value_ptr == (cs->type == flag_off)
271669606e3fSchristos 	      && (cs->default_value == 0
271769606e3fSchristos 		  || *(int *) cs->value_ptr != *(int *) cs->default_value))
271869606e3fSchristos 	    ADD_FLAG (0, 0);
271969606e3fSchristos 	  break;
272069606e3fSchristos 
272169606e3fSchristos 	case positive_int:
272269606e3fSchristos 	  if (all)
272369606e3fSchristos 	    {
272469606e3fSchristos 	      if ((cs->default_value != 0
272569606e3fSchristos 		   && (*(unsigned int *) cs->value_ptr
272669606e3fSchristos 		       == *(unsigned int *) cs->default_value)))
272769606e3fSchristos 		break;
272869606e3fSchristos 	      else if (cs->noarg_value != 0
272969606e3fSchristos 		       && (*(unsigned int *) cs->value_ptr ==
273069606e3fSchristos 			   *(unsigned int *) cs->noarg_value))
273169606e3fSchristos 		ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
273269606e3fSchristos 	      else if (cs->c == 'j')
273369606e3fSchristos 		/* Special case for `-j'.  */
273469606e3fSchristos 		ADD_FLAG ("1", 1);
273569606e3fSchristos 	      else
273669606e3fSchristos 		{
273769606e3fSchristos 		  char *buf = (char *) alloca (30);
273869606e3fSchristos 		  sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
273969606e3fSchristos 		  ADD_FLAG (buf, strlen (buf));
274069606e3fSchristos 		}
274169606e3fSchristos 	    }
274269606e3fSchristos 	  break;
274369606e3fSchristos 
274469606e3fSchristos #ifndef NO_FLOAT
274569606e3fSchristos 	case floating:
274669606e3fSchristos 	  if (all)
274769606e3fSchristos 	    {
274869606e3fSchristos 	      if (cs->default_value != 0
274969606e3fSchristos 		  && (*(double *) cs->value_ptr
275069606e3fSchristos 		      == *(double *) cs->default_value))
275169606e3fSchristos 		break;
275269606e3fSchristos 	      else if (cs->noarg_value != 0
275369606e3fSchristos 		       && (*(double *) cs->value_ptr
275469606e3fSchristos 			   == *(double *) cs->noarg_value))
275569606e3fSchristos 		ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
275669606e3fSchristos 	      else
275769606e3fSchristos 		{
275869606e3fSchristos 		  char *buf = (char *) alloca (100);
275969606e3fSchristos 		  sprintf (buf, "%g", *(double *) cs->value_ptr);
276069606e3fSchristos 		  ADD_FLAG (buf, strlen (buf));
276169606e3fSchristos 		}
276269606e3fSchristos 	    }
276369606e3fSchristos 	  break;
276469606e3fSchristos #endif
276569606e3fSchristos 
276669606e3fSchristos 	case string:
276769606e3fSchristos 	  if (all)
276869606e3fSchristos 	    {
276969606e3fSchristos 	      struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
277069606e3fSchristos 	      if (sl != 0)
277169606e3fSchristos 		{
277269606e3fSchristos 		  /* Add the elements in reverse order, because
277369606e3fSchristos 		     all the flags get reversed below; and the order
277469606e3fSchristos 		     matters for some switches (like -I).  */
277569606e3fSchristos 		  register unsigned int i = sl->idx;
277669606e3fSchristos 		  while (i-- > 0)
277769606e3fSchristos 		    ADD_FLAG (sl->list[i], strlen (sl->list[i]));
277869606e3fSchristos 		}
277969606e3fSchristos 	    }
278069606e3fSchristos 	  break;
278169606e3fSchristos 	}
278269606e3fSchristos 
278369606e3fSchristos   flagslen += 4 + sizeof posixref; /* Four more for the possible " -- ".  */
278469606e3fSchristos 
278569606e3fSchristos #undef	ADD_FLAG
278669606e3fSchristos 
278769606e3fSchristos   /* Construct the value in FLAGSTRING.
278869606e3fSchristos      We allocate enough space for a preceding dash and trailing null.  */
278969606e3fSchristos   flagstring = (char *) alloca (1 + flagslen + 1);
279069606e3fSchristos   bzero (flagstring, 1 + flagslen + 1);
279169606e3fSchristos   p = flagstring;
279269606e3fSchristos   words = 1;
279369606e3fSchristos   *p++ = '-';
279469606e3fSchristos   while (flags != 0)
279569606e3fSchristos     {
279669606e3fSchristos       /* Add the flag letter or name to the string.  */
279769606e3fSchristos       if (short_option (flags->cs->c))
279869606e3fSchristos 	*p++ = flags->cs->c;
279969606e3fSchristos       else
280069606e3fSchristos 	{
280169606e3fSchristos           if (*p != '-')
280269606e3fSchristos             {
280369606e3fSchristos               *p++ = ' ';
280469606e3fSchristos               *p++ = '-';
280569606e3fSchristos             }
280669606e3fSchristos 	  *p++ = '-';
280769606e3fSchristos 	  strcpy (p, flags->cs->long_name);
280869606e3fSchristos 	  p += strlen (p);
280969606e3fSchristos 	}
281069606e3fSchristos       if (flags->arg != 0)
281169606e3fSchristos 	{
281269606e3fSchristos 	  /* A flag that takes an optional argument which in this case is
281369606e3fSchristos 	     omitted is specified by ARG being "".  We must distinguish
281469606e3fSchristos 	     because a following flag appended without an intervening " -"
281569606e3fSchristos 	     is considered the arg for the first.  */
281669606e3fSchristos 	  if (flags->arg[0] != '\0')
281769606e3fSchristos 	    {
281869606e3fSchristos 	      /* Add its argument too.  */
281969606e3fSchristos 	      *p++ = !short_option (flags->cs->c) ? '=' : ' ';
282069606e3fSchristos 	      p = quote_for_env (p, flags->arg);
282169606e3fSchristos 	    }
282269606e3fSchristos 	  ++words;
282369606e3fSchristos 	  /* Write a following space and dash, for the next flag.  */
282469606e3fSchristos 	  *p++ = ' ';
282569606e3fSchristos 	  *p++ = '-';
282669606e3fSchristos 	}
282769606e3fSchristos       else if (!short_option (flags->cs->c))
282869606e3fSchristos 	{
282969606e3fSchristos 	  ++words;
283069606e3fSchristos 	  /* Long options must each go in their own word,
283169606e3fSchristos 	     so we write the following space and dash.  */
283269606e3fSchristos 	  *p++ = ' ';
283369606e3fSchristos 	  *p++ = '-';
283469606e3fSchristos 	}
283569606e3fSchristos       flags = flags->next;
283669606e3fSchristos     }
283769606e3fSchristos 
283869606e3fSchristos   /* Define MFLAGS before appending variable definitions.  */
283969606e3fSchristos 
284069606e3fSchristos   if (p == &flagstring[1])
284169606e3fSchristos     /* No flags.  */
284269606e3fSchristos     flagstring[0] = '\0';
284369606e3fSchristos   else if (p[-1] == '-')
284469606e3fSchristos     {
284569606e3fSchristos       /* Kill the final space and dash.  */
284669606e3fSchristos       p -= 2;
284769606e3fSchristos       *p = '\0';
284869606e3fSchristos     }
284969606e3fSchristos   else
285069606e3fSchristos     /* Terminate the string.  */
285169606e3fSchristos     *p = '\0';
285269606e3fSchristos 
285369606e3fSchristos   /* Since MFLAGS is not parsed for flags, there is no reason to
285469606e3fSchristos      override any makefile redefinition.  */
285569606e3fSchristos   (void) define_variable ("MFLAGS", 6, flagstring, o_env, 1);
285669606e3fSchristos 
285769606e3fSchristos   if (all && command_variables != 0)
285869606e3fSchristos     {
285969606e3fSchristos       /* Now write a reference to $(MAKEOVERRIDES), which contains all the
286069606e3fSchristos 	 command-line variable definitions.  */
286169606e3fSchristos 
286269606e3fSchristos       if (p == &flagstring[1])
286369606e3fSchristos 	/* No flags written, so elide the leading dash already written.  */
286469606e3fSchristos 	p = flagstring;
286569606e3fSchristos       else
286669606e3fSchristos 	{
286769606e3fSchristos 	  /* Separate the variables from the switches with a "--" arg.  */
286869606e3fSchristos 	  if (p[-1] != '-')
286969606e3fSchristos 	    {
287069606e3fSchristos 	      /* We did not already write a trailing " -".  */
287169606e3fSchristos 	      *p++ = ' ';
287269606e3fSchristos 	      *p++ = '-';
287369606e3fSchristos 	    }
287469606e3fSchristos 	  /* There is a trailing " -"; fill it out to " -- ".  */
287569606e3fSchristos 	  *p++ = '-';
287669606e3fSchristos 	  *p++ = ' ';
287769606e3fSchristos 	}
287869606e3fSchristos 
287969606e3fSchristos       /* Copy in the string.  */
288069606e3fSchristos       if (posix_pedantic)
288169606e3fSchristos 	{
288269606e3fSchristos 	  bcopy (posixref, p, sizeof posixref - 1);
288369606e3fSchristos 	  p += sizeof posixref - 1;
288469606e3fSchristos 	}
288569606e3fSchristos       else
288669606e3fSchristos 	{
288769606e3fSchristos 	  bcopy (ref, p, sizeof ref - 1);
288869606e3fSchristos 	  p += sizeof ref - 1;
288969606e3fSchristos 	}
289069606e3fSchristos     }
289169606e3fSchristos   else if (p == &flagstring[1])
289269606e3fSchristos     {
289369606e3fSchristos       words = 0;
289469606e3fSchristos       --p;
289569606e3fSchristos     }
289669606e3fSchristos   else if (p[-1] == '-')
289769606e3fSchristos     /* Kill the final space and dash.  */
289869606e3fSchristos     p -= 2;
289969606e3fSchristos   /* Terminate the string.  */
290069606e3fSchristos   *p = '\0';
290169606e3fSchristos 
290269606e3fSchristos   v = define_variable ("MAKEFLAGS", 9,
290369606e3fSchristos 		       /* If there are switches, omit the leading dash
290469606e3fSchristos 			  unless it is a single long option with two
290569606e3fSchristos 			  leading dashes.  */
290669606e3fSchristos 		       &flagstring[(flagstring[0] == '-'
290769606e3fSchristos 				    && flagstring[1] != '-')
290869606e3fSchristos 				   ? 1 : 0],
290969606e3fSchristos 		       /* This used to use o_env, but that lost when a
291069606e3fSchristos 			  makefile defined MAKEFLAGS.  Makefiles set
291169606e3fSchristos 			  MAKEFLAGS to add switches, but we still want
291269606e3fSchristos 			  to redefine its value with the full set of
291369606e3fSchristos 			  switches.  Of course, an override or command
291469606e3fSchristos 			  definition will still take precedence.  */
291569606e3fSchristos 		       o_file, 1);
291669606e3fSchristos   if (! all)
291769606e3fSchristos     /* The first time we are called, set MAKEFLAGS to always be exported.
291869606e3fSchristos        We should not do this again on the second call, because that is
291969606e3fSchristos        after reading makefiles which might have done `unexport MAKEFLAGS'. */
292069606e3fSchristos     v->export = v_export;
292169606e3fSchristos }
292269606e3fSchristos 
292369606e3fSchristos /* Print version information.  */
292469606e3fSchristos 
292569606e3fSchristos static void
292669606e3fSchristos print_version (void)
292769606e3fSchristos {
292869606e3fSchristos   static int printed_version = 0;
292969606e3fSchristos 
293069606e3fSchristos   char *precede = print_data_base_flag ? "# " : "";
293169606e3fSchristos 
293269606e3fSchristos   if (printed_version)
293369606e3fSchristos     /* Do it only once.  */
293469606e3fSchristos     return;
293569606e3fSchristos 
293669606e3fSchristos   /* Print this untranslated.  The coding standards recommend translating the
293769606e3fSchristos      (C) to the copyright symbol, but this string is going to change every
293869606e3fSchristos      year, and none of the rest of it should be translated (including the
293969606e3fSchristos      word "Copyright", so it hardly seems worth it.  */
294069606e3fSchristos 
294169606e3fSchristos   printf ("%sGNU Make %s\n\
294269606e3fSchristos %sCopyright (C) 2006  Free Software Foundation, Inc.\n",
294369606e3fSchristos           precede, version_string, precede);
294469606e3fSchristos 
294569606e3fSchristos   printf (_("%sThis is free software; see the source for copying conditions.\n\
294669606e3fSchristos %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
294769606e3fSchristos %sPARTICULAR PURPOSE.\n"),
294869606e3fSchristos             precede, precede, precede);
294969606e3fSchristos 
295069606e3fSchristos   if (!remote_description || *remote_description == '\0')
295169606e3fSchristos     printf (_("\n%sThis program built for %s\n"), precede, make_host);
295269606e3fSchristos   else
295369606e3fSchristos     printf (_("\n%sThis program built for %s (%s)\n"),
295469606e3fSchristos             precede, make_host, remote_description);
295569606e3fSchristos 
295669606e3fSchristos   printed_version = 1;
295769606e3fSchristos 
295869606e3fSchristos   /* Flush stdout so the user doesn't have to wait to see the
295969606e3fSchristos      version information while things are thought about.  */
296069606e3fSchristos   fflush (stdout);
296169606e3fSchristos }
296269606e3fSchristos 
296369606e3fSchristos /* Print a bunch of information about this and that.  */
296469606e3fSchristos 
296569606e3fSchristos static void
296669606e3fSchristos print_data_base ()
296769606e3fSchristos {
296869606e3fSchristos   time_t when;
296969606e3fSchristos 
297069606e3fSchristos   when = time ((time_t *) 0);
297169606e3fSchristos   printf (_("\n# Make data base, printed on %s"), ctime (&when));
297269606e3fSchristos 
297369606e3fSchristos   print_variable_data_base ();
297469606e3fSchristos   print_dir_data_base ();
297569606e3fSchristos   print_rule_data_base ();
297669606e3fSchristos   print_file_data_base ();
297769606e3fSchristos   print_vpath_data_base ();
297869606e3fSchristos   strcache_print_stats ("#");
297969606e3fSchristos 
298069606e3fSchristos   when = time ((time_t *) 0);
298169606e3fSchristos   printf (_("\n# Finished Make data base on %s\n"), ctime (&when));
298269606e3fSchristos }
298369606e3fSchristos 
298469606e3fSchristos static void
298569606e3fSchristos clean_jobserver (int status)
298669606e3fSchristos {
298769606e3fSchristos   char token = '+';
298869606e3fSchristos 
298969606e3fSchristos   /* Sanity: have we written all our jobserver tokens back?  If our
299069606e3fSchristos      exit status is 2 that means some kind of syntax error; we might not
299169606e3fSchristos      have written all our tokens so do that now.  If tokens are left
299269606e3fSchristos      after any other error code, that's bad.  */
299369606e3fSchristos 
299469606e3fSchristos   if (job_fds[0] != -1 && jobserver_tokens)
299569606e3fSchristos     {
299669606e3fSchristos       if (status != 2)
299769606e3fSchristos         error (NILF,
299869606e3fSchristos                "INTERNAL: Exiting with %u jobserver tokens (should be 0)!",
299969606e3fSchristos                jobserver_tokens);
300069606e3fSchristos       else
300169606e3fSchristos         while (jobserver_tokens--)
300269606e3fSchristos           {
300369606e3fSchristos             int r;
300469606e3fSchristos 
300569606e3fSchristos             EINTRLOOP (r, write (job_fds[1], &token, 1));
300669606e3fSchristos             if (r != 1)
300769606e3fSchristos               perror_with_name ("write", "");
300869606e3fSchristos           }
300969606e3fSchristos     }
301069606e3fSchristos 
301169606e3fSchristos 
301269606e3fSchristos   /* Sanity: If we're the master, were all the tokens written back?  */
301369606e3fSchristos 
301469606e3fSchristos   if (master_job_slots)
301569606e3fSchristos     {
301669606e3fSchristos       /* We didn't write one for ourself, so start at 1.  */
301769606e3fSchristos       unsigned int tcnt = 1;
301869606e3fSchristos 
301969606e3fSchristos       /* Close the write side, so the read() won't hang.  */
302069606e3fSchristos       close (job_fds[1]);
302169606e3fSchristos 
302269606e3fSchristos       while (read (job_fds[0], &token, 1) == 1)
302369606e3fSchristos         ++tcnt;
302469606e3fSchristos 
302569606e3fSchristos       if (tcnt != master_job_slots)
302669606e3fSchristos         error (NILF,
302769606e3fSchristos                "INTERNAL: Exiting with %u jobserver tokens available; should be %u!",
302869606e3fSchristos                tcnt, master_job_slots);
302969606e3fSchristos 
303069606e3fSchristos       close (job_fds[0]);
303169606e3fSchristos     }
303269606e3fSchristos }
303369606e3fSchristos 
303469606e3fSchristos /* Exit with STATUS, cleaning up as necessary.  */
303569606e3fSchristos 
303669606e3fSchristos void
303769606e3fSchristos die (int status)
303869606e3fSchristos {
303969606e3fSchristos   static char dying = 0;
304069606e3fSchristos 
304169606e3fSchristos   if (!dying)
304269606e3fSchristos     {
304369606e3fSchristos       int err;
304469606e3fSchristos 
304569606e3fSchristos       dying = 1;
304669606e3fSchristos 
304769606e3fSchristos       if (print_version_flag)
304869606e3fSchristos 	print_version ();
304969606e3fSchristos 
305069606e3fSchristos       /* Wait for children to die.  */
305169606e3fSchristos       err = (status != 0);
305269606e3fSchristos       while (job_slots_used > 0)
305369606e3fSchristos 	reap_children (1, err);
305469606e3fSchristos 
305569606e3fSchristos       /* Let the remote job module clean up its state.  */
305669606e3fSchristos       remote_cleanup ();
305769606e3fSchristos 
305869606e3fSchristos       /* Remove the intermediate files.  */
305969606e3fSchristos       remove_intermediates (0);
306069606e3fSchristos 
306169606e3fSchristos       if (print_data_base_flag)
306269606e3fSchristos 	print_data_base ();
306369606e3fSchristos 
306469606e3fSchristos       clean_jobserver (status);
306569606e3fSchristos 
306669606e3fSchristos       /* Try to move back to the original directory.  This is essential on
306769606e3fSchristos 	 MS-DOS (where there is really only one process), and on Unix it
306869606e3fSchristos 	 puts core files in the original directory instead of the -C
306969606e3fSchristos 	 directory.  Must wait until after remove_intermediates(), or unlinks
307069606e3fSchristos          of relative pathnames fail.  */
307169606e3fSchristos       if (directory_before_chdir != 0)
307269606e3fSchristos 	chdir (directory_before_chdir);
307369606e3fSchristos 
307469606e3fSchristos       log_working_directory (0);
307569606e3fSchristos     }
307669606e3fSchristos 
307769606e3fSchristos   exit (status);
307869606e3fSchristos }
307969606e3fSchristos 
308069606e3fSchristos /* Write a message indicating that we've just entered or
308169606e3fSchristos    left (according to ENTERING) the current directory.  */
308269606e3fSchristos 
308369606e3fSchristos void
308469606e3fSchristos log_working_directory (int entering)
308569606e3fSchristos {
308669606e3fSchristos   static int entered = 0;
308769606e3fSchristos 
308869606e3fSchristos   /* Print nothing without the flag.  Don't print the entering message
308969606e3fSchristos      again if we already have.  Don't print the leaving message if we
309069606e3fSchristos      haven't printed the entering message.  */
309169606e3fSchristos   if (! print_directory_flag || entering == entered)
309269606e3fSchristos     return;
309369606e3fSchristos 
309469606e3fSchristos   entered = entering;
309569606e3fSchristos 
309669606e3fSchristos   if (print_data_base_flag)
309769606e3fSchristos     fputs ("# ", stdout);
309869606e3fSchristos 
309969606e3fSchristos   /* Use entire sentences to give the translators a fighting chance.  */
310069606e3fSchristos 
310169606e3fSchristos   if (makelevel == 0)
310269606e3fSchristos     if (starting_directory == 0)
310369606e3fSchristos       if (entering)
310469606e3fSchristos         printf (_("%s: Entering an unknown directory\n"), program);
310569606e3fSchristos       else
310669606e3fSchristos         printf (_("%s: Leaving an unknown directory\n"), program);
310769606e3fSchristos     else
310869606e3fSchristos       if (entering)
310969606e3fSchristos         printf (_("%s: Entering directory `%s'\n"),
311069606e3fSchristos                 program, starting_directory);
311169606e3fSchristos       else
311269606e3fSchristos         printf (_("%s: Leaving directory `%s'\n"),
311369606e3fSchristos                 program, starting_directory);
311469606e3fSchristos   else
311569606e3fSchristos     if (starting_directory == 0)
311669606e3fSchristos       if (entering)
311769606e3fSchristos         printf (_("%s[%u]: Entering an unknown directory\n"),
311869606e3fSchristos                 program, makelevel);
311969606e3fSchristos       else
312069606e3fSchristos         printf (_("%s[%u]: Leaving an unknown directory\n"),
312169606e3fSchristos                 program, makelevel);
312269606e3fSchristos     else
312369606e3fSchristos       if (entering)
312469606e3fSchristos         printf (_("%s[%u]: Entering directory `%s'\n"),
312569606e3fSchristos                 program, makelevel, starting_directory);
312669606e3fSchristos       else
312769606e3fSchristos         printf (_("%s[%u]: Leaving directory `%s'\n"),
312869606e3fSchristos                 program, makelevel, starting_directory);
312969606e3fSchristos 
313069606e3fSchristos   /* Flush stdout to be sure this comes before any stderr output.  */
313169606e3fSchristos   fflush (stdout);
313269606e3fSchristos }
3133