2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * Copyright (c) 1988, 1989 by Adam de Boor
4 * Copyright (c) 1989 by Berkeley Softworks
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)compat.c 8.2 (Berkeley) 3/19/94
39 * $FreeBSD: src/usr.bin/make/compat.c,v 1.16.2.2 2000/07/01 12:24:21 ps Exp $
40 * $DragonFly: src/usr.bin/make/Attic/compat.c,v 1.3 2003/11/03 19:31:30 eirikn Exp $
45 * The routines in this file implement the full-compatibility
46 * mode of PMake. Most of the special functionality of PMake
47 * is available in this mode. Things not supported:
49 * - friendly variable substitution.
52 * Compat_Run Initialize things for this module and recreate
53 * thems as need creatin'
57 #include <sys/types.h>
69 * The following array is used to make a fast determination of which
70 * characters are interpreted specially by the shell. If a command
71 * contains any of these characters, it is executed by the shell, not
75 static char meta[256];
77 static GNode *curTarg = NILGNODE;
78 static GNode *ENDNode;
79 static void CompatInterrupt(int);
80 static int CompatRunCommand(ClientData, ClientData);
81 static int CompatMake(ClientData, ClientData);
83 static char *sh_builtin[] = {
84 "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
85 "unalias", "umask", "unset", "wait", ":", 0};
88 *-----------------------------------------------------------------------
90 * Interrupt the creation of the current target and remove it if
97 * The target is removed and the process exits. If .INTERRUPT exists,
98 * its commands are run first WITH INTERRUPTS IGNORED..
100 *-----------------------------------------------------------------------
103 CompatInterrupt (signo)
108 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
110 char *file = Var_Value (TARGET, curTarg, &p1);
112 if (!noExecute && eunlink(file) != -1) {
113 printf ("*** %s removed\n", file);
118 * Run .INTERRUPT only if hit with interrupt signal
120 if (signo == SIGINT) {
121 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
122 if (gn != NILGNODE) {
123 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
128 if (signo == SIGQUIT)
130 (void) signal(signo, SIG_DFL);
131 (void) kill(getpid(), signo);
135 *-----------------------------------------------------------------------
139 * Returns 1 if a specified line must be executed by the shell,
140 * 0 if it can be run via execve, and -1 if the command is a no-op.
145 *-----------------------------------------------------------------------
154 av = brk_string(cmd, &ac, TRUE);
155 for(p = sh_builtin; *p != 0; p++)
156 if (strcmp(av[1], *p) == 0)
162 *-----------------------------------------------------------------------
163 * CompatRunCommand --
164 * Execute the next command for a target. If the command returns an
165 * error, the node's made field is set to ERROR and creation stops.
168 * 0 if the command succeeded, 1 if an error occurred.
171 * The node's 'made' field may be set to ERROR.
173 *-----------------------------------------------------------------------
176 CompatRunCommand (cmdp, gnp)
177 ClientData cmdp; /* Command to execute */
178 ClientData gnp; /* Node from which the command came */
180 char *cmdStart; /* Start of expanded command */
182 Boolean silent, /* Don't print command */
183 errCheck; /* Check errors */
184 int reason; /* Reason for child's death */
185 int status; /* Description of child's death */
186 int cpid; /* Child actually found */
187 ReturnStatus stat; /* Status of fork */
188 LstNode cmdNode; /* Node where current command is located */
189 char **av; /* Argument vector for thing to exec */
190 int argc; /* Number of arguments in av or 0 if not
191 * dynamically allocated */
192 Boolean local; /* TRUE if command should be executed
194 int internal; /* Various values.. */
195 char *cmd = (char *) cmdp;
196 GNode *gn = (GNode *) gnp;
199 * Avoid clobbered variable warnings by forcing the compiler
200 * to ``unregister'' variables
206 silent = gn->type & OP_SILENT;
207 errCheck = !(gn->type & OP_IGNORE);
209 cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
210 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
213 * brk_string will return an argv with a NULL in av[0], thus causing
214 * execvp to choke and die horribly. Besides, how can we execute a null
215 * command? In any case, we warn the user that the command expanded to
216 * nothing (is this the right thing to do?).
219 if (*cmdStart == '\0') {
221 Error("%s expands to empty string", cmd);
226 Lst_Replace (cmdNode, (ClientData)cmdStart);
228 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
229 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
231 } else if (strcmp(cmdStart, "...") == 0) {
232 gn->type |= OP_SAVE_CMDS;
236 while ((*cmd == '@') || (*cmd == '-')) {
238 silent = DEBUG(LOUD) ? FALSE : TRUE;
245 while (isspace((unsigned char)*cmd))
249 * Search for meta characters in the command. If there are no meta
250 * characters, there's no need to execute a shell to execute the
253 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
258 * Print the command before echoing if we're not supposed to be quiet for
259 * this one. We also print the command if -n given.
261 if (!silent || noExecute) {
262 printf ("%s\n", cmd);
267 * If we're not supposed to execute any commands, this is as far as
276 * If *cp isn't the null character, we hit a "meta" character and
277 * need to pass the command off to the shell. We give the shell the
278 * -e flag as well as -c if it's supposed to exit when it hits an
281 static char *shargv[4] = { "/bin/sh" };
283 shargv[1] = (errCheck ? "-ec" : "-c");
285 shargv[3] = (char *)NULL;
288 } else if ((internal = shellneed(cmd))) {
290 * This command must be passed by the shell for other reasons..
291 * or.. possibly not at all.
293 static char *shargv[4] = { "/bin/sh" };
295 if (internal == -1) {
296 /* Command does not need to be executed */
300 shargv[1] = (errCheck ? "-ec" : "-c");
302 shargv[3] = (char *)NULL;
307 * No meta-characters, so no need to exec a shell. Break the command
308 * into words to form an argument vector we can execute.
309 * brk_string sticks our name in av[0], so we have to
312 av = brk_string(cmd, &argc, TRUE);
319 * Fork and execute the single command. If the fork fails, we abort.
323 Fatal("Could not fork");
328 (void) write (2, av[0], strlen (av[0]));
329 (void) write (2, ":", 1);
330 (void) write (2, strerror(errno), strlen(strerror(errno)));
331 (void) write (2, "\n", 1);
333 (void)execv(av[0], av);
339 * we need to print out the command associated with this Gnode in
340 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
341 * in main(), Fatal() and DieHorribly(), therefore do not free it
344 if (!DEBUG(GRAPH2)) {
346 Lst_Replace (cmdNode, cmdp);
350 * The child is off and running. Now all we can do is wait...
354 while ((stat = wait(&reason)) != cpid) {
355 if (stat == -1 && errno != EINTR) {
361 if (WIFSTOPPED(reason)) {
362 status = WSTOPSIG(reason); /* stopped */
363 } else if (WIFEXITED(reason)) {
364 status = WEXITSTATUS(reason); /* exited */
366 printf ("*** Error code %d", status);
369 status = WTERMSIG(reason); /* signaled */
370 printf ("*** Signal %d", status);
374 if (!WIFEXITED(reason) || (status != 0)) {
379 * Abort the current target, but let others
382 printf (" (continuing)\n");
386 * Continue executing commands for this target.
387 * If we return 0, this will happen...
389 printf (" (ignored)\n");
395 Fatal ("error in wait: %d", stat);
404 *-----------------------------------------------------------------------
412 * If an error is detected and not being ignored, the process exits.
414 *-----------------------------------------------------------------------
417 CompatMake (gnp, pgnp)
418 ClientData gnp; /* The node to make */
419 ClientData pgnp; /* Parent to abort if necessary */
421 GNode *gn = (GNode *) gnp;
422 GNode *pgn = (GNode *) pgnp;
423 if (gn->type & OP_USE) {
424 Make_HandleUse(gn, pgn);
425 } else if (gn->made == UNMADE) {
427 * First mark ourselves to be made, then apply whatever transformations
428 * the suffix module thinks are necessary. Once that's done, we can
429 * descend and make all our children. If any of them has an error
430 * but the -k flag was given, our 'make' field will be set FALSE again.
431 * This is our signal to not attempt to do anything but abort our
435 gn->made = BEINGMADE;
437 Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
444 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
446 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
451 * All the children were made ok. Now cmtime contains the modification
452 * time of the newest child, we need to find out if we exist and when
453 * we were modified last. The criteria for datedness are defined by the
454 * Make_OODate function.
457 printf("Examining %s...", gn->name);
459 if (! Make_OODate(gn)) {
462 printf("up-to-date.\n");
465 } else if (DEBUG(MAKE)) {
466 printf("out-of-date.\n");
470 * If the user is just seeing if something is out-of-date, exit now
471 * to tell him/her "yes".
478 * We need to be re-made. We also have to make sure we've got a $?
479 * variable. To be nice, we also define the $> variable using
485 * Alter our type to tell if errors should be ignored or things
486 * should not be printed so CompatRunCommand knows what to do.
488 if (Targ_Ignore (gn)) {
489 gn->type |= OP_IGNORE;
491 if (Targ_Silent (gn)) {
492 gn->type |= OP_SILENT;
495 if (Job_CheckCommands (gn, Fatal)) {
497 * Our commands are ok, but we still have to worry about the -t
502 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
505 Job_Touch (gn, gn->type & OP_SILENT);
511 if (gn->made != ERROR) {
513 * If the node was made successfully, mark it so, update
514 * its modification time and timestamp all its parents. Note
515 * that for .ZEROTIME targets, the timestamping isn't done.
516 * This is to keep its state from affecting that of its parent.
521 * We can't re-stat the thing, but we can at least take care of
522 * rules where a target depends on a source that actually creates
523 * the target, but only if it has changed, e.g.
531 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
533 * In this case, if the definitions produced by yacc haven't
534 * changed from before, parse.h won't have been updated and
535 * gn->mtime will reflect the current modification time for
536 * parse.h. This is something of a kludge, I admit, but it's a
539 * XXX: People like to use a rule like
543 * To force things that depend on FRC to be made, so we have to
544 * check for gn->children being empty as well...
546 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
551 * This is what Make does and it's actually a good thing, as it
554 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
556 * to function as intended. Unfortunately, thanks to the stateless
557 * nature of NFS (and the speed of this program), there are times
558 * when the modification time of a file created on a remote
559 * machine will not be modified before the stat() implied by
560 * the Dir_MTime occurs, thus leading us to believe that the file
561 * is unchanged, wreaking havoc with files that depend on this one.
563 * I have decided it is better to make too much than to make too
564 * little, so this stuff is commented out unless you're sure it's
568 if (noExecute || Dir_MTime(gn) == 0) {
571 if (gn->cmtime > gn->mtime)
572 gn->mtime = gn->cmtime;
574 printf("update time: %s\n", Targ_FmtTime(gn->mtime));
577 if (!(gn->type & OP_EXEC)) {
578 pgn->childMade = TRUE;
579 Make_TimeStamp(pgn, gn);
581 } else if (keepgoing) {
586 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
590 } else if (gn->made == ERROR) {
592 * Already had an error when making this beastie. Tell the parent
597 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
599 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
604 Error("Graph cycles through %s\n", gn->name);
609 if ((gn->type & OP_EXEC) == 0) {
610 pgn->childMade = TRUE;
611 Make_TimeStamp(pgn, gn);
615 if ((gn->type & OP_EXEC) == 0) {
616 Make_TimeStamp(pgn, gn);
628 *-----------------------------------------------------------------------
630 * Initialize this mode and start making.
638 *-----------------------------------------------------------------------
642 Lst targs; /* List of target nodes to re-create */
644 char *cp; /* Pointer to string of shell meta-characters */
645 GNode *gn = NULL;/* Current root target */
646 int errors; /* Number of targets not remade due to errors */
648 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
649 signal(SIGINT, CompatInterrupt);
651 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
652 signal(SIGTERM, CompatInterrupt);
654 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
655 signal(SIGHUP, CompatInterrupt);
657 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
658 signal(SIGQUIT, CompatInterrupt);
661 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
662 meta[(unsigned char) *cp] = 1;
665 * The null character serves as a sentinel in the string.
669 ENDNode = Targ_FindNode(".END", TARG_CREATE);
671 * If the user has defined a .BEGIN target, execute the commands attached
675 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
676 if (gn != NILGNODE) {
677 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
678 if (gn->made == ERROR) {
679 printf("\n\nStop.\n");
686 * For each entry in the list of targets to create, call CompatMake on
687 * it to create the thing. CompatMake will leave the 'made' field of gn
688 * in one of several states:
689 * UPTODATE gn was already up-to-date
690 * MADE gn was recreated successfully
691 * ERROR An error occurred while gn was being created
692 * ABORTED gn was not remade because one of its inferiors
693 * could not be made due to errors.
696 while (!Lst_IsEmpty (targs)) {
697 gn = (GNode *) Lst_DeQueue (targs);
700 if (gn->made == UPTODATE) {
701 printf ("`%s' is up to date.\n", gn->name);
702 } else if (gn->made == ABORTED) {
703 printf ("`%s' not remade because of errors.\n", gn->name);
709 * If the user has defined a .END target, run its commands.
712 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);