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