1 /* $NetBSD: compat.c,v 1.100 2015/06/19 14:32:04 christos 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.100 2015/06/19 14:32:04 christos 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.100 2015/06/19 14:32:04 christos 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'
98 #include <sys/types.h>
100 #include <sys/wait.h>
111 #include "metachar.h"
112 #include "pathnames.h"
115 static GNode
*curTarg
= NULL
;
116 static GNode
*ENDNode
;
117 static void CompatInterrupt(int);
120 *-----------------------------------------------------------------------
122 * Interrupt the creation of the current target and remove it if
129 * The target is removed and the process exits. If .INTERRUPT exists,
130 * its commands are run first WITH INTERRUPTS IGNORED..
132 *-----------------------------------------------------------------------
135 CompatInterrupt(int signo
)
139 if ((curTarg
!= NULL
) && !Targ_Precious (curTarg
)) {
141 char *file
= Var_Value(TARGET
, curTarg
, &p1
);
143 if (!noExecute
&& eunlink(file
) != -1) {
144 Error("*** %s removed", file
);
150 * Run .INTERRUPT only if hit with interrupt signal
152 if (signo
== SIGINT
) {
153 gn
= Targ_FindNode(".INTERRUPT", TARG_NOCREATE
);
160 if (signo
== SIGQUIT
)
162 bmake_signal(signo
, SIG_DFL
);
167 *-----------------------------------------------------------------------
168 * CompatRunCommand --
169 * Execute the next command for a target. If the command returns an
170 * error, the node's made field is set to ERROR and creation stops.
173 * cmdp Command to execute
174 * gnp Node from which the command came
177 * 0 if the command succeeded, 1 if an error occurred.
180 * The node's 'made' field may be set to ERROR.
182 *-----------------------------------------------------------------------
185 CompatRunCommand(void *cmdp
, void *gnp
)
187 char *cmdStart
; /* Start of expanded command */
189 Boolean silent
, /* Don't print command */
190 doIt
; /* Execute even if -n */
191 volatile Boolean errCheck
; /* Check errors */
192 int reason
; /* Reason for child's death */
193 int status
; /* Description of child's death */
194 pid_t cpid
; /* Child actually found */
195 pid_t retstat
; /* Result of wait */
196 LstNode cmdNode
; /* Node where current command is located */
197 const char ** volatile av
; /* Argument vector for thing to exec */
198 char ** volatile mav
;/* Copy of the argument vector for freeing */
199 int argc
; /* Number of arguments in av or 0 if not
200 * dynamically allocated */
201 Boolean local
; /* TRUE if command should be executed
203 Boolean useShell
; /* TRUE if command should be executed
205 char * volatile cmd
= (char *)cmdp
;
206 GNode
*gn
= (GNode
*)gnp
;
208 silent
= gn
->type
& OP_SILENT
;
209 errCheck
= !(gn
->type
& OP_IGNORE
);
212 cmdNode
= Lst_Member(gn
->commands
, cmd
);
213 cmdStart
= Var_Subst(NULL
, cmd
, gn
, FALSE
);
216 * brk_string will return an argv with a NULL in av[0], thus causing
217 * execvp to choke and die horribly. Besides, how can we execute a null
218 * command? In any case, we warn the user that the command expanded to
219 * nothing (is this the right thing to do?).
222 if (*cmdStart
== '\0') {
227 Lst_Replace(cmdNode
, cmdStart
);
229 if ((gn
->type
& OP_SAVE_CMDS
) && (gn
!= ENDNode
)) {
230 (void)Lst_AtEnd(ENDNode
->commands
, cmdStart
);
233 if (strcmp(cmdStart
, "...") == 0) {
234 gn
->type
|= OP_SAVE_CMDS
;
238 while ((*cmd
== '@') || (*cmd
== '-') || (*cmd
== '+')) {
241 silent
= DEBUG(LOUD
) ? FALSE
: TRUE
;
248 if (!shellName
) /* we came here from jobs */
255 while (isspace((unsigned char)*cmd
))
259 * If we did not end up with a command, just skip it.
264 #if !defined(MAKE_NATIVE)
266 * In a non-native build, the host environment might be weird enough
267 * that it's necessary to go through a shell to get the correct
268 * behaviour. Or perhaps the shell has been replaced with something
269 * that does extra logging, and that should not be bypassed.
274 * Search for meta characters in the command. If there are no meta
275 * characters, there's no need to execute a shell to execute the
278 * Additionally variable assignments and empty commands
279 * go to the shell. Therefore treat '=' and ':' like shell
280 * meta characters as documented in make(1).
283 useShell
= needshell(cmd
, FALSE
);
287 * Print the command before echoing if we're not supposed to be quiet for
288 * this one. We also print the command if -n given.
290 if (!silent
|| NoExecute(gn
)) {
296 * If we're not supposed to execute any commands, this is as far as
299 if (!doIt
&& NoExecute(gn
)) {
303 fprintf(debug_file
, "Execute: '%s'\n", cmd
);
308 * We need to pass the command off to the shell, typically
309 * because the command contains a "meta" character.
311 static const char *shargv
[5];
315 shargv
[shargc
++] = shellPath
;
317 * The following work for any of the builtin shell specs.
319 if (errCheck
&& shellErrFlag
) {
320 shargv
[shargc
++] = shellErrFlag
;
323 shargv
[shargc
++] = "-xc";
325 shargv
[shargc
++] = "-c";
326 shargv
[shargc
++] = cmd
;
327 shargv
[shargc
++] = NULL
;
334 * No meta-characters, so no need to exec a shell. Break the command
335 * into words to form an argument vector we can execute.
337 mav
= brk_string(cmd
, &argc
, TRUE
, &bp
);
354 * Fork and execute the single command. If the fork fails, we abort.
358 Fatal("Could not fork");
368 (void)execvp(av
[0], (char *const *)UNCONST(av
));
370 (void)execv(av
[0], (char *const *)UNCONST(av
));
371 execError("exec", av
[0]);
378 Lst_Replace(cmdNode
, NULL
);
382 meta_compat_parent();
387 * The child is off and running. Now all we can do is wait...
391 while ((retstat
= wait(&reason
)) != cpid
) {
393 JobReapChild(retstat
, reason
, FALSE
); /* not ours? */
394 if (retstat
== -1 && errno
!= EINTR
) {
400 if (WIFSTOPPED(reason
)) {
401 status
= WSTOPSIG(reason
); /* stopped */
402 } else if (WIFEXITED(reason
)) {
403 status
= WEXITSTATUS(reason
); /* exited */
404 #if defined(USE_META) && defined(USE_FILEMON_ONCE)
406 meta_cmd_finish(NULL
);
411 fprintf(debug_file
, "\n*** Failed target: %s\n*** Failed command: ",
413 for (cp
= cmd
; *cp
; ) {
414 if (isspace((unsigned char)*cp
)) {
415 fprintf(debug_file
, " ");
416 while (isspace((unsigned char)*cp
))
419 fprintf(debug_file
, "%c", *cp
);
423 fprintf(debug_file
, "\n");
425 printf("*** Error code %d", status
);
428 status
= WTERMSIG(reason
); /* signaled */
429 printf("*** Signal %d", status
);
433 if (!WIFEXITED(reason
) || (status
!= 0)) {
437 meta_job_error(NULL
, gn
, 0, status
);
443 * Abort the current target, but let others
446 printf(" (continuing)\n");
450 * Continue executing commands for this target.
451 * If we return 0, this will happen...
453 printf(" (ignored)\n");
459 Fatal("error in wait: %d: %s", retstat
, strerror(errno
));
469 *-----------------------------------------------------------------------
474 * gnp The node to make
475 * pgnp Parent to abort if necessary
481 * If an error is detected and not being ignored, the process exits.
483 *-----------------------------------------------------------------------
486 Compat_Make(void *gnp
, void *pgnp
)
488 GNode
*gn
= (GNode
*)gnp
;
489 GNode
*pgn
= (GNode
*)pgnp
;
491 if (!shellName
) /* we came here from jobs */
493 if (gn
->made
== UNMADE
&& (gn
== pgn
|| (pgn
->type
& OP_MADE
) == 0)) {
495 * First mark ourselves to be made, then apply whatever transformations
496 * the suffix module thinks are necessary. Once that's done, we can
497 * descend and make all our children. If any of them has an error
498 * but the -k flag was given, our 'make' field will be set FALSE again.
499 * This is our signal to not attempt to do anything but abort our
503 gn
->made
= BEINGMADE
;
504 if ((gn
->type
& OP_MADE
) == 0)
506 Lst_ForEach(gn
->children
, Compat_Make
, gn
);
507 if ((gn
->flags
& REMAKE
) == 0) {
509 pgn
->flags
&= ~REMAKE
;
513 if (Lst_Member(gn
->iParents
, pgn
) != NULL
) {
515 Var_Set(IMPSRC
, Var_Value(TARGET
, gn
, &p1
), pgn
, 0);
521 * All the children were made ok. Now cmgn->mtime contains the
522 * modification time of the newest child, we need to find out if we
523 * exist and when we were modified last. The criteria for datedness
524 * are defined by the Make_OODate function.
527 fprintf(debug_file
, "Examining %s...", gn
->name
);
529 if (! Make_OODate(gn
)) {
532 fprintf(debug_file
, "up-to-date.\n");
535 } else if (DEBUG(MAKE
)) {
536 fprintf(debug_file
, "out-of-date.\n");
540 * If the user is just seeing if something is out-of-date, exit now
541 * to tell him/her "yes".
548 * We need to be re-made. We also have to make sure we've got a $?
549 * variable. To be nice, we also define the $> variable using
555 * Alter our type to tell if errors should be ignored or things
556 * should not be printed so CompatRunCommand knows what to do.
558 if (Targ_Ignore(gn
)) {
559 gn
->type
|= OP_IGNORE
;
561 if (Targ_Silent(gn
)) {
562 gn
->type
|= OP_SILENT
;
565 if (Job_CheckCommands(gn
, Fatal
)) {
567 * Our commands are ok, but we still have to worry about the -t
570 if (!touchFlag
|| (gn
->type
& OP_MAKE
)) {
573 if (useMeta
&& !NoExecute(gn
)) {
574 meta_job_start(NULL
, gn
);
577 Lst_ForEach(gn
->commands
, CompatRunCommand
, gn
);
580 Job_Touch(gn
, gn
->type
& OP_SILENT
);
586 if (useMeta
&& !NoExecute(gn
)) {
587 meta_job_finish(NULL
);
591 if (gn
->made
!= ERROR
) {
593 * If the node was made successfully, mark it so, update
594 * its modification time and timestamp all its parents. Note
595 * that for .ZEROTIME targets, the timestamping isn't done.
596 * This is to keep its state from affecting that of its parent.
599 pgn
->flags
|= Make_Recheck(gn
) == 0 ? FORCE
: 0;
600 if (!(gn
->type
& OP_EXEC
)) {
601 pgn
->flags
|= CHILDMADE
;
602 Make_TimeStamp(pgn
, gn
);
604 } else if (keepgoing
) {
605 pgn
->flags
&= ~REMAKE
;
607 PrintOnError(gn
, "\n\nStop.");
610 } else if (gn
->made
== ERROR
) {
612 * Already had an error when making this beastie. Tell the parent
615 pgn
->flags
&= ~REMAKE
;
617 if (Lst_Member(gn
->iParents
, pgn
) != NULL
) {
619 Var_Set(IMPSRC
, Var_Value(TARGET
, gn
, &p1
), pgn
, 0);
625 Error("Graph cycles through %s", gn
->name
);
627 pgn
->flags
&= ~REMAKE
;
630 if ((gn
->type
& OP_EXEC
) == 0) {
631 pgn
->flags
|= CHILDMADE
;
632 Make_TimeStamp(pgn
, gn
);
636 if ((gn
->type
& OP_EXEC
) == 0) {
637 Make_TimeStamp(pgn
, gn
);
646 Lst_ForEach(gn
->cohorts
, Compat_Make
, pgnp
);
651 *-----------------------------------------------------------------------
653 * Initialize this mode and start making.
656 * targs List of target nodes to re-create
664 *-----------------------------------------------------------------------
667 Compat_Run(Lst targs
)
669 GNode
*gn
= NULL
;/* Current root target */
670 int errors
; /* Number of targets not remade due to errors */
675 if (bmake_signal(SIGINT
, SIG_IGN
) != SIG_IGN
) {
676 bmake_signal(SIGINT
, CompatInterrupt
);
678 if (bmake_signal(SIGTERM
, SIG_IGN
) != SIG_IGN
) {
679 bmake_signal(SIGTERM
, CompatInterrupt
);
681 if (bmake_signal(SIGHUP
, SIG_IGN
) != SIG_IGN
) {
682 bmake_signal(SIGHUP
, CompatInterrupt
);
684 if (bmake_signal(SIGQUIT
, SIG_IGN
) != SIG_IGN
) {
685 bmake_signal(SIGQUIT
, CompatInterrupt
);
688 ENDNode
= Targ_FindNode(".END", TARG_CREATE
);
689 ENDNode
->type
= OP_SPECIAL
;
691 * If the user has defined a .BEGIN target, execute the commands attached
695 gn
= Targ_FindNode(".BEGIN", TARG_NOCREATE
);
698 if (gn
->made
== ERROR
) {
699 PrintOnError(gn
, "\n\nStop.");
706 * Expand .USE nodes right now, because they can modify the structure
709 Make_ExpandUse(targs
);
712 * For each entry in the list of targets to create, call Compat_Make on
713 * it to create the thing. Compat_Make will leave the 'made' field of gn
714 * in one of several states:
715 * UPTODATE gn was already up-to-date
716 * MADE gn was recreated successfully
717 * ERROR An error occurred while gn was being created
718 * ABORTED gn was not remade because one of its inferiors
719 * could not be made due to errors.
722 while (!Lst_IsEmpty (targs
)) {
723 gn
= (GNode
*)Lst_DeQueue(targs
);
726 if (gn
->made
== UPTODATE
) {
727 printf("`%s' is up to date.\n", gn
->name
);
728 } else if (gn
->made
== ABORTED
) {
729 printf("`%s' not remade because of errors.\n", gn
->name
);
735 * If the user has defined a .END target, run its commands.
738 Compat_Make(ENDNode
, ENDNode
);
739 if (gn
->made
== ERROR
) {
740 PrintOnError(gn
, "\n\nStop.");