main.c revision 146056
1/*- 2 * Copyright (c) 1988, 1989, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1989 by Berkeley Softworks 5 * 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)main.c 8.3 (Berkeley) 3/19/94 39 */ 40 41#ifndef lint 42#if 0 43static char copyright[] = 44"@(#) Copyright (c) 1988, 1989, 1990, 1993\n\ 45 The Regents of the University of California. All rights reserved.\n"; 46#endif 47#endif /* not lint */ 48#include <sys/cdefs.h> 49__FBSDID("$FreeBSD: head/usr.bin/make/main.c 146056 2005-05-10 13:45:29Z harti $"); 50 51/* 52 * main.c 53 * The main file for this entire program. Exit routines etc 54 * reside here. 55 * 56 * Utility functions defined in this file: 57 * Main_ParseArgLine 58 * Takes a line of arguments, breaks them and 59 * treats them as if they were given when first 60 * invoked. Used by the parse module to implement 61 * the .MFLAGS target. 62 */ 63 64#ifndef MACHINE 65#include <sys/utsname.h> 66#endif 67#include <sys/param.h> 68#include <sys/stat.h> 69#include <sys/sysctl.h> 70#include <sys/time.h> 71#include <sys/queue.h> 72#include <sys/resource.h> 73#include <sys/wait.h> 74#include <err.h> 75#include <errno.h> 76#include <signal.h> 77#include <stdlib.h> 78#include <string.h> 79#include <unistd.h> 80 81#include "arch.h" 82#include "buf.h" 83#include "config.h" 84#include "dir.h" 85#include "globals.h" 86#include "job.h" 87#include "make.h" 88#include "nonints.h" 89#include "parse.h" 90#include "pathnames.h" 91#include "str.h" 92#include "suff.h" 93#include "targ.h" 94#include "util.h" 95#include "var.h" 96 97#define WANT_ENV_MKLVL 1 98#define MKLVL_MAXVAL 500 99#define MKLVL_ENVVAR "__MKLVL__" 100 101#define MAKEFLAGS ".MAKEFLAGS" 102 103extern char **environ; /* XXX what header declares this variable? */ 104 105/* Targets to be made */ 106Lst create = Lst_Initializer(create); 107 108time_t now; /* Time at start of make */ 109struct GNode *DEFAULT; /* .DEFAULT node */ 110Boolean allPrecious; /* .PRECIOUS given on line by itself */ 111uint32_t warn_flags; /* actual warning flags */ 112uint32_t warn_cmd; /* command line warning flags */ 113uint32_t warn_nocmd; /* command line no-warning flags */ 114 115static Boolean noBuiltins; /* -r flag */ 116 117/* ordered list of makefiles to read */ 118static Lst makefiles = Lst_Initializer(makefiles); 119 120static Boolean expandVars; /* fully expand printed variables */ 121 122/* list of variables to print */ 123static Lst variables = Lst_Initializer(variables); 124 125int maxJobs; /* -j argument */ 126static Boolean forceJobs; /* -j argument given */ 127Boolean compatMake; /* -B argument */ 128Boolean debug; /* -d flag */ 129Boolean noExecute; /* -n flag */ 130Boolean keepgoing; /* -k flag */ 131Boolean queryFlag; /* -q flag */ 132Boolean touchFlag; /* -t flag */ 133Boolean usePipes; /* !-P flag */ 134Boolean ignoreErrors; /* -i flag */ 135Boolean beSilent; /* -s flag */ 136Boolean beVerbose; /* -v flag */ 137Boolean oldVars; /* variable substitution style */ 138Boolean checkEnvFirst; /* -e flag */ 139 140/* (-E) vars to override from env */ 141Lst envFirstVars = Lst_Initializer(envFirstVars); 142 143Boolean jobsRunning; /* TRUE if the jobs might be running */ 144 145char *chdir_verify_path(const char *, char *); 146static int ReadMakefile(const char *); 147static void usage(void); 148 149static char *curdir; /* startup directory */ 150static char *objdir; /* where we chdir'ed to */ 151 152/** 153 * MFLAGS_append 154 * Append a flag with an optional argument to MAKEFLAGS and MFLAGS 155 */ 156static void 157MFLAGS_append(const char *flag, char *arg) 158{ 159 char *str; 160 161 Var_Append(MAKEFLAGS, flag, VAR_GLOBAL); 162 if (arg != NULL) { 163 str = MAKEFLAGS_quote(arg); 164 Var_Append(MAKEFLAGS, str, VAR_GLOBAL); 165 free(str); 166 } 167 168 Var_Append("MFLAGS", flag, VAR_GLOBAL); 169 if (arg != NULL) { 170 str = MAKEFLAGS_quote(arg); 171 Var_Append("MFLAGS", str, VAR_GLOBAL); 172 free(str); 173 } 174} 175 176/** 177 * Main_ParseWarn 178 * 179 * Handle argument to warning option. 180 */ 181int 182Main_ParseWarn(const char *arg, int iscmd) 183{ 184 int i, neg; 185 186 static const struct { 187 const char *option; 188 uint32_t flag; 189 } options[] = { 190 { "dirsyntax", WARN_DIRSYNTAX }, 191 { NULL, 0 } 192 }; 193 194 neg = 0; 195 if (arg[0] == 'n' && arg[1] == 'o') { 196 neg = 1; 197 arg += 2; 198 } 199 200 for (i = 0; options[i].option != NULL; i++) 201 if (strcmp(arg, options[i].option) == 0) 202 break; 203 204 if (options[i].option == NULL) 205 /* unknown option */ 206 return (-1); 207 208 if (iscmd) { 209 if (!neg) { 210 warn_cmd |= options[i].flag; 211 warn_nocmd &= ~options[i].flag; 212 warn_flags |= options[i].flag; 213 } else { 214 warn_nocmd |= options[i].flag; 215 warn_cmd &= ~options[i].flag; 216 warn_flags &= ~options[i].flag; 217 } 218 } else { 219 if (!neg) { 220 warn_flags |= (options[i].flag & ~warn_nocmd); 221 } else { 222 warn_flags &= ~(options[i].flag | warn_cmd); 223 } 224 } 225 return (0); 226} 227 228/** 229 * MainParseArgs 230 * Parse a given argument vector. Called from main() and from 231 * Main_ParseArgLine() when the .MAKEFLAGS target is used. 232 * 233 * XXX: Deal with command line overriding .MAKEFLAGS in makefile 234 * 235 * Side Effects: 236 * Various global and local flags will be set depending on the flags 237 * given 238 */ 239static void 240MainParseArgs(int argc, char **argv) 241{ 242 int c; 243 Boolean found_dd = FALSE; 244 245rearg: 246 optind = 1; /* since we're called more than once */ 247 optreset = 1; 248#define OPTFLAGS "ABC:D:E:I:PSV:Xd:ef:ij:km:nqrstvx:" 249 for (;;) { 250 if ((optind < argc) && strcmp(argv[optind], "--") == 0) { 251 found_dd = TRUE; 252 } 253 if ((c = getopt(argc, argv, OPTFLAGS)) == -1) { 254 break; 255 } 256 switch(c) { 257 258 case 'A': 259 arch_fatal = FALSE; 260 MFLAGS_append("-A", NULL); 261 break; 262 case 'C': 263 if (chdir(optarg) == -1) 264 err(1, "chdir %s", optarg); 265 break; 266 case 'D': 267 Var_Set(optarg, "1", VAR_GLOBAL); 268 MFLAGS_append("-D", optarg); 269 break; 270 case 'I': 271 Parse_AddIncludeDir(optarg); 272 MFLAGS_append("-I", optarg); 273 break; 274 case 'V': 275 Lst_AtEnd(&variables, estrdup(optarg)); 276 MFLAGS_append("-V", optarg); 277 break; 278 case 'X': 279 expandVars = FALSE; 280 break; 281 case 'B': 282 compatMake = TRUE; 283 MFLAGS_append("-B", NULL); 284 unsetenv("MAKE_JOBS_FIFO"); 285 break; 286 case 'P': 287 usePipes = FALSE; 288 MFLAGS_append("-P", NULL); 289 break; 290 case 'S': 291 keepgoing = FALSE; 292 MFLAGS_append("-S", NULL); 293 break; 294 case 'd': { 295 char *modules = optarg; 296 297 for (; *modules; ++modules) 298 switch (*modules) { 299 case 'A': 300 debug = ~0; 301 break; 302 case 'a': 303 debug |= DEBUG_ARCH; 304 break; 305 case 'c': 306 debug |= DEBUG_COND; 307 break; 308 case 'd': 309 debug |= DEBUG_DIR; 310 break; 311 case 'f': 312 debug |= DEBUG_FOR; 313 break; 314 case 'g': 315 if (modules[1] == '1') { 316 debug |= DEBUG_GRAPH1; 317 ++modules; 318 } 319 else if (modules[1] == '2') { 320 debug |= DEBUG_GRAPH2; 321 ++modules; 322 } 323 break; 324 case 'j': 325 debug |= DEBUG_JOB; 326 break; 327 case 'l': 328 debug |= DEBUG_LOUD; 329 break; 330 case 'm': 331 debug |= DEBUG_MAKE; 332 break; 333 case 's': 334 debug |= DEBUG_SUFF; 335 break; 336 case 't': 337 debug |= DEBUG_TARG; 338 break; 339 case 'v': 340 debug |= DEBUG_VAR; 341 break; 342 default: 343 warnx("illegal argument to d option " 344 "-- %c", *modules); 345 usage(); 346 } 347 MFLAGS_append("-d", optarg); 348 break; 349 } 350 case 'E': 351 Lst_AtEnd(&envFirstVars, estrdup(optarg)); 352 MFLAGS_append("-E", optarg); 353 break; 354 case 'e': 355 checkEnvFirst = TRUE; 356 MFLAGS_append("-e", NULL); 357 break; 358 case 'f': 359 Lst_AtEnd(&makefiles, estrdup(optarg)); 360 break; 361 case 'i': 362 ignoreErrors = TRUE; 363 MFLAGS_append("-i", NULL); 364 break; 365 case 'j': { 366 char *endptr; 367 368 forceJobs = TRUE; 369 maxJobs = strtol(optarg, &endptr, 10); 370 if (maxJobs <= 0 || *endptr != '\0') { 371 warnx("illegal number, -j argument -- %s", 372 optarg); 373 usage(); 374 } 375 MFLAGS_append("-j", optarg); 376 break; 377 } 378 case 'k': 379 keepgoing = TRUE; 380 MFLAGS_append("-k", NULL); 381 break; 382 case 'm': 383 Path_AddDir(&sysIncPath, optarg); 384 MFLAGS_append("-m", optarg); 385 break; 386 case 'n': 387 noExecute = TRUE; 388 MFLAGS_append("-n", NULL); 389 break; 390 case 'q': 391 queryFlag = TRUE; 392 /* Kind of nonsensical, wot? */ 393 MFLAGS_append("-q", NULL); 394 break; 395 case 'r': 396 noBuiltins = TRUE; 397 MFLAGS_append("-r", NULL); 398 break; 399 case 's': 400 beSilent = TRUE; 401 MFLAGS_append("-s", NULL); 402 break; 403 case 't': 404 touchFlag = TRUE; 405 MFLAGS_append("-t", NULL); 406 break; 407 case 'v': 408 beVerbose = TRUE; 409 MFLAGS_append("-v", NULL); 410 break; 411 case 'x': 412 if (Main_ParseWarn(optarg, 1) != -1) 413 MFLAGS_append("-x", optarg); 414 break; 415 416 default: 417 case '?': 418 usage(); 419 } 420 } 421 argv += optind; 422 argc -= optind; 423 424 oldVars = TRUE; 425 426 /* 427 * Parse the rest of the arguments. 428 * o Check for variable assignments and perform them if so. 429 * o Check for more flags and restart getopt if so. 430 * o Anything else is taken to be a target and added 431 * to the end of the "create" list. 432 */ 433 for (; *argv != NULL; ++argv, --argc) { 434 if (Parse_IsVar(*argv)) { 435 char *ptr = MAKEFLAGS_quote(*argv); 436 437 Var_Append(MAKEFLAGS, ptr, VAR_GLOBAL); 438 Parse_DoVar(*argv, VAR_CMD); 439 free(ptr); 440 441 } else if ((*argv)[0] == '-') { 442 if ((*argv)[1] == '\0') { 443 /* 444 * (*argv) is a single dash, so we 445 * just ignore it. 446 */ 447 } else if (found_dd) { 448 /* 449 * Double dash has been found, ignore 450 * any more options. But what do we do 451 * with it? For now treat it like a target. 452 */ 453 Lst_AtEnd(&create, estrdup(*argv)); 454 } else { 455 /* 456 * (*argv) is a -flag, so backup argv and 457 * argc. getopt() expects options to start 458 * in the 2nd position. 459 */ 460 argc++; 461 argv--; 462 goto rearg; 463 } 464 465 } else if ((*argv)[0] == '\0') { 466 Punt("illegal (null) argument."); 467 468 } else { 469 Lst_AtEnd(&create, estrdup(*argv)); 470 } 471 } 472} 473 474/** 475 * Main_ParseArgLine 476 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 477 * is encountered and by main() when reading the .MAKEFLAGS envariable. 478 * Takes a line of arguments and breaks it into its 479 * component words and passes those words and the number of them to the 480 * MainParseArgs function. 481 * The line should have all its leading whitespace removed. 482 * 483 * Side Effects: 484 * Only those that come from the various arguments. 485 */ 486void 487Main_ParseArgLine(char *line, int mflags) 488{ 489 char **argv; /* Manufactured argument vector */ 490 int argc; /* Number of arguments in argv */ 491 492 if (line == NULL) 493 return; 494 for (; *line == ' '; ++line) 495 continue; 496 if (!*line) 497 return; 498 499 if (mflags) 500 argv = MAKEFLAGS_break(line, &argc); 501 else 502 argv = brk_string(line, &argc, TRUE); 503 504 MainParseArgs(argc, argv); 505} 506 507char * 508chdir_verify_path(const char *path, char *obpath) 509{ 510 struct stat sb; 511 512 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 513 if (chdir(path) == -1 || getcwd(obpath, MAXPATHLEN) == NULL) { 514 warn("warning: %s", path); 515 return (NULL); 516 } 517 return (obpath); 518 } 519 520 return (NULL); 521} 522 523static void 524catch_child(int sig __unused) 525{ 526} 527 528/* 529 * In lieu of a good way to prevent every possible looping in 530 * make(1), stop there from being more than MKLVL_MAXVAL processes forked 531 * by make(1), to prevent a forkbomb from happening, in a dumb and 532 * mechanical way. 533 */ 534static void 535check_make_level(void) 536{ 537#ifdef WANT_ENV_MKLVL 538 char *value = getenv(MKLVL_ENVVAR); 539 int level = (value == NULL) ? 0 : atoi(value); 540 541 if (level < 0) { 542 errc(2, EAGAIN, "Invalid value for recursion level (%d).", 543 level); 544 } else if (level > MKLVL_MAXVAL) { 545 errc(2, EAGAIN, "Max recursion level (%d) exceeded.", 546 MKLVL_MAXVAL); 547 } else { 548 char new_value[32]; 549 sprintf(new_value, "%d", level + 1); 550 setenv(MKLVL_ENVVAR, new_value, 1); 551 } 552#endif /* WANT_ENV_MKLVL */ 553} 554 555/** 556 * main 557 * The main function, for obvious reasons. Initializes variables 558 * and a few modules, then parses the arguments give it in the 559 * environment and on the command line. Reads the system makefile 560 * followed by either Makefile, makefile or the file given by the 561 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 562 * flags it has received by then uses either the Make or the Compat 563 * module to create the initial list of targets. 564 * 565 * Results: 566 * If -q was given, exits -1 if anything was out-of-date. Else it exits 567 * 0. 568 * 569 * Side Effects: 570 * The program exits when done. Targets are created. etc. etc. etc. 571 */ 572int 573main(int argc, char **argv) 574{ 575 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */ 576 char *p, *p1, *pathp; 577 char *path; 578 char mdpath[MAXPATHLEN]; 579 char obpath[MAXPATHLEN]; 580 char cdpath[MAXPATHLEN]; 581 const char *machine = getenv("MACHINE"); 582 const char *machine_arch = getenv("MACHINE_ARCH"); 583 const char *machine_cpu = getenv("MACHINE_CPU"); 584 char *cp = NULL, *start; 585 586 /* avoid faults on read-only strings */ 587 static char syspath[] = PATH_DEFSYSPATH; 588 589 { 590 /* 591 * Catch SIGCHLD so that we get kicked out of select() when we 592 * need to look at a child. This is only known to matter for the 593 * -j case (perhaps without -P). 594 * 595 * XXX this is intentionally misplaced. 596 */ 597 struct sigaction sa; 598 599 sigemptyset(&sa.sa_mask); 600 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; 601 sa.sa_handler = catch_child; 602 sigaction(SIGCHLD, &sa, NULL); 603 } 604 605 check_make_level(); 606 607#if DEFSHELL == 2 608 /* 609 * Turn off ENV to make ksh happier. 610 */ 611 unsetenv("ENV"); 612#endif 613 614#ifdef RLIMIT_NOFILE 615 /* 616 * get rid of resource limit on file descriptors 617 */ 618 { 619 struct rlimit rl; 620 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && 621 rl.rlim_cur != rl.rlim_max) { 622 rl.rlim_cur = rl.rlim_max; 623 setrlimit(RLIMIT_NOFILE, &rl); 624 } 625 } 626#endif 627 628 /* 629 * PC-98 kernel sets the `i386' string to the utsname.machine and 630 * it cannot be distinguished from IBM-PC by uname(3). Therefore, 631 * we check machine.ispc98 and adjust the machine variable before 632 * using usname(3) below. 633 * NOTE: machdep.ispc98 was defined on 1998/8/31. At that time, 634 * __FreeBSD_version was defined as 300003. So, this check can 635 * safely be done with any kernel with version > 300003. 636 */ 637 if (!machine) { 638 int ispc98; 639 size_t len; 640 641 len = sizeof(ispc98); 642 if (!sysctlbyname("machdep.ispc98", &ispc98, &len, NULL, 0)) { 643 if (ispc98) 644 machine = "pc98"; 645 } 646 } 647 648 /* 649 * Get the name of this type of MACHINE from utsname 650 * so we can share an executable for similar machines. 651 * (i.e. m68k: amiga hp300, mac68k, sun3, ...) 652 * 653 * Note that while MACHINE is decided at run-time, 654 * MACHINE_ARCH is always known at compile time. 655 */ 656 if (!machine) { 657#ifndef MACHINE 658 static struct utsname utsname; 659 660 if (uname(&utsname) == -1) 661 err(2, "uname"); 662 machine = utsname.machine; 663#else 664 machine = MACHINE; 665#endif 666 } 667 668 if (!machine_arch) { 669#ifndef MACHINE_ARCH 670 machine_arch = "unknown"; 671#else 672 machine_arch = MACHINE_ARCH; 673#endif 674 } 675 676 /* 677 * Set machine_cpu to the minumum supported CPU revision based 678 * on the target architecture, if not already set. 679 */ 680 if (!machine_cpu) { 681 if (!strcmp(machine_arch, "i386")) 682 machine_cpu = "i386"; 683 else if (!strcmp(machine_arch, "alpha")) 684 machine_cpu = "ev4"; 685 else 686 machine_cpu = "unknown"; 687 } 688 689 expandVars = TRUE; 690 beSilent = FALSE; /* Print commands as executed */ 691 ignoreErrors = FALSE; /* Pay attention to non-zero returns */ 692 noExecute = FALSE; /* Execute all commands */ 693 keepgoing = FALSE; /* Stop on error */ 694 allPrecious = FALSE; /* Remove targets when interrupted */ 695 queryFlag = FALSE; /* This is not just a check-run */ 696 noBuiltins = FALSE; /* Read the built-in rules */ 697 touchFlag = FALSE; /* Actually update targets */ 698 usePipes = TRUE; /* Catch child output in pipes */ 699 debug = 0; /* No debug verbosity, please. */ 700 jobsRunning = FALSE; 701 702 maxJobs = DEFMAXJOBS; 703 forceJobs = FALSE; /* No -j flag */ 704 compatMake = FALSE; /* No compat mode */ 705 706 /* 707 * Initialize the parsing, directory and variable modules to prepare 708 * for the reading of inclusion paths and variable settings on the 709 * command line 710 */ 711 Dir_Init(); /* Initialize directory structures so -I flags 712 * can be processed correctly */ 713 Var_Init(environ); /* As well as the lists of variables for 714 * parsing arguments */ 715 str_init(); 716 717 /* 718 * Initialize various variables. 719 * MAKE also gets this name, for compatibility 720 * .MAKEFLAGS gets set to the empty string just in case. 721 * MFLAGS also gets initialized empty, for compatibility. 722 */ 723 Var_Set("MAKE", argv[0], VAR_GLOBAL); 724 Var_Set(MAKEFLAGS, "", VAR_GLOBAL); 725 Var_Set("MFLAGS", "", VAR_GLOBAL); 726 Var_Set("MACHINE", machine, VAR_GLOBAL); 727 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL); 728 Var_Set("MACHINE_CPU", machine_cpu, VAR_GLOBAL); 729#ifdef MAKE_VERSION 730 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL); 731#endif 732 733 /* 734 * First snag things out of the MAKEFLAGS environment 735 * variable. Then parse the command line arguments. 736 */ 737 Main_ParseArgLine(getenv("MAKEFLAGS"), 1); 738 739 MainParseArgs(argc, argv); 740 741 /* 742 * Find where we are... 743 */ 744 curdir = cdpath; 745 if (getcwd(curdir, MAXPATHLEN) == NULL) 746 err(2, NULL); 747 748 { 749 struct stat sa; 750 751 if (stat(curdir, &sa) == -1) 752 err(2, "%s", curdir); 753 } 754 755 /* 756 * The object directory location is determined using the 757 * following order of preference: 758 * 759 * 1. MAKEOBJDIRPREFIX`cwd` 760 * 2. MAKEOBJDIR 761 * 3. PATH_OBJDIR.${MACHINE} 762 * 4. PATH_OBJDIR 763 * 5. PATH_OBJDIRPREFIX`cwd` 764 * 765 * If one of the first two fails, use the current directory. 766 * If the remaining three all fail, use the current directory. 767 * 768 * Once things are initted, 769 * have to add the original directory to the search path, 770 * and modify the paths for the Makefiles apropriately. The 771 * current directory is also placed as a variable for make scripts. 772 */ 773 if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) { 774 if (!(path = getenv("MAKEOBJDIR"))) { 775 path = PATH_OBJDIR; 776 pathp = PATH_OBJDIRPREFIX; 777 snprintf(mdpath, MAXPATHLEN, "%s.%s", 778 path, machine); 779 if (!(objdir = chdir_verify_path(mdpath, obpath))) 780 if (!(objdir=chdir_verify_path(path, obpath))) { 781 snprintf(mdpath, MAXPATHLEN, 782 "%s%s", pathp, curdir); 783 if (!(objdir=chdir_verify_path(mdpath, 784 obpath))) 785 objdir = curdir; 786 } 787 } 788 else if (!(objdir = chdir_verify_path(path, obpath))) 789 objdir = curdir; 790 } 791 else { 792 snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir); 793 if (!(objdir = chdir_verify_path(mdpath, obpath))) 794 objdir = curdir; 795 } 796 Dir_InitDot(); /* Initialize the "." directory */ 797 if (objdir != curdir) 798 Path_AddDir(&dirSearchPath, curdir); 799 Var_Set(".ST_EXPORTVAR", "YES", VAR_GLOBAL); 800 Var_Set(".CURDIR", curdir, VAR_GLOBAL); 801 Var_Set(".OBJDIR", objdir, VAR_GLOBAL); 802 803 if (getenv("MAKE_JOBS_FIFO") != NULL) 804 forceJobs = TRUE; 805 /* 806 * Be compatible if user did not specify -j and did not explicitly 807 * turned compatibility on 808 */ 809 if (!compatMake && !forceJobs) 810 compatMake = TRUE; 811 812 /* 813 * Initialize target and suffix modules in preparation for 814 * parsing the makefile(s) 815 */ 816 Targ_Init(); 817 Suff_Init(); 818 819 DEFAULT = NULL; 820 time(&now); 821 822 /* 823 * Set up the .TARGETS variable to contain the list of targets to be 824 * created. If none specified, make the variable empty -- the parser 825 * will fill the thing in with the default or .MAIN target. 826 */ 827 if (!Lst_IsEmpty(&create)) { 828 LstNode *ln; 829 830 for (ln = Lst_First(&create); ln != NULL; ln = Lst_Succ(ln)) { 831 char *name = Lst_Datum(ln); 832 833 Var_Append(".TARGETS", name, VAR_GLOBAL); 834 } 835 } else 836 Var_Set(".TARGETS", "", VAR_GLOBAL); 837 838 839 /* 840 * If no user-supplied system path was given (through the -m option) 841 * add the directories from the DEFSYSPATH (more than one may be given 842 * as dir1:...:dirn) to the system include path. 843 */ 844 if (TAILQ_EMPTY(&sysIncPath)) { 845 for (start = syspath; *start != '\0'; start = cp) { 846 for (cp = start; *cp != '\0' && *cp != ':'; cp++) 847 continue; 848 if (*cp == '\0') { 849 Path_AddDir(&sysIncPath, start); 850 } else { 851 *cp++ = '\0'; 852 Path_AddDir(&sysIncPath, start); 853 } 854 } 855 } 856 857 /* 858 * Read in the built-in rules first, followed by the specified 859 * makefile, if it was (makefile != (char *) NULL), or the default 860 * Makefile and makefile, in that order, if it wasn't. 861 */ 862 if (!noBuiltins) { 863 /* Path of sys.mk */ 864 Lst sysMkPath = Lst_Initializer(sysMkPath); 865 LstNode *ln; 866 867 Path_Expand(PATH_DEFSYSMK, &sysIncPath, &sysMkPath); 868 if (Lst_IsEmpty(&sysMkPath)) 869 Fatal("make: no system rules (%s).", PATH_DEFSYSMK); 870 LST_FOREACH(ln, &sysMkPath) { 871 if (!ReadMakefile(Lst_Datum(ln))) 872 break; 873 } 874 if (ln != NULL) 875 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 876 Lst_Destroy(&sysMkPath, free); 877 } 878 879 if (!Lst_IsEmpty(&makefiles)) { 880 LstNode *ln; 881 882 LST_FOREACH(ln, &makefiles) { 883 if (!ReadMakefile(Lst_Datum(ln))) 884 break; 885 } 886 if (ln != NULL) 887 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 888 } else if (!ReadMakefile("BSDmakefile")) 889 if (!ReadMakefile("makefile")) 890 ReadMakefile("Makefile"); 891 892 ReadMakefile(".depend"); 893 894 /* Install all the flags into the MAKE envariable. */ 895 if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p) 896 setenv("MAKEFLAGS", p, 1); 897 free(p1); 898 899 /* 900 * For compatibility, look at the directories in the VPATH variable 901 * and add them to the search path, if the variable is defined. The 902 * variable's value is in the same format as the PATH envariable, i.e. 903 * <directory>:<directory>:<directory>... 904 */ 905 if (Var_Exists("VPATH", VAR_CMD)) { 906 /* 907 * GCC stores string constants in read-only memory, but 908 * Var_Subst will want to write this thing, so store it 909 * in an array 910 */ 911 static char VPATH[] = "${VPATH}"; 912 Buffer *buf; 913 char *vpath; 914 char *ptr; 915 char savec; 916 917 buf = Var_Subst(VPATH, VAR_CMD, FALSE); 918 919 vpath = Buf_Data(buf); 920 do { 921 /* skip to end of directory */ 922 for (ptr = vpath; *ptr != ':' && *ptr != '\0'; ptr++) 923 ; 924 925 /* Save terminator character so know when to stop */ 926 savec = *ptr; 927 *ptr = '\0'; 928 929 /* Add directory to search path */ 930 Path_AddDir(&dirSearchPath, vpath); 931 932 vpath = ptr + 1; 933 } while (savec != '\0'); 934 935 Buf_Destroy(buf, TRUE); 936 } 937 938 /* 939 * Now that all search paths have been read for suffixes et al, it's 940 * time to add the default search path to their lists... 941 */ 942 Suff_DoPaths(); 943 944 /* print the initial graph, if the user requested it */ 945 if (DEBUG(GRAPH1)) 946 Targ_PrintGraph(1); 947 948 /* print the values of any variables requested by the user */ 949 if (Lst_IsEmpty(&variables)) { 950 /* 951 * Since the user has not requested that any variables 952 * be printed, we can build targets. 953 * 954 * Have read the entire graph and need to make a list of targets 955 * to create. If none was given on the command line, we consult 956 * the parsing module to find the main target(s) to create. 957 */ 958 Lst targs = Lst_Initializer(targs); 959 960 if (Lst_IsEmpty(&create)) 961 Parse_MainName(&targs); 962 else 963 Targ_FindList(&targs, &create, TARG_CREATE); 964 965 if (compatMake) { 966 /* 967 * Compat_Init will take care of creating 968 * all the targets as well as initializing 969 * the module. 970 */ 971 Compat_Run(&targs); 972 outOfDate = 0; 973 } else { 974 /* 975 * Initialize job module before traversing 976 * the graph, now that any .BEGIN and .END 977 * targets have been read. This is done 978 * only if the -q flag wasn't given (to 979 * prevent the .BEGIN from being executed 980 * should it exist). 981 */ 982 if (!queryFlag) { 983 Job_Init(maxJobs); 984 jobsRunning = TRUE; 985 } 986 987 /* Traverse the graph, checking on all the targets */ 988 outOfDate = Make_Run(&targs); 989 } 990 Lst_Destroy(&targs, NOFREE); 991 992 } else { 993 /* 994 * Print the values of any variables requested by 995 * the user. 996 */ 997 LstNode *n; 998 const char *name; 999 char *v; 1000 char *value; 1001 1002 LST_FOREACH(n, &variables) { 1003 name = Lst_Datum(n); 1004 if (expandVars) { 1005 v = emalloc(strlen(name) + 1 + 3); 1006 sprintf(v, "${%s}", name); 1007 1008 value = Buf_Peel(Var_Subst(v, 1009 VAR_GLOBAL, FALSE)); 1010 printf("%s\n", value); 1011 1012 free(v); 1013 free(value); 1014 } else { 1015 value = Var_Value(name, VAR_GLOBAL, &v); 1016 printf("%s\n", value != NULL ? value : ""); 1017 if (v != NULL) 1018 free(v); 1019 } 1020 } 1021 } 1022 1023 Lst_Destroy(&variables, free); 1024 Lst_Destroy(&makefiles, free); 1025 Lst_Destroy(&create, free); 1026 1027 /* print the graph now it's been processed if the user requested it */ 1028 if (DEBUG(GRAPH2)) 1029 Targ_PrintGraph(2); 1030 1031 if (queryFlag && outOfDate) 1032 return (1); 1033 else 1034 return (0); 1035} 1036 1037/** 1038 * ReadMakefile 1039 * Open and parse the given makefile. 1040 * 1041 * Results: 1042 * TRUE if ok. FALSE if couldn't open file. 1043 * 1044 * Side Effects: 1045 * lots 1046 */ 1047static Boolean 1048ReadMakefile(const char *p) 1049{ 1050 char *fname; /* makefile to read */ 1051 FILE *stream; 1052 char *name, path[MAXPATHLEN]; 1053 char *MAKEFILE; 1054 int setMAKEFILE; 1055 1056 /* XXX - remove this once constification is done */ 1057 fname = estrdup(p); 1058 1059 if (!strcmp(fname, "-")) { 1060 Parse_File("(stdin)", stdin); 1061 Var_Set("MAKEFILE", "", VAR_GLOBAL); 1062 } else { 1063 setMAKEFILE = strcmp(fname, ".depend"); 1064 1065 /* if we've chdir'd, rebuild the path name */ 1066 if (curdir != objdir && *fname != '/') { 1067 snprintf(path, MAXPATHLEN, "%s/%s", curdir, fname); 1068 /* 1069 * XXX The realpath stuff breaks relative includes 1070 * XXX in some cases. The problem likely is in 1071 * XXX parse.c where it does special things in 1072 * XXX ParseDoInclude if the file is relateive 1073 * XXX or absolute and not a system file. There 1074 * XXX it assumes that if the current file that's 1075 * XXX being included is absolute, that any files 1076 * XXX that it includes shouldn't do the -I path 1077 * XXX stuff, which is inconsistant with historical 1078 * XXX behavior. However, I can't pentrate the mists 1079 * XXX further, so I'm putting this workaround in 1080 * XXX here until such time as the underlying bug 1081 * XXX can be fixed. 1082 */ 1083#if THIS_BREAKS_THINGS 1084 if (realpath(path, path) != NULL && 1085 (stream = fopen(path, "r")) != NULL) { 1086 MAKEFILE = fname; 1087 fname = path; 1088 goto found; 1089 } 1090 } else if (realpath(fname, path) != NULL) { 1091 MAKEFILE = fname; 1092 fname = path; 1093 if ((stream = fopen(fname, "r")) != NULL) 1094 goto found; 1095 } 1096#else 1097 if ((stream = fopen(path, "r")) != NULL) { 1098 MAKEFILE = fname; 1099 fname = path; 1100 goto found; 1101 } 1102 } else { 1103 MAKEFILE = fname; 1104 if ((stream = fopen(fname, "r")) != NULL) 1105 goto found; 1106 } 1107#endif 1108 /* look in -I and system include directories. */ 1109 name = Path_FindFile(fname, &parseIncPath); 1110 if (!name) 1111 name = Path_FindFile(fname, &sysIncPath); 1112 if (!name || !(stream = fopen(name, "r"))) 1113 return (FALSE); 1114 MAKEFILE = fname = name; 1115 /* 1116 * set the MAKEFILE variable desired by System V fans -- the 1117 * placement of the setting here means it gets set to the last 1118 * makefile specified, as it is set by SysV make. 1119 */ 1120found: 1121 if (setMAKEFILE) 1122 Var_Set("MAKEFILE", MAKEFILE, VAR_GLOBAL); 1123 Parse_File(fname, stream); 1124 fclose(stream); 1125 } 1126 return (TRUE); 1127} 1128 1129/* 1130 * usage -- 1131 * exit with usage message 1132 */ 1133static void 1134usage(void) 1135{ 1136 fprintf(stderr, "%s\n%s\n%s\n%s\n", 1137"usage: make [-ABPSXeiknqrstv] [-C directory] [-D variable] [-d flags]", 1138" [-E variable] [-f makefile] [-I directory] [-j max_jobs]", 1139" [-m directory] [-V variable] [variable=value] [-x warn_flag]", 1140" [target ...]"); 1141 exit(2); 1142} 1143