1 /* $OpenPackages$ */ 2 /* $OpenBSD: main.c,v 1.92 2008/11/04 07:22:35 espie Exp $ */ 3 /* $NetBSD: main.c,v 1.34 1997/03/24 20:56:36 gwr Exp $ */ 4 5 /* 6 * Copyright (c) 1988, 1989, 1990, 1993 7 * The Regents of the University of California. All rights reserved. 8 * Copyright (c) 1989 by Berkeley Softworks 9 * All rights reserved. 10 * 11 * This code is derived from software contributed to Berkeley by 12 * Adam de Boor. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 #ifndef MAKE_BOOTSTRAP 43 #include <sys/utsname.h> 44 #endif 45 #include <errno.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include "config.h" 51 #include "defines.h" 52 #include "var.h" 53 #include "parse.h" 54 #include "parsevar.h" 55 #include "dir.h" 56 #include "direxpand.h" 57 #include "error.h" 58 #include "pathnames.h" 59 #include "init.h" 60 #include "job.h" 61 #include "compat.h" 62 #include "targ.h" 63 #include "suff.h" 64 #include "str.h" 65 #include "main.h" 66 #include "lst.h" 67 #include "memory.h" 68 #include "make.h" 69 70 #ifndef PATH_MAX 71 # ifdef MAXPATHLEN 72 # define PATH_MAX (MAXPATHLEN+1) 73 # else 74 # define PATH_MAX 1024 75 # endif 76 #endif 77 78 79 #define MAKEFLAGS ".MAKEFLAGS" 80 81 static LIST to_create; /* Targets to be made */ 82 Lst create = &to_create; 83 bool allPrecious; /* .PRECIOUS given on line by itself */ 84 85 static bool noBuiltins; /* -r flag */ 86 static LIST makefiles; /* ordered list of makefiles to read */ 87 static LIST varstoprint; /* list of variables to print */ 88 int maxJobs; /* -j argument */ 89 bool compatMake; /* -B argument */ 90 static bool forceJobs = false; 91 int debug; /* -d flag */ 92 bool noExecute; /* -n flag */ 93 bool keepgoing; /* -k flag */ 94 bool queryFlag; /* -q flag */ 95 bool touchFlag; /* -t flag */ 96 bool ignoreErrors; /* -i flag */ 97 bool beSilent; /* -s flag */ 98 99 struct dirs { 100 char *current; 101 char *object; 102 }; 103 104 static void MainParseArgs(int, char **); 105 static void add_dirpath(Lst, const char *); 106 static void usage(void); 107 static void posixParseOptLetter(int); 108 static void record_option(int, const char *); 109 110 static char *figure_out_MACHINE(void); 111 static char *figure_out_MACHINE_ARCH(void); 112 static void no_fd_limits(void); 113 114 static char *chdir_verify_path(const char *, struct dirs *); 115 static char *concat_verify(const char *, const char *, char, struct dirs *); 116 static char *figure_out_CURDIR(void); 117 static void setup_CURDIR_OBJDIR(struct dirs *, const char *); 118 119 static void setup_VPATH(void); 120 121 static void read_all_make_rules(bool, bool, Lst, struct dirs *); 122 static void read_makefile_list(Lst, struct dirs *); 123 static int ReadMakefile(void *, void *); 124 125 126 static void record_option(int c, const char *arg) 127 { 128 char opt[3]; 129 130 opt[0] = '-'; 131 opt[1] = c; 132 opt[2] = '\0'; 133 Var_Append(MAKEFLAGS, opt); 134 if (arg != NULL) 135 Var_Append(MAKEFLAGS, arg); 136 } 137 138 static void 139 posixParseOptLetter(int c) 140 { 141 switch(c) { 142 case 'B': 143 compatMake = true; 144 return; /* XXX don't pass to submakes. */ 145 case 'P': 146 break; /* old option */ 147 case 'S': 148 keepgoing = false; 149 break; 150 case 'e': 151 Var_setCheckEnvFirst(true); 152 break; 153 case 'i': 154 ignoreErrors = true; 155 break; 156 case 'k': 157 keepgoing = true; 158 break; 159 case 'n': 160 noExecute = true; 161 break; 162 case 'q': 163 queryFlag = true; 164 /* Kind of nonsensical, wot? */ 165 break; 166 case 'r': 167 noBuiltins = true; 168 break; 169 case 's': 170 beSilent = true; 171 break; 172 case 't': 173 touchFlag = true; 174 break; 175 default: 176 case '?': 177 usage(); 178 } 179 record_option(c, NULL); 180 } 181 182 /*- 183 * MainParseArgs -- 184 * Parse a given argument vector. Called from main() and from 185 * Main_ParseArgLine() when the .MAKEFLAGS target is used. 186 * 187 * XXX: Deal with command line overriding .MAKEFLAGS in makefile 188 * 189 * Side Effects: 190 * Various global and local flags will be set depending on the flags 191 * given 192 */ 193 static void 194 MainParseArgs(int argc, char **argv) 195 { 196 int c, optend; 197 198 #define OPTFLAGS "BD:I:PSV:d:ef:ij:km:nqrst" 199 #define OPTLETTERS "BPSiknqrst" 200 201 optind = 1; /* since we're called more than once */ 202 optreset = 1; 203 optend = 0; 204 while (optind < argc) { 205 if (!optend && argv[optind][0] == '-') { 206 if (argv[optind][1] == '\0') 207 optind++; /* ignore "-" */ 208 else if (argv[optind][1] == '-' && 209 argv[optind][2] == '\0') { 210 optind++; /* ignore "--" */ 211 optend++; /* "--" denotes end of flags */ 212 } 213 } 214 c = optend ? -1 : getopt(argc, argv, OPTFLAGS); 215 switch (c) { 216 case 'D': 217 Var_Set(optarg, "1"); 218 record_option(c, optarg); 219 break; 220 case 'I': 221 Parse_AddIncludeDir(optarg); 222 record_option(c, optarg); 223 break; 224 case 'V': 225 Lst_AtEnd(&varstoprint, optarg); 226 record_option(c, optarg); 227 break; 228 case 'd': { 229 char *modules = optarg; 230 231 for (; *modules; ++modules) 232 switch (*modules) { 233 case 'A': 234 debug = ~0; 235 break; 236 case 'a': 237 debug |= DEBUG_ARCH; 238 break; 239 case 'c': 240 debug |= DEBUG_COND; 241 break; 242 case 'd': 243 debug |= DEBUG_DIR; 244 break; 245 case 'f': 246 debug |= DEBUG_FOR; 247 break; 248 case 'g': 249 if (modules[1] == '1') { 250 debug |= DEBUG_GRAPH1; 251 ++modules; 252 } 253 else if (modules[1] == '2') { 254 debug |= DEBUG_GRAPH2; 255 ++modules; 256 } 257 break; 258 case 'j': 259 debug |= DEBUG_JOB; 260 break; 261 case 'J': 262 debug |= DEBUG_JOBBANNER; 263 break; 264 case 'l': 265 debug |= DEBUG_LOUD; 266 break; 267 case 'm': 268 debug |= DEBUG_MAKE; 269 break; 270 case 'n': 271 debug |= DEBUG_NAME_MATCHING; 272 break; 273 case 'p': 274 debug |= DEBUG_PARALLEL; 275 break; 276 case 's': 277 debug |= DEBUG_SUFF; 278 break; 279 case 't': 280 debug |= DEBUG_TARG; 281 break; 282 case 'v': 283 debug |= DEBUG_VAR; 284 break; 285 default: 286 (void)fprintf(stderr, 287 "make: illegal argument to -d option -- %c\n", 288 *modules); 289 usage(); 290 } 291 record_option(c, optarg); 292 break; 293 } 294 case 'f': 295 Lst_AtEnd(&makefiles, optarg); 296 break; 297 case 'j': { 298 char *endptr; 299 300 forceJobs = true; 301 maxJobs = strtol(optarg, &endptr, 0); 302 if (endptr == optarg) { 303 fprintf(stderr, 304 "make: illegal argument to -j option -- %s -- not a number\n", 305 optarg); 306 usage(); 307 } 308 record_option(c, optarg); 309 break; 310 } 311 case 'm': 312 Dir_AddDir(systemIncludePath, optarg); 313 record_option(c, optarg); 314 break; 315 case -1: 316 /* Check for variable assignments and targets. */ 317 if (argv[optind] != NULL && 318 !Parse_CmdlineVar(argv[optind])) { 319 if (!*argv[optind]) 320 Punt("illegal (null) argument."); 321 Lst_AtEnd(create, estrdup(argv[optind])); 322 } 323 optind++; /* skip over non-option */ 324 break; 325 default: 326 posixParseOptLetter(c); 327 } 328 } 329 330 } 331 332 /*- 333 * Main_ParseArgLine -- 334 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target 335 * is encountered and by main() when reading the .MAKEFLAGS envariable. 336 * Takes a line of arguments and breaks it into its 337 * component words and passes those words and the number of them to the 338 * MainParseArgs function. 339 * The line should have all its leading whitespace removed. 340 * 341 * Side Effects: 342 * Only those that come from the various arguments. 343 */ 344 void 345 Main_ParseArgLine(const char *line) /* Line to fracture */ 346 { 347 char **argv; /* Manufactured argument vector */ 348 int argc; /* Number of arguments in argv */ 349 char *args; /* Space used by the args */ 350 char *buf; 351 char *argv0; 352 const char *s; 353 size_t len; 354 355 356 if (line == NULL) 357 return; 358 for (; *line == ' '; ++line) 359 continue; 360 if (!*line) 361 return; 362 363 /* POSIX rule: MAKEFLAGS can hold a set of option letters without 364 * any blanks or dashes. */ 365 for (s = line;; s++) { 366 if (*s == '\0') { 367 while (line != s) 368 posixParseOptLetter(*line++); 369 return; 370 } 371 if (strchr(OPTLETTERS, *s) == NULL) 372 break; 373 } 374 argv0 = Var_Value(".MAKE"); 375 len = strlen(line) + strlen(argv0) + 2; 376 buf = emalloc(len); 377 (void)snprintf(buf, len, "%s %s", argv0, line); 378 379 argv = brk_string(buf, &argc, &args); 380 free(buf); 381 MainParseArgs(argc, argv); 382 383 free(args); 384 free(argv); 385 } 386 387 /* Add a :-separated path to a Lst of directories. */ 388 static void 389 add_dirpath(Lst l, const char *n) 390 { 391 const char *start; 392 const char *cp; 393 394 for (start = n;;) { 395 for (cp = start; *cp != '\0' && *cp != ':';) 396 cp++; 397 Dir_AddDiri(l, start, cp); 398 if (*cp == '\0') 399 break; 400 else 401 start= cp+1; 402 } 403 } 404 405 /* 406 * Get the name of this type of MACHINE from utsname so we can share an 407 * executable for similar machines. (i.e. m68k: amiga hp300, mac68k, sun3, ...) 408 * 409 * Note that both MACHINE and MACHINE_ARCH are decided at 410 * run-time. 411 */ 412 static char * 413 figure_out_MACHINE() 414 { 415 char *r = getenv("MACHINE"); 416 if (r == NULL) { 417 #ifndef MAKE_BOOTSTRAP 418 static struct utsname utsname; 419 420 if (uname(&utsname) == -1) { 421 perror("make: uname"); 422 exit(2); 423 } 424 r = utsname.machine; 425 #else 426 r = MACHINE; 427 #endif 428 } 429 return r; 430 } 431 432 static char * 433 figure_out_MACHINE_ARCH() 434 { 435 char *r = getenv("MACHINE_ARCH"); 436 if (r == NULL) { 437 #ifndef MACHINE_ARCH 438 r = "unknown"; /* XXX: no uname -p yet */ 439 #else 440 r = MACHINE_ARCH; 441 #endif 442 } 443 return r; 444 } 445 446 /* get rid of resource limit on file descriptors */ 447 static void 448 no_fd_limits() 449 { 450 #ifdef RLIMIT_NOFILE 451 struct rlimit rl; 452 if (getrlimit(RLIMIT_NOFILE, &rl) != -1 && 453 rl.rlim_cur != rl.rlim_max) { 454 rl.rlim_cur = rl.rlim_max; 455 (void)setrlimit(RLIMIT_NOFILE, &rl); 456 } 457 #endif 458 } 459 460 static char * 461 figure_out_CURDIR() 462 { 463 char *dir, *cwd; 464 struct stat sa, sb; 465 466 /* curdir is cwd... */ 467 cwd = dogetcwd(); 468 if (cwd == NULL) { 469 (void)fprintf(stderr, "make: %s.\n", strerror(errno)); 470 exit(2); 471 } 472 473 if (stat(cwd, &sa) == -1) { 474 (void)fprintf(stderr, "make: %s: %s.\n", cwd, strerror(errno)); 475 exit(2); 476 } 477 478 /* ...but we can use the alias $PWD if we can prove it is the same 479 * directory */ 480 if ((dir = getenv("PWD")) != NULL) { 481 if (stat(dir, &sb) == 0 && sa.st_ino == sb.st_ino && 482 sa.st_dev == sb.st_dev) { 483 free(cwd); 484 return estrdup(dir); 485 } 486 } 487 488 return cwd; 489 } 490 491 static char * 492 chdir_verify_path(const char *path, struct dirs *d) 493 { 494 struct stat sb; 495 496 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { 497 if (chdir(path)) { 498 (void)fprintf(stderr, "make warning: %s: %s.\n", 499 path, strerror(errno)); 500 return NULL; 501 } else { 502 if (path[0] != '/') 503 return Str_concat(d->current, path, '/'); 504 else 505 return estrdup(path); 506 } 507 } 508 509 return NULL; 510 } 511 512 static char * 513 concat_verify(const char *p1, const char *p2, char c, struct dirs *d) 514 { 515 char *tmp = Str_concat(p1, p2, c); 516 char *result = chdir_verify_path(tmp, d); 517 free(tmp); 518 return result; 519 } 520 521 static void 522 setup_CURDIR_OBJDIR(struct dirs *d, const char *machine) 523 { 524 char *path, *prefix; 525 526 d->current = figure_out_CURDIR(); 527 d->object = NULL; 528 /* 529 * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory 530 * exists, change into it and build there. (If a .${MACHINE} suffix 531 * exists, use that directory instead). 532 * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default, 533 * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists. 534 * If all fails, use the current directory to build. 535 * 536 * Once things are initted, 537 * have to add the original directory to the search path, 538 * and modify the paths for the Makefiles appropriately. The 539 * current directory is also placed as a variable for make scripts. 540 */ 541 if ((prefix = getenv("MAKEOBJDIRPREFIX")) != NULL) { 542 d->object = concat_verify(prefix, d->current, 0, d); 543 } else if ((path = getenv("MAKEOBJDIR")) != NULL) { 544 d->object = chdir_verify_path(path, d); 545 } else { 546 path = _PATH_OBJDIR; 547 prefix = _PATH_OBJDIRPREFIX; 548 d->object = concat_verify(path, machine, '.', d); 549 if (!d->object) 550 d->object=chdir_verify_path(path, d); 551 if (!d->object) 552 d->object = concat_verify(prefix, d->current, 0, d); 553 } 554 if (d->object == NULL) 555 d->object = d->current; 556 } 557 558 #ifdef CLEANUP 559 static void 560 free_CURDIR_OBJDIR(struct dirs *d) 561 { 562 if (d->object != d->current) 563 free(d->object); 564 free(d->current); 565 } 566 #endif 567 568 569 /* 570 * if the VPATH variable is defined, add its contents to the search path. 571 * Uses the same format as the PATH env variable, i.e., 572 * <directory>:<directory>:<directory>... 573 */ 574 static void 575 setup_VPATH() 576 { 577 if (Var_Value("VPATH") != NULL) { 578 char *vpath; 579 580 vpath = Var_Subst("${VPATH}", NULL, false); 581 add_dirpath(defaultPath, vpath); 582 (void)free(vpath); 583 } 584 } 585 586 static void 587 read_makefile_list(Lst mk, struct dirs *d) 588 { 589 LstNode ln; 590 ln = Lst_Find(mk, ReadMakefile, d); 591 if (ln != NULL) 592 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); 593 } 594 595 static void 596 read_all_make_rules(bool noBuiltins, bool read_depend, 597 Lst makefiles, struct dirs *d) 598 { 599 /* 600 * Read in the built-in rules first, followed by the specified 601 * makefile(s), or the default BSDmakefile, Makefile or 602 * makefile, in that order. 603 */ 604 if (!noBuiltins) { 605 LIST sysMkPath; /* Path of sys.mk */ 606 607 Lst_Init(&sysMkPath); 608 Dir_Expand(_PATH_DEFSYSMK, systemIncludePath, &sysMkPath); 609 if (Lst_IsEmpty(&sysMkPath)) 610 Fatal("make: no system rules (%s).", _PATH_DEFSYSMK); 611 612 read_makefile_list(&sysMkPath, d); 613 #ifdef CLEANUP 614 Lst_Destroy(&sysMkPath, (SimpleProc)free); 615 #endif 616 } 617 618 if (!Lst_IsEmpty(makefiles)) { 619 read_makefile_list(makefiles, d); 620 } else if (!ReadMakefile("BSDmakefile", d)) 621 if (!ReadMakefile("makefile", d)) 622 (void)ReadMakefile("Makefile", d); 623 624 /* read a .depend file, if it exists, and we're not building depend */ 625 626 if (read_depend) 627 (void)ReadMakefile(".depend", d); 628 } 629 630 631 int main(int, char **); 632 /*- 633 * main -- 634 * The main function, for obvious reasons. Initializes variables 635 * and a few modules, then parses the arguments give it in the 636 * environment and on the command line. Reads the system makefile 637 * followed by either Makefile, makefile or the file given by the 638 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the 639 * flags it has received by then uses either the Make or the Compat 640 * module to create the initial list of targets. 641 * 642 * Results: 643 * If -q was given, exits -1 if anything was out-of-date. Else it exits 644 * 0. 645 * 646 * Side Effects: 647 * The program exits when done. Targets are created. etc. etc. etc. 648 */ 649 int 650 main(int argc, char **argv) 651 { 652 static LIST targs; /* target nodes to create */ 653 bool outOfDate = true; /* false if all targets up to date */ 654 char *machine = figure_out_MACHINE(); 655 char *machine_arch = figure_out_MACHINE_ARCH(); 656 const char *syspath = _PATH_DEFSYSPATH; 657 char *p; 658 static struct dirs d; 659 bool read_depend = true;/* false if we don't want to read .depend */ 660 661 no_fd_limits(); 662 setup_CURDIR_OBJDIR(&d, machine); 663 664 esetenv("PWD", d.object); 665 unsetenv("CDPATH"); 666 667 Static_Lst_Init(create); 668 Static_Lst_Init(&makefiles); 669 Static_Lst_Init(&varstoprint); 670 Static_Lst_Init(&targs); 671 672 beSilent = false; /* Print commands as executed */ 673 ignoreErrors = false; /* Pay attention to non-zero returns */ 674 noExecute = false; /* Execute all commands */ 675 keepgoing = false; /* Stop on error */ 676 allPrecious = false; /* Remove targets when interrupted */ 677 queryFlag = false; /* This is not just a check-run */ 678 noBuiltins = false; /* Read the built-in rules */ 679 touchFlag = false; /* Actually update targets */ 680 debug = 0; /* No debug verbosity, please. */ 681 682 maxJobs = DEFMAXJOBS; 683 compatMake = false; /* No compat mode */ 684 685 686 /* 687 * Initialize all external modules. 688 */ 689 Init(); 690 691 if (d.object != d.current) 692 Dir_AddDir(defaultPath, d.current); 693 Var_Set(".CURDIR", d.current); 694 Var_Set(".OBJDIR", d.object); 695 Targ_setdirs(d.current, d.object); 696 697 /* 698 * Initialize various variables. 699 * MAKE also gets this name, for compatibility 700 * .MAKEFLAGS gets set to the empty string just in case. 701 * MFLAGS also gets initialized empty, for compatibility. 702 */ 703 Var_Set("MAKE", argv[0]); 704 Var_Set(".MAKE", argv[0]); 705 Var_Set(MAKEFLAGS, ""); 706 Var_Set("MFLAGS", ""); 707 Var_Set("MACHINE", machine); 708 Var_Set("MACHINE_ARCH", machine_arch); 709 710 /* 711 * First snag any flags out of the MAKEFLAGS environment variable. 712 */ 713 Main_ParseArgLine(getenv("MAKEFLAGS")); 714 715 MainParseArgs(argc, argv); 716 717 /* 718 * Be compatible if user did not specify -j and did not explicitly 719 * turn compatibility on 720 */ 721 if (!compatMake && !forceJobs) 722 compatMake = true; 723 724 /* And set up everything for sub-makes */ 725 Var_AddCmdline(MAKEFLAGS); 726 727 728 /* 729 * Set up the .TARGETS variable to contain the list of targets to be 730 * created. If none specified, make the variable empty -- the parser 731 * will fill the thing in with the default or .MAIN target. 732 */ 733 if (!Lst_IsEmpty(create)) { 734 LstNode ln; 735 736 for (ln = Lst_First(create); ln != NULL; ln = Lst_Adv(ln)) { 737 char *name = (char *)Lst_Datum(ln); 738 739 if (strcmp(name, "depend") == 0) 740 read_depend = false; 741 742 Var_Append(".TARGETS", name); 743 } 744 } else 745 Var_Set(".TARGETS", ""); 746 747 748 /* 749 * If no user-supplied system path was given (through the -m option) 750 * add the directories from the DEFSYSPATH (more than one may be given 751 * as dir1:...:dirn) to the system include path. 752 */ 753 if (Lst_IsEmpty(systemIncludePath)) 754 add_dirpath(systemIncludePath, syspath); 755 756 read_all_make_rules(noBuiltins, read_depend, &makefiles, &d); 757 758 Var_Append("MFLAGS", Var_Value(MAKEFLAGS)); 759 760 /* Install all the flags into the MAKEFLAGS env variable. */ 761 if (((p = Var_Value(MAKEFLAGS)) != NULL) && *p) 762 esetenv("MAKEFLAGS", p); 763 764 setup_VPATH(); 765 766 process_suffixes_after_makefile_is_read(); 767 768 /* Print the initial graph, if the user requested it. */ 769 if (DEBUG(GRAPH1)) 770 Targ_PrintGraph(1); 771 772 /* Print the values of any variables requested by the user. */ 773 if (!Lst_IsEmpty(&varstoprint)) { 774 LstNode ln; 775 776 for (ln = Lst_First(&varstoprint); ln != NULL; 777 ln = Lst_Adv(ln)) { 778 char *value = Var_Value((char *)Lst_Datum(ln)); 779 780 printf("%s\n", value ? value : ""); 781 } 782 } else { 783 /* Have now read the entire graph and need to make a list 784 * of targets to create. If none was given on the command 785 * line, we consult the parsing module to find the main 786 * target(s) to create. */ 787 if (Lst_IsEmpty(create)) 788 Parse_MainName(&targs); 789 else 790 Targ_FindList(&targs, create); 791 792 if (compatMake) 793 /* Compat_Init will take care of creating all the 794 * targets as well as initializing the module. */ 795 Compat_Run(&targs); 796 else { 797 /* Initialize job module before traversing the graph, 798 * now that any .BEGIN and .END targets have been 799 * read. This is done only if the -q flag wasn't given 800 * (to prevent the .BEGIN from being executed should 801 * it exist). */ 802 if (!queryFlag) 803 Job_Init(maxJobs); 804 805 /* Traverse the graph, checking on all the targets. */ 806 outOfDate = Make_Run(&targs); 807 } 808 } 809 810 #ifdef CLEANUP 811 Lst_Destroy(&targs, NOFREE); 812 Lst_Destroy(&varstoprint, NOFREE); 813 Lst_Destroy(&makefiles, NOFREE); 814 Lst_Destroy(create, (SimpleProc)free); 815 #endif 816 817 /* print the graph now it's been processed if the user requested it */ 818 if (DEBUG(GRAPH2)) 819 Targ_PrintGraph(2); 820 821 #ifdef CLEANUP 822 free_CURDIR_OBJDIR(&d); 823 End(); 824 #endif 825 if (queryFlag && outOfDate) 826 return 1; 827 else 828 return 0; 829 } 830 831 /*- 832 * ReadMakefile -- 833 * Open and parse the given makefile. 834 * 835 * Results: 836 * true if ok. false if couldn't open file. 837 * 838 * Side Effects: 839 * lots 840 */ 841 static bool 842 ReadMakefile(void *p, void *q) 843 { 844 char *fname = (char *)p; /* makefile to read */ 845 struct dirs *d = (struct dirs *)q; 846 FILE *stream; 847 char *name; 848 849 if (!strcmp(fname, "-")) { 850 Var_Set("MAKEFILE", ""); 851 Parse_File(estrdup("(stdin)"), stdin); 852 } else { 853 if ((stream = fopen(fname, "r")) != NULL) 854 goto found; 855 /* if we've chdir'd, rebuild the path name */ 856 if (d->current != d->object && *fname != '/') { 857 char *path; 858 859 path = Str_concat(d->current, fname, '/'); 860 if ((stream = fopen(path, "r")) == NULL) 861 free(path); 862 else { 863 fname = path; 864 goto found; 865 } 866 } 867 /* look in -I and system include directories. */ 868 name = Dir_FindFile(fname, userIncludePath); 869 if (!name) 870 name = Dir_FindFile(fname, systemIncludePath); 871 if (!name || !(stream = fopen(name, "r"))) 872 return false; 873 fname = name; 874 /* 875 * set the MAKEFILE variable desired by System V fans -- the 876 * placement of the setting here means it gets set to the last 877 * makefile specified, as it is set by SysV make. 878 */ 879 found: Var_Set("MAKEFILE", fname); 880 Parse_File(fname, stream); 881 } 882 return true; 883 } 884 885 886 /* 887 * usage -- 888 * exit with usage message 889 */ 890 static void 891 usage() 892 { 893 (void)fprintf(stderr, 894 "usage: make [-BeiknPqrSst] [-D variable] [-d flags] [-f makefile]\n\ 895 [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\ 896 [NAME=value] [target ...]\n"); 897 exit(2); 898 } 899 900 901