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