* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
* Copyright (c) 1988, 1989 by Adam de Boor
* Copyright (c) 1989 by Berkeley Softworks
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)compat.c 5.6 (Berkeley) %G%";
* The routines in this file implement the full-compatibility
* mode of PMake. Most of the special functionality of PMake
* is available in this mode. Things not supported:
* - friendly variable substitution.
* Compat_Run Initialize things for this module and recreate
* The following array is used to make a fast determination of which
* characters are interpreted specially by the shell. If a command
* contains any of these characters, it is executed by the shell, not
static GNode
*curTarg
= NILGNODE
;
static int CompatRunCommand();
*-----------------------------------------------------------------------
* Interrupt the creation of the current target and remove it if
* The target is removed and the process exits. If .INTERRUPT exists,
* its commands are run first WITH INTERRUPTS IGNORED..
*-----------------------------------------------------------------------
if ((curTarg
!= NILGNODE
) && !Targ_Precious (curTarg
)) {
char *file
= Var_Value (TARGET
, curTarg
);
if (unlink (file
) == SUCCESS
) {
printf ("*** %s removed\n", file
);
* Run .INTERRUPT only if hit with interrupt signal
gn
= Targ_FindNode(".INTERRUPT", TARG_NOCREATE
);
Lst_ForEach(gn
->commands
, CompatRunCommand
, (ClientData
)gn
);
*-----------------------------------------------------------------------
* Execute the next command for a target. If the command returns an
* error, the node's made field is set to ERROR and creation stops.
* 0 if the command succeeded, 1 if an error occurred.
* The node's 'made' field may be set to ERROR.
*-----------------------------------------------------------------------
CompatRunCommand (cmd
, gn
)
char *cmd
; /* Command to execute */
GNode
*gn
; /* Node from which the command came */
char *cmdStart
; /* Start of expanded command */
Boolean silent
, /* Don't print command */
errCheck
; /* Check errors */
union wait reason
; /* Reason for child's death */
int status
; /* Description of child's death */
int cpid
; /* Child actually found */
int numWritten
; /* Number of bytes written for error message */
ReturnStatus stat
; /* Status of fork */
LstNode cmdNode
; /* Node where current command is located */
char **av
; /* Argument vector for thing to exec */
int argc
; /* Number of arguments in av or 0 if not
* dynamically allocated */
Boolean local
; /* TRUE if command should be executed
silent
= gn
->type
& OP_SILENT
;
errCheck
= !(gn
->type
& OP_IGNORE
);
cmdNode
= Lst_Member (gn
->commands
, (ClientData
)cmd
);
cmdStart
= Var_Subst (cmd
, gn
, FALSE
);
* brk_string will return an argv with a NULL in av[1], thus causing
* execvp to choke and die horribly. Besides, how can we execute a null
* command? In any case, we warn the user that the command expanded to
* nothing (is this the right thing to do?).
Error("%s expands to empty string", cmd
);
Lst_Replace (cmdNode
, (ClientData
)cmdStart
);
if ((gn
->type
& OP_SAVE_CMDS
) && (gn
!= ENDNode
)) {
(void)Lst_AtEnd(ENDNode
->commands
, (ClientData
)cmdStart
);
} else if (strcmp(cmdStart
, "...") == 0) {
gn
->type
|= OP_SAVE_CMDS
;
while ((*cmd
== '@') || (*cmd
== '-')) {
* Search for meta characters in the command. If there are no meta
* characters, there's no need to execute a shell to execute the
for (cp
= cmd
; !meta
[*cp
]; cp
++) {
* Print the command before echoing if we're not supposed to be quiet for
* this one. We also print the command if -n given.
if (!silent
|| noExecute
) {
* If we're not supposed to execute any commands, this is as far as
* If *cp isn't the null character, we hit a "meta" character and
* need to pass the command off to the shell. We give the shell the
* -e flag as well as -c if it's supposed to exit when it hits an
static char *shargv
[4] = { "/bin/sh" };
shargv
[1] = (errCheck
? "-ec" : "-c");
shargv
[3] = (char *)NULL
;
* No meta-characters, so no need to exec a shell. Break the command
* into words to form an argument vector we can execute.
* brk_string sticks our name in av[0], so we have to
av
= brk_string(cmd
, &argc
);
* Fork and execute the single command. If the fork fails, we abort.
numWritten
= write (2, av
[0], strlen (av
[0]));
numWritten
= write (2, ": not found\n", sizeof(": not found"));
* The child is off and running. Now all we can do is wait...
while ((stat
= wait(&reason
)) != cpid
) {
if (stat
== -1 && errno
!= EINTR
) {
if (WIFSTOPPED(reason
)) {
status
= reason
.w_stopval
; /* stopped */
} else if (WIFEXITED(reason
)) {
status
= reason
.w_retcode
; /* exited */
printf ("*** Error code %d", status
);
status
= reason
.w_termsig
; /* signaled */
printf ("*** Signal %d", status
);
if (!WIFEXITED(reason
) || (status
!= 0)) {
* Abort the current target, but let others
printf (" (continuing)\n");
* Continue executing commands for this target.
* If we return 0, this will happen...
Fatal ("error in wait: %d", stat
);
*-----------------------------------------------------------------------
* If an error is detected and not being ignored, the process exits.
*-----------------------------------------------------------------------
GNode
*gn
; /* The node to make */
GNode
*pgn
; /* Parent to abort if necessary */
} else if (gn
->made
== UNMADE
) {
* First mark ourselves to be made, then apply whatever transformations
* the suffix module thinks are necessary. Once that's done, we can
* descend and make all our children. If any of them has an error
* but the -k flag was given, our 'make' field will be set FALSE again.
* This is our signal to not attempt to do anything but abort our
Lst_ForEach (gn
->children
, CompatMake
, (ClientData
)gn
);
if (Lst_Member (gn
->iParents
, pgn
) != NILLNODE
) {
Var_Set (IMPSRC
, Var_Value(TARGET
, gn
), pgn
);
* All the children were made ok. Now cmtime contains the modification
* time of the newest child, we need to find out if we exist and when
* we were modified last. The criteria for datedness are defined by the
printf("Examining %s...", gn
->name
);
} else if (DEBUG(MAKE
)) {
printf("out-of-date.\n");
* If the user is just seeing if something is out-of-date, exit now
* We need to be re-made. We also have to make sure we've got a $?
* variable. To be nice, we also define the $> variable using
* Alter our type to tell if errors should be ignored or things
* should not be printed so CompatRunCommand knows what to do.
if (Job_CheckCommands (gn
, Fatal
)) {
* Our commands are ok, but we still have to worry about the -t
Lst_ForEach (gn
->commands
, CompatRunCommand
, (ClientData
)gn
);
Job_Touch (gn
, gn
->type
& OP_SILENT
);
* If the node was made successfully, mark it so, update
* its modification time and timestamp all its parents. Note
* that for .ZEROTIME targets, the timestamping isn't done.
* This is to keep its state from affecting that of its parent.
* We can't re-stat the thing, but we can at least take care of
* rules where a target depends on a source that actually creates
* the target, but only if it has changed, e.g.
* cmp -s y.tab.h parse.h || mv y.tab.h parse.h
* In this case, if the definitions produced by yacc haven't
* changed from before, parse.h won't have been updated and
* gn->mtime will reflect the current modification time for
* parse.h. This is something of a kludge, I admit, but it's a
* XXX: People like to use a rule like
* To force things that depend on FRC to be made, so we have to
* check for gn->children being empty as well...
if (!Lst_IsEmpty(gn
->commands
) || Lst_IsEmpty(gn
->children
)) {
* This is what Make does and it's actually a good thing, as it
* cmp -s y.tab.h parse.h || cp y.tab.h parse.h
* to function as intended. Unfortunately, thanks to the stateless
* nature of NFS (and the speed of this program), there are times
* when the modification time of a file created on a remote
* machine will not be modified before the stat() implied by
* the Dir_MTime occurs, thus leading us to believe that the file
* is unchanged, wreaking havoc with files that depend on this one.
* I have decided it is better to make too much than to make too
* little, so this stuff is commented out unless you're sure it's
if (noExecute
|| Dir_MTime(gn
) == 0) {
printf("update time: %s\n", Targ_FmtTime(gn
->mtime
));
if (!(gn
->type
& OP_EXEC
)) {
} else if (gn
->made
== ERROR
) {
* Already had an error when making this beastie. Tell the parent
if (Lst_Member (gn
->iParents
, pgn
) != NILLNODE
) {
Var_Set (IMPSRC
, Var_Value(TARGET
, gn
), pgn
);
Error("Graph cycles through %s\n", gn
->name
);
if ((gn
->type
& OP_EXEC
) == 0) {
if ((gn
->type
& OP_EXEC
) == 0) {
*-----------------------------------------------------------------------
* Initialize this mode and start making.
*-----------------------------------------------------------------------
Lst targs
; /* List of target nodes to re-create */
char *cp
; /* Pointer to string of shell meta-characters */
GNode
*gn
; /* Current root target */
int errors
; /* Number of targets not remade due to errors */
if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
) {
signal(SIGINT
, CompatInterrupt
);
if (signal(SIGTERM
, SIG_IGN
) != SIG_IGN
) {
signal(SIGTERM
, CompatInterrupt
);
if (signal(SIGHUP
, SIG_IGN
) != SIG_IGN
) {
signal(SIGHUP
, CompatInterrupt
);
if (signal(SIGQUIT
, SIG_IGN
) != SIG_IGN
) {
signal(SIGQUIT
, CompatInterrupt
);
for (cp
= "#=|^(){};&<>*?[]:$`\\\n"; *cp
!= '\0'; cp
++) {
* The null character serves as a sentinel in the string.
ENDNode
= Targ_FindNode(".END", TARG_CREATE
);
* If the user has defined a .BEGIN target, execute the commands attached
gn
= Targ_FindNode(".BEGIN", TARG_NOCREATE
);
Lst_ForEach(gn
->commands
, CompatRunCommand
, (ClientData
)gn
);
* For each entry in the list of targets to create, call CompatMake on
* it to create the thing. CompatMake will leave the 'made' field of gn
* in one of several states:
* UPTODATE gn was already up-to-date
* MADE gn was recreated successfully
* ERROR An error occurred while gn was being created
* ABORTED gn was not remade because one of its inferiors
* could not be made due to errors.
while (!Lst_IsEmpty (targs
)) {
gn
= (GNode
*) Lst_DeQueue (targs
);
if (gn
->made
== UPTODATE
) {
printf ("`%s' is up to date.\n", gn
->name
);
} else if (gn
->made
== ABORTED
) {
printf ("`%s' not remade because of errors.\n", gn
->name
);
* If the user has defined a .END target, run its commands.
Lst_ForEach(ENDNode
->commands
, CompatRunCommand
, (ClientData
)gn
);