1 /* $NetBSD: compat.c,v 1.139 2020/08/30 20:08:47 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
73 static char rcsid[] = "$NetBSD: compat.c,v 1.139 2020/08/30 20:08:47 rillig Exp $";
75 #include <sys/cdefs.h>
78 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
80 __RCSID("$NetBSD: compat.c,v 1.139 2020/08/30 20:08:47 rillig Exp $");
87 * The routines in this file implement the full-compatibility
88 * mode of PMake. Most of the special functionality of PMake
89 * is available in this mode. Things not supported:
91 * - friendly variable substitution.
94 * Compat_Run Initialize things for this module and recreate
95 * thems as need creatin'
101 #include <sys/types.h>
102 #include <sys/stat.h>
114 #include "metachar.h"
115 #include "pathnames.h"
118 static GNode *curTarg = NULL;
119 static GNode *ENDNode;
120 static void CompatInterrupt(int);
121 static pid_t compatChild;
122 static int compatSigno;
125 * CompatDeleteTarget -- delete a failed, interrupted, or otherwise
126 * duffed target if not inhibited by .PRECIOUS.
129 CompatDeleteTarget(GNode *gn)
131 if ((gn != NULL) && !Targ_Precious (gn)) {
133 const char *file = Var_Value(TARGET, gn, &p1);
135 if (!noExecute && eunlink(file) != -1) {
136 Error("*** %s removed", file);
143 /* Interrupt the creation of the current target and remove it if it ain't
144 * precious. Then exit.
146 * If .INTERRUPT exists, its commands are run first WITH INTERRUPTS IGNORED.
148 * XXX: is .PRECIOUS supposed to inhibit .INTERRUPT? I doubt it, but I've
149 * left the logic alone for now. - dholland 20160826
152 CompatInterrupt(int signo)
156 CompatDeleteTarget(curTarg);
158 if ((curTarg != NULL) && !Targ_Precious (curTarg)) {
160 * Run .INTERRUPT only if hit with interrupt signal
162 if (signo == SIGINT) {
163 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
169 if (signo == SIGQUIT)
172 * If there is a child running, pass the signal on
173 * we will exist after it has exited.
176 if (compatChild > 0) {
177 KILLPG(compatChild, signo);
179 bmake_signal(signo, SIG_DFL);
185 *-----------------------------------------------------------------------
186 * CompatRunCommand --
187 * Execute the next command for a target. If the command returns an
188 * error, the node's made field is set to ERROR and creation stops.
191 * cmdp Command to execute
192 * gnp Node from which the command came
195 * 0 if the command succeeded, 1 if an error occurred.
198 * The node's 'made' field may be set to ERROR.
200 *-----------------------------------------------------------------------
203 CompatRunCommand(void *cmdp, void *gnp)
205 char *cmdStart; /* Start of expanded command */
207 Boolean silent, /* Don't print command */
208 doIt; /* Execute even if -n */
209 volatile Boolean errCheck; /* Check errors */
210 WAIT_T reason; /* Reason for child's death */
211 int status; /* Description of child's death */
212 pid_t cpid; /* Child actually found */
213 pid_t retstat; /* Result of wait */
214 LstNode cmdNode; /* Node where current command is located */
215 const char ** volatile av; /* Argument vector for thing to exec */
216 char ** volatile mav;/* Copy of the argument vector for freeing */
217 Boolean useShell; /* TRUE if command should be executed
219 char * volatile cmd = (char *)cmdp;
220 GNode *gn = (GNode *)gnp;
222 silent = (gn->type & OP_SILENT) != 0;
223 errCheck = !(gn->type & OP_IGNORE);
226 cmdNode = Lst_FindDatum(gn->commands, cmd);
227 cmdStart = Var_Subst(cmd, gn, VARE_WANTRES);
230 * brk_string will return an argv with a NULL in av[0], thus causing
231 * execvp to choke and die horribly. Besides, how can we execute a null
232 * command? In any case, we warn the user that the command expanded to
233 * nothing (is this the right thing to do?).
236 if (*cmdStart == '\0') {
241 LstNode_Set(cmdNode, cmdStart);
243 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
244 assert(ENDNode != NULL);
245 Lst_Append(ENDNode->commands, cmdStart);
248 if (strcmp(cmdStart, "...") == 0) {
249 gn->type |= OP_SAVE_CMDS;
253 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
256 silent = !DEBUG(LOUD);
263 if (!shellName) /* we came here from jobs */
270 while (isspace((unsigned char)*cmd))
274 * If we did not end up with a command, just skip it.
279 #if !defined(MAKE_NATIVE)
281 * In a non-native build, the host environment might be weird enough
282 * that it's necessary to go through a shell to get the correct
283 * behaviour. Or perhaps the shell has been replaced with something
284 * that does extra logging, and that should not be bypassed.
289 * Search for meta characters in the command. If there are no meta
290 * characters, there's no need to execute a shell to execute the
293 * Additionally variable assignments and empty commands
294 * go to the shell. Therefore treat '=' and ':' like shell
295 * meta characters as documented in make(1).
298 useShell = needshell(cmd, FALSE);
302 * Print the command before echoing if we're not supposed to be quiet for
303 * this one. We also print the command if -n given.
305 if (!silent || NoExecute(gn)) {
311 * If we're not supposed to execute any commands, this is as far as
314 if (!doIt && NoExecute(gn)) {
318 fprintf(debug_file, "Execute: '%s'\n", cmd);
322 * We need to pass the command off to the shell, typically
323 * because the command contains a "meta" character.
325 static const char *shargv[5];
329 shargv[shargc++] = shellPath;
331 * The following work for any of the builtin shell specs.
333 if (errCheck && shellErrFlag) {
334 shargv[shargc++] = shellErrFlag;
337 shargv[shargc++] = "-xc";
339 shargv[shargc++] = "-c";
340 shargv[shargc++] = cmd;
341 shargv[shargc] = NULL;
347 * No meta-characters, so no need to exec a shell. Break the command
348 * into words to form an argument vector we can execute.
350 Words words = Str_Words(cmd, FALSE);
363 * Fork and execute the single command. If the fork fails, we abort.
365 compatChild = cpid = vFork();
367 Fatal("Could not fork");
376 (void)execvp(av[0], (char *const *)UNCONST(av));
377 execError("exec", av[0]);
384 /* XXX: Memory management looks suspicious here. */
385 /* XXX: Setting a list item to NULL is unexpected. */
386 LstNode_SetNull(cmdNode);
390 meta_compat_parent(cpid);
395 * The child is off and running. Now all we can do is wait...
399 while ((retstat = wait(&reason)) != cpid) {
401 JobReapChild(retstat, reason, FALSE); /* not ours? */
402 if (retstat == -1 && errno != EINTR) {
408 if (WIFSTOPPED(reason)) {
409 status = WSTOPSIG(reason); /* stopped */
410 } else if (WIFEXITED(reason)) {
411 status = WEXITSTATUS(reason); /* exited */
412 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
414 meta_cmd_finish(NULL);
419 fprintf(debug_file, "\n*** Failed target: %s\n*** Failed command: ",
421 for (cp = cmd; *cp; ) {
422 if (isspace((unsigned char)*cp)) {
423 fprintf(debug_file, " ");
424 while (isspace((unsigned char)*cp))
427 fprintf(debug_file, "%c", *cp);
431 fprintf(debug_file, "\n");
433 printf("*** Error code %d", status);
436 status = WTERMSIG(reason); /* signaled */
437 printf("*** Signal %d", status);
441 if (!WIFEXITED(reason) || (status != 0)) {
445 meta_job_error(NULL, gn, 0, status);
451 * Abort the current target, but let others
454 printf(" (continuing)\n");
459 CompatDeleteTarget(gn);
463 * Continue executing commands for this target.
464 * If we return 0, this will happen...
466 printf(" (ignored)\n");
472 Fatal("error in wait: %d: %s", retstat, strerror(errno));
479 bmake_signal(compatSigno, SIG_DFL);
480 kill(myPid, compatSigno);
487 *-----------------------------------------------------------------------
492 * gnp The node to make
493 * pgnp Parent to abort if necessary
499 * If an error is detected and not being ignored, the process exits.
501 *-----------------------------------------------------------------------
504 Compat_Make(void *gnp, void *pgnp)
506 GNode *gn = (GNode *)gnp;
507 GNode *pgn = (GNode *)pgnp;
509 if (!shellName) /* we came here from jobs */
511 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
513 * First mark ourselves to be made, then apply whatever transformations
514 * the suffix module thinks are necessary. Once that's done, we can
515 * descend and make all our children. If any of them has an error
516 * but the -k flag was given, our 'make' field will be set FALSE again.
517 * This is our signal to not attempt to do anything but abort our
521 gn->made = BEINGMADE;
522 if ((gn->type & OP_MADE) == 0)
524 Lst_ForEach(gn->children, Compat_Make, gn);
525 if ((gn->flags & REMAKE) == 0) {
527 pgn->flags &= ~(unsigned)REMAKE;
531 if (Lst_FindDatum(gn->implicitParents, pgn) != NULL) {
533 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
538 * All the children were made ok. Now cmgn->mtime contains the
539 * modification time of the newest child, we need to find out if we
540 * exist and when we were modified last. The criteria for datedness
541 * are defined by the Make_OODate function.
544 fprintf(debug_file, "Examining %s...", gn->name);
546 if (! Make_OODate(gn)) {
549 fprintf(debug_file, "up-to-date.\n");
552 } else if (DEBUG(MAKE)) {
553 fprintf(debug_file, "out-of-date.\n");
557 * If the user is just seeing if something is out-of-date, exit now
558 * to tell him/her "yes".
565 * We need to be re-made. We also have to make sure we've got a $?
566 * variable. To be nice, we also define the $> variable using
572 * Alter our type to tell if errors should be ignored or things
573 * should not be printed so CompatRunCommand knows what to do.
575 if (Targ_Ignore(gn)) {
576 gn->type |= OP_IGNORE;
578 if (Targ_Silent(gn)) {
579 gn->type |= OP_SILENT;
582 if (Job_CheckCommands(gn, Fatal)) {
584 * Our commands are ok, but we still have to worry about the -t
587 if (!touchFlag || (gn->type & OP_MAKE)) {
590 if (useMeta && !NoExecute(gn)) {
591 meta_job_start(NULL, gn);
594 Lst_ForEach(gn->commands, CompatRunCommand, gn);
597 Job_Touch(gn, (gn->type & OP_SILENT) != 0);
603 if (useMeta && !NoExecute(gn)) {
604 if (meta_job_finish(NULL) != 0)
609 if (gn->made != ERROR) {
611 * If the node was made successfully, mark it so, update
612 * its modification time and timestamp all its parents. Note
613 * that for .ZEROTIME targets, the timestamping isn't done.
614 * This is to keep its state from affecting that of its parent.
617 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
618 if (!(gn->type & OP_EXEC)) {
619 pgn->flags |= CHILDMADE;
620 Make_TimeStamp(pgn, gn);
622 } else if (keepgoing) {
623 pgn->flags &= ~(unsigned)REMAKE;
625 PrintOnError(gn, "\nStop.");
628 } else if (gn->made == ERROR) {
630 * Already had an error when making this beastie. Tell the parent
633 pgn->flags &= ~(unsigned)REMAKE;
635 if (Lst_FindDatum(gn->implicitParents, pgn) != NULL) {
637 const char *target = Var_Value(TARGET, gn, &p1);
638 Var_Set(IMPSRC, target != NULL ? target : "", pgn);
643 Error("Graph cycles through %s", gn->name);
645 pgn->flags &= ~(unsigned)REMAKE;
648 if ((gn->type & OP_EXEC) == 0) {
649 pgn->flags |= CHILDMADE;
650 Make_TimeStamp(pgn, gn);
654 if ((gn->type & OP_EXEC) == 0) {
655 Make_TimeStamp(pgn, gn);
664 Lst_ForEach(gn->cohorts, Compat_Make, pgnp);
668 /* Initialize this module and start making.
671 * targs The target nodes to re-create
674 Compat_Run(Lst targs)
676 GNode *gn = NULL;/* Current root target */
677 int errors; /* Number of targets not remade due to errors */
682 if (bmake_signal(SIGINT, SIG_IGN) != SIG_IGN) {
683 bmake_signal(SIGINT, CompatInterrupt);
685 if (bmake_signal(SIGTERM, SIG_IGN) != SIG_IGN) {
686 bmake_signal(SIGTERM, CompatInterrupt);
688 if (bmake_signal(SIGHUP, SIG_IGN) != SIG_IGN) {
689 bmake_signal(SIGHUP, CompatInterrupt);
691 if (bmake_signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
692 bmake_signal(SIGQUIT, CompatInterrupt);
695 ENDNode = Targ_FindNode(".END", TARG_CREATE);
696 ENDNode->type = OP_SPECIAL;
698 * If the user has defined a .BEGIN target, execute the commands attached
702 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
705 if (gn->made == ERROR) {
706 PrintOnError(gn, "\nStop.");
713 * Expand .USE nodes right now, because they can modify the structure
716 Make_ExpandUse(targs);
719 * For each entry in the list of targets to create, call Compat_Make on
720 * it to create the thing. Compat_Make will leave the 'made' field of gn
721 * in one of several states:
722 * UPTODATE gn was already up-to-date
723 * MADE gn was recreated successfully
724 * ERROR An error occurred while gn was being created
725 * ABORTED gn was not remade because one of its inferiors
726 * could not be made due to errors.
729 while (!Lst_IsEmpty(targs)) {
730 gn = Lst_Dequeue(targs);
733 if (gn->made == UPTODATE) {
734 printf("`%s' is up to date.\n", gn->name);
735 } else if (gn->made == ABORTED) {
736 printf("`%s' not remade because of errors.\n", gn->name);
742 * If the user has defined a .END target, run its commands.
745 Compat_Make(ENDNode, ENDNode);
746 if (gn->made == ERROR) {
747 PrintOnError(gn, "\nStop.");