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