compat.c revision 1.58 1 /* $NetBSD: compat.c,v 1.58 2005/05/08 04:19:12 christos 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 #ifndef MAKE_NATIVE
73 static char rcsid[] = "$NetBSD: compat.c,v 1.58 2005/05/08 04:19:12 christos 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.58 2005/05/08 04:19:12 christos 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
126 static void
127 Compat_Init(void)
128 {
129 const char *cp;
130
131 Shell_Init(); /* setup default shell */
132
133 for (cp = "#=|^(){};&<>*?[]:$`\\\n"; *cp != '\0'; cp++) {
134 meta[(unsigned char) *cp] = 1;
135 }
136 /*
137 * The null character serves as a sentinel in the string.
138 */
139 meta[0] = 1;
140 }
141
142 /*-
143 *-----------------------------------------------------------------------
144 * CompatInterrupt --
145 * Interrupt the creation of the current target and remove it if
146 * it ain't precious.
147 *
148 * Results:
149 * None.
150 *
151 * Side Effects:
152 * The target is removed and the process exits. If .INTERRUPT exists,
153 * its commands are run first WITH INTERRUPTS IGNORED..
154 *
155 *-----------------------------------------------------------------------
156 */
157 static void
158 CompatInterrupt(int signo)
159 {
160 GNode *gn;
161
162 if ((curTarg != NILGNODE) && !Targ_Precious (curTarg)) {
163 char *p1;
164 char *file = Var_Value(TARGET, curTarg, &p1);
165
166 if (!noExecute && eunlink(file) != -1) {
167 Error("*** %s removed", file);
168 }
169 if (p1)
170 free(p1);
171
172 /*
173 * Run .INTERRUPT only if hit with interrupt signal
174 */
175 if (signo == SIGINT) {
176 gn = Targ_FindNode(".INTERRUPT", TARG_NOCREATE);
177 if (gn != NILGNODE) {
178 Compat_Make(gn, gn);
179 }
180 }
181
182 }
183 exit (signo);
184 }
185
186 /*-
188 *-----------------------------------------------------------------------
189 * CompatRunCommand --
190 * Execute the next command for a target. If the command returns an
191 * error, the node's made field is set to ERROR and creation stops.
192 *
193 * Input:
194 * cmdp Command to execute
195 * gnp Node from which the command came
196 *
197 * Results:
198 * 0 if the command succeeded, 1 if an error occurred.
199 *
200 * Side Effects:
201 * The node's 'made' field may be set to ERROR.
202 *
203 *-----------------------------------------------------------------------
204 */
205 int
206 CompatRunCommand(ClientData cmdp, ClientData gnp)
207 {
208 char *cmdStart; /* Start of expanded command */
209 char *cp, *bp;
210 Boolean silent, /* Don't print command */
211 doIt, /* Execute even if -n */
212 errCheck; /* Check errors */
213 int reason; /* Reason for child's death */
214 int status; /* Description of child's death */
215 int cpid; /* Child actually found */
216 ReturnStatus retstat; /* Status of fork */
217 LstNode cmdNode; /* Node where current command is located */
218 const char **av; /* Argument vector for thing to exec */
219 int argc; /* Number of arguments in av or 0 if not
220 * dynamically allocated */
221 Boolean local; /* TRUE if command should be executed
222 * locally */
223 char *cmd = (char *) cmdp;
224 GNode *gn = (GNode *) gnp;
225
226 /*
227 * Avoid clobbered variable warnings by forcing the compiler
228 * to ``unregister'' variables
229 */
230 #if __GNUC__
231 (void) &av;
232 (void) &errCheck;
233 (void) &cmd;
234 #endif
235 silent = gn->type & OP_SILENT;
236 errCheck = !(gn->type & OP_IGNORE);
237 doIt = FALSE;
238
239 cmdNode = Lst_Member(gn->commands, (ClientData)cmd);
240 cmdStart = Var_Subst(NULL, cmd, gn, FALSE);
241
242 /*
243 * brk_string will return an argv with a NULL in av[0], thus causing
244 * execvp to choke and die horribly. Besides, how can we execute a null
245 * command? In any case, we warn the user that the command expanded to
246 * nothing (is this the right thing to do?).
247 */
248
249 if (*cmdStart == '\0') {
250 free(cmdStart);
251 Error("%s expands to empty string", cmd);
252 return(0);
253 } else {
254 cmd = cmdStart;
255 }
256 Lst_Replace(cmdNode, (ClientData)cmdStart);
257
258 if ((gn->type & OP_SAVE_CMDS) && (gn != ENDNode)) {
259 (void)Lst_AtEnd(ENDNode->commands, (ClientData)cmdStart);
260 return(0);
261 } else if (strcmp(cmdStart, "...") == 0) {
262 gn->type |= OP_SAVE_CMDS;
263 return(0);
264 }
265
266 while ((*cmd == '@') || (*cmd == '-') || (*cmd == '+')) {
267 switch (*cmd) {
268 case '@':
269 silent = TRUE;
270 break;
271 case '-':
272 errCheck = FALSE;
273 break;
274 case '+':
275 doIt = TRUE;
276 if (!meta[0]) /* we came here from jobs */
277 Compat_Init();
278 break;
279 }
280 cmd++;
281 }
282
283 while (isspace((unsigned char)*cmd))
284 cmd++;
285
286 /*
287 * Search for meta characters in the command. If there are no meta
288 * characters, there's no need to execute a shell to execute the
289 * command.
290 */
291 for (cp = cmd; !meta[(unsigned char)*cp]; cp++) {
292 continue;
293 }
294
295 /*
296 * Print the command before echoing if we're not supposed to be quiet for
297 * this one. We also print the command if -n given.
298 */
299 if (!silent || NoExecute(gn)) {
300 printf ("%s\n", cmd);
301 fflush(stdout);
302 }
303
304 /*
305 * If we're not supposed to execute any commands, this is as far as
306 * we go...
307 */
308 if (!doIt && NoExecute(gn)) {
309 return (0);
310 }
311
312 if (*cp != '\0') {
313 /*
314 * If *cp isn't the null character, we hit a "meta" character and
315 * need to pass the command off to the shell.
316 */
317 static const char *shargv[4];
318
319 shargv[0] = shellPath;
320 /*
321 * The following work for any of the builtin shell specs.
322 */
323 if (DEBUG(SHELL))
324 shargv[1] = "-xc";
325 else
326 shargv[1] = "-c";
327 shargv[2] = cmd;
328 shargv[3] = (char *)NULL;
329 av = shargv;
330 argc = 0;
331 bp = NULL;
332 } else {
333 /*
334 * No meta-characters, so no need to exec a shell. Break the command
335 * into words to form an argument vector we can execute.
336 */
337 av = (const char **)brk_string(cmd, &argc, TRUE, &bp);
338 }
339
340 local = TRUE;
341
342 /*
343 * Fork and execute the single command. If the fork fails, we abort.
344 */
345 cpid = vfork();
346 if (cpid < 0) {
347 Fatal("Could not fork");
348 }
349 if (cpid == 0) {
350 Check_Cwd(av);
351 if (local)
352 (void)execvp(av[0], (char *const *)UNCONST(av));
353 else
354 (void)execv(av[0], (char *const *)UNCONST(av));
355 execError("exec", av[0]);
356 _exit(1);
357 }
358 if (bp) {
359 free(av);
360 free(bp);
361 }
362 Lst_Replace(cmdNode, (ClientData) NULL);
363
364 /*
365 * The child is off and running. Now all we can do is wait...
366 */
367 while (1) {
368
369 while ((retstat = wait(&reason)) != cpid) {
370 if (retstat == -1 && errno != EINTR) {
371 break;
372 }
373 }
374
375 if (retstat > -1) {
376 if (WIFSTOPPED(reason)) {
377 status = WSTOPSIG(reason); /* stopped */
378 } else if (WIFEXITED(reason)) {
379 status = WEXITSTATUS(reason); /* exited */
380 if (status != 0) {
381 if (DEBUG(ERROR)) {
382 printf("\n*** Failed target: %s\n*** Failed command: ",
383 gn->name);
384 for (cp = cmd; *cp; ) {
385 if (isspace((unsigned char)*cp)) {
386 putchar(' ');
387 while (isspace((unsigned char)*cp))
388 cp++;
389 } else {
390 putchar(*cp);
391 cp++;
392 }
393 }
394 printf ("\n");
395 }
396 printf ("*** Error code %d", status);
397 }
398 } else {
399 status = WTERMSIG(reason); /* signaled */
400 printf ("*** Signal %d", status);
401 }
402
403
404 if (!WIFEXITED(reason) || (status != 0)) {
405 if (errCheck) {
406 gn->made = ERROR;
407 if (keepgoing) {
408 /*
409 * Abort the current target, but let others
410 * continue.
411 */
412 printf (" (continuing)\n");
413 }
414 } else {
415 /*
416 * Continue executing commands for this target.
417 * If we return 0, this will happen...
418 */
419 printf (" (ignored)\n");
420 status = 0;
421 }
422 }
423 break;
424 } else {
425 Fatal("error in wait: %d: %s", retstat, strerror(errno));
426 /*NOTREACHED*/
427 }
428 }
429 free(cmdStart);
430
431 return (status);
432 }
433
434 /*-
436 *-----------------------------------------------------------------------
437 * Compat_Make --
438 * Make a target.
439 *
440 * Input:
441 * gnp The node to make
442 * pgnp Parent to abort if necessary
443 *
444 * Results:
445 * 0
446 *
447 * Side Effects:
448 * If an error is detected and not being ignored, the process exits.
449 *
450 *-----------------------------------------------------------------------
451 */
452 int
453 Compat_Make(ClientData gnp, ClientData pgnp)
454 {
455 GNode *gn = (GNode *) gnp;
456 GNode *pgn = (GNode *) pgnp;
457
458 if (!meta[0]) /* we came here from jobs */
459 Compat_Init();
460 if (gn->made == UNMADE && (gn == pgn || (pgn->type & OP_MADE) == 0)) {
461 /*
462 * First mark ourselves to be made, then apply whatever transformations
463 * the suffix module thinks are necessary. Once that's done, we can
464 * descend and make all our children. If any of them has an error
465 * but the -k flag was given, our 'make' field will be set FALSE again.
466 * This is our signal to not attempt to do anything but abort our
467 * parent as well.
468 */
469 gn->flags |= REMAKE;
470 gn->made = BEINGMADE;
471 if ((gn->type & OP_MADE) == 0)
472 Suff_FindDeps(gn);
473 Lst_ForEach(gn->children, Compat_Make, (ClientData)gn);
474 if ((gn->flags & REMAKE) == 0) {
475 gn->made = ABORTED;
476 pgn->flags &= ~REMAKE;
477 goto cohorts;
478 }
479
480 if (Lst_Member(gn->iParents, pgn) != NILLNODE) {
481 char *p1;
482 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
483 if (p1)
484 free(p1);
485 }
486
487 /*
488 * All the children were made ok. Now cmtime contains the modification
489 * time of the newest child, we need to find out if we exist and when
490 * we were modified last. The criteria for datedness are defined by the
491 * Make_OODate function.
492 */
493 if (DEBUG(MAKE)) {
494 printf("Examining %s...", gn->name);
495 }
496 if (! Make_OODate(gn)) {
497 gn->made = UPTODATE;
498 if (DEBUG(MAKE)) {
499 printf("up-to-date.\n");
500 }
501 goto cohorts;
502 } else if (DEBUG(MAKE)) {
503 printf("out-of-date.\n");
504 }
505
506 /*
507 * If the user is just seeing if something is out-of-date, exit now
508 * to tell him/her "yes".
509 */
510 if (queryFlag) {
511 exit (1);
512 }
513
514 /*
515 * We need to be re-made. We also have to make sure we've got a $?
516 * variable. To be nice, we also define the $> variable using
517 * Make_DoAllVar().
518 */
519 Make_DoAllVar(gn);
520
521 /*
522 * Alter our type to tell if errors should be ignored or things
523 * should not be printed so CompatRunCommand knows what to do.
524 */
525 if (Targ_Ignore(gn)) {
526 gn->type |= OP_IGNORE;
527 }
528 if (Targ_Silent(gn)) {
529 gn->type |= OP_SILENT;
530 }
531
532 if (Job_CheckCommands(gn, Fatal)) {
533 /*
534 * Our commands are ok, but we still have to worry about the -t
535 * flag...
536 */
537 if (!touchFlag || (gn->type & OP_MAKE)) {
538 curTarg = gn;
539 Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
540 curTarg = NILGNODE;
541 } else {
542 Job_Touch(gn, gn->type & OP_SILENT);
543 }
544 } else {
545 gn->made = ERROR;
546 }
547
548 if (gn->made != ERROR) {
549 /*
550 * If the node was made successfully, mark it so, update
551 * its modification time and timestamp all its parents. Note
552 * that for .ZEROTIME targets, the timestamping isn't done.
553 * This is to keep its state from affecting that of its parent.
554 */
555 gn->made = MADE;
556 pgn->flags |= Make_Recheck(gn) == 0 ? FORCE : 0;
557 if (!(gn->type & OP_EXEC)) {
558 pgn->flags |= CHILDMADE;
559 Make_TimeStamp(pgn, gn);
560 }
561 } else if (keepgoing) {
562 pgn->flags &= ~REMAKE;
563 } else {
564 PrintOnError("\n\nStop.");
565 exit (1);
566 }
567 } else if (gn->made == ERROR) {
568 /*
569 * Already had an error when making this beastie. Tell the parent
570 * to abort.
571 */
572 pgn->flags &= ~REMAKE;
573 } else {
574 if (Lst_Member(gn->iParents, pgn) != NILLNODE) {
575 char *p1;
576 Var_Set(IMPSRC, Var_Value(TARGET, gn, &p1), pgn, 0);
577 if (p1)
578 free(p1);
579 }
580 switch(gn->made) {
581 case BEINGMADE:
582 Error("Graph cycles through %s", gn->name);
583 gn->made = ERROR;
584 pgn->flags &= ~REMAKE;
585 break;
586 case MADE:
587 if ((gn->type & OP_EXEC) == 0) {
588 pgn->flags |= CHILDMADE;
589 Make_TimeStamp(pgn, gn);
590 }
591 break;
592 case UPTODATE:
593 if ((gn->type & OP_EXEC) == 0) {
594 Make_TimeStamp(pgn, gn);
595 }
596 break;
597 default:
598 break;
599 }
600 }
601
602 cohorts:
603 Lst_ForEach(gn->cohorts, Compat_Make, pgnp);
604 return (0);
605 }
606
607 /*-
609 *-----------------------------------------------------------------------
610 * Compat_Run --
611 * Initialize this mode and start making.
612 *
613 * Input:
614 * targs List of target nodes to re-create
615 *
616 * Results:
617 * None.
618 *
619 * Side Effects:
620 * Guess what?
621 *
622 *-----------------------------------------------------------------------
623 */
624 void
625 Compat_Run(Lst targs)
626 {
627 GNode *gn = NULL;/* Current root target */
628 int errors; /* Number of targets not remade due to errors */
629
630 Compat_Init();
631
632 if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
633 signal(SIGINT, CompatInterrupt);
634 }
635 if (signal(SIGTERM, SIG_IGN) != SIG_IGN) {
636 signal(SIGTERM, CompatInterrupt);
637 }
638 if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
639 signal(SIGHUP, CompatInterrupt);
640 }
641 if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) {
642 signal(SIGQUIT, CompatInterrupt);
643 }
644
645 ENDNode = Targ_FindNode(".END", TARG_CREATE);
646 ENDNode->type = OP_SPECIAL;
647 /*
648 * If the user has defined a .BEGIN target, execute the commands attached
649 * to it.
650 */
651 if (!queryFlag) {
652 gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
653 if (gn != NILGNODE) {
654 Compat_Make(gn, gn);
655 if (gn->made == ERROR) {
656 PrintOnError("\n\nStop.");
657 exit(1);
658 }
659 }
660 }
661
662 /*
663 * Expand .USE nodes right now, because they can modify the structure
664 * of the tree.
665 */
666 Lst_Destroy(Make_ExpandUse(targs), NOFREE);
667
668 /*
669 * For each entry in the list of targets to create, call Compat_Make on
670 * it to create the thing. Compat_Make will leave the 'made' field of gn
671 * in one of several states:
672 * UPTODATE gn was already up-to-date
673 * MADE gn was recreated successfully
674 * ERROR An error occurred while gn was being created
675 * ABORTED gn was not remade because one of its inferiors
676 * could not be made due to errors.
677 */
678 errors = 0;
679 while (!Lst_IsEmpty (targs)) {
680 gn = (GNode *) Lst_DeQueue(targs);
681 Compat_Make(gn, gn);
682
683 if (gn->made == UPTODATE) {
684 printf ("`%s' is up to date.\n", gn->name);
685 } else if (gn->made == ABORTED) {
686 printf ("`%s' not remade because of errors.\n", gn->name);
687 errors += 1;
688 }
689 }
690
691 /*
692 * If the user has defined a .END target, run its commands.
693 */
694 if (errors == 0) {
695 Compat_Make(ENDNode, ENDNode);
696 if (gn->made == ERROR) {
697 PrintOnError("\n\nStop.");
698 exit(1);
699 }
700 }
701 }
702