1 /* $NetBSD: compat.c,v 1.183 2020/11/15 22:31:03 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
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.
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
74 * The routines in this file implement the full-compatibility
75 * mode of PMake. Most of the special functionality of PMake
76 * is available in this mode. Things not supported:
78 * - friendly variable substitution.
81 * Compat_Run Initialize things for this module and recreate
82 * thems as need creatin'
88 #include <sys/types.h>
99 #include "pathnames.h"
101 /* "@(#)compat.c 8.2 (Berkeley) 3/19/94" */
102 MAKE_RCSID("$NetBSD: compat.c,v 1.183 2020/11/15 22:31:03 rillig Exp $");
104 static GNode *curTarg = NULL;
105 static pid_t compatChild;
106 static int compatSigno;
109 * CompatDeleteTarget -- delete the file of a failed, interrupted, or
110 * otherwise duffed target if not inhibited by .PRECIOUS.
113 CompatDeleteTarget(GNode *gn)
115 if (gn != NULL && !Targ_Precious(gn)) {
116 const char *file = GNode_VarTarget(gn);
118 if (!opts.noExecute && eunlink(file) != -1) {
119 Error("*** %s removed", file);
124 /* Interrupt the creation of the current target and remove it if it ain't
125 * precious. Then exit.
127 * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
129 * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
130 * left the logic alone for now. - dholland 20160826
133 CompatInterrupt(int signo)
135 CompatDeleteTarget(curTarg);
137 if (curTarg != NULL && !Targ_Precious(curTarg)) {
139 * Run .INTERRUPT only if hit with interrupt signal
141 if (signo == SIGINT) {
142 GNode *gn = Targ_FindNode(".INTERRUPT");
149 if (signo == SIGQUIT)
153 * If there is a child running, pass the signal on.
154 * We will exist after it has exited.
157 if (compatChild > 0) {
158 KILLPG(compatChild, signo);
160 bmake_signal(signo, SIG_DFL);
165 /* Execute the next command for a target. If the command returns an error,
166 * the node's made field is set to ERROR and creation stops.
169 * cmdp Command to execute
170 * gnp Node from which the command came
173 * 0 if the command succeeded, 1 if an error occurred.
176 Compat_RunCommand(const char *cmdp, GNode *gn)
178 char *cmdStart; /* Start of expanded command */
180 Boolean silent; /* Don't print command */
181 Boolean doIt; /* Execute even if -n */
182 volatile Boolean errCheck; /* Check errors */
183 WAIT_T reason; /* Reason for child's death */
184 int status; /* Description of child's death */
185 pid_t cpid; /* Child actually found */
186 pid_t retstat; /* Result of wait */
187 StringListNode *cmdNode; /* Node where current command is located */
188 const char **volatile av; /* Argument vector for thing to exec */
189 char **volatile mav; /* Copy of the argument vector for freeing */
190 Boolean useShell; /* TRUE if command should be executed
192 const char *volatile cmd = cmdp;
194 silent = (gn->type & OP_SILENT) != 0;
195 errCheck = !(gn->type & OP_IGNORE);
198 /* Luckily the commands don't end up in a string pool, otherwise
199 * this comparison could match too early, in a dependency using "..."
200 * for delayed commands, run in parallel mode, using the same shell
201 * command line more than once; see JobPrintCommand.
202 * TODO: write a unit-test to protect against this potential bug. */
203 cmdNode = Lst_FindDatum(gn->commands, cmd);
204 (void)Var_Subst(cmd, gn, VARE_WANTRES, &cmdStart);
205 /* TODO: handle errors */
207 if (cmdStart[0] == '\0') {
212 LstNode_Set(cmdNode, cmdStart);
214 if (gn->type & OP_SAVE_CMDS) {
215 GNode *endNode = Targ_GetEndNode();
217 Lst_Append(endNode->commands, cmdStart);
221 if (strcmp(cmdStart, "...") == 0) {
222 gn->type |= OP_SAVE_CMDS;
228 silent = !DEBUG(LOUD);
229 else if (*cmd == '-')
231 else if (*cmd == '+') {
233 if (!shellName) /* we came here from jobs */
240 while (ch_isspace(*cmd))
244 * If we did not end up with a command, just skip it.
249 #if !defined(MAKE_NATIVE)
251 * In a non-native build, the host environment might be weird enough
252 * that it's necessary to go through a shell to get the correct
253 * behaviour. Or perhaps the shell has been replaced with something
254 * that does extra logging, and that should not be bypassed.
259 * Search for meta characters in the command. If there are no meta
260 * characters, there's no need to execute a shell to execute the
263 * Additionally variable assignments and empty commands
264 * go to the shell. Therefore treat '=' and ':' like shell
265 * meta characters as documented in make(1).
268 useShell = needshell(cmd);
272 * Print the command before echoing if we're not supposed to be quiet for
273 * this one. We also print the command if -n given.
275 if (!silent || !GNode_ShouldExecute(gn)) {
281 * If we're not supposed to execute any commands, this is as far as
284 if (!doIt && !GNode_ShouldExecute(gn))
287 DEBUG1(JOB, "Execute: '%s'\n", cmd);
291 * We need to pass the command off to the shell, typically
292 * because the command contains a "meta" character.
294 static const char *shargv[5];
296 /* The following work for any of the builtin shell specs. */
298 shargv[shargc++] = shellPath;
299 if (errCheck && shellErrFlag)
300 shargv[shargc++] = shellErrFlag;
301 shargv[shargc++] = DEBUG(SHELL) ? "-xc" : "-c";
302 shargv[shargc++] = cmd;
303 shargv[shargc] = NULL;
309 * No meta-characters, so no need to exec a shell. Break the command
310 * into words to form an argument vector we can execute.
312 Words words = Str_Words(cmd, FALSE);
325 * Fork and execute the single command. If the fork fails, we abort.
327 compatChild = cpid = vFork();
329 Fatal("Could not fork");
338 (void)execvp(av[0], (char *const *)UNCONST(av));
339 execDie("exec", av[0]);
345 /* XXX: Memory management looks suspicious here. */
346 /* XXX: Setting a list item to NULL is unexpected. */
347 LstNode_SetNull(cmdNode);
351 meta_compat_parent(cpid);
356 * The child is off and running. Now all we can do is wait...
358 while ((retstat = wait(&reason)) != cpid) {
360 JobReapChild(retstat, reason, FALSE); /* not ours? */
361 if (retstat == -1 && errno != EINTR) {
367 Fatal("error in wait: %d: %s", retstat, strerror(errno));
369 if (WIFSTOPPED(reason)) {
370 status = WSTOPSIG(reason); /* stopped */
371 } else if (WIFEXITED(reason)) {
372 status = WEXITSTATUS(reason); /* exited */
373 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
375 meta_cmd_finish(NULL);
381 debug_printf("\n*** Failed target: %s\n*** Failed command: ",
384 /* Replace runs of whitespace with a single space, to reduce
385 * the amount of whitespace for multi-line command lines. */
387 if (ch_isspace(*p)) {
389 cpp_skip_whitespace(&p);
391 debug_printf("%c", *p);
397 printf("*** Error code %d", status);
400 status = WTERMSIG(reason); /* signaled */
401 printf("*** Signal %d", status);
405 if (!WIFEXITED(reason) || status != 0) {
409 meta_job_error(NULL, gn, 0, status);
413 if (opts.keepgoing) {
414 /* Abort the current target, but let others continue. */
415 printf(" (continuing)\n");
420 CompatDeleteTarget(gn);
423 * Continue executing commands for this target.
424 * If we return 0, this will happen...
426 printf(" (ignored)\n");
434 bmake_signal(compatSigno, SIG_DFL);
435 kill(myPid, compatSigno);
442 RunCommands(GNode *gn)
445 for (ln = gn->commands->first; ln != NULL; ln = ln->next) {
446 const char *cmd = ln->datum;
447 if (Compat_RunCommand(cmd, gn) != 0)
453 MakeNodes(GNodeList *gnodes, GNode *pgn)
456 for (ln = gnodes->first; ln != NULL; ln = ln->next) {
457 GNode *cohort = ln->datum;
458 Compat_Make(cohort, pgn);
464 * If an error is detected and not being ignored, the process exits.
467 * gn The node to make
468 * pgn Parent to abort if necessary
471 Compat_Make(GNode *gn, GNode *pgn)
473 if (shellName == NULL) /* we came here from jobs */
476 if (gn->made == UNMADE && (gn == pgn || !(pgn->type & OP_MADE))) {
478 * First mark ourselves to be made, then apply whatever transformations
479 * the suffix module thinks are necessary. Once that's done, we can
480 * descend and make all our children. If any of them has an error
481 * but the -k flag was given, our 'make' field will be set to FALSE
482 * again. This is our signal to not attempt to do anything but abort
483 * our parent as well.
486 gn->made = BEINGMADE;
487 if (!(gn->type & OP_MADE))
489 MakeNodes(gn->children, gn);
490 if (!(gn->flags & REMAKE)) {
492 pgn->flags &= ~(unsigned)REMAKE;
496 if (Lst_FindDatum(gn->implicitParents, pgn) != NULL)
497 Var_Set(IMPSRC, GNode_VarTarget(gn), pgn);
500 * All the children were made ok. Now youngestChild->mtime contains the
501 * modification time of the newest child, we need to find out if we
502 * exist and when we were modified last. The criteria for datedness
503 * are defined by GNode_IsOODate.
505 DEBUG1(MAKE, "Examining %s...", gn->name);
506 if (!GNode_IsOODate(gn)) {
508 DEBUG0(MAKE, "up-to-date.\n");
511 DEBUG0(MAKE, "out-of-date.\n");
514 * If the user is just seeing if something is out-of-date, exit now
515 * to tell him/her "yes".
521 * We need to be re-made. We also have to make sure we've got a $?
522 * variable. To be nice, we also define the $> variable using
528 * Alter our type to tell if errors should be ignored or things
529 * should not be printed so CompatRunCommand knows what to do.
532 gn->type |= OP_IGNORE;
534 gn->type |= OP_SILENT;
536 if (Job_CheckCommands(gn, Fatal)) {
538 * Our commands are ok, but we still have to worry about the -t
541 if (!opts.touchFlag || (gn->type & OP_MAKE)) {
544 if (useMeta && GNode_ShouldExecute(gn)) {
545 meta_job_start(NULL, gn);
551 Job_Touch(gn, (gn->type & OP_SILENT) != 0);
557 if (useMeta && GNode_ShouldExecute(gn)) {
558 if (meta_job_finish(NULL) != 0)
563 if (gn->made != ERROR) {
565 * If the node was made successfully, mark it so, update
566 * its modification time and timestamp all its parents.
567 * This is to keep its state from affecting that of its parent.
570 if (Make_Recheck(gn) == 0)
572 if (!(gn->type & OP_EXEC)) {
573 pgn->flags |= CHILDMADE;
574 GNode_UpdateYoungestChild(pgn, gn);
576 } else if (opts.keepgoing) {
577 pgn->flags &= ~(unsigned)REMAKE;
579 PrintOnError(gn, "\nStop.");
582 } else if (gn->made == ERROR) {
583 /* Already had an error when making this. Tell the parent to abort. */
584 pgn->flags &= ~(unsigned)REMAKE;
586 if (Lst_FindDatum(gn->implicitParents, pgn) != NULL) {
587 const char *target = GNode_VarTarget(gn);
588 Var_Set(IMPSRC, target != NULL ? target : "", pgn);
592 Error("Graph cycles through %s", gn->name);
594 pgn->flags &= ~(unsigned)REMAKE;
597 if (!(gn->type & OP_EXEC)) {
598 pgn->flags |= CHILDMADE;
599 GNode_UpdateYoungestChild(pgn, gn);
603 if (!(gn->type & OP_EXEC))
604 GNode_UpdateYoungestChild(pgn, gn);
612 MakeNodes(gn->cohorts, pgn);
615 /* Initialize this module and start making.
618 * targs The target nodes to re-create
621 Compat_Run(GNodeList *targs)
623 GNode *gn = NULL; /* Current root target */
624 int errors; /* Number of targets not remade due to errors */
629 if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN)
630 bmake_signal(SIGINT, CompatInterrupt);
631 if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN)
632 bmake_signal(SIGTERM, CompatInterrupt);
633 if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN)
634 bmake_signal(SIGHUP, CompatInterrupt);
635 if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
636 bmake_signal(SIGQUIT, CompatInterrupt);
638 /* Create the .END node now, to keep the (debug) output of the
639 * counter.mk test the same as before 2020-09-23. This implementation
640 * detail probably doesn't matter though. */
641 (void)Targ_GetEndNode();
643 * If the user has defined a .BEGIN target, execute the commands attached
646 if (!opts.queryFlag) {
647 gn = Targ_FindNode(".BEGIN");
650 if (gn->made == ERROR) {
651 PrintOnError(gn, "\nStop.");
658 * Expand .USE nodes right now, because they can modify the structure
661 Make_ExpandUse(targs);
664 * For each entry in the list of targets to create, call Compat_Make on
665 * it to create the thing. Compat_Make will leave the 'made' field of gn
666 * in one of several states:
667 * UPTODATE gn was already up-to-date
668 * MADE gn was recreated successfully
669 * ERROR An error occurred while gn was being created
670 * ABORTED gn was not remade because one of its inferiors
671 * could not be made due to errors.
674 while (!Lst_IsEmpty(targs)) {
675 gn = Lst_Dequeue(targs);
678 if (gn->made == UPTODATE) {
679 printf("`%s' is up to date.\n", gn->name);
680 } else if (gn->made == ABORTED) {
681 printf("`%s' not remade because of errors.\n", gn->name);
687 * If the user has defined a .END target, run its commands.
690 GNode *endNode = Targ_GetEndNode();
691 Compat_Make(endNode, endNode);
692 /* XXX: Did you mean endNode->made instead of gn->made? */
693 if (gn->made == ERROR) {
694 PrintOnError(gn, "\nStop.");