Annotation of src/usr.bin/make/make.c, Revision 1.202
1.202 ! rillig 1: /* $NetBSD: make.c,v 1.201 2020/11/08 11:25:26 rillig Exp $ */
1.7 christos 2:
1.1 cgd 3: /*
1.10 christos 4: * Copyright (c) 1988, 1989, 1990, 1993
5: * The Regents of the University of California. All rights reserved.
1.51 agc 6: *
7: * This code is derived from software contributed to Berkeley by
8: * Adam de Boor.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. Neither the name of the University nor the names of its contributors
19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
34:
35: /*
1.1 cgd 36: * Copyright (c) 1989 by Berkeley Softworks
37: * All rights reserved.
38: *
39: * This code is derived from software contributed to Berkeley by
40: * Adam de Boor.
41: *
42: * Redistribution and use in source and binary forms, with or without
43: * modification, are permitted provided that the following conditions
44: * are met:
45: * 1. Redistributions of source code must retain the above copyright
46: * notice, this list of conditions and the following disclaimer.
47: * 2. Redistributions in binary form must reproduce the above copyright
48: * notice, this list of conditions and the following disclaimer in the
49: * documentation and/or other materials provided with the distribution.
50: * 3. All advertising materials mentioning features or use of this software
51: * must display the following acknowledgement:
52: * This product includes software developed by the University of
53: * California, Berkeley and its contributors.
54: * 4. Neither the name of the University nor the names of its contributors
55: * may be used to endorse or promote products derived from this software
56: * without specific prior written permission.
57: *
58: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68: * SUCH DAMAGE.
69: */
70:
71: /*-
72: * make.c --
73: * The functions which perform the examination of targets and
74: * their suitability for creation
75: *
76: * Interface:
1.151 rillig 77: * Make_Run Initialize things for the module and recreate
78: * whatever needs recreating. Returns TRUE if
79: * work was (or would have been) done and FALSE
80: * otherwise.
81: *
82: * Make_Update Update all parents of a given child. Performs
83: * various bookkeeping chores like the updating
1.178 rillig 84: * of the youngestChild field of the parent, filling
1.151 rillig 85: * of the IMPSRC context variable, etc. It will
86: * place the parent on the toBeMade queue if it
87: * should be.
88: *
1.189 rillig 89: * GNode_UpdateYoungestChild
90: * Update the node's youngestChild field based on the
91: * child's modification time.
1.151 rillig 92: *
93: * Make_DoAllVar Set up the various local variables for a
94: * target, including the .ALLSRC variable, making
95: * sure that any variable that needs to exist
96: * at the very least has the empty value.
1.1 cgd 97: *
1.190 rillig 98: * GNode_IsOODate Determine if a target is out-of-date.
1.1 cgd 99: *
1.151 rillig 100: * Make_HandleUse See if a child is a .USE node for a parent
101: * and perform the .USE actions if so.
1.11 christos 102: *
1.151 rillig 103: * Make_ExpandUse Expand .USE nodes
1.1 cgd 104: */
105:
1.185 rillig 106: #include "make.h"
107: #include "dir.h"
108: #include "job.h"
1.1 cgd 109:
1.136 rillig 110: /* "@(#)make.c 8.1 (Berkeley) 6/6/93" */
1.202 ! rillig 111: MAKE_RCSID("$NetBSD: make.c,v 1.201 2020/11/08 11:25:26 rillig Exp $");
1.136 rillig 112:
1.149 rillig 113: /* Sequence # to detect recursion. */
114: static unsigned int checked = 1;
115:
116: /* The current fringe of the graph.
117: * These are nodes which await examination by MakeOODate.
118: * It is added to by Make_Update and subtracted from by MakeStartJobs */
119: static GNodeList *toBeMade;
1.1 cgd 120:
1.148 rillig 121: static int MakeBuildParent(void *, void *);
1.68 dsl 122:
1.154 rillig 123: void
124: debug_printf(const char *fmt, ...)
125: {
126: va_list args;
127:
128: va_start(args, fmt);
1.181 rillig 129: vfprintf(opts.debug_file, fmt, args);
1.154 rillig 130: va_end(args);
131: }
132:
1.87 joerg 133: MAKE_ATTR_DEAD static void
1.68 dsl 134: make_abort(GNode *gn, int line)
135: {
1.153 rillig 136: debug_printf("make_abort from line %d\n", line);
1.135 rillig 137: Targ_PrintNode(gn, 2);
138: Targ_PrintNodes(toBeMade, 2);
1.68 dsl 139: Targ_PrintGraph(3);
140: abort();
141: }
142:
1.123 rillig 143: ENUM_VALUE_RTTI_8(GNodeMade,
144: UNMADE, DEFERRED, REQUESTED, BEINGMADE,
145: MADE, UPTODATE, ERROR, ABORTED);
146:
147: ENUM_FLAGS_RTTI_31(GNodeType,
148: OP_DEPENDS, OP_FORCE, OP_DOUBLEDEP,
149: /* OP_OPMASK is omitted since it combines other flags */
150: OP_OPTIONAL, OP_USE, OP_EXEC, OP_IGNORE,
151: OP_PRECIOUS, OP_SILENT, OP_MAKE, OP_JOIN,
152: OP_MADE, OP_SPECIAL, OP_USEBEFORE, OP_INVISIBLE,
153: OP_NOTMAIN, OP_PHONY, OP_NOPATH, OP_WAIT,
154: OP_NOMETA, OP_META, OP_NOMETA_CMP, OP_SUBMAKE,
155: OP_TRANSFORM, OP_MEMBER, OP_LIB, OP_ARCHV,
156: OP_HAS_COMMANDS, OP_SAVE_CMDS, OP_DEPS_FOUND, OP_MARK);
157:
158: ENUM_FLAGS_RTTI_10(GNodeFlags,
159: REMAKE, CHILDMADE, FORCE, DONE_WAIT,
160: DONE_ORDER, FROM_DEPEND, DONE_ALLSRC, CYCLE,
161: DONECYCLE, INTERNAL);
1.122 rillig 162:
163: void
164: GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
165: const char *suffix)
166: {
167: char type_buf[GNodeType_ToStringSize];
168: char flags_buf[GNodeFlags_ToStringSize];
169:
170: fprintf(f, "%smade %s, type %s, flags %s%s",
171: prefix,
172: Enum_ValueToString(gn->made, GNodeMade_ToStringSpecs),
173: Enum_FlagsToString(type_buf, sizeof type_buf,
174: gn->type, GNodeType_ToStringSpecs),
175: Enum_FlagsToString(flags_buf, sizeof flags_buf,
176: gn->flags, GNodeFlags_ToStringSpecs),
177: suffix);
178: }
179:
1.159 rillig 180: Boolean
1.186 rillig 181: GNode_ShouldExecute(GNode *gn)
1.159 rillig 182: {
1.186 rillig 183: return !((gn->type & OP_MAKE) ? opts.noRecursiveExecute : opts.noExecute);
1.159 rillig 184: }
185:
1.149 rillig 186: /* Update the youngest child of the node, according to the given child. */
1.143 rillig 187: void
1.189 rillig 188: GNode_UpdateYoungestChild(GNode *gn, GNode *cgn)
1.1 cgd 189: {
1.189 rillig 190: if (gn->youngestChild == NULL || cgn->mtime > gn->youngestChild->mtime)
191: gn->youngestChild = cgn;
1.1 cgd 192: }
1.5 jtc 193:
1.195 rillig 194: static Boolean
1.197 rillig 195: IsOODateRegular(GNode *gn)
1.195 rillig 196: {
1.197 rillig 197: /* These rules are inherited from the original Make. */
198:
199: if (gn->youngestChild != NULL) {
200: if (gn->mtime < gn->youngestChild->mtime) {
201: DEBUG1(MAKE, "modified before source \"%s\"...",
202: GNode_Path(gn->youngestChild));
203: return TRUE;
204: }
205: return FALSE;
206: }
207:
208: if (gn->mtime == 0 && !(gn->type & OP_OPTIONAL)) {
209: DEBUG0(MAKE, "non-existent and no sources...");
210: return TRUE;
211: }
212:
213: if (gn->type & OP_DOUBLEDEP) {
214: DEBUG0(MAKE, ":: operator and no sources...");
215: return TRUE;
216: }
217:
218: return FALSE;
1.195 rillig 219: }
220:
1.149 rillig 221: /* See if the node is out of date with respect to its sources.
1.1 cgd 222: *
1.149 rillig 223: * Used by Make_Run when deciding which nodes to place on the
224: * toBeMade queue initially and by Make_Update to screen out .USE and
225: * .EXEC nodes. In the latter case, however, any other sort of node
226: * must be considered out-of-date since at least one of its children
227: * will have been recreated.
1.1 cgd 228: *
1.178 rillig 229: * The mtime field of the node and the youngestChild field of its parents
1.149 rillig 230: * may be changed.
1.1 cgd 231: */
232: Boolean
1.190 rillig 233: GNode_IsOODate(GNode *gn)
1.1 cgd 234: {
235: Boolean oodate;
236:
237: /*
238: * Certain types of targets needn't even be sought as their datedness
239: * doesn't depend on their modification time...
240: */
1.187 rillig 241: if (!(gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
1.194 rillig 242: Dir_UpdateMTime(gn, TRUE);
1.1 cgd 243: if (DEBUG(MAKE)) {
1.188 rillig 244: if (gn->mtime != 0)
1.153 rillig 245: debug_printf("modified %s...", Targ_FmtTime(gn->mtime));
1.188 rillig 246: else
1.153 rillig 247: debug_printf("non-existent...");
1.1 cgd 248: }
249: }
250:
251: /*
252: * A target is remade in one of the following circumstances:
1.177 rillig 253: * its modification time is smaller than that of its youngest child and
254: * it would actually be run (has commands or is not GNode_IsTarget)
1.1 cgd 255: * it's the object of a force operator
256: * it has no children, was on the lhs of an operator and doesn't exist
257: * already.
258: *
259: * Libraries are only considered out-of-date if the archive module says
260: * they are.
261: *
1.37 wiz 262: * These weird rules are brought to you by Backward-Compatibility and
1.1 cgd 263: * the strange people who wrote 'Make'.
264: */
1.39 christos 265: if (gn->type & (OP_USE|OP_USEBEFORE)) {
1.1 cgd 266: /*
267: * If the node is a USE node it is *never* out of date
268: * no matter *what*.
269: */
1.152 rillig 270: DEBUG0(MAKE, ".USE node...");
1.1 cgd 271: oodate = FALSE;
1.188 rillig 272: } else if ((gn->type & OP_LIB) && (gn->mtime == 0 || Arch_IsLib(gn))) {
1.152 rillig 273: DEBUG0(MAKE, "library...");
1.6 christos 274:
275: /*
276: * always out of date if no children and :: target
1.26 sjg 277: * or non-existent.
1.6 christos 278: */
1.99 rillig 279: oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
1.178 rillig 280: (gn->youngestChild == NULL && (gn->type & OP_DOUBLEDEP)));
1.1 cgd 281: } else if (gn->type & OP_JOIN) {
282: /*
283: * A target with the .JOIN attribute is only considered
284: * out-of-date if any of its children was out-of-date.
285: */
1.152 rillig 286: DEBUG0(MAKE, ".JOIN node...");
287: DEBUG1(MAKE, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
1.52 dsl 288: oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
1.8 christos 289: } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
1.1 cgd 290: /*
291: * A node which is the object of the force (!) operator or which has
292: * the .EXEC attribute is always considered out-of-date.
293: */
294: if (DEBUG(MAKE)) {
295: if (gn->type & OP_FORCE) {
1.153 rillig 296: debug_printf("! operator...");
1.8 christos 297: } else if (gn->type & OP_PHONY) {
1.153 rillig 298: debug_printf(".PHONY node...");
1.1 cgd 299: } else {
1.153 rillig 300: debug_printf(".EXEC node...");
1.1 cgd 301: }
302: }
303: oodate = TRUE;
1.197 rillig 304: } else if (IsOODateRegular(gn)) {
1.1 cgd 305: oodate = TRUE;
306: } else {
1.99 rillig 307: /*
1.22 christos 308: * When a non-existing child with no sources
1.21 christos 309: * (such as a typically used FORCE source) has been made and
310: * the target of the child (usually a directory) has the same
311: * timestamp as the timestamp just given to the non-existing child
312: * after it was considered made.
313: */
1.1 cgd 314: if (DEBUG(MAKE)) {
1.22 christos 315: if (gn->flags & FORCE)
1.153 rillig 316: debug_printf("non existing child...");
1.1 cgd 317: }
1.52 dsl 318: oodate = (gn->flags & FORCE) ? TRUE : FALSE;
1.1 cgd 319: }
320:
1.82 sjg 321: #ifdef USE_META
322: if (useMeta) {
323: oodate = meta_oodate(gn, oodate);
324: }
325: #endif
326:
1.1 cgd 327: /*
328: * If the target isn't out-of-date, the parents need to know its
329: * modification time. Note that targets that appear to be out-of-date
1.177 rillig 330: * but aren't, because they have no commands and are GNode_IsTarget,
1.1 cgd 331: * have their mtime stay below their children's mtime to keep parents from
332: * thinking they're out-of-date.
333: */
334: if (!oodate) {
1.163 rillig 335: GNodeListNode *ln;
336: for (ln = gn->parents->first; ln != NULL; ln = ln->next)
1.189 rillig 337: GNode_UpdateYoungestChild(ln->datum, gn);
1.1 cgd 338: }
339:
1.98 rillig 340: return oodate;
1.1 cgd 341: }
1.108 rillig 342:
1.199 rillig 343: static void
344: PretendAllChildrenAreMade(GNode *pgn)
1.13 christos 345: {
1.199 rillig 346: GNodeListNode *ln;
1.14 christos 347:
1.199 rillig 348: for (ln = pgn->children->first; ln != NULL; ln = ln->next) {
349: GNode *cgn = ln->datum;
1.148 rillig 350:
1.199 rillig 351: Dir_UpdateMTime(cgn, FALSE); /* cgn->path may get updated as well */
352: GNode_UpdateYoungestChild(pgn, cgn);
353: pgn->unmade--;
354: }
1.198 rillig 355: }
356:
1.109 rillig 357: /* Called by Make_Run and SuffApplyTransform on the downward pass to handle
358: * .USE and transformation nodes, by copying the child node's commands, type
359: * flags and children to the parent node.
360: *
361: * A .USE node is much like an explicit transformation rule, except its
362: * commands are always added to the target node, even if the target already
363: * has commands.
1.1 cgd 364: *
1.50 wiz 365: * Input:
1.161 rillig 366: * cgn The source node, which is either a .USE/.USEBEFORE
367: * node or a transformation node (OP_TRANSFORM).
368: * pgn The target node
1.1 cgd 369: */
1.43 pk 370: void
1.50 wiz 371: Make_HandleUse(GNode *cgn, GNode *pgn)
1.1 cgd 372: {
1.151 rillig 373: GNodeListNode *ln; /* An element in the children list */
1.44 pk 374:
375: #ifdef DEBUG_SRC
1.187 rillig 376: if (!(cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM))) {
1.153 rillig 377: debug_printf("Make_HandleUse: called for plain node %s\n", cgn->name);
1.187 rillig 378: return; /* XXX: debug mode should not affect control flow */
1.44 pk 379: }
380: #endif
1.1 cgd 381:
1.129 rillig 382: if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
1.39 christos 383: if (cgn->type & OP_USEBEFORE) {
1.112 rillig 384: /* .USEBEFORE */
1.129 rillig 385: Lst_PrependAll(pgn->commands, cgn->commands);
1.39 christos 386: } else {
1.112 rillig 387: /* .USE, or target has no commands */
1.129 rillig 388: Lst_AppendAll(pgn->commands, cgn->commands);
1.39 christos 389: }
1.44 pk 390: }
1.10 christos 391:
1.161 rillig 392: for (ln = cgn->children->first; ln != NULL; ln = ln->next) {
393: GNode *gn = ln->datum;
1.11 christos 394:
1.119 rillig 395: /*
396: * Expand variables in the .USE node's name
397: * and save the unexpanded form.
398: * We don't need to do this for commands.
399: * They get expanded properly when we execute.
400: */
401: if (gn->uname == NULL) {
402: gn->uname = gn->name;
403: } else {
404: free(gn->name);
405: }
1.138 rillig 406: (void)Var_Subst(gn->uname, pgn, VARE_WANTRES, &gn->name);
407: /* TODO: handle errors */
1.130 rillig 408: if (gn->uname && strcmp(gn->name, gn->uname) != 0) {
1.119 rillig 409: /* See if we have a target for this node. */
1.145 rillig 410: GNode *tgn = Targ_FindNode(gn->name);
1.119 rillig 411: if (tgn != NULL)
412: gn = tgn;
413: }
1.1 cgd 414:
1.129 rillig 415: Lst_Append(pgn->children, gn);
416: Lst_Append(gn->parents, pgn);
1.156 rillig 417: pgn->unmade++;
1.44 pk 418: }
1.10 christos 419:
1.90 joerg 420: pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
1.1 cgd 421: }
1.31 mycroft 422:
1.149 rillig 423: /* Used by Make_Run on the downward pass to handle .USE nodes. Should be
424: * called before the children are enqueued to be looked at by MakeAddChild.
425: *
1.150 rillig 426: * For a .USE child, the commands, type flags and children are copied to the
1.149 rillig 427: * parent node, and since the relation to the .USE node is then no longer
428: * needed, that relation is removed.
1.44 pk 429: *
1.50 wiz 430: * Input:
1.150 rillig 431: * cgn the child, which may be a .USE node
432: * pgn the current parent
1.44 pk 433: */
1.150 rillig 434: static void
435: MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
1.5 jtc 436: {
1.150 rillig 437: Boolean unmarked;
1.44 pk 438:
1.187 rillig 439: unmarked = !(cgn->type & OP_MARK);
1.44 pk 440: cgn->type |= OP_MARK;
1.31 mycroft 441:
1.187 rillig 442: if (!(cgn->type & (OP_USE|OP_USEBEFORE)))
1.150 rillig 443: return;
1.44 pk 444:
445: if (unmarked)
1.43 pk 446: Make_HandleUse(cgn, pgn);
1.31 mycroft 447:
1.43 pk 448: /*
449: * This child node is now "made", so we decrement the count of
450: * unmade children in the parent... We also remove the child
451: * from the parent's list to accurately reflect the number of decent
452: * children the parent has. This is used by Make_Run to decide
453: * whether to queue the parent or examine its children...
454: */
1.150 rillig 455: Lst_Remove(pgn->children, ln);
456: pgn->unmade--;
457: }
458:
459: static void
460: HandleUseNodes(GNode *gn)
461: {
462: GNodeListNode *ln, *nln;
463: for (ln = gn->children->first; ln != NULL; ln = nln) {
464: nln = ln->next;
1.158 rillig 465: MakeHandleUse(ln->datum, gn, ln);
1.43 pk 466: }
1.5 jtc 467: }
1.22 christos 468:
469:
1.149 rillig 470: /* Check the modification time of a gnode, and update it if necessary.
471: * Return 0 if the gnode does not exist, or its filesystem time if it does. */
1.22 christos 472: time_t
1.50 wiz 473: Make_Recheck(GNode *gn)
1.22 christos 474: {
1.194 rillig 475: time_t mtime;
476:
477: Dir_UpdateMTime(gn, TRUE);
478: mtime = gn->mtime;
1.22 christos 479:
480: #ifndef RECHECK
481: /*
482: * We can't re-stat the thing, but we can at least take care of rules
483: * where a target depends on a source that actually creates the
484: * target, but only if it has changed, e.g.
485: *
486: * parse.h : parse.o
487: *
488: * parse.o : parse.y
1.151 rillig 489: * yacc -d parse.y
490: * cc -c y.tab.c
491: * mv y.tab.o parse.o
492: * cmp -s y.tab.h parse.h || mv y.tab.h parse.h
1.22 christos 493: *
494: * In this case, if the definitions produced by yacc haven't changed
495: * from before, parse.h won't have been updated and gn->mtime will
496: * reflect the current modification time for parse.h. This is
1.192 rillig 497: * something of a kludge, I admit, but it's a useful one.
1.22 christos 498: *
1.192 rillig 499: * XXX: People like to use a rule like "FRC:" to force things that
500: * depend on FRC to be made, so we have to check for gn->children
501: * being empty as well.
1.22 christos 502: */
1.129 rillig 503: if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
1.22 christos 504: gn->mtime = now;
505: }
506: #else
507: /*
508: * This is what Make does and it's actually a good thing, as it
509: * allows rules like
510: *
511: * cmp -s y.tab.h parse.h || cp y.tab.h parse.h
512: *
513: * to function as intended. Unfortunately, thanks to the stateless
514: * nature of NFS (by which I mean the loose coupling of two clients
515: * using the same file from a common server), there are times
516: * when the modification time of a file created on a remote
517: * machine will not be modified before the local stat() implied by
1.194 rillig 518: * the Dir_UpdateMTime occurs, thus leading us to believe that the file
1.22 christos 519: * is unchanged, wreaking havoc with files that depend on this one.
520: *
521: * I have decided it is better to make too much than to make too
522: * little, so this stuff is commented out unless you're sure it's ok.
523: * -- ardeb 1/12/88
524: */
525: /*
1.192 rillig 526: * Christos, 4/9/92: If we are saving commands, pretend that
527: * the target is made now. Otherwise archives with '...' rules
1.22 christos 528: * don't work!
529: */
1.186 rillig 530: if (!GNode_ShouldExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
1.73 dsl 531: (mtime == 0 && !(gn->type & OP_WAIT))) {
1.152 rillig 532: DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
533: gn->name, Targ_FmtTime(gn->mtime));
1.22 christos 534: gn->mtime = now;
1.191 rillig 535: } else {
1.152 rillig 536: DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
537: gn->name, Targ_FmtTime(gn->mtime));
1.22 christos 538: }
539: #endif
1.193 rillig 540:
541: /* XXX: The returned mtime may differ from gn->mtime.
542: * Intentionally? */
1.22 christos 543: return mtime;
544: }
545:
1.169 rillig 546: /*
547: * Set the .PREFIX and .IMPSRC variables for all the implied parents
548: * of this node.
549: */
550: static void
551: UpdateImplicitParentsVars(GNode *cgn, const char *cname)
552: {
553: GNodeListNode *ln;
1.184 rillig 554: const char *cpref = GNode_VarPrefix(cgn);
1.169 rillig 555:
556: for (ln = cgn->implicitParents->first; ln != NULL; ln = ln->next) {
557: GNode *pgn = ln->datum;
558: if (pgn->flags & REMAKE) {
559: Var_Set(IMPSRC, cname, pgn);
560: if (cpref != NULL)
561: Var_Set(PREFIX, cpref, pgn);
562: }
563: }
564: }
565:
1.201 rillig 566: /* See if a .ORDER rule stops us from building this node. */
567: static Boolean
568: IsWaitingForOrder(GNode *gn)
569: {
570: GNodeListNode *ln;
571:
572: for (ln = gn->order_pred->first; ln != NULL; ln = ln->next) {
573: GNode *ogn = ln->datum;
574:
575: if (ogn->made >= MADE || !(ogn->flags & REMAKE))
576: continue;
577:
578: DEBUG2(MAKE, "IsWaitingForOrder: Waiting for .ORDER node \"%s%s\"\n",
579: ogn->name, ogn->cohort_num);
580: return TRUE;
581: }
582: return FALSE;
583: }
584:
1.149 rillig 585: /* Perform update on the parents of a node. Used by JobFinish once
586: * a node has been dealt with and by MakeStartJobs if it finds an
587: * up-to-date node.
1.1 cgd 588: *
1.149 rillig 589: * The unmade field of pgn is decremented and pgn may be placed on
590: * the toBeMade queue if this field becomes 0.
1.22 christos 591: *
1.149 rillig 592: * If the child was made, the parent's flag CHILDMADE field will be
593: * set true.
1.1 cgd 594: *
1.149 rillig 595: * If the child is not up-to-date and still does not exist,
596: * set the FORCE flag on the parents.
1.1 cgd 597: *
1.178 rillig 598: * If the child wasn't made, the youngestChild field of the parent will be
1.149 rillig 599: * altered if the child's mtime is big enough.
1.1 cgd 600: *
1.149 rillig 601: * Finally, if the child is the implied source for the parent, the
602: * parent's IMPSRC variable is set appropriately.
1.1 cgd 603: */
604: void
1.50 wiz 605: Make_Update(GNode *cgn)
1.1 cgd 606: {
1.151 rillig 607: const char *cname; /* the child's name */
1.44 pk 608: time_t mtime = -1;
1.137 rillig 609: GNodeList *parents;
1.170 rillig 610: GNodeListNode *ln;
1.47 pk 611: GNode *centurion;
1.1 cgd 612:
1.70 dsl 613: /* It is save to re-examine any nodes again */
614: checked++;
615:
1.184 rillig 616: cname = GNode_VarTarget(cgn);
1.1 cgd 617:
1.152 rillig 618: DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
1.68 dsl 619:
1.1 cgd 620: /*
621: * If the child was actually made, see what its modification time is
622: * now -- some rules won't actually update the file. If the file still
623: * doesn't exist, make its mtime now.
624: */
625: if (cgn->made != UPTODATE) {
1.22 christos 626: mtime = Make_Recheck(cgn);
1.1 cgd 627: }
1.10 christos 628:
1.47 pk 629: /*
630: * If this is a `::' node, we must consult its first instance
631: * which is where all parents are linked.
632: */
633: if ((centurion = cgn->centurion) != NULL) {
1.129 rillig 634: if (!Lst_IsEmpty(cgn->parents))
1.68 dsl 635: Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
1.156 rillig 636: centurion->unmade_cohorts--;
1.47 pk 637: if (centurion->unmade_cohorts < 0)
1.56 christos 638: Error("Graph cycles through centurion %s", centurion->name);
1.47 pk 639: } else {
640: centurion = cgn;
641: }
1.68 dsl 642: parents = centurion->parents;
643:
644: /* If this was a .ORDER node, schedule the RHS */
1.176 rillig 645: Lst_ForEachUntil(centurion->order_succ, MakeBuildParent, toBeMade->first);
1.68 dsl 646:
647: /* Now mark all the parents as having one less unmade child */
1.170 rillig 648: for (ln = parents->first; ln != NULL; ln = ln->next) {
649: GNode *pgn = ln->datum;
1.169 rillig 650:
1.119 rillig 651: if (DEBUG(MAKE))
1.153 rillig 652: debug_printf("inspect parent %s%s: flags %x, "
653: "type %x, made %d, unmade %d ",
654: pgn->name, pgn->cohort_num, pgn->flags,
655: pgn->type, pgn->made, pgn->unmade - 1);
1.119 rillig 656:
657: if (!(pgn->flags & REMAKE)) {
658: /* This parent isn't needed */
1.152 rillig 659: DEBUG0(MAKE, "- not needed\n");
1.119 rillig 660: continue;
661: }
662: if (mtime == 0 && !(cgn->type & OP_WAIT))
663: pgn->flags |= FORCE;
664:
665: /*
666: * If the parent has the .MADE attribute, its timestamp got
1.178 rillig 667: * updated to that of its newest child, and its unmade
1.119 rillig 668: * child count got set to zero in Make_ExpandUse().
669: * However other things might cause us to build one of its
670: * children - and so we mustn't do any processing here when
671: * the child build finishes.
672: */
673: if (pgn->type & OP_MADE) {
1.152 rillig 674: DEBUG0(MAKE, "- .MADE\n");
1.119 rillig 675: continue;
676: }
677:
1.189 rillig 678: if (!(cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE))) {
1.119 rillig 679: if (cgn->made == MADE)
680: pgn->flags |= CHILDMADE;
1.189 rillig 681: GNode_UpdateYoungestChild(pgn, cgn);
1.119 rillig 682: }
1.68 dsl 683:
1.119 rillig 684: /*
685: * A parent must wait for the completion of all instances
686: * of a `::' dependency.
687: */
688: if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
1.152 rillig 689: DEBUG2(MAKE, "- centurion made %d, %d unmade cohorts\n",
690: centurion->made, centurion->unmade_cohorts);
1.119 rillig 691: continue;
692: }
1.47 pk 693:
1.119 rillig 694: /* One more child of this parent is now made */
1.156 rillig 695: pgn->unmade--;
1.119 rillig 696: if (pgn->unmade < 0) {
697: if (DEBUG(MAKE)) {
1.153 rillig 698: debug_printf("Graph cycles through %s%s\n",
699: pgn->name, pgn->cohort_num);
1.119 rillig 700: Targ_PrintGraph(2);
1.47 pk 701: }
1.119 rillig 702: Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
703: }
1.1 cgd 704:
1.119 rillig 705: /* We must always rescan the parents of .WAIT and .ORDER nodes. */
706: if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
707: && !(centurion->flags & DONE_ORDER)) {
1.152 rillig 708: DEBUG0(MAKE, "- unmade children\n");
1.119 rillig 709: continue;
710: }
711: if (pgn->made != DEFERRED) {
1.47 pk 712: /*
1.119 rillig 713: * Either this parent is on a different branch of the tree,
714: * or it on the RHS of a .WAIT directive
715: * or it is already on the toBeMade list.
1.47 pk 716: */
1.152 rillig 717: DEBUG0(MAKE, "- not deferred\n");
1.119 rillig 718: continue;
719: }
1.201 rillig 720:
721: if (IsWaitingForOrder(pgn))
1.119 rillig 722: continue;
1.201 rillig 723:
1.119 rillig 724: if (DEBUG(MAKE)) {
1.153 rillig 725: debug_printf("- %s%s made, schedule %s%s (made %d)\n",
726: cgn->name, cgn->cohort_num,
727: pgn->name, pgn->cohort_num, pgn->made);
1.135 rillig 728: Targ_PrintNode(pgn, 2);
1.119 rillig 729: }
730: /* Ok, we can schedule the parent again */
731: pgn->made = REQUESTED;
1.129 rillig 732: Lst_Enqueue(toBeMade, pgn);
1.1 cgd 733: }
1.10 christos 734:
1.169 rillig 735: UpdateImplicitParentsVars(cgn, cname);
1.1 cgd 736: }
1.108 rillig 737:
1.143 rillig 738: static void
1.144 rillig 739: UnmarkChildren(GNode *gn)
1.141 rillig 740: {
1.144 rillig 741: GNodeListNode *ln;
1.141 rillig 742:
1.144 rillig 743: for (ln = gn->children->first; ln != NULL; ln = ln->next) {
744: GNode *child = ln->datum;
745: child->type &= ~OP_MARK;
746: }
1.141 rillig 747: }
748:
1.149 rillig 749: /* Add a child's name to the ALLSRC and OODATE variables of the given
1.172 rillig 750: * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
751: * attributes. .EXEC and .USE children are very rarely going to be files,
752: * so...
753: *
1.149 rillig 754: * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
755: *
756: * A child is added to the OODATE variable if its modification time is
757: * later than that of its parent, as defined by Make, except if the
758: * parent is a .JOIN node. In that case, it is only added to the OODATE
759: * variable if it was actually made (since .JOIN nodes don't have
760: * modification times, the comparison is rather unfair...)..
1.1 cgd 761: *
1.141 rillig 762: * Input:
1.162 rillig 763: * cgn The child to add
764: * pgn The parent to whose ALLSRC variable it should
1.141 rillig 765: * be added
1.1 cgd 766: */
1.142 rillig 767: static void
1.162 rillig 768: MakeAddAllSrc(GNode *cgn, GNode *pgn)
1.1 cgd 769: {
1.31 mycroft 770: if (cgn->type & OP_MARK)
1.142 rillig 771: return;
1.31 mycroft 772: cgn->type |= OP_MARK;
773:
1.187 rillig 774: if (!(cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE))) {
1.102 rillig 775: const char *child, *allsrc;
1.1 cgd 776:
1.17 christos 777: if (cgn->type & OP_ARCHV)
1.184 rillig 778: child = GNode_VarMember(cgn);
1.17 christos 779: else
1.180 rillig 780: child = GNode_Path(cgn);
1.45 pk 781: if (cgn->type & OP_JOIN) {
1.184 rillig 782: allsrc = GNode_VarAllsrc(cgn);
1.45 pk 783: } else {
784: allsrc = child;
785: }
1.46 pk 786: if (allsrc != NULL)
1.56 christos 787: Var_Append(ALLSRC, allsrc, pgn);
1.1 cgd 788: if (pgn->type & OP_JOIN) {
789: if (cgn->made == MADE) {
790: Var_Append(OODATE, child, pgn);
791: }
792: } else if ((pgn->mtime < cgn->mtime) ||
793: (cgn->mtime >= now && cgn->made == MADE))
794: {
795: /*
796: * It goes in the OODATE variable if the parent is younger than the
797: * child or if the child has been modified more recently than
798: * the start of the make. This is to keep pmake from getting
799: * confused if something else updates the parent after the
800: * make starts (shouldn't happen, I know, but sometimes it
801: * does). In such a case, if we've updated the kid, the parent
802: * is likely to have a modification time later than that of
803: * the kid and anything that relies on the OODATE variable will
804: * be hosed.
805: *
806: * XXX: This will cause all made children to go in the OODATE
807: * variable, even if they're not touched, if RECHECK isn't defined,
808: * since cgn->mtime is set to now in Make_Update. According to
809: * some people, this is good...
810: */
811: Var_Append(OODATE, child, pgn);
812: }
813: }
814: }
1.108 rillig 815:
1.149 rillig 816: /* Set up the ALLSRC and OODATE variables. Sad to say, it must be
817: * done separately, rather than while traversing the graph. This is
818: * because Make defined OODATE to contain all sources whose modification
819: * times were later than that of the target, *not* those sources that
820: * were out-of-date. Since in both compatibility and native modes,
821: * the modification time of the parent isn't found until the child
822: * has been dealt with, we have to wait until now to fill in the
823: * variable. As for ALLSRC, the ordering is important and not
824: * guaranteed when in native mode, so it must be set here, too.
1.1 cgd 825: *
1.149 rillig 826: * If the node is a .JOIN node, its TARGET variable will be set to
827: * match its ALLSRC variable.
1.1 cgd 828: */
829: void
1.50 wiz 830: Make_DoAllVar(GNode *gn)
1.1 cgd 831: {
1.162 rillig 832: GNodeListNode *ln;
833:
1.79 sjg 834: if (gn->flags & DONE_ALLSRC)
835: return;
1.99 rillig 836:
1.144 rillig 837: UnmarkChildren(gn);
1.162 rillig 838: for (ln = gn->children->first; ln != NULL; ln = ln->next)
1.182 rillig 839: MakeAddAllSrc(ln->datum, gn);
1.1 cgd 840:
1.149 rillig 841: if (!Var_Exists(OODATE, gn)) {
1.97 rillig 842: Var_Set(OODATE, "", gn);
1.1 cgd 843: }
1.149 rillig 844: if (!Var_Exists(ALLSRC, gn)) {
1.97 rillig 845: Var_Set(ALLSRC, "", gn);
1.1 cgd 846: }
847:
1.184 rillig 848: if (gn->type & OP_JOIN)
849: Var_Set(TARGET, GNode_VarAllsrc(gn), gn);
1.79 sjg 850: gn->flags |= DONE_ALLSRC;
1.1 cgd 851: }
1.108 rillig 852:
1.68 dsl 853: static int
1.78 dsl 854: MakeBuildChild(void *v_cn, void *toBeMade_next)
1.68 dsl 855: {
856: GNode *cn = v_cn;
857:
1.152 rillig 858: DEBUG4(MAKE, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
859: cn->name, cn->cohort_num, cn->made, cn->type);
1.68 dsl 860: if (cn->made > DEFERRED)
861: return 0;
862:
863: /* If this node is on the RHS of a .ORDER, check LHSs. */
1.201 rillig 864: if (IsWaitingForOrder(cn)) {
1.68 dsl 865: /* Can't build this (or anything else in this child list) yet */
866: cn->made = DEFERRED;
1.88 sjg 867: return 0; /* but keep looking */
1.68 dsl 868: }
869:
1.152 rillig 870: DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n", cn->name, cn->cohort_num);
1.68 dsl 871:
872: cn->made = REQUESTED;
1.77 dsl 873: if (toBeMade_next == NULL)
1.129 rillig 874: Lst_Append(toBeMade, cn);
1.68 dsl 875: else
1.129 rillig 876: Lst_InsertBefore(toBeMade, toBeMade_next, cn);
1.68 dsl 877:
878: if (cn->unmade_cohorts != 0)
1.176 rillig 879: Lst_ForEachUntil(cn->cohorts, MakeBuildChild, toBeMade_next);
1.68 dsl 880:
881: /*
1.142 rillig 882: * If this node is a .WAIT node with unmade children
1.68 dsl 883: * then don't add the next sibling.
884: */
885: return cn->type & OP_WAIT && cn->unmade > 0;
886: }
887:
1.88 sjg 888: /* When a .ORDER LHS node completes we do this on each RHS */
1.148 rillig 889: static int
1.78 dsl 890: MakeBuildParent(void *v_pn, void *toBeMade_next)
1.68 dsl 891: {
892: GNode *pn = v_pn;
893:
894: if (pn->made != DEFERRED)
1.148 rillig 895: return 0;
1.68 dsl 896:
897: if (MakeBuildChild(pn, toBeMade_next) == 0) {
898: /* Mark so that when this node is built we reschedule its parents */
899: pn->flags |= DONE_ORDER;
900: }
1.148 rillig 901:
902: return 0;
1.68 dsl 903: }
904:
1.149 rillig 905: /* Start as many jobs as possible, taking them from the toBeMade queue.
906: *
907: * If the query flag was given to pmake, no job will be started,
908: * but as soon as an out-of-date target is found, this function
909: * returns TRUE. At all other times, this function returns FALSE.
910: */
1.1 cgd 911: static Boolean
1.50 wiz 912: MakeStartJobs(void)
1.1 cgd 913: {
1.202 ! rillig 914: GNode *gn;
! 915: Boolean have_token = FALSE;
1.10 christos 916:
1.129 rillig 917: while (!Lst_IsEmpty(toBeMade)) {
1.60 dsl 918: /* Get token now to avoid cycling job-list when we only have 1 token */
919: if (!have_token && !Job_TokenWithdraw())
920: break;
1.202 ! rillig 921: have_token = TRUE;
1.60 dsl 922:
1.129 rillig 923: gn = Lst_Dequeue(toBeMade);
1.152 rillig 924: DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
1.68 dsl 925:
926: if (gn->made != REQUESTED) {
1.152 rillig 927: DEBUG1(MAKE, "state %d\n", gn->made);
1.68 dsl 928:
929: make_abort(gn, __LINE__);
1.1 cgd 930: }
931:
1.166 rillig 932: if (gn->checked_seqno == checked) {
1.70 dsl 933: /* We've already looked at this node since a job finished... */
1.152 rillig 934: DEBUG2(MAKE, "already checked %s%s\n", gn->name, gn->cohort_num);
1.71 dsl 935: gn->made = DEFERRED;
1.70 dsl 936: continue;
937: }
1.166 rillig 938: gn->checked_seqno = checked;
1.70 dsl 939:
1.68 dsl 940: if (gn->unmade != 0) {
1.1 cgd 941: /*
1.68 dsl 942: * We can't build this yet, add all unmade children to toBeMade,
943: * just before the current first element.
1.1 cgd 944: */
1.68 dsl 945: gn->made = DEFERRED;
1.176 rillig 946: Lst_ForEachUntil(gn->children, MakeBuildChild, toBeMade->first);
1.68 dsl 947: /* and drop this node on the floor */
1.152 rillig 948: DEBUG2(MAKE, "dropped %s%s\n", gn->name, gn->cohort_num);
1.68 dsl 949: continue;
1.1 cgd 950: }
1.10 christos 951:
1.68 dsl 952: gn->made = BEINGMADE;
1.190 rillig 953: if (GNode_IsOODate(gn)) {
1.152 rillig 954: DEBUG0(MAKE, "out-of-date\n");
1.181 rillig 955: if (opts.queryFlag) {
1.98 rillig 956: return TRUE;
1.1 cgd 957: }
1.56 christos 958: Make_DoAllVar(gn);
959: Job_Make(gn);
1.202 ! rillig 960: have_token = FALSE;
1.1 cgd 961: } else {
1.152 rillig 962: DEBUG0(MAKE, "up-to-date\n");
1.1 cgd 963: gn->made = UPTODATE;
964: if (gn->type & OP_JOIN) {
965: /*
966: * Even for an up-to-date .JOIN node, we need it to have its
967: * context variables so references to it get the correct
968: * value for .TARGET when building up the context variables
969: * of its parent(s)...
970: */
1.56 christos 971: Make_DoAllVar(gn);
1.1 cgd 972: }
1.56 christos 973: Make_Update(gn);
1.1 cgd 974: }
975: }
1.59 dsl 976:
977: if (have_token)
978: Job_TokenReturn();
979:
1.98 rillig 980: return FALSE;
1.1 cgd 981: }
1.108 rillig 982:
1.173 rillig 983: static void
984: MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
1.68 dsl 985: {
986: if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
987: /* not waiting for this one */
1.173 rillig 988: return;
1.68 dsl 989:
1.122 rillig 990: printf(" `%s%s' has .ORDER dependency against %s%s ",
1.173 rillig 991: gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1.122 rillig 992: GNode_FprintDetails(stdout, "(", ogn, ")\n");
993:
1.181 rillig 994: if (DEBUG(MAKE) && opts.debug_file != stdout) {
1.153 rillig 995: debug_printf(" `%s%s' has .ORDER dependency against %s%s ",
996: gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1.181 rillig 997: GNode_FprintDetails(opts.debug_file, "(", ogn, ")\n");
1.122 rillig 998: }
1.173 rillig 999: }
1000:
1001: static void
1002: MakePrintStatusOrder(GNode *gn)
1003: {
1004: GNodeListNode *ln;
1005: for (ln = gn->order_pred->first; ln != NULL; ln = ln->next)
1.182 rillig 1006: MakePrintStatusOrderNode(ln->datum, gn);
1.68 dsl 1007: }
1008:
1.174 rillig 1009: static void MakePrintStatusList(GNodeList *, int *);
1010:
1.149 rillig 1011: /* Print the status of a top-level node, viz. it being up-to-date already
1012: * or not created due to an error in a lower level.
1013: * Callback function for Make_Run via Lst_ForEachUntil.
1014: */
1.175 rillig 1015: static Boolean
1.174 rillig 1016: MakePrintStatus(GNode *gn, int *errors)
1.1 cgd 1017: {
1.68 dsl 1018: if (gn->flags & DONECYCLE)
1019: /* We've completely processed this node before, don't do it again. */
1.175 rillig 1020: return FALSE;
1.68 dsl 1021:
1022: if (gn->unmade == 0) {
1023: gn->flags |= DONECYCLE;
1024: switch (gn->made) {
1025: case UPTODATE:
1026: printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
1027: break;
1028: case MADE:
1029: break;
1030: case UNMADE:
1031: case DEFERRED:
1032: case REQUESTED:
1033: case BEINGMADE:
1034: (*errors)++;
1.122 rillig 1035: printf("`%s%s' was not built", gn->name, gn->cohort_num);
1036: GNode_FprintDetails(stdout, " (", gn, ")!\n");
1.181 rillig 1037: if (DEBUG(MAKE) && opts.debug_file != stdout) {
1.153 rillig 1038: debug_printf("`%s%s' was not built", gn->name, gn->cohort_num);
1.181 rillig 1039: GNode_FprintDetails(opts.debug_file, " (", gn, ")!\n");
1.122 rillig 1040: }
1.68 dsl 1041: /* Most likely problem is actually caused by .ORDER */
1.173 rillig 1042: MakePrintStatusOrder(gn);
1.68 dsl 1043: break;
1044: default:
1045: /* Errors - already counted */
1046: printf("`%s%s' not remade because of errors.\n",
1047: gn->name, gn->cohort_num);
1.181 rillig 1048: if (DEBUG(MAKE) && opts.debug_file != stdout)
1.153 rillig 1049: debug_printf("`%s%s' not remade because of errors.\n",
1050: gn->name, gn->cohort_num);
1.68 dsl 1051: break;
1.1 cgd 1052: }
1.175 rillig 1053: return FALSE;
1.68 dsl 1054: }
1055:
1.152 rillig 1056: DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
1057: gn->name, gn->cohort_num, gn->unmade);
1.68 dsl 1058: /*
1059: * If printing cycles and came to one that has unmade children,
1.70 dsl 1060: * print out the cycle by recursing on its children.
1.68 dsl 1061: */
1.70 dsl 1062: if (!(gn->flags & CYCLE)) {
1063: /* Fist time we've seen this node, check all children */
1.68 dsl 1064: gn->flags |= CYCLE;
1.174 rillig 1065: MakePrintStatusList(gn->children, errors);
1.70 dsl 1066: /* Mark that this node needn't be processed again */
1.68 dsl 1067: gn->flags |= DONECYCLE;
1.175 rillig 1068: return FALSE;
1.1 cgd 1069: }
1.68 dsl 1070:
1.70 dsl 1071: /* Only output the error once per node */
1072: gn->flags |= DONECYCLE;
1073: Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
1074: if ((*errors)++ > 100)
1075: /* Abandon the whole error report */
1.175 rillig 1076: return TRUE;
1.70 dsl 1077:
1078: /* Reporting for our children will give the rest of the loop */
1.174 rillig 1079: MakePrintStatusList(gn->children, errors);
1.175 rillig 1080: return FALSE;
1.1 cgd 1081: }
1.108 rillig 1082:
1.174 rillig 1083: static void
1084: MakePrintStatusList(GNodeList *gnodes, int *errors)
1085: {
1086: GNodeListNode *ln;
1087: for (ln = gnodes->first; ln != NULL; ln = ln->next)
1.182 rillig 1088: if (MakePrintStatus(ln->datum, errors))
1.175 rillig 1089: break;
1.174 rillig 1090: }
1.11 christos 1091:
1.200 rillig 1092: static void
1093: ExamineLater(GNodeList *examine, GNodeList *toBeExamined)
1094: {
1095: ListNode *ln;
1096:
1097: for (ln = toBeExamined->first; ln != NULL; ln = ln->next) {
1098: GNode *gn = ln->datum;
1099:
1100: if (gn->flags & REMAKE)
1101: continue;
1102: if (gn->type & (OP_USE | OP_USEBEFORE))
1103: continue;
1104:
1105: DEBUG2(MAKE, "ExamineLater: need to examine \"%s%s\"\n",
1106: gn->name, gn->cohort_num);
1107: Lst_Enqueue(examine, gn);
1108: }
1109: }
1110:
1.149 rillig 1111: /* Expand .USE nodes and create a new targets list.
1.50 wiz 1112: *
1113: * Input:
1114: * targs the initial list of targets
1.1 cgd 1115: */
1.68 dsl 1116: void
1.137 rillig 1117: Make_ExpandUse(GNodeList *targs)
1.1 cgd 1118: {
1.137 rillig 1119: GNodeList *examine; /* List of targets to examine */
1.1 cgd 1120:
1.179 rillig 1121: {
1.182 rillig 1122: /* XXX: Why is it necessary to copy the list? There shouldn't be
1123: * any modifications to the list, at least the function name
1124: * ExpandUse doesn't suggest that. */
1.179 rillig 1125: GNodeListNode *ln;
1126: examine = Lst_New();
1127: for (ln = targs->first; ln != NULL; ln = ln->next)
1128: Lst_Append(examine, ln->datum);
1129: }
1.10 christos 1130:
1.1 cgd 1131: /*
1132: * Make an initial downward pass over the graph, marking nodes to be made
1133: * as we go down. We call Suff_FindDeps to find where a node is and
1134: * to get some children for it if it has none and also has no commands.
1135: * If the node is a leaf, we stick it on the toBeMade queue to
1136: * be looked at in a minute, otherwise we add its children to our queue
1.10 christos 1137: * and go on about our business.
1.1 cgd 1138: */
1.129 rillig 1139: while (!Lst_IsEmpty(examine)) {
1.137 rillig 1140: GNode *gn = Lst_Dequeue(examine);
1.99 rillig 1141:
1.68 dsl 1142: if (gn->flags & REMAKE)
1143: /* We've looked at this one already */
1144: continue;
1145: gn->flags |= REMAKE;
1.152 rillig 1146: DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
1147: gn->name, gn->cohort_num);
1.10 christos 1148:
1.120 rillig 1149: if (gn->type & OP_DOUBLEDEP)
1.129 rillig 1150: Lst_PrependAll(examine, gn->cohorts);
1.10 christos 1151:
1.68 dsl 1152: /*
1153: * Apply any .USE rules before looking for implicit dependencies
1154: * to make sure everything has commands that should...
1155: * Make sure that the TARGET is set, so that we can make
1156: * expansions.
1157: */
1158: if (gn->type & OP_ARCHV) {
1159: char *eoa, *eon;
1160: eoa = strchr(gn->name, '(');
1161: eon = strchr(gn->name, ')');
1162: if (eoa == NULL || eon == NULL)
1163: continue;
1164: *eoa = '\0';
1165: *eon = '\0';
1.97 rillig 1166: Var_Set(MEMBER, eoa + 1, gn);
1167: Var_Set(ARCHIVE, gn->name, gn);
1.68 dsl 1168: *eoa = '(';
1169: *eon = ')';
1170: }
1171:
1.194 rillig 1172: Dir_UpdateMTime(gn, FALSE);
1.180 rillig 1173: Var_Set(TARGET, GNode_Path(gn), gn);
1.144 rillig 1174: UnmarkChildren(gn);
1.150 rillig 1175: HandleUseNodes(gn);
1.68 dsl 1176:
1.187 rillig 1177: if (!(gn->type & OP_MADE))
1.68 dsl 1178: Suff_FindDeps(gn);
1179: else {
1.198 rillig 1180: PretendAllChildrenAreMade(gn);
1.68 dsl 1181: if (gn->unmade != 0)
1182: printf("Warning: %s%s still has %d unmade children\n",
1183: gn->name, gn->cohort_num, gn->unmade);
1184: }
1185:
1186: if (gn->unmade != 0)
1.200 rillig 1187: ExamineLater(examine, gn->children);
1.68 dsl 1188: }
1189:
1.129 rillig 1190: Lst_Free(examine);
1.68 dsl 1191: }
1192:
1.139 rillig 1193: /* Make the .WAIT node depend on the previous children */
1194: static void
1195: add_wait_dependency(GNodeListNode *owln, GNode *wn)
1.68 dsl 1196: {
1.139 rillig 1197: GNodeListNode *cln;
1198: GNode *cn;
1.68 dsl 1199:
1.147 rillig 1200: for (cln = owln; (cn = cln->datum) != wn; cln = cln->next) {
1.152 rillig 1201: DEBUG3(MAKE, ".WAIT: add dependency %s%s -> %s\n",
1202: cn->name, cn->cohort_num, wn->name);
1.68 dsl 1203:
1.139 rillig 1204: /* XXX: This pattern should be factored out, it repeats often */
1205: Lst_Append(wn->children, cn);
1206: wn->unmade++;
1207: Lst_Append(cn->parents, wn);
1.68 dsl 1208: }
1209: }
1210:
1.149 rillig 1211: /* Convert .WAIT nodes into dependencies. */
1.68 dsl 1212: static void
1.137 rillig 1213: Make_ProcessWait(GNodeList *targs)
1.68 dsl 1214: {
1.137 rillig 1215: GNode *pgn; /* 'parent' node we are examining */
1216: GNodeListNode *owln; /* Previous .WAIT node */
1.151 rillig 1217: GNodeList *examine; /* List of targets to examine */
1.68 dsl 1218:
1219: /*
1220: * We need all the nodes to have a common parent in order for the
1221: * .WAIT and .ORDER scheduling to work.
1222: * Perhaps this should be done earlier...
1223: */
1224:
1225: pgn = Targ_NewGN(".MAIN");
1226: pgn->flags = REMAKE;
1227: pgn->type = OP_PHONY | OP_DEPENDS;
1228: /* Get it displayed in the diag dumps */
1.129 rillig 1229: Lst_Prepend(Targ_List(), pgn);
1.68 dsl 1230:
1.164 rillig 1231: {
1232: GNodeListNode *ln;
1233: for (ln = targs->first; ln != NULL; ln = ln->next) {
1234: GNode *cgn = ln->datum;
1235:
1236: Lst_Append(pgn->children, cgn);
1237: Lst_Append(cgn->parents, pgn);
1238: pgn->unmade++;
1239: }
1240: }
1.68 dsl 1241:
1242: /* Start building with the 'dummy' .MAIN' node */
1.77 dsl 1243: MakeBuildChild(pgn, NULL);
1.68 dsl 1244:
1.160 rillig 1245: examine = Lst_New();
1.129 rillig 1246: Lst_Append(examine, pgn);
1.68 dsl 1247:
1.129 rillig 1248: while (!Lst_IsEmpty(examine)) {
1.164 rillig 1249: GNodeListNode *ln;
1250:
1.129 rillig 1251: pgn = Lst_Dequeue(examine);
1.99 rillig 1252:
1.68 dsl 1253: /* We only want to process each child-list once */
1254: if (pgn->flags & DONE_WAIT)
1255: continue;
1256: pgn->flags |= DONE_WAIT;
1.152 rillig 1257: DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
1.68 dsl 1258:
1.120 rillig 1259: if (pgn->type & OP_DOUBLEDEP)
1.129 rillig 1260: Lst_PrependAll(examine, pgn->cohorts);
1.1 cgd 1261:
1.165 rillig 1262: owln = pgn->children->first;
1.171 rillig 1263: for (ln = pgn->children->first; ln != NULL; ln = ln->next) {
1.165 rillig 1264: GNode *cgn = ln->datum;
1.68 dsl 1265: if (cgn->type & OP_WAIT) {
1.139 rillig 1266: add_wait_dependency(owln, cgn);
1.68 dsl 1267: owln = ln;
1.1 cgd 1268: } else {
1.129 rillig 1269: Lst_Append(examine, cgn);
1.1 cgd 1270: }
1271: }
1272: }
1.10 christos 1273:
1.129 rillig 1274: Lst_Free(examine);
1.11 christos 1275: }
1276:
1277: /*-
1278: *-----------------------------------------------------------------------
1279: * Make_Run --
1280: * Initialize the nodes to remake and the list of nodes which are
1281: * ready to be made by doing a breadth-first traversal of the graph
1282: * starting from the nodes in the given list. Once this traversal
1283: * is finished, all the 'leaves' of the graph are in the toBeMade
1284: * queue.
1285: * Using this queue and the Job module, work back up the graph,
1286: * calling on MakeStartJobs to keep the job table as full as
1287: * possible.
1288: *
1.50 wiz 1289: * Input:
1290: * targs the initial list of targets
1291: *
1.11 christos 1292: * Results:
1293: * TRUE if work was done. FALSE otherwise.
1294: *
1295: * Side Effects:
1296: * The make field of all nodes involved in the creation of the given
1297: * targets is set to 1. The toBeMade list is set to contain all the
1298: * 'leaves' of these subgraphs.
1299: *-----------------------------------------------------------------------
1300: */
1301: Boolean
1.137 rillig 1302: Make_Run(GNodeList *targs)
1.11 christos 1303: {
1.151 rillig 1304: int errors; /* Number of errors the Job module reports */
1.11 christos 1305:
1.68 dsl 1306: /* Start trying to make the current targets... */
1.160 rillig 1307: toBeMade = Lst_New();
1.68 dsl 1308:
1309: Make_ExpandUse(targs);
1310: Make_ProcessWait(targs);
1311:
1.60 dsl 1312: if (DEBUG(MAKE)) {
1.153 rillig 1313: debug_printf("#***# full graph\n");
1.68 dsl 1314: Targ_PrintGraph(1);
1.60 dsl 1315: }
1.1 cgd 1316:
1.181 rillig 1317: if (opts.queryFlag) {
1.1 cgd 1318: /*
1319: * We wouldn't do any work unless we could start some jobs in the
1320: * next loop... (we won't actually start any, of course, this is just
1321: * to see if any of the targets was out of date)
1322: */
1.98 rillig 1323: return MakeStartJobs();
1.1 cgd 1324: }
1.68 dsl 1325: /*
1326: * Initialization. At the moment, no jobs are running and until some
1327: * get started, nothing will happen since the remaining upward
1328: * traversal of the graph is performed by the routines in job.c upon
1329: * the finishing of a job. So we fill the Job table as much as we can
1330: * before going into our loop.
1331: */
1332: (void)MakeStartJobs();
1.1 cgd 1333:
1334: /*
1335: * Main Loop: The idea here is that the ending of jobs will take
1336: * care of the maintenance of data structures and the waiting for output
1337: * will cause us to be idle most of the time while our children run as
1338: * much as possible. Because the job table is kept as full as possible,
1339: * the only time when it will be empty is when all the jobs which need
1340: * running have been run, so that is the end condition of this loop.
1341: * Note that the Job module will exit if there were any errors unless the
1342: * keepgoing flag was given.
1343: */
1.129 rillig 1344: while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
1.56 christos 1345: Job_CatchOutput();
1.1 cgd 1346: (void)MakeStartJobs();
1347: }
1348:
1.20 christos 1349: errors = Job_Finish();
1.1 cgd 1350:
1351: /*
1352: * Print the final status of each target. E.g. if it wasn't made
1353: * because some inferior reported an error.
1354: */
1.152 rillig 1355: DEBUG1(MAKE, "done: errors %d\n", errors);
1.68 dsl 1356: if (errors == 0) {
1.174 rillig 1357: MakePrintStatusList(targs, &errors);
1.68 dsl 1358: if (DEBUG(MAKE)) {
1.153 rillig 1359: debug_printf("done: errors %d\n", errors);
1.188 rillig 1360: if (errors > 0)
1.68 dsl 1361: Targ_PrintGraph(4);
1362: }
1363: }
1.188 rillig 1364: return errors > 0;
1.1 cgd 1365: }
CVSweb <webmaster@jp.NetBSD.org>