main.c revision 1.14 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 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. 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 #ifndef lint
40 char copyright[] =
41 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
42 All rights reserved.\n";
43 #endif /* not lint */
44
45 #ifndef lint
46 /* from: static char sccsid[] = "@(#)main.c 5.25 (Berkeley) 4/1/91"; */
47 static char *rcsid = "$Id: main.c,v 1.14 1994/06/06 22:45:33 jtc Exp $";
48 #endif /* not lint */
49
50 /*-
51 * main.c --
52 * The main file for this entire program. Exit routines etc
53 * reside here.
54 *
55 * Utility functions defined in this file:
56 * Main_ParseArgLine Takes a line of arguments, breaks them and
57 * treats them as if they were given when first
58 * invoked. Used by the parse module to implement
59 * the .MFLAGS target.
60 *
61 * Error Print a tagged error message. The global
62 * MAKE variable must have been defined. This
63 * takes a format string and two optional
64 * arguments for it.
65 *
66 * Fatal Print an error message and exit. Also takes
67 * a format string and two arguments.
68 *
69 * Punt Aborts all jobs and exits with a message. Also
70 * takes a format string and two arguments.
71 *
72 * Finish Finish things up by printing the number of
73 * errors which occured, as passed to it, and
74 * exiting.
75 */
76
77 #include <sys/types.h>
78 #include <sys/time.h>
79 #include <sys/param.h>
80 #include <sys/resource.h>
81 #include <sys/signal.h>
82 #include <sys/stat.h>
83 #include <errno.h>
84 #include <fcntl.h>
85 #include <stdio.h>
86 #if __STDC__
87 #include <stdarg.h>
88 #else
89 #include <varargs.h>
90 #endif
91 #include "make.h"
92 #include "hash.h"
93 #include "dir.h"
94 #include "job.h"
95 #include "pathnames.h"
96
97 #ifndef DEFMAXLOCAL
98 #define DEFMAXLOCAL DEFMAXJOBS
99 #endif DEFMAXLOCAL
100
101 #define MAKEFLAGS ".MAKEFLAGS"
102
103 Lst create; /* Targets to be made */
104 time_t now; /* Time at start of make */
105 GNode *DEFAULT; /* .DEFAULT node */
106 Boolean allPrecious; /* .PRECIOUS given on line by itself */
107
108 static Boolean noBuiltins; /* -r flag */
109 static Lst makefiles; /* ordered list of makefiles to read */
110 int maxJobs; /* -J argument */
111 static int maxLocal; /* -L argument */
112 Boolean compatMake; /* -B argument */
113 Boolean debug; /* -d flag */
114 Boolean noExecute; /* -n flag */
115 Boolean keepgoing; /* -k flag */
116 Boolean queryFlag; /* -q flag */
117 Boolean touchFlag; /* -t flag */
118 Boolean usePipes; /* !-P flag */
119 Boolean ignoreErrors; /* -i flag */
120 Boolean beSilent; /* -s flag */
121 Boolean oldVars; /* variable substitution style */
122 Boolean checkEnvFirst; /* -e flag */
123 static Boolean jobsRunning; /* TRUE if the jobs might be running */
124
125 static Boolean ReadMakefile();
126 static void usage();
127
128 static char *curdir; /* startup directory */
129 static char *objdir; /* where we chdir'ed to */
130
131 /*-
132 * MainParseArgs --
133 * Parse a given argument vector. Called from main() and from
134 * Main_ParseArgLine() when the .MAKEFLAGS target is used.
135 *
136 * XXX: Deal with command line overriding .MAKEFLAGS in makefile
137 *
138 * Results:
139 * None
140 *
141 * Side Effects:
142 * Various global and local flags will be set depending on the flags
143 * given
144 */
145 static void
146 MainParseArgs(argc, argv)
147 int argc;
148 char **argv;
149 {
150 extern int optind;
151 extern char *optarg;
152 char c;
153
154 optind = 1; /* since we're called more than once */
155 #ifdef notyet
156 # define OPTFLAGS "BD:I:L:PSd:ef:ij:knqrst"
157 #else
158 # define OPTFLAGS "D:I:d:ef:ij:knqrst"
159 #endif
160 rearg: while((c = getopt(argc, argv, OPTFLAGS)) != EOF) {
161 switch(c) {
162 case 'D':
163 Var_Set(optarg, "1", VAR_GLOBAL);
164 Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
165 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
166 break;
167 case 'I':
168 Parse_AddIncludeDir(optarg);
169 Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL);
170 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
171 break;
172 #ifdef notyet
173 case 'B':
174 compatMake = TRUE;
175 break;
176 case 'L':
177 maxLocal = atoi(optarg);
178 Var_Append(MAKEFLAGS, "-L", VAR_GLOBAL);
179 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
180 break;
181 case 'P':
182 usePipes = FALSE;
183 Var_Append(MAKEFLAGS, "-P", VAR_GLOBAL);
184 break;
185 case 'S':
186 keepgoing = FALSE;
187 Var_Append(MAKEFLAGS, "-S", VAR_GLOBAL);
188 break;
189 #endif
190 case 'd': {
191 char *modules = optarg;
192
193 for (; *modules; ++modules)
194 switch (*modules) {
195 case 'A':
196 debug = ~0;
197 break;
198 case 'a':
199 debug |= DEBUG_ARCH;
200 break;
201 case 'c':
202 debug |= DEBUG_COND;
203 break;
204 case 'd':
205 debug |= DEBUG_DIR;
206 break;
207 case 'f':
208 debug |= DEBUG_FOR;
209 break;
210 case 'g':
211 if (modules[1] == '1') {
212 debug |= DEBUG_GRAPH1;
213 ++modules;
214 }
215 else if (modules[1] == '2') {
216 debug |= DEBUG_GRAPH2;
217 ++modules;
218 }
219 break;
220 case 'j':
221 debug |= DEBUG_JOB;
222 break;
223 case 'm':
224 debug |= DEBUG_MAKE;
225 break;
226 case 's':
227 debug |= DEBUG_SUFF;
228 break;
229 case 't':
230 debug |= DEBUG_TARG;
231 break;
232 case 'v':
233 debug |= DEBUG_VAR;
234 break;
235 default:
236 (void)fprintf(stderr,
237 "make: illegal argument to d option -- %c\n",
238 *modules);
239 usage();
240 }
241 Var_Append(MAKEFLAGS, "-d", VAR_GLOBAL);
242 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
243 break;
244 }
245 case 'e':
246 checkEnvFirst = TRUE;
247 Var_Append(MAKEFLAGS, "-e", VAR_GLOBAL);
248 break;
249 case 'f':
250 (void)Lst_AtEnd(makefiles, (ClientData)optarg);
251 break;
252 case 'i':
253 ignoreErrors = TRUE;
254 Var_Append(MAKEFLAGS, "-i", VAR_GLOBAL);
255 break;
256 case 'j':
257 maxJobs = atoi(optarg);
258 Var_Append(MAKEFLAGS, "-j", VAR_GLOBAL);
259 Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL);
260 break;
261 case 'k':
262 keepgoing = TRUE;
263 Var_Append(MAKEFLAGS, "-k", VAR_GLOBAL);
264 break;
265 case 'n':
266 noExecute = TRUE;
267 Var_Append(MAKEFLAGS, "-n", VAR_GLOBAL);
268 break;
269 case 'q':
270 queryFlag = TRUE;
271 /* Kind of nonsensical, wot? */
272 Var_Append(MAKEFLAGS, "-q", VAR_GLOBAL);
273 break;
274 case 'r':
275 noBuiltins = TRUE;
276 Var_Append(MAKEFLAGS, "-r", VAR_GLOBAL);
277 break;
278 case 's':
279 beSilent = TRUE;
280 Var_Append(MAKEFLAGS, "-s", VAR_GLOBAL);
281 break;
282 case 't':
283 touchFlag = TRUE;
284 Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
285 break;
286 default:
287 case '?':
288 usage();
289 }
290 }
291
292 oldVars = TRUE;
293
294 /*
295 * See if the rest of the arguments are variable assignments and
296 * perform them if so. Else take them to be targets and stuff them
297 * on the end of the "create" list.
298 */
299 for (argv += optind, argc -= optind; *argv; ++argv, --argc)
300 if (Parse_IsVar(*argv))
301 Parse_DoVar(*argv, VAR_CMD);
302 else {
303 if (!**argv)
304 Punt("illegal (null) argument.");
305 if (**argv == '-') {
306 if ((*argv)[1])
307 optind = 0; /* -flag... */
308 else
309 optind = 1; /* - */
310 goto rearg;
311 }
312 (void)Lst_AtEnd(create, (ClientData)strdup(*argv));
313 }
314 }
315
316 /*-
317 * Main_ParseArgLine --
318 * Used by the parse module when a .MFLAGS or .MAKEFLAGS target
319 * is encountered and by main() when reading the .MAKEFLAGS envariable.
320 * Takes a line of arguments and breaks it into its
321 * component words and passes those words and the number of them to the
322 * MainParseArgs function.
323 * The line should have all its leading whitespace removed.
324 *
325 * Results:
326 * None
327 *
328 * Side Effects:
329 * Only those that come from the various arguments.
330 */
331 void
332 Main_ParseArgLine(line)
333 char *line; /* Line to fracture */
334 {
335 char **argv; /* Manufactured argument vector */
336 int argc; /* Number of arguments in argv */
337 char *p1;
338
339 if (line == NULL)
340 return;
341 for (; *line == ' '; ++line)
342 continue;
343 if (!*line)
344 return;
345
346 argv = brk_string(line, &argc);
347 MainParseArgs(argc, argv);
348 }
349
350 /*-
351 * main --
352 * The main function, for obvious reasons. Initializes variables
353 * and a few modules, then parses the arguments give it in the
354 * environment and on the command line. Reads the system makefile
355 * followed by either Makefile, makefile or the file given by the
356 * -f argument. Sets the .MAKEFLAGS PMake variable based on all the
357 * flags it has received by then uses either the Make or the Compat
358 * module to create the initial list of targets.
359 *
360 * Results:
361 * If -q was given, exits -1 if anything was out-of-date. Else it exits
362 * 0.
363 *
364 * Side Effects:
365 * The program exits when done. Targets are created. etc. etc. etc.
366 */
367 int
368 main(argc, argv)
369 int argc;
370 char **argv;
371 {
372 Lst targs; /* target nodes to create -- passed to Make_Init */
373 Boolean outOfDate = TRUE; /* FALSE if all targets up to date */
374 struct stat sb, sa;
375 char *p, *p1, *path, *pwd, *getenv(), *getwd();
376 char mdpath[MAXPATHLEN + 1];
377 char obpath[MAXPATHLEN + 1];
378 char cdpath[MAXPATHLEN + 1];
379
380 /*
381 * Find where we are and take care of PWD for the automounter...
382 * All this code is so that we know where we are when we start up
383 * on a different machine with pmake.
384 */
385 curdir = cdpath;
386 if (getwd(curdir) == NULL) {
387 (void)fprintf(stderr, "make: %s.\n", curdir);
388 exit(2);
389 }
390
391 if (stat(curdir, &sa) == -1) {
392 (void)fprintf(stderr, "make: %s: %s.\n",
393 curdir, strerror(errno));
394 exit(2);
395 }
396
397 if ((pwd = getenv("PWD")) != NULL) {
398 if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
399 sa.st_dev == sb.st_dev)
400 (void) strcpy(curdir, pwd);
401 }
402
403
404 /*
405 * if the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
406 * exists, change into it and build there. Once things are
407 * initted, have to add the original directory to the search path,
408 * and modify the paths for the Makefiles apropriately. The
409 * current directory is also placed as a variable for make scripts.
410 */
411 if (!(path = getenv("MAKEOBJDIR"))) {
412 path = _PATH_OBJDIR;
413 (void) sprintf(mdpath, "%s.%s", path, MACHINE);
414 }
415 else
416 (void) strncpy(mdpath, path, MAXPATHLEN + 1);
417
418 if (stat(mdpath, &sb) == 0 && S_ISDIR(sb.st_mode)) {
419
420 if (chdir(mdpath)) {
421 (void)fprintf(stderr, "make warning: %s: %s.\n",
422 mdpath, strerror(errno));
423 objdir = curdir;
424 }
425 else {
426 if (mdpath[0] != '/') {
427 (void) sprintf(obpath, "%s/%s", curdir, mdpath);
428 objdir = obpath;
429 }
430 else
431 objdir = mdpath;
432 }
433 }
434 else {
435 if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) {
436
437 if (chdir(path)) {
438 (void)fprintf(stderr, "make warning: %s: %s.\n",
439 path, strerror(errno));
440 objdir = curdir;
441 }
442 else {
443 if (path[0] != '/') {
444 (void) sprintf(obpath, "%s/%s", curdir,
445 path);
446 objdir = obpath;
447 }
448 else
449 objdir = obpath;
450 }
451 }
452 else
453 objdir = curdir;
454 }
455
456 setenv("PWD", objdir, 1);
457
458 create = Lst_Init(FALSE);
459 makefiles = Lst_Init(FALSE);
460 beSilent = FALSE; /* Print commands as executed */
461 ignoreErrors = FALSE; /* Pay attention to non-zero returns */
462 noExecute = FALSE; /* Execute all commands */
463 keepgoing = FALSE; /* Stop on error */
464 allPrecious = FALSE; /* Remove targets when interrupted */
465 queryFlag = FALSE; /* This is not just a check-run */
466 noBuiltins = FALSE; /* Read the built-in rules */
467 touchFlag = FALSE; /* Actually update targets */
468 usePipes = TRUE; /* Catch child output in pipes */
469 debug = 0; /* No debug verbosity, please. */
470 jobsRunning = FALSE;
471
472 maxJobs = DEFMAXJOBS; /* Set default max concurrency */
473 maxLocal = DEFMAXLOCAL; /* Set default local max concurrency */
474 #ifdef notyet
475 compatMake = FALSE; /* No compat mode */
476 #else
477 compatMake = TRUE; /* No compat mode */
478 #endif
479
480
481 /*
482 * Initialize the parsing, directory and variable modules to prepare
483 * for the reading of inclusion paths and variable settings on the
484 * command line
485 */
486 Dir_Init(); /* Initialize directory structures so -I flags
487 * can be processed correctly */
488 Parse_Init(); /* Need to initialize the paths of #include
489 * directories */
490 Var_Init(); /* As well as the lists of variables for
491 * parsing arguments */
492 str_init();
493 if (objdir != curdir)
494 Dir_AddDir(dirSearchPath, curdir);
495 Var_Set(".CURDIR", curdir, VAR_GLOBAL);
496 Var_Set(".OBJDIR", objdir, VAR_GLOBAL);
497
498 /*
499 * Initialize various variables.
500 * MAKE also gets this name, for compatibility
501 * .MAKEFLAGS gets set to the empty string just in case.
502 * MFLAGS also gets initialized empty, for compatibility.
503 */
504 Var_Set("MAKE", argv[0], VAR_GLOBAL);
505 Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
506 Var_Set("MFLAGS", "", VAR_GLOBAL);
507 #ifdef MACHINE
508 Var_Set("MACHINE", MACHINE, VAR_GLOBAL);
509 #endif
510 #ifdef MACHINE_ARCH
511 Var_Set("MACHINE_ARCH", MACHINE_ARCH, VAR_GLOBAL);
512 #endif
513
514 /*
515 * First snag any flags out of the MAKE environment variable.
516 * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
517 * in a different format).
518 */
519 #ifdef POSIX
520 Main_ParseArgLine(getenv("MAKEFLAGS"));
521 #else
522 Main_ParseArgLine(getenv("MAKE"));
523 #endif
524
525 MainParseArgs(argc, argv);
526
527 /*
528 * Initialize archive, target and suffix modules in preparation for
529 * parsing the makefile(s)
530 */
531 Arch_Init();
532 Targ_Init();
533 Suff_Init();
534
535 DEFAULT = NILGNODE;
536 (void)time(&now);
537
538 /*
539 * Set up the .TARGETS variable to contain the list of targets to be
540 * created. If none specified, make the variable empty -- the parser
541 * will fill the thing in with the default or .MAIN target.
542 */
543 if (!Lst_IsEmpty(create)) {
544 LstNode ln;
545
546 for (ln = Lst_First(create); ln != NILLNODE;
547 ln = Lst_Succ(ln)) {
548 char *name = (char *)Lst_Datum(ln);
549
550 Var_Append(".TARGETS", name, VAR_GLOBAL);
551 }
552 } else
553 Var_Set(".TARGETS", "", VAR_GLOBAL);
554
555 /*
556 * Read in the built-in rules first, followed by the specified makefile,
557 * if it was (makefile != (char *) NULL), or the default Makefile and
558 * makefile, in that order, if it wasn't.
559 */
560 if (!noBuiltins && !ReadMakefile(_PATH_DEFSYSMK))
561 Fatal("make: no system rules (%s).", _PATH_DEFSYSMK);
562
563 if (!Lst_IsEmpty(makefiles)) {
564 LstNode ln;
565
566 ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile);
567 if (ln != NILLNODE)
568 Fatal("make: cannot open %s.", (char *)Lst_Datum(ln));
569 } else if (!ReadMakefile("makefile"))
570 (void)ReadMakefile("Makefile");
571
572 (void)ReadMakefile(".depend");
573
574 Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL);
575 if (p1)
576 free(p1);
577
578 /* Install all the flags into the MAKE envariable. */
579 if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p)
580 #ifdef POSIX
581 setenv("MAKEFLAGS", p, 1);
582 #else
583 setenv("MAKE", p, 1);
584 #endif
585 if (p1)
586 free(p1);
587
588 /*
589 * For compatibility, look at the directories in the VPATH variable
590 * and add them to the search path, if the variable is defined. The
591 * variable's value is in the same format as the PATH envariable, i.e.
592 * <directory>:<directory>:<directory>...
593 */
594 if (Var_Exists("VPATH", VAR_CMD)) {
595 char *vpath, *path, *cp, savec;
596 /*
597 * GCC stores string constants in read-only memory, but
598 * Var_Subst will want to write this thing, so store it
599 * in an array
600 */
601 static char VPATH[] = "${VPATH}";
602
603 vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
604 path = vpath;
605 do {
606 /* skip to end of directory */
607 for (cp = path; *cp != ':' && *cp != '\0'; cp++)
608 continue;
609 /* Save terminator character so know when to stop */
610 savec = *cp;
611 *cp = '\0';
612 /* Add directory to search path */
613 Dir_AddDir(dirSearchPath, path);
614 *cp = savec;
615 path = cp + 1;
616 } while (savec == ':');
617 (void)free((Address)vpath);
618 }
619
620 /*
621 * Now that all search paths have been read for suffixes et al, it's
622 * time to add the default search path to their lists...
623 */
624 Suff_DoPaths();
625
626 /* print the initial graph, if the user requested it */
627 if (DEBUG(GRAPH1))
628 Targ_PrintGraph(1);
629
630 /*
631 * Have now read the entire graph and need to make a list of targets
632 * to create. If none was given on the command line, we consult the
633 * parsing module to find the main target(s) to create.
634 */
635 if (Lst_IsEmpty(create))
636 targs = Parse_MainName();
637 else
638 targs = Targ_FindList(create, TARG_CREATE);
639
640 /*
641 * this was original amMake -- want to allow parallelism, so put this
642 * back in, eventually.
643 */
644 if (!compatMake) {
645 /*
646 * Initialize job module before traversing the graph, now that
647 * any .BEGIN and .END targets have been read. This is done
648 * only if the -q flag wasn't given (to prevent the .BEGIN from
649 * being executed should it exist).
650 */
651 if (!queryFlag) {
652 if (maxLocal == -1)
653 maxLocal = maxJobs;
654 Job_Init(maxJobs, maxLocal);
655 jobsRunning = TRUE;
656 }
657
658 /* Traverse the graph, checking on all the targets */
659 outOfDate = Make_Run(targs);
660 } else
661 /*
662 * Compat_Init will take care of creating all the targets as
663 * well as initializing the module.
664 */
665 Compat_Run(targs);
666
667 Lst_Destroy(targs, NOFREE);
668 Lst_Destroy(makefiles, NOFREE);
669 Lst_Destroy(create, (void (*) __P((ClientData))) free);
670
671 /* print the graph now it's been processed if the user requested it */
672 if (DEBUG(GRAPH2))
673 Targ_PrintGraph(2);
674
675 Suff_End();
676 Targ_End();
677 Arch_End();
678 str_end();
679 Var_End();
680 Parse_End();
681 Dir_End();
682
683 if (queryFlag && outOfDate)
684 return(1);
685 else
686 return(0);
687 }
688
689 /*-
690 * ReadMakefile --
691 * Open and parse the given makefile.
692 *
693 * Results:
694 * TRUE if ok. FALSE if couldn't open file.
695 *
696 * Side Effects:
697 * lots
698 */
699 static Boolean
700 ReadMakefile(fname)
701 char *fname; /* makefile to read */
702 {
703 extern Lst parseIncPath, sysIncPath;
704 FILE *stream;
705 char *name, path[MAXPATHLEN + 1];
706
707 if (!strcmp(fname, "-")) {
708 Parse_File("(stdin)", stdin);
709 Var_Set("MAKEFILE", "", VAR_GLOBAL);
710 } else {
711 if ((stream = fopen(fname, "r")) != NULL)
712 goto found;
713 /* if we've chdir'd, rebuild the path name */
714 if (curdir != objdir && *fname != '/') {
715 (void)sprintf(path, "%s/%s", curdir, fname);
716 if ((stream = fopen(path, "r")) != NULL) {
717 fname = path;
718 goto found;
719 }
720 }
721 /* look in -I and system include directories. */
722 name = Dir_FindFile(fname, parseIncPath);
723 if (!name)
724 name = Dir_FindFile(fname, sysIncPath);
725 if (!name || !(stream = fopen(name, "r")))
726 return(FALSE);
727 fname = name;
728 /*
729 * set the MAKEFILE variable desired by System V fans -- the
730 * placement of the setting here means it gets set to the last
731 * makefile specified, as it is set by SysV make.
732 */
733 found: Var_Set("MAKEFILE", fname, VAR_GLOBAL);
734 Parse_File(fname, stream);
735 (void)fclose(stream);
736 }
737 return(TRUE);
738 }
739
740 /*-
741 * Error --
742 * Print an error message given its format.
743 *
744 * Results:
745 * None.
746 *
747 * Side Effects:
748 * The message is printed.
749 */
750 /* VARARGS */
751 void
752 #if __STDC__
753 Error(char *fmt, ...)
754 #else
755 Error(va_alist)
756 va_dcl
757 #endif
758 {
759 va_list ap;
760 #if __STDC__
761 va_start(ap, fmt);
762 #else
763 char *fmt;
764
765 va_start(ap);
766 fmt = va_arg(ap, char *);
767 #endif
768 (void)vfprintf(stderr, fmt, ap);
769 va_end(ap);
770 (void)fprintf(stderr, "\n");
771 (void)fflush(stderr);
772 }
773
774 /*-
775 * Fatal --
776 * Produce a Fatal error message. If jobs are running, waits for them
777 * to finish.
778 *
779 * Results:
780 * None
781 *
782 * Side Effects:
783 * The program exits
784 */
785 /* VARARGS */
786 void
787 #if __STDC__
788 Fatal(char *fmt, ...)
789 #else
790 Fatal(va_alist)
791 va_dcl
792 #endif
793 {
794 va_list ap;
795 #if __STDC__
796 va_start(ap, fmt);
797 #else
798 char *fmt;
799
800 va_start(ap);
801 fmt = va_arg(ap, char *);
802 #endif
803 if (jobsRunning)
804 Job_Wait();
805
806 (void)vfprintf(stderr, fmt, ap);
807 va_end(ap);
808 (void)fprintf(stderr, "\n");
809 (void)fflush(stderr);
810
811 if (DEBUG(GRAPH2))
812 Targ_PrintGraph(2);
813 exit(2); /* Not 1 so -q can distinguish error */
814 }
815
816 /*
817 * Punt --
818 * Major exception once jobs are being created. Kills all jobs, prints
819 * a message and exits.
820 *
821 * Results:
822 * None
823 *
824 * Side Effects:
825 * All children are killed indiscriminately and the program Lib_Exits
826 */
827 /* VARARGS */
828 void
829 #if __STDC__
830 Punt(char *fmt, ...)
831 #else
832 Punt(va_alist)
833 va_dcl
834 #endif
835 {
836 va_list ap;
837 #if __STDC__
838 va_start(ap, fmt);
839 #else
840 char *fmt;
841
842 va_start(ap);
843 fmt = va_arg(ap, char *);
844 #endif
845
846 (void)fprintf(stderr, "make: ");
847 (void)vfprintf(stderr, fmt, ap);
848 va_end(ap);
849 (void)fprintf(stderr, "\n");
850 (void)fflush(stderr);
851
852 DieHorribly();
853 }
854
855 /*-
856 * DieHorribly --
857 * Exit without giving a message.
858 *
859 * Results:
860 * None
861 *
862 * Side Effects:
863 * A big one...
864 */
865 void
866 DieHorribly()
867 {
868 if (jobsRunning)
869 Job_AbortAll();
870 if (DEBUG(GRAPH2))
871 Targ_PrintGraph(2);
872 exit(2); /* Not 1, so -q can distinguish error */
873 }
874
875 /*
876 * Finish --
877 * Called when aborting due to errors in child shell to signal
878 * abnormal exit.
879 *
880 * Results:
881 * None
882 *
883 * Side Effects:
884 * The program exits
885 */
886 void
887 Finish(errors)
888 int errors; /* number of errors encountered in Make_Make */
889 {
890 Fatal("%d error%s", errors, errors == 1 ? "" : "s");
891 }
892
893 /*
894 * emalloc --
895 * malloc, but die on error.
896 */
897 char *
898 emalloc(len)
899 size_t len;
900 {
901 char *p;
902
903 if ((p = (char *) malloc(len)) == NULL)
904 enomem();
905 return(p);
906 }
907
908 /*
909 * enomem --
910 * die when out of memory.
911 */
912 void
913 enomem()
914 {
915 (void)fprintf(stderr, "make: %s.\n", strerror(errno));
916 exit(2);
917 }
918
919 /*
920 * usage --
921 * exit with usage message
922 */
923 static void
924 usage()
925 {
926 (void)fprintf(stderr,
927 "usage: make [-eiknqrst] [-D variable] [-d flags] [-f makefile ]\n\
928 [-I directory] [-j max_jobs] [variable=value]\n");
929 exit(2);
930 }
931
932
933 int
934 PrintAddr(a, b)
935 ClientData a;
936 ClientData b;
937 {
938 printf("%lx ", (unsigned long) a);
939 return b ? 0 : 0;
940 }
941