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