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