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