Annotation of src/usr.bin/make/engine.c, Revision 1.40
1.40 ! espie 1: /* $OpenBSD: engine.c,v 1.39 2012/11/21 23:21:54 espie Exp $ */
1.33 espie 2: /*
3: * Copyright (c) 2012 Marc Espie.
4: *
5: * Extensive code modifications for the OpenBSD project.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
20: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
1.1 espie 28: /*
29: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
30: * Copyright (c) 1988, 1989 by Adam de Boor
31: * Copyright (c) 1989 by Berkeley Softworks
32: * All rights reserved.
33: *
34: * This code is derived from software contributed to Berkeley by
35: * Adam de Boor.
36: *
37: * Redistribution and use in source and binary forms, with or without
38: * modification, are permitted provided that the following conditions
39: * are met:
40: * 1. Redistributions of source code must retain the above copyright
41: * notice, this list of conditions and the following disclaimer.
42: * 2. Redistributions in binary form must reproduce the above copyright
43: * notice, this list of conditions and the following disclaimer in the
44: * documentation and/or other materials provided with the distribution.
45: * 3. Neither the name of the University nor the names of its contributors
46: * may be used to endorse or promote products derived from this software
47: * without specific prior written permission.
48: *
49: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59: * SUCH DAMAGE.
60: */
61:
1.10 espie 62: #include <sys/types.h>
1.33 espie 63: #include <sys/time.h>
1.10 espie 64: #include <sys/wait.h>
1.20 espie 65: #include <assert.h>
1.1 espie 66: #include <limits.h>
67: #include <stdio.h>
1.10 espie 68: #include <stdlib.h>
69: #include <ctype.h>
1.1 espie 70: #include <errno.h>
71: #include <fcntl.h>
72: #include <unistd.h>
73: #include <string.h>
1.10 espie 74: #include <signal.h>
1.1 espie 75: #include "config.h"
76: #include "defines.h"
77: #include "dir.h"
78: #include "engine.h"
79: #include "arch.h"
80: #include "gnode.h"
81: #include "targ.h"
82: #include "var.h"
83: #include "extern.h"
84: #include "lst.h"
85: #include "timestamp.h"
86: #include "make.h"
1.10 espie 87: #include "pathnames.h"
88: #include "error.h"
89: #include "str.h"
90: #include "memory.h"
1.16 espie 91: #include "buf.h"
1.24 espie 92: #include "job.h"
1.33 espie 93: #include "lowparse.h"
1.1 espie 94:
95: static void MakeTimeStamp(void *, void *);
1.6 espie 96: static int rewrite_time(const char *);
1.10 espie 97: static void setup_meta(void);
1.33 espie 98: static void setup_engine(void);
1.10 espie 99: static char **recheck_command_for_shell(char **);
1.34 espie 100: static void list_parents(GNode *, FILE *);
1.10 espie 101:
1.36 espie 102: /* XXX due to a bug in make's logic, targets looking like *.a or -l*
103: * have been silently dropped when make couldn't figure them out.
104: * Now, we warn about them until all Makefile bugs have been fixed.
105: */
106: static bool
107: drop_silently(const char *s)
108: {
109: size_t len;
110:
111: if (s[0] == '-' && s[1] == 'l')
112: return true;
113:
114: len = strlen(s);
115: if (len >=2 && s[len-2] == '.' && s[len-1] == 'a')
116: return true;
117: return false;
118: }
119:
1.1 espie 120: bool
1.33 espie 121: node_find_valid_commands(GNode *gn)
1.1 espie 122: {
1.11 espie 123: /* Alter our type to tell if errors should be ignored or things
1.21 espie 124: * should not be printed so setup_and_run_command knows what to do.
1.11 espie 125: */
126: if (Targ_Ignore(gn))
127: gn->type |= OP_IGNORE;
128: if (Targ_Silent(gn))
129: gn->type |= OP_SILENT;
130:
1.37 espie 131: if (DEBUG(DOUBLE) && (gn->type & OP_DOUBLE))
132: fprintf(stderr, "Warning: target %s had >1 lists of "
133: "shell commands (ignoring later ones)\n", gn->name);
1.34 espie 134: if (OP_NOP(gn->type) && Lst_IsEmpty(&gn->commands)) {
1.36 espie 135: if (drop_silently(gn->name)) {
1.34 espie 136: printf("Warning: target %s", gn->name);
137: list_parents(gn, stdout);
1.36 espie 138: printf(" does not have any command (BUG)\n");
1.34 espie 139: return true;
140: }
1.3 espie 141: /*
142: * No commands. Look for .DEFAULT rule from which we might infer
143: * commands
144: */
1.28 espie 145: if ((gn->type & OP_NODEFAULT) == 0 &&
1.18 espie 146: (DEFAULT->type & OP_DUMMY) == 0 &&
1.9 espie 147: !Lst_IsEmpty(&DEFAULT->commands)) {
1.3 espie 148: /*
149: * Make only looks for a .DEFAULT if the node was never
150: * the target of an operator, so that's what we do too.
151: * If a .DEFAULT was given, we substitute its commands
152: * for gn's commands and set the IMPSRC variable to be
153: * the target's name The DEFAULT node acts like a
154: * transformation rule, in that gn also inherits any
155: * attributes or sources attached to .DEFAULT itself.
156: */
157: Make_HandleUse(DEFAULT, gn);
1.16 espie 158: Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn);
1.3 espie 159: } else if (is_out_of_date(Dir_MTime(gn))) {
160: /*
161: * The node wasn't the target of an operator we have no
162: * .DEFAULT rule to go on and the target doesn't
163: * already exist. There's nothing more we can do for
1.28 espie 164: * this branch.
1.3 espie 165: */
1.20 espie 166: return false;
167: }
1.1 espie 168: }
1.3 espie 169: return true;
1.1 espie 170: }
171:
1.34 espie 172: static void
173: list_parents(GNode *gn, FILE *out)
174: {
175: LstNode ln;
176: bool first = true;
177:
178: for (ln = Lst_First(&gn->parents); ln != NULL; ln = Lst_Adv(ln)) {
179: GNode *p = Lst_Datum(ln);
180: if (!p->must_make)
181: continue;
182: if (first) {
183: fprintf(out, " (prerequisite of:");
184: first = false;
185: }
186: fprintf(out, " %s", p->name);
187: }
188: if (!first)
189: fprintf(out, ")");
190: }
191:
1.20 espie 192: void
1.33 espie 193: node_failure(GNode *gn)
1.20 espie 194: {
195: /*
196: If the -k flag wasn't given, we stop in
197: * our tracks, otherwise we just don't update this
198: * node's parents so they never get examined.
199: */
1.34 espie 200: const char *diag;
201: FILE *out;
1.20 espie 202:
203: if (gn->type & OP_OPTIONAL) {
1.34 espie 204: out = stdout;
205: diag = "(ignored)";
1.20 espie 206: } else if (keepgoing) {
1.34 espie 207: out = stdout;
208: diag = "(continuing)";
1.20 espie 209: } else {
1.34 espie 210: out = stderr;
211: diag = "";
212: }
213: fprintf(out, "make: don't know how to make %s", gn->name);
214: list_parents(gn, out);
215: fprintf(out, "%s\n", diag);
216: if (out == stdout)
217: fflush(stdout);
218: else {
1.33 espie 219: print_errors();
220: Punt(NULL);
1.20 espie 221: }
222: }
1.33 espie 223:
1.6 espie 224: /* touch files the hard way, by writing stuff to them */
225: static int
226: rewrite_time(const char *name)
227: {
228: int fd;
229: char c;
230:
231: fd = open(name, O_RDWR | O_CREAT, 0666);
232: if (fd < 0)
233: return -1;
234: /*
235: * Read and write a byte to the file to change
236: * the modification time.
237: */
238: if (read(fd, &c, 1) == 1) {
239: (void)lseek(fd, 0, SEEK_SET);
240: (void)write(fd, &c, 1);
241: }
242:
243: (void)close(fd);
244: return 0;
245: }
246:
1.1 espie 247: void
1.12 espie 248: Job_Touch(GNode *gn)
1.1 espie 249: {
1.33 espie 250: handle_all_signals();
1.26 espie 251: if (gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_OPTIONAL|OP_PHONY)) {
1.3 espie 252: /*
1.9 espie 253: * .JOIN, .USE, and .OPTIONAL targets are "virtual" targets
254: * and, as such, shouldn't really be created.
1.26 espie 255: * Likewise, .PHONY targets are not really files
1.3 espie 256: */
257: return;
258: }
1.1 espie 259:
1.12 espie 260: if (!(gn->type & OP_SILENT)) {
1.3 espie 261: (void)fprintf(stdout, "touch %s\n", gn->name);
262: (void)fflush(stdout);
263: }
1.1 espie 264:
1.3 espie 265: if (noExecute) {
266: return;
267: }
1.1 espie 268:
1.3 espie 269: if (gn->type & OP_ARCHV) {
270: Arch_Touch(gn);
271: } else {
272: const char *file = gn->path != NULL ? gn->path : gn->name;
273:
274: if (set_times(file) == -1){
1.6 espie 275: if (rewrite_time(file) == -1) {
1.5 espie 276: (void)fprintf(stdout,
277: "*** couldn't touch %s: %s", file,
1.3 espie 278: strerror(errno));
279: (void)fflush(stdout);
1.9 espie 280: }
1.1 espie 281: }
282: }
283: }
284:
285: void
1.7 espie 286: Make_TimeStamp(GNode *parent, GNode *child)
1.1 espie 287: {
1.8 espie 288: if (is_strictly_before(parent->cmtime, child->mtime))
289: parent->cmtime = child->mtime;
1.1 espie 290: }
291:
292: void
1.9 espie 293: Make_HandleUse(GNode *cgn, /* The .USE node */
1.1 espie 294: GNode *pgn) /* The target of the .USE node */
295: {
1.3 espie 296: GNode *gn; /* A child of the .USE node */
297: LstNode ln; /* An element in the children list */
1.1 espie 298:
1.20 espie 299:
300: assert(cgn->type & (OP_USE|OP_TRANSFORM));
301:
302: if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
303: /* .USE or transformation and target has no commands
304: * -- append the child's commands to the parent. */
305: Lst_Concat(&pgn->commands, &cgn->commands);
306: }
307:
308: for (ln = Lst_First(&cgn->children); ln != NULL;
309: ln = Lst_Adv(ln)) {
310: gn = (GNode *)Lst_Datum(ln);
311:
312: if (Lst_AddNew(&pgn->children, gn)) {
313: Lst_AtEnd(&gn->parents, pgn);
314: pgn->unmade++;
1.3 espie 315: }
1.20 espie 316: }
1.1 espie 317:
1.37 espie 318: if (DEBUG(DOUBLE) && (cgn->type & OP_DOUBLE))
319: fprintf(stderr,
320: "Warning: .USE %s expanded in %s had >1 lists of "
321: "shell commands (ignoring later ones)\n",
322: cgn->name, pgn->name);
323: pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM|OP_DOUBLE);
1.1 espie 324:
1.20 espie 325: /*
326: * This child node is now "made", so we decrement the count of
327: * unmade children in the parent... We also remove the child
328: * from the parent's list to accurately reflect the number of
329: * decent children the parent has. This is used by Make_Run to
330: * decide whether to queue the parent or examine its children...
331: */
332: if (cgn->type & OP_USE)
333: pgn->unmade--;
1.1 espie 334: }
335:
1.16 espie 336: void
337: Make_DoAllVar(GNode *gn)
1.1 espie 338: {
1.16 espie 339: GNode *child;
340: LstNode ln;
341: BUFFER allsrc, oodate;
342: char *target;
343: bool do_oodate;
344: int oodate_count, allsrc_count = 0;
345:
346: oodate_count = 0;
347: allsrc_count = 0;
1.39 espie 348:
349: Var(OODATE_INDEX, gn) = "";
350: Var(ALLSRC_INDEX, gn) = "";
1.16 espie 351:
352: for (ln = Lst_First(&gn->children); ln != NULL; ln = Lst_Adv(ln)) {
353: child = (GNode *)Lst_Datum(ln);
354: if ((child->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) != 0)
355: continue;
1.9 espie 356: if (OP_NOP(child->type) ||
1.16 espie 357: (target = Var(TARGET_INDEX, child)) == NULL) {
1.3 espie 358: /*
359: * this node is only source; use the specific pathname
360: * for it
361: */
1.9 espie 362: target = child->path != NULL ? child->path :
363: child->name;
1.3 espie 364: }
1.1 espie 365:
1.16 espie 366: /*
367: * It goes in the OODATE variable if the parent is younger than
368: * the child or if the child has been modified more recently
369: * than the start of the make. This is to keep make from
370: * getting confused if something else updates the parent after
371: * the make starts (shouldn't happen, I know, but sometimes it
372: * does). In such a case, if we've updated the kid, the parent
373: * is likely to have a modification time later than that of the
374: * kid and anything that relies on the OODATE variable will be
375: * hosed.
376: */
377: do_oodate = false;
378: if (gn->type & OP_JOIN) {
1.14 espie 379: if (child->built_status == MADE)
1.16 espie 380: do_oodate = true;
381: } else if (is_strictly_before(gn->mtime, child->mtime) ||
1.9 espie 382: (!is_strictly_before(child->mtime, now) &&
1.16 espie 383: child->built_status == MADE))
384: do_oodate = true;
385: if (do_oodate) {
386: oodate_count++;
387: if (oodate_count == 1)
388: Var(OODATE_INDEX, gn) = target;
389: else {
390: if (oodate_count == 2) {
391: Buf_Init(&oodate, 0);
1.28 espie 392: Buf_AddString(&oodate,
1.16 espie 393: Var(OODATE_INDEX, gn));
394: }
395: Buf_AddSpace(&oodate);
396: Buf_AddString(&oodate, target);
397: }
398: }
399: allsrc_count++;
400: if (allsrc_count == 1)
401: Var(ALLSRC_INDEX, gn) = target;
402: else {
403: if (allsrc_count == 2) {
404: Buf_Init(&allsrc, 0);
1.28 espie 405: Buf_AddString(&allsrc,
1.16 espie 406: Var(ALLSRC_INDEX, gn));
407: }
408: Buf_AddSpace(&allsrc);
409: Buf_AddString(&allsrc, target);
1.3 espie 410: }
1.1 espie 411: }
412:
1.16 espie 413: if (allsrc_count > 1)
414: Var(ALLSRC_INDEX, gn) = Buf_Retrieve(&allsrc);
415: if (oodate_count > 1)
416: Var(OODATE_INDEX, gn) = Buf_Retrieve(&oodate);
1.1 espie 417:
1.13 espie 418: if (gn->impliedsrc)
1.16 espie 419: Var(IMPSRC_INDEX, gn) = Var(TARGET_INDEX, gn->impliedsrc);
1.28 espie 420:
1.3 espie 421: if (gn->type & OP_JOIN)
1.16 espie 422: Var(TARGET_INDEX, gn) = Var(ALLSRC_INDEX, gn);
1.1 espie 423: }
424:
425: /* Wrapper to call Make_TimeStamp from a forEach loop. */
426: static void
1.9 espie 427: MakeTimeStamp(void *parent, void *child)
1.1 espie 428: {
1.9 espie 429: Make_TimeStamp((GNode *)parent, (GNode *)child);
1.1 espie 430: }
431:
432: bool
1.9 espie 433: Make_OODate(GNode *gn)
1.1 espie 434: {
1.9 espie 435: bool oodate;
1.1 espie 436:
437: /*
1.3 espie 438: * Certain types of targets needn't even be sought as their datedness
439: * doesn't depend on their modification time...
1.1 espie 440: */
1.15 espie 441: if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC|OP_PHONY)) == 0) {
1.3 espie 442: (void)Dir_MTime(gn);
443: if (DEBUG(MAKE)) {
1.9 espie 444: if (!is_out_of_date(gn->mtime))
1.5 espie 445: printf("modified %s...",
1.3 espie 446: time_to_string(gn->mtime));
1.9 espie 447: else
1.3 espie 448: printf("non-existent...");
449: }
1.1 espie 450: }
451:
452: /*
1.3 espie 453: * A target is remade in one of the following circumstances:
1.9 espie 454: * - its modification time is smaller than that of its youngest child
455: * and it would actually be run (has commands or type OP_NOP)
456: * - it's the object of a force operator
457: * - it has no children, was on the lhs of an operator and doesn't
458: * exist already.
1.3 espie 459: *
1.1 espie 460: */
1.3 espie 461: if (gn->type & OP_USE) {
462: /*
463: * If the node is a USE node it is *never* out of date
464: * no matter *what*.
465: */
1.9 espie 466: if (DEBUG(MAKE))
1.3 espie 467: printf(".USE node...");
468: oodate = false;
469: } else if (gn->type & OP_JOIN) {
470: /*
471: * A target with the .JOIN attribute is only considered
472: * out-of-date if any of its children was out-of-date.
473: */
1.9 espie 474: if (DEBUG(MAKE))
1.3 espie 475: printf(".JOIN node...");
476: oodate = gn->childMade;
477: } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
478: /*
1.9 espie 479: * A node which is the object of the force (!) operator or which
480: * has the .EXEC attribute is always considered out-of-date.
1.3 espie 481: */
482: if (DEBUG(MAKE)) {
1.9 espie 483: if (gn->type & OP_FORCE)
1.3 espie 484: printf("! operator...");
1.9 espie 485: else if (gn->type & OP_PHONY)
1.3 espie 486: printf(".PHONY node...");
1.9 espie 487: else
1.3 espie 488: printf(".EXEC node...");
489: }
490: oodate = true;
491: } else if (is_strictly_before(gn->mtime, gn->cmtime) ||
1.9 espie 492: (is_out_of_date(gn->cmtime) &&
1.3 espie 493: (is_out_of_date(gn->mtime) || (gn->type & OP_DOUBLEDEP)))) {
494: /*
495: * A node whose modification time is less than that of its
496: * youngest child or that has no children (cmtime ==
497: * OUT_OF_DATE) and either doesn't exist (mtime == OUT_OF_DATE)
1.9 espie 498: * or was the object of a :: operator is out-of-date.
1.3 espie 499: */
500: if (DEBUG(MAKE)) {
1.9 espie 501: if (is_strictly_before(gn->mtime, gn->cmtime))
1.3 espie 502: printf("modified before source...");
1.9 espie 503: else if (is_out_of_date(gn->mtime))
1.3 espie 504: printf("non-existent and no sources...");
1.9 espie 505: else
1.3 espie 506: printf(":: operator and no sources...");
507: }
508: oodate = true;
509: } else {
510: oodate = false;
511: }
1.1 espie 512:
1.3 espie 513: /*
514: * If the target isn't out-of-date, the parents need to know its
515: * modification time. Note that targets that appear to be out-of-date
516: * but aren't, because they have no commands and aren't of type OP_NOP,
517: * have their mtime stay below their children's mtime to keep parents
518: * from thinking they're out-of-date.
519: */
520: if (!oodate)
521: Lst_ForEach(&gn->parents, MakeTimeStamp, gn);
1.1 espie 522:
1.3 espie 523: return oodate;
1.1 espie 524: }
525:
1.10 espie 526: /* The following array is used to make a fast determination of which
527: * characters are interpreted specially by the shell. If a command
528: * contains any of these characters, it is executed by the shell, not
529: * directly by us. */
530: static char meta[256];
531:
532: void
533: setup_meta(void)
534: {
535: char *p;
536:
1.32 espie 537: for (p = "#=|^(){};&<>*?[]:$`\\\n"; *p != '\0'; p++)
1.10 espie 538: meta[(unsigned char) *p] = 1;
539: /* The null character serves as a sentinel in the string. */
540: meta[0] = 1;
541: }
542:
543: static char **
544: recheck_command_for_shell(char **av)
545: {
546: char *runsh[] = {
1.32 espie 547: "!", "alias", "cd", "eval", "exit", "read", "set", "ulimit",
1.10 espie 548: "unalias", "unset", "wait", "umask", NULL
549: };
550:
551: char **p;
552:
553: /* optimization: if exec cmd, we avoid the intermediate shell */
554: if (strcmp(av[0], "exec") == 0)
555: av++;
556:
557: for (p = runsh; *p; p++)
558: if (strcmp(av[0], *p) == 0)
559: return NULL;
560:
561: return av;
562: }
563:
1.40 ! espie 564: static void
1.10 espie 565: run_command(const char *cmd, bool errCheck)
566: {
567: const char *p;
568: char *shargv[4];
569: char **todo;
570:
571: shargv[0] = _PATH_BSHELL;
572:
573: shargv[1] = errCheck ? "-ec" : "-c";
574: shargv[2] = (char *)cmd;
575: shargv[3] = NULL;
576:
577: todo = shargv;
578:
579:
580: /* Search for meta characters in the command. If there are no meta
581: * characters, there's no need to execute a shell to execute the
582: * command. */
583: for (p = cmd; !meta[(unsigned char)*p]; p++)
584: continue;
585: if (*p == '\0') {
586: char *bp;
587: char **av;
588: int argc;
589: /* No meta-characters, so probably no need to exec a shell.
590: * Break the command into words to form an argument vector
591: * we can execute. */
592: av = brk_string(cmd, &argc, &bp);
593: av = recheck_command_for_shell(av);
594: if (av != NULL)
595: todo = av;
596: }
597: execvp(todo[0], todo);
598:
599: if (errno == ENOENT)
600: fprintf(stderr, "%s: not found\n", todo[0]);
601: else
602: perror(todo[0]);
603: _exit(1);
604: }
605:
1.33 espie 606: static Job myjob;
607:
608: void
609: job_attach_node(Job *job, GNode *node)
610: {
611: job->node = node;
1.34 espie 612: job->node->built_status = BUILDING;
1.33 espie 613: job->next_cmd = Lst_First(&node->commands);
614: job->exit_type = JOB_EXIT_OKAY;
615: job->location = NULL;
616: job->flags = 0;
617: }
618:
619: void
620: job_handle_status(Job *job, int status)
621: {
1.36 espie 622: bool silent;
1.38 espie 623: int dying;
1.36 espie 624:
625: /* if there's one job running and we don't keep going, no need
626: * to report right now.
627: */
628: if ((job->flags & JOB_ERRCHECK) && !keepgoing && runningJobs == NULL)
629: silent = !DEBUG(JOB);
630: else
631: silent = false;
632:
1.33 espie 633: debug_job_printf("Process %ld (%s) exited with status %d.\n",
634: (long)job->pid, job->node->name, status);
635:
636: /* classify status */
637: if (WIFEXITED(status)) {
638: job->code = WEXITSTATUS(status);/* exited */
1.38 espie 639: if (job->code != 0) {
640: /* if we're already dying from that signal, be silent */
641: if (!silent && job->code > 128
642: && job->code <= 128 + _NSIG) {
643: dying = check_dying_signal();
644: silent = dying && job->code == dying + 128;
645: }
1.36 espie 646: if (!silent)
647: printf("*** Error %d", job->code);
1.33 espie 648: job->exit_type = JOB_EXIT_BAD;
649: } else
650: job->exit_type = JOB_EXIT_OKAY;
651: } else {
652: job->exit_type = JOB_SIGNALED;
653: job->code = WTERMSIG(status); /* signaled */
1.38 espie 654: /* if we're already dying from that signal, be silent */
655: if (!silent) {
656: dying = check_dying_signal();
657: silent = dying && job->code == dying;
658: }
1.36 espie 659: if (!silent)
660: printf("*** Signal %d", job->code);
1.33 espie 661: }
662:
663: /* if there is a problem, what's going on ? */
664: if (job->exit_type != JOB_EXIT_OKAY) {
1.36 espie 665: if (!silent)
666: printf(" in target '%s'", job->node->name);
1.33 espie 667: if (job->flags & JOB_ERRCHECK) {
668: job->node->built_status = ERROR;
669: /* compute expensive status if we really want it */
670: if ((job->flags & JOB_SILENT) && job == &myjob)
671: determine_expensive_job(job);
672: if (!keepgoing) {
1.36 espie 673: if (!silent)
674: printf("\n");
1.33 espie 675: job->next = errorJobs;
676: errorJobs = job;
677: /* XXX don't free the command */
678: return;
679: }
680: printf(", line %lu of %s", job->location->lineno,
681: job->location->fname);
682: if ((job->flags & (JOB_SILENT | JOB_IS_EXPENSIVE))
683: == JOB_SILENT)
684: printf(": %s", job->cmd);
685: /* Abort the current target,
686: * but let others continue. */
687: printf(" (continuing)\n");
688: } else {
689: /* Continue executing commands for
690: * this target. If we return 0,
691: * this will happen... */
692: printf(" (ignored)\n");
693: job->exit_type = JOB_EXIT_OKAY;
694: }
695: }
696: free(job->cmd);
697: }
698:
699: int
700: run_gnode(GNode *gn)
701: {
702: if (!gn || (gn->type & OP_DUMMY))
703: return NOSUCHNODE;
704:
705: gn->built_status = MADE;
706:
707: job_attach_node(&myjob, gn);
708: while (myjob.exit_type == JOB_EXIT_OKAY) {
709: bool finished = job_run_next(&myjob);
710: if (finished)
711: break;
712: handle_one_job(&myjob);
713: }
714:
715: return gn->built_status;
716: }
717:
718:
719: static void
720: setup_engine(void)
721: {
722: static int already_setup = 0;
723:
724: if (!already_setup) {
725: setup_meta();
726: already_setup = 1;
727: }
728: }
729:
730: static bool
731: do_run_command(Job *job)
1.10 espie 732: {
733: bool silent; /* Don't print command */
734: bool doExecute; /* Execute the command */
735: bool errCheck; /* Check errors */
1.33 espie 736: pid_t cpid; /* Child pid */
737:
738: const char *cmd = job->cmd;
739: silent = job->node->type & OP_SILENT;
740: errCheck = !(job->node->type & OP_IGNORE);
741: if (job->node->type & OP_MAKE)
742: doExecute = true;
743: else
744: doExecute = !noExecute;
1.10 espie 745:
746: /* How can we execute a null command ? we warn the user that the
747: * command expanded to nothing (is this the right thing to do?). */
1.19 espie 748: if (*cmd == '\0') {
1.10 espie 749: Error("%s expands to empty string", cmd);
1.33 espie 750: return false;
1.28 espie 751: }
1.10 espie 752:
753: for (;; cmd++) {
754: if (*cmd == '@')
755: silent = DEBUG(LOUD) ? false : true;
756: else if (*cmd == '-')
757: errCheck = false;
758: else if (*cmd == '+')
759: doExecute = true;
760: else
761: break;
762: }
763: while (isspace(*cmd))
764: cmd++;
1.33 espie 765: /* Print the command before fork if make -n or !silent*/
766: if ( noExecute || !silent)
1.10 espie 767: printf("%s\n", cmd);
1.33 espie 768:
769: if (silent)
770: job->flags |= JOB_SILENT;
771: else
772: job->flags &= ~JOB_SILENT;
773:
1.10 espie 774: /* If we're not supposed to execute any commands, this is as far as
775: * we go... */
776: if (!doExecute)
1.33 espie 777: return false;
778: /* always flush for other stuff */
779: fflush(stdout);
1.10 espie 780:
781: /* Fork and execute the single command. If the fork fails, we abort. */
782: switch (cpid = fork()) {
783: case -1:
1.33 espie 784: Punt("Could not fork");
1.10 espie 785: /*NOTREACHED*/
786: case 0:
1.34 espie 787: /* put a random delay unless we're the only job running
788: * and there's nothing left to do.
789: */
790: if (random_delay)
791: if (!(runningJobs == NULL && no_jobs_left()))
792: usleep(random() % random_delay);
1.10 espie 793: run_command(cmd, errCheck);
794: /*NOTREACHED*/
795: default:
1.33 espie 796: job->pid = cpid;
797: job->next = runningJobs;
798: runningJobs = job;
799: if (errCheck)
800: job->flags |= JOB_ERRCHECK;
801: else
802: job->flags &= ~JOB_ERRCHECK;
803: debug_job_printf("Running %ld (%s) %s\n", (long)job->pid,
804: job->node->name, (noExecute || !silent) ? "" : cmd);
805: return true;
1.10 espie 806: }
807: }
808:
1.33 espie 809: bool
810: job_run_next(Job *job)
1.10 espie 811: {
1.33 espie 812: bool started;
813: GNode *gn = job->node;
1.19 espie 814:
1.33 espie 815: setup_engine();
816: while (job->next_cmd != NULL) {
817: struct command *command = Lst_Datum(job->next_cmd);
818:
819: handle_all_signals();
820: job->location = &command->location;
821: Parse_SetLocation(job->location);
822: job->cmd = Var_Subst(command->string, &gn->context, false);
823: job->next_cmd = Lst_Adv(job->next_cmd);
1.30 espie 824: if (fatal_errors)
1.33 espie 825: Punt(NULL);
826: started = do_run_command(job);
827: if (started)
828: return false;
829: else
830: free(job->cmd);
1.19 espie 831: }
1.33 espie 832: job->exit_type = JOB_EXIT_OKAY;
833: return true;
1.19 espie 834: }
835: