Home | History | Annotate | Line # | Download | only in make
      1 /*	$NetBSD: make.c,v 1.273 2025/07/06 07:11:31 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. 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 /*
     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  * Examination of targets and their suitability for creation.
     73  *
     74  * Interface:
     75  *	Make_MakeParallel
     76  *			Make the targets in parallel mode.
     77  *
     78  *	Make_Update	After a target is made, update all its parents.
     79  *			Perform various bookkeeping chores like the updating
     80  *			of the youngestChild field of the parent, filling
     81  *			of the IMPSRC variable, etc. Place the parent on the
     82  *			toBeMade queue if it should be.
     83  *
     84  *	GNode_UpdateYoungestChild
     85  *			Update the node's youngestChild field based on the
     86  *			child's modification time.
     87  *
     88  *	GNode_SetLocalVars
     89  *			Set up the various local variables for a
     90  *			target, including the .ALLSRC variable, making
     91  *			sure that any variable that needs to exist
     92  *			at the very least has the empty value.
     93  *
     94  *	GNode_IsOODate	Determine if a target is out-of-date.
     95  *
     96  *	Make_HandleUse	See if a child is a .USE node for a parent
     97  *			and perform the .USE actions if so.
     98  *
     99  *	Make_ExpandUse	Expand .USE nodes
    100  */
    101 
    102 #include "make.h"
    103 #include "dir.h"
    104 #include "job.h"
    105 #ifdef USE_META
    106 # include "meta.h"
    107 #endif
    108 
    109 /*	"@(#)make.c	8.1 (Berkeley) 6/6/93"	*/
    110 MAKE_RCSID("$NetBSD: make.c,v 1.273 2025/07/06 07:11:31 rillig Exp $");
    111 
    112 /* Sequence # to detect recursion. */
    113 static unsigned checked_seqno = 1;
    114 
    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  */
    120 static GNodeList toBeMade = LST_INIT;
    121 
    122 
    123 void
    124 debug_printf(const char *fmt, ...)
    125 {
    126 	va_list ap;
    127 
    128 	va_start(ap, fmt);
    129 	vfprintf(opts.debug_file, fmt, ap);
    130 	va_end(ap);
    131 }
    132 
    133 char *
    134 GNodeType_ToString(GNodeType type)
    135 {
    136 	Buffer buf;
    137 
    138 	Buf_Init(&buf);
    139 #define ADD(flag) Buf_AddFlag(&buf, (type & (flag)) != OP_NONE, #flag)
    140 	ADD(OP_DEPENDS);
    141 	ADD(OP_FORCE);
    142 	ADD(OP_DOUBLEDEP);
    143 	ADD(OP_OPTIONAL);
    144 	ADD(OP_USE);
    145 	ADD(OP_EXEC);
    146 	ADD(OP_IGNORE);
    147 	ADD(OP_PRECIOUS);
    148 	ADD(OP_SILENT);
    149 	ADD(OP_MAKE);
    150 	ADD(OP_JOIN);
    151 	ADD(OP_MADE);
    152 	ADD(OP_SPECIAL);
    153 	ADD(OP_USEBEFORE);
    154 	ADD(OP_INVISIBLE);
    155 	ADD(OP_NOTMAIN);
    156 	ADD(OP_PHONY);
    157 	ADD(OP_NOPATH);
    158 	ADD(OP_WAIT);
    159 	ADD(OP_NOMETA);
    160 	ADD(OP_META);
    161 	ADD(OP_NOMETA_CMP);
    162 	ADD(OP_SUBMAKE);
    163 	ADD(OP_TRANSFORM);
    164 	ADD(OP_MEMBER);
    165 	ADD(OP_LIB);
    166 	ADD(OP_ARCHV);
    167 	ADD(OP_HAS_COMMANDS);
    168 	ADD(OP_SAVE_CMDS);
    169 	ADD(OP_DEPS_FOUND);
    170 	ADD(OP_MARK);
    171 #undef ADD
    172 	if (buf.len == 0)
    173 		Buf_AddStr(&buf, "none");
    174 	return Buf_DoneData(&buf);
    175 }
    176 
    177 static char *
    178 GNodeFlags_ToString(GNodeFlags flags)
    179 {
    180 	Buffer buf;
    181 
    182 	Buf_Init(&buf);
    183 	Buf_AddFlag(&buf, flags.remake, "REMAKE");
    184 	Buf_AddFlag(&buf, flags.childMade, "CHILDMADE");
    185 	Buf_AddFlag(&buf, flags.force, "FORCE");
    186 	Buf_AddFlag(&buf, flags.doneWait, "DONE_WAIT");
    187 	Buf_AddFlag(&buf, flags.doneOrder, "DONE_ORDER");
    188 	Buf_AddFlag(&buf, flags.fromDepend, "FROM_DEPEND");
    189 	Buf_AddFlag(&buf, flags.doneAllsrc, "DONE_ALLSRC");
    190 	Buf_AddFlag(&buf, flags.cycle, "CYCLE");
    191 	Buf_AddFlag(&buf, flags.doneCycle, "DONECYCLE");
    192 	if (buf.len == 0)
    193 		Buf_AddStr(&buf, "none");
    194 	return Buf_DoneData(&buf);
    195 }
    196 
    197 void
    198 GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
    199 		    const char *suffix)
    200 {
    201 	char *type = GNodeType_ToString(gn->type);
    202 	char *flags = GNodeFlags_ToString(gn->flags);
    203 
    204 	fprintf(f, "%s%s, type %s, flags %s%s",
    205 	    prefix, GNodeMade_Name(gn->made), type, flags, suffix);
    206 	free(type);
    207 	free(flags);
    208 }
    209 
    210 bool
    211 GNode_ShouldExecute(GNode *gn)
    212 {
    213 	return !((gn->type & OP_MAKE)
    214 	    ? opts.noRecursiveExecute
    215 	    : opts.noExecute);
    216 }
    217 
    218 /* Update the youngest child of the node, according to the given child. */
    219 void
    220 GNode_UpdateYoungestChild(GNode *gn, GNode *cgn)
    221 {
    222 	if (gn->youngestChild == NULL || cgn->mtime > gn->youngestChild->mtime)
    223 		gn->youngestChild = cgn;
    224 }
    225 
    226 static bool
    227 IsOODateRegular(GNode *gn)
    228 {
    229 	/* These rules are inherited from the original Make. */
    230 
    231 	if (gn->youngestChild != NULL) {
    232 		if (gn->mtime < gn->youngestChild->mtime) {
    233 			DEBUG1(MAKE, "modified before source \"%s\"...",
    234 			    GNode_Path(gn->youngestChild));
    235 			return true;
    236 		}
    237 		return false;
    238 	}
    239 
    240 	if (gn->mtime == 0 && !(gn->type & OP_OPTIONAL)) {
    241 		DEBUG0(MAKE, "nonexistent and no sources...");
    242 		return true;
    243 	}
    244 
    245 	if (gn->type & OP_DOUBLEDEP) {
    246 		DEBUG0(MAKE, ":: operator and no sources...");
    247 		return true;
    248 	}
    249 
    250 	return false;
    251 }
    252 
    253 /*
    254  * See if the node is out of date with respect to its sources.
    255  *
    256  * Used by Make_MakeParallel when deciding which nodes to place on the
    257  * toBeMade queue initially and by Make_Update to screen out .USE and
    258  * .EXEC nodes. In the latter case, however, any other sort of node
    259  * must be considered out-of-date since at least one of its children
    260  * will have been recreated.
    261  *
    262  * The mtime field of the node and the youngestChild field of its parents
    263  * may be changed.
    264  */
    265 bool
    266 GNode_IsOODate(GNode *gn)
    267 {
    268 	bool oodate;
    269 
    270 	/*
    271 	 * Certain types of targets needn't even be sought as their datedness
    272 	 * doesn't depend on their modification time...
    273 	 */
    274 	if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
    275 		Dir_UpdateMTime(gn, true);
    276 		if (DEBUG(MAKE)) {
    277 			if (gn->mtime != 0)
    278 				debug_printf("modified %s...",
    279 				    Targ_FmtTime(gn->mtime));
    280 			else
    281 				debug_printf("nonexistent...");
    282 		}
    283 	}
    284 
    285 	/*
    286 	 * A target is remade in one of the following circumstances:
    287 	 *
    288 	 *	its modification time is smaller than that of its youngest
    289 	 *	child and it would actually be run (has commands or is not
    290 	 *	GNode_IsTarget)
    291 	 *
    292 	 *	it's the object of a force operator
    293 	 *
    294 	 *	it has no children, was on the lhs of an operator and doesn't
    295 	 *	exist already.
    296 	 *
    297 	 * Libraries are only considered out-of-date if the archive module
    298 	 * says they are.
    299 	 *
    300 	 * These weird rules are brought to you by Backward-Compatibility
    301 	 * and the strange people who wrote 'Make'.
    302 	 */
    303 	if (gn->type & (OP_USE | OP_USEBEFORE)) {
    304 		/*
    305 		 * If the node is a USE node it is *never* out of date
    306 		 * no matter *what*.
    307 		 */
    308 		DEBUG0(MAKE, ".USE node...");
    309 		oodate = false;
    310 	} else if ((gn->type & OP_LIB) && (gn->mtime == 0 || Arch_IsLib(gn))) {
    311 		DEBUG0(MAKE, "library...");
    312 
    313 		/*
    314 		 * always out of date if no children and :: target
    315 		 * or nonexistent.
    316 		 */
    317 		oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
    318 			  (gn->youngestChild == NULL &&
    319 			   (gn->type & OP_DOUBLEDEP)));
    320 	} else if (gn->type & OP_JOIN) {
    321 		/*
    322 		 * A target with the .JOIN attribute is only considered
    323 		 * out-of-date if any of its children was out-of-date.
    324 		 */
    325 		DEBUG0(MAKE, ".JOIN node...");
    326 		DEBUG1(MAKE, "source %smade...",
    327 		    gn->flags.childMade ? "" : "not ");
    328 		oodate = gn->flags.childMade;
    329 	} else if (gn->type & (OP_FORCE | OP_EXEC | OP_PHONY)) {
    330 		/*
    331 		 * A node which is the object of the force (!) operator or
    332 		 * which has the .EXEC attribute is always considered
    333 		 * out-of-date.
    334 		 */
    335 		if (DEBUG(MAKE)) {
    336 			if (gn->type & OP_FORCE)
    337 				debug_printf("! operator...");
    338 			else if (gn->type & OP_PHONY)
    339 				debug_printf(".PHONY node...");
    340 			else
    341 				debug_printf(".EXEC node...");
    342 		}
    343 		oodate = true;
    344 	} else if (IsOODateRegular(gn)) {
    345 		oodate = true;
    346 	} else {
    347 		/*
    348 		 * When a nonexistent child with no sources
    349 		 * (such as a typically used FORCE source) has been made and
    350 		 * the target of the child (usually a directory) has the same
    351 		 * timestamp as the timestamp just given to the nonexistent
    352 		 * child after it was considered made.
    353 		 */
    354 		if (DEBUG(MAKE)) {
    355 			if (gn->flags.force)
    356 				debug_printf("non existing child...");
    357 		}
    358 		oodate = gn->flags.force;
    359 	}
    360 
    361 #ifdef USE_META
    362 	if (useMeta)
    363 		oodate = meta_oodate(gn, oodate);
    364 #endif
    365 
    366 	/*
    367 	 * If the target isn't out-of-date, the parents need to know its
    368 	 * modification time. Note that targets that appear to be out-of-date
    369 	 * but aren't, because they have no commands and are GNode_IsTarget,
    370 	 * have their mtime stay below their children's mtime to keep parents
    371 	 * from thinking they're out-of-date.
    372 	 */
    373 	if (!oodate) {
    374 		GNodeListNode *ln;
    375 		for (ln = gn->parents.first; ln != NULL; ln = ln->next)
    376 			GNode_UpdateYoungestChild(ln->datum, gn);
    377 	}
    378 
    379 	return oodate;
    380 }
    381 
    382 static void
    383 PretendAllChildrenAreMade(GNode *pgn)
    384 {
    385 	GNodeListNode *ln;
    386 
    387 	for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
    388 		GNode *cgn = ln->datum;
    389 
    390 		/* This may also update cgn->path. */
    391 		Dir_UpdateMTime(cgn, false);
    392 		GNode_UpdateYoungestChild(pgn, cgn);
    393 		pgn->unmade--;
    394 	}
    395 }
    396 
    397 /*
    398  * Called by Make_MakeParallel and SuffApplyTransform on the downward pass to
    399  * handle .USE and transformation nodes, by copying the child node's commands,
    400  * type flags and children to the parent node.
    401  *
    402  * A .USE node is much like an explicit transformation rule, except its
    403  * commands are always added to the target node, even if the target already
    404  * has commands.
    405  *
    406  * Input:
    407  *	cgn		The source node, which is either a .USE/.USEBEFORE
    408  *			node or a transformation node (OP_TRANSFORM).
    409  *	pgn		The target node
    410  */
    411 void
    412 Make_HandleUse(GNode *cgn, GNode *pgn)
    413 {
    414 	GNodeListNode *ln;	/* An element in the children list */
    415 
    416 #ifdef DEBUG_SRC
    417 	if (!(cgn->type & (OP_USE | OP_USEBEFORE | OP_TRANSFORM))) {
    418 		debug_printf("Make_HandleUse: called for plain node %s\n",
    419 		    cgn->name);
    420 		/* XXX: debug mode should not affect control flow */
    421 		return;
    422 	}
    423 #endif
    424 
    425 	if ((cgn->type & (OP_USE | OP_USEBEFORE)) ||
    426 	    Lst_IsEmpty(&pgn->commands)) {
    427 		if (cgn->type & OP_USEBEFORE) {
    428 			/* .USEBEFORE */
    429 			Lst_PrependAll(&pgn->commands, &cgn->commands);
    430 		} else {
    431 			/* .USE, or target has no commands */
    432 			Lst_AppendAll(&pgn->commands, &cgn->commands);
    433 		}
    434 	}
    435 
    436 	for (ln = cgn->children.first; ln != NULL; ln = ln->next) {
    437 		GNode *gn = ln->datum;
    438 
    439 		/*
    440 		 * Expand variables in the .USE node's name
    441 		 * and save the unexpanded form.
    442 		 * We don't need to do this for commands.
    443 		 * They get expanded properly when we execute.
    444 		 */
    445 		if (gn->uname == NULL)
    446 			gn->uname = gn->name;
    447 		else
    448 			free(gn->name);
    449 		gn->name = Var_Subst(gn->uname, pgn, VARE_EVAL);
    450 		/* TODO: handle errors */
    451 		if (gn->uname != NULL && strcmp(gn->name, gn->uname) != 0) {
    452 			/* See if we have a target for this node. */
    453 			GNode *tgn = Targ_FindNode(gn->name);
    454 			if (tgn != NULL)
    455 				gn = tgn;
    456 		}
    457 
    458 		Lst_Append(&pgn->children, gn);
    459 		Lst_Append(&gn->parents, pgn);
    460 		pgn->unmade++;
    461 	}
    462 
    463 	pgn->type |=
    464 	    cgn->type & (unsigned)~(OP_OPMASK | OP_USE | OP_USEBEFORE | OP_TRANSFORM);
    465 }
    466 
    467 /*
    468  * Used by Make_MakeParallel on the downward pass to handle .USE nodes. Should
    469  * be called before the children are enqueued to be looked at by MakeAddChild.
    470  *
    471  * For a .USE child, the commands, type flags and children are copied to the
    472  * parent node, and since the relation to the .USE node is then no longer
    473  * needed, that relation is removed.
    474  *
    475  * Input:
    476  *	cgn		the child, which may be a .USE node
    477  *	pgn		the current parent
    478  */
    479 static void
    480 MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
    481 {
    482 	bool unmarked;
    483 
    484 	unmarked = !(cgn->type & OP_MARK);
    485 	cgn->type |= OP_MARK;
    486 
    487 	if (!(cgn->type & (OP_USE | OP_USEBEFORE)))
    488 		return;
    489 
    490 	if (unmarked)
    491 		Make_HandleUse(cgn, pgn);
    492 
    493 	Lst_Remove(&pgn->children, ln);
    494 	pgn->unmade--;
    495 }
    496 
    497 static void
    498 HandleUseNodes(GNode *gn)
    499 {
    500 	GNodeListNode *ln, *nln;
    501 	for (ln = gn->children.first; ln != NULL; ln = nln) {
    502 		nln = ln->next;
    503 		MakeHandleUse(ln->datum, gn, ln);
    504 	}
    505 }
    506 
    507 
    508 /*
    509  * Check the modification time of a gnode, and update it if necessary.
    510  * Return 0 if the gnode does not exist, or its filesystem time if it does.
    511  */
    512 time_t
    513 Make_Recheck(GNode *gn)
    514 {
    515 	time_t mtime;
    516 
    517 	Dir_UpdateMTime(gn, true);
    518 	mtime = gn->mtime;
    519 
    520 #ifndef RECHECK
    521 	/*
    522 	 * We can't re-stat the thing, but we can at least take care of rules
    523 	 * where a target depends on a source that actually creates the
    524 	 * target, but only if it has changed, e.g.
    525 	 *
    526 	 * parse.h : parse.o
    527 	 *
    528 	 * parse.o : parse.y
    529 	 *		yacc -d parse.y
    530 	 *		cc -c y.tab.c
    531 	 *		mv y.tab.o parse.o
    532 	 *		cmp -s y.tab.h parse.h || mv y.tab.h parse.h
    533 	 *
    534 	 * In this case, if the definitions produced by yacc haven't changed
    535 	 * from before, parse.h won't have been updated and gn->mtime will
    536 	 * reflect the current modification time for parse.h. This is
    537 	 * something of a kludge, I admit, but it's a useful one.
    538 	 *
    539 	 * XXX: People like to use a rule like "FRC:" to force things that
    540 	 * depend on FRC to be made, so we have to check for gn->children
    541 	 * being empty as well.
    542 	 */
    543 	if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children))
    544 		gn->mtime = now;
    545 #else
    546 	/*
    547 	 * This is what Make does and it's actually a good thing, as it
    548 	 * allows rules like
    549 	 *
    550 	 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
    551 	 *
    552 	 * to function as intended. Unfortunately, thanks to the stateless
    553 	 * nature of NFS (by which I mean the loose coupling of two clients
    554 	 * using the same file from a common server), there are times when
    555 	 * the modification time of a file created on a remote machine
    556 	 * will not be modified before the local stat() implied by the
    557 	 * Dir_UpdateMTime occurs, thus leading us to believe that the file
    558 	 * is unchanged, wreaking havoc with files that depend on this one.
    559 	 *
    560 	 * I have decided it is better to make too much than to make too
    561 	 * little, so this stuff is commented out unless you're sure it's ok.
    562 	 * -- ardeb 1/12/88
    563 	 */
    564 	/*
    565 	 * Christos, 4/9/92: If we are saving commands, pretend that
    566 	 * the target is made now. Otherwise archives with '...' rules
    567 	 * don't work!
    568 	 */
    569 	if (!GNode_ShouldExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
    570 	    (mtime == 0 && !(gn->type & OP_WAIT))) {
    571 		DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
    572 		    gn->name,
    573 		    gn->mtime == 0 ? "nonexistent" : Targ_FmtTime(gn->mtime));
    574 		gn->mtime = now;
    575 	} else {
    576 		DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
    577 		    gn->name, Targ_FmtTime(gn->mtime));
    578 	}
    579 #endif
    580 
    581 	/*
    582 	 * XXX: The returned mtime may differ from gn->mtime. Intentionally?
    583 	 */
    584 	return mtime;
    585 }
    586 
    587 /*
    588  * Set the .PREFIX and .IMPSRC variables for all the implied parents
    589  * of this node.
    590  */
    591 static void
    592 UpdateImplicitParentsVars(GNode *cgn, const char *cname)
    593 {
    594 	GNodeListNode *ln;
    595 	const char *cpref = GNode_VarPrefix(cgn);
    596 
    597 	for (ln = cgn->implicitParents.first; ln != NULL; ln = ln->next) {
    598 		GNode *pgn = ln->datum;
    599 		if (pgn->flags.remake) {
    600 			Var_Set(pgn, IMPSRC, cname);
    601 			if (cpref != NULL)
    602 				Var_Set(pgn, PREFIX, cpref);
    603 		}
    604 	}
    605 }
    606 
    607 /* See if a .ORDER rule stops us from building this node. */
    608 static bool
    609 IsWaitingForOrder(GNode *gn)
    610 {
    611 	GNodeListNode *ln;
    612 
    613 	for (ln = gn->order_pred.first; ln != NULL; ln = ln->next) {
    614 		GNode *ogn = ln->datum;
    615 
    616 		if (GNode_IsDone(ogn) || !ogn->flags.remake)
    617 			continue;
    618 
    619 		DEBUG2(MAKE,
    620 		    "IsWaitingForOrder: Waiting for .ORDER node \"%s%s\"\n",
    621 		    ogn->name, ogn->cohort_num);
    622 		return true;
    623 	}
    624 	return false;
    625 }
    626 
    627 static bool MakeBuildChild(GNode *, GNodeListNode *);
    628 
    629 static void
    630 ScheduleOrderSuccessors(GNode *gn)
    631 {
    632 	GNodeListNode *toBeMadeNext = toBeMade.first;
    633 	GNodeListNode *ln;
    634 
    635 	for (ln = gn->order_succ.first; ln != NULL; ln = ln->next) {
    636 		GNode *succ = ln->datum;
    637 
    638 		if (succ->made == DEFERRED &&
    639 		    !MakeBuildChild(succ, toBeMadeNext))
    640 			succ->flags.doneOrder = true;
    641 	}
    642 }
    643 
    644 /*
    645  * Perform update on the parents of a node. Used by JobFinish once
    646  * a node has been dealt with and by MakeStartJobs if it finds an
    647  * up-to-date node.
    648  *
    649  * The unmade field of pgn is decremented and pgn may be placed on
    650  * the toBeMade queue if this field becomes 0.
    651  *
    652  * If the child was made, the parent's flag CHILDMADE field will be
    653  * set true.
    654  *
    655  * If the child is not up-to-date and still does not exist,
    656  * set the FORCE flag on the parents.
    657  *
    658  * If the child wasn't made, the youngestChild field of the parent will be
    659  * altered if the child's mtime is big enough.
    660  *
    661  * Finally, if the child is the implied source for the parent, the
    662  * parent's IMPSRC variable is set appropriately.
    663  */
    664 void
    665 Make_Update(GNode *cgn)
    666 {
    667 	const char *cname;	/* the child's name */
    668 	time_t mtime = -1;
    669 	GNodeList *parents;
    670 	GNodeListNode *ln;
    671 	GNode *centurion;
    672 
    673 	/* It is save to re-examine any nodes again */
    674 	checked_seqno++;
    675 
    676 	cname = GNode_VarTarget(cgn);
    677 
    678 	DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
    679 
    680 	/*
    681 	 * If the child was actually made, see what its modification time is
    682 	 * now -- some rules won't actually update the file. If the file
    683 	 * still doesn't exist, make its mtime now.
    684 	 */
    685 	if (cgn->made != UPTODATE)
    686 		mtime = Make_Recheck(cgn);
    687 
    688 	/*
    689 	 * If this is a `::' node, we must consult its first instance
    690 	 * which is where all parents are linked.
    691 	 */
    692 	if ((centurion = cgn->centurion) != NULL) {
    693 		if (!Lst_IsEmpty(&cgn->parents))
    694 			Punt("%s%s: cohort has parents", cgn->name,
    695 			    cgn->cohort_num);
    696 		centurion->unmade_cohorts--;
    697 		if (centurion->unmade_cohorts < 0)
    698 			Error("Graph cycles through centurion %s",
    699 			    centurion->name);
    700 	} else {
    701 		centurion = cgn;
    702 	}
    703 	parents = &centurion->parents;
    704 
    705 	/* If this was a .ORDER node, schedule the RHS */
    706 	ScheduleOrderSuccessors(centurion);
    707 
    708 	/* Now mark all the parents as having one less unmade child */
    709 	for (ln = parents->first; ln != NULL; ln = ln->next) {
    710 		GNode *pgn = ln->datum;
    711 
    712 		if (DEBUG(MAKE)) {
    713 			debug_printf("inspect parent %s%s: ", pgn->name,
    714 			    pgn->cohort_num);
    715 			GNode_FprintDetails(opts.debug_file, "", pgn, "");
    716 			debug_printf(", unmade %d ", pgn->unmade - 1);
    717 		}
    718 
    719 		if (!pgn->flags.remake) {
    720 			/* This parent isn't needed */
    721 			DEBUG0(MAKE, "- not needed\n");
    722 			continue;
    723 		}
    724 		if (mtime == 0 && !(cgn->type & OP_WAIT))
    725 			pgn->flags.force = true;
    726 
    727 		/*
    728 		 * If the parent has the .MADE attribute, its timestamp got
    729 		 * updated to that of its newest child, and its unmade
    730 		 * child count got set to zero in Make_ExpandUse().
    731 		 * However other things might cause us to build one of its
    732 		 * children - and so we mustn't do any processing here when
    733 		 * the child build finishes.
    734 		 */
    735 		if (pgn->type & OP_MADE) {
    736 			DEBUG0(MAKE, "- .MADE\n");
    737 			continue;
    738 		}
    739 
    740 		if (!(cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE))) {
    741 			if (cgn->made == MADE)
    742 				pgn->flags.childMade = true;
    743 			GNode_UpdateYoungestChild(pgn, cgn);
    744 		}
    745 
    746 		/*
    747 		 * A parent must wait for the completion of all instances
    748 		 * of a `::' dependency.
    749 		 */
    750 		if (centurion->unmade_cohorts != 0 ||
    751 		    !GNode_IsDone(centurion)) {
    752 			DEBUG2(MAKE,
    753 			    "- centurion made %d, %d unmade cohorts\n",
    754 			    centurion->made, centurion->unmade_cohorts);
    755 			continue;
    756 		}
    757 
    758 		/* One more child of this parent is now made */
    759 		pgn->unmade--;
    760 		if (pgn->unmade < 0) {
    761 			if (DEBUG(MAKE)) {
    762 				debug_printf("Graph cycles through %s%s\n",
    763 				    pgn->name, pgn->cohort_num);
    764 				Targ_PrintGraph(2);
    765 			}
    766 			Error("Graph cycles through %s%s", pgn->name,
    767 			    pgn->cohort_num);
    768 		}
    769 
    770 		/*
    771 		 * We must always rescan the parents of .WAIT and .ORDER
    772 		 * nodes.
    773 		 */
    774 		if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
    775 		    && !centurion->flags.doneOrder) {
    776 			DEBUG0(MAKE, "- unmade children\n");
    777 			continue;
    778 		}
    779 		if (pgn->made != DEFERRED) {
    780 			/*
    781 			 * Either this parent is on a different branch of
    782 			 * the tree, or it on the RHS of a .WAIT directive
    783 			 * or it is already on the toBeMade list.
    784 			 */
    785 			DEBUG0(MAKE, "- not deferred\n");
    786 			continue;
    787 		}
    788 
    789 		if (IsWaitingForOrder(pgn))
    790 			continue;
    791 
    792 		if (DEBUG(MAKE)) {
    793 			debug_printf("- %s%s made, schedule %s%s (made %d)\n",
    794 			    cgn->name, cgn->cohort_num,
    795 			    pgn->name, pgn->cohort_num, pgn->made);
    796 			Targ_PrintNode(pgn, 2);
    797 		}
    798 		/* Ok, we can schedule the parent again */
    799 		pgn->made = REQUESTED;
    800 		Lst_Enqueue(&toBeMade, pgn);
    801 	}
    802 
    803 	UpdateImplicitParentsVars(cgn, cname);
    804 }
    805 
    806 static void
    807 UnmarkChildren(GNode *gn)
    808 {
    809 	GNodeListNode *ln;
    810 
    811 	for (ln = gn->children.first; ln != NULL; ln = ln->next) {
    812 		GNode *child = ln->datum;
    813 		child->type &= (unsigned)~OP_MARK;
    814 	}
    815 }
    816 
    817 /*
    818  * Add a child's name to the ALLSRC and OODATE variables of the given
    819  * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
    820  * attributes. .EXEC and .USE children are very rarely going to be files,
    821  * so...
    822  *
    823  * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
    824  *
    825  * A child is added to the OODATE variable if its modification time is
    826  * later than that of its parent, as defined by Make, except if the
    827  * parent is a .JOIN node. In that case, it is only added to the OODATE
    828  * variable if it was actually made (since .JOIN nodes don't have
    829  * modification times, the comparison is rather unfair...)..
    830  *
    831  * Input:
    832  *	cgn		The child to add
    833  *	pgn		The parent to whose ALLSRC variable it should
    834  *			be added
    835  */
    836 static void
    837 MakeAddAllSrc(GNode *cgn, GNode *pgn)
    838 {
    839 	const char *child, *allsrc;
    840 
    841 	if (cgn->type & OP_MARK)
    842 		return;
    843 	cgn->type |= OP_MARK;
    844 
    845 	if (cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE | OP_INVISIBLE))
    846 		return;
    847 
    848 	if (cgn->type & OP_ARCHV)
    849 		child = GNode_VarMember(cgn);
    850 	else
    851 		child = GNode_Path(cgn);
    852 
    853 	if (cgn->type & OP_JOIN)
    854 		allsrc = GNode_VarAllsrc(cgn);
    855 	else
    856 		allsrc = child;
    857 
    858 	if (allsrc != NULL)
    859 		Var_Append(pgn, ALLSRC, allsrc);
    860 
    861 	if (pgn->type & OP_JOIN) {
    862 		if (cgn->made == MADE)
    863 			Var_Append(pgn, OODATE, child);
    864 
    865 	} else if ((pgn->mtime < cgn->mtime) ||
    866 		   (cgn->mtime >= now && cgn->made == MADE)) {
    867 		/*
    868 		 * It goes in the OODATE variable if the parent is
    869 		 * younger than the child or if the child has been
    870 		 * modified more recently than the start of the make.
    871 		 * This is to keep pmake from getting confused if
    872 		 * something else updates the parent after the make
    873 		 * starts (shouldn't happen, I know, but sometimes it
    874 		 * does). In such a case, if we've updated the child,
    875 		 * the parent is likely to have a modification time
    876 		 * later than that of the child and anything that
    877 		 * relies on the OODATE variable will be hosed.
    878 		 *
    879 		 * XXX: This will cause all made children to go in
    880 		 * the OODATE variable, even if they're not touched,
    881 		 * if RECHECK isn't defined, since cgn->mtime is set
    882 		 * to now in Make_Update. According to some people,
    883 		 * this is good...
    884 		 */
    885 		Var_Append(pgn, OODATE, child);
    886 	}
    887 }
    888 
    889 /*
    890  * Set up the ALLSRC and OODATE variables. Sad to say, it must be
    891  * done separately, rather than while traversing the graph. This is
    892  * because Make defined OODATE to contain all sources whose modification
    893  * times were later than that of the target, *not* those sources that
    894  * were out-of-date. Since in both compatibility and native modes,
    895  * the modification time of the parent isn't found until the child
    896  * has been dealt with, we have to wait until now to fill in the
    897  * variable. As for ALLSRC, the ordering is important and not
    898  * guaranteed when in native mode, so it must be set here, too.
    899  *
    900  * If the node is a .JOIN node, its TARGET variable will be set to
    901  * match its ALLSRC variable.
    902  */
    903 void
    904 GNode_SetLocalVars(GNode *gn)
    905 {
    906 	GNodeListNode *ln;
    907 
    908 	if (gn->flags.doneAllsrc)
    909 		return;
    910 
    911 	UnmarkChildren(gn);
    912 	for (ln = gn->children.first; ln != NULL; ln = ln->next)
    913 		MakeAddAllSrc(ln->datum, gn);
    914 
    915 	if (!Var_Exists(gn, OODATE))
    916 		Var_Set(gn, OODATE, "");
    917 	if (!Var_Exists(gn, ALLSRC))
    918 		Var_Set(gn, ALLSRC, "");
    919 
    920 	if (gn->type & OP_JOIN)
    921 		Var_Set(gn, TARGET, GNode_VarAllsrc(gn));
    922 	gn->flags.doneAllsrc = true;
    923 }
    924 
    925 static void
    926 ScheduleRandomly(GNode *gn)
    927 {
    928 	GNodeListNode *ln;
    929 	size_t i, n;
    930 
    931 	n = 0;
    932 	for (ln = toBeMade.first; ln != NULL; ln = ln->next)
    933 		n++;
    934 	i = n > 0 ? (size_t)random() % (n + 1) : 0;
    935 
    936 	if (i == 0) {
    937 		Lst_Append(&toBeMade, gn);
    938 		return;
    939 	}
    940 	i--;
    941 
    942 	for (ln = toBeMade.first; i > 0; ln = ln->next)
    943 		i--;
    944 	Lst_InsertBefore(&toBeMade, ln, gn);
    945 }
    946 
    947 static bool
    948 MakeBuildChild(GNode *cn, GNodeListNode *toBeMadeNext)
    949 {
    950 
    951 	if (DEBUG(MAKE)) {
    952 		debug_printf("MakeBuildChild: inspect %s%s, ",
    953 		    cn->name, cn->cohort_num);
    954 		GNode_FprintDetails(opts.debug_file, "", cn, "\n");
    955 	}
    956 	if (GNode_IsReady(cn))
    957 		return false;
    958 
    959 	/* If this node is on the RHS of a .ORDER, check LHSs. */
    960 	if (IsWaitingForOrder(cn)) {
    961 		/*
    962 		 * Can't build this (or anything else in this child list) yet
    963 		 */
    964 		cn->made = DEFERRED;
    965 		return false;	/* but keep looking */
    966 	}
    967 
    968 	DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n",
    969 	    cn->name, cn->cohort_num);
    970 
    971 	cn->made = REQUESTED;
    972 	if (opts.randomizeTargets && !(cn->type & OP_WAIT))
    973 		ScheduleRandomly(cn);
    974 	else if (toBeMadeNext == NULL)
    975 		Lst_Append(&toBeMade, cn);
    976 	else
    977 		Lst_InsertBefore(&toBeMade, toBeMadeNext, cn);
    978 
    979 	if (cn->unmade_cohorts != 0) {
    980 		ListNode *ln;
    981 
    982 		for (ln = cn->cohorts.first; ln != NULL; ln = ln->next)
    983 			if (MakeBuildChild(ln->datum, toBeMadeNext))
    984 				break;
    985 	}
    986 
    987 	/*
    988 	 * If this node is a .WAIT node with unmade children
    989 	 * then don't add the next sibling.
    990 	 */
    991 	return cn->type & OP_WAIT && cn->unmade > 0;
    992 }
    993 
    994 static void
    995 MakeChildren(GNode *gn)
    996 {
    997 	GNodeListNode *toBeMadeNext = toBeMade.first;
    998 	GNodeListNode *ln;
    999 
   1000 	for (ln = gn->children.first; ln != NULL; ln = ln->next)
   1001 		if (MakeBuildChild(ln->datum, toBeMadeNext))
   1002 			break;
   1003 }
   1004 
   1005 /*
   1006  * Start as many jobs as possible, taking them from the toBeMade queue.
   1007  *
   1008  * If the -q option was given, no job will be started,
   1009  * but as soon as an out-of-date target is found, this function
   1010  * returns true. In all other cases, this function returns false.
   1011  */
   1012 static bool
   1013 MakeStartJobs(void)
   1014 {
   1015 	GNode *gn;
   1016 	bool have_token = false;
   1017 
   1018 	while (!Lst_IsEmpty(&toBeMade)) {
   1019 		/*
   1020 		 * Get token now to avoid cycling job-list when we only
   1021 		 * have 1 token
   1022 		 */
   1023 		if (!have_token && !TokenPool_Take())
   1024 			break;
   1025 		have_token = true;
   1026 
   1027 		gn = Lst_Dequeue(&toBeMade);
   1028 		DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
   1029 
   1030 		if (gn->made != REQUESTED) {
   1031 			debug_printf("internal error: made = %s\n",
   1032 			    GNodeMade_Name(gn->made));
   1033 			Targ_PrintNode(gn, 2);
   1034 			Targ_PrintNodes(&toBeMade, 2);
   1035 			Targ_PrintGraph(3);
   1036 			abort();
   1037 		}
   1038 
   1039 		if (gn->checked_seqno == checked_seqno) {
   1040 			/*
   1041 			 * We've already looked at this node since a job
   1042 			 * finished...
   1043 			 */
   1044 			DEBUG2(MAKE, "already checked %s%s\n",
   1045 			    gn->name, gn->cohort_num);
   1046 			gn->made = DEFERRED;
   1047 			continue;
   1048 		}
   1049 		gn->checked_seqno = checked_seqno;
   1050 
   1051 		if (gn->unmade != 0) {
   1052 			gn->made = DEFERRED;
   1053 			MakeChildren(gn);
   1054 			DEBUG2(MAKE, "deferred %s%s\n",
   1055 			    gn->name, gn->cohort_num);
   1056 			continue;
   1057 		}
   1058 
   1059 		gn->made = BEINGMADE;
   1060 		if (GNode_IsOODate(gn)) {
   1061 			DEBUG0(MAKE, "out-of-date\n");
   1062 			if (opts.query)
   1063 				return strcmp(gn->name, ".MAIN") != 0;
   1064 			GNode_SetLocalVars(gn);
   1065 			Job_Make(gn);
   1066 			have_token = false;
   1067 		} else {
   1068 			DEBUG0(MAKE, "up-to-date\n");
   1069 			gn->made = UPTODATE;
   1070 			if (gn->type & OP_JOIN) {
   1071 				/*
   1072 				 * Even for an up-to-date .JOIN node, we
   1073 				 * need it to have its local variables so
   1074 				 * references to it get the correct value
   1075 				 * for .TARGET when building up the local
   1076 				 * variables of its parent(s)...
   1077 				 */
   1078 				GNode_SetLocalVars(gn);
   1079 			}
   1080 			Make_Update(gn);
   1081 		}
   1082 	}
   1083 
   1084 	if (have_token)
   1085 		TokenPool_Return();
   1086 
   1087 	return false;
   1088 }
   1089 
   1090 /* Print the status of a .ORDER node. */
   1091 static void
   1092 MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
   1093 {
   1094 	if (!GNode_IsWaitingFor(ogn))
   1095 		return;
   1096 
   1097 	printf("    `%s%s' has .ORDER dependency on %s%s ",
   1098 	    gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
   1099 	GNode_FprintDetails(stdout, "(", ogn, ")\n");
   1100 
   1101 	if (DEBUG(MAKE) && opts.debug_file != stdout) {
   1102 		debug_printf("    `%s%s' has .ORDER dependency on %s%s ",
   1103 		    gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
   1104 		GNode_FprintDetails(opts.debug_file, "(", ogn, ")\n");
   1105 	}
   1106 }
   1107 
   1108 static void
   1109 MakePrintStatusOrder(GNode *gn)
   1110 {
   1111 	GNodeListNode *ln;
   1112 	for (ln = gn->order_pred.first; ln != NULL; ln = ln->next)
   1113 		MakePrintStatusOrderNode(ln->datum, gn);
   1114 }
   1115 
   1116 static void MakePrintStatusList(GNodeList *, int *);
   1117 
   1118 /*
   1119  * Print the status of a top-level node, viz. it being up-to-date already
   1120  * or not created due to an error in a lower level.
   1121  */
   1122 static bool
   1123 MakePrintStatus(GNode *gn, int *errors)
   1124 {
   1125 	if (gn->flags.doneCycle) {
   1126 		/*
   1127 		 * We've completely processed this node before, don't do
   1128 		 * it again.
   1129 		 */
   1130 		return false;
   1131 	}
   1132 
   1133 	if (gn->unmade == 0) {
   1134 		gn->flags.doneCycle = true;
   1135 		switch (gn->made) {
   1136 		case UPTODATE:
   1137 			printf("`%s%s' is up to date.\n", gn->name,
   1138 			    gn->cohort_num);
   1139 			break;
   1140 		case MADE:
   1141 			break;
   1142 		case UNMADE:
   1143 		case DEFERRED:
   1144 		case REQUESTED:
   1145 		case BEINGMADE:
   1146 			(*errors)++;
   1147 			printf("`%s%s' was not built", gn->name,
   1148 			    gn->cohort_num);
   1149 			GNode_FprintDetails(stdout, " (", gn, ")!\n");
   1150 			if (DEBUG(MAKE) && opts.debug_file != stdout) {
   1151 				debug_printf("`%s%s' was not built", gn->name,
   1152 				    gn->cohort_num);
   1153 				GNode_FprintDetails(opts.debug_file, " (", gn,
   1154 				    ")!\n");
   1155 			}
   1156 			/* Most likely problem is actually caused by .ORDER */
   1157 			MakePrintStatusOrder(gn);
   1158 			break;
   1159 		default:
   1160 			/* Errors - already counted */
   1161 			printf("`%s%s' not remade because of errors.\n",
   1162 			    gn->name, gn->cohort_num);
   1163 			if (DEBUG(MAKE) && opts.debug_file != stdout)
   1164 				debug_printf(
   1165 				    "`%s%s' not remade because of errors.\n",
   1166 				    gn->name, gn->cohort_num);
   1167 			break;
   1168 		}
   1169 		return false;
   1170 	}
   1171 
   1172 	DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
   1173 	    gn->name, gn->cohort_num, gn->unmade);
   1174 	/*
   1175 	 * If printing cycles and came to one that has unmade children,
   1176 	 * print out the cycle by recursing on its children.
   1177 	 */
   1178 	if (!gn->flags.cycle) {
   1179 		/* First time we've seen this node, check all children */
   1180 		gn->flags.cycle = true;
   1181 		MakePrintStatusList(&gn->children, errors);
   1182 		/* Mark that this node needn't be processed again */
   1183 		gn->flags.doneCycle = true;
   1184 		return false;
   1185 	}
   1186 
   1187 	/* Only output the error once per node */
   1188 	gn->flags.doneCycle = true;
   1189 	Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
   1190 	if ((*errors)++ > 100)
   1191 		/* Abandon the whole error report */
   1192 		return true;
   1193 
   1194 	/* Reporting for our children will give the rest of the loop */
   1195 	MakePrintStatusList(&gn->children, errors);
   1196 	return false;
   1197 }
   1198 
   1199 static void
   1200 MakePrintStatusList(GNodeList *gnodes, int *errors)
   1201 {
   1202 	GNodeListNode *ln;
   1203 
   1204 	for (ln = gnodes->first; ln != NULL; ln = ln->next)
   1205 		if (MakePrintStatus(ln->datum, errors))
   1206 			break;
   1207 }
   1208 
   1209 static void
   1210 ExamineLater(GNodeList *examine, GNodeList *toBeExamined)
   1211 {
   1212 	GNodeListNode *ln;
   1213 
   1214 	for (ln = toBeExamined->first; ln != NULL; ln = ln->next) {
   1215 		GNode *gn = ln->datum;
   1216 
   1217 		if (gn->flags.remake)
   1218 			continue;
   1219 		if (gn->type & (OP_USE | OP_USEBEFORE))
   1220 			continue;
   1221 
   1222 		DEBUG2(MAKE, "ExamineLater: need to examine \"%s%s\"\n",
   1223 		    gn->name, gn->cohort_num);
   1224 		Lst_Enqueue(examine, gn);
   1225 	}
   1226 }
   1227 
   1228 /* Expand .USE nodes and create a new targets list. */
   1229 void
   1230 Make_ExpandUse(GNodeList *targets)
   1231 {
   1232 	GNodeList examine = LST_INIT;	/* Queue of targets to examine */
   1233 	Lst_AppendAll(&examine, targets);
   1234 
   1235 	/*
   1236 	 * Make an initial downward pass over the graph, marking nodes to
   1237 	 * be made as we go down.
   1238 	 *
   1239 	 * We call Suff_FindDeps to find where a node is and to get some
   1240 	 * children for it if it has none and also has no commands. If the
   1241 	 * node is a leaf, we stick it on the toBeMade queue to be looked
   1242 	 * at in a minute, otherwise we add its children to our queue and
   1243 	 * go on.
   1244 	 */
   1245 	while (!Lst_IsEmpty(&examine)) {
   1246 		GNode *gn = Lst_Dequeue(&examine);
   1247 
   1248 		if (gn->flags.remake)
   1249 			continue;
   1250 		gn->flags.remake = true;
   1251 
   1252 		DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
   1253 		    gn->name, gn->cohort_num);
   1254 
   1255 		if (gn->type & OP_DOUBLEDEP)
   1256 			Lst_PrependAll(&examine, &gn->cohorts);
   1257 
   1258 		/*
   1259 		 * Apply any .USE rules before looking for implicit
   1260 		 * dependencies to make sure everything has commands that
   1261 		 * should.
   1262 		 *
   1263 		 * Make sure that the TARGET is set, so that we can make
   1264 		 * expansions.
   1265 		 */
   1266 		if (gn->type & OP_ARCHV) {
   1267 			char *eoa = strchr(gn->name, '(');
   1268 			char *eon = strchr(gn->name, ')');
   1269 			if (eoa == NULL || eon == NULL)
   1270 				continue;
   1271 			*eoa = '\0';
   1272 			*eon = '\0';
   1273 			Var_Set(gn, MEMBER, eoa + 1);
   1274 			Var_Set(gn, ARCHIVE, gn->name);
   1275 			*eoa = '(';
   1276 			*eon = ')';
   1277 		}
   1278 
   1279 		Dir_UpdateMTime(gn, false);
   1280 		Var_Set(gn, TARGET, GNode_Path(gn));
   1281 		UnmarkChildren(gn);
   1282 		HandleUseNodes(gn);
   1283 
   1284 		if (!(gn->type & OP_MADE))
   1285 			Suff_FindDeps(gn);
   1286 		else {
   1287 			PretendAllChildrenAreMade(gn);
   1288 			if (gn->unmade != 0) {
   1289 				printf(
   1290 				    "Warning: "
   1291 				    "%s%s still has %d unmade children\n",
   1292 				    gn->name, gn->cohort_num, gn->unmade);
   1293 			}
   1294 		}
   1295 
   1296 		if (gn->unmade != 0)
   1297 			ExamineLater(&examine, &gn->children);
   1298 	}
   1299 
   1300 	Lst_Done(&examine);
   1301 }
   1302 
   1303 /* Make the .WAIT node depend on the previous children */
   1304 static void
   1305 AddWaitDependency(GNodeListNode *prevWaitNode, GNode *waitNode)
   1306 {
   1307 	GNodeListNode *ln;
   1308 
   1309 	for (ln = prevWaitNode; ln->datum != waitNode; ln = ln->next) {
   1310 		GNode *gn = ln->datum;
   1311 		DEBUG3(MAKE, ".WAIT: add dependency \"%s: %s%s\"\n",
   1312 		    waitNode->name, gn->name, gn->cohort_num);
   1313 		Lst_Append(&waitNode->children, gn);
   1314 		Lst_Append(&gn->parents, waitNode);
   1315 		waitNode->unmade++;
   1316 	}
   1317 }
   1318 
   1319 /* Convert .WAIT nodes into dependencies. */
   1320 static void
   1321 Make_ProcessWait(GNodeList *targets)
   1322 {
   1323 	GNode *pgn;		/* 'parent' node we are examining */
   1324 	GNodeList examine;
   1325 
   1326 	/*
   1327 	 * We need all the nodes to have a common parent in order for the
   1328 	 * .WAIT and .ORDER scheduling to work.
   1329 	 * Perhaps this should be done earlier...
   1330 	 */
   1331 
   1332 	pgn = GNode_New(".MAIN");
   1333 	pgn->flags.remake = true;
   1334 	pgn->type = OP_PHONY | OP_DEPENDS;
   1335 	/* Get it displayed in the diag dumps */
   1336 	Lst_Prepend(Targ_List(), pgn);
   1337 
   1338 	{
   1339 		GNodeListNode *ln;
   1340 		for (ln = targets->first; ln != NULL; ln = ln->next) {
   1341 			GNode *cgn = ln->datum;
   1342 
   1343 			Lst_Append(&pgn->children, cgn);
   1344 			Lst_Append(&cgn->parents, pgn);
   1345 			pgn->unmade++;
   1346 		}
   1347 	}
   1348 
   1349 	/* Start building with the 'dummy' .MAIN' node */
   1350 	MakeBuildChild(pgn, NULL);
   1351 
   1352 	Lst_Init(&examine);
   1353 	Lst_Append(&examine, pgn);
   1354 
   1355 	while (!Lst_IsEmpty(&examine)) {
   1356 		GNodeListNode *waitNode, *ln;
   1357 
   1358 		pgn = Lst_Dequeue(&examine);
   1359 
   1360 		/* We only want to process each child-list once */
   1361 		if (pgn->flags.doneWait)
   1362 			continue;
   1363 		pgn->flags.doneWait = true;
   1364 		DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
   1365 
   1366 		if (pgn->type & OP_DOUBLEDEP)
   1367 			Lst_PrependAll(&examine, &pgn->cohorts);
   1368 
   1369 		waitNode = pgn->children.first;
   1370 		for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
   1371 			GNode *cgn = ln->datum;
   1372 			if (cgn->type & OP_WAIT) {
   1373 				AddWaitDependency(waitNode, cgn);
   1374 				waitNode = ln;
   1375 			} else
   1376 				Lst_Append(&examine, cgn);
   1377 		}
   1378 	}
   1379 
   1380 	Lst_Done(&examine);
   1381 }
   1382 
   1383 bool
   1384 Make_MakeParallel(GNodeList *targets)
   1385 {
   1386 	int errors;		/* Number of errors the Job module reports */
   1387 
   1388 	Lst_Init(&toBeMade);
   1389 
   1390 	Make_ExpandUse(targets);
   1391 	Make_ProcessWait(targets);
   1392 
   1393 	if (DEBUG(MAKE)) {
   1394 		debug_printf("#***# full graph\n");
   1395 		Targ_PrintGraph(1);
   1396 	}
   1397 
   1398 	if (opts.query)
   1399 		return MakeStartJobs();
   1400 
   1401 	(void)MakeStartJobs();
   1402 	while (!Lst_IsEmpty(&toBeMade) || jobTokensRunning > 0) {
   1403 		Job_CatchOutput();
   1404 		(void)MakeStartJobs();
   1405 	}
   1406 
   1407 	errors = Job_MakeDotEnd();
   1408 
   1409 	DEBUG1(MAKE, "done: errors %d\n", errors);
   1410 	if (errors == 0) {
   1411 		MakePrintStatusList(targets, &errors);
   1412 		if (DEBUG(MAKE)) {
   1413 			debug_printf("done: errors %d\n", errors);
   1414 			if (errors > 0)
   1415 				Targ_PrintGraph(4);
   1416 		}
   1417 	}
   1418 	return errors > 0;
   1419 }
   1420