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