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