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