1 /* $NetBSD: main.c,v 1.91 2003/08/07 11:14:53 agc Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Adam de Boor. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1989 by Berkeley Softworks 37 * All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * Adam de Boor. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. All advertising materials mentioning features or use of this software 51 * must display the following acknowledgement: 52 * This product includes software developed by the University of 53 * California, Berkeley and its contributors. 54 * 4. Neither the name of the University nor the names of its contributors 55 * may be used to endorse or promote products derived from this software 56 * without specific prior written permission. 57 * 58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68 * SUCH DAMAGE. 69 */ 70 71 #ifdef MAKE_BOOTSTRAP 72 static char rcsid[] = "$NetBSD: main.c,v 1.91 2003/08/07 11:14:53 agc Exp $"; 73 #else 74 #include <sys/cdefs.h> 75 #ifndef lint 76 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\n\ 77 The Regents of the University of California. All rights reserved.\n"); 78 #endif /* not lint */ 79 80 #ifndef lint 81 #if 0 82 static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94"; 83 #else 84 __RCSID("$NetBSD: main.c,v 1.91 2003/08/07 11:14:53 agc Exp $"); 85 #endif 86 #endif /* not lint */ 87 #endif 88 89 /*- 90 * main.c -- 91 * The main file for this entire program. Exit routines etc 92 * reside here. 93 * 94 * Utility functions defined in this file: 95 * Main_ParseArgLine Takes a line of arguments, breaks them and 96 * treats them as if they were given when first 97 * invoked. Used by the parse module to implement 98 * the .MFLAGS target. 99 * 100 * Error Print a tagged error message. The global 101 * MAKE variable must have been defined. This 102 * takes a format string and two optional 103 * arguments for it. 104 * 105 * Fatal Print an error message and exit. Also takes 106 * a format string and two arguments. 107 * 108 * Punt Aborts all jobs and exits with a message. Also 109 * takes a format string and two arguments. 110 * 111 * Finish Finish things up by printing the number of 112 * errors which occurred, as passed to it, and 113 * exiting. 114 */ 115 116 #include <sys/types.h> 117 #include <sys/time.h> 118 #include <sys/param.h> 119 #include <sys/resource.h> 120 #include <sys/signal.h> 121 #include <sys/stat.h> 122 #ifndef MAKE_BOOTSTRAP 123 #include <sys/utsname.h> 124 #endif 125 #include <sys/wait.h> 126 127 #include <errno.h> 128 #include <fcntl.h> 129 #include <stdarg.h> 130 #include <stdio.h> 131 #include <stdlib.h> 132 #include <time.h> 133 134 #include "make.h" 135 #include "hash.h" 136 #include "dir.h" 137 #include "job.h" 138 #include "pathnames.h" 139 #include "trace.h" 140 141 #ifdef USE_IOVEC 142 #include <sys/uio.h> 143 #endif 144 145 #ifndef DEFMAXLOCAL 146 #define DEFMAXLOCAL DEFMAXJOBS 147 #endif /* DEFMAXLOCAL */ 148 149 Lst create; /* Targets to be made */ 150 time_t now; /* Time at start of make */ 151 GNode *DEFAULT; /* .DEFAULT node */ 152 Boolean allPrecious; /* .PRECIOUS given on line by itself */ 153 154 static Boolean noBuiltins; /* -r flag */ 155 static Lst makefiles; /* ordered list of makefiles to read */ 156 static Boolean printVars; /* print value of one or more vars */ 157 static Lst variables; /* list of variables to print */ 158 int maxJobs; /* -j argument */ 159 static int maxLocal; /* -L argument */ 160 Boolean compatMake; /* -B argument */ 161 Boolean debug; /* -d flag */ 162 Boolean noExecute; /* -n flag */ 163 Boolean noRecursiveExecute; /* -N flag */ 164 Boolean keepgoing; /* -k flag */ 165 Boolean queryFlag; /* -q flag */ 166 Boolean touchFlag; /* -t flag */ 167 Boolean usePipes; /* !-P flag */ 168 Boolean ignoreErrors; /* -i flag */ 169 Boolean beSilent; /* -s flag */ 170 Boolean oldVars; /* variable substitution style */ 171 Boolean checkEnvFirst; /* -e flag */ 172 Boolean parseWarnFatal; /* -W flag */ 173 Boolean jobServer; /* -J flag */ 174 Boolean varNoExportEnv; /* -X flag */ 175 static Boolean jobsRunning; /* TRUE if the jobs might be running */ 176 static const char * tracefile; 177 static char * Check_Cwd_av(int, char **, int); 178 static void MainParseArgs(int, char **); 179 static int ReadMakefile(ClientData, ClientData); 180 static void usage(void); 181 182 static char curdir[MAXPATHLEN + 1]; /* startup directory */ 183 static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */ 184 char *progname; /* the program name */ 185 186 Boolean forceJobs = FALSE; 187 188 extern Lst parseIncPath; 189 190 /*- 191 * MainParseArgs -- 192 * Parse a given argument vector. Called from main() and from 193 * Main_ParseArgLine() when the .MAKEFLAGS target is used. 194 * 195 * XXX: Deal with command line overriding .MAKEFLAGS in makefile 196 * 197 * Results: 198 * None 199 * 200 * Side Effects: 201 * Various global and local flags will be set depending on the flags 202 * given 203 */ 204 static void 205 MainParseArgs(int argc, char **argv) 206 { 207 char *p; 208 int c; 209 210 optind = 1; /* since we're called more than once */ 211 #ifdef REMOTE 212 # define OPTFLAGS "BD:I:J:L:NPST:V:WXd:ef:ij:km:nqrst" 213 #else 214 # define OPTFLAGS "BD:I:J:NPST:V:WXd:ef:ij:km:nqrst" 215 #endif 216 rearg: while((c = getopt(argc, argv, OPTFLAGS)) != -1) { 217 switch(c) { 218 case 'D': 219 Var_Set(optarg, "1", VAR_GLOBAL, 0); 220 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); 221 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 222 break; 223 case 'I': 224 Parse_AddIncludeDir(optarg); 225 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); 226 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 227 break; 228 case 'J': 229 if (sscanf(optarg, "%d,%d", &job_pipe[0], &job_pipe[1]) != 2) { 230 /* backslash to avoid trigraph ??) */ 231 (void)fprintf(stderr, 232 "%s: internal error -- J option malformed (%s?\?)\n", 233 progname, optarg); 234 usage(); 235 } 236 if ((fcntl(job_pipe[0], F_GETFD, 0) < 0) || 237 (fcntl(job_pipe[1], F_GETFD, 0) < 0)) { 238 #if 0 239 (void)fprintf(stderr, 240 "%s: warning -- J descriptors were closed!\n", 241 progname); 242 #endif 243 job_pipe[0] = -1; 244 job_pipe[1] = -1; 245 compatMake = TRUE; 246 } else { 247 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 248 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 249 jobServer = TRUE; 250 } 251 break; 252 case 'V': 253 printVars = TRUE; 254 (void)Lst_AtEnd(variables, (ClientData)optarg); 255 Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL); 256 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 257 break; 258 case 'B': 259 compatMake = TRUE; 260 Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL); 261 break; 262 #ifdef REMOTE 263 case 'L': 264 maxLocal = strtol(optarg, &p, 0); 265 if (*p != '\0' || maxLocal < 1) { 266 (void) fprintf(stderr, "%s: illegal argument to -L -- must be positive integer!\n", 267 progname); 268 exit(1); 269 } 270 Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL); 271 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 272 break; 273 #endif 274 case 'N': 275 noExecute = TRUE; 276 noRecursiveExecute = TRUE; 277 Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL); 278 break; 279 case 'P': 280 usePipes = FALSE; 281 Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL); 282 break; 283 case 'S': 284 keepgoing = FALSE; 285 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); 286 break; 287 case 'T': 288 tracefile = estrdup(optarg); 289 Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL); 290 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 291 break; 292 case 'W': 293 parseWarnFatal = TRUE; 294 break; 295 case 'X': 296 varNoExportEnv = TRUE; 297 Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL); 298 break; 299 case 'd': { 300 char *modules = optarg; 301 302 for (; *modules; ++modules) 303 switch (*modules) { 304 case 'A': 305 debug = ~0; 306 break; 307 case 'a': 308 debug |= DEBUG_ARCH; 309 break; 310 case 'c': 311 debug |= DEBUG_COND; 312 break; 313 case 'd': 314 debug |= DEBUG_DIR; 315 break; 316 case 'f': 317 debug |= DEBUG_FOR; 318 break; 319 case 'g': 320 if (modules[1] == '1') { 321 debug |= DEBUG_GRAPH1; 322 ++modules; 323 } 324 else if (modules[1] == '2') { 325 debug |= DEBUG_GRAPH2; 326 ++modules; 327 } 328 break; 329 case 'j': 330 debug |= DEBUG_JOB; 331 break; 332 case 'm': 333 debug |= DEBUG_MAKE; 334 break; 335 case 's': 336 debug |= DEBUG_SUFF; 337 break; 338 case 't': 339 debug |= DEBUG_TARG; 340 break; 341 case 'v': 342 debug |= DEBUG_VAR; 343 break; 344 case 'x': 345 debug |= DEBUG_SHELL; 346 break; 347 default: 348 (void)fprintf(stderr, 349 "%s: illegal argument to d option -- %c\n", 350 progname, *modules); 351 usage(); 352 } 353 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); 354 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 355 break; 356 } 357 case 'e': 358 checkEnvFirst = TRUE; 359 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); 360 break; 361 case 'f': 362 (void)Lst_AtEnd(makefiles, (ClientData)optarg); 363 break; 364 case 'i': 365 ignoreErrors = TRUE; 366 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); 367 break; 368 case 'j': 369 forceJobs = TRUE; 370 maxJobs = strtol(optarg, &p, 0); 371 if (*p != '\0' || maxJobs < 1) { 372 (void) fprintf(stderr, "%s: illegal argument to -j -- must be positive integer!\n", 373 progname); 374 exit(1); 375 } 376 #ifndef REMOTE 377 maxLocal = maxJobs; 378 #endif 379 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL); 380 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 381 break; 382 case 'k': 383 keepgoing = TRUE; 384 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); 385 break; 386 case 'm': 387 (void) Dir_AddDir(sysIncPath, optarg); 388 Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL); 389 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 390 break; 391 case 'n': 392 noExecute = TRUE; 393 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); 394 break; 395 case 'q': 396 queryFlag = TRUE; 397 /* Kind of nonsensical, wot? */ 398 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); 399 break; 400 case 'r': 401 noBuiltins = TRUE; 402 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); 403 break; 404 case 's': 405 beSilent = TRUE; 406 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); 407 break; 408 case 't': 409 touchFlag = TRUE; 410 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); 411 break; 412 default: 413 case '?': 414 usage(); 415 } 416 } 417 418 oldVars = TRUE; 419 420 /* 421 * See if the rest of the arguments are variable assignments and 422 * perform them if so. Else take them to be targets and stuff them 423 * on the end of the "create" list. 424 */ 425 for (argv += optind, argc -= optind; *argv; ++argv, --argc) 426 if (Parse_IsVar(*argv)) { 427 Parse_DoVar(*argv, VAR_CMD); 428 } else { 429 if (!**argv) 430 Punt("illegal (null) argument."); 431 if (**argv == '-') { 432 if ((*argv)[1]) 433 optind = 0; /* -flag... */ 434 else 435 optind = 1; /* - */ 436 goto rearg; 437 } 438 (void)Lst_AtEnd(create, (ClientData)estrdup(*argv)); 439 } 440 } 441 442 /*- 443 * Main_ParseArgLine -- 444 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 445 * is encountered and by main() when reading the .MAKEFLAGS envariable. 446 * Takes a line of arguments and breaks it into its 447 * component words and passes those words and the number of them to the 448 * MainParseArgs function. 449 * The line should have all its leading whitespace removed. 450 * 451 * Input: 452 * line Line to fracture 453 * 454 * Results: 455 * None 456 * 457 * Side Effects: 458 * Only those that come from the various arguments. 459 */ 460 void 461 Main_ParseArgLine(char *line) 462 { 463 char **argv; /* Manufactured argument vector */ 464 int argc; /* Number of arguments in argv */ 465 char *args; /* Space used by the args */ 466 char *buf, *p1; 467 char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1); 468 size_t len; 469 470 if (line == NULL) 471 return; 472 for (; *line == ' '; ++line) 473 continue; 474 if (!*line) 475 return; 476 477 buf = emalloc(len = strlen(line) + strlen(argv0) + 2); 478 (void)snprintf(buf, len, "%s %s", argv0, line); 479 if (p1) 480 free(p1); 481 482 argv = brk_string(buf, &argc, TRUE, &args); 483 free(buf); 484 MainParseArgs(argc, argv); 485 486 free(args); 487 free(argv); 488 } 489 490 Boolean 491 Main_SetObjdir(const char *path) 492 { 493 struct stat sb; 494 char *p = NULL; 495 char buf[MAXPATHLEN + 1]; 496 Boolean rc = FALSE; 497 498 /* expand variable substitutions */ 499 if (strchr(path, '$') != 0) { 500 snprintf(buf, MAXPATHLEN, "%s", path); 501 path = p = Var_Subst(NULL, buf, VAR_GLOBAL, 0); 502 } 503 504 if (path[0] != '/') { 505 snprintf(buf, MAXPATHLEN, "%s/%s", curdir, path); 506 path = buf; 507 } 508 509 /* look for the directory and try to chdir there */ 510 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 511 if (chdir(path)) { 512 (void)fprintf(stderr, "make warning: %s: %s.\n", 513 path, strerror(errno)); 514 } else { 515 strncpy(objdir, path, MAXPATHLEN); 516 Var_Set(".OBJDIR", objdir, VAR_GLOBAL, 0); 517 setenv("PWD", objdir, 1); 518 Dir_InitDot(); 519 rc = TRUE; 520 } 521 } 522 523 if (p) 524 free(p); 525 return rc; 526 } 527 528 529 /*- 530 * main -- 531 * The main function, for obvious reasons. Initializes variables 532 * and a few modules, then parses the arguments give it in the 533 * environment and on the command line. Reads the system makefile 534 * followed by either Makefile, makefile or the file given by the 535 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 536 * flags it has received by then uses either the Make or the Compat 537 * module to create the initial list of targets. 538 * 539 * Results: 540 * If -q was given, exits -1 if anything was out-of-date. Else it exits 541 * 0. 542 * 543 * Side Effects: 544 * The program exits when done. Targets are created. etc. etc. etc. 545 */ 546 int 547 main(int argc, char **argv) 548 { 549 Lst targs; /* target nodes to create -- passed to Make_Init */ 550 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */ 551 struct stat sb, sa; 552 char *p1, *path, *pwd; 553 char mdpath[MAXPATHLEN]; 554 char *machine = getenv("MACHINE"); 555 const char *machine_arch = getenv("MACHINE_ARCH"); 556 char *syspath = getenv("MAKESYSPATH"); 557 Lst sysMkPath; /* Path of sys.mk */ 558 char *cp = NULL, *start; 559 /* avoid faults on read-only strings */ 560 static char defsyspath[] = _PATH_DEFSYSPATH; 561 562 if ((progname = strrchr(argv[0], '/')) != NULL) 563 progname++; 564 else 565 progname = argv[0]; 566 #ifdef RLIMIT_NOFILE 567 /* 568 * get rid of resource limit on file descriptors 569 */ 570 { 571 struct rlimit rl; 572 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && 573 rl.rlim_cur != rl.rlim_max) { 574 rl.rlim_cur = rl.rlim_max; 575 (void) setrlimit(RLIMIT_NOFILE, &rl); 576 } 577 } 578 #endif 579 /* 580 * Find where we are and take care of PWD for the automounter... 581 * All this code is so that we know where we are when we start up 582 * on a different machine with pmake. 583 */ 584 if (getcwd(curdir, MAXPATHLEN) == NULL) { 585 (void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno)); 586 exit(2); 587 } 588 589 if (stat(curdir, &sa) == -1) { 590 (void)fprintf(stderr, "%s: %s: %s.\n", 591 progname, curdir, strerror(errno)); 592 exit(2); 593 } 594 595 /* 596 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX 597 * since the value of curdir can very depending on how we got 598 * here. Ie sitting at a shell prompt (shell that provides $PWD) 599 * or via subdir.mk in which case its likely a shell which does 600 * not provide it. 601 * So, to stop it breaking this case only, we ignore PWD if 602 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform. 603 */ 604 if ((pwd = getenv("PWD")) != NULL && getenv("MAKEOBJDIRPREFIX") == NULL) { 605 const char *makeobjdir = getenv("MAKEOBJDIR"); 606 607 if (makeobjdir == NULL || !strchr(makeobjdir, '$')) { 608 if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino && 609 sa.st_dev == sb.st_dev) 610 (void) strncpy(curdir, pwd, MAXPATHLEN); 611 } 612 } 613 614 /* 615 * Get the name of this type of MACHINE from utsname 616 * so we can share an executable for similar machines. 617 * (i.e. m68k: amiga hp300, mac68k, sun3, ...) 618 * 619 * Note that both MACHINE and MACHINE_ARCH are decided at 620 * run-time. 621 */ 622 if (!machine) { 623 #ifndef MAKE_BOOTSTRAP 624 struct utsname utsname; 625 626 if (uname(&utsname) == -1) { 627 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname, 628 strerror(errno)); 629 exit(2); 630 } 631 machine = utsname.machine; 632 #else 633 #ifdef MAKE_MACHINE 634 machine = MAKE_MACHINE; 635 #else 636 machine = "unknown"; 637 #endif 638 #endif 639 } 640 641 if (!machine_arch) { 642 #ifndef MACHINE_ARCH 643 #ifdef MAKE_MACHINE_ARCH 644 machine_arch = MAKE_MACHINE_ARCH; 645 #else 646 machine_arch = "unknown"; 647 #endif 648 #else 649 machine_arch = MACHINE_ARCH; 650 #endif 651 } 652 653 /* 654 * Just in case MAKEOBJDIR wants us to do something tricky. 655 */ 656 Var_Init(); /* Initialize the lists of variables for 657 * parsing arguments */ 658 Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0); 659 Var_Set("MACHINE", machine, VAR_GLOBAL, 0); 660 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0); 661 #ifdef MAKE_VERSION 662 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL, 0); 663 #endif 664 Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */ 665 666 /* 667 * Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that, 668 * MAKEOBJDIR is set in the environment, try only that value 669 * and fall back to .CURDIR if it does not exist. 670 * 671 * Otherwise, try _PATH_OBJDIR.MACHINE, _PATH_OBJDIR, and 672 * finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none 673 * of these paths exist, just use .CURDIR. 674 */ 675 Dir_Init(curdir); 676 (void) Main_SetObjdir(curdir); 677 678 if ((path = getenv("MAKEOBJDIRPREFIX")) != NULL) { 679 (void) snprintf(mdpath, MAXPATHLEN, "%s%s", path, curdir); 680 (void) Main_SetObjdir(mdpath); 681 } else if ((path = getenv("MAKEOBJDIR")) != NULL) { 682 (void) Main_SetObjdir(path); 683 } else { 684 (void) snprintf(mdpath, MAXPATHLEN, "%s.%s", _PATH_OBJDIR, machine); 685 if (!Main_SetObjdir(mdpath) && !Main_SetObjdir(_PATH_OBJDIR)) { 686 (void) snprintf(mdpath, MAXPATHLEN, "%s%s", 687 _PATH_OBJDIRPREFIX, curdir); 688 (void) Main_SetObjdir(mdpath); 689 } 690 } 691 692 create = Lst_Init(FALSE); 693 makefiles = Lst_Init(FALSE); 694 printVars = FALSE; 695 variables = Lst_Init(FALSE); 696 beSilent = FALSE; /* Print commands as executed */ 697 ignoreErrors = FALSE; /* Pay attention to non-zero returns */ 698 noExecute = FALSE; /* Execute all commands */ 699 noRecursiveExecute = FALSE; /* Execute all .MAKE targets */ 700 keepgoing = FALSE; /* Stop on error */ 701 allPrecious = FALSE; /* Remove targets when interrupted */ 702 queryFlag = FALSE; /* This is not just a check-run */ 703 noBuiltins = FALSE; /* Read the built-in rules */ 704 touchFlag = FALSE; /* Actually update targets */ 705 usePipes = TRUE; /* Catch child output in pipes */ 706 debug = 0; /* No debug verbosity, please. */ 707 jobsRunning = FALSE; 708 709 maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */ 710 #ifdef REMOTE 711 maxJobs = DEFMAXJOBS; /* Set default max concurrency */ 712 #else 713 maxJobs = maxLocal; 714 #endif 715 compatMake = FALSE; /* No compat mode */ 716 717 718 /* 719 * Initialize the parsing, directory and variable modules to prepare 720 * for the reading of inclusion paths and variable settings on the 721 * command line 722 */ 723 724 /* 725 * Initialize various variables. 726 * MAKE also gets this name, for compatibility 727 * .MAKEFLAGS gets set to the empty string just in case. 728 * MFLAGS also gets initialized empty, for compatibility. 729 */ 730 Parse_Init(); 731 Var_Set("MAKE", argv[0], VAR_GLOBAL, 0); 732 Var_Set(".MAKE", argv[0], VAR_GLOBAL, 0); 733 Var_Set(MAKEFLAGS, "", VAR_GLOBAL, 0); 734 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL, 0); 735 Var_Set("MFLAGS", "", VAR_GLOBAL, 0); 736 Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0); 737 738 /* 739 * First snag any flags out of the MAKE environment variable. 740 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's 741 * in a different format). 742 */ 743 #ifdef POSIX 744 Main_ParseArgLine(getenv("MAKEFLAGS")); 745 #else 746 Main_ParseArgLine(getenv("MAKE")); 747 #endif 748 749 MainParseArgs(argc, argv); 750 751 /* 752 * Be compatible if user did not specify -j and did not explicitly 753 * turned compatibility on 754 */ 755 if (!compatMake && !forceJobs) { 756 compatMake = TRUE; 757 } 758 759 /* 760 * Initialize archive, target and suffix modules in preparation for 761 * parsing the makefile(s) 762 */ 763 Arch_Init(); 764 Targ_Init(); 765 Suff_Init(); 766 Trace_Init(tracefile); 767 768 DEFAULT = NILGNODE; 769 (void)time(&now); 770 771 Trace_Log(MAKESTART, NULL); 772 773 /* 774 * Set up the .TARGETS variable to contain the list of targets to be 775 * created. If none specified, make the variable empty -- the parser 776 * will fill the thing in with the default or .MAIN target. 777 */ 778 if (!Lst_IsEmpty(create)) { 779 LstNode ln; 780 781 for (ln = Lst_First(create); ln != NILLNODE; 782 ln = Lst_Succ(ln)) { 783 char *name = (char *)Lst_Datum(ln); 784 785 Var_Append(".TARGETS", name, VAR_GLOBAL); 786 } 787 } else 788 Var_Set(".TARGETS", "", VAR_GLOBAL, 0); 789 790 791 /* 792 * If no user-supplied system path was given (through the -m option) 793 * add the directories from the DEFSYSPATH (more than one may be given 794 * as dir1:...:dirn) to the system include path. 795 */ 796 if (syspath == NULL || *syspath == '\0') 797 syspath = defsyspath; 798 else 799 syspath = strdup(syspath); 800 801 for (start = syspath; *start != '\0'; start = cp) { 802 for (cp = start; *cp != '\0' && *cp != ':'; cp++) 803 continue; 804 if (*cp == '\0') { 805 (void) Dir_AddDir(defIncPath, start); 806 } else { 807 *cp++ = '\0'; 808 (void) Dir_AddDir(defIncPath, start); 809 } 810 } 811 if (syspath != defsyspath) 812 free(syspath); 813 814 /* 815 * Read in the built-in rules first, followed by the specified 816 * makefile, if it was (makefile != (char *) NULL), or the default 817 * Makefile and makefile, in that order, if it wasn't. 818 */ 819 if (!noBuiltins) { 820 LstNode ln; 821 822 sysMkPath = Lst_Init (FALSE); 823 Dir_Expand(_PATH_DEFSYSMK, 824 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath, 825 sysMkPath); 826 if (Lst_IsEmpty(sysMkPath)) 827 Fatal("%s: no system rules (%s).", progname, 828 _PATH_DEFSYSMK); 829 ln = Lst_Find(sysMkPath, (ClientData)NULL, ReadMakefile); 830 if (ln != NILLNODE) 831 Fatal("%s: cannot open %s.", progname, 832 (char *)Lst_Datum(ln)); 833 } 834 835 if (!Lst_IsEmpty(makefiles)) { 836 LstNode ln; 837 838 ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile); 839 if (ln != NILLNODE) 840 Fatal("%s: cannot open %s.", progname, 841 (char *)Lst_Datum(ln)); 842 } else if (!ReadMakefile(UNCONST("makefile"), NULL)) 843 (void)ReadMakefile(UNCONST("Makefile"), NULL); 844 845 (void)ReadMakefile(UNCONST(".depend"), NULL); 846 847 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); 848 if (p1) 849 free(p1); 850 851 if (!jobServer && !compatMake) 852 Job_ServerStart(maxJobs); 853 if (DEBUG(JOB)) 854 printf("job_pipe %d %d, maxjobs %d maxlocal %d compat %d\n", job_pipe[0], job_pipe[1], maxJobs, 855 maxLocal, compatMake); 856 857 Main_ExportMAKEFLAGS(TRUE); /* initial export */ 858 859 Check_Cwd_av(0, NULL, 0); /* initialize it */ 860 861 862 /* 863 * For compatibility, look at the directories in the VPATH variable 864 * and add them to the search path, if the variable is defined. The 865 * variable's value is in the same format as the PATH envariable, i.e. 866 * <directory>:<directory>:<directory>... 867 */ 868 if (Var_Exists("VPATH", VAR_CMD)) { 869 char *vpath, savec; 870 /* 871 * GCC stores string constants in read-only memory, but 872 * Var_Subst will want to write this thing, so store it 873 * in an array 874 */ 875 static char VPATH[] = "${VPATH}"; 876 877 vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE); 878 path = vpath; 879 do { 880 /* skip to end of directory */ 881 for (cp = path; *cp != ':' && *cp != '\0'; cp++) 882 continue; 883 /* Save terminator character so know when to stop */ 884 savec = *cp; 885 *cp = '\0'; 886 /* Add directory to search path */ 887 (void) Dir_AddDir(dirSearchPath, path); 888 *cp = savec; 889 path = cp + 1; 890 } while (savec == ':'); 891 (void)free((Address)vpath); 892 } 893 894 /* 895 * Now that all search paths have been read for suffixes et al, it's 896 * time to add the default search path to their lists... 897 */ 898 Suff_DoPaths(); 899 900 /* 901 * Propagate attributes through :: dependency lists. 902 */ 903 Targ_Propagate(); 904 905 /* print the initial graph, if the user requested it */ 906 if (DEBUG(GRAPH1)) 907 Targ_PrintGraph(1); 908 909 /* print the values of any variables requested by the user */ 910 if (printVars) { 911 LstNode ln; 912 913 for (ln = Lst_First(variables); ln != NILLNODE; 914 ln = Lst_Succ(ln)) { 915 char *value = Var_Value((char *)Lst_Datum(ln), 916 VAR_GLOBAL, &p1); 917 918 printf("%s\n", value ? value : ""); 919 if (p1) 920 free(p1); 921 } 922 } 923 924 /* 925 * Have now read the entire graph and need to make a list of targets 926 * to create. If none was given on the command line, we consult the 927 * parsing module to find the main target(s) to create. 928 */ 929 if (Lst_IsEmpty(create)) 930 targs = Parse_MainName(); 931 else 932 targs = Targ_FindList(create, TARG_CREATE); 933 934 if (!compatMake && !printVars) { 935 /* 936 * Initialize job module before traversing the graph, now that 937 * any .BEGIN and .END targets have been read. This is done 938 * only if the -q flag wasn't given (to prevent the .BEGIN from 939 * being executed should it exist). 940 */ 941 if (!queryFlag) { 942 if (maxLocal == -1) 943 maxLocal = maxJobs; 944 Job_Init(maxJobs, maxLocal); 945 jobsRunning = TRUE; 946 } 947 948 /* Traverse the graph, checking on all the targets */ 949 outOfDate = Make_Run(targs); 950 } else if (!printVars) { 951 /* 952 * Compat_Init will take care of creating all the targets as 953 * well as initializing the module. 954 */ 955 Compat_Run(targs); 956 } 957 958 #ifdef CLEANUP 959 Lst_Destroy(targs, NOFREE); 960 Lst_Destroy(variables, NOFREE); 961 Lst_Destroy(makefiles, NOFREE); 962 Lst_Destroy(create, (void (*)(ClientData))) free; 963 #endif 964 965 /* print the graph now it's been processed if the user requested it */ 966 if (DEBUG(GRAPH2)) 967 Targ_PrintGraph(2); 968 969 Trace_Log(MAKEEND, 0); 970 971 Suff_End(); 972 Targ_End(); 973 Arch_End(); 974 Var_End(); 975 Parse_End(); 976 Dir_End(); 977 Job_End(); 978 Trace_End(); 979 980 if (queryFlag && outOfDate) 981 return(1); 982 else 983 return(0); 984 } 985 986 /*- 987 * ReadMakefile -- 988 * Open and parse the given makefile. 989 * 990 * Results: 991 * TRUE if ok. FALSE if couldn't open file. 992 * 993 * Side Effects: 994 * lots 995 */ 996 static Boolean 997 ReadMakefile(ClientData p, ClientData q) 998 { 999 char *fname = p; /* makefile to read */ 1000 FILE *stream; 1001 size_t len = MAXPATHLEN; 1002 char *name, *path = emalloc(len); 1003 int setMAKEFILE; 1004 1005 if (!strcmp(fname, "-")) { 1006 Parse_File("(stdin)", stdin); 1007 Var_Set("MAKEFILE", "", VAR_GLOBAL, 0); 1008 } else { 1009 setMAKEFILE = strcmp(fname, ".depend"); 1010 1011 /* if we've chdir'd, rebuild the path name */ 1012 if (strcmp(curdir, objdir) && *fname != '/') { 1013 size_t plen = strlen(curdir) + strlen(fname) + 2; 1014 if (len < plen) 1015 path = erealloc(path, len = 2 * plen); 1016 1017 (void)snprintf(path, len, "%s/%s", curdir, fname); 1018 if ((stream = fopen(path, "r")) != NULL) { 1019 fname = path; 1020 goto found; 1021 } 1022 } else if ((stream = fopen(fname, "r")) != NULL) 1023 goto found; 1024 /* look in -I and system include directories. */ 1025 name = Dir_FindFile(fname, parseIncPath); 1026 if (!name) 1027 name = Dir_FindFile(fname, 1028 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath); 1029 if (!name || !(stream = fopen(name, "r"))) { 1030 free(path); 1031 return(FALSE); 1032 } 1033 fname = name; 1034 /* 1035 * set the MAKEFILE variable desired by System V fans -- the 1036 * placement of the setting here means it gets set to the last 1037 * makefile specified, as it is set by SysV make. 1038 */ 1039 found: 1040 if (setMAKEFILE) 1041 Var_Set("MAKEFILE", fname, VAR_GLOBAL, 0); 1042 Parse_File(fname, stream); 1043 (void)fclose(stream); 1044 } 1045 free(path); 1046 return(TRUE); 1047 } 1048 1049 1050 /* 1051 * If MAKEOBJDIRPREFIX is in use, make ends up not in .CURDIR 1052 * in situations that would not arrise with ./obj (links or not). 1053 * This tends to break things like: 1054 * 1055 * build: 1056 * ${MAKE} includes 1057 * 1058 * This function spots when ${.MAKE:T} or ${.MAKE} is a command (as 1059 * opposed to an argument) in a command line and if so returns 1060 * ${.CURDIR} so caller can chdir() so that the assumptions made by 1061 * the Makefile hold true. 1062 * 1063 * If ${.MAKE} does not contain any '/', then ${.MAKE:T} is skipped. 1064 * 1065 * The chdir() only happens in the child process, and does nothing if 1066 * MAKEOBJDIRPREFIX and MAKEOBJDIR are not in the environment so it 1067 * should not break anything. Also if NOCHECKMAKECHDIR is set we 1068 * do nothing - to ensure historic semantics can be retained. 1069 */ 1070 static int Check_Cwd_Off = 0; 1071 1072 static char * 1073 Check_Cwd_av(int ac, char **av, int copy) 1074 { 1075 static char *make[4]; 1076 static char *cur_dir = NULL; 1077 char **mp; 1078 char *cp; 1079 int is_cmd, next_cmd; 1080 int i; 1081 int n; 1082 1083 if (Check_Cwd_Off) 1084 return NULL; 1085 1086 if (make[0] == NULL) { 1087 if (Var_Exists("NOCHECKMAKECHDIR", VAR_GLOBAL)) { 1088 Check_Cwd_Off = 1; 1089 return NULL; 1090 } 1091 1092 make[1] = Var_Value(".MAKE", VAR_GLOBAL, &cp); 1093 if ((make[0] = strrchr(make[1], '/')) == NULL) { 1094 make[0] = make[1]; 1095 make[1] = NULL; 1096 } else 1097 ++make[0]; 1098 make[2] = NULL; 1099 cur_dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp); 1100 } 1101 if (ac == 0 || av == NULL) 1102 return NULL; /* initialization only */ 1103 1104 if (getenv("MAKEOBJDIR") == NULL && 1105 getenv("MAKEOBJDIRPREFIX") == NULL) 1106 return NULL; 1107 1108 1109 next_cmd = 1; 1110 for (i = 0; i < ac; ++i) { 1111 is_cmd = next_cmd; 1112 1113 n = strlen(av[i]); 1114 cp = &(av[i])[n - 1]; 1115 if (strspn(av[i], "|&;") == n) { 1116 next_cmd = 1; 1117 continue; 1118 } else if (*cp == ';' || *cp == '&' || *cp == '|' || *cp == ')') { 1119 next_cmd = 1; 1120 if (copy) { 1121 do { 1122 *cp-- = '\0'; 1123 } while (*cp == ';' || *cp == '&' || *cp == '|' || 1124 *cp == ')' || *cp == '}') ; 1125 } else { 1126 /* 1127 * XXX this should not happen. 1128 */ 1129 fprintf(stderr, "WARNING: raw arg ends in shell meta '%s'\n", 1130 av[i]); 1131 } 1132 } else 1133 next_cmd = 0; 1134 1135 cp = av[i]; 1136 if (*cp == ';' || *cp == '&' || *cp == '|') 1137 is_cmd = 1; 1138 1139 #ifdef check_cwd_debug 1140 fprintf(stderr, "av[%d] == %s '%s'", 1141 i, (is_cmd) ? "cmd" : "arg", av[i]); 1142 #endif 1143 if (is_cmd != 0) { 1144 if (*cp == '(' || *cp == '{' || 1145 *cp == ';' || *cp == '&' || *cp == '|') { 1146 do { 1147 ++cp; 1148 } while (*cp == '(' || *cp == '{' || 1149 *cp == ';' || *cp == '&' || *cp == '|'); 1150 if (*cp == '\0') { 1151 next_cmd = 1; 1152 continue; 1153 } 1154 } 1155 if (strcmp(cp, "cd") == 0 || strcmp(cp, "chdir") == 0) { 1156 #ifdef check_cwd_debug 1157 fprintf(stderr, " == cd, done.\n"); 1158 #endif 1159 return NULL; 1160 } 1161 for (mp = make; *mp != NULL; ++mp) { 1162 n = strlen(*mp); 1163 if (strcmp(cp, *mp) == 0) { 1164 #ifdef check_cwd_debug 1165 fprintf(stderr, " %s == '%s', chdir(%s)\n", 1166 cp, *mp, cur_dir); 1167 #endif 1168 return cur_dir; 1169 } 1170 } 1171 } 1172 #ifdef check_cwd_debug 1173 fprintf(stderr, "\n"); 1174 #endif 1175 } 1176 return NULL; 1177 } 1178 1179 char * 1180 Check_Cwd_Cmd(const char *cmd) 1181 { 1182 char *cp, *bp; 1183 char **av; 1184 int ac; 1185 1186 if (Check_Cwd_Off) 1187 return NULL; 1188 1189 if (cmd) { 1190 av = brk_string(cmd, &ac, TRUE, &bp); 1191 #ifdef check_cwd_debug 1192 fprintf(stderr, "splitting: '%s' -> %d words\n", 1193 cmd, ac); 1194 #endif 1195 } else { 1196 ac = 0; 1197 av = NULL; 1198 bp = NULL; 1199 } 1200 cp = Check_Cwd_av(ac, av, 1); 1201 if (bp) { 1202 free(av); 1203 free(bp); 1204 } 1205 return cp; 1206 } 1207 1208 void 1209 Check_Cwd(const char **argv) 1210 { 1211 char *cp; 1212 int ac; 1213 1214 if (Check_Cwd_Off) 1215 return; 1216 1217 for (ac = 0; argv[ac] != NULL; ++ac) 1218 /* NOTHING */; 1219 if (ac == 3 && *argv[1] == '-') { 1220 cp = Check_Cwd_Cmd(argv[2]); 1221 } else { 1222 cp = Check_Cwd_av(ac, UNCONST(argv), 0); 1223 } 1224 if (cp) { 1225 chdir(cp); 1226 } 1227 } 1228 1229 /*- 1230 * Cmd_Exec -- 1231 * Execute the command in cmd, and return the output of that command 1232 * in a string. 1233 * 1234 * Results: 1235 * A string containing the output of the command, or the empty string 1236 * If err is not NULL, it contains the reason for the command failure 1237 * 1238 * Side Effects: 1239 * The string must be freed by the caller. 1240 */ 1241 char * 1242 Cmd_Exec(const char *cmd, const char **err) 1243 { 1244 const char *args[4]; /* Args for invoking the shell */ 1245 int fds[2]; /* Pipe streams */ 1246 int cpid; /* Child PID */ 1247 int pid; /* PID from wait() */ 1248 char *res; /* result */ 1249 int status; /* command exit status */ 1250 Buffer buf; /* buffer to store the result */ 1251 char *cp; 1252 int cc; 1253 1254 1255 *err = NULL; 1256 1257 if (!shellName) 1258 Shell_Init(); 1259 /* 1260 * Set up arguments for shell 1261 */ 1262 args[0] = shellName; 1263 args[1] = "-c"; 1264 args[2] = cmd; 1265 args[3] = NULL; 1266 1267 /* 1268 * Open a pipe for fetching its output 1269 */ 1270 if (pipe(fds) == -1) { 1271 *err = "Couldn't create pipe for \"%s\""; 1272 goto bad; 1273 } 1274 1275 /* 1276 * Fork 1277 */ 1278 switch (cpid = vfork()) { 1279 case 0: 1280 /* 1281 * Close input side of pipe 1282 */ 1283 (void) close(fds[0]); 1284 1285 /* 1286 * Duplicate the output stream to the shell's output, then 1287 * shut the extra thing down. Note we don't fetch the error 1288 * stream...why not? Why? 1289 */ 1290 (void) dup2(fds[1], 1); 1291 (void) close(fds[1]); 1292 1293 (void) execv(shellPath, UNCONST(args)); 1294 _exit(1); 1295 /*NOTREACHED*/ 1296 1297 case -1: 1298 *err = "Couldn't exec \"%s\""; 1299 goto bad; 1300 1301 default: 1302 /* 1303 * No need for the writing half 1304 */ 1305 (void) close(fds[1]); 1306 1307 buf = Buf_Init (MAKE_BSIZE); 1308 1309 do { 1310 char result[BUFSIZ]; 1311 cc = read(fds[0], result, sizeof(result)); 1312 if (cc > 0) 1313 Buf_AddBytes(buf, cc, (Byte *) result); 1314 } 1315 while (cc > 0 || (cc == -1 && errno == EINTR)); 1316 1317 /* 1318 * Close the input side of the pipe. 1319 */ 1320 (void) close(fds[0]); 1321 1322 /* 1323 * Wait for the process to exit. 1324 */ 1325 while(((pid = wait(&status)) != cpid) && (pid >= 0)) 1326 continue; 1327 1328 res = (char *)Buf_GetAll (buf, &cc); 1329 Buf_Destroy (buf, FALSE); 1330 1331 if (cc == 0) 1332 *err = "Couldn't read shell's output for \"%s\""; 1333 1334 if (status) 1335 *err = "\"%s\" returned non-zero status"; 1336 1337 /* 1338 * Null-terminate the result, convert newlines to spaces and 1339 * install it in the variable. 1340 */ 1341 res[cc] = '\0'; 1342 cp = &res[cc]; 1343 1344 if (cc > 0 && *--cp == '\n') { 1345 /* 1346 * A final newline is just stripped 1347 */ 1348 *cp-- = '\0'; 1349 } 1350 while (cp >= res) { 1351 if (*cp == '\n') { 1352 *cp = ' '; 1353 } 1354 cp--; 1355 } 1356 break; 1357 } 1358 return res; 1359 bad: 1360 res = emalloc(1); 1361 *res = '\0'; 1362 return res; 1363 } 1364 1365 /*- 1366 * Error -- 1367 * Print an error message given its format. 1368 * 1369 * Results: 1370 * None. 1371 * 1372 * Side Effects: 1373 * The message is printed. 1374 */ 1375 /* VARARGS */ 1376 void 1377 Error(const char *fmt, ...) 1378 { 1379 va_list ap; 1380 1381 va_start(ap, fmt); 1382 fprintf(stderr, "%s: ", progname); 1383 (void)vfprintf(stderr, fmt, ap); 1384 va_end(ap); 1385 (void)fprintf(stderr, "\n"); 1386 (void)fflush(stderr); 1387 } 1388 1389 /*- 1390 * Fatal -- 1391 * Produce a Fatal error message. If jobs are running, waits for them 1392 * to finish. 1393 * 1394 * Results: 1395 * None 1396 * 1397 * Side Effects: 1398 * The program exits 1399 */ 1400 /* VARARGS */ 1401 void 1402 Fatal(const char *fmt, ...) 1403 { 1404 va_list ap; 1405 1406 va_start(ap, fmt); 1407 if (jobsRunning) 1408 Job_Wait(); 1409 Job_TokenFlush(); 1410 1411 (void)vfprintf(stderr, fmt, ap); 1412 va_end(ap); 1413 (void)fprintf(stderr, "\n"); 1414 (void)fflush(stderr); 1415 1416 PrintOnError(NULL); 1417 1418 if (DEBUG(GRAPH2)) 1419 Targ_PrintGraph(2); 1420 Trace_Log(MAKEERROR, 0); 1421 exit(2); /* Not 1 so -q can distinguish error */ 1422 } 1423 1424 /* 1425 * Punt -- 1426 * Major exception once jobs are being created. Kills all jobs, prints 1427 * a message and exits. 1428 * 1429 * Results: 1430 * None 1431 * 1432 * Side Effects: 1433 * All children are killed indiscriminately and the program Lib_Exits 1434 */ 1435 /* VARARGS */ 1436 void 1437 Punt(const char *fmt, ...) 1438 { 1439 va_list ap; 1440 1441 va_start(ap, fmt); 1442 (void)fprintf(stderr, "%s: ", progname); 1443 (void)vfprintf(stderr, fmt, ap); 1444 va_end(ap); 1445 (void)fprintf(stderr, "\n"); 1446 (void)fflush(stderr); 1447 1448 PrintOnError(NULL); 1449 1450 DieHorribly(); 1451 } 1452 1453 /*- 1454 * DieHorribly -- 1455 * Exit without giving a message. 1456 * 1457 * Results: 1458 * None 1459 * 1460 * Side Effects: 1461 * A big one... 1462 */ 1463 void 1464 DieHorribly(void) 1465 { 1466 if (jobsRunning) 1467 Job_AbortAll(); 1468 if (DEBUG(GRAPH2)) 1469 Targ_PrintGraph(2); 1470 Trace_Log(MAKEERROR, 0); 1471 exit(2); /* Not 1, so -q can distinguish error */ 1472 } 1473 1474 /* 1475 * Finish -- 1476 * Called when aborting due to errors in child shell to signal 1477 * abnormal exit. 1478 * 1479 * Results: 1480 * None 1481 * 1482 * Side Effects: 1483 * The program exits 1484 */ 1485 void 1486 Finish(int errors) 1487 /* number of errors encountered in Make_Make */ 1488 { 1489 Fatal("%d error%s", errors, errors == 1 ? "" : "s"); 1490 } 1491 1492 /* 1493 * emalloc -- 1494 * malloc, but die on error. 1495 */ 1496 void * 1497 emalloc(size_t len) 1498 { 1499 void *p; 1500 1501 if ((p = malloc(len)) == NULL) 1502 enomem(); 1503 return(p); 1504 } 1505 1506 /* 1507 * estrdup -- 1508 * strdup, but die on error. 1509 */ 1510 char * 1511 estrdup(const char *str) 1512 { 1513 char *p; 1514 1515 if ((p = strdup(str)) == NULL) 1516 enomem(); 1517 return(p); 1518 } 1519 1520 /* 1521 * erealloc -- 1522 * realloc, but die on error. 1523 */ 1524 void * 1525 erealloc(void *ptr, size_t size) 1526 { 1527 if ((ptr = realloc(ptr, size)) == NULL) 1528 enomem(); 1529 return(ptr); 1530 } 1531 1532 /* 1533 * enomem -- 1534 * die when out of memory. 1535 */ 1536 void 1537 enomem(void) 1538 { 1539 (void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno)); 1540 exit(2); 1541 } 1542 1543 /* 1544 * enunlink -- 1545 * Remove a file carefully, avoiding directories. 1546 */ 1547 int 1548 eunlink(const char *file) 1549 { 1550 struct stat st; 1551 1552 if (lstat(file, &st) == -1) 1553 return -1; 1554 1555 if (S_ISDIR(st.st_mode)) { 1556 errno = EISDIR; 1557 return -1; 1558 } 1559 return unlink(file); 1560 } 1561 1562 /* 1563 * execError -- 1564 * Print why exec failed, avoiding stdio. 1565 */ 1566 void 1567 execError(const char *af, const char *av) 1568 { 1569 #ifdef USE_IOVEC 1570 int i = 0; 1571 struct iovec iov[8]; 1572 #define IOADD(s) \ 1573 (void)(iov[i].iov_base = UNCONST(s), \ 1574 iov[i].iov_len = strlen(iov[i].iov_base), \ 1575 i++) 1576 #else 1577 #define IOADD (void)write(2, s, strlen(s)) 1578 #endif 1579 1580 IOADD(progname); 1581 IOADD(": "); 1582 IOADD(af); 1583 IOADD("("); 1584 IOADD(av); 1585 IOADD(") failed ("); 1586 IOADD(strerror(errno)); 1587 IOADD(")\n"); 1588 1589 #ifdef USE_IOVEC 1590 (void)writev(2, iov, 8); 1591 #endif 1592 } 1593 1594 /* 1595 * usage -- 1596 * exit with usage message 1597 */ 1598 static void 1599 usage(void) 1600 { 1601 (void)fprintf(stderr, 1602 "Usage: %s [-Beiknqrst] [-D variable] [-d flags] [-f makefile]\n\ 1603 [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\ 1604 [variable=value] [target ...]\n", progname); 1605 exit(2); 1606 } 1607 1608 1609 int 1610 PrintAddr(ClientData a, ClientData b) 1611 { 1612 printf("%lx ", (unsigned long) a); 1613 return b ? 0 : 0; 1614 } 1615 1616 1617 1618 void 1619 PrintOnError(const char *s) 1620 { 1621 char tmp[64]; 1622 1623 if (s) 1624 printf("%s", s); 1625 1626 printf("\n%s: stopped in %s\n", progname, curdir); 1627 strncpy(tmp, "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}", 1628 sizeof(tmp) - 1); 1629 s = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 1630 if (s && *s) 1631 printf("%s", s); 1632 } 1633 1634 void 1635 Main_ExportMAKEFLAGS(Boolean first) 1636 { 1637 static int once = 1; 1638 char tmp[64]; 1639 char *s; 1640 1641 if (once != first) 1642 return; 1643 once = 0; 1644 1645 strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}", 1646 sizeof(tmp)); 1647 s = Var_Subst(NULL, tmp, VAR_CMD, 0); 1648 if (s && *s) { 1649 #ifdef POSIX 1650 setenv("MAKEFLAGS", s, 1); 1651 #else 1652 setenv("MAKE", s, 1); 1653 #endif 1654 } 1655 } 1656