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