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