compat.c revision 1.25 1 /* $NetBSD: compat.c,v 1.25 1998/11/01 03:07:33 itohy Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * Copyright (c) 1988, 1989 by Adam de Boor
6 * Copyright (c) 1989 by Berkeley Softworks
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Adam de Boor.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifdef MAKE_BOOTSTRAP
42 static char rcsid[] = "$NetBSD: compat.c,v 1.25 1998/11/01 03:07:33 itohy Exp $";
43 #else
44 #include <sys/cdefs.h>
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)compat.c 8.2 (Berkeley) 3/19/94";
48 #else
49 __RCSID("$NetBSD: compat.c,v 1.25 1998/11/01 03:07:33 itohy Exp $");
50 #endif
51 #endif /* not lint */
52 #endif
53
54 /*-
55 * compat.c --
56 * The routines in this file implement the full-compatibility
57 * mode of PMake. Most of the special functionality of PMake
58 * is available in this mode. Things not supported:
59 * - different shells.
60 * - friendly variable substitution.
61 *
62 * Interface:
63 * Compat_Run Initialize things for this module and recreate
64 * thems as need creatin'
65 */
66
67 #include <stdio.h>
68 #include <sys/types.h>
69 #include <sys/stat.h>
70 #include <sys/wait.h>
71 #include <ctype.h>
72 #include <errno.h>
73 #include <signal.h>
74 #include "make.h"
75 #include "hash.h"
76 #include "dir.h"
77 #include "job.h"
78 extern int errno;
79
80 /*
81 * The following array is used to make a fast determination of which
82 * characters are interpreted specially by the shell. If a command
83 * contains any of these characters, it is executed by the shell, not
84 * directly by us.
85 */
86
87 static char meta[256];
88
89 static GNode *curTarg = NILGNODE;
90 static GNode *ENDNode;
91 static void CompatInterrupt __P((int));
92 static int CompatRunCommand __P((ClientData, ClientData));
93 static int CompatMake __P((ClientData, ClientData));
94
95 /*-
96 *-----------------------------------------------------------------------
97 * CompatInterrupt --
98 * Interrupt the creation of the current target and remove it if
99 * it ain't precious.
100 *
101 * Results:
102 * None.
103 *
104 * Side Effects:
105 * The target is removed and the process exits. If .INTERRUPT exists,
106 * its commands are run first WITH INTERRUPTS IGNORED..
107 *
108 *-----------------------------------------------------------------------
109 */
110 static void
111 CompatInterrupt (signo)
112 int signo;
113 {
114 GNode *gn;
115
116 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
117 char *p1;
118 char *file = Var_Value (TARGET, curTarg, &p1);
119
120 if (!noExecute && eunlink(file) != -1) {
121 Error("*** %s removed\n", file);
122 }
123 if (p1)
124 free(p1);
125
126 /*
127 * Run .INTERRUPT only if hit with interrupt signal
128 */
129 if (signo == SIGINT) {
130 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
131 if (gn != NILGNODE) {
132 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
133 }
134 }
135
136 }
137 exit (signo);
138 }
139
140 /*-
142 *-----------------------------------------------------------------------
143 * CompatRunCommand --
144 * Execute the next command for a target. If the command returns an
145 * error, the node's made field is set to ERROR and creation stops.
146 *
147 * Results:
148 * 0 if the command succeeded, 1 if an error occurred.
149 *
150 * Side Effects:
151 * The node's 'made' field may be set to ERROR.
152 *
153 *-----------------------------------------------------------------------
154 */
155 static int
156 CompatRunCommand (cmdp, gnp)
157 ClientData cmdp; /* Command to execute */
158 ClientData gnp; /* Node from which the command came */
159 {
160 char *cmdStart; /* Start of expanded command */
161 char *cp, *bp;
162 Boolean silent, /* Don't print command */
163 errCheck; /* Check errors */
164 int reason; /* Reason for child's death */
165 int status; /* Description of child's death */
166 int cpid; /* Child actually found */
167 ReturnStatus stat; /* Status of fork */
168 LstNode cmdNode; /* Node where current command is located */
169 char **av; /* Argument vector for thing to exec */
170 int argc; /* Number of arguments in av or 0 if not
171 * dynamically allocated */
172 Boolean local; /* TRUE if command should be executed
173 * locally */
174 char *cmd = (char *) cmdp;
175 GNode *gn = (GNode *) gnp;
176
177 /*
178 * Avoid clobbered variable warnings by forcing the compiler
179 * to ``unregister'' variables
180 */
181 #if __GNUC__
182 (void) &av;
183 (void) &errCheck;
184 #endif
185 silent = gn->type & OP_SILENT;
186 errCheck = !(gn->type & OP_IGNORE);
187
188 cmdNode = Lst_Member (gn->commands, (ClientData)cmd);
189 cmdStart = Var_Subst (NULL, cmd, gn, FALSE);
190
191 /*
192 * brk_string will return an argv with a NULL in av[0], thus causing
193 * execvp to choke and die horribly. Besides, how can we execute a null
194 * command? In any case, we warn the user that the command expanded to
195 * nothing (is this the right thing to do?).
196 */
197
198 if (*cmdStart == '\0') {
199 free(cmdStart);
200 Error("%s expands to empty string", cmd);
201 return(0);
202 } else {
203 cmd = cmdStart;
204 }
205 Lst_Replace (cmdNode, (ClientData)cmdStart);
206
207 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
208 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
209 return(0);
210 } else if (strcmp(cmdStart, "...") == 0) {
211 gn->type |= OP_SAVE_CMDS;
212 return(0);
213 }
214
215 while ((*cmd == '@') || (*cmd == '-')) {
216 if (*cmd == '@') {
217 silent = TRUE;
218 } else {
219 errCheck = FALSE;
220 }
221 cmd++;
222 }
223
224 while (isspace((unsigned char)*cmd))
225 cmd++;
226
227 /*
228 * Search for meta characters in the command. If there are no meta
229 * characters, there's no need to execute a shell to execute the
230 * command.
231 */
232 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
233 continue;
234 }
235
236 /*
237 * Print the command before echoing if we're not supposed to be quiet for
238 * this one. We also print the command if -n given.
239 */
240 if (!silent || (noExecute && !(gn->type & OP_MAKE))) {
241 printf ("%s\n", cmd);
242 fflush(stdout);
243 }
244
245 /*
246 * If we're not supposed to execute any commands, this is as far as
247 * we go...
248 */
249 if (noExecute && !(gn->type & OP_MAKE)) {
250 return (0);
251 }
252
253 if (*cp != '\0') {
254 /*
255 * If *cp isn't the null character, we hit a "meta" character and
256 * need to pass the command off to the shell. We give the shell the
257 * -e flag as well as -c if it's supposed to exit when it hits an
258 * error.
259 */
260 static char *shargv[4] = { "/bin/sh" };
261
262 shargv[1] = (errCheck ? "-ec" : "-c");
263 shargv[2] = cmd;
264 shargv[3] = (char *)NULL;
265 av = shargv;
266 argc = 0;
267 bp = NULL;
268 } else {
269 /*
270 * No meta-characters, so no need to exec a shell. Break the command
271 * into words to form an argument vector we can execute.
272 */
273 av = brk_string(cmd, &argc, TRUE, &bp);
274 }
275
276 local = TRUE;
277
278 /*
279 * Fork and execute the single command. If the fork fails, we abort.
280 */
281 cpid = vfork();
282 if (cpid < 0) {
283 Fatal("Could not fork");
284 }
285 if (cpid == 0) {
286 if (local) {
287 execvp(av[0], av);
288 (void) write (2, av[0], strlen (av[0]));
289 (void) write (2, ": not found\n", sizeof(": not found"));
290 } else {
291 (void)execv(av[0], av);
292 }
293 _exit(1);
294 }
295 if (bp) {
296 free(av);
297 free(bp);
298 }
299 free(cmdStart);
300 Lst_Replace (cmdNode, (ClientData) NULL);
301
302 /*
303 * The child is off and running. Now all we can do is wait...
304 */
305 while (1) {
306
307 while ((stat = wait(&reason)) != cpid) {
308 if (stat == -1 && errno != EINTR) {
309 break;
310 }
311 }
312
313 if (stat > -1) {
314 if (WIFSTOPPED(reason)) {
315 status = WSTOPSIG(reason); /* stopped */
316 } else if (WIFEXITED(reason)) {
317 status = WEXITSTATUS(reason); /* exited */
318 if (status != 0) {
319 printf ("*** Error code %d", status);
320 }
321 } else {
322 status = WTERMSIG(reason); /* signaled */
323 printf ("*** Signal %d", status);
324 }
325
326
327 if (!WIFEXITED(reason) || (status != 0)) {
328 if (errCheck) {
329 gn->made = ERROR;
330 if (keepgoing) {
331 /*
332 * Abort the current target, but let others
333 * continue.
334 */
335 printf (" (continuing)\n");
336 }
337 } else {
338 /*
339 * Continue executing commands for this target.
340 * If we return 0, this will happen...
341 */
342 printf (" (ignored)\n");
343 status = 0;
344 }
345 }
346 break;
347 } else {
348 Fatal ("error in wait: %d", stat);
349 /*NOTREACHED*/
350 }
351 }
352
353 return (status);
354 }
355
356 /*-
358 *-----------------------------------------------------------------------
359 * CompatMake --
360 * Make a target.
361 *
362 * Results:
363 * 0
364 *
365 * Side Effects:
366 * If an error is detected and not being ignored, the process exits.
367 *
368 *-----------------------------------------------------------------------
369 */
370 static int
371 CompatMake (gnp, pgnp)
372 ClientData gnp; /* The node to make */
373 ClientData pgnp; /* Parent to abort if necessary */
374 {
375 GNode *gn = (GNode *) gnp;
376 GNode *pgn = (GNode *) pgnp;
377
378 if (pgn->type & OP_MADE) {
379 (void) Dir_MTime(gn);
380 gn->made = UPTODATE;
381 }
382
383 if (gn->made == UNMADE) {
384 /*
385 * First mark ourselves to be made, then apply whatever transformations
386 * the suffix module thinks are necessary. Once that's done, we can
387 * descend and make all our children. If any of them has an error
388 * but the -k flag was given, our 'make' field will be set FALSE again.
389 * This is our signal to not attempt to do anything but abort our
390 * parent as well.
391 */
392 gn->make = TRUE;
393 gn->made = BEINGMADE;
394 Suff_FindDeps (gn);
395 Lst_ForEach (gn->children, CompatMake, (ClientData)gn);
396 if (!gn->make) {
397 gn->made = ABORTED;
398 pgn->make = FALSE;
399 return (0);
400 }
401
402 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
403 char *p1;
404 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
405 if (p1)
406 free(p1);
407 }
408
409 /*
410 * All the children were made ok. Now cmtime contains the modification
411 * time of the newest child, we need to find out if we exist and when
412 * we were modified last. The criteria for datedness are defined by the
413 * Make_OODate function.
414 */
415 if (DEBUG(MAKE)) {
416 printf("Examining %s...", gn->name);
417 }
418 if (! Make_OODate(gn)) {
419 gn->made = UPTODATE;
420 if (DEBUG(MAKE)) {
421 printf("up-to-date.\n");
422 }
423 return (0);
424 } else if (DEBUG(MAKE)) {
425 printf("out-of-date.\n");
426 }
427
428 /*
429 * If the user is just seeing if something is out-of-date, exit now
430 * to tell him/her "yes".
431 */
432 if (queryFlag) {
433 exit (-1);
434 }
435
436 /*
437 * We need to be re-made. We also have to make sure we've got a $?
438 * variable. To be nice, we also define the $> variable using
439 * Make_DoAllVar().
440 */
441 Make_DoAllVar(gn);
442
443 /*
444 * Alter our type to tell if errors should be ignored or things
445 * should not be printed so CompatRunCommand knows what to do.
446 */
447 if (Targ_Ignore (gn)) {
448 gn->type |= OP_IGNORE;
449 }
450 if (Targ_Silent (gn)) {
451 gn->type |= OP_SILENT;
452 }
453
454 if (Job_CheckCommands (gn, Fatal)) {
455 /*
456 * Our commands are ok, but we still have to worry about the -t
457 * flag...
458 */
459 if (!touchFlag || (gn->type & OP_MAKE)) {
460 curTarg = gn;
461 Lst_ForEach (gn->commands, CompatRunCommand, (ClientData)gn);
462 curTarg = NILGNODE;
463 } else {
464 Job_Touch (gn, gn->type & OP_SILENT);
465 }
466 } else {
467 gn->made = ERROR;
468 }
469
470 if (gn->made != ERROR) {
471 /*
472 * If the node was made successfully, mark it so, update
473 * its modification time and timestamp all its parents. Note
474 * that for .ZEROTIME targets, the timestamping isn't done.
475 * This is to keep its state from affecting that of its parent.
476 */
477 gn->made = MADE;
478 #ifndef RECHECK
479 /*
480 * We can't re-stat the thing, but we can at least take care of
481 * rules where a target depends on a source that actually creates
482 * the target, but only if it has changed, e.g.
483 *
484 * parse.h : parse.o
485 *
486 * parse.o : parse.y
487 * yacc -d parse.y
488 * cc -c y.tab.c
489 * mv y.tab.o parse.o
490 * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
491 *
492 * In this case, if the definitions produced by yacc haven't
493 * changed from before, parse.h won't have been updated and
494 * gn->mtime will reflect the current modification time for
495 * parse.h. This is something of a kludge, I admit, but it's a
496 * useful one..
497 *
498 * XXX: People like to use a rule like
499 *
500 * FRC:
501 *
502 * To force things that depend on FRC to be made, so we have to
503 * check for gn->children being empty as well...
504 */
505 if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
506 gn->mtime = now;
507 }
508 #else
509 /*
510 * This is what Make does and it's actually a good thing, as it
511 * allows rules like
512 *
513 * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
514 *
515 * to function as intended. Unfortunately, thanks to the stateless
516 * nature of NFS (and the speed of this program), there are times
517 * when the modification time of a file created on a remote
518 * machine will not be modified before the stat() implied by
519 * the Dir_MTime occurs, thus leading us to believe that the file
520 * is unchanged, wreaking havoc with files that depend on this one.
521 *
522 * I have decided it is better to make too much than to make too
523 * little, so this stuff is commented out unless you're sure it's
524 * ok.
525 * -- ardeb 1/12/88
526 */
527 if ((noExecute && !(gn->type & OP_MAKE)) ||
528 (gn->type & OP_SAVE_CMDS) || Dir_MTime(gn) == 0) {
529 gn->mtime = now;
530 }
531 if (gn->cmtime > gn->mtime)
532 gn->mtime = gn->cmtime;
533 if (DEBUG(MAKE)) {
534 printf("update time: %s\n", Targ_FmtTime(gn->mtime));
535 }
536 #endif
537 if (!(gn->type & OP_EXEC)) {
538 pgn->childMade = TRUE;
539 Make_TimeStamp(pgn, gn);
540 }
541 } else if (keepgoing) {
542 pgn->make = FALSE;
543 } else {
544 printf ("\n\nStop.\n");
545 exit (1);
546 }
547 } else if (gn->made == ERROR) {
548 /*
549 * Already had an error when making this beastie. Tell the parent
550 * to abort.
551 */
552 pgn->make = FALSE;
553 } else {
554 if (Lst_Member (gn->iParents, pgn) != NILLNODE) {
555 char *p1;
556 Var_Set (IMPSRC, Var_Value(TARGET, gn, &p1), pgn);
557 if (p1)
558 free(p1);
559 }
560 switch(gn->made) {
561 case BEINGMADE:
562 Error("Graph cycles through %s\n", gn->name);
563 gn->made = ERROR;
564 pgn->make = FALSE;
565 break;
566 case MADE:
567 if ((gn->type & OP_EXEC) == 0) {
568 pgn->childMade = TRUE;
569 Make_TimeStamp(pgn, gn);
570 }
571 break;
572 case UPTODATE:
573 if ((gn->type & OP_EXEC) == 0) {
574 Make_TimeStamp(pgn, gn);
575 }
576 break;
577 default:
578 break;
579 }
580 }
581
582 return (0);
583 }
584
585 /*-
587 *-----------------------------------------------------------------------
588 * Compat_Run --
589 * Initialize this mode and start making.
590 *
591 * Results:
592 * None.
593 *
594 * Side Effects:
595 * Guess what?
596 *
597 *-----------------------------------------------------------------------
598 */
599 void
600 Compat_Run(targs)
601 Lst targs; /* List of target nodes to re-create */
602 {
603 char *cp; /* Pointer to string of shell meta-characters */
604 GNode *gn = NULL;/* Current root target */
605 int errors; /* Number of targets not remade due to errors */
606
607 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
608 signal(SIGINT, CompatInterrupt);
609 }
610 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
611 signal(SIGTERM, CompatInterrupt);
612 }
613 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
614 signal(SIGHUP, CompatInterrupt);
615 }
616 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
617 signal(SIGQUIT, CompatInterrupt);
618 }
619
620 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
621 meta[(unsigned char) *cp] = 1;
622 }
623 /*
624 * The null character serves as a sentinel in the string.
625 */
626 meta[0] = 1;
627
628 ENDNode = Targ_FindNode(".END", TARG_CREATE);
629 /*
630 * If the user has defined a .BEGIN target, execute the commands attached
631 * to it.
632 */
633 if (!queryFlag) {
634 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
635 if (gn != NILGNODE) {
636 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
637 if (gn->made == ERROR) {
638 printf("\n\nStop.\n");
639 exit(1);
640 }
641 }
642 }
643
644 /*
645 * Expand .USE nodes right now, because they can modify the structure
646 * of the tree.
647 */
648 Lst_Destroy(Make_ExpandUse(targs), NOFREE);
649
650 /*
651 * For each entry in the list of targets to create, call CompatMake on
652 * it to create the thing. CompatMake will leave the 'made' field of gn
653 * in one of several states:
654 * UPTODATE gn was already up-to-date
655 * MADE gn was recreated successfully
656 * ERROR An error occurred while gn was being created
657 * ABORTED gn was not remade because one of its inferiors
658 * could not be made due to errors.
659 */
660 errors = 0;
661 while (!Lst_IsEmpty (targs)) {
662 gn = (GNode *) Lst_DeQueue (targs);
663 CompatMake (gn, gn);
664
665 if (gn->made == UPTODATE) {
666 printf ("`%s' is up to date.\n", gn->name);
667 } else if (gn->made == ABORTED) {
668 printf ("`%s' not remade because of errors.\n", gn->name);
669 errors += 1;
670 }
671 }
672
673 /*
674 * If the user has defined a .END target, run its commands.
675 */
676 if (errors == 0) {
677 Lst_ForEach(ENDNode->commands, CompatRunCommand, (ClientData)gn);
678 }
679 }
680