182static void MainParseArgs(int, char **); 183static int ReadMakefile(const void *, const void *); 184static void usage(void) MAKE_ATTR_DEAD; 185 186static Boolean ignorePWD; /* if we use -C, PWD is meaningless */ 187static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */ 188char curdir[MAXPATHLEN + 1]; /* Startup directory */ 189char *progname; /* the program name */ 190char *makeDependfile; 191pid_t myPid; 192 193Boolean forceJobs = FALSE; 194 195/* 196 * On some systems MACHINE is defined as something other than 197 * what we want. 198 */ 199#ifdef FORCE_MACHINE 200# undef MACHINE 201# define MACHINE FORCE_MACHINE 202#endif 203 204extern Lst parseIncPath; 205 206static void 207parse_debug_options(const char *argvalue) 208{ 209 const char *modules; 210 const char *mode; 211 char *fname; 212 int len; 213 214 for (modules = argvalue; *modules; ++modules) { 215 switch (*modules) { 216 case 'A': 217 debug = ~0; 218 break; 219 case 'a': 220 debug |= DEBUG_ARCH; 221 break; 222 case 'C': 223 debug |= DEBUG_CWD; 224 break; 225 case 'c': 226 debug |= DEBUG_COND; 227 break; 228 case 'd': 229 debug |= DEBUG_DIR; 230 break; 231 case 'e': 232 debug |= DEBUG_ERROR; 233 break; 234 case 'f': 235 debug |= DEBUG_FOR; 236 break; 237 case 'g': 238 if (modules[1] == '1') { 239 debug |= DEBUG_GRAPH1; 240 ++modules; 241 } 242 else if (modules[1] == '2') { 243 debug |= DEBUG_GRAPH2; 244 ++modules; 245 } 246 else if (modules[1] == '3') { 247 debug |= DEBUG_GRAPH3; 248 ++modules; 249 } 250 break; 251 case 'j': 252 debug |= DEBUG_JOB; 253 break; 254 case 'l': 255 debug |= DEBUG_LOUD; 256 break; 257 case 'M': 258 debug |= DEBUG_META; 259 break; 260 case 'm': 261 debug |= DEBUG_MAKE; 262 break; 263 case 'n': 264 debug |= DEBUG_SCRIPT; 265 break; 266 case 'p': 267 debug |= DEBUG_PARSE; 268 break; 269 case 's': 270 debug |= DEBUG_SUFF; 271 break; 272 case 't': 273 debug |= DEBUG_TARG; 274 break; 275 case 'V': 276 debugVflag = TRUE; 277 break; 278 case 'v': 279 debug |= DEBUG_VAR; 280 break; 281 case 'x': 282 debug |= DEBUG_SHELL; 283 break; 284 case 'F': 285 if (debug_file != stdout && debug_file != stderr) 286 fclose(debug_file); 287 if (*++modules == '+') { 288 modules++; 289 mode = "a"; 290 } else 291 mode = "w"; 292 if (strcmp(modules, "stdout") == 0) { 293 debug_file = stdout; 294 goto debug_setbuf; 295 } 296 if (strcmp(modules, "stderr") == 0) { 297 debug_file = stderr; 298 goto debug_setbuf; 299 } 300 len = strlen(modules); 301 fname = malloc(len + 20); 302 memcpy(fname, modules, len + 1); 303 /* Let the filename be modified by the pid */ 304 if (strcmp(fname + len - 3, ".%d") == 0) 305 snprintf(fname + len - 2, 20, "%d", getpid()); 306 debug_file = fopen(fname, mode); 307 if (!debug_file) { 308 fprintf(stderr, "Cannot open debug file %s\n", 309 fname); 310 usage(); 311 } 312 free(fname); 313 goto debug_setbuf; 314 default: 315 (void)fprintf(stderr, 316 "%s: illegal argument to d option -- %c\n", 317 progname, *modules); 318 usage(); 319 } 320 } 321debug_setbuf: 322 /* 323 * Make the debug_file unbuffered, and make 324 * stdout line buffered (unless debugfile == stdout). 325 */ 326 setvbuf(debug_file, NULL, _IONBF, 0); 327 if (debug_file != stdout) { 328 setvbuf(stdout, NULL, _IOLBF, 0); 329 } 330} 331 332/*- 333 * MainParseArgs -- 334 * Parse a given argument vector. Called from main() and from 335 * Main_ParseArgLine() when the .MAKEFLAGS target is used. 336 * 337 * XXX: Deal with command line overriding .MAKEFLAGS in makefile 338 * 339 * Results: 340 * None 341 * 342 * Side Effects: 343 * Various global and local flags will be set depending on the flags 344 * given 345 */ 346static void 347MainParseArgs(int argc, char **argv) 348{ 349 char *p; 350 int c = '?'; 351 int arginc; 352 char *argvalue; 353 const char *getopt_def; 354 char *optscan; 355 Boolean inOption, dashDash = FALSE; 356 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */ 357 358#define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrst" 359/* Can't actually use getopt(3) because rescanning is not portable */ 360 361 getopt_def = OPTFLAGS; 362rearg: 363 inOption = FALSE; 364 optscan = NULL; 365 while(argc > 1) { 366 char *getopt_spec; 367 if(!inOption) 368 optscan = argv[1]; 369 c = *optscan++; 370 arginc = 0; 371 if(inOption) { 372 if(c == '\0') { 373 ++argv; 374 --argc; 375 inOption = FALSE; 376 continue; 377 } 378 } else { 379 if (c != '-' || dashDash) 380 break; 381 inOption = TRUE; 382 c = *optscan++; 383 } 384 /* '-' found at some earlier point */ 385 getopt_spec = strchr(getopt_def, c); 386 if(c != '\0' && getopt_spec != NULL && getopt_spec[1] == ':') { 387 /* -<something> found, and <something> should have an arg */ 388 inOption = FALSE; 389 arginc = 1; 390 argvalue = optscan; 391 if(*argvalue == '\0') { 392 if (argc < 3) 393 goto noarg; 394 argvalue = argv[2]; 395 arginc = 2; 396 } 397 } else { 398 argvalue = NULL; 399 } 400 switch(c) { 401 case '\0': 402 arginc = 1; 403 inOption = FALSE; 404 break; 405 case 'B': 406 compatMake = TRUE; 407 Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL); 408 Var_Set(MAKE_MODE, "compat", VAR_GLOBAL, 0); 409 break; 410 case 'C': 411 if (chdir(argvalue) == -1) { 412 (void)fprintf(stderr, 413 "%s: chdir %s: %s\n", 414 progname, argvalue, 415 strerror(errno)); 416 exit(1); 417 } 418 if (getcwd(curdir, MAXPATHLEN) == NULL) { 419 (void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno)); 420 exit(2); 421 } 422 ignorePWD = TRUE; 423 break; 424 case 'D': 425 if (argvalue == NULL || argvalue[0] == 0) goto noarg; 426 Var_Set(argvalue, "1", VAR_GLOBAL, 0); 427 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); 428 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 429 break; 430 case 'I': 431 if (argvalue == NULL) goto noarg; 432 Parse_AddIncludeDir(argvalue); 433 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); 434 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 435 break; 436 case 'J': 437 if (argvalue == NULL) goto noarg; 438 if (sscanf(argvalue, "%d,%d", &jp_0, &jp_1) != 2) { 439 (void)fprintf(stderr, 440 "%s: internal error -- J option malformed (%s)\n", 441 progname, argvalue); 442 usage(); 443 } 444 if ((fcntl(jp_0, F_GETFD, 0) < 0) || 445 (fcntl(jp_1, F_GETFD, 0) < 0)) { 446#if 0 447 (void)fprintf(stderr, 448 "%s: ###### warning -- J descriptors were closed!\n", 449 progname); 450 exit(2); 451#endif 452 jp_0 = -1; 453 jp_1 = -1; 454 compatMake = TRUE; 455 } else { 456 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 457 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 458 jobServer = TRUE; 459 } 460 break; 461 case 'N': 462 noExecute = TRUE; 463 noRecursiveExecute = TRUE; 464 Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL); 465 break; 466 case 'S': 467 keepgoing = FALSE; 468 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); 469 break; 470 case 'T': 471 if (argvalue == NULL) goto noarg; 472 tracefile = bmake_strdup(argvalue); 473 Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL); 474 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 475 break; 476 case 'V': 477 if (argvalue == NULL) goto noarg; 478 printVars = TRUE; 479 (void)Lst_AtEnd(variables, argvalue); 480 Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL); 481 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 482 break; 483 case 'W': 484 parseWarnFatal = TRUE; 485 break; 486 case 'X': 487 varNoExportEnv = TRUE; 488 Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL); 489 break; 490 case 'd': 491 if (argvalue == NULL) goto noarg; 492 /* If '-d-opts' don't pass to children */ 493 if (argvalue[0] == '-') 494 argvalue++; 495 else { 496 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); 497 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 498 } 499 parse_debug_options(argvalue); 500 break; 501 case 'e': 502 checkEnvFirst = TRUE; 503 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); 504 break; 505 case 'f': 506 if (argvalue == NULL) goto noarg; 507 (void)Lst_AtEnd(makefiles, argvalue); 508 break; 509 case 'i': 510 ignoreErrors = TRUE; 511 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); 512 break; 513 case 'j': 514 if (argvalue == NULL) goto noarg; 515 forceJobs = TRUE; 516 maxJobs = strtol(argvalue, &p, 0); 517 if (*p != '\0' || maxJobs < 1) { 518 (void)fprintf(stderr, "%s: illegal argument to -j -- must be positive integer!\n", 519 progname); 520 exit(1); 521 } 522 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL); 523 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 524 Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL, 0); 525 maxJobTokens = maxJobs; 526 break; 527 case 'k': 528 keepgoing = TRUE; 529 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); 530 break; 531 case 'm': 532 if (argvalue == NULL) goto noarg; 533 /* look for magic parent directory search string */ 534 if (strncmp(".../", argvalue, 4) == 0) { 535 if (!Dir_FindHereOrAbove(curdir, argvalue+4, 536 found_path, sizeof(found_path))) 537 break; /* nothing doing */ 538 (void)Dir_AddDir(sysIncPath, found_path); 539 } else { 540 (void)Dir_AddDir(sysIncPath, argvalue); 541 } 542 Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL); 543 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 544 break; 545 case 'n': 546 noExecute = TRUE; 547 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); 548 break; 549 case 'q': 550 queryFlag = TRUE; 551 /* Kind of nonsensical, wot? */ 552 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); 553 break; 554 case 'r': 555 noBuiltins = TRUE; 556 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); 557 break; 558 case 's': 559 beSilent = TRUE; 560 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); 561 break; 562 case 't': 563 touchFlag = TRUE; 564 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); 565 break; 566 case '-': 567 dashDash = TRUE; 568 break; 569 default: 570 case '?': 571#ifndef MAKE_NATIVE 572 fprintf(stderr, "getopt(%s) -> %d (%c)\n", 573 OPTFLAGS, c, c); 574#endif 575 usage(); 576 } 577 argv += arginc; 578 argc -= arginc; 579 } 580 581 oldVars = TRUE; 582 583 /* 584 * See if the rest of the arguments are variable assignments and 585 * perform them if so. Else take them to be targets and stuff them 586 * on the end of the "create" list. 587 */ 588 for (; argc > 1; ++argv, --argc) 589 if (Parse_IsVar(argv[1])) { 590 Parse_DoVar(argv[1], VAR_CMD); 591 } else { 592 if (!*argv[1]) 593 Punt("illegal (null) argument."); 594 if (*argv[1] == '-' && !dashDash) 595 goto rearg; 596 (void)Lst_AtEnd(create, bmake_strdup(argv[1])); 597 } 598 599 return; 600noarg: 601 (void)fprintf(stderr, "%s: option requires an argument -- %c\n", 602 progname, c); 603 usage(); 604} 605 606/*- 607 * Main_ParseArgLine -- 608 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 609 * is encountered and by main() when reading the .MAKEFLAGS envariable. 610 * Takes a line of arguments and breaks it into its 611 * component words and passes those words and the number of them to the 612 * MainParseArgs function. 613 * The line should have all its leading whitespace removed. 614 * 615 * Input: 616 * line Line to fracture 617 * 618 * Results: 619 * None 620 * 621 * Side Effects: 622 * Only those that come from the various arguments. 623 */ 624void 625Main_ParseArgLine(const char *line) 626{ 627 char **argv; /* Manufactured argument vector */ 628 int argc; /* Number of arguments in argv */ 629 char *args; /* Space used by the args */ 630 char *buf, *p1; 631 char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1); 632 size_t len; 633 634 if (line == NULL) 635 return; 636 for (; *line == ' '; ++line) 637 continue; 638 if (!*line) 639 return; 640 641#ifndef POSIX 642 { 643 /* 644 * $MAKE may simply be naming the make(1) binary 645 */ 646 char *cp; 647 648 if (!(cp = strrchr(line, '/'))) 649 cp = line; 650 if ((cp = strstr(cp, "make")) && 651 strcmp(cp, "make") == 0) 652 return; 653 } 654#endif 655 buf = bmake_malloc(len = strlen(line) + strlen(argv0) + 2); 656 (void)snprintf(buf, len, "%s %s", argv0, line); 657 if (p1) 658 free(p1); 659 660 argv = brk_string(buf, &argc, TRUE, &args); 661 if (argv == NULL) { 662 Error("Unterminated quoted string [%s]", buf); 663 free(buf); 664 return; 665 } 666 free(buf); 667 MainParseArgs(argc, argv); 668 669 free(args); 670 free(argv); 671} 672 673Boolean 674Main_SetObjdir(const char *path) 675{ 676 struct stat sb; 677 char *p = NULL; 678 char buf[MAXPATHLEN + 1]; 679 Boolean rc = FALSE; 680 681 /* expand variable substitutions */ 682 if (strchr(path, '$') != 0) { 683 snprintf(buf, MAXPATHLEN, "%s", path); 684 path = p = Var_Subst(NULL, buf, VAR_GLOBAL, 0); 685 } 686 687 if (path[0] != '/') { 688 snprintf(buf, MAXPATHLEN, "%s/%s", curdir, path); 689 path = buf; 690 } 691 692 /* look for the directory and try to chdir there */ 693 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 694 if (chdir(path)) { 695 (void)fprintf(stderr, "make warning: %s: %s.\n", 696 path, strerror(errno)); 697 } else { 698 strncpy(objdir, path, MAXPATHLEN); 699 Var_Set(".OBJDIR", objdir, VAR_GLOBAL, 0); 700 setenv("PWD", objdir, 1); 701 Dir_InitDot(); 702 rc = TRUE; 703 } 704 } 705 706 if (p) 707 free(p); 708 return rc; 709} 710 711/*- 712 * ReadAllMakefiles -- 713 * wrapper around ReadMakefile() to read all. 714 * 715 * Results: 716 * TRUE if ok, FALSE on error 717 */ 718static int 719ReadAllMakefiles(const void *p, const void *q) 720{ 721 return (ReadMakefile(p, q) == 0); 722} 723 724int 725str2Lst_Append(Lst lp, char *str, const char *sep) 726{ 727 char *cp; 728 int n; 729 730 if (!sep) 731 sep = " \t"; 732 733 for (n = 0, cp = strtok(str, sep); cp; cp = strtok(NULL, sep)) { 734 (void)Lst_AtEnd(lp, cp); 735 n++; 736 } 737 return (n); 738} 739 740#ifdef SIGINFO 741/*ARGSUSED*/ 742static void 743siginfo(int signo MAKE_ATTR_UNUSED) 744{ 745 char dir[MAXPATHLEN]; 746 char str[2 * MAXPATHLEN]; 747 int len; 748 if (getcwd(dir, sizeof(dir)) == NULL) 749 return; 750 len = snprintf(str, sizeof(str), "%s: Working in: %s\n", progname, dir); 751 if (len > 0) 752 (void)write(STDERR_FILENO, str, (size_t)len); 753} 754#endif 755 756/* 757 * Allow makefiles some control over the mode we run in. 758 */ 759void 760MakeMode(const char *mode) 761{ 762 char *mp = NULL; 763 764 if (!mode) 765 mode = mp = Var_Subst(NULL, "${" MAKE_MODE ":tl}", VAR_GLOBAL, 0); 766 767 if (mode && *mode) { 768 if (strstr(mode, "compat")) { 769 compatMake = TRUE; 770 forceJobs = FALSE; 771 } 772#if USE_META 773 if (strstr(mode, "meta")) 774 meta_init(mode); 775#endif 776 } 777 if (mp) 778 free(mp); 779} 780 781/*- 782 * main -- 783 * The main function, for obvious reasons. Initializes variables 784 * and a few modules, then parses the arguments give it in the 785 * environment and on the command line. Reads the system makefile 786 * followed by either Makefile, makefile or the file given by the 787 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 788 * flags it has received by then uses either the Make or the Compat 789 * module to create the initial list of targets. 790 * 791 * Results: 792 * If -q was given, exits -1 if anything was out-of-date. Else it exits 793 * 0. 794 * 795 * Side Effects: 796 * The program exits when done. Targets are created. etc. etc. etc. 797 */ 798int 799main(int argc, char **argv) 800{ 801 Lst targs; /* target nodes to create -- passed to Make_Init */ 802 Boolean outOfDate = FALSE; /* FALSE if all targets up to date */ 803 struct stat sb, sa; 804 char *p1, *path, *pwd; 805 char mdpath[MAXPATHLEN]; 806#ifdef FORCE_MACHINE 807 const char *machine = FORCE_MACHINE; 808#else 809 const char *machine = getenv("MACHINE"); 810#endif 811 const char *machine_arch = getenv("MACHINE_ARCH"); 812 char *syspath = getenv("MAKESYSPATH"); 813 Lst sysMkPath; /* Path of sys.mk */ 814 char *cp = NULL, *start; 815 /* avoid faults on read-only strings */ 816 static char defsyspath[] = _PATH_DEFSYSPATH; 817 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */ 818 struct timeval rightnow; /* to initialize random seed */ 819#ifdef MAKE_NATIVE 820 struct utsname utsname; 821#endif 822 823 /* default to writing debug to stderr */ 824 debug_file = stderr; 825 826#ifdef SIGINFO 827 (void)bmake_signal(SIGINFO, siginfo); 828#endif 829 /* 830 * Set the seed to produce a different random sequence 831 * on each program execution. 832 */ 833 gettimeofday(&rightnow, NULL); 834 srandom(rightnow.tv_sec + rightnow.tv_usec); 835 836 if ((progname = strrchr(argv[0], '/')) != NULL) 837 progname++; 838 else 839 progname = argv[0]; 840#ifdef RLIMIT_NOFILE 841 /* 842 * get rid of resource limit on file descriptors 843 */ 844 { 845 struct rlimit rl; 846 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && 847 rl.rlim_cur != rl.rlim_max) { 848 rl.rlim_cur = rl.rlim_max; 849 (void)setrlimit(RLIMIT_NOFILE, &rl); 850 } 851 } 852#endif 853 854 /* 855 * Get the name of this type of MACHINE from utsname 856 * so we can share an executable for similar machines. 857 * (i.e. m68k: amiga hp300, mac68k, sun3, ...) 858 * 859 * Note that both MACHINE and MACHINE_ARCH are decided at 860 * run-time. 861 */ 862 if (!machine) { 863#ifdef MAKE_NATIVE 864 if (uname(&utsname) == -1) { 865 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname, 866 strerror(errno)); 867 exit(2); 868 } 869 machine = utsname.machine; 870#else 871#ifdef MAKE_MACHINE 872 machine = MAKE_MACHINE; 873#else 874 machine = "unknown"; 875#endif 876#endif 877 } 878 879 if (!machine_arch) { 880#ifndef MACHINE_ARCH 881#ifdef MAKE_MACHINE_ARCH 882 machine_arch = MAKE_MACHINE_ARCH; 883#else 884 machine_arch = "unknown"; 885#endif 886#else 887 machine_arch = MACHINE_ARCH; 888#endif 889 } 890 891 myPid = getpid(); /* remember this for vFork() */ 892 893 /* 894 * Just in case MAKEOBJDIR wants us to do something tricky. 895 */ 896 Var_Init(); /* Initialize the lists of variables for 897 * parsing arguments */ 898 Var_Set("MACHINE", machine, VAR_GLOBAL, 0); 899 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0); 900#ifdef MAKE_VERSION 901 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL, 0); 902#endif 903 Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */ 904 /* 905 * This is the traditional preference for makefiles. 906 */ 907#ifndef MAKEFILE_PREFERENCE_LIST 908# define MAKEFILE_PREFERENCE_LIST "makefile Makefile" 909#endif 910 Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST, 911 VAR_GLOBAL, 0); 912 Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL, 0); 913 914 create = Lst_Init(FALSE); 915 makefiles = Lst_Init(FALSE); 916 printVars = FALSE; 917 debugVflag = FALSE; 918 variables = Lst_Init(FALSE); 919 beSilent = FALSE; /* Print commands as executed */ 920 ignoreErrors = FALSE; /* Pay attention to non-zero returns */ 921 noExecute = FALSE; /* Execute all commands */ 922 noRecursiveExecute = FALSE; /* Execute all .MAKE targets */ 923 keepgoing = FALSE; /* Stop on error */ 924 allPrecious = FALSE; /* Remove targets when interrupted */ 925 queryFlag = FALSE; /* This is not just a check-run */ 926 noBuiltins = FALSE; /* Read the built-in rules */ 927 touchFlag = FALSE; /* Actually update targets */ 928 debug = 0; /* No debug verbosity, please. */ 929 jobsRunning = FALSE; 930 931 maxJobs = DEFMAXLOCAL; /* Set default local max concurrency */ 932 maxJobTokens = maxJobs; 933 compatMake = FALSE; /* No compat mode */ 934 ignorePWD = FALSE; 935 936 /* 937 * Initialize the parsing, directory and variable modules to prepare 938 * for the reading of inclusion paths and variable settings on the 939 * command line 940 */ 941 942 /* 943 * Initialize various variables. 944 * MAKE also gets this name, for compatibility 945 * .MAKEFLAGS gets set to the empty string just in case. 946 * MFLAGS also gets initialized empty, for compatibility. 947 */ 948 Parse_Init(); 949 if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) { 950 /* 951 * Leave alone if it is an absolute path, or if it does 952 * not contain a '/' in which case we need to find it in 953 * the path, like execvp(3) and the shells do. 954 */ 955 p1 = argv[0]; 956 } else { 957 /* 958 * A relative path, canonicalize it. 959 */ 960 p1 = realpath(argv[0], mdpath); 961 if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) { 962 p1 = argv[0]; /* realpath failed */ 963 } 964 } 965 Var_Set("MAKE", p1, VAR_GLOBAL, 0); 966 Var_Set(".MAKE", p1, VAR_GLOBAL, 0); 967 Var_Set(MAKEFLAGS, "", VAR_GLOBAL, 0); 968 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL, 0); 969 Var_Set("MFLAGS", "", VAR_GLOBAL, 0); 970 Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0); 971 972 /* 973 * Set some other useful macros 974 */ 975 { 976 char tmp[64]; 977 const char *ep; 978 979 if (!(ep = getenv(MAKE_LEVEL))) { 980#ifdef MAKE_LEVEL_SAFE 981 if (!(ep = getenv(MAKE_LEVEL_SAFE))) 982#endif 983 ep = "0"; 984 } 985 Var_Set(MAKE_LEVEL, ep, VAR_GLOBAL, 0); 986 snprintf(tmp, sizeof(tmp), "%u", myPid); 987 Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0); 988 snprintf(tmp, sizeof(tmp), "%u", getppid()); 989 Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0); 990 } 991 Job_SetPrefix(); 992 993 /* 994 * First snag any flags out of the MAKE environment variable. 995 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's 996 * in a different format). 997 */ 998#ifdef POSIX 999 Main_ParseArgLine(getenv("MAKEFLAGS")); 1000#else 1001 Main_ParseArgLine(getenv("MAKE")); 1002#endif 1003 1004 /* 1005 * Find where we are (now). 1006 * We take care of PWD for the automounter below... 1007 */ 1008 if (getcwd(curdir, MAXPATHLEN) == NULL) { 1009 (void)fprintf(stderr, "%s: getcwd: %s.\n", 1010 progname, strerror(errno)); 1011 exit(2); 1012 } 1013 1014 MainParseArgs(argc, argv); 1015 1016 /* 1017 * Verify that cwd is sane. 1018 */ 1019 if (stat(curdir, &sa) == -1) { 1020 (void)fprintf(stderr, "%s: %s: %s.\n", 1021 progname, curdir, strerror(errno)); 1022 exit(2); 1023 } 1024 1025 /* 1026 * All this code is so that we know where we are when we start up 1027 * on a different machine with pmake. 1028 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX 1029 * since the value of curdir can vary depending on how we got 1030 * here. Ie sitting at a shell prompt (shell that provides $PWD) 1031 * or via subdir.mk in which case its likely a shell which does 1032 * not provide it. 1033 * So, to stop it breaking this case only, we ignore PWD if 1034 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform. 1035 */ 1036#ifndef NO_PWD_OVERRIDE 1037 if (!ignorePWD && 1038 (pwd = getenv("PWD")) != NULL && 1039 getenv("MAKEOBJDIRPREFIX") == NULL) { 1040 const char *makeobjdir = getenv("MAKEOBJDIR"); 1041 1042 if (makeobjdir == NULL || !strchr(makeobjdir, '$')) { 1043 if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino && 1044 sa.st_dev == sb.st_dev) 1045 (void)strncpy(curdir, pwd, MAXPATHLEN); 1046 } 1047 } 1048#endif 1049 Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0); 1050 1051 /* 1052 * Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that, 1053 * MAKEOBJDIR is set in the environment, try only that value 1054 * and fall back to .CURDIR if it does not exist. 1055 * 1056 * Otherwise, try _PATH_OBJDIR.MACHINE, _PATH_OBJDIR, and 1057 * finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none 1058 * of these paths exist, just use .CURDIR. 1059 */ 1060 Dir_Init(curdir); 1061 (void)Main_SetObjdir(curdir); 1062 1063 if ((path = getenv("MAKEOBJDIRPREFIX")) != NULL) { 1064 (void)snprintf(mdpath, MAXPATHLEN, "%s%s", path, curdir); 1065 (void)Main_SetObjdir(mdpath); 1066 } else if ((path = getenv("MAKEOBJDIR")) != NULL) { 1067 (void)Main_SetObjdir(path); 1068 } else { 1069 (void)snprintf(mdpath, MAXPATHLEN, "%s.%s", _PATH_OBJDIR, machine); 1070 if (!Main_SetObjdir(mdpath) && !Main_SetObjdir(_PATH_OBJDIR)) { 1071 (void)snprintf(mdpath, MAXPATHLEN, "%s%s", 1072 _PATH_OBJDIRPREFIX, curdir); 1073 (void)Main_SetObjdir(mdpath); 1074 } 1075 } 1076 1077 /* 1078 * Be compatible if user did not specify -j and did not explicitly 1079 * turned compatibility on 1080 */ 1081 if (!compatMake && !forceJobs) { 1082 compatMake = TRUE; 1083 } 1084 1085 /* 1086 * Initialize archive, target and suffix modules in preparation for 1087 * parsing the makefile(s) 1088 */ 1089 Arch_Init(); 1090 Targ_Init(); 1091 Suff_Init(); 1092 Trace_Init(tracefile); 1093 1094 DEFAULT = NULL; 1095 (void)time(&now); 1096 1097 Trace_Log(MAKESTART, NULL); 1098 1099 /* 1100 * Set up the .TARGETS variable to contain the list of targets to be 1101 * created. If none specified, make the variable empty -- the parser 1102 * will fill the thing in with the default or .MAIN target. 1103 */ 1104 if (!Lst_IsEmpty(create)) { 1105 LstNode ln; 1106 1107 for (ln = Lst_First(create); ln != NULL; 1108 ln = Lst_Succ(ln)) { 1109 char *name = (char *)Lst_Datum(ln); 1110 1111 Var_Append(".TARGETS", name, VAR_GLOBAL); 1112 } 1113 } else 1114 Var_Set(".TARGETS", "", VAR_GLOBAL, 0); 1115 1116 1117 /* 1118 * If no user-supplied system path was given (through the -m option) 1119 * add the directories from the DEFSYSPATH (more than one may be given 1120 * as dir1:...:dirn) to the system include path. 1121 */ 1122 if (syspath == NULL || *syspath == '\0') 1123 syspath = defsyspath; 1124 else 1125 syspath = bmake_strdup(syspath); 1126 1127 for (start = syspath; *start != '\0'; start = cp) { 1128 for (cp = start; *cp != '\0' && *cp != ':'; cp++) 1129 continue; 1130 if (*cp == ':') { 1131 *cp++ = '\0'; 1132 } 1133 /* look for magic parent directory search string */ 1134 if (strncmp(".../", start, 4) != 0) { 1135 (void)Dir_AddDir(defIncPath, start); 1136 } else { 1137 if (Dir_FindHereOrAbove(curdir, start+4, 1138 found_path, sizeof(found_path))) { 1139 (void)Dir_AddDir(defIncPath, found_path); 1140 } 1141 } 1142 } 1143 if (syspath != defsyspath) 1144 free(syspath); 1145 1146 /* 1147 * Read in the built-in rules first, followed by the specified 1148 * makefile, if it was (makefile != NULL), or the default 1149 * makefile and Makefile, in that order, if it wasn't. 1150 */ 1151 if (!noBuiltins) { 1152 LstNode ln; 1153 1154 sysMkPath = Lst_Init(FALSE); 1155 Dir_Expand(_PATH_DEFSYSMK, 1156 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath, 1157 sysMkPath); 1158 if (Lst_IsEmpty(sysMkPath)) 1159 Fatal("%s: no system rules (%s).", progname, 1160 _PATH_DEFSYSMK); 1161 ln = Lst_Find(sysMkPath, NULL, ReadMakefile); 1162 if (ln == NULL) 1163 Fatal("%s: cannot open %s.", progname, 1164 (char *)Lst_Datum(ln)); 1165 } 1166 1167 if (!Lst_IsEmpty(makefiles)) { 1168 LstNode ln; 1169 1170 ln = Lst_Find(makefiles, NULL, ReadAllMakefiles); 1171 if (ln != NULL) 1172 Fatal("%s: cannot open %s.", progname, 1173 (char *)Lst_Datum(ln)); 1174 } else { 1175 p1 = Var_Subst(NULL, "${" MAKEFILE_PREFERENCE "}", 1176 VAR_CMD, 0); 1177 if (p1) { 1178 (void)str2Lst_Append(makefiles, p1, NULL); 1179 (void)Lst_Find(makefiles, NULL, ReadMakefile); 1180 free(p1); 1181 } 1182 } 1183 1184 /* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */ 1185 if (!noBuiltins || !printVars) { 1186 makeDependfile = Var_Subst(NULL, "${.MAKE.DEPENDFILE:T}", 1187 VAR_CMD, 0); 1188 doing_depend = TRUE; 1189 (void)ReadMakefile(makeDependfile, NULL); 1190 doing_depend = FALSE; 1191 } 1192 1193 MakeMode(NULL); 1194 1195 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); 1196 if (p1) 1197 free(p1); 1198 1199 if (!compatMake) 1200 Job_ServerStart(maxJobTokens, jp_0, jp_1); 1201 if (DEBUG(JOB)) 1202 fprintf(debug_file, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n", 1203 jp_0, jp_1, maxJobs, maxJobTokens, compatMake); 1204 1205 Main_ExportMAKEFLAGS(TRUE); /* initial export */ 1206
| 179static void MainParseArgs(int, char **); 180static int ReadMakefile(const void *, const void *); 181static void usage(void) MAKE_ATTR_DEAD; 182 183static Boolean ignorePWD; /* if we use -C, PWD is meaningless */ 184static char objdir[MAXPATHLEN + 1]; /* where we chdir'ed to */ 185char curdir[MAXPATHLEN + 1]; /* Startup directory */ 186char *progname; /* the program name */ 187char *makeDependfile; 188pid_t myPid; 189 190Boolean forceJobs = FALSE; 191 192/* 193 * On some systems MACHINE is defined as something other than 194 * what we want. 195 */ 196#ifdef FORCE_MACHINE 197# undef MACHINE 198# define MACHINE FORCE_MACHINE 199#endif 200 201extern Lst parseIncPath; 202 203static void 204parse_debug_options(const char *argvalue) 205{ 206 const char *modules; 207 const char *mode; 208 char *fname; 209 int len; 210 211 for (modules = argvalue; *modules; ++modules) { 212 switch (*modules) { 213 case 'A': 214 debug = ~0; 215 break; 216 case 'a': 217 debug |= DEBUG_ARCH; 218 break; 219 case 'C': 220 debug |= DEBUG_CWD; 221 break; 222 case 'c': 223 debug |= DEBUG_COND; 224 break; 225 case 'd': 226 debug |= DEBUG_DIR; 227 break; 228 case 'e': 229 debug |= DEBUG_ERROR; 230 break; 231 case 'f': 232 debug |= DEBUG_FOR; 233 break; 234 case 'g': 235 if (modules[1] == '1') { 236 debug |= DEBUG_GRAPH1; 237 ++modules; 238 } 239 else if (modules[1] == '2') { 240 debug |= DEBUG_GRAPH2; 241 ++modules; 242 } 243 else if (modules[1] == '3') { 244 debug |= DEBUG_GRAPH3; 245 ++modules; 246 } 247 break; 248 case 'j': 249 debug |= DEBUG_JOB; 250 break; 251 case 'l': 252 debug |= DEBUG_LOUD; 253 break; 254 case 'M': 255 debug |= DEBUG_META; 256 break; 257 case 'm': 258 debug |= DEBUG_MAKE; 259 break; 260 case 'n': 261 debug |= DEBUG_SCRIPT; 262 break; 263 case 'p': 264 debug |= DEBUG_PARSE; 265 break; 266 case 's': 267 debug |= DEBUG_SUFF; 268 break; 269 case 't': 270 debug |= DEBUG_TARG; 271 break; 272 case 'V': 273 debugVflag = TRUE; 274 break; 275 case 'v': 276 debug |= DEBUG_VAR; 277 break; 278 case 'x': 279 debug |= DEBUG_SHELL; 280 break; 281 case 'F': 282 if (debug_file != stdout && debug_file != stderr) 283 fclose(debug_file); 284 if (*++modules == '+') { 285 modules++; 286 mode = "a"; 287 } else 288 mode = "w"; 289 if (strcmp(modules, "stdout") == 0) { 290 debug_file = stdout; 291 goto debug_setbuf; 292 } 293 if (strcmp(modules, "stderr") == 0) { 294 debug_file = stderr; 295 goto debug_setbuf; 296 } 297 len = strlen(modules); 298 fname = malloc(len + 20); 299 memcpy(fname, modules, len + 1); 300 /* Let the filename be modified by the pid */ 301 if (strcmp(fname + len - 3, ".%d") == 0) 302 snprintf(fname + len - 2, 20, "%d", getpid()); 303 debug_file = fopen(fname, mode); 304 if (!debug_file) { 305 fprintf(stderr, "Cannot open debug file %s\n", 306 fname); 307 usage(); 308 } 309 free(fname); 310 goto debug_setbuf; 311 default: 312 (void)fprintf(stderr, 313 "%s: illegal argument to d option -- %c\n", 314 progname, *modules); 315 usage(); 316 } 317 } 318debug_setbuf: 319 /* 320 * Make the debug_file unbuffered, and make 321 * stdout line buffered (unless debugfile == stdout). 322 */ 323 setvbuf(debug_file, NULL, _IONBF, 0); 324 if (debug_file != stdout) { 325 setvbuf(stdout, NULL, _IOLBF, 0); 326 } 327} 328 329/*- 330 * MainParseArgs -- 331 * Parse a given argument vector. Called from main() and from 332 * Main_ParseArgLine() when the .MAKEFLAGS target is used. 333 * 334 * XXX: Deal with command line overriding .MAKEFLAGS in makefile 335 * 336 * Results: 337 * None 338 * 339 * Side Effects: 340 * Various global and local flags will be set depending on the flags 341 * given 342 */ 343static void 344MainParseArgs(int argc, char **argv) 345{ 346 char *p; 347 int c = '?'; 348 int arginc; 349 char *argvalue; 350 const char *getopt_def; 351 char *optscan; 352 Boolean inOption, dashDash = FALSE; 353 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */ 354 355#define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrst" 356/* Can't actually use getopt(3) because rescanning is not portable */ 357 358 getopt_def = OPTFLAGS; 359rearg: 360 inOption = FALSE; 361 optscan = NULL; 362 while(argc > 1) { 363 char *getopt_spec; 364 if(!inOption) 365 optscan = argv[1]; 366 c = *optscan++; 367 arginc = 0; 368 if(inOption) { 369 if(c == '\0') { 370 ++argv; 371 --argc; 372 inOption = FALSE; 373 continue; 374 } 375 } else { 376 if (c != '-' || dashDash) 377 break; 378 inOption = TRUE; 379 c = *optscan++; 380 } 381 /* '-' found at some earlier point */ 382 getopt_spec = strchr(getopt_def, c); 383 if(c != '\0' && getopt_spec != NULL && getopt_spec[1] == ':') { 384 /* -<something> found, and <something> should have an arg */ 385 inOption = FALSE; 386 arginc = 1; 387 argvalue = optscan; 388 if(*argvalue == '\0') { 389 if (argc < 3) 390 goto noarg; 391 argvalue = argv[2]; 392 arginc = 2; 393 } 394 } else { 395 argvalue = NULL; 396 } 397 switch(c) { 398 case '\0': 399 arginc = 1; 400 inOption = FALSE; 401 break; 402 case 'B': 403 compatMake = TRUE; 404 Var_Append(MAKEFLAGS, "-B", VAR_GLOBAL); 405 Var_Set(MAKE_MODE, "compat", VAR_GLOBAL, 0); 406 break; 407 case 'C': 408 if (chdir(argvalue) == -1) { 409 (void)fprintf(stderr, 410 "%s: chdir %s: %s\n", 411 progname, argvalue, 412 strerror(errno)); 413 exit(1); 414 } 415 if (getcwd(curdir, MAXPATHLEN) == NULL) { 416 (void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno)); 417 exit(2); 418 } 419 ignorePWD = TRUE; 420 break; 421 case 'D': 422 if (argvalue == NULL || argvalue[0] == 0) goto noarg; 423 Var_Set(argvalue, "1", VAR_GLOBAL, 0); 424 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); 425 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 426 break; 427 case 'I': 428 if (argvalue == NULL) goto noarg; 429 Parse_AddIncludeDir(argvalue); 430 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); 431 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 432 break; 433 case 'J': 434 if (argvalue == NULL) goto noarg; 435 if (sscanf(argvalue, "%d,%d", &jp_0, &jp_1) != 2) { 436 (void)fprintf(stderr, 437 "%s: internal error -- J option malformed (%s)\n", 438 progname, argvalue); 439 usage(); 440 } 441 if ((fcntl(jp_0, F_GETFD, 0) < 0) || 442 (fcntl(jp_1, F_GETFD, 0) < 0)) { 443#if 0 444 (void)fprintf(stderr, 445 "%s: ###### warning -- J descriptors were closed!\n", 446 progname); 447 exit(2); 448#endif 449 jp_0 = -1; 450 jp_1 = -1; 451 compatMake = TRUE; 452 } else { 453 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 454 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 455 jobServer = TRUE; 456 } 457 break; 458 case 'N': 459 noExecute = TRUE; 460 noRecursiveExecute = TRUE; 461 Var_Append(MAKEFLAGS, "-N", VAR_GLOBAL); 462 break; 463 case 'S': 464 keepgoing = FALSE; 465 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); 466 break; 467 case 'T': 468 if (argvalue == NULL) goto noarg; 469 tracefile = bmake_strdup(argvalue); 470 Var_Append(MAKEFLAGS, "-T", VAR_GLOBAL); 471 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 472 break; 473 case 'V': 474 if (argvalue == NULL) goto noarg; 475 printVars = TRUE; 476 (void)Lst_AtEnd(variables, argvalue); 477 Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL); 478 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 479 break; 480 case 'W': 481 parseWarnFatal = TRUE; 482 break; 483 case 'X': 484 varNoExportEnv = TRUE; 485 Var_Append(MAKEFLAGS, "-X", VAR_GLOBAL); 486 break; 487 case 'd': 488 if (argvalue == NULL) goto noarg; 489 /* If '-d-opts' don't pass to children */ 490 if (argvalue[0] == '-') 491 argvalue++; 492 else { 493 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); 494 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 495 } 496 parse_debug_options(argvalue); 497 break; 498 case 'e': 499 checkEnvFirst = TRUE; 500 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); 501 break; 502 case 'f': 503 if (argvalue == NULL) goto noarg; 504 (void)Lst_AtEnd(makefiles, argvalue); 505 break; 506 case 'i': 507 ignoreErrors = TRUE; 508 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); 509 break; 510 case 'j': 511 if (argvalue == NULL) goto noarg; 512 forceJobs = TRUE; 513 maxJobs = strtol(argvalue, &p, 0); 514 if (*p != '\0' || maxJobs < 1) { 515 (void)fprintf(stderr, "%s: illegal argument to -j -- must be positive integer!\n", 516 progname); 517 exit(1); 518 } 519 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL); 520 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 521 Var_Set(".MAKE.JOBS", argvalue, VAR_GLOBAL, 0); 522 maxJobTokens = maxJobs; 523 break; 524 case 'k': 525 keepgoing = TRUE; 526 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); 527 break; 528 case 'm': 529 if (argvalue == NULL) goto noarg; 530 /* look for magic parent directory search string */ 531 if (strncmp(".../", argvalue, 4) == 0) { 532 if (!Dir_FindHereOrAbove(curdir, argvalue+4, 533 found_path, sizeof(found_path))) 534 break; /* nothing doing */ 535 (void)Dir_AddDir(sysIncPath, found_path); 536 } else { 537 (void)Dir_AddDir(sysIncPath, argvalue); 538 } 539 Var_Append(MAKEFLAGS, "-m", VAR_GLOBAL); 540 Var_Append(MAKEFLAGS, argvalue, VAR_GLOBAL); 541 break; 542 case 'n': 543 noExecute = TRUE; 544 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); 545 break; 546 case 'q': 547 queryFlag = TRUE; 548 /* Kind of nonsensical, wot? */ 549 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); 550 break; 551 case 'r': 552 noBuiltins = TRUE; 553 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); 554 break; 555 case 's': 556 beSilent = TRUE; 557 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); 558 break; 559 case 't': 560 touchFlag = TRUE; 561 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); 562 break; 563 case '-': 564 dashDash = TRUE; 565 break; 566 default: 567 case '?': 568#ifndef MAKE_NATIVE 569 fprintf(stderr, "getopt(%s) -> %d (%c)\n", 570 OPTFLAGS, c, c); 571#endif 572 usage(); 573 } 574 argv += arginc; 575 argc -= arginc; 576 } 577 578 oldVars = TRUE; 579 580 /* 581 * See if the rest of the arguments are variable assignments and 582 * perform them if so. Else take them to be targets and stuff them 583 * on the end of the "create" list. 584 */ 585 for (; argc > 1; ++argv, --argc) 586 if (Parse_IsVar(argv[1])) { 587 Parse_DoVar(argv[1], VAR_CMD); 588 } else { 589 if (!*argv[1]) 590 Punt("illegal (null) argument."); 591 if (*argv[1] == '-' && !dashDash) 592 goto rearg; 593 (void)Lst_AtEnd(create, bmake_strdup(argv[1])); 594 } 595 596 return; 597noarg: 598 (void)fprintf(stderr, "%s: option requires an argument -- %c\n", 599 progname, c); 600 usage(); 601} 602 603/*- 604 * Main_ParseArgLine -- 605 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 606 * is encountered and by main() when reading the .MAKEFLAGS envariable. 607 * Takes a line of arguments and breaks it into its 608 * component words and passes those words and the number of them to the 609 * MainParseArgs function. 610 * The line should have all its leading whitespace removed. 611 * 612 * Input: 613 * line Line to fracture 614 * 615 * Results: 616 * None 617 * 618 * Side Effects: 619 * Only those that come from the various arguments. 620 */ 621void 622Main_ParseArgLine(const char *line) 623{ 624 char **argv; /* Manufactured argument vector */ 625 int argc; /* Number of arguments in argv */ 626 char *args; /* Space used by the args */ 627 char *buf, *p1; 628 char *argv0 = Var_Value(".MAKE", VAR_GLOBAL, &p1); 629 size_t len; 630 631 if (line == NULL) 632 return; 633 for (; *line == ' '; ++line) 634 continue; 635 if (!*line) 636 return; 637 638#ifndef POSIX 639 { 640 /* 641 * $MAKE may simply be naming the make(1) binary 642 */ 643 char *cp; 644 645 if (!(cp = strrchr(line, '/'))) 646 cp = line; 647 if ((cp = strstr(cp, "make")) && 648 strcmp(cp, "make") == 0) 649 return; 650 } 651#endif 652 buf = bmake_malloc(len = strlen(line) + strlen(argv0) + 2); 653 (void)snprintf(buf, len, "%s %s", argv0, line); 654 if (p1) 655 free(p1); 656 657 argv = brk_string(buf, &argc, TRUE, &args); 658 if (argv == NULL) { 659 Error("Unterminated quoted string [%s]", buf); 660 free(buf); 661 return; 662 } 663 free(buf); 664 MainParseArgs(argc, argv); 665 666 free(args); 667 free(argv); 668} 669 670Boolean 671Main_SetObjdir(const char *path) 672{ 673 struct stat sb; 674 char *p = NULL; 675 char buf[MAXPATHLEN + 1]; 676 Boolean rc = FALSE; 677 678 /* expand variable substitutions */ 679 if (strchr(path, '$') != 0) { 680 snprintf(buf, MAXPATHLEN, "%s", path); 681 path = p = Var_Subst(NULL, buf, VAR_GLOBAL, 0); 682 } 683 684 if (path[0] != '/') { 685 snprintf(buf, MAXPATHLEN, "%s/%s", curdir, path); 686 path = buf; 687 } 688 689 /* look for the directory and try to chdir there */ 690 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 691 if (chdir(path)) { 692 (void)fprintf(stderr, "make warning: %s: %s.\n", 693 path, strerror(errno)); 694 } else { 695 strncpy(objdir, path, MAXPATHLEN); 696 Var_Set(".OBJDIR", objdir, VAR_GLOBAL, 0); 697 setenv("PWD", objdir, 1); 698 Dir_InitDot(); 699 rc = TRUE; 700 } 701 } 702 703 if (p) 704 free(p); 705 return rc; 706} 707 708/*- 709 * ReadAllMakefiles -- 710 * wrapper around ReadMakefile() to read all. 711 * 712 * Results: 713 * TRUE if ok, FALSE on error 714 */ 715static int 716ReadAllMakefiles(const void *p, const void *q) 717{ 718 return (ReadMakefile(p, q) == 0); 719} 720 721int 722str2Lst_Append(Lst lp, char *str, const char *sep) 723{ 724 char *cp; 725 int n; 726 727 if (!sep) 728 sep = " \t"; 729 730 for (n = 0, cp = strtok(str, sep); cp; cp = strtok(NULL, sep)) { 731 (void)Lst_AtEnd(lp, cp); 732 n++; 733 } 734 return (n); 735} 736 737#ifdef SIGINFO 738/*ARGSUSED*/ 739static void 740siginfo(int signo MAKE_ATTR_UNUSED) 741{ 742 char dir[MAXPATHLEN]; 743 char str[2 * MAXPATHLEN]; 744 int len; 745 if (getcwd(dir, sizeof(dir)) == NULL) 746 return; 747 len = snprintf(str, sizeof(str), "%s: Working in: %s\n", progname, dir); 748 if (len > 0) 749 (void)write(STDERR_FILENO, str, (size_t)len); 750} 751#endif 752 753/* 754 * Allow makefiles some control over the mode we run in. 755 */ 756void 757MakeMode(const char *mode) 758{ 759 char *mp = NULL; 760 761 if (!mode) 762 mode = mp = Var_Subst(NULL, "${" MAKE_MODE ":tl}", VAR_GLOBAL, 0); 763 764 if (mode && *mode) { 765 if (strstr(mode, "compat")) { 766 compatMake = TRUE; 767 forceJobs = FALSE; 768 } 769#if USE_META 770 if (strstr(mode, "meta")) 771 meta_init(mode); 772#endif 773 } 774 if (mp) 775 free(mp); 776} 777 778/*- 779 * main -- 780 * The main function, for obvious reasons. Initializes variables 781 * and a few modules, then parses the arguments give it in the 782 * environment and on the command line. Reads the system makefile 783 * followed by either Makefile, makefile or the file given by the 784 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 785 * flags it has received by then uses either the Make or the Compat 786 * module to create the initial list of targets. 787 * 788 * Results: 789 * If -q was given, exits -1 if anything was out-of-date. Else it exits 790 * 0. 791 * 792 * Side Effects: 793 * The program exits when done. Targets are created. etc. etc. etc. 794 */ 795int 796main(int argc, char **argv) 797{ 798 Lst targs; /* target nodes to create -- passed to Make_Init */ 799 Boolean outOfDate = FALSE; /* FALSE if all targets up to date */ 800 struct stat sb, sa; 801 char *p1, *path, *pwd; 802 char mdpath[MAXPATHLEN]; 803#ifdef FORCE_MACHINE 804 const char *machine = FORCE_MACHINE; 805#else 806 const char *machine = getenv("MACHINE"); 807#endif 808 const char *machine_arch = getenv("MACHINE_ARCH"); 809 char *syspath = getenv("MAKESYSPATH"); 810 Lst sysMkPath; /* Path of sys.mk */ 811 char *cp = NULL, *start; 812 /* avoid faults on read-only strings */ 813 static char defsyspath[] = _PATH_DEFSYSPATH; 814 char found_path[MAXPATHLEN + 1]; /* for searching for sys.mk */ 815 struct timeval rightnow; /* to initialize random seed */ 816#ifdef MAKE_NATIVE 817 struct utsname utsname; 818#endif 819 820 /* default to writing debug to stderr */ 821 debug_file = stderr; 822 823#ifdef SIGINFO 824 (void)bmake_signal(SIGINFO, siginfo); 825#endif 826 /* 827 * Set the seed to produce a different random sequence 828 * on each program execution. 829 */ 830 gettimeofday(&rightnow, NULL); 831 srandom(rightnow.tv_sec + rightnow.tv_usec); 832 833 if ((progname = strrchr(argv[0], '/')) != NULL) 834 progname++; 835 else 836 progname = argv[0]; 837#ifdef RLIMIT_NOFILE 838 /* 839 * get rid of resource limit on file descriptors 840 */ 841 { 842 struct rlimit rl; 843 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && 844 rl.rlim_cur != rl.rlim_max) { 845 rl.rlim_cur = rl.rlim_max; 846 (void)setrlimit(RLIMIT_NOFILE, &rl); 847 } 848 } 849#endif 850 851 /* 852 * Get the name of this type of MACHINE from utsname 853 * so we can share an executable for similar machines. 854 * (i.e. m68k: amiga hp300, mac68k, sun3, ...) 855 * 856 * Note that both MACHINE and MACHINE_ARCH are decided at 857 * run-time. 858 */ 859 if (!machine) { 860#ifdef MAKE_NATIVE 861 if (uname(&utsname) == -1) { 862 (void)fprintf(stderr, "%s: uname failed (%s).\n", progname, 863 strerror(errno)); 864 exit(2); 865 } 866 machine = utsname.machine; 867#else 868#ifdef MAKE_MACHINE 869 machine = MAKE_MACHINE; 870#else 871 machine = "unknown"; 872#endif 873#endif 874 } 875 876 if (!machine_arch) { 877#ifndef MACHINE_ARCH 878#ifdef MAKE_MACHINE_ARCH 879 machine_arch = MAKE_MACHINE_ARCH; 880#else 881 machine_arch = "unknown"; 882#endif 883#else 884 machine_arch = MACHINE_ARCH; 885#endif 886 } 887 888 myPid = getpid(); /* remember this for vFork() */ 889 890 /* 891 * Just in case MAKEOBJDIR wants us to do something tricky. 892 */ 893 Var_Init(); /* Initialize the lists of variables for 894 * parsing arguments */ 895 Var_Set("MACHINE", machine, VAR_GLOBAL, 0); 896 Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0); 897#ifdef MAKE_VERSION 898 Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL, 0); 899#endif 900 Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */ 901 /* 902 * This is the traditional preference for makefiles. 903 */ 904#ifndef MAKEFILE_PREFERENCE_LIST 905# define MAKEFILE_PREFERENCE_LIST "makefile Makefile" 906#endif 907 Var_Set(MAKEFILE_PREFERENCE, MAKEFILE_PREFERENCE_LIST, 908 VAR_GLOBAL, 0); 909 Var_Set(MAKE_DEPENDFILE, ".depend", VAR_GLOBAL, 0); 910 911 create = Lst_Init(FALSE); 912 makefiles = Lst_Init(FALSE); 913 printVars = FALSE; 914 debugVflag = FALSE; 915 variables = Lst_Init(FALSE); 916 beSilent = FALSE; /* Print commands as executed */ 917 ignoreErrors = FALSE; /* Pay attention to non-zero returns */ 918 noExecute = FALSE; /* Execute all commands */ 919 noRecursiveExecute = FALSE; /* Execute all .MAKE targets */ 920 keepgoing = FALSE; /* Stop on error */ 921 allPrecious = FALSE; /* Remove targets when interrupted */ 922 queryFlag = FALSE; /* This is not just a check-run */ 923 noBuiltins = FALSE; /* Read the built-in rules */ 924 touchFlag = FALSE; /* Actually update targets */ 925 debug = 0; /* No debug verbosity, please. */ 926 jobsRunning = FALSE; 927 928 maxJobs = DEFMAXLOCAL; /* Set default local max concurrency */ 929 maxJobTokens = maxJobs; 930 compatMake = FALSE; /* No compat mode */ 931 ignorePWD = FALSE; 932 933 /* 934 * Initialize the parsing, directory and variable modules to prepare 935 * for the reading of inclusion paths and variable settings on the 936 * command line 937 */ 938 939 /* 940 * Initialize various variables. 941 * MAKE also gets this name, for compatibility 942 * .MAKEFLAGS gets set to the empty string just in case. 943 * MFLAGS also gets initialized empty, for compatibility. 944 */ 945 Parse_Init(); 946 if (argv[0][0] == '/' || strchr(argv[0], '/') == NULL) { 947 /* 948 * Leave alone if it is an absolute path, or if it does 949 * not contain a '/' in which case we need to find it in 950 * the path, like execvp(3) and the shells do. 951 */ 952 p1 = argv[0]; 953 } else { 954 /* 955 * A relative path, canonicalize it. 956 */ 957 p1 = realpath(argv[0], mdpath); 958 if (!p1 || *p1 != '/' || stat(p1, &sb) < 0) { 959 p1 = argv[0]; /* realpath failed */ 960 } 961 } 962 Var_Set("MAKE", p1, VAR_GLOBAL, 0); 963 Var_Set(".MAKE", p1, VAR_GLOBAL, 0); 964 Var_Set(MAKEFLAGS, "", VAR_GLOBAL, 0); 965 Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL, 0); 966 Var_Set("MFLAGS", "", VAR_GLOBAL, 0); 967 Var_Set(".ALLTARGETS", "", VAR_GLOBAL, 0); 968 969 /* 970 * Set some other useful macros 971 */ 972 { 973 char tmp[64]; 974 const char *ep; 975 976 if (!(ep = getenv(MAKE_LEVEL))) { 977#ifdef MAKE_LEVEL_SAFE 978 if (!(ep = getenv(MAKE_LEVEL_SAFE))) 979#endif 980 ep = "0"; 981 } 982 Var_Set(MAKE_LEVEL, ep, VAR_GLOBAL, 0); 983 snprintf(tmp, sizeof(tmp), "%u", myPid); 984 Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0); 985 snprintf(tmp, sizeof(tmp), "%u", getppid()); 986 Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0); 987 } 988 Job_SetPrefix(); 989 990 /* 991 * First snag any flags out of the MAKE environment variable. 992 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's 993 * in a different format). 994 */ 995#ifdef POSIX 996 Main_ParseArgLine(getenv("MAKEFLAGS")); 997#else 998 Main_ParseArgLine(getenv("MAKE")); 999#endif 1000 1001 /* 1002 * Find where we are (now). 1003 * We take care of PWD for the automounter below... 1004 */ 1005 if (getcwd(curdir, MAXPATHLEN) == NULL) { 1006 (void)fprintf(stderr, "%s: getcwd: %s.\n", 1007 progname, strerror(errno)); 1008 exit(2); 1009 } 1010 1011 MainParseArgs(argc, argv); 1012 1013 /* 1014 * Verify that cwd is sane. 1015 */ 1016 if (stat(curdir, &sa) == -1) { 1017 (void)fprintf(stderr, "%s: %s: %s.\n", 1018 progname, curdir, strerror(errno)); 1019 exit(2); 1020 } 1021 1022 /* 1023 * All this code is so that we know where we are when we start up 1024 * on a different machine with pmake. 1025 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX 1026 * since the value of curdir can vary depending on how we got 1027 * here. Ie sitting at a shell prompt (shell that provides $PWD) 1028 * or via subdir.mk in which case its likely a shell which does 1029 * not provide it. 1030 * So, to stop it breaking this case only, we ignore PWD if 1031 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform. 1032 */ 1033#ifndef NO_PWD_OVERRIDE 1034 if (!ignorePWD && 1035 (pwd = getenv("PWD")) != NULL && 1036 getenv("MAKEOBJDIRPREFIX") == NULL) { 1037 const char *makeobjdir = getenv("MAKEOBJDIR"); 1038 1039 if (makeobjdir == NULL || !strchr(makeobjdir, '$')) { 1040 if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino && 1041 sa.st_dev == sb.st_dev) 1042 (void)strncpy(curdir, pwd, MAXPATHLEN); 1043 } 1044 } 1045#endif 1046 Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0); 1047 1048 /* 1049 * Find the .OBJDIR. If MAKEOBJDIRPREFIX, or failing that, 1050 * MAKEOBJDIR is set in the environment, try only that value 1051 * and fall back to .CURDIR if it does not exist. 1052 * 1053 * Otherwise, try _PATH_OBJDIR.MACHINE, _PATH_OBJDIR, and 1054 * finally _PATH_OBJDIRPREFIX`pwd`, in that order. If none 1055 * of these paths exist, just use .CURDIR. 1056 */ 1057 Dir_Init(curdir); 1058 (void)Main_SetObjdir(curdir); 1059 1060 if ((path = getenv("MAKEOBJDIRPREFIX")) != NULL) { 1061 (void)snprintf(mdpath, MAXPATHLEN, "%s%s", path, curdir); 1062 (void)Main_SetObjdir(mdpath); 1063 } else if ((path = getenv("MAKEOBJDIR")) != NULL) { 1064 (void)Main_SetObjdir(path); 1065 } else { 1066 (void)snprintf(mdpath, MAXPATHLEN, "%s.%s", _PATH_OBJDIR, machine); 1067 if (!Main_SetObjdir(mdpath) && !Main_SetObjdir(_PATH_OBJDIR)) { 1068 (void)snprintf(mdpath, MAXPATHLEN, "%s%s", 1069 _PATH_OBJDIRPREFIX, curdir); 1070 (void)Main_SetObjdir(mdpath); 1071 } 1072 } 1073 1074 /* 1075 * Be compatible if user did not specify -j and did not explicitly 1076 * turned compatibility on 1077 */ 1078 if (!compatMake && !forceJobs) { 1079 compatMake = TRUE; 1080 } 1081 1082 /* 1083 * Initialize archive, target and suffix modules in preparation for 1084 * parsing the makefile(s) 1085 */ 1086 Arch_Init(); 1087 Targ_Init(); 1088 Suff_Init(); 1089 Trace_Init(tracefile); 1090 1091 DEFAULT = NULL; 1092 (void)time(&now); 1093 1094 Trace_Log(MAKESTART, NULL); 1095 1096 /* 1097 * Set up the .TARGETS variable to contain the list of targets to be 1098 * created. If none specified, make the variable empty -- the parser 1099 * will fill the thing in with the default or .MAIN target. 1100 */ 1101 if (!Lst_IsEmpty(create)) { 1102 LstNode ln; 1103 1104 for (ln = Lst_First(create); ln != NULL; 1105 ln = Lst_Succ(ln)) { 1106 char *name = (char *)Lst_Datum(ln); 1107 1108 Var_Append(".TARGETS", name, VAR_GLOBAL); 1109 } 1110 } else 1111 Var_Set(".TARGETS", "", VAR_GLOBAL, 0); 1112 1113 1114 /* 1115 * If no user-supplied system path was given (through the -m option) 1116 * add the directories from the DEFSYSPATH (more than one may be given 1117 * as dir1:...:dirn) to the system include path. 1118 */ 1119 if (syspath == NULL || *syspath == '\0') 1120 syspath = defsyspath; 1121 else 1122 syspath = bmake_strdup(syspath); 1123 1124 for (start = syspath; *start != '\0'; start = cp) { 1125 for (cp = start; *cp != '\0' && *cp != ':'; cp++) 1126 continue; 1127 if (*cp == ':') { 1128 *cp++ = '\0'; 1129 } 1130 /* look for magic parent directory search string */ 1131 if (strncmp(".../", start, 4) != 0) { 1132 (void)Dir_AddDir(defIncPath, start); 1133 } else { 1134 if (Dir_FindHereOrAbove(curdir, start+4, 1135 found_path, sizeof(found_path))) { 1136 (void)Dir_AddDir(defIncPath, found_path); 1137 } 1138 } 1139 } 1140 if (syspath != defsyspath) 1141 free(syspath); 1142 1143 /* 1144 * Read in the built-in rules first, followed by the specified 1145 * makefile, if it was (makefile != NULL), or the default 1146 * makefile and Makefile, in that order, if it wasn't. 1147 */ 1148 if (!noBuiltins) { 1149 LstNode ln; 1150 1151 sysMkPath = Lst_Init(FALSE); 1152 Dir_Expand(_PATH_DEFSYSMK, 1153 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath, 1154 sysMkPath); 1155 if (Lst_IsEmpty(sysMkPath)) 1156 Fatal("%s: no system rules (%s).", progname, 1157 _PATH_DEFSYSMK); 1158 ln = Lst_Find(sysMkPath, NULL, ReadMakefile); 1159 if (ln == NULL) 1160 Fatal("%s: cannot open %s.", progname, 1161 (char *)Lst_Datum(ln)); 1162 } 1163 1164 if (!Lst_IsEmpty(makefiles)) { 1165 LstNode ln; 1166 1167 ln = Lst_Find(makefiles, NULL, ReadAllMakefiles); 1168 if (ln != NULL) 1169 Fatal("%s: cannot open %s.", progname, 1170 (char *)Lst_Datum(ln)); 1171 } else { 1172 p1 = Var_Subst(NULL, "${" MAKEFILE_PREFERENCE "}", 1173 VAR_CMD, 0); 1174 if (p1) { 1175 (void)str2Lst_Append(makefiles, p1, NULL); 1176 (void)Lst_Find(makefiles, NULL, ReadMakefile); 1177 free(p1); 1178 } 1179 } 1180 1181 /* In particular suppress .depend for '-r -V .OBJDIR -f /dev/null' */ 1182 if (!noBuiltins || !printVars) { 1183 makeDependfile = Var_Subst(NULL, "${.MAKE.DEPENDFILE:T}", 1184 VAR_CMD, 0); 1185 doing_depend = TRUE; 1186 (void)ReadMakefile(makeDependfile, NULL); 1187 doing_depend = FALSE; 1188 } 1189 1190 MakeMode(NULL); 1191 1192 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); 1193 if (p1) 1194 free(p1); 1195 1196 if (!compatMake) 1197 Job_ServerStart(maxJobTokens, jp_0, jp_1); 1198 if (DEBUG(JOB)) 1199 fprintf(debug_file, "job_pipe %d %d, maxjobs %d, tokens %d, compat %d\n", 1200 jp_0, jp_1, maxJobs, maxJobTokens, compatMake); 1201 1202 Main_ExportMAKEFLAGS(TRUE); /* initial export */ 1203
|