1 /* 2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 3 * Copyright (c) 1988, 1989 by Adam de Boor 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 * %sccs.include.redist.c% 11 */ 12 13 #ifndef lint 14 char copyright[] = 15 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 16 All rights reserved.\n"; 17 #endif /* not lint */ 18 19 #ifndef lint 20 static char sccsid[] = "@(#)main.c 5.23 (Berkeley) 10/11/90"; 21 #endif /* not lint */ 22 23 /*- 24 * main.c -- 25 * The main file for this entire program. Exit routines etc 26 * reside here. 27 * 28 * Utility functions defined in this file: 29 * Main_ParseArgLine Takes a line of arguments, breaks them and 30 * treats them as if they were given when first 31 * invoked. Used by the parse module to implement 32 * the .MFLAGS target. 33 * 34 * Error Print a tagged error message. The global 35 * MAKE variable must have been defined. This 36 * takes a format string and two optional 37 * arguments for it. 38 * 39 * Fatal Print an error message and exit. Also takes 40 * a format string and two arguments. 41 * 42 * Punt Aborts all jobs and exits with a message. Also 43 * takes a format string and two arguments. 44 * 45 * Finish Finish things up by printing the number of 46 * errors which occured, as passed to it, and 47 * exiting. 48 */ 49 50 #include <sys/param.h> 51 #include <sys/signal.h> 52 #include <sys/stat.h> 53 #include <fcntl.h> 54 #include <stdio.h> 55 #include <varargs.h> 56 #include "make.h" 57 #include "pathnames.h" 58 59 #ifndef DEFMAXLOCAL 60 #define DEFMAXLOCAL DEFMAXJOBS 61 #endif DEFMAXLOCAL 62 63 #define MAKEFLAGS ".MAKEFLAGS" 64 65 Lst create; /* Targets to be made */ 66 time_t now; /* Time at start of make */ 67 GNode *DEFAULT; /* .DEFAULT node */ 68 Boolean allPrecious; /* .PRECIOUS given on line by itself */ 69 70 static Boolean noBuiltins; /* -r flag */ 71 static Lst makefiles; /* ordered list of makefiles to read */ 72 int maxJobs; /* -J argument */ 73 static int maxLocal; /* -L argument */ 74 Boolean debug; /* -d flag */ 75 Boolean noExecute; /* -n flag */ 76 Boolean keepgoing; /* -k flag */ 77 Boolean queryFlag; /* -q flag */ 78 Boolean touchFlag; /* -t flag */ 79 Boolean usePipes; /* !-P flag */ 80 Boolean ignoreErrors; /* -i flag */ 81 Boolean beSilent; /* -s flag */ 82 Boolean oldVars; /* variable substitution style */ 83 Boolean checkEnvFirst; /* -e flag */ 84 static Boolean jobsRunning; /* TRUE if the jobs might be running */ 85 86 static Boolean ReadMakefile(); 87 88 static char *curdir; /* if chdir'd for an architecture */ 89 90 /*- 91 * MainParseArgs -- 92 * Parse a given argument vector. Called from main() and from 93 * Main_ParseArgLine() when the .MAKEFLAGS target is used. 94 * 95 * XXX: Deal with command line overriding .MAKEFLAGS in makefile 96 * 97 * Results: 98 * None 99 * 100 * Side Effects: 101 * Various global and local flags will be set depending on the flags 102 * given 103 */ 104 static void 105 MainParseArgs(argc, argv) 106 int argc; 107 char **argv; 108 { 109 extern int optind; 110 extern char *optarg; 111 register int i; 112 register char *cp; 113 char c; 114 115 optind = 1; /* since we're called more than once */ 116 rearg: while((c = getopt(argc, argv, "D:I:d:ef:ij:knqrst")) != EOF) { 117 switch(c) { 118 case 'D': 119 Var_Set(optarg, "1", VAR_GLOBAL); 120 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); 121 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 122 break; 123 case 'I': 124 Parse_AddIncludeDir(optarg); 125 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); 126 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 127 break; 128 #ifdef notdef 129 case 'L': 130 maxLocal = atoi(optarg); 131 Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL); 132 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 133 break; 134 case 'P': 135 usePipes = FALSE; 136 Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL); 137 break; 138 case 'S': 139 keepgoing = FALSE; 140 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); 141 break; 142 #endif 143 case 'd': { 144 char *modules = optarg; 145 146 for (; *modules; ++modules) 147 switch (*modules) { 148 case 'A': 149 debug = ~0; 150 break; 151 case 'a': 152 debug |= DEBUG_ARCH; 153 break; 154 case 'c': 155 debug |= DEBUG_COND; 156 break; 157 case 'd': 158 debug |= DEBUG_DIR; 159 break; 160 case 'g': 161 if (modules[1] == '1') { 162 debug |= DEBUG_GRAPH1; 163 ++modules; 164 } 165 else if (modules[1] == '2') { 166 debug |= DEBUG_GRAPH2; 167 ++modules; 168 } 169 break; 170 case 'j': 171 debug |= DEBUG_JOB; 172 break; 173 case 'm': 174 debug |= DEBUG_MAKE; 175 break; 176 case 's': 177 debug |= DEBUG_SUFF; 178 break; 179 case 't': 180 debug |= DEBUG_TARG; 181 break; 182 case 'v': 183 debug |= DEBUG_VAR; 184 break; 185 } 186 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); 187 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 188 break; 189 } 190 case 'e': 191 checkEnvFirst = TRUE; 192 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); 193 break; 194 case 'f': 195 (void)Lst_AtEnd(makefiles, (ClientData)optarg); 196 break; 197 case 'i': 198 ignoreErrors = TRUE; 199 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); 200 break; 201 case 'j': 202 maxJobs = atoi(optarg); 203 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 204 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 205 break; 206 case 'k': 207 keepgoing = TRUE; 208 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); 209 break; 210 case 'n': 211 noExecute = TRUE; 212 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); 213 break; 214 case 'q': 215 queryFlag = TRUE; 216 /* Kind of nonsensical, wot? */ 217 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); 218 break; 219 case 'r': 220 noBuiltins = TRUE; 221 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); 222 break; 223 case 's': 224 beSilent = TRUE; 225 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); 226 break; 227 case 't': 228 touchFlag = TRUE; 229 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); 230 break; 231 default: 232 case '?': 233 usage(); 234 } 235 } 236 237 oldVars = TRUE; 238 239 /* 240 * See if the rest of the arguments are variable assignments and 241 * perform them if so. Else take them to be targets and stuff them 242 * on the end of the "create" list. 243 */ 244 for (argv += optind, argc -= optind; *argv; ++argv, --argc) 245 if (Parse_IsVar(*argv)) 246 Parse_DoVar(*argv, VAR_CMD); 247 else { 248 if (!**argv) 249 Punt("illegal (null) argument."); 250 if (**argv == '-') { 251 optind = 0; 252 goto rearg; 253 } 254 (void)Lst_AtEnd(create, (ClientData)*argv); 255 } 256 } 257 258 /*- 259 * Main_ParseArgLine -- 260 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 261 * is encountered and by main() when reading the .MAKEFLAGS envariable. 262 * Takes a line of arguments and breaks it into its 263 * component words and passes those words and the number of them to the 264 * MainParseArgs function. 265 * The line should have all its leading whitespace removed. 266 * 267 * Results: 268 * None 269 * 270 * Side Effects: 271 * Only those that come from the various arguments. 272 */ 273 void 274 Main_ParseArgLine(line) 275 char *line; /* Line to fracture */ 276 { 277 char **argv; /* Manufactured argument vector */ 278 int argc; /* Number of arguments in argv */ 279 280 if (line == NULL) 281 return; 282 for (; *line == ' '; ++line); 283 if (!*line) 284 return; 285 286 argv = brk_string(line, &argc); 287 MainParseArgs(argc, argv); 288 } 289 290 /*- 291 * main -- 292 * The main function, for obvious reasons. Initializes variables 293 * and a few modules, then parses the arguments give it in the 294 * environment and on the command line. Reads the system makefile 295 * followed by either Makefile, makefile or the file given by the 296 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 297 * flags it has received by then uses either the Make or the Compat 298 * module to create the initial list of targets. 299 * 300 * Results: 301 * If -q was given, exits -1 if anything was out-of-date. Else it exits 302 * 0. 303 * 304 * Side Effects: 305 * The program exits when done. Targets are created. etc. etc. etc. 306 */ 307 main(argc, argv) 308 int argc; 309 char **argv; 310 { 311 Lst targs; /* target nodes to create -- passed to Make_Init */ 312 Boolean outOfDate; /* FALSE if all targets up to date */ 313 struct stat sb; 314 char *p, *path, *getenv(); 315 316 /* 317 * if the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory 318 * exists, change into it and build there. Once things are 319 * initted, have to add the original directory to the search path, 320 * and modify the paths for the Makefiles apropriately. The 321 * current directory is also placed as a variable for make scripts. 322 */ 323 if (!(path = getenv("MAKEOBJDIR"))) 324 path = _PATH_OBJDIR; 325 if (!lstat(path, &sb)) { 326 if (S_ISDIR(sb.st_mode)) 327 curdir = ".."; 328 else { 329 curdir = emalloc((u_int)MAXPATHLEN + 1); 330 if (!getwd(curdir)) { 331 (void)fprintf(stderr, "make: %s.\n", curdir); 332 exit(2); 333 } 334 } 335 if (chdir(path)) { 336 extern int errno; 337 338 (void)fprintf(stderr, "make: %s: %s.\n", 339 path, strerror(errno)); 340 exit(2); 341 } 342 } 343 344 create = Lst_Init(FALSE); 345 makefiles = Lst_Init(FALSE); 346 beSilent = FALSE; /* Print commands as executed */ 347 ignoreErrors = FALSE; /* Pay attention to non-zero returns */ 348 noExecute = FALSE; /* Execute all commands */ 349 keepgoing = FALSE; /* Stop on error */ 350 allPrecious = FALSE; /* Remove targets when interrupted */ 351 queryFlag = FALSE; /* This is not just a check-run */ 352 noBuiltins = FALSE; /* Read the built-in rules */ 353 touchFlag = FALSE; /* Actually update targets */ 354 usePipes = TRUE; /* Catch child output in pipes */ 355 debug = 0; /* No debug verbosity, please. */ 356 jobsRunning = FALSE; 357 358 maxJobs = DEFMAXJOBS; /* Set default max concurrency */ 359 maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */ 360 361 /* 362 * Initialize the parsing, directory and variable modules to prepare 363 * for the reading of inclusion paths and variable settings on the 364 * command line 365 */ 366 Dir_Init(); /* Initialize directory structures so -I flags 367 * can be processed correctly */ 368 Parse_Init(); /* Need to initialize the paths of #include 369 * directories */ 370 Var_Init(); /* As well as the lists of variables for 371 * parsing arguments */ 372 373 if (curdir) { 374 Dir_AddDir(dirSearchPath, curdir); 375 Var_Set(".CURDIR", curdir, VAR_GLOBAL); 376 } else 377 Var_Set(".CURDIR", ".", VAR_GLOBAL); 378 379 /* 380 * Initialize various variables. 381 * MAKE also gets this name, for compatibility 382 * .MAKEFLAGS gets set to the empty string just in case. 383 * MFLAGS also gets initialized empty, for compatibility. 384 */ 385 Var_Set("MAKE", argv[0], VAR_GLOBAL); 386 Var_Set(MAKEFLAGS, "", VAR_GLOBAL); 387 Var_Set("MFLAGS", "", VAR_GLOBAL); 388 Var_Set("MACHINE", MACHINE, VAR_GLOBAL); 389 390 /* 391 * First snag any flags out of the MAKE environment variable. 392 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's 393 * in a different format). 394 */ 395 #ifdef POSIX 396 Main_ParseArgLine(getenv("MAKEFLAGS")); 397 #else 398 Main_ParseArgLine(getenv("MAKE")); 399 #endif 400 401 MainParseArgs(argc, argv); 402 403 /* 404 * Initialize archive, target and suffix modules in preparation for 405 * parsing the makefile(s) 406 */ 407 Arch_Init(); 408 Targ_Init(); 409 Suff_Init(); 410 411 DEFAULT = NILGNODE; 412 (void)time(&now); 413 414 /* 415 * Set up the .TARGETS variable to contain the list of targets to be 416 * created. If none specified, make the variable empty -- the parser 417 * will fill the thing in with the default or .MAIN target. 418 */ 419 if (!Lst_IsEmpty(create)) { 420 LstNode ln; 421 422 for (ln = Lst_First(create); ln != NILLNODE; 423 ln = Lst_Succ(ln)) { 424 char *name = (char *)Lst_Datum(ln); 425 426 Var_Append(".TARGETS", name, VAR_GLOBAL); 427 } 428 } else 429 Var_Set(".TARGETS", "", VAR_GLOBAL); 430 431 /* 432 * Read in the built-in rules first, followed by the specified makefile, 433 * if it was (makefile != (char *) NULL), or the default Makefile and 434 * makefile, in that order, if it wasn't. 435 */ 436 if (!noBuiltins && !ReadMakefile(_PATH_DEFSYSMK)) 437 Fatal("make: no system rules (%s).", _PATH_DEFSYSMK); 438 439 if (!Lst_IsEmpty(makefiles)) { 440 LstNode ln; 441 442 ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile); 443 if (ln != NILLNODE) 444 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 445 } else if (!ReadMakefile("makefile")) 446 (void)ReadMakefile("Makefile"); 447 448 (void)ReadMakefile(".depend"); 449 450 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL), VAR_GLOBAL); 451 452 /* Install all the flags into the MAKE envariable. */ 453 if ((p = Var_Value(MAKEFLAGS, VAR_GLOBAL)) && *p) 454 #ifdef POSIX 455 setenv("MAKEFLAGS", p, 1); 456 #else 457 setenv("MAKE", p, 1); 458 #endif 459 460 /* 461 * For compatibility, look at the directories in the VPATH variable 462 * and add them to the search path, if the variable is defined. The 463 * variable's value is in the same format as the PATH envariable, i.e. 464 * <directory>:<directory>:<directory>... 465 */ 466 if (Var_Exists("VPATH", VAR_CMD)) { 467 char *vpath, *path, *cp, savec; 468 /* 469 * GCC stores string constants in read-only memory, but 470 * Var_Subst will want to write this thing, so store it 471 * in an array 472 */ 473 static char VPATH[] = "${VPATH}"; 474 475 vpath = Var_Subst(VPATH, VAR_CMD, FALSE); 476 path = vpath; 477 do { 478 /* skip to end of directory */ 479 for (cp = path; *cp != ':' && *cp != '\0'; cp++); 480 /* Save terminator character so know when to stop */ 481 savec = *cp; 482 *cp = '\0'; 483 /* Add directory to search path */ 484 Dir_AddDir(dirSearchPath, path); 485 *cp = savec; 486 path = cp + 1; 487 } while (savec == ':'); 488 (void)free((Address)vpath); 489 } 490 491 /* 492 * Now that all search paths have been read for suffixes et al, it's 493 * time to add the default search path to their lists... 494 */ 495 Suff_DoPaths(); 496 497 /* print the initial graph, if the user requested it */ 498 if (DEBUG(GRAPH1)) 499 Targ_PrintGraph(1); 500 501 /* 502 * Have now read the entire graph and need to make a list of targets 503 * to create. If none was given on the command line, we consult the 504 * parsing module to find the main target(s) to create. 505 */ 506 if (Lst_IsEmpty(create)) 507 targs = Parse_MainName(); 508 else 509 targs = Targ_FindList(create, TARG_CREATE); 510 511 /* 512 * this was original amMake -- want to allow parallelism, so put this 513 * back in, eventually. 514 */ 515 if (0) { 516 /* 517 * Initialize job module before traversing the graph, now that 518 * any .BEGIN and .END targets have been read. This is done 519 * only if the -q flag wasn't given (to prevent the .BEGIN from 520 * being executed should it exist). 521 */ 522 if (!queryFlag) { 523 if (maxLocal == -1) 524 maxLocal = maxJobs; 525 Job_Init(maxJobs, maxLocal); 526 jobsRunning = TRUE; 527 } 528 529 /* Traverse the graph, checking on all the targets */ 530 outOfDate = Make_Run(targs); 531 } else 532 /* 533 * Compat_Init will take care of creating all the targets as 534 * well as initializing the module. 535 */ 536 Compat_Run(targs); 537 538 /* print the graph now it's been processed if the user requested it */ 539 if (DEBUG(GRAPH2)) 540 Targ_PrintGraph(2); 541 542 if (queryFlag && outOfDate) 543 exit(1); 544 else 545 exit(0); 546 } 547 548 /*- 549 * ReadMakefile -- 550 * Open and parse the given makefile. 551 * 552 * Results: 553 * TRUE if ok. FALSE if couldn't open file. 554 * 555 * Side Effects: 556 * lots 557 */ 558 static Boolean 559 ReadMakefile(fname) 560 char *fname; /* makefile to read */ 561 { 562 extern Lst parseIncPath, sysIncPath; 563 FILE *stream; 564 char *name, path[MAXPATHLEN + 1]; 565 566 if (!strcmp(fname, "-")) { 567 Parse_File("(stdin)", stdin); 568 Var_Set("MAKEFILE", "", VAR_GLOBAL); 569 } else { 570 if (stream = fopen(fname, "r")) 571 goto found; 572 /* if we've chdir'd, rebuild the path name */ 573 if (curdir && *fname != '/') { 574 (void)sprintf(path, "%s/%s", curdir, fname); 575 if (stream = fopen(path, "r")) { 576 fname = path; 577 goto found; 578 } 579 } 580 /* look in -I and system include directories. */ 581 name = Dir_FindFile(fname, parseIncPath); 582 if (!name) 583 name = Dir_FindFile(fname, sysIncPath); 584 if (!name || !(stream = fopen(name, "r"))) 585 return(FALSE); 586 fname = name; 587 /* 588 * set the MAKEFILE variable desired by System V fans -- the 589 * placement of the setting here means it gets set to the last 590 * makefile specified, as it is set by SysV make. 591 */ 592 found: Var_Set("MAKEFILE", fname, VAR_GLOBAL); 593 Parse_File(fname, stream); 594 (void)fclose(stream); 595 } 596 return(TRUE); 597 } 598 599 /*- 600 * Error -- 601 * Print an error message given its format. 602 * 603 * Results: 604 * None. 605 * 606 * Side Effects: 607 * The message is printed. 608 */ 609 /* VARARGS */ 610 void 611 Error(va_alist) 612 va_dcl 613 { 614 va_list ap; 615 char *fmt; 616 617 va_start(ap); 618 fmt = va_arg(ap, char *); 619 (void)vfprintf(stderr, fmt, ap); 620 va_end(ap); 621 (void)fprintf(stderr, "\n"); 622 (void)fflush(stderr); 623 } 624 625 /*- 626 * Fatal -- 627 * Produce a Fatal error message. If jobs are running, waits for them 628 * to finish. 629 * 630 * Results: 631 * None 632 * 633 * Side Effects: 634 * The program exits 635 */ 636 /* VARARGS */ 637 void 638 Fatal(va_alist) 639 va_dcl 640 { 641 va_list ap; 642 char *fmt; 643 644 if (jobsRunning) 645 Job_Wait(); 646 647 va_start(ap); 648 fmt = va_arg(ap, char *); 649 (void)vfprintf(stderr, fmt, ap); 650 va_end(ap); 651 (void)fprintf(stderr, "\n"); 652 (void)fflush(stderr); 653 654 if (DEBUG(GRAPH2)) 655 Targ_PrintGraph(2); 656 exit(2); /* Not 1 so -q can distinguish error */ 657 } 658 659 /* 660 * Punt -- 661 * Major exception once jobs are being created. Kills all jobs, prints 662 * a message and exits. 663 * 664 * Results: 665 * None 666 * 667 * Side Effects: 668 * All children are killed indiscriminately and the program Lib_Exits 669 */ 670 /* VARARGS */ 671 void 672 Punt(va_alist) 673 va_dcl 674 { 675 va_list ap; 676 char *fmt; 677 678 (void)fprintf(stderr, "make: "); 679 va_start(ap); 680 fmt = va_arg(ap, char *); 681 (void)vfprintf(stderr, fmt, ap); 682 va_end(ap); 683 (void)fprintf(stderr, "\n"); 684 (void)fflush(stderr); 685 686 DieHorribly(); 687 } 688 689 /*- 690 * DieHorribly -- 691 * Exit without giving a message. 692 * 693 * Results: 694 * None 695 * 696 * Side Effects: 697 * A big one... 698 */ 699 void 700 DieHorribly() 701 { 702 if (jobsRunning) 703 Job_AbortAll(); 704 if (DEBUG(GRAPH2)) 705 Targ_PrintGraph(2); 706 exit(2); /* Not 1, so -q can distinguish error */ 707 } 708 709 /* 710 * Finish -- 711 * Called when aborting due to errors in child shell to signal 712 * abnormal exit. 713 * 714 * Results: 715 * None 716 * 717 * Side Effects: 718 * The program exits 719 */ 720 void 721 Finish(errors) 722 int errors; /* number of errors encountered in Make_Make */ 723 { 724 Fatal("%d error%s", errors, errors == 1 ? "" : "s"); 725 } 726 727 /* 728 * emalloc -- 729 * malloc, but die on error. 730 */ 731 char * 732 emalloc(len) 733 u_int len; 734 { 735 extern int errno; 736 char *p, *malloc(); 737 738 if (!(p = malloc(len))) 739 enomem(); 740 return(p); 741 } 742 743 /* 744 * enomem -- 745 * die when out of memory. 746 */ 747 enomem() 748 { 749 (void)fprintf(stderr, "make: %s.\n", strerror(errno)); 750 exit(2); 751 } 752 753 /* 754 * usage -- 755 * exit with usage message 756 */ 757 usage() 758 { 759 (void)fprintf(stderr, 760 "usage: make [-eiknqrst] [-D variable] [-d flags] [-f makefile ]\n\t\ 761 [-I directory] [-j max_jobs] [variable=value]\n"); 762 exit(2); 763 } 764