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