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