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