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