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.24 (Berkeley) 02/02/91"; 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 <errno.h> 54 #include <fcntl.h> 55 #include <stdio.h> 56 #include <varargs.h> 57 #include "make.h" 58 #include "pathnames.h" 59 60 #ifndef DEFMAXLOCAL 61 #define DEFMAXLOCAL DEFMAXJOBS 62 #endif DEFMAXLOCAL 63 64 #define MAKEFLAGS ".MAKEFLAGS" 65 66 Lst create; /* Targets to be made */ 67 time_t now; /* Time at start of make */ 68 GNode *DEFAULT; /* .DEFAULT node */ 69 Boolean allPrecious; /* .PRECIOUS given on line by itself */ 70 71 static Boolean noBuiltins; /* -r flag */ 72 static Lst makefiles; /* ordered list of makefiles to read */ 73 int maxJobs; /* -J argument */ 74 static int maxLocal; /* -L argument */ 75 Boolean debug; /* -d flag */ 76 Boolean noExecute; /* -n flag */ 77 Boolean keepgoing; /* -k flag */ 78 Boolean queryFlag; /* -q flag */ 79 Boolean touchFlag; /* -t flag */ 80 Boolean usePipes; /* !-P flag */ 81 Boolean ignoreErrors; /* -i flag */ 82 Boolean beSilent; /* -s flag */ 83 Boolean oldVars; /* variable substitution style */ 84 Boolean checkEnvFirst; /* -e flag */ 85 static Boolean jobsRunning; /* TRUE if the jobs might be running */ 86 87 static Boolean ReadMakefile(); 88 89 static char *curdir; /* if chdir'd for an architecture */ 90 91 /*- 92 * MainParseArgs -- 93 * Parse a given argument vector. Called from main() and from 94 * Main_ParseArgLine() when the .MAKEFLAGS target is used. 95 * 96 * XXX: Deal with command line overriding .MAKEFLAGS in makefile 97 * 98 * Results: 99 * None 100 * 101 * Side Effects: 102 * Various global and local flags will be set depending on the flags 103 * given 104 */ 105 static void 106 MainParseArgs(argc, argv) 107 int argc; 108 char **argv; 109 { 110 extern int optind; 111 extern char *optarg; 112 register int i; 113 register char *cp; 114 char c; 115 116 optind = 1; /* since we're called more than once */ 117 rearg: while((c = getopt(argc, argv, "D:I:d:ef:ij:knqrst")) != EOF) { 118 switch(c) { 119 case 'D': 120 Var_Set(optarg, "1", VAR_GLOBAL); 121 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL); 122 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 123 break; 124 case 'I': 125 Parse_AddIncludeDir(optarg); 126 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); 127 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 128 break; 129 #ifdef notdef 130 case 'L': 131 maxLocal = atoi(optarg); 132 Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL); 133 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 134 break; 135 case 'P': 136 usePipes = FALSE; 137 Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL); 138 break; 139 case 'S': 140 keepgoing = FALSE; 141 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL); 142 break; 143 #endif 144 case 'd': { 145 char *modules = optarg; 146 147 for (; *modules; ++modules) 148 switch (*modules) { 149 case 'A': 150 debug = ~0; 151 break; 152 case 'a': 153 debug |= DEBUG_ARCH; 154 break; 155 case 'c': 156 debug |= DEBUG_COND; 157 break; 158 case 'd': 159 debug |= DEBUG_DIR; 160 break; 161 case 'g': 162 if (modules[1] == '1') { 163 debug |= DEBUG_GRAPH1; 164 ++modules; 165 } 166 else if (modules[1] == '2') { 167 debug |= DEBUG_GRAPH2; 168 ++modules; 169 } 170 break; 171 case 'j': 172 debug |= DEBUG_JOB; 173 break; 174 case 'm': 175 debug |= DEBUG_MAKE; 176 break; 177 case 's': 178 debug |= DEBUG_SUFF; 179 break; 180 case 't': 181 debug |= DEBUG_TARG; 182 break; 183 case 'v': 184 debug |= DEBUG_VAR; 185 break; 186 } 187 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL); 188 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 189 break; 190 } 191 case 'e': 192 checkEnvFirst = TRUE; 193 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL); 194 break; 195 case 'f': 196 (void)Lst_AtEnd(makefiles, (ClientData)optarg); 197 break; 198 case 'i': 199 ignoreErrors = TRUE; 200 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL); 201 break; 202 case 'j': 203 maxJobs = atoi(optarg); 204 Var_Append(MAKEFLAGS, "-J", VAR_GLOBAL); 205 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); 206 break; 207 case 'k': 208 keepgoing = TRUE; 209 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL); 210 break; 211 case 'n': 212 noExecute = TRUE; 213 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL); 214 break; 215 case 'q': 216 queryFlag = TRUE; 217 /* Kind of nonsensical, wot? */ 218 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL); 219 break; 220 case 'r': 221 noBuiltins = TRUE; 222 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL); 223 break; 224 case 's': 225 beSilent = TRUE; 226 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL); 227 break; 228 case 't': 229 touchFlag = TRUE; 230 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL); 231 break; 232 default: 233 case '?': 234 usage(); 235 } 236 } 237 238 oldVars = TRUE; 239 240 /* 241 * See if the rest of the arguments are variable assignments and 242 * perform them if so. Else take them to be targets and stuff them 243 * on the end of the "create" list. 244 */ 245 for (argv += optind, argc -= optind; *argv; ++argv, --argc) 246 if (Parse_IsVar(*argv)) 247 Parse_DoVar(*argv, VAR_CMD); 248 else { 249 if (!**argv) 250 Punt("illegal (null) argument."); 251 if (**argv == '-') { 252 optind = 0; 253 goto rearg; 254 } 255 (void)Lst_AtEnd(create, (ClientData)*argv); 256 } 257 } 258 259 /*- 260 * Main_ParseArgLine -- 261 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 262 * is encountered and by main() when reading the .MAKEFLAGS envariable. 263 * Takes a line of arguments and breaks it into its 264 * component words and passes those words and the number of them to the 265 * MainParseArgs function. 266 * The line should have all its leading whitespace removed. 267 * 268 * Results: 269 * None 270 * 271 * Side Effects: 272 * Only those that come from the various arguments. 273 */ 274 void 275 Main_ParseArgLine(line) 276 char *line; /* Line to fracture */ 277 { 278 char **argv; /* Manufactured argument vector */ 279 int argc; /* Number of arguments in argv */ 280 281 if (line == NULL) 282 return; 283 for (; *line == ' '; ++line); 284 if (!*line) 285 return; 286 287 argv = brk_string(line, &argc); 288 MainParseArgs(argc, argv); 289 } 290 291 /*- 292 * main -- 293 * The main function, for obvious reasons. Initializes variables 294 * and a few modules, then parses the arguments give it in the 295 * environment and on the command line. Reads the system makefile 296 * followed by either Makefile, makefile or the file given by the 297 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 298 * flags it has received by then uses either the Make or the Compat 299 * module to create the initial list of targets. 300 * 301 * Results: 302 * If -q was given, exits -1 if anything was out-of-date. Else it exits 303 * 0. 304 * 305 * Side Effects: 306 * The program exits when done. Targets are created. etc. etc. etc. 307 */ 308 main(argc, argv) 309 int argc; 310 char **argv; 311 { 312 Lst targs; /* target nodes to create -- passed to Make_Init */ 313 Boolean outOfDate; /* FALSE if all targets up to date */ 314 struct stat sb; 315 char *p, *path, *getenv(); 316 317 /* 318 * if the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory 319 * exists, change into it and build there. Once things are 320 * initted, have to add the original directory to the search path, 321 * and modify the paths for the Makefiles apropriately. The 322 * current directory is also placed as a variable for make scripts. 323 */ 324 if (!(path = getenv("MAKEOBJDIR"))) 325 path = _PATH_OBJDIR; 326 if (!lstat(path, &sb)) { 327 if (S_ISDIR(sb.st_mode)) 328 curdir = ".."; 329 else { 330 curdir = emalloc((u_int)MAXPATHLEN + 1); 331 if (!getwd(curdir)) { 332 (void)fprintf(stderr, "make: %s.\n", curdir); 333 exit(2); 334 } 335 } 336 if (chdir(path)) { 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 char *p, *malloc(); 735 736 if (!(p = malloc(len))) 737 enomem(); 738 return(p); 739 } 740 741 /* 742 * enomem -- 743 * die when out of memory. 744 */ 745 enomem() 746 { 747 (void)fprintf(stderr, "make: %s.\n", strerror(errno)); 748 exit(2); 749 } 750 751 /* 752 * usage -- 753 * exit with usage message 754 */ 755 usage() 756 { 757 (void)fprintf(stderr, 758 "usage: make [-eiknqrst] [-D variable] [-d flags] [-f makefile ]\n\t\ 759 [-I directory] [-j max_jobs] [variable=value]\n"); 760 exit(2); 761 } 762