xref: /freebsd/contrib/bmake/compat.c (revision 3b68c491d37196bb76a95bce3c02f7c6d5ba22fd)
1 /*	$NetBSD: compat.c,v 1.259 2024/06/15 20:02:45 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.259 2024/06/15 20:02:45 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
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
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
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
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
207 Compat_Spawn(const char **av)
208 {
209 	int pid = vfork();
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
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 
253 	silent = (gn->type & OP_SILENT) != OP_NONE;
254 	errCheck = !(gn->type & OP_IGNORE);
255 	doIt = false;
256 
257 	cmdStart = Var_SubstInTarget(cmd, gn);
258 	/* TODO: handle errors */
259 
260 	if (cmdStart[0] == '\0') {
261 		free(cmdStart);
262 		return true;
263 	}
264 	cmd = cmdStart;
265 	LstNode_Set(ln, cmdStart);
266 
267 	if (gn->type & OP_SAVE_CMDS) {
268 		GNode *endNode = Targ_GetEndNode();
269 		if (gn != endNode) {
270 			/*
271 			 * Append the expanded command, to prevent the
272 			 * local variables from being interpreted in the
273 			 * scope of the .END node.
274 			 *
275 			 * A probably unintended side effect of this is that
276 			 * the expanded command will be expanded again in the
277 			 * .END node.  Therefore, a literal '$' in these
278 			 * commands must be written as '$$$$' instead of the
279 			 * usual '$$'.
280 			 */
281 			Lst_Append(&endNode->commands, cmdStart);
282 			goto register_command;
283 		}
284 	}
285 	if (strcmp(cmdStart, "...") == 0) {
286 		gn->type |= OP_SAVE_CMDS;
287 	register_command:
288 		Parse_RegisterCommand(cmdStart);
289 		return true;
290 	}
291 
292 	for (;;) {
293 		if (*cmd == '@')
294 			silent = !DEBUG(LOUD);
295 		else if (*cmd == '-')
296 			errCheck = false;
297 		else if (*cmd == '+')
298 			doIt = true;
299 		else if (!ch_isspace(*cmd))
300 			/* Ignore whitespace for compatibility with gnu make */
301 			break;
302 		cmd++;
303 	}
304 
305 	while (ch_isspace(*cmd))
306 		cmd++;
307 	if (cmd[0] == '\0')
308 		goto register_command;
309 
310 	useShell = UseShell(cmd);
311 
312 	if (!silent || !GNode_ShouldExecute(gn)) {
313 		printf("%s\n", cmd);
314 		fflush(stdout);
315 	}
316 
317 	if (!doIt && !GNode_ShouldExecute(gn))
318 		goto register_command;
319 
320 	DEBUG1(JOB, "Execute: '%s'\n", cmd);
321 
322 	if (useShell && shellPath == NULL)
323 		Shell_Init();		/* we need shellPath */
324 
325 	if (useShell) {
326 		static const char *shargv[5];
327 
328 		/* The following work for any of the builtin shell specs. */
329 		int shargc = 0;
330 		shargv[shargc++] = shellPath;
331 		if (errCheck && shellErrFlag != NULL)
332 			shargv[shargc++] = shellErrFlag;
333 		shargv[shargc++] = DEBUG(SHELL) ? "-xc" : "-c";
334 		shargv[shargc++] = cmd;
335 		shargv[shargc] = NULL;
336 		av = shargv;
337 		bp = NULL;
338 		mav = NULL;
339 	} else {
340 		Words words = Str_Words(cmd, false);
341 		mav = words.words;
342 		bp = words.freeIt;
343 		av = (void *)mav;
344 	}
345 
346 #ifdef USE_META
347 	if (useMeta)
348 		meta_compat_start();
349 #endif
350 
351 	Var_ReexportVars(gn);
352 
353 	compatChild = Compat_Spawn(av);
354 	free(mav);
355 	free(bp);
356 
357 	/* XXX: Memory management looks suspicious here. */
358 	/* XXX: Setting a list item to NULL is unexpected. */
359 	LstNode_SetNull(ln);
360 
361 #ifdef USE_META
362 	if (useMeta)
363 		meta_compat_parent(compatChild);
364 #endif
365 
366 	/* The child is off and running. Now all we can do is wait... */
367 	while ((retstat = wait(&reason)) != compatChild) {
368 		if (retstat > 0)
369 			JobReapChild(retstat, reason, false); /* not ours? */
370 		if (retstat == -1 && errno != EINTR)
371 			break;
372 	}
373 
374 	if (retstat < 0)
375 		Fatal("error in wait: %d: %s", retstat, strerror(errno));
376 
377 	if (WIFSTOPPED(reason)) {
378 		status = WSTOPSIG(reason);
379 	} else if (WIFEXITED(reason)) {
380 		status = WEXITSTATUS(reason);
381 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
382 		if (useMeta)
383 			meta_cmd_finish(NULL);
384 #endif
385 		if (status != 0) {
386 			if (DEBUG(ERROR))
387 				DebugFailedTarget(cmd, gn);
388 			printf("*** Error code %d", status);
389 		}
390 	} else {
391 		status = WTERMSIG(reason);
392 		printf("*** Signal %d", status);
393 	}
394 
395 
396 	if (!WIFEXITED(reason) || status != 0) {
397 		if (errCheck) {
398 #ifdef USE_META
399 			if (useMeta)
400 				meta_job_error(NULL, gn, false, status);
401 #endif
402 			gn->made = ERROR;
403 			if (WIFEXITED(reason))
404 				gn->exit_status = status;
405 			if (opts.keepgoing) {
406 				/*
407 				 * Abort the current target,
408 				 * but let others continue.
409 				 */
410 				printf(" (continuing)\n");
411 			} else {
412 				printf("\n");
413 			}
414 			if (deleteOnError)
415 				CompatDeleteTarget(gn);
416 		} else {
417 			/*
418 			 * Continue executing commands for this target.
419 			 * If we return 0, this will happen...
420 			 */
421 			printf(" (ignored)\n");
422 			status = 0;
423 		}
424 		fflush(stdout);
425 	}
426 
427 	free(cmdStart);
428 	compatChild = 0;
429 	if (compatSigno != 0) {
430 		bmake_signal(compatSigno, SIG_DFL);
431 		kill(myPid, compatSigno);
432 	}
433 
434 	return status == 0;
435 }
436 
437 static void
438 RunCommands(GNode *gn)
439 {
440 	StringListNode *ln;
441 
442 	for (ln = gn->commands.first; ln != NULL; ln = ln->next) {
443 		const char *cmd = ln->datum;
444 		if (!Compat_RunCommand(cmd, gn, ln))
445 			break;
446 	}
447 }
448 
449 static void
450 MakeInRandomOrder(GNode **gnodes, GNode **end, GNode *pgn)
451 {
452 	GNode **it;
453 	size_t r;
454 
455 	for (r = (size_t)(end - gnodes); r >= 2; r--) {
456 		/* Biased, but irrelevant in practice. */
457 		size_t i = (size_t)random() % r;
458 		GNode *t = gnodes[r - 1];
459 		gnodes[r - 1] = gnodes[i];
460 		gnodes[i] = t;
461 	}
462 
463 	for (it = gnodes; it != end; it++)
464 		Compat_Make(*it, pgn);
465 }
466 
467 static void
468 MakeWaitGroupsInRandomOrder(GNodeList *gnodes, GNode *pgn)
469 {
470 	Vector vec;
471 	GNodeListNode *ln;
472 	GNode **nodes;
473 	size_t i, n, start;
474 
475 	Vector_Init(&vec, sizeof(GNode *));
476 	for (ln = gnodes->first; ln != NULL; ln = ln->next)
477 		*(GNode **)Vector_Push(&vec) = ln->datum;
478 	nodes = vec.items;
479 	n = vec.len;
480 
481 	start = 0;
482 	for (i = 0; i < n; i++) {
483 		if (nodes[i]->type & OP_WAIT) {
484 			MakeInRandomOrder(nodes + start, nodes + i, pgn);
485 			Compat_Make(nodes[i], pgn);
486 			start = i + 1;
487 		}
488 	}
489 	MakeInRandomOrder(nodes + start, nodes + i, pgn);
490 
491 	Vector_Done(&vec);
492 }
493 
494 static void
495 MakeNodes(GNodeList *gnodes, GNode *pgn)
496 {
497 	GNodeListNode *ln;
498 
499 	if (Lst_IsEmpty(gnodes))
500 		return;
501 	if (opts.randomizeTargets) {
502 		MakeWaitGroupsInRandomOrder(gnodes, pgn);
503 		return;
504 	}
505 
506 	for (ln = gnodes->first; ln != NULL; ln = ln->next) {
507 		GNode *cgn = ln->datum;
508 		Compat_Make(cgn, pgn);
509 	}
510 }
511 
512 static bool
513 MakeUnmade(GNode *gn, GNode *pgn)
514 {
515 
516 	assert(gn->made == UNMADE);
517 
518 	/*
519 	 * First mark ourselves to be made, then apply whatever transformations
520 	 * the suffix module thinks are necessary. Once that's done, we can
521 	 * descend and make all our children. If any of them has an error
522 	 * but the -k flag was given, our 'make' field will be set to false
523 	 * again. This is our signal to not attempt to do anything but abort
524 	 * our parent as well.
525 	 */
526 	gn->flags.remake = true;
527 	gn->made = BEINGMADE;
528 
529 	if (!(gn->type & OP_MADE))
530 		Suff_FindDeps(gn);
531 
532 	MakeNodes(&gn->children, gn);
533 
534 	if (!gn->flags.remake) {
535 		gn->made = ABORTED;
536 		pgn->flags.remake = false;
537 		return false;
538 	}
539 
540 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL)
541 		Var_Set(pgn, IMPSRC, GNode_VarTarget(gn));
542 
543 	/*
544 	 * All the children were made ok. Now youngestChild->mtime contains the
545 	 * modification time of the newest child, we need to find out if we
546 	 * exist and when we were modified last. The criteria for datedness
547 	 * are defined by GNode_IsOODate.
548 	 */
549 	DEBUG1(MAKE, "Examining %s...", gn->name);
550 	if (!GNode_IsOODate(gn)) {
551 		gn->made = UPTODATE;
552 		DEBUG0(MAKE, "up-to-date.\n");
553 		return false;
554 	}
555 
556 	/*
557 	 * If the user is just seeing if something is out-of-date, exit now
558 	 * to tell him/her "yes".
559 	 */
560 	DEBUG0(MAKE, "out-of-date.\n");
561 	if (opts.query && gn != Targ_GetEndNode())
562 		exit(1);
563 
564 	/*
565 	 * We need to be re-made.
566 	 * Ensure that $? (.OODATE) and $> (.ALLSRC) are both set.
567 	 */
568 	GNode_SetLocalVars(gn);
569 
570 	/*
571 	 * Alter our type to tell if errors should be ignored or things
572 	 * should not be printed so Compat_RunCommand knows what to do.
573 	 */
574 	if (opts.ignoreErrors)
575 		gn->type |= OP_IGNORE;
576 	if (opts.silent)
577 		gn->type |= OP_SILENT;
578 
579 	if (Job_CheckCommands(gn, Fatal)) {
580 		if (!opts.touch || (gn->type & OP_MAKE)) {
581 			curTarg = gn;
582 #ifdef USE_META
583 			if (useMeta && GNode_ShouldExecute(gn))
584 				meta_job_start(NULL, gn);
585 #endif
586 			RunCommands(gn);
587 			curTarg = NULL;
588 		} else {
589 			Job_Touch(gn, (gn->type & OP_SILENT) != OP_NONE);
590 		}
591 	} else {
592 		gn->made = ERROR;
593 	}
594 #ifdef USE_META
595 	if (useMeta && GNode_ShouldExecute(gn)) {
596 		if (meta_job_finish(NULL) != 0)
597 			gn->made = ERROR;
598 	}
599 #endif
600 
601 	if (gn->made != ERROR) {
602 		/*
603 		 * If the node was made successfully, mark it so, update
604 		 * its modification time and timestamp all its parents.
605 		 * This is to keep its state from affecting that of its parent.
606 		 */
607 		gn->made = MADE;
608 		if (Make_Recheck(gn) == 0)
609 			pgn->flags.force = true;
610 		if (!(gn->type & OP_EXEC)) {
611 			pgn->flags.childMade = true;
612 			GNode_UpdateYoungestChild(pgn, gn);
613 		}
614 	} else if (opts.keepgoing) {
615 		pgn->flags.remake = false;
616 	} else {
617 		PrintOnError(gn, "\nStop.\n");
618 		exit(1);
619 	}
620 	return true;
621 }
622 
623 static void
624 MakeOther(GNode *gn, GNode *pgn)
625 {
626 
627 	if (Lst_FindDatum(&gn->implicitParents, pgn) != NULL) {
628 		const char *target = GNode_VarTarget(gn);
629 		Var_Set(pgn, IMPSRC, target != NULL ? target : "");
630 	}
631 
632 	switch (gn->made) {
633 	case BEINGMADE:
634 		Error("Graph cycles through %s", gn->name);
635 		gn->made = ERROR;
636 		pgn->flags.remake = false;
637 		break;
638 	case MADE:
639 		if (!(gn->type & OP_EXEC)) {
640 			pgn->flags.childMade = true;
641 			GNode_UpdateYoungestChild(pgn, gn);
642 		}
643 		break;
644 	case UPTODATE:
645 		if (!(gn->type & OP_EXEC))
646 			GNode_UpdateYoungestChild(pgn, gn);
647 		break;
648 	default:
649 		break;
650 	}
651 }
652 
653 /*
654  * Make a target.
655  *
656  * If an error is detected and not being ignored, the process exits.
657  *
658  * Input:
659  *	gn		The node to make
660  *	pgn		Parent to abort if necessary
661  *
662  * Output:
663  *	gn->made
664  *		UPTODATE	gn was already up-to-date.
665  *		MADE		gn was recreated successfully.
666  *		ERROR		An error occurred while gn was being created,
667  *				either due to missing commands or in -k mode.
668  *		ABORTED		gn was not remade because one of its
669  *				dependencies could not be made due to errors.
670  */
671 void
672 Compat_Make(GNode *gn, GNode *pgn)
673 {
674 	if (shellName == NULL)	/* we came here from jobs */
675 		Shell_Init();
676 
677 	if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
678 		if (!MakeUnmade(gn, pgn))
679 			goto cohorts;
680 
681 		/* XXX: Replace with GNode_IsError(gn) */
682 	} else if (gn->made == ERROR) {
683 		/*
684 		 * Already had an error when making this.
685 		 * Tell the parent to abort.
686 		 */
687 		pgn->flags.remake = false;
688 	} else {
689 		MakeOther(gn, pgn);
690 	}
691 
692 cohorts:
693 	MakeNodes(&gn->cohorts, pgn);
694 }
695 
696 static void
697 MakeBeginNode(void)
698 {
699 	GNode *gn = Targ_FindNode(".BEGIN");
700 	if (gn == NULL)
701 		return;
702 
703 	Compat_Make(gn, gn);
704 	if (GNode_IsError(gn)) {
705 		PrintOnError(gn, "\nStop.\n");
706 		exit(1);
707 	}
708 }
709 
710 static void
711 InitSignals(void)
712 {
713 	if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
714 		bmake_signal(SIGINT, CompatInterrupt);
715 	if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
716 		bmake_signal(SIGTERM, CompatInterrupt);
717 	if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
718 		bmake_signal(SIGHUP, CompatInterrupt);
719 	if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
720 		bmake_signal(SIGQUIT, CompatInterrupt);
721 }
722 
723 void
724 Compat_MakeAll(GNodeList *targs)
725 {
726 	GNode *errorNode = NULL;
727 
728 	if (shellName == NULL)
729 		Shell_Init();
730 
731 	InitSignals();
732 
733 	/*
734 	 * Create the .END node now, to keep the (debug) output of the
735 	 * counter.mk test the same as before 2020-09-23.  This
736 	 * implementation detail probably doesn't matter though.
737 	 */
738 	(void)Targ_GetEndNode();
739 
740 	if (!opts.query)
741 		MakeBeginNode();
742 
743 	/*
744 	 * Expand .USE nodes right now, because they can modify the structure
745 	 * of the tree.
746 	 */
747 	Make_ExpandUse(targs);
748 
749 	while (!Lst_IsEmpty(targs)) {
750 		GNode *gn = Lst_Dequeue(targs);
751 		Compat_Make(gn, gn);
752 
753 		if (gn->made == UPTODATE) {
754 			printf("`%s' is up to date.\n", gn->name);
755 		} else if (gn->made == ABORTED) {
756 			printf("`%s' not remade because of errors.\n",
757 			    gn->name);
758 		}
759 		if (GNode_IsError(gn) && errorNode == NULL)
760 			errorNode = gn;
761 	}
762 
763 	if (errorNode == NULL) {
764 		GNode *endNode = Targ_GetEndNode();
765 		Compat_Make(endNode, endNode);
766 		if (GNode_IsError(endNode))
767 			errorNode = endNode;
768 	}
769 
770 	if (errorNode != NULL) {
771 		if (DEBUG(GRAPH2))
772 			Targ_PrintGraph(2);
773 		else if (DEBUG(GRAPH3))
774 			Targ_PrintGraph(3);
775 		PrintOnError(errorNode, "\nStop.\n");
776 		exit(1);
777 	}
778 }
779