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
41 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
43 static const char rcsid[] =
44 "$FreeBSD: src/usr.bin/make/compat.c,v 1.16.2.2 2000/07/01 12:24:21 ps Exp $";
50 * The routines in this file implement the full-compatibility
51 * mode of PMake. Most of the special functionality of PMake
52 * is available in this mode. Things not supported:
54 * - friendly variable substitution.
57 * Compat_Run Initialize things for this module and recreate
58 * thems as need creatin'
62 #include <sys/types.h>
74 * The following array is used to make a fast determination of which
75 * characters are interpreted specially by the shell. If a command
76 * contains any of these characters, it is executed by the shell, not
80 static char meta[256];
82 static GNode *curTarg = NILGNODE;
83 static GNode *ENDNode;
84 static void CompatInterrupt __P((int));
85 static int CompatRunCommand __P((ClientData, ClientData));
86 static int CompatMake __P((ClientData, ClientData));
88 static char *sh_builtin[] = {
89 "alias", "cd", "eval", "exec", "exit", "read", "set", "ulimit",
90 "unalias", "umask", "unset", "wait", ":", 0};
93 *-----------------------------------------------------------------------
95 * Interrupt the creation of the current target and remove it if
102 * The target is removed and the process exits. If .INTERRUPT exists,
103 * its commands are run first WITH INTERRUPTS IGNORED..
105 *-----------------------------------------------------------------------
108 CompatInterrupt (signo)
113 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
115 char *file = Var_Value (TARGET, curTarg, &p1);
117 if (!noExecute && eunlink(file) != -1) {
118 printf ("*** %s removed\n", file);
123 * Run .INTERRUPT only if hit with interrupt signal
125 if (signo == SIGINT) {
126 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
127 if (gn != NILGNODE) {
128 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
133 if (signo == SIGQUIT)
135 (void) signal(signo, SIG_DFL);
136 (void) kill(getpid(), signo);
140 *-----------------------------------------------------------------------
144 * Returns 1 if a specified line must be executed by the shell,
145 * 0 if it can be run via execve, and -1 if the command is a no-op.
150 *-----------------------------------------------------------------------
159 av = brk_string(cmd, &ac, TRUE);
160 for(p = sh_builtin; *p != 0; p++)
161 if (strcmp(av[1], *p) == 0)
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 * 0 if the command succeeded, 1 if an error occurred.
176 * The node's 'made' field may be set to ERROR.
178 *-----------------------------------------------------------------------
181 CompatRunCommand (cmdp, gnp)
182 ClientData cmdp; /* Command to execute */
183 ClientData gnp; /* Node from which the command came */
185 char *cmdStart; /* Start of expanded command */
187 Boolean silent, /* Don't print command */
188 errCheck; /* Check errors */
189 int reason; /* Reason for child's death */
190 int status; /* Description of child's death */
191 int cpid; /* Child actually found */
192 ReturnStatus stat; /* Status of fork */
193 LstNode cmdNode; /* Node where current command is located */
194 char **av; /* Argument vector for thing to exec */
195 int argc; /* Number of arguments in av or 0 if not
196 * dynamically allocated */
197 Boolean local; /* TRUE if command should be executed
199 int internal; /* Various values.. */
200 char *cmd = (char *) cmdp;
201 GNode *gn = (GNode *) gnp;
204 * Avoid clobbered variable warnings by forcing the compiler
205 * to ``unregister'' variables
211 silent = gn->type & OP_SILENT;
212 errCheck = !(gn->type & OP_IGNORE);
214 cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
215 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
218 * brk_string will return an argv with a NULL in av[0], thus causing
219 * execvp to choke and die horribly. Besides, how can we execute a null
220 * command? In any case, we warn the user that the command expanded to
221 * nothing (is this the right thing to do?).
224 if (*cmdStart == '\0') {
226 Error("%s expands to empty string", cmd);
231 Lst_Replace (cmdNode, (ClientData)cmdStart);
233 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
234 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
236 } else if (strcmp(cmdStart, "...") == 0) {
237 gn->type |= OP_SAVE_CMDS;
241 while ((*cmd == '@') || (*cmd == '-')) {
243 silent = DEBUG(LOUD) ? FALSE : TRUE;
250 while (isspace((unsigned char)*cmd))
254 * Search for meta characters in the command. If there are no meta
255 * characters, there's no need to execute a shell to execute the
258 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
263 * Print the command before echoing if we're not supposed to be quiet for
264 * this one. We also print the command if -n given.
266 if (!silent || noExecute) {
267 printf ("%s\n", cmd);
272 * If we're not supposed to execute any commands, this is as far as
281 * If *cp isn't the null character, we hit a "meta" character and
282 * need to pass the command off to the shell. We give the shell the
283 * -e flag as well as -c if it's supposed to exit when it hits an
286 static char *shargv[4] = { "/bin/sh" };
288 shargv[1] = (errCheck ? "-ec" : "-c");
290 shargv[3] = (char *)NULL;
293 } else if ((internal = shellneed(cmd))) {
295 * This command must be passed by the shell for other reasons..
296 * or.. possibly not at all.
298 static char *shargv[4] = { "/bin/sh" };
300 if (internal == -1) {
301 /* Command does not need to be executed */
305 shargv[1] = (errCheck ? "-ec" : "-c");
307 shargv[3] = (char *)NULL;
312 * No meta-characters, so no need to exec a shell. Break the command
313 * into words to form an argument vector we can execute.
314 * brk_string sticks our name in av[0], so we have to
317 av = brk_string(cmd, &argc, TRUE);
324 * Fork and execute the single command. If the fork fails, we abort.
328 Fatal("Could not fork");
333 (void) write (2, av[0], strlen (av[0]));
334 (void) write (2, ":", 1);
335 (void) write (2, strerror(errno), strlen(strerror(errno)));
336 (void) write (2, "\n", 1);
338 (void)execv(av[0], av);
344 * we need to print out the command associated with this Gnode in
345 * Targ_PrintCmd from Targ_PrintGraph when debugging at level g2,
346 * in main(), Fatal() and DieHorribly(), therefore do not free it
349 if (!DEBUG(GRAPH2)) {
351 Lst_Replace (cmdNode, cmdp);
355 * The child is off and running. Now all we can do is wait...
359 while ((stat = wait(&reason)) != cpid) {
360 if (stat == -1 && errno != EINTR) {
366 if (WIFSTOPPED(reason)) {
367 status = WSTOPSIG(reason); /* stopped */
368 } else if (WIFEXITED(reason)) {
369 status = WEXITSTATUS(reason); /* exited */
371 printf ("*** Error code %d", status);
374 status = WTERMSIG(reason); /* signaled */
375 printf ("*** Signal %d", status);
379 if (!WIFEXITED(reason) || (status != 0)) {
384 * Abort the current target, but let others
387 printf (" (continuing)\n");
391 * Continue executing commands for this target.
392 * If we return 0, this will happen...
394 printf (" (ignored)\n");
400 Fatal ("error in wait: %d", stat);
409 *-----------------------------------------------------------------------
417 * If an error is detected and not being ignored, the process exits.
419 *-----------------------------------------------------------------------
422 CompatMake (gnp, pgnp)
423 ClientData gnp; /* The node to make */
424 ClientData pgnp; /* Parent to abort if necessary */
426 GNode *gn = (GNode *) gnp;
427 GNode *pgn = (GNode *) pgnp;
428 if (gn->type & OP_USE) {
429 Make_HandleUse(gn, pgn);
430 } else if (gn->made == UNMADE) {
432 * First mark ourselves to be made, then apply whatever transformations
433 * the suffix module thinks are necessary. Once that's done, we can
434 * descend and make all our children. If any of them has an error
435 * but the -k flag was given, our 'make' field will be set FALSE again.
436 * This is our signal to not attempt to do anything but abort our
440 gn->made = BEINGMADE;
442 Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
449 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
451 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
456 * All the children were made ok. Now cmtime contains the modification
457 * time of the newest child, we need to find out if we exist and when
458 * we were modified last. The criteria for datedness are defined by the
459 * Make_OODate function.
462 printf("Examining %s...", gn->name);
464 if (! Make_OODate(gn)) {
467 printf("up-to-date.\n");
470 } else if (DEBUG(MAKE)) {
471 printf("out-of-date.\n");
475 * If the user is just seeing if something is out-of-date, exit now
476 * to tell him/her "yes".
483 * We need to be re-made. We also have to make sure we've got a $?
484 * variable. To be nice, we also define the $> variable using
490 * Alter our type to tell if errors should be ignored or things
491 * should not be printed so CompatRunCommand knows what to do.
493 if (Targ_Ignore (gn)) {
494 gn->type |= OP_IGNORE;
496 if (Targ_Silent (gn)) {
497 gn->type |= OP_SILENT;
500 if (Job_CheckCommands (gn, Fatal)) {
502 * Our commands are ok, but we still have to worry about the -t
507 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
510 Job_Touch (gn, gn->type & OP_SILENT);
516 if (gn->made != ERROR) {
518 * If the node was made successfully, mark it so, update
519 * its modification time and timestamp all its parents. Note
520 * that for .ZEROTIME targets, the timestamping isn't done.
521 * This is to keep its state from affecting that of its parent.
526 * We can't re-stat the thing, but we can at least take care of
527 * rules where a target depends on a source that actually creates
528 * the target, but only if it has changed, e.g.
536 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
538 * In this case, if the definitions produced by yacc haven't
539 * changed from before, parse.h won't have been updated and
540 * gn->mtime will reflect the current modification time for
541 * parse.h. This is something of a kludge, I admit, but it's a
544 * XXX: People like to use a rule like
548 * To force things that depend on FRC to be made, so we have to
549 * check for gn->children being empty as well...
551 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
556 * This is what Make does and it's actually a good thing, as it
559 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
561 * to function as intended. Unfortunately, thanks to the stateless
562 * nature of NFS (and the speed of this program), there are times
563 * when the modification time of a file created on a remote
564 * machine will not be modified before the stat() implied by
565 * the Dir_MTime occurs, thus leading us to believe that the file
566 * is unchanged, wreaking havoc with files that depend on this one.
568 * I have decided it is better to make too much than to make too
569 * little, so this stuff is commented out unless you're sure it's
573 if (noExecute || Dir_MTime(gn) == 0) {
576 if (gn->cmtime > gn->mtime)
577 gn->mtime = gn->cmtime;
579 printf("update time: %s\n", Targ_FmtTime(gn->mtime));
582 if (!(gn->type & OP_EXEC)) {
583 pgn->childMade = TRUE;
584 Make_TimeStamp(pgn, gn);
586 } else if (keepgoing) {
591 printf ("\n\nStop in %s.\n", Var_Value(".CURDIR", gn, &p1));
595 } else if (gn->made == ERROR) {
597 * Already had an error when making this beastie. Tell the parent
602 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
604 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
609 Error("Graph cycles through %s\n", gn->name);
614 if ((gn->type & OP_EXEC) == 0) {
615 pgn->childMade = TRUE;
616 Make_TimeStamp(pgn, gn);
620 if ((gn->type & OP_EXEC) == 0) {
621 Make_TimeStamp(pgn, gn);
633 *-----------------------------------------------------------------------
635 * Initialize this mode and start making.
643 *-----------------------------------------------------------------------
647 Lst targs; /* List of target nodes to re-create */
649 char *cp; /* Pointer to string of shell meta-characters */
650 GNode *gn = NULL;/* Current root target */
651 int errors; /* Number of targets not remade due to errors */
653 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
654 signal(SIGINT, CompatInterrupt);
656 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
657 signal(SIGTERM, CompatInterrupt);
659 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
660 signal(SIGHUP, CompatInterrupt);
662 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
663 signal(SIGQUIT, CompatInterrupt);
666 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
667 meta[(unsigned char) *cp] = 1;
670 * The null character serves as a sentinel in the string.
674 ENDNode = Targ_FindNode(".END", TARG_CREATE);
676 * If the user has defined a .BEGIN target, execute the commands attached
680 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
681 if (gn != NILGNODE) {
682 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
683 if (gn->made == ERROR) {
684 printf("\n\nStop.\n");
691 * For each entry in the list of targets to create, call CompatMake on
692 * it to create the thing. CompatMake will leave the 'made' field of gn
693 * in one of several states:
694 * UPTODATE gn was already up-to-date
695 * MADE gn was recreated successfully
696 * ERROR An error occurred while gn was being created
697 * ABORTED gn was not remade because one of its inferiors
698 * could not be made due to errors.
701 while (!Lst_IsEmpty (targs)) {
702 gn = (GNode *) Lst_DeQueue (targs);
705 if (gn->made == UPTODATE) {
706 printf ("`%s' is up to date.\n", gn->name);
707 } else if (gn->made == ABORTED) {
708 printf ("`%s' not remade because of errors.\n", gn->name);
714 * If the user has defined a .END target, run its commands.
717 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);