xref: /freebsd/contrib/bmake/compat.c (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1 /*	$NetBSD: compat.c,v 1.262 2025/01/19 10:57:10 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
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. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1988, 1989 by Adam de Boor
37  * Copyright (c) 1989 by Berkeley Softworks
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Adam de Boor.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 /*
73  * This file implements the full-compatibility mode of make, which makes the
74  * targets without parallelism and without a custom shell.
75  *
76  * Interface:
77  *	Compat_MakeAll	Initialize this module and make the given targets.
78  */
79 
80 #ifdef HAVE_CONFIG_H
81 # include   "config.h"
82 #endif
83 #include <sys/types.h>
84 #include <sys/stat.h>
85 #include "wait.h"
86 
87 #include <errno.h>
88 #include <signal.h>
89 
90 #include "make.h"
91 #include "dir.h"
92 #include "job.h"
93 #include "metachar.h"
94 #include "pathnames.h"
95 
96 /*	"@(#)compat.c	8.2 (Berkeley) 3/19/94"	*/
97 MAKE_RCSID("$NetBSD: compat.c,v 1.262 2025/01/19 10:57:10 rillig Exp $");
98 
99 static GNode *curTarg = NULL;
100 static pid_t compatChild;
101 static int compatSigno;
102 
103 /*
104  * Delete the file of a failed, interrupted, or otherwise duffed target,
105  * unless inhibited by .PRECIOUS.
106  */
107 static void
CompatDeleteTarget(GNode * gn)108 CompatDeleteTarget(GNode *gn)
109 {
110 	if (gn != NULL && !GNode_IsPrecious(gn) &&
111 	    (gn->type & OP_PHONY) == 0) {
112 		const char *file = GNode_VarTarget(gn);
113 		if (!opts.noExecute && unlink_file(file) == 0)
114 			Error("*** %s removed", file);
115 	}
116 }
117 
118 /*
119  * Interrupt the creation of the current target and remove it if it ain't
120  * precious. Then exit.
121  *
122  * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
123  *
124  * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
125  * left the logic alone for now. - dholland 20160826
126  */
127 static void
CompatInterrupt(int signo)128 CompatInterrupt(int signo)
129 {
130 	CompatDeleteTarget(curTarg);
131 
132 	if (curTarg != NULL && !GNode_IsPrecious(curTarg)) {
133 		/* Run .INTERRUPT only if hit with interrupt signal. */
134 		if (signo == SIGINT) {
135 			GNode *gn = Targ_FindNode(".INTERRUPT");
136 			if (gn != NULL)
137 				Compat_Make(gn, gn);
138 		}
139 	}
140 
141 	if (signo == SIGQUIT)
142 		_exit(signo);
143 
144 	/*
145 	 * If there is a child running, pass the signal on.
146 	 * We will exist after it has exited.
147 	 */
148 	compatSigno = signo;
149 	if (compatChild > 0) {
150 		KILLPG(compatChild, signo);
151 	} else {
152 		bmake_signal(signo, SIG_DFL);
153 		kill(myPid, signo);
154 	}
155 }
156 
157 static void
DebugFailedTarget(const char * cmd,const GNode * gn)158 DebugFailedTarget(const char *cmd, const GNode *gn)
159 {
160 	const char *p = cmd;
161 	debug_printf("\n*** Failed target:  %s\n*** Failed command: ",
162 	    gn->name);
163 
164 	/*
165 	 * Replace runs of whitespace with a single space, to reduce the
166 	 * amount of whitespace for multi-line command lines.
167 	 */
168 	while (*p != '\0') {
169 		if (ch_isspace(*p)) {
170 			debug_printf(" ");
171 			cpp_skip_whitespace(&p);
172 		} else {
173 			debug_printf("%c", *p);
174 			p++;
175 		}
176 	}
177 	debug_printf("\n");
178 }
179 
180 static bool
UseShell(const char * cmd MAKE_ATTR_UNUSED)181 UseShell(const char *cmd MAKE_ATTR_UNUSED)
182 {
183 #if defined(FORCE_USE_SHELL) || !defined(MAKE_NATIVE)
184 	/*
185 	 * In a non-native build, the host environment might be weird enough
186 	 * that it's necessary to go through a shell to get the correct
187 	 * behaviour.  Or perhaps the shell has been replaced with something
188 	 * that does extra logging, and that should not be bypassed.
189 	 */
190 	return true;
191 #else
192 	/*
193 	 * Search for meta characters in the command. If there are no meta
194 	 * characters, there's no need to execute a shell to execute the
195 	 * command.
196 	 *
197 	 * Additionally variable assignments and empty commands
198 	 * go to the shell. Therefore treat '=' and ':' like shell
199 	 * meta characters as documented in make(1).
200 	 */
201 
202 	return needshell(cmd);
203 #endif
204 }
205 
206 static int
Compat_Spawn(const char ** av)207 Compat_Spawn(const char **av)
208 {
209 	int pid = FORK_FUNCTION();
210 	if (pid < 0)
211 		Fatal("Could not fork");
212 
213 	if (pid == 0) {
214 #ifdef USE_META
215 		if (useMeta)
216 			meta_compat_child();
217 #endif
218 		(void)execvp(av[0], (char *const *)UNCONST(av));
219 		execDie("exec", av[0]);
220 	}
221 	return pid;
222 }
223 
224 /*
225  * Execute the next command for a target. If the command returns an error,
226  * the node's made field is set to ERROR and creation stops.
227  *
228  * Input:
229  *	cmdp		Command to execute
230  *	gn		Node from which the command came
231  *	ln		List node that contains the command
232  *
233  * Results:
234  *	true if the command succeeded.
235  */
236 bool
Compat_RunCommand(const char * cmdp,GNode * gn,StringListNode * ln)237 Compat_RunCommand(const char *cmdp, GNode *gn, StringListNode *ln)
238 {
239 	char *cmdStart;		/* Start of expanded command */
240 	char *volatile bp;
241 	bool silent;		/* Don't print command */
242 	bool doIt;		/* Execute even if -n */
243 	volatile bool errCheck;	/* Check errors */
244 	WAIT_T reason;		/* Reason for child's death */
245 	WAIT_T status;		/* Description of child's death */
246 	pid_t retstat;		/* Result of wait */
247 	const char **av;	/* Arguments for the child process */
248 	char **volatile mav;	/* Copy of the argument vector for freeing */
249 	bool useShell;		/* True if command should be executed using a
250 				 * shell */
251 	const char *cmd = cmdp;
252 	char cmd_file[MAXPATHLEN];
253 	size_t cmd_len;
254 	int parseErrorsBefore;
255 
256 	silent = (gn->type & OP_SILENT) != OP_NONE;
257 	errCheck = !(gn->type & OP_IGNORE);
258 	doIt = false;
259 
260 	parseErrorsBefore = parseErrors;
261 	cmdStart = Var_SubstInTarget(cmd, gn);
262 	if (parseErrors != parseErrorsBefore) {
263 		free(cmdStart);
264 		return false;
265 	}
266 
267 	if (cmdStart[0] == '\0') {
268 		free(cmdStart);
269 		return true;
270 	}
271 	cmd = cmdStart;
272 	LstNode_Set(ln, cmdStart);
273 
274 	if (gn->type & OP_SAVE_CMDS) {
275 		GNode *endNode = Targ_GetEndNode();
276 		if (gn != endNode) {
277 			/*
278 			 * Append the expanded command, to prevent the
279 			 * local variables from being interpreted in the
280 			 * scope of the .END node.
281 			 *
282 			 * A probably unintended side effect of this is that
283 			 * the expanded command will be expanded again in the
284 			 * .END node.  Therefore, a literal '$' in these
285 			 * commands must be written as '$$$$' instead of the
286 			 * usual '$$'.
287 			 */
288 			Lst_Append(&endNode->commands, cmdStart);
289 			goto register_command;
290 		}
291 	}
292 	if (strcmp(cmdStart, "...") == 0) {
293 		gn->type |= OP_SAVE_CMDS;
294 	register_command:
295 		Parse_RegisterCommand(cmdStart);
296 		return true;
297 	}
298 
299 	for (;;) {
300 		if (*cmd == '@')
301 			silent = !DEBUG(LOUD);
302 		else if (*cmd == '-')
303 			errCheck = false;
304 		else if (*cmd == '+')
305 			doIt = true;
306 		else if (!ch_isspace(*cmd))
307 			/* Ignore whitespace for compatibility with gnu make */
308 			break;
309 		cmd++;
310 	}
311 
312 	while (ch_isspace(*cmd))
313 		cmd++;
314 	if (cmd[0] == '\0')
315 		goto register_command;
316 
317 	useShell = UseShell(cmd);
318 
319 	if (!silent || !GNode_ShouldExecute(gn)) {
320 		printf("%s\n", cmd);
321 		fflush(stdout);
322 	}
323 
324 	if (!doIt && !GNode_ShouldExecute(gn))
325 		goto register_command;
326 
327 	DEBUG1(JOB, "Execute: '%s'\n", cmd);
328 
329 	cmd_len = strlen(cmd);
330 	if (cmd_len > MAKE_CMDLEN_LIMIT)
331 		useShell = true;
332 	else
333 		cmd_file[0] = '\0';
334 
335 	if (useShell) {
336 		static const char *shargv[5];
337 
338 		if (Cmd_Argv(cmd, cmd_len, shargv, 5,
339 			cmd_file, sizeof(cmd_file),
340 			(errCheck && shellErrFlag != NULL),
341 			DEBUG(SHELL)) < 0)
342 			Fatal("cannot run \"%s\"", cmd);
343 		av = shargv;
344 		bp = NULL;
345 		mav = NULL;
346 	} else {
347 		Words words = Str_Words(cmd, false);
348 		mav = words.words;
349 		bp = words.freeIt;
350 		av = (void *)mav;
351 	}
352 
353 #ifdef USE_META
354 	if (useMeta)
355 		meta_compat_start();
356 #endif
357 
358 	Var_ReexportVars(gn);
359 
360 	compatChild = Compat_Spawn(av);
361 	free(mav);
362 	free(bp);
363 
364 	/* XXX: Memory management looks suspicious here. */
365 	/* XXX: Setting a list item to NULL is unexpected. */
366 	LstNode_SetNull(ln);
367 
368 #ifdef USE_META
369 	if (useMeta)
370 		meta_compat_parent(compatChild);
371 #endif
372 
373 	/* The child is off and running. Now all we can do is wait... */
374 	while ((retstat = wait(&reason)) != compatChild) {
375 		if (retstat > 0)
376 			JobReapChild(retstat, reason, false); /* not ours? */
377 		if (retstat == -1 && errno != EINTR)
378 			break;
379 	}
380 
381 	if (retstat < 0)
382 		Fatal("error in wait: %d: %s", retstat, strerror(errno));
383 
384 	if (WIFSTOPPED(reason)) {
385 		status = WSTOPSIG(reason);
386 	} else if (WIFEXITED(reason)) {
387 		status = WEXITSTATUS(reason);
388 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
389 		if (useMeta)
390 			meta_cmd_finish(NULL);
391 #endif
392 		if (status != 0) {
393 			if (DEBUG(ERROR))
394 				DebugFailedTarget(cmd, gn);
395 			printf("*** Error code %d", status);
396 		}
397 	} else {
398 		status = WTERMSIG(reason);
399 		printf("*** Signal %d", status);
400 	}
401 
402 
403 	if (!WIFEXITED(reason) || status != 0) {
404 		if (errCheck) {
405 #ifdef USE_META
406 			if (useMeta)
407 				meta_job_error(NULL, gn, false, status);
408 #endif
409 			gn->made = ERROR;
410 			if (WIFEXITED(reason))
411 				gn->exit_status = status;
412 			if (opts.keepgoing) {
413 				/*
414 				 * Abort the current target,
415 				 * but let others continue.
416 				 */
417 				printf(" (continuing)\n");
418 			} else {
419 				printf("\n");
420 			}
421 			if (deleteOnError)
422 				CompatDeleteTarget(gn);
423 		} else {
424 			/*
425 			 * Continue executing commands for this target.
426 			 * If we return 0, this will happen...
427 			 */
428 			printf(" (ignored)\n");
429 			status = 0;
430 		}
431 		fflush(stdout);
432 	}
433 
434 	free(cmdStart);
435 	if (cmd_file[0] != '\0')
436 		unlink(cmd_file);
437 	compatChild = 0;
438 	if (compatSigno != 0) {
439 		bmake_signal(compatSigno, SIG_DFL);
440 		kill(myPid, compatSigno);
441 	}
442 
443 	return status == 0;
444 }
445 
446 static void
RunCommands(GNode * gn)447 RunCommands(GNode *gn)
448 {
449 	StringListNode *ln;
450 
451 	for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
452 		const char *cmd = ln->datum;
453 		if (!Compat_RunCommand(cmd, gn, ln))
454 			break;
455 	}
456 }
457 
458 static void
MakeInRandomOrder(GNode ** gnodes,GNode ** end,GNode * pgn)459 MakeInRandomOrder(GNode **gnodes, GNode **end, GNode *pgn)
460 {
461 	GNode **it;
462 	size_t r;
463 
464 	for (r = (size_t)(end - gnodes); r >= 2; r--) {
465 		/* Biased, but irrelevant in practice. */
466 		size_t i = (size_t)random() % r;
467 		GNode *t = gnodes[r - 1];
468 		gnodes[r - 1] = gnodes[i];
469 		gnodes[i] = t;
470 	}
471 
472 	for (it = gnodes; it != end; it++)
473 		Compat_Make(*it, pgn);
474 }
475 
476 static void
MakeWaitGroupsInRandomOrder(GNodeList * gnodes,GNode * pgn)477 MakeWaitGroupsInRandomOrder(GNodeList *gnodes, GNode *pgn)
478 {
479 	Vector vec;
480 	GNodeListNode *ln;
481 	GNode **nodes;
482 	size_t i, n, start;
483 
484 	Vector_Init(&vec, sizeof(GNode *));
485 	for (ln = gnodes->first; ln != NULL; ln = ln->next)
486 		*(GNode **)Vector_Push(&vec) = ln->datum;
487 	nodes = vec.items;
488 	n = vec.len;
489 
490 	start = 0;
491 	for (i = 0; i < n; i++) {
492 		if (nodes[i]->type & OP_WAIT) {
493 			MakeInRandomOrder(nodes + start, nodes + i, pgn);
494 			Compat_Make(nodes[i], pgn);
495 			start = i + 1;
496 		}
497 	}
498 	MakeInRandomOrder(nodes + start, nodes + i, pgn);
499 
500 	Vector_Done(&vec);
501 }
502 
503 static void
MakeNodes(GNodeList * gnodes,GNode * pgn)504 MakeNodes(GNodeList *gnodes, GNode *pgn)
505 {
506 	GNodeListNode *ln;
507 
508 	if (Lst_IsEmpty(gnodes))
509 		return;
510 	if (opts.randomizeTargets) {
511 		MakeWaitGroupsInRandomOrder(gnodes, pgn);
512 		return;
513 	}
514 
515 	for (ln = gnodes->first; ln != NULL; ln = ln->next) {
516 		GNode *cgn = ln->datum;
517 		Compat_Make(cgn, pgn);
518 	}
519 }
520 
521 static bool
MakeUnmade(GNode * gn,GNode * pgn)522 MakeUnmade(GNode *gn, GNode *pgn)
523 {
524 
525 	assert(gn->made == UNMADE);
526 
527 	/*
528 	 * First mark ourselves to be made, then apply whatever transformations
529 	 * the suffix module thinks are necessary. Once that's done, we can
530 	 * descend and make all our children. If any of them has an error
531 	 * but the -k flag was given, our 'make' field will be set to false
532 	 * again. This is our signal to not attempt to do anything but abort
533 	 * our parent as well.
534 	 */
535 	gn->flags.remake = true;
536 	gn->made = BEINGMADE;
537 
538 	if (!(gn->type & OP_MADE))
539 		Suff_FindDeps(gn);
540 
541 	MakeNodes(&gn->children, gn);
542 
543 	if (!gn->flags.remake) {
544 		gn->made = ABORTED;
545 		pgn->flags.remake = false;
546 		return false;
547 	}
548 
549 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL)
550 		Var_Set(pgn, IMPSRC, GNode_VarTarget(gn));
551 
552 	/*
553 	 * All the children were made ok. Now youngestChild->mtime contains the
554 	 * modification time of the newest child, we need to find out if we
555 	 * exist and when we were modified last. The criteria for datedness
556 	 * are defined by GNode_IsOODate.
557 	 */
558 	DEBUG1(MAKE, "Examining %s...", gn->name);
559 	if (!GNode_IsOODate(gn)) {
560 		gn->made = UPTODATE;
561 		DEBUG0(MAKE, "up-to-date.\n");
562 		return false;
563 	}
564 
565 	/*
566 	 * If the user is just seeing if something is out-of-date, exit now
567 	 * to tell him/her "yes".
568 	 */
569 	DEBUG0(MAKE, "out-of-date.\n");
570 	if (opts.query && gn != Targ_GetEndNode())
571 		exit(1);
572 
573 	/*
574 	 * We need to be re-made.
575 	 * Ensure that $? (.OODATE) and $> (.ALLSRC) are both set.
576 	 */
577 	GNode_SetLocalVars(gn);
578 
579 	/*
580 	 * Alter our type to tell if errors should be ignored or things
581 	 * should not be printed so Compat_RunCommand knows what to do.
582 	 */
583 	if (opts.ignoreErrors)
584 		gn->type |= OP_IGNORE;
585 	if (opts.silent)
586 		gn->type |= OP_SILENT;
587 
588 	if (Job_CheckCommands(gn, Fatal)) {
589 		if (!opts.touch || (gn->type & OP_MAKE)) {
590 			curTarg = gn;
591 #ifdef USE_META
592 			if (useMeta && GNode_ShouldExecute(gn))
593 				meta_job_start(NULL, gn);
594 #endif
595 			RunCommands(gn);
596 			curTarg = NULL;
597 		} else {
598 			Job_Touch(gn, (gn->type & OP_SILENT) != OP_NONE);
599 		}
600 	} else {
601 		gn->made = ERROR;
602 	}
603 #ifdef USE_META
604 	if (useMeta && GNode_ShouldExecute(gn)) {
605 		if (meta_job_finish(NULL) != 0)
606 			gn->made = ERROR;
607 	}
608 #endif
609 
610 	if (gn->made != ERROR) {
611 		/*
612 		 * If the node was made successfully, mark it so, update
613 		 * its modification time and timestamp all its parents.
614 		 * This is to keep its state from affecting that of its parent.
615 		 */
616 		gn->made = MADE;
617 		if (Make_Recheck(gn) == 0)
618 			pgn->flags.force = true;
619 		if (!(gn->type & OP_EXEC)) {
620 			pgn->flags.childMade = true;
621 			GNode_UpdateYoungestChild(pgn, gn);
622 		}
623 	} else if (opts.keepgoing) {
624 		pgn->flags.remake = false;
625 	} else {
626 		PrintOnError(gn, "\nStop.\n");
627 		exit(1);
628 	}
629 	return true;
630 }
631 
632 static void
MakeOther(GNode * gn,GNode * pgn)633 MakeOther(GNode *gn, GNode *pgn)
634 {
635 
636 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL) {
637 		const char *target = GNode_VarTarget(gn);
638 		Var_Set(pgn, IMPSRC, target != NULL ? target : "");
639 	}
640 
641 	switch (gn->made) {
642 	case BEINGMADE:
643 		Error("Graph cycles through %s", gn->name);
644 		gn->made = ERROR;
645 		pgn->flags.remake = false;
646 		break;
647 	case MADE:
648 		if (!(gn->type & OP_EXEC)) {
649 			pgn->flags.childMade = true;
650 			GNode_UpdateYoungestChild(pgn, gn);
651 		}
652 		break;
653 	case UPTODATE:
654 		if (!(gn->type & OP_EXEC))
655 			GNode_UpdateYoungestChild(pgn, gn);
656 		break;
657 	default:
658 		break;
659 	}
660 }
661 
662 /*
663  * Make a target.
664  *
665  * If an error is detected and not being ignored, the process exits.
666  *
667  * Input:
668  *	gn		The node to make
669  *	pgn		Parent to abort if necessary
670  *
671  * Output:
672  *	gn->made
673  *		UPTODATE	gn was already up-to-date.
674  *		MADE		gn was recreated successfully.
675  *		ERROR		An error occurred while gn was being created,
676  *				either due to missing commands or in -k mode.
677  *		ABORTED		gn was not remade because one of its
678  *				dependencies could not be made due to errors.
679  */
680 void
Compat_Make(GNode * gn,GNode * pgn)681 Compat_Make(GNode *gn, GNode *pgn)
682 {
683 	if (shellName == NULL)	/* we came here from jobs */
684 		Shell_Init();
685 
686 	if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
687 		if (!MakeUnmade(gn, pgn))
688 			goto cohorts;
689 
690 		/* XXX: Replace with GNode_IsError(gn) */
691 	} else if (gn->made == ERROR) {
692 		/*
693 		 * Already had an error when making this.
694 		 * Tell the parent to abort.
695 		 */
696 		pgn->flags.remake = false;
697 	} else {
698 		MakeOther(gn, pgn);
699 	}
700 
701 cohorts:
702 	MakeNodes(&gn->cohorts, pgn);
703 }
704 
705 static void
MakeBeginNode(void)706 MakeBeginNode(void)
707 {
708 	GNode *gn = Targ_FindNode(".BEGIN");
709 	if (gn == NULL)
710 		return;
711 
712 	Compat_Make(gn, gn);
713 	if (GNode_IsError(gn)) {
714 		PrintOnError(gn, "\nStop.\n");
715 		exit(1);
716 	}
717 }
718 
719 static void
InitSignals(void)720 InitSignals(void)
721 {
722 	if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
723 		bmake_signal(SIGINT, CompatInterrupt);
724 	if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
725 		bmake_signal(SIGTERM, CompatInterrupt);
726 	if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
727 		bmake_signal(SIGHUP, CompatInterrupt);
728 	if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
729 		bmake_signal(SIGQUIT, CompatInterrupt);
730 }
731 
732 void
Compat_MakeAll(GNodeList * targs)733 Compat_MakeAll(GNodeList *targs)
734 {
735 	GNode *errorNode = NULL;
736 
737 	if (shellName == NULL)
738 		Shell_Init();
739 
740 	InitSignals();
741 
742 	/*
743 	 * Create the .END node now, to keep the (debug) output of the
744 	 * counter.mk test the same as before 2020-09-23.  This
745 	 * implementation detail probably doesn't matter though.
746 	 */
747 	(void)Targ_GetEndNode();
748 
749 	if (!opts.query)
750 		MakeBeginNode();
751 
752 	/*
753 	 * Expand .USE nodes right now, because they can modify the structure
754 	 * of the tree.
755 	 */
756 	Make_ExpandUse(targs);
757 
758 	while (!Lst_IsEmpty(targs)) {
759 		GNode *gn = Lst_Dequeue(targs);
760 		Compat_Make(gn, gn);
761 
762 		if (gn->made == UPTODATE) {
763 			printf("`%s' is up to date.\n", gn->name);
764 		} else if (gn->made == ABORTED) {
765 			printf("`%s' not remade because of errors.\n",
766 			    gn->name);
767 		}
768 		if (GNode_IsError(gn) && errorNode == NULL)
769 			errorNode = gn;
770 	}
771 
772 	if (errorNode == NULL) {
773 		GNode *endNode = Targ_GetEndNode();
774 		Compat_Make(endNode, endNode);
775 		if (GNode_IsError(endNode))
776 			errorNode = endNode;
777 	}
778 
779 	if (errorNode != NULL) {
780 		if (DEBUG(GRAPH2))
781 			Targ_PrintGraph(2);
782 		else if (DEBUG(GRAPH3))
783 			Targ_PrintGraph(3);
784 		PrintOnError(errorNode, "\nStop.\n");
785 		exit(1);
786 	}
787 }
788