Annotation of src/usr.bin/make/make.c, Revision 1.7
1.6 millert 1: /* $OpenBSD: make.c,v 1.5 1997/04/01 07:28:17 millert Exp $ */
2: /* $NetBSD: make.c,v 1.10 1996/11/06 17:59:15 christos Exp $ */
1.1 deraadt 3:
4: /*
1.4 millert 5: * Copyright (c) 1988, 1989, 1990, 1993
6: * The Regents of the University of California. All rights reserved.
1.1 deraadt 7: * Copyright (c) 1989 by Berkeley Softworks
8: * All rights reserved.
9: *
10: * This code is derived from software contributed to Berkeley by
11: * Adam de Boor.
12: *
13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
16: * 1. Redistributions of source code must retain the above copyright
17: * notice, this list of conditions and the following disclaimer.
18: * 2. Redistributions in binary form must reproduce the above copyright
19: * notice, this list of conditions and the following disclaimer in the
20: * documentation and/or other materials provided with the distribution.
21: * 3. All advertising materials mentioning features or use of this software
22: * must display the following acknowledgement:
23: * This product includes software developed by the University of
24: * California, Berkeley and its contributors.
25: * 4. Neither the name of the University nor the names of its contributors
26: * may be used to endorse or promote products derived from this software
27: * without specific prior written permission.
28: *
29: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39: * SUCH DAMAGE.
40: */
41:
42: #ifndef lint
43: #if 0
1.4 millert 44: static char sccsid[] = "@(#)make.c 8.1 (Berkeley) 6/6/93";
1.1 deraadt 45: #else
1.6 millert 46: static char rcsid[] = "$OpenBSD: make.c,v 1.5 1997/04/01 07:28:17 millert Exp $";
1.1 deraadt 47: #endif
48: #endif /* not lint */
49:
50: /*-
51: * make.c --
52: * The functions which perform the examination of targets and
53: * their suitability for creation
54: *
55: * Interface:
56: * Make_Run Initialize things for the module and recreate
57: * whatever needs recreating. Returns TRUE if
58: * work was (or would have been) done and FALSE
59: * otherwise.
60: *
61: * Make_Update Update all parents of a given child. Performs
62: * various bookkeeping chores like the updating
63: * of the cmtime field of the parent, filling
64: * of the IMPSRC context variable, etc. It will
65: * place the parent on the toBeMade queue if it
66: * should be.
67: *
68: * Make_TimeStamp Function to set the parent's cmtime field
69: * based on a child's modification time.
70: *
71: * Make_DoAllVar Set up the various local variables for a
72: * target, including the .ALLSRC variable, making
73: * sure that any variable that needs to exist
74: * at the very least has the empty value.
75: *
76: * Make_OODate Determine if a target is out-of-date.
77: *
1.5 millert 78: * Make_HandleUse See if a child is a .USE node for a parent
1.1 deraadt 79: * and perform the .USE actions if so.
80: */
81:
82: #include "make.h"
83: #include "hash.h"
84: #include "dir.h"
85: #include "job.h"
86:
87: static Lst toBeMade; /* The current fringe of the graph. These
88: * are nodes which await examination by
89: * MakeOODate. It is added to by
90: * Make_Update and subtracted from by
91: * MakeStartJobs */
92: static int numNodes; /* Number of nodes to be processed. If this
93: * is non-zero when Job_Empty() returns
94: * TRUE, there's a cycle in the graph */
95:
96: static int MakeAddChild __P((ClientData, ClientData));
97: static int MakeAddAllSrc __P((ClientData, ClientData));
98: static int MakeTimeStamp __P((ClientData, ClientData));
99: static int MakeHandleUse __P((ClientData, ClientData));
100: static Boolean MakeStartJobs __P((void));
101: static int MakePrintStatus __P((ClientData, ClientData));
102: /*-
103: *-----------------------------------------------------------------------
104: * Make_TimeStamp --
105: * Set the cmtime field of a parent node based on the mtime stamp in its
1.4 millert 106: * child. Called from MakeOODate via Lst_ForEach.
1.1 deraadt 107: *
108: * Results:
1.4 millert 109: * Always returns 0.
1.1 deraadt 110: *
111: * Side Effects:
112: * The cmtime of the parent node will be changed if the mtime
113: * field of the child is greater than it.
114: *-----------------------------------------------------------------------
115: */
116: int
117: Make_TimeStamp (pgn, cgn)
118: GNode *pgn; /* the current parent */
119: GNode *cgn; /* the child we've just examined */
120: {
121: if (cgn->mtime > pgn->cmtime) {
122: pgn->cmtime = cgn->mtime;
123: }
124: return (0);
125: }
126:
127: static int
128: MakeTimeStamp (pgn, cgn)
129: ClientData pgn; /* the current parent */
130: ClientData cgn; /* the child we've just examined */
131: {
1.7 ! espie 132: return Make_TimeStamp((GNode *) pgn, (GNode *) cgn);
1.1 deraadt 133: }
134:
135: /*-
136: *-----------------------------------------------------------------------
137: * Make_OODate --
138: * See if a given node is out of date with respect to its sources.
139: * Used by Make_Run when deciding which nodes to place on the
140: * toBeMade queue initially and by Make_Update to screen out USE and
141: * EXEC nodes. In the latter case, however, any other sort of node
142: * must be considered out-of-date since at least one of its children
143: * will have been recreated.
144: *
145: * Results:
1.4 millert 146: * TRUE if the node is out of date. FALSE otherwise.
1.1 deraadt 147: *
148: * Side Effects:
149: * The mtime field of the node and the cmtime field of its parents
150: * will/may be changed.
151: *-----------------------------------------------------------------------
152: */
153: Boolean
154: Make_OODate (gn)
155: register GNode *gn; /* the node to check */
156: {
157: Boolean oodate;
158:
159: /*
160: * Certain types of targets needn't even be sought as their datedness
161: * doesn't depend on their modification time...
162: */
163: if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC)) == 0) {
164: (void) Dir_MTime (gn);
165: if (DEBUG(MAKE)) {
166: if (gn->mtime != 0) {
167: printf ("modified %s...", Targ_FmtTime(gn->mtime));
168: } else {
169: printf ("non-existent...");
170: }
171: }
172: }
173:
174: /*
175: * A target is remade in one of the following circumstances:
176: * its modification time is smaller than that of its youngest child
177: * and it would actually be run (has commands or type OP_NOP)
178: * it's the object of a force operator
179: * it has no children, was on the lhs of an operator and doesn't exist
180: * already.
181: *
182: * Libraries are only considered out-of-date if the archive module says
183: * they are.
184: *
185: * These weird rules are brought to you by Backward-Compatability and
186: * the strange people who wrote 'Make'.
187: */
188: if (gn->type & OP_USE) {
189: /*
190: * If the node is a USE node it is *never* out of date
191: * no matter *what*.
192: */
193: if (DEBUG(MAKE)) {
194: printf(".USE node...");
195: }
196: oodate = FALSE;
1.4 millert 197: } else if ((gn->type & OP_LIB) && Arch_IsLib(gn)) {
1.1 deraadt 198: if (DEBUG(MAKE)) {
199: printf("library...");
200: }
201:
202: /*
203: * always out of date if no children and :: target
204: */
205:
206: oodate = Arch_LibOODate (gn) ||
207: ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
208: } else if (gn->type & OP_JOIN) {
209: /*
210: * A target with the .JOIN attribute is only considered
211: * out-of-date if any of its children was out-of-date.
212: */
213: if (DEBUG(MAKE)) {
214: printf(".JOIN node...");
215: }
216: oodate = gn->childMade;
1.2 niklas 217: } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
1.1 deraadt 218: /*
219: * A node which is the object of the force (!) operator or which has
220: * the .EXEC attribute is always considered out-of-date.
221: */
222: if (DEBUG(MAKE)) {
223: if (gn->type & OP_FORCE) {
224: printf("! operator...");
1.2 niklas 225: } else if (gn->type & OP_PHONY) {
226: printf(".PHONY node...");
1.1 deraadt 227: } else {
228: printf(".EXEC node...");
229: }
230: }
231: oodate = TRUE;
232: } else if ((gn->mtime < gn->cmtime) ||
233: ((gn->cmtime == 0) &&
234: ((gn->mtime==0) || (gn->type & OP_DOUBLEDEP))))
235: {
236: /*
237: * A node whose modification time is less than that of its
238: * youngest child or that has no children (cmtime == 0) and
239: * either doesn't exist (mtime == 0) or was the object of a
240: * :: operator is out-of-date. Why? Because that's the way Make does
241: * it.
242: */
243: if (DEBUG(MAKE)) {
244: if (gn->mtime < gn->cmtime) {
245: printf("modified before source...");
246: } else if (gn->mtime == 0) {
247: printf("non-existent and no sources...");
248: } else {
249: printf(":: operator and no sources...");
250: }
251: }
252: oodate = TRUE;
253: } else {
254: #if 0
255: /* WHY? */
256: if (DEBUG(MAKE)) {
257: printf("source %smade...", gn->childMade ? "" : "not ");
258: }
259: oodate = gn->childMade;
260: #else
261: oodate = FALSE;
262: #endif /* 0 */
263: }
264:
265: /*
266: * If the target isn't out-of-date, the parents need to know its
267: * modification time. Note that targets that appear to be out-of-date
268: * but aren't, because they have no commands and aren't of type OP_NOP,
269: * have their mtime stay below their children's mtime to keep parents from
270: * thinking they're out-of-date.
271: */
272: if (!oodate) {
273: Lst_ForEach (gn->parents, MakeTimeStamp, (ClientData)gn);
274: }
275:
276: return (oodate);
277: }
278:
279: /*-
280: *-----------------------------------------------------------------------
281: * MakeAddChild --
282: * Function used by Make_Run to add a child to the list l.
283: * It will only add the child if its make field is FALSE.
284: *
285: * Results:
286: * Always returns 0
287: *
288: * Side Effects:
289: * The given list is extended
290: *-----------------------------------------------------------------------
291: */
292: static int
293: MakeAddChild (gnp, lp)
294: ClientData gnp; /* the node to add */
295: ClientData lp; /* the list to which to add it */
296: {
297: GNode *gn = (GNode *) gnp;
298: Lst l = (Lst) lp;
1.5 millert 299:
1.1 deraadt 300: if (!gn->make && !(gn->type & OP_USE)) {
301: (void)Lst_EnQueue (l, (ClientData)gn);
302: }
303: return (0);
304: }
1.5 millert 305:
306: /*-
307: *-----------------------------------------------------------------------
1.1 deraadt 308: * Make_HandleUse --
309: * Function called by Make_Run and SuffApplyTransform on the downward
310: * pass to handle .USE and transformation nodes. A callback function
311: * for Lst_ForEach, it implements the .USE and transformation
312: * functionality by copying the node's commands, type flags
313: * and children to the parent node. Should be called before the
314: * children are enqueued to be looked at by MakeAddChild.
315: *
316: * A .USE node is much like an explicit transformation rule, except
317: * its commands are always added to the target node, even if the
318: * target already has commands.
319: *
320: * Results:
321: * returns 0.
322: *
323: * Side Effects:
324: * Children and commands may be added to the parent and the parent's
325: * type may be changed.
326: *
327: *-----------------------------------------------------------------------
328: */
329: int
330: Make_HandleUse (cgn, pgn)
331: register GNode *cgn; /* The .USE node */
332: register GNode *pgn; /* The target of the .USE node */
333: {
1.6 millert 334: register GNode *gn; /* A child of the .USE node */
1.1 deraadt 335: register LstNode ln; /* An element in the children list */
336:
337: if (cgn->type & (OP_USE|OP_TRANSFORM)) {
338: if ((cgn->type & OP_USE) || Lst_IsEmpty(pgn->commands)) {
339: /*
340: * .USE or transformation and target has no commands -- append
341: * the child's commands to the parent.
342: */
343: (void) Lst_Concat (pgn->commands, cgn->commands, LST_CONCNEW);
344: }
1.4 millert 345:
1.1 deraadt 346: if (Lst_Open (cgn->children) == SUCCESS) {
347: while ((ln = Lst_Next (cgn->children)) != NILLNODE) {
1.6 millert 348: gn = (GNode *)Lst_Datum (ln);
1.1 deraadt 349:
350: if (Lst_Member (pgn->children, gn) == NILLNODE) {
351: (void) Lst_AtEnd (pgn->children, gn);
352: (void) Lst_AtEnd (gn->parents, pgn);
353: pgn->unmade += 1;
354: }
355: }
356: Lst_Close (cgn->children);
357: }
1.4 millert 358:
1.1 deraadt 359: pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
360:
361: /*
362: * This child node is now "made", so we decrement the count of
363: * unmade children in the parent... We also remove the child
364: * from the parent's list to accurately reflect the number of decent
365: * children the parent has. This is used by Make_Run to decide
366: * whether to queue the parent or examine its children...
367: */
1.6 millert 368: if (cgn->type & OP_USE) {
1.5 millert 369: pgn->unmade--;
1.1 deraadt 370: }
371: }
372: return (0);
373: }
374: static int
375: MakeHandleUse (pgn, cgn)
376: ClientData pgn; /* the current parent */
377: ClientData cgn; /* the child we've just examined */
378: {
1.7 ! espie 379: return Make_HandleUse((GNode *) pgn, (GNode *) cgn);
1.1 deraadt 380: }
381:
382: /*-
383: *-----------------------------------------------------------------------
384: * Make_Update --
385: * Perform update on the parents of a node. Used by JobFinish once
386: * a node has been dealt with and by MakeStartJobs if it finds an
1.4 millert 387: * up-to-date node.
1.1 deraadt 388: *
389: * Results:
390: * Always returns 0
391: *
392: * Side Effects:
393: * The unmade field of pgn is decremented and pgn may be placed on
394: * the toBeMade queue if this field becomes 0.
395: *
396: * If the child was made, the parent's childMade field will be set true
397: * and its cmtime set to now.
398: *
399: * If the child wasn't made, the cmtime field of the parent will be
400: * altered if the child's mtime is big enough.
401: *
402: * Finally, if the child is the implied source for the parent, the
403: * parent's IMPSRC variable is set appropriately.
404: *
405: *-----------------------------------------------------------------------
406: */
407: void
408: Make_Update (cgn)
409: register GNode *cgn; /* the child node */
410: {
411: register GNode *pgn; /* the parent node */
412: register char *cname; /* the child's name */
413: register LstNode ln; /* Element in parents and iParents lists */
414: char *p1;
415:
416: cname = Var_Value (TARGET, cgn, &p1);
1.7 ! espie 417: efree(p1);
1.1 deraadt 418:
419: /*
420: * If the child was actually made, see what its modification time is
421: * now -- some rules won't actually update the file. If the file still
422: * doesn't exist, make its mtime now.
423: */
424: if (cgn->made != UPTODATE) {
425: #ifndef RECHECK
426: /*
427: * We can't re-stat the thing, but we can at least take care of rules
428: * where a target depends on a source that actually creates the
429: * target, but only if it has changed, e.g.
430: *
431: * parse.h : parse.o
432: *
433: * parse.o : parse.y
434: * yacc -d parse.y
435: * cc -c y.tab.c
436: * mv y.tab.o parse.o
437: * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
438: *
439: * In this case, if the definitions produced by yacc haven't changed
440: * from before, parse.h won't have been updated and cgn->mtime will
441: * reflect the current modification time for parse.h. This is
442: * something of a kludge, I admit, but it's a useful one..
443: * XXX: People like to use a rule like
444: *
445: * FRC:
446: *
447: * To force things that depend on FRC to be made, so we have to
448: * check for gn->children being empty as well...
449: */
450: if (!Lst_IsEmpty(cgn->commands) || Lst_IsEmpty(cgn->children)) {
451: cgn->mtime = now;
452: }
453: #else
454: /*
455: * This is what Make does and it's actually a good thing, as it
456: * allows rules like
457: *
458: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
459: *
460: * to function as intended. Unfortunately, thanks to the stateless
461: * nature of NFS (by which I mean the loose coupling of two clients
462: * using the same file from a common server), there are times
463: * when the modification time of a file created on a remote
464: * machine will not be modified before the local stat() implied by
465: * the Dir_MTime occurs, thus leading us to believe that the file
466: * is unchanged, wreaking havoc with files that depend on this one.
467: *
468: * I have decided it is better to make too much than to make too
469: * little, so this stuff is commented out unless you're sure it's ok.
470: * -- ardeb 1/12/88
471: */
472: /*
473: * Christos, 4/9/92: If we are saving commands pretend that
474: * the target is made now. Otherwise archives with ... rules
475: * don't work!
476: */
477: if (noExecute || (cgn->type & OP_SAVE_CMDS) || Dir_MTime(cgn) == 0) {
478: cgn->mtime = now;
479: }
480: if (DEBUG(MAKE)) {
481: printf("update time: %s\n", Targ_FmtTime(cgn->mtime));
482: }
483: #endif
484: }
1.4 millert 485:
1.1 deraadt 486: if (Lst_Open (cgn->parents) == SUCCESS) {
487: while ((ln = Lst_Next (cgn->parents)) != NILLNODE) {
488: pgn = (GNode *)Lst_Datum (ln);
489: if (pgn->make) {
490: pgn->unmade -= 1;
491:
492: if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
493: if (cgn->made == MADE) {
494: pgn->childMade = TRUE;
495: if (pgn->cmtime < cgn->mtime) {
496: pgn->cmtime = cgn->mtime;
497: }
498: } else {
499: (void)Make_TimeStamp (pgn, cgn);
500: }
501: }
502: if (pgn->unmade == 0) {
503: /*
504: * Queue the node up -- any unmade predecessors will
505: * be dealt with in MakeStartJobs.
506: */
507: (void)Lst_EnQueue (toBeMade, (ClientData)pgn);
508: } else if (pgn->unmade < 0) {
509: Error ("Graph cycles through %s", pgn->name);
510: }
511: }
512: }
513: Lst_Close (cgn->parents);
514: }
515: /*
516: * Deal with successor nodes. If any is marked for making and has an unmade
517: * count of 0, has not been made and isn't in the examination queue,
518: * it means we need to place it in the queue as it restrained itself
519: * before.
520: */
521: for (ln = Lst_First(cgn->successors); ln != NILLNODE; ln = Lst_Succ(ln)) {
522: GNode *succ = (GNode *)Lst_Datum(ln);
523:
524: if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
525: Lst_Member(toBeMade, (ClientData)succ) == NILLNODE)
526: {
527: (void)Lst_EnQueue(toBeMade, (ClientData)succ);
528: }
529: }
1.4 millert 530:
1.1 deraadt 531: /*
532: * Set the .PREFIX and .IMPSRC variables for all the implied parents
533: * of this node.
534: */
535: if (Lst_Open (cgn->iParents) == SUCCESS) {
536: char *p1;
537: char *cpref = Var_Value(PREFIX, cgn, &p1);
538:
539: while ((ln = Lst_Next (cgn->iParents)) != NILLNODE) {
540: pgn = (GNode *)Lst_Datum (ln);
541: if (pgn->make) {
542: Var_Set (IMPSRC, cname, pgn);
543: Var_Set (PREFIX, cpref, pgn);
544: }
545: }
1.7 ! espie 546: efree(p1);
1.1 deraadt 547: Lst_Close (cgn->iParents);
548: }
549: }
550:
551: /*-
552: *-----------------------------------------------------------------------
553: * MakeAddAllSrc --
554: * Add a child's name to the ALLSRC and OODATE variables of the given
555: * node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
556: * if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
557: * .EXEC and .USE children are very rarely going to be files, so...
558: * A child is added to the OODATE variable if its modification time is
559: * later than that of its parent, as defined by Make, except if the
560: * parent is a .JOIN node. In that case, it is only added to the OODATE
561: * variable if it was actually made (since .JOIN nodes don't have
562: * modification times, the comparison is rather unfair...)..
563: *
564: * Results:
565: * Always returns 0
566: *
567: * Side Effects:
568: * The ALLSRC variable for the given node is extended.
569: *-----------------------------------------------------------------------
570: */
571: static int
572: MakeAddAllSrc (cgnp, pgnp)
573: ClientData cgnp; /* The child to add */
574: ClientData pgnp; /* The parent to whose ALLSRC variable it should be */
575: /* added */
576: {
577: GNode *cgn = (GNode *) cgnp;
578: GNode *pgn = (GNode *) pgnp;
579: if ((cgn->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) == 0) {
580: char *child;
1.3 briggs 581: char *p1 = NULL;
1.1 deraadt 582:
1.5 millert 583: if (OP_NOP(cgn->type) ||
584: (child = Var_Value(TARGET, cgn, &p1)) == NULL) {
1.3 briggs 585: /*
586: * this node is only source; use the specific pathname for it
587: */
588: child = cgn->path ? cgn->path : cgn->name;
589: }
1.5 millert 590:
1.1 deraadt 591: Var_Append (ALLSRC, child, pgn);
592: if (pgn->type & OP_JOIN) {
593: if (cgn->made == MADE) {
594: Var_Append(OODATE, child, pgn);
595: }
596: } else if ((pgn->mtime < cgn->mtime) ||
597: (cgn->mtime >= now && cgn->made == MADE))
598: {
599: /*
600: * It goes in the OODATE variable if the parent is younger than the
601: * child or if the child has been modified more recently than
602: * the start of the make. This is to keep pmake from getting
603: * confused if something else updates the parent after the
604: * make starts (shouldn't happen, I know, but sometimes it
605: * does). In such a case, if we've updated the kid, the parent
606: * is likely to have a modification time later than that of
607: * the kid and anything that relies on the OODATE variable will
608: * be hosed.
609: *
610: * XXX: This will cause all made children to go in the OODATE
611: * variable, even if they're not touched, if RECHECK isn't defined,
612: * since cgn->mtime is set to now in Make_Update. According to
613: * some people, this is good...
614: */
615: Var_Append(OODATE, child, pgn);
616: }
1.7 ! espie 617: efree(p1);
1.1 deraadt 618: }
619: return (0);
620: }
621:
622: /*-
623: *-----------------------------------------------------------------------
624: * Make_DoAllVar --
625: * Set up the ALLSRC and OODATE variables. Sad to say, it must be
626: * done separately, rather than while traversing the graph. This is
627: * because Make defined OODATE to contain all sources whose modification
628: * times were later than that of the target, *not* those sources that
629: * were out-of-date. Since in both compatibility and native modes,
630: * the modification time of the parent isn't found until the child
631: * has been dealt with, we have to wait until now to fill in the
632: * variable. As for ALLSRC, the ordering is important and not
633: * guaranteed when in native mode, so it must be set here, too.
634: *
635: * Results:
636: * None
637: *
638: * Side Effects:
639: * The ALLSRC and OODATE variables of the given node is filled in.
640: * If the node is a .JOIN node, its TARGET variable will be set to
641: * match its ALLSRC variable.
642: *-----------------------------------------------------------------------
643: */
644: void
645: Make_DoAllVar (gn)
646: GNode *gn;
647: {
648: Lst_ForEach (gn->children, MakeAddAllSrc, (ClientData) gn);
649:
650: if (!Var_Exists (OODATE, gn)) {
651: Var_Set (OODATE, "", gn);
652: }
653: if (!Var_Exists (ALLSRC, gn)) {
654: Var_Set (ALLSRC, "", gn);
655: }
656:
657: if (gn->type & OP_JOIN) {
658: char *p1;
659: Var_Set (TARGET, Var_Value (ALLSRC, gn, &p1), gn);
1.7 ! espie 660: efree(p1);
1.1 deraadt 661: }
662: }
663:
664: /*-
665: *-----------------------------------------------------------------------
666: * MakeStartJobs --
667: * Start as many jobs as possible.
668: *
669: * Results:
670: * If the query flag was given to pmake, no job will be started,
671: * but as soon as an out-of-date target is found, this function
672: * returns TRUE. At all other times, this function returns FALSE.
673: *
674: * Side Effects:
675: * Nodes are removed from the toBeMade queue and job table slots
676: * are filled.
677: *
678: *-----------------------------------------------------------------------
679: */
680: static Boolean
681: MakeStartJobs ()
682: {
683: register GNode *gn;
1.4 millert 684:
1.1 deraadt 685: while (!Job_Full() && !Lst_IsEmpty (toBeMade)) {
686: gn = (GNode *) Lst_DeQueue (toBeMade);
687: if (DEBUG(MAKE)) {
688: printf ("Examining %s...", gn->name);
689: }
690: /*
691: * Make sure any and all predecessors that are going to be made,
692: * have been.
693: */
694: if (!Lst_IsEmpty(gn->preds)) {
695: LstNode ln;
696:
697: for (ln = Lst_First(gn->preds); ln != NILLNODE; ln = Lst_Succ(ln)){
698: GNode *pgn = (GNode *)Lst_Datum(ln);
699:
700: if (pgn->make && pgn->made == UNMADE) {
701: if (DEBUG(MAKE)) {
702: printf("predecessor %s not made yet.\n", pgn->name);
703: }
704: break;
705: }
706: }
707: /*
708: * If ln isn't nil, there's a predecessor as yet unmade, so we
709: * just drop this node on the floor. When the node in question
710: * has been made, it will notice this node as being ready to
711: * make but as yet unmade and will place the node on the queue.
712: */
713: if (ln != NILLNODE) {
714: continue;
715: }
716: }
1.4 millert 717:
1.1 deraadt 718: numNodes--;
719: if (Make_OODate (gn)) {
720: if (DEBUG(MAKE)) {
721: printf ("out-of-date\n");
722: }
723: if (queryFlag) {
724: return (TRUE);
725: }
726: Make_DoAllVar (gn);
727: Job_Make (gn);
728: } else {
729: if (DEBUG(MAKE)) {
730: printf ("up-to-date\n");
731: }
732: gn->made = UPTODATE;
733: if (gn->type & OP_JOIN) {
734: /*
735: * Even for an up-to-date .JOIN node, we need it to have its
736: * context variables so references to it get the correct
737: * value for .TARGET when building up the context variables
738: * of its parent(s)...
739: */
740: Make_DoAllVar (gn);
741: }
1.4 millert 742:
1.1 deraadt 743: Make_Update (gn);
744: }
745: }
746: return (FALSE);
747: }
748:
749: /*-
750: *-----------------------------------------------------------------------
751: * MakePrintStatus --
752: * Print the status of a top-level node, viz. it being up-to-date
753: * already or not created due to an error in a lower level.
754: * Callback function for Make_Run via Lst_ForEach.
755: *
756: * Results:
757: * Always returns 0.
758: *
759: * Side Effects:
760: * A message may be printed.
761: *
762: *-----------------------------------------------------------------------
763: */
764: static int
765: MakePrintStatus(gnp, cyclep)
766: ClientData gnp; /* Node to examine */
767: ClientData cyclep; /* True if gn->unmade being non-zero implies
768: * a cycle in the graph, not an error in an
769: * inferior */
770: {
771: GNode *gn = (GNode *) gnp;
772: Boolean cycle = *(Boolean *) cyclep;
773: if (gn->made == UPTODATE) {
774: printf ("`%s' is up to date.\n", gn->name);
775: } else if (gn->unmade != 0) {
776: if (cycle) {
777: Boolean t = TRUE;
778: /*
779: * If printing cycles and came to one that has unmade children,
780: * print out the cycle by recursing on its children. Note a
781: * cycle like:
782: * a : b
783: * b : c
784: * c : b
785: * will cause this to erroneously complain about a being in
786: * the cycle, but this is a good approximation.
787: */
788: if (gn->made == CYCLE) {
789: Error("Graph cycles through `%s'", gn->name);
790: gn->made = ENDCYCLE;
791: Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
792: gn->made = UNMADE;
793: } else if (gn->made != ENDCYCLE) {
794: gn->made = CYCLE;
795: Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
796: }
797: } else {
798: printf ("`%s' not remade because of errors.\n", gn->name);
799: }
800: }
801: return (0);
802: }
803:
1.5 millert 804:
1.1 deraadt 805: /*-
806: *-----------------------------------------------------------------------
1.6 millert 807: * Make_Run --
808: * Initialize the nodes to remake and the list of nodes which are
809: * ready to be made by doing a breadth-first traversal of the graph
810: * starting from the nodes in the given list. Once this traversal
811: * is finished, all the 'leaves' of the graph are in the toBeMade
812: * queue.
813: * Using this queue and the Job module, work back up the graph,
814: * calling on MakeStartJobs to keep the job table as full as
815: * possible.
816: *
1.1 deraadt 817: * Results:
1.6 millert 818: * TRUE if work was done. FALSE otherwise.
1.1 deraadt 819: *
820: * Side Effects:
1.6 millert 821: * The make field of all nodes involved in the creation of the given
822: * targets is set to 1. The toBeMade list is set to contain all the
823: * 'leaves' of these subgraphs.
1.1 deraadt 824: *-----------------------------------------------------------------------
825: */
1.6 millert 826: Boolean
827: Make_Run (targs)
1.1 deraadt 828: Lst targs; /* the initial list of targets */
829: {
830: register GNode *gn; /* a temporary pointer */
831: register Lst examine; /* List of targets to examine */
1.6 millert 832: int errors; /* Number of errors the Job module reports */
1.1 deraadt 833:
1.6 millert 834: toBeMade = Lst_Init (FALSE);
1.1 deraadt 835:
836: examine = Lst_Duplicate(targs, NOCOPY);
837: numNodes = 0;
1.4 millert 838:
1.1 deraadt 839: /*
840: * Make an initial downward pass over the graph, marking nodes to be made
841: * as we go down. We call Suff_FindDeps to find where a node is and
842: * to get some children for it if it has none and also has no commands.
843: * If the node is a leaf, we stick it on the toBeMade queue to
844: * be looked at in a minute, otherwise we add its children to our queue
1.4 millert 845: * and go on about our business.
1.1 deraadt 846: */
847: while (!Lst_IsEmpty (examine)) {
848: gn = (GNode *) Lst_DeQueue (examine);
1.4 millert 849:
1.1 deraadt 850: if (!gn->make) {
851: gn->make = TRUE;
852: numNodes++;
1.4 millert 853:
1.1 deraadt 854: /*
855: * Apply any .USE rules before looking for implicit dependencies
856: * to make sure everything has commands that should...
857: */
858: Lst_ForEach (gn->children, MakeHandleUse, (ClientData)gn);
859: Suff_FindDeps (gn);
860:
1.6 millert 861: if (gn->unmade != 0) {
1.1 deraadt 862: Lst_ForEach (gn->children, MakeAddChild, (ClientData)examine);
863: } else {
1.6 millert 864: (void)Lst_EnQueue (toBeMade, (ClientData)gn);
1.1 deraadt 865: }
866: }
867: }
1.4 millert 868:
1.1 deraadt 869: Lst_Destroy (examine, NOFREE);
870:
871: if (queryFlag) {
872: /*
873: * We wouldn't do any work unless we could start some jobs in the
874: * next loop... (we won't actually start any, of course, this is just
875: * to see if any of the targets was out of date)
876: */
877: return (MakeStartJobs());
878: } else {
879: /*
880: * Initialization. At the moment, no jobs are running and until some
881: * get started, nothing will happen since the remaining upward
882: * traversal of the graph is performed by the routines in job.c upon
883: * the finishing of a job. So we fill the Job table as much as we can
1.4 millert 884: * before going into our loop.
1.1 deraadt 885: */
886: (void) MakeStartJobs();
887: }
888:
889: /*
890: * Main Loop: The idea here is that the ending of jobs will take
891: * care of the maintenance of data structures and the waiting for output
892: * will cause us to be idle most of the time while our children run as
893: * much as possible. Because the job table is kept as full as possible,
894: * the only time when it will be empty is when all the jobs which need
895: * running have been run, so that is the end condition of this loop.
896: * Note that the Job module will exit if there were any errors unless the
897: * keepgoing flag was given.
898: */
899: while (!Job_Empty ()) {
900: Job_CatchOutput ();
901: Job_CatchChildren (!usePipes);
902: (void)MakeStartJobs();
903: }
904:
1.7 ! espie 905: errors = Job_Finish();
1.1 deraadt 906:
907: /*
908: * Print the final status of each target. E.g. if it wasn't made
909: * because some inferior reported an error.
910: */
911: errors = ((errors == 0) && (numNodes != 0));
912: Lst_ForEach(targs, MakePrintStatus, (ClientData) &errors);
1.4 millert 913:
1.1 deraadt 914: return (TRUE);
915: }