2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)compat.c 8.2 (Berkeley) 3/19/94
40 * $FreeBSD: src/usr.bin/make/compat.c,v 1.16.2.2 2000/07/01 12:24:21 ps Exp $
41 * $DragonFly: src/usr.bin/make/Attic/compat.c,v 1.22 2005/01/09 17:05:33 okumoto Exp $
46 * The routines in this file implement the full-compatibility
47 * mode of PMake. Most of the special functionality of PMake
48 * is available in this mode. Things not supported:
50 * - friendly variable substitution.
53 * Compat_Run Initialize things for this module and recreate
54 * thems as need creatin'
57 #include <sys/types.h>
80 * The following array is used to make a fast determination of which
81 * characters are interpreted specially by the shell. If a command
82 * contains any of these characters, it is executed by the shell, not
86 static char meta[256];
88 static GNode *curTarg = NULL;
89 static GNode *ENDNode;
90 static sig_atomic_t interrupted;
92 static void CompatInterrupt(int);
93 static int CompatMake(void *, void *);
94 static int shellneed(char *);
99 const char *cp; /* Pointer to string of shell meta-characters */
101 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
102 meta[(unsigned char)*cp] = 1;
105 * The null character serves as a sentinel in the string.
111 * Interrupt handler - set flag and defer handling to the main code
114 CompatCatchSig(int signo)
121 *-----------------------------------------------------------------------
123 * Interrupt the creation of the current target and remove it if
130 * The target is removed and the process exits. If .INTERRUPT exists,
131 * its commands are run first WITH INTERRUPTS IGNORED..
133 *-----------------------------------------------------------------------
136 CompatInterrupt (int signo)
139 sigset_t nmask, omask;
142 sigaddset(&nmask, SIGINT);
143 sigaddset(&nmask, SIGTERM);
144 sigaddset(&nmask, SIGHUP);
145 sigaddset(&nmask, SIGQUIT);
146 sigprocmask(SIG_SETMASK, &nmask, &omask);
148 /* prevent recursion in evaluation of .INTERRUPT */
151 if ((curTarg != NULL) && !Targ_Precious(curTarg)) {
153 char *file = Var_Value(TARGET, curTarg, &p1);
155 if (!noExecute && eunlink(file) != -1) {
156 printf("*** %s removed\n", file);
162 * Run .INTERRUPT only if hit with interrupt signal
164 if (signo == SIGINT) {
165 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
167 Lst_ForEach(&gn->commands, Compat_RunCommand, (void *)gn);
171 sigprocmask(SIG_SETMASK, &omask, NULL);
173 if (signo == SIGQUIT)
175 signal(signo, SIG_DFL);
176 kill(getpid(), signo);
180 *-----------------------------------------------------------------------
184 * Returns 1 if a specified line must be executed by the shell,
185 * and 0 if it can be run via execve.
190 *-----------------------------------------------------------------------
195 static const char *sh_builtin[] = {
196 "alias", "cd", "eval", "exec",
197 "exit", "read", "set", "ulimit",
198 "unalias", "umask", "unset", "wait",
205 av = brk_string(cmd, &ac, TRUE);
206 for (p = sh_builtin; *p != 0; p++)
207 if (strcmp(av[1], *p) == 0)
213 *-----------------------------------------------------------------------
214 * Compat_RunCommand --
215 * Execute the next command for a target. If the command returns an
216 * error, the node's made field is set to ERROR and creation stops.
217 * The node from which the command came is also given.
220 * 0 if the command succeeded, 1 if an error occurred.
223 * The node's 'made' field may be set to ERROR.
225 *-----------------------------------------------------------------------
228 Compat_RunCommand(void *cmdp, void *gnp)
230 char *cmdStart; /* Start of expanded command */
232 Boolean silent, /* Don't print command */
233 doit, /* Execute even in -n */
234 errCheck; /* Check errors */
235 int reason; /* Reason for child's death */
236 int status; /* Description of child's death */
237 int cpid; /* Child actually found */
238 ReturnStatus rstat; /* Status of fork */
239 LstNode *cmdNode; /* Node where current command is located */
240 char **av; /* Argument vector for thing to exec */
241 int argc; /* Number of arguments in av or 0 if not
242 * dynamically allocated */
243 int internal; /* Various values.. */
248 * Avoid clobbered variable warnings by forcing the compiler
249 * to ``unregister'' variables
255 silent = gn->type & OP_SILENT;
256 errCheck = !(gn->type & OP_IGNORE);
259 cmdNode = Lst_Member(&gn->commands, cmd);
260 cmdStart = Var_Subst(NULL, cmd, gn, FALSE);
263 * brk_string will return an argv with a NULL in av[0], thus causing
264 * execvp to choke and die horribly. Besides, how can we execute a null
265 * command? In any case, we warn the user that the command expanded to
266 * nothing (is this the right thing to do?).
269 if (*cmdStart == '\0') {
271 Error("%s expands to empty string", cmd);
276 Lst_Replace (cmdNode, cmdStart);
278 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
279 Lst_AtEnd(&ENDNode->commands, cmdStart);
281 } else if (strcmp(cmdStart, "...") == 0) {
282 gn->type |= OP_SAVE_CMDS;
286 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
290 silent = DEBUG(LOUD) ? FALSE : TRUE;
299 if (!meta[0]) /* we came here from jobs */
306 while (isspace((unsigned char)*cmd))
310 * Search for meta characters in the command. If there are no meta
311 * characters, there's no need to execute a shell to execute the
314 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
319 * Print the command before echoing if we're not supposed to be quiet for
320 * this one. We also print the command if -n given, but not if '+'.
322 if (!silent || (noExecute && !doit)) {
328 * If we're not supposed to execute any commands, this is as far as
331 if (!doit && noExecute) {
337 * If *cp isn't the null character, we hit a "meta" character and
338 * need to pass the command off to the shell. We give the shell the
339 * -e flag as well as -c if it's supposed to exit when it hits an
342 static char *shargv[4];
344 shargv[0] = shellPath;
345 shargv[1] = (errCheck ? "-ec" : "-c");
350 } else if ((internal = shellneed(cmd))) {
352 * This command must be passed by the shell for other reasons..
353 * or.. possibly not at all.
355 static char *shargv[4];
357 shargv[0] = shellPath;
358 shargv[1] = (errCheck ? "-ec" : "-c");
365 * No meta-characters, so no need to exec a shell. Break the command
366 * into words to form an argument vector we can execute.
367 * brk_string sticks our name in av[0], so we have to
370 av = brk_string(cmd, &argc, TRUE);
375 * Fork and execute the single command. If the fork fails, we abort.
379 Fatal("Could not fork");
383 write(STDERR_FILENO, av[0], strlen (av[0]));
384 write(STDERR_FILENO, ":", 1);
385 write(STDERR_FILENO, strerror(errno), strlen(strerror(errno)));
386 write(STDERR_FILENO, "\n", 1);
391 * we need to print out the command associated with this Gnode in
392 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
393 * in main(), Fatal() and DieHorribly(), therefore do not free it
396 if (!DEBUG(GRAPH2)) {
398 Lst_Replace(cmdNode, cmdp);
402 * The child is off and running. Now all we can do is wait...
406 while ((rstat = wait(&reason)) != cpid) {
407 if (interrupted || (rstat == -1 && errno != EINTR)) {
412 CompatInterrupt(interrupted);
415 if (WIFSTOPPED(reason)) {
416 status = WSTOPSIG(reason); /* stopped */
417 } else if (WIFEXITED(reason)) {
418 status = WEXITSTATUS(reason); /* exited */
420 printf("*** Error code %d", status);
423 status = WTERMSIG(reason); /* signaled */
424 printf("*** Signal %d", status);
428 if (!WIFEXITED(reason) || (status != 0)) {
433 * Abort the current target, but let others
436 printf(" (continuing)\n");
440 * Continue executing commands for this target.
441 * If we return 0, this will happen...
443 printf(" (ignored)\n");
449 Fatal("error in wait: %d", rstat);
458 *-----------------------------------------------------------------------
460 * Make a target, given the parent, to abort if necessary.
466 * If an error is detected and not being ignored, the process exits.
468 *-----------------------------------------------------------------------
471 CompatMake(void *gnp, void *pgnp)
476 if (gn->type & OP_USE) {
477 Make_HandleUse(gn, pgn);
478 } else if (gn->made == UNMADE) {
480 * First mark ourselves to be made, then apply whatever transformations
481 * the suffix module thinks are necessary. Once that's done, we can
482 * descend and make all our children. If any of them has an error
483 * but the -k flag was given, our 'make' field will be set FALSE again.
484 * This is our signal to not attempt to do anything but abort our
488 gn->made = BEINGMADE;
490 Lst_ForEach(&gn->children, CompatMake, gn);
497 if (Lst_Member(&gn->iParents, pgn) != NULL) {
499 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
504 * All the children were made ok. Now cmtime contains the modification
505 * time of the newest child, we need to find out if we exist and when
506 * we were modified last. The criteria for datedness are defined by the
507 * Make_OODate function.
509 DEBUGF(MAKE, ("Examining %s...", gn->name));
510 if (!Make_OODate(gn)) {
512 DEBUGF(MAKE, ("up-to-date.\n"));
515 DEBUGF(MAKE, ("out-of-date.\n"));
519 * If the user is just seeing if something is out-of-date, exit now
520 * to tell him/her "yes".
527 * We need to be re-made. We also have to make sure we've got a $?
528 * variable. To be nice, we also define the $> variable using
534 * Alter our type to tell if errors should be ignored or things
535 * should not be printed so Compat_RunCommand knows what to do.
537 if (Targ_Ignore(gn)) {
538 gn->type |= OP_IGNORE;
540 if (Targ_Silent(gn)) {
541 gn->type |= OP_SILENT;
544 if (Job_CheckCommands(gn, Fatal)) {
546 * Our commands are ok, but we still have to worry about the -t
551 Lst_ForEach(&gn->commands, Compat_RunCommand, (void *)gn);
554 Job_Touch(gn, gn->type & OP_SILENT);
560 if (gn->made != ERROR) {
562 * If the node was made successfully, mark it so, update
563 * its modification time and timestamp all its parents. Note
564 * that for .ZEROTIME targets, the timestamping isn't done.
565 * This is to keep its state from affecting that of its parent.
570 * We can't re-stat the thing, but we can at least take care of
571 * rules where a target depends on a source that actually creates
572 * the target, but only if it has changed, e.g.
580 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
582 * In this case, if the definitions produced by yacc haven't
583 * changed from before, parse.h won't have been updated and
584 * gn->mtime will reflect the current modification time for
585 * parse.h. This is something of a kludge, I admit, but it's a
588 * XXX: People like to use a rule like
592 * To force things that depend on FRC to be made, so we have to
593 * check for gn->children being empty as well...
595 if (!Lst_IsEmpty(&gn->commands) || Lst_IsEmpty(&gn->children)) {
600 * This is what Make does and it's actually a good thing, as it
603 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
605 * to function as intended. Unfortunately, thanks to the stateless
606 * nature of NFS (and the speed of this program), there are times
607 * when the modification time of a file created on a remote
608 * machine will not be modified before the stat() implied by
609 * the Dir_MTime occurs, thus leading us to believe that the file
610 * is unchanged, wreaking havoc with files that depend on this one.
612 * I have decided it is better to make too much than to make too
613 * little, so this stuff is commented out unless you're sure it's
617 if (noExecute || Dir_MTime(gn) == 0) {
620 if (gn->cmtime > gn->mtime)
621 gn->mtime = gn->cmtime;
622 DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(gn->mtime)));
624 if (!(gn->type & OP_EXEC)) {
625 pgn->childMade = TRUE;
626 Make_TimeStamp(pgn, gn);
628 } else if (keepgoing) {
633 printf("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
637 } else if (gn->made == ERROR) {
639 * Already had an error when making this beastie. Tell the parent
644 if (Lst_Member(&gn->iParents, pgn) != NULL) {
646 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
651 Error("Graph cycles through %s\n", gn->name);
656 if ((gn->type & OP_EXEC) == 0) {
657 pgn->childMade = TRUE;
658 Make_TimeStamp(pgn, gn);
662 if ((gn->type & OP_EXEC) == 0) {
663 Make_TimeStamp(pgn, gn);
675 *-----------------------------------------------------------------------
677 * Start making again, given a list of target nodes.
685 *-----------------------------------------------------------------------
688 Compat_Run(Lst *targs)
690 GNode *gn = NULL;/* Current root target */
691 int errors; /* Number of targets not remade due to errors */
694 Shell_Init(); /* Set up shell. */
696 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
697 signal(SIGINT, CompatCatchSig);
699 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
700 signal(SIGTERM, CompatCatchSig);
702 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
703 signal(SIGHUP, CompatCatchSig);
705 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
706 signal(SIGQUIT, CompatCatchSig);
709 ENDNode = Targ_FindNode(".END", TARG_CREATE);
711 * If the user has defined a .BEGIN target, execute the commands attached
715 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
717 Lst_ForEach(&gn->commands, Compat_RunCommand, gn);
718 if (gn->made == ERROR) {
719 printf("\n\nStop.\n");
726 * For each entry in the list of targets to create, call CompatMake on
727 * it to create the thing. CompatMake will leave the 'made' field of gn
728 * in one of several states:
729 * UPTODATE gn was already up-to-date
730 * MADE gn was recreated successfully
731 * ERROR An error occurred while gn was being created
732 * ABORTED gn was not remade because one of its inferiors
733 * could not be made due to errors.
736 while (!Lst_IsEmpty(targs)) {
737 gn = Lst_DeQueue(targs);
740 if (gn->made == UPTODATE) {
741 printf("`%s' is up to date.\n", gn->name);
742 } else if (gn->made == ABORTED) {
743 printf("`%s' not remade because of errors.\n", gn->name);
749 * If the user has defined a .END target, run its commands.
752 Lst_ForEach(&ENDNode->commands, Compat_RunCommand, gn);