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