xref: /freebsd/contrib/bmake/make.c (revision 129043849f62f9cfa72f6fae68417d9995860f3f)
1*12904384SSimon J. Gerraty /*	$NetBSD: make.c,v 1.248 2021/11/28 23:12:51 rillig Exp $	*/
23955d011SMarcel Moolenaar 
33955d011SMarcel Moolenaar /*
43955d011SMarcel Moolenaar  * Copyright (c) 1988, 1989, 1990, 1993
53955d011SMarcel Moolenaar  *	The Regents of the University of California.  All rights reserved.
63955d011SMarcel Moolenaar  *
73955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
83955d011SMarcel Moolenaar  * Adam de Boor.
93955d011SMarcel Moolenaar  *
103955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
113955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
123955d011SMarcel Moolenaar  * are met:
133955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
143955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
153955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
163955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
173955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
183955d011SMarcel Moolenaar  * 3. Neither the name of the University nor the names of its contributors
193955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
203955d011SMarcel Moolenaar  *    without specific prior written permission.
213955d011SMarcel Moolenaar  *
223955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323955d011SMarcel Moolenaar  * SUCH DAMAGE.
333955d011SMarcel Moolenaar  */
343955d011SMarcel Moolenaar 
353955d011SMarcel Moolenaar /*
363955d011SMarcel Moolenaar  * Copyright (c) 1989 by Berkeley Softworks
373955d011SMarcel Moolenaar  * All rights reserved.
383955d011SMarcel Moolenaar  *
393955d011SMarcel Moolenaar  * This code is derived from software contributed to Berkeley by
403955d011SMarcel Moolenaar  * Adam de Boor.
413955d011SMarcel Moolenaar  *
423955d011SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
433955d011SMarcel Moolenaar  * modification, are permitted provided that the following conditions
443955d011SMarcel Moolenaar  * are met:
453955d011SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
463955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
473955d011SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
483955d011SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
493955d011SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
503955d011SMarcel Moolenaar  * 3. All advertising materials mentioning features or use of this software
513955d011SMarcel Moolenaar  *    must display the following acknowledgement:
523955d011SMarcel Moolenaar  *	This product includes software developed by the University of
533955d011SMarcel Moolenaar  *	California, Berkeley and its contributors.
543955d011SMarcel Moolenaar  * 4. Neither the name of the University nor the names of its contributors
553955d011SMarcel Moolenaar  *    may be used to endorse or promote products derived from this software
563955d011SMarcel Moolenaar  *    without specific prior written permission.
573955d011SMarcel Moolenaar  *
583955d011SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
593955d011SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
603955d011SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
613955d011SMarcel Moolenaar  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
623955d011SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
633955d011SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
643955d011SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
653955d011SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
663955d011SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
673955d011SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
683955d011SMarcel Moolenaar  * SUCH DAMAGE.
693955d011SMarcel Moolenaar  */
703955d011SMarcel Moolenaar 
7106b9b3e0SSimon J. Gerraty /*
7206b9b3e0SSimon J. Gerraty  * Examination of targets and their suitability for creation.
733955d011SMarcel Moolenaar  *
743955d011SMarcel Moolenaar  * Interface:
75b0c40a00SSimon J. Gerraty  *	Make_Run	Initialize things for the module. Returns true if
76e2eeea75SSimon J. Gerraty  *			work was (or would have been) done.
773955d011SMarcel Moolenaar  *
78e2eeea75SSimon J. Gerraty  *	Make_Update	After a target is made, update all its parents.
79e2eeea75SSimon J. Gerraty  *			Perform various bookkeeping chores like the updating
80956e45f6SSimon J. Gerraty  *			of the youngestChild field of the parent, filling
81dba7b0efSSimon J. Gerraty  *			of the IMPSRC variable, etc. Place the parent on the
82dba7b0efSSimon J. Gerraty  *			toBeMade queue if it should be.
833955d011SMarcel Moolenaar  *
84e2eeea75SSimon J. Gerraty  *	GNode_UpdateYoungestChild
85e2eeea75SSimon J. Gerraty  *			Update the node's youngestChild field based on the
86e2eeea75SSimon J. Gerraty  *			child's modification time.
873955d011SMarcel Moolenaar  *
88b0c40a00SSimon J. Gerraty  *	GNode_SetLocalVars
89b0c40a00SSimon J. Gerraty  *			Set up the various local variables for a
903955d011SMarcel Moolenaar  *			target, including the .ALLSRC variable, making
913955d011SMarcel Moolenaar  *			sure that any variable that needs to exist
923955d011SMarcel Moolenaar  *			at the very least has the empty value.
933955d011SMarcel Moolenaar  *
94e2eeea75SSimon J. Gerraty  *	GNode_IsOODate	Determine if a target is out-of-date.
953955d011SMarcel Moolenaar  *
963955d011SMarcel Moolenaar  *	Make_HandleUse	See if a child is a .USE node for a parent
973955d011SMarcel Moolenaar  *			and perform the .USE actions if so.
983955d011SMarcel Moolenaar  *
993955d011SMarcel Moolenaar  *	Make_ExpandUse	Expand .USE nodes
1003955d011SMarcel Moolenaar  */
1013955d011SMarcel Moolenaar 
1023955d011SMarcel Moolenaar #include "make.h"
1033955d011SMarcel Moolenaar #include "dir.h"
1043955d011SMarcel Moolenaar #include "job.h"
1053955d011SMarcel Moolenaar 
106956e45f6SSimon J. Gerraty /*	"@(#)make.c	8.1 (Berkeley) 6/6/93"	*/
107*12904384SSimon J. Gerraty MAKE_RCSID("$NetBSD: make.c,v 1.248 2021/11/28 23:12:51 rillig Exp $");
1083955d011SMarcel Moolenaar 
109956e45f6SSimon J. Gerraty /* Sequence # to detect recursion. */
110e2eeea75SSimon J. Gerraty static unsigned int checked_seqno = 1;
111956e45f6SSimon J. Gerraty 
11206b9b3e0SSimon J. Gerraty /*
11306b9b3e0SSimon J. Gerraty  * The current fringe of the graph.
114956e45f6SSimon J. Gerraty  * These are nodes which await examination by MakeOODate.
11506b9b3e0SSimon J. Gerraty  * It is added to by Make_Update and subtracted from by MakeStartJobs
11606b9b3e0SSimon J. Gerraty  */
11706b9b3e0SSimon J. Gerraty static GNodeList toBeMade = LST_INIT;
118956e45f6SSimon J. Gerraty 
1193955d011SMarcel Moolenaar 
120956e45f6SSimon J. Gerraty void
121956e45f6SSimon J. Gerraty debug_printf(const char *fmt, ...)
122956e45f6SSimon J. Gerraty {
123956e45f6SSimon J. Gerraty 	va_list args;
124956e45f6SSimon J. Gerraty 
125956e45f6SSimon J. Gerraty 	va_start(args, fmt);
126956e45f6SSimon J. Gerraty 	vfprintf(opts.debug_file, fmt, args);
127956e45f6SSimon J. Gerraty 	va_end(args);
128956e45f6SSimon J. Gerraty }
129956e45f6SSimon J. Gerraty 
1303955d011SMarcel Moolenaar MAKE_ATTR_DEAD static void
13106b9b3e0SSimon J. Gerraty make_abort(GNode *gn, int lineno)
1323955d011SMarcel Moolenaar {
13306b9b3e0SSimon J. Gerraty 
13406b9b3e0SSimon J. Gerraty 	debug_printf("make_abort from line %d\n", lineno);
135956e45f6SSimon J. Gerraty 	Targ_PrintNode(gn, 2);
13606b9b3e0SSimon J. Gerraty 	Targ_PrintNodes(&toBeMade, 2);
1373955d011SMarcel Moolenaar 	Targ_PrintGraph(3);
1383955d011SMarcel Moolenaar 	abort();
1393955d011SMarcel Moolenaar }
1403955d011SMarcel Moolenaar 
141*12904384SSimon J. Gerraty static const char *
142*12904384SSimon J. Gerraty GNodeType_ToString(GNodeType type, void **freeIt)
143*12904384SSimon J. Gerraty {
144*12904384SSimon J. Gerraty 	Buffer buf;
1452c3632d1SSimon J. Gerraty 
146*12904384SSimon J. Gerraty 	Buf_InitSize(&buf, 32);
147*12904384SSimon J. Gerraty #define ADD(flag) Buf_AddFlag(&buf, (type & (flag)) != OP_NONE, #flag)
148*12904384SSimon J. Gerraty 	ADD(OP_DEPENDS);
149*12904384SSimon J. Gerraty 	ADD(OP_FORCE);
150*12904384SSimon J. Gerraty 	ADD(OP_DOUBLEDEP);
151*12904384SSimon J. Gerraty 	ADD(OP_OPTIONAL);
152*12904384SSimon J. Gerraty 	ADD(OP_USE);
153*12904384SSimon J. Gerraty 	ADD(OP_EXEC);
154*12904384SSimon J. Gerraty 	ADD(OP_IGNORE);
155*12904384SSimon J. Gerraty 	ADD(OP_PRECIOUS);
156*12904384SSimon J. Gerraty 	ADD(OP_SILENT);
157*12904384SSimon J. Gerraty 	ADD(OP_MAKE);
158*12904384SSimon J. Gerraty 	ADD(OP_JOIN);
159*12904384SSimon J. Gerraty 	ADD(OP_MADE);
160*12904384SSimon J. Gerraty 	ADD(OP_SPECIAL);
161*12904384SSimon J. Gerraty 	ADD(OP_USEBEFORE);
162*12904384SSimon J. Gerraty 	ADD(OP_INVISIBLE);
163*12904384SSimon J. Gerraty 	ADD(OP_NOTMAIN);
164*12904384SSimon J. Gerraty 	ADD(OP_PHONY);
165*12904384SSimon J. Gerraty 	ADD(OP_NOPATH);
166*12904384SSimon J. Gerraty 	ADD(OP_WAIT);
167*12904384SSimon J. Gerraty 	ADD(OP_NOMETA);
168*12904384SSimon J. Gerraty 	ADD(OP_META);
169*12904384SSimon J. Gerraty 	ADD(OP_NOMETA_CMP);
170*12904384SSimon J. Gerraty 	ADD(OP_SUBMAKE);
171*12904384SSimon J. Gerraty 	ADD(OP_TRANSFORM);
172*12904384SSimon J. Gerraty 	ADD(OP_MEMBER);
173*12904384SSimon J. Gerraty 	ADD(OP_LIB);
174*12904384SSimon J. Gerraty 	ADD(OP_ARCHV);
175*12904384SSimon J. Gerraty 	ADD(OP_HAS_COMMANDS);
176*12904384SSimon J. Gerraty 	ADD(OP_SAVE_CMDS);
177*12904384SSimon J. Gerraty 	ADD(OP_DEPS_FOUND);
178*12904384SSimon J. Gerraty 	ADD(OP_MARK);
179*12904384SSimon J. Gerraty #undef ADD
180*12904384SSimon J. Gerraty 	return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
181*12904384SSimon J. Gerraty }
182*12904384SSimon J. Gerraty 
183*12904384SSimon J. Gerraty static const char *
184*12904384SSimon J. Gerraty GNodeFlags_ToString(GNodeFlags flags, void **freeIt)
185*12904384SSimon J. Gerraty {
186*12904384SSimon J. Gerraty 	Buffer buf;
187*12904384SSimon J. Gerraty 
188*12904384SSimon J. Gerraty 	Buf_InitSize(&buf, 32);
189*12904384SSimon J. Gerraty #define ADD(flag, name) Buf_AddFlag(&buf, flags.flag, name)
190*12904384SSimon J. Gerraty 	ADD(remake, "REMAKE");
191*12904384SSimon J. Gerraty 	ADD(childMade, "CHILDMADE");
192*12904384SSimon J. Gerraty 	ADD(force, "FORCE");
193*12904384SSimon J. Gerraty 	ADD(doneWait, "DONE_WAIT");
194*12904384SSimon J. Gerraty 	ADD(doneOrder, "DONE_ORDER");
195*12904384SSimon J. Gerraty 	ADD(fromDepend, "FROM_DEPEND");
196*12904384SSimon J. Gerraty 	ADD(doneAllsrc, "DONE_ALLSRC");
197*12904384SSimon J. Gerraty 	ADD(cycle, "CYCLE");
198*12904384SSimon J. Gerraty 	ADD(doneCycle, "DONECYCLE");
199*12904384SSimon J. Gerraty #undef ADD
200*12904384SSimon J. Gerraty 	return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
201*12904384SSimon J. Gerraty }
2022c3632d1SSimon J. Gerraty 
2032c3632d1SSimon J. Gerraty void
2042c3632d1SSimon J. Gerraty GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
2052c3632d1SSimon J. Gerraty 		    const char *suffix)
2062c3632d1SSimon J. Gerraty {
207*12904384SSimon J. Gerraty 	void *type_freeIt = NULL;
208*12904384SSimon J. Gerraty 	void *flags_freeIt = NULL;
2092c3632d1SSimon J. Gerraty 
210dba7b0efSSimon J. Gerraty 	fprintf(f, "%s%s, type %s, flags %s%s",
2112c3632d1SSimon J. Gerraty 	    prefix,
212dba7b0efSSimon J. Gerraty 	    GNodeMade_Name(gn->made),
213*12904384SSimon J. Gerraty 	    GNodeType_ToString(gn->type, &type_freeIt),
214*12904384SSimon J. Gerraty 	    GNodeFlags_ToString(gn->flags, &flags_freeIt),
2152c3632d1SSimon J. Gerraty 	    suffix);
216*12904384SSimon J. Gerraty 	free(type_freeIt);
217*12904384SSimon J. Gerraty 	free(flags_freeIt);
2182c3632d1SSimon J. Gerraty }
2192c3632d1SSimon J. Gerraty 
220b0c40a00SSimon J. Gerraty bool
221956e45f6SSimon J. Gerraty GNode_ShouldExecute(GNode *gn)
222956e45f6SSimon J. Gerraty {
22306b9b3e0SSimon J. Gerraty 	return !((gn->type & OP_MAKE)
22406b9b3e0SSimon J. Gerraty 	    ? opts.noRecursiveExecute
22506b9b3e0SSimon J. Gerraty 	    : opts.noExecute);
226956e45f6SSimon J. Gerraty }
227956e45f6SSimon J. Gerraty 
228956e45f6SSimon J. Gerraty /* Update the youngest child of the node, according to the given child. */
229956e45f6SSimon J. Gerraty void
230e2eeea75SSimon J. Gerraty GNode_UpdateYoungestChild(GNode *gn, GNode *cgn)
2313955d011SMarcel Moolenaar {
232e2eeea75SSimon J. Gerraty 	if (gn->youngestChild == NULL || cgn->mtime > gn->youngestChild->mtime)
233e2eeea75SSimon J. Gerraty 		gn->youngestChild = cgn;
2343955d011SMarcel Moolenaar }
235e2eeea75SSimon J. Gerraty 
236b0c40a00SSimon J. Gerraty static bool
237e2eeea75SSimon J. Gerraty IsOODateRegular(GNode *gn)
238e2eeea75SSimon J. Gerraty {
239e2eeea75SSimon J. Gerraty 	/* These rules are inherited from the original Make. */
240e2eeea75SSimon J. Gerraty 
241e2eeea75SSimon J. Gerraty 	if (gn->youngestChild != NULL) {
242e2eeea75SSimon J. Gerraty 		if (gn->mtime < gn->youngestChild->mtime) {
243e2eeea75SSimon J. Gerraty 			DEBUG1(MAKE, "modified before source \"%s\"...",
244e2eeea75SSimon J. Gerraty 			    GNode_Path(gn->youngestChild));
245b0c40a00SSimon J. Gerraty 			return true;
246e2eeea75SSimon J. Gerraty 		}
247b0c40a00SSimon J. Gerraty 		return false;
248e2eeea75SSimon J. Gerraty 	}
249e2eeea75SSimon J. Gerraty 
250e2eeea75SSimon J. Gerraty 	if (gn->mtime == 0 && !(gn->type & OP_OPTIONAL)) {
25106b9b3e0SSimon J. Gerraty 		DEBUG0(MAKE, "nonexistent and no sources...");
252b0c40a00SSimon J. Gerraty 		return true;
253e2eeea75SSimon J. Gerraty 	}
254e2eeea75SSimon J. Gerraty 
255e2eeea75SSimon J. Gerraty 	if (gn->type & OP_DOUBLEDEP) {
256e2eeea75SSimon J. Gerraty 		DEBUG0(MAKE, ":: operator and no sources...");
257b0c40a00SSimon J. Gerraty 		return true;
258e2eeea75SSimon J. Gerraty 	}
259e2eeea75SSimon J. Gerraty 
260b0c40a00SSimon J. Gerraty 	return false;
2613955d011SMarcel Moolenaar }
2623955d011SMarcel Moolenaar 
26306b9b3e0SSimon J. Gerraty /*
26406b9b3e0SSimon J. Gerraty  * See if the node is out of date with respect to its sources.
2653955d011SMarcel Moolenaar  *
2663955d011SMarcel Moolenaar  * Used by Make_Run when deciding which nodes to place on the
267956e45f6SSimon J. Gerraty  * toBeMade queue initially and by Make_Update to screen out .USE and
268956e45f6SSimon J. Gerraty  * .EXEC nodes. In the latter case, however, any other sort of node
2693955d011SMarcel Moolenaar  * must be considered out-of-date since at least one of its children
2703955d011SMarcel Moolenaar  * will have been recreated.
2713955d011SMarcel Moolenaar  *
272956e45f6SSimon J. Gerraty  * The mtime field of the node and the youngestChild field of its parents
273956e45f6SSimon J. Gerraty  * may be changed.
2743955d011SMarcel Moolenaar  */
275b0c40a00SSimon J. Gerraty bool
276e2eeea75SSimon J. Gerraty GNode_IsOODate(GNode *gn)
2773955d011SMarcel Moolenaar {
278b0c40a00SSimon J. Gerraty 	bool oodate;
2793955d011SMarcel Moolenaar 
2803955d011SMarcel Moolenaar 	/*
2813955d011SMarcel Moolenaar 	 * Certain types of targets needn't even be sought as their datedness
2823955d011SMarcel Moolenaar 	 * doesn't depend on their modification time...
2833955d011SMarcel Moolenaar 	 */
284e2eeea75SSimon J. Gerraty 	if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
285b0c40a00SSimon J. Gerraty 		Dir_UpdateMTime(gn, true);
2863955d011SMarcel Moolenaar 		if (DEBUG(MAKE)) {
287e2eeea75SSimon J. Gerraty 			if (gn->mtime != 0)
28806b9b3e0SSimon J. Gerraty 				debug_printf("modified %s...",
28906b9b3e0SSimon J. Gerraty 				    Targ_FmtTime(gn->mtime));
290e2eeea75SSimon J. Gerraty 			else
29106b9b3e0SSimon J. Gerraty 				debug_printf("nonexistent...");
2923955d011SMarcel Moolenaar 		}
2933955d011SMarcel Moolenaar 	}
2943955d011SMarcel Moolenaar 
2953955d011SMarcel Moolenaar 	/*
2963955d011SMarcel Moolenaar 	 * A target is remade in one of the following circumstances:
29706b9b3e0SSimon J. Gerraty 	 *
29806b9b3e0SSimon J. Gerraty 	 *	its modification time is smaller than that of its youngest
29906b9b3e0SSimon J. Gerraty 	 *	child and it would actually be run (has commands or is not
30006b9b3e0SSimon J. Gerraty 	 *	GNode_IsTarget)
30106b9b3e0SSimon J. Gerraty 	 *
3023955d011SMarcel Moolenaar 	 *	it's the object of a force operator
3033955d011SMarcel Moolenaar 	 *
30406b9b3e0SSimon J. Gerraty 	 *	it has no children, was on the lhs of an operator and doesn't
30506b9b3e0SSimon J. Gerraty 	 *	exist already.
3063955d011SMarcel Moolenaar 	 *
30706b9b3e0SSimon J. Gerraty 	 * Libraries are only considered out-of-date if the archive module
30806b9b3e0SSimon J. Gerraty 	 * says they are.
30906b9b3e0SSimon J. Gerraty 	 *
31006b9b3e0SSimon J. Gerraty 	 * These weird rules are brought to you by Backward-Compatibility
31106b9b3e0SSimon J. Gerraty 	 * and the strange people who wrote 'Make'.
3123955d011SMarcel Moolenaar 	 */
3133955d011SMarcel Moolenaar 	if (gn->type & (OP_USE | OP_USEBEFORE)) {
3143955d011SMarcel Moolenaar 		/*
3153955d011SMarcel Moolenaar 		 * If the node is a USE node it is *never* out of date
3163955d011SMarcel Moolenaar 		 * no matter *what*.
3173955d011SMarcel Moolenaar 		 */
318956e45f6SSimon J. Gerraty 		DEBUG0(MAKE, ".USE node...");
319b0c40a00SSimon J. Gerraty 		oodate = false;
320e2eeea75SSimon J. Gerraty 	} else if ((gn->type & OP_LIB) && (gn->mtime == 0 || Arch_IsLib(gn))) {
321956e45f6SSimon J. Gerraty 		DEBUG0(MAKE, "library...");
3223955d011SMarcel Moolenaar 
3233955d011SMarcel Moolenaar 		/*
3243955d011SMarcel Moolenaar 		 * always out of date if no children and :: target
32506b9b3e0SSimon J. Gerraty 		 * or nonexistent.
3263955d011SMarcel Moolenaar 		 */
3273955d011SMarcel Moolenaar 		oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
32806b9b3e0SSimon J. Gerraty 			  (gn->youngestChild == NULL &&
32906b9b3e0SSimon J. Gerraty 			   (gn->type & OP_DOUBLEDEP)));
3303955d011SMarcel Moolenaar 	} else if (gn->type & OP_JOIN) {
3313955d011SMarcel Moolenaar 		/*
3323955d011SMarcel Moolenaar 		 * A target with the .JOIN attribute is only considered
3333955d011SMarcel Moolenaar 		 * out-of-date if any of its children was out-of-date.
3343955d011SMarcel Moolenaar 		 */
335956e45f6SSimon J. Gerraty 		DEBUG0(MAKE, ".JOIN node...");
33606b9b3e0SSimon J. Gerraty 		DEBUG1(MAKE, "source %smade...",
337*12904384SSimon J. Gerraty 		    gn->flags.childMade ? "" : "not ");
338*12904384SSimon J. Gerraty 		oodate = gn->flags.childMade;
3393955d011SMarcel Moolenaar 	} else if (gn->type & (OP_FORCE | OP_EXEC | OP_PHONY)) {
3403955d011SMarcel Moolenaar 		/*
34106b9b3e0SSimon J. Gerraty 		 * A node which is the object of the force (!) operator or
34206b9b3e0SSimon J. Gerraty 		 * which has the .EXEC attribute is always considered
34306b9b3e0SSimon J. Gerraty 		 * out-of-date.
3443955d011SMarcel Moolenaar 		 */
3453955d011SMarcel Moolenaar 		if (DEBUG(MAKE)) {
3463955d011SMarcel Moolenaar 			if (gn->type & OP_FORCE) {
347956e45f6SSimon J. Gerraty 				debug_printf("! operator...");
3483955d011SMarcel Moolenaar 			} else if (gn->type & OP_PHONY) {
349956e45f6SSimon J. Gerraty 				debug_printf(".PHONY node...");
3503955d011SMarcel Moolenaar 			} else {
351956e45f6SSimon J. Gerraty 				debug_printf(".EXEC node...");
3523955d011SMarcel Moolenaar 			}
3533955d011SMarcel Moolenaar 		}
354b0c40a00SSimon J. Gerraty 		oodate = true;
355e2eeea75SSimon J. Gerraty 	} else if (IsOODateRegular(gn)) {
356b0c40a00SSimon J. Gerraty 		oodate = true;
3573955d011SMarcel Moolenaar 	} else {
3583955d011SMarcel Moolenaar 		/*
35906b9b3e0SSimon J. Gerraty 		 * When a nonexistent child with no sources
3603955d011SMarcel Moolenaar 		 * (such as a typically used FORCE source) has been made and
3613955d011SMarcel Moolenaar 		 * the target of the child (usually a directory) has the same
36206b9b3e0SSimon J. Gerraty 		 * timestamp as the timestamp just given to the nonexistent
36306b9b3e0SSimon J. Gerraty 		 * child after it was considered made.
3643955d011SMarcel Moolenaar 		 */
3653955d011SMarcel Moolenaar 		if (DEBUG(MAKE)) {
366*12904384SSimon J. Gerraty 			if (gn->flags.force)
367956e45f6SSimon J. Gerraty 				debug_printf("non existing child...");
3683955d011SMarcel Moolenaar 		}
369*12904384SSimon J. Gerraty 		oodate = gn->flags.force;
3703955d011SMarcel Moolenaar 	}
3713955d011SMarcel Moolenaar 
3723955d011SMarcel Moolenaar #ifdef USE_META
3733955d011SMarcel Moolenaar 	if (useMeta) {
3743955d011SMarcel Moolenaar 		oodate = meta_oodate(gn, oodate);
3753955d011SMarcel Moolenaar 	}
3763955d011SMarcel Moolenaar #endif
3773955d011SMarcel Moolenaar 
3783955d011SMarcel Moolenaar 	/*
3793955d011SMarcel Moolenaar 	 * If the target isn't out-of-date, the parents need to know its
3803955d011SMarcel Moolenaar 	 * modification time. Note that targets that appear to be out-of-date
381956e45f6SSimon J. Gerraty 	 * but aren't, because they have no commands and are GNode_IsTarget,
38206b9b3e0SSimon J. Gerraty 	 * have their mtime stay below their children's mtime to keep parents
38306b9b3e0SSimon J. Gerraty 	 * from thinking they're out-of-date.
3843955d011SMarcel Moolenaar 	 */
3853955d011SMarcel Moolenaar 	if (!oodate) {
386956e45f6SSimon J. Gerraty 		GNodeListNode *ln;
38706b9b3e0SSimon J. Gerraty 		for (ln = gn->parents.first; ln != NULL; ln = ln->next)
388e2eeea75SSimon J. Gerraty 			GNode_UpdateYoungestChild(ln->datum, gn);
3893955d011SMarcel Moolenaar 	}
3903955d011SMarcel Moolenaar 
3913841c287SSimon J. Gerraty 	return oodate;
3923955d011SMarcel Moolenaar }
3932c3632d1SSimon J. Gerraty 
394e2eeea75SSimon J. Gerraty static void
395e2eeea75SSimon J. Gerraty PretendAllChildrenAreMade(GNode *pgn)
3963955d011SMarcel Moolenaar {
397e2eeea75SSimon J. Gerraty 	GNodeListNode *ln;
3983955d011SMarcel Moolenaar 
39906b9b3e0SSimon J. Gerraty 	for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
400e2eeea75SSimon J. Gerraty 		GNode *cgn = ln->datum;
4012c3632d1SSimon J. Gerraty 
40206b9b3e0SSimon J. Gerraty 		/* This may also update cgn->path. */
403b0c40a00SSimon J. Gerraty 		Dir_UpdateMTime(cgn, false);
404e2eeea75SSimon J. Gerraty 		GNode_UpdateYoungestChild(pgn, cgn);
4053955d011SMarcel Moolenaar 		pgn->unmade--;
406e2eeea75SSimon J. Gerraty 	}
4073955d011SMarcel Moolenaar }
4082c3632d1SSimon J. Gerraty 
40906b9b3e0SSimon J. Gerraty /*
41006b9b3e0SSimon J. Gerraty  * Called by Make_Run and SuffApplyTransform on the downward pass to handle
4112c3632d1SSimon J. Gerraty  * .USE and transformation nodes, by copying the child node's commands, type
4122c3632d1SSimon J. Gerraty  * flags and children to the parent node.
4133955d011SMarcel Moolenaar  *
4142c3632d1SSimon J. Gerraty  * A .USE node is much like an explicit transformation rule, except its
4152c3632d1SSimon J. Gerraty  * commands are always added to the target node, even if the target already
4162c3632d1SSimon J. Gerraty  * has commands.
4173955d011SMarcel Moolenaar  *
4183955d011SMarcel Moolenaar  * Input:
419956e45f6SSimon J. Gerraty  *	cgn		The source node, which is either a .USE/.USEBEFORE
420956e45f6SSimon J. Gerraty  *			node or a transformation node (OP_TRANSFORM).
421956e45f6SSimon J. Gerraty  *	pgn		The target node
4223955d011SMarcel Moolenaar  */
4233955d011SMarcel Moolenaar void
4243955d011SMarcel Moolenaar Make_HandleUse(GNode *cgn, GNode *pgn)
4253955d011SMarcel Moolenaar {
426956e45f6SSimon J. Gerraty 	GNodeListNode *ln;	/* An element in the children list */
4273955d011SMarcel Moolenaar 
4283955d011SMarcel Moolenaar #ifdef DEBUG_SRC
429e2eeea75SSimon J. Gerraty 	if (!(cgn->type & (OP_USE | OP_USEBEFORE | OP_TRANSFORM))) {
43006b9b3e0SSimon J. Gerraty 		debug_printf("Make_HandleUse: called for plain node %s\n",
43106b9b3e0SSimon J. Gerraty 		    cgn->name);
43206b9b3e0SSimon J. Gerraty 		/* XXX: debug mode should not affect control flow */
43306b9b3e0SSimon J. Gerraty 		return;
4343955d011SMarcel Moolenaar 	}
4353955d011SMarcel Moolenaar #endif
4363955d011SMarcel Moolenaar 
43706b9b3e0SSimon J. Gerraty 	if ((cgn->type & (OP_USE | OP_USEBEFORE)) ||
43806b9b3e0SSimon J. Gerraty 	    Lst_IsEmpty(&pgn->commands)) {
4393955d011SMarcel Moolenaar 		if (cgn->type & OP_USEBEFORE) {
4402c3632d1SSimon J. Gerraty 			/* .USEBEFORE */
44106b9b3e0SSimon J. Gerraty 			Lst_PrependAll(&pgn->commands, &cgn->commands);
4423955d011SMarcel Moolenaar 		} else {
4432c3632d1SSimon J. Gerraty 			/* .USE, or target has no commands */
44406b9b3e0SSimon J. Gerraty 			Lst_AppendAll(&pgn->commands, &cgn->commands);
4453955d011SMarcel Moolenaar 		}
4463955d011SMarcel Moolenaar 	}
4473955d011SMarcel Moolenaar 
44806b9b3e0SSimon J. Gerraty 	for (ln = cgn->children.first; ln != NULL; ln = ln->next) {
449956e45f6SSimon J. Gerraty 		GNode *gn = ln->datum;
4503955d011SMarcel Moolenaar 
4513955d011SMarcel Moolenaar 		/*
4523955d011SMarcel Moolenaar 		 * Expand variables in the .USE node's name
4533955d011SMarcel Moolenaar 		 * and save the unexpanded form.
4543955d011SMarcel Moolenaar 		 * We don't need to do this for commands.
4553955d011SMarcel Moolenaar 		 * They get expanded properly when we execute.
4563955d011SMarcel Moolenaar 		 */
4573955d011SMarcel Moolenaar 		if (gn->uname == NULL) {
4583955d011SMarcel Moolenaar 			gn->uname = gn->name;
4593955d011SMarcel Moolenaar 		} else {
4603955d011SMarcel Moolenaar 			free(gn->name);
4613955d011SMarcel Moolenaar 		}
462956e45f6SSimon J. Gerraty 		(void)Var_Subst(gn->uname, pgn, VARE_WANTRES, &gn->name);
463956e45f6SSimon J. Gerraty 		/* TODO: handle errors */
46406b9b3e0SSimon J. Gerraty 		if (gn->uname != NULL && strcmp(gn->name, gn->uname) != 0) {
4653955d011SMarcel Moolenaar 			/* See if we have a target for this node. */
466956e45f6SSimon J. Gerraty 			GNode *tgn = Targ_FindNode(gn->name);
4673955d011SMarcel Moolenaar 			if (tgn != NULL)
4683955d011SMarcel Moolenaar 				gn = tgn;
4693955d011SMarcel Moolenaar 		}
4703955d011SMarcel Moolenaar 
47106b9b3e0SSimon J. Gerraty 		Lst_Append(&pgn->children, gn);
47206b9b3e0SSimon J. Gerraty 		Lst_Append(&gn->parents, pgn);
473956e45f6SSimon J. Gerraty 		pgn->unmade++;
4743955d011SMarcel Moolenaar 	}
4753955d011SMarcel Moolenaar 
47606b9b3e0SSimon J. Gerraty 	pgn->type |=
47706b9b3e0SSimon J. Gerraty 	    cgn->type & ~(OP_OPMASK | OP_USE | OP_USEBEFORE | OP_TRANSFORM);
4783955d011SMarcel Moolenaar }
4793955d011SMarcel Moolenaar 
48006b9b3e0SSimon J. Gerraty /*
48106b9b3e0SSimon J. Gerraty  * Used by Make_Run on the downward pass to handle .USE nodes. Should be
482956e45f6SSimon J. Gerraty  * called before the children are enqueued to be looked at by MakeAddChild.
483956e45f6SSimon J. Gerraty  *
484956e45f6SSimon J. Gerraty  * For a .USE child, the commands, type flags and children are copied to the
485956e45f6SSimon J. Gerraty  * parent node, and since the relation to the .USE node is then no longer
486956e45f6SSimon J. Gerraty  * needed, that relation is removed.
4873955d011SMarcel Moolenaar  *
4883955d011SMarcel Moolenaar  * Input:
489956e45f6SSimon J. Gerraty  *	cgn		the child, which may be a .USE node
490956e45f6SSimon J. Gerraty  *	pgn		the current parent
4913955d011SMarcel Moolenaar  */
492956e45f6SSimon J. Gerraty static void
493956e45f6SSimon J. Gerraty MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
4943955d011SMarcel Moolenaar {
495b0c40a00SSimon J. Gerraty 	bool unmarked;
4963955d011SMarcel Moolenaar 
497e2eeea75SSimon J. Gerraty 	unmarked = !(cgn->type & OP_MARK);
4983955d011SMarcel Moolenaar 	cgn->type |= OP_MARK;
4993955d011SMarcel Moolenaar 
500e2eeea75SSimon J. Gerraty 	if (!(cgn->type & (OP_USE | OP_USEBEFORE)))
501956e45f6SSimon J. Gerraty 		return;
5023955d011SMarcel Moolenaar 
5033955d011SMarcel Moolenaar 	if (unmarked)
5043955d011SMarcel Moolenaar 		Make_HandleUse(cgn, pgn);
5053955d011SMarcel Moolenaar 
5063955d011SMarcel Moolenaar 	/*
5073955d011SMarcel Moolenaar 	 * This child node is now "made", so we decrement the count of
5083955d011SMarcel Moolenaar 	 * unmade children in the parent... We also remove the child
5093955d011SMarcel Moolenaar 	 * from the parent's list to accurately reflect the number of decent
5103955d011SMarcel Moolenaar 	 * children the parent has. This is used by Make_Run to decide
5113955d011SMarcel Moolenaar 	 * whether to queue the parent or examine its children...
5123955d011SMarcel Moolenaar 	 */
51306b9b3e0SSimon J. Gerraty 	Lst_Remove(&pgn->children, ln);
5143955d011SMarcel Moolenaar 	pgn->unmade--;
5153955d011SMarcel Moolenaar }
516956e45f6SSimon J. Gerraty 
517956e45f6SSimon J. Gerraty static void
518956e45f6SSimon J. Gerraty HandleUseNodes(GNode *gn)
519956e45f6SSimon J. Gerraty {
520956e45f6SSimon J. Gerraty 	GNodeListNode *ln, *nln;
52106b9b3e0SSimon J. Gerraty 	for (ln = gn->children.first; ln != NULL; ln = nln) {
522956e45f6SSimon J. Gerraty 		nln = ln->next;
523956e45f6SSimon J. Gerraty 		MakeHandleUse(ln->datum, gn, ln);
524956e45f6SSimon J. Gerraty 	}
5253955d011SMarcel Moolenaar }
5263955d011SMarcel Moolenaar 
5273955d011SMarcel Moolenaar 
52806b9b3e0SSimon J. Gerraty /*
52906b9b3e0SSimon J. Gerraty  * Check the modification time of a gnode, and update it if necessary.
53006b9b3e0SSimon J. Gerraty  * Return 0 if the gnode does not exist, or its filesystem time if it does.
53106b9b3e0SSimon J. Gerraty  */
5323955d011SMarcel Moolenaar time_t
5333955d011SMarcel Moolenaar Make_Recheck(GNode *gn)
5343955d011SMarcel Moolenaar {
535e2eeea75SSimon J. Gerraty 	time_t mtime;
536e2eeea75SSimon J. Gerraty 
537b0c40a00SSimon J. Gerraty 	Dir_UpdateMTime(gn, true);
538e2eeea75SSimon J. Gerraty 	mtime = gn->mtime;
5393955d011SMarcel Moolenaar 
5403955d011SMarcel Moolenaar #ifndef RECHECK
5413955d011SMarcel Moolenaar 	/*
5423955d011SMarcel Moolenaar 	 * We can't re-stat the thing, but we can at least take care of rules
5433955d011SMarcel Moolenaar 	 * where a target depends on a source that actually creates the
5443955d011SMarcel Moolenaar 	 * target, but only if it has changed, e.g.
5453955d011SMarcel Moolenaar 	 *
5463955d011SMarcel Moolenaar 	 * parse.h : parse.o
5473955d011SMarcel Moolenaar 	 *
5483955d011SMarcel Moolenaar 	 * parse.o : parse.y
5493955d011SMarcel Moolenaar 	 *		yacc -d parse.y
5503955d011SMarcel Moolenaar 	 *		cc -c y.tab.c
5513955d011SMarcel Moolenaar 	 *		mv y.tab.o parse.o
5523955d011SMarcel Moolenaar 	 *		cmp -s y.tab.h parse.h || mv y.tab.h parse.h
5533955d011SMarcel Moolenaar 	 *
5543955d011SMarcel Moolenaar 	 * In this case, if the definitions produced by yacc haven't changed
5553955d011SMarcel Moolenaar 	 * from before, parse.h won't have been updated and gn->mtime will
5563955d011SMarcel Moolenaar 	 * reflect the current modification time for parse.h. This is
557e2eeea75SSimon J. Gerraty 	 * something of a kludge, I admit, but it's a useful one.
5583955d011SMarcel Moolenaar 	 *
559e2eeea75SSimon J. Gerraty 	 * XXX: People like to use a rule like "FRC:" to force things that
560e2eeea75SSimon J. Gerraty 	 * depend on FRC to be made, so we have to check for gn->children
561e2eeea75SSimon J. Gerraty 	 * being empty as well.
5623955d011SMarcel Moolenaar 	 */
5633955d011SMarcel Moolenaar 	if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
5643955d011SMarcel Moolenaar 		gn->mtime = now;
5653955d011SMarcel Moolenaar 	}
5663955d011SMarcel Moolenaar #else
5673955d011SMarcel Moolenaar 	/*
5683955d011SMarcel Moolenaar 	 * This is what Make does and it's actually a good thing, as it
5693955d011SMarcel Moolenaar 	 * allows rules like
5703955d011SMarcel Moolenaar 	 *
5713955d011SMarcel Moolenaar 	 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
5723955d011SMarcel Moolenaar 	 *
5733955d011SMarcel Moolenaar 	 * to function as intended. Unfortunately, thanks to the stateless
5743955d011SMarcel Moolenaar 	 * nature of NFS (by which I mean the loose coupling of two clients
57506b9b3e0SSimon J. Gerraty 	 * using the same file from a common server), there are times when
57606b9b3e0SSimon J. Gerraty 	 * the modification time of a file created on a remote machine
57706b9b3e0SSimon J. Gerraty 	 * will not be modified before the local stat() implied by the
57806b9b3e0SSimon J. Gerraty 	 * Dir_UpdateMTime occurs, thus leading us to believe that the file
5793955d011SMarcel Moolenaar 	 * is unchanged, wreaking havoc with files that depend on this one.
5803955d011SMarcel Moolenaar 	 *
5813955d011SMarcel Moolenaar 	 * I have decided it is better to make too much than to make too
5823955d011SMarcel Moolenaar 	 * little, so this stuff is commented out unless you're sure it's ok.
5833955d011SMarcel Moolenaar 	 * -- ardeb 1/12/88
5843955d011SMarcel Moolenaar 	 */
5853955d011SMarcel Moolenaar 	/*
586e2eeea75SSimon J. Gerraty 	 * Christos, 4/9/92: If we are saving commands, pretend that
587e2eeea75SSimon J. Gerraty 	 * the target is made now. Otherwise archives with '...' rules
5883955d011SMarcel Moolenaar 	 * don't work!
5893955d011SMarcel Moolenaar 	 */
590956e45f6SSimon J. Gerraty 	if (!GNode_ShouldExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
5913955d011SMarcel Moolenaar 	    (mtime == 0 && !(gn->type & OP_WAIT))) {
592956e45f6SSimon J. Gerraty 		DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
59306b9b3e0SSimon J. Gerraty 		    gn->name,
59406b9b3e0SSimon J. Gerraty 		    gn->mtime == 0 ? "nonexistent" : Targ_FmtTime(gn->mtime));
5953955d011SMarcel Moolenaar 		gn->mtime = now;
596e2eeea75SSimon J. Gerraty 	} else {
597956e45f6SSimon J. Gerraty 		DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
5983955d011SMarcel Moolenaar 		    gn->name, Targ_FmtTime(gn->mtime));
5993955d011SMarcel Moolenaar 	}
6003955d011SMarcel Moolenaar #endif
601e2eeea75SSimon J. Gerraty 
602e2eeea75SSimon J. Gerraty 	/* XXX: The returned mtime may differ from gn->mtime.
603e2eeea75SSimon J. Gerraty 	 * Intentionally? */
6043955d011SMarcel Moolenaar 	return mtime;
6053955d011SMarcel Moolenaar }
6063955d011SMarcel Moolenaar 
607956e45f6SSimon J. Gerraty /*
608956e45f6SSimon J. Gerraty  * Set the .PREFIX and .IMPSRC variables for all the implied parents
609956e45f6SSimon J. Gerraty  * of this node.
610956e45f6SSimon J. Gerraty  */
611956e45f6SSimon J. Gerraty static void
612956e45f6SSimon J. Gerraty UpdateImplicitParentsVars(GNode *cgn, const char *cname)
613956e45f6SSimon J. Gerraty {
614956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
615956e45f6SSimon J. Gerraty 	const char *cpref = GNode_VarPrefix(cgn);
616956e45f6SSimon J. Gerraty 
61706b9b3e0SSimon J. Gerraty 	for (ln = cgn->implicitParents.first; ln != NULL; ln = ln->next) {
618956e45f6SSimon J. Gerraty 		GNode *pgn = ln->datum;
619*12904384SSimon J. Gerraty 		if (pgn->flags.remake) {
620dba7b0efSSimon J. Gerraty 			Var_Set(pgn, IMPSRC, cname);
621956e45f6SSimon J. Gerraty 			if (cpref != NULL)
622dba7b0efSSimon J. Gerraty 				Var_Set(pgn, PREFIX, cpref);
623956e45f6SSimon J. Gerraty 		}
624956e45f6SSimon J. Gerraty 	}
625956e45f6SSimon J. Gerraty }
626956e45f6SSimon J. Gerraty 
627e2eeea75SSimon J. Gerraty /* See if a .ORDER rule stops us from building this node. */
628b0c40a00SSimon J. Gerraty static bool
629e2eeea75SSimon J. Gerraty IsWaitingForOrder(GNode *gn)
630e2eeea75SSimon J. Gerraty {
631e2eeea75SSimon J. Gerraty 	GNodeListNode *ln;
632e2eeea75SSimon J. Gerraty 
63306b9b3e0SSimon J. Gerraty 	for (ln = gn->order_pred.first; ln != NULL; ln = ln->next) {
634e2eeea75SSimon J. Gerraty 		GNode *ogn = ln->datum;
635e2eeea75SSimon J. Gerraty 
636*12904384SSimon J. Gerraty 		if (GNode_IsDone(ogn) || !ogn->flags.remake)
637e2eeea75SSimon J. Gerraty 			continue;
638e2eeea75SSimon J. Gerraty 
63906b9b3e0SSimon J. Gerraty 		DEBUG2(MAKE,
64006b9b3e0SSimon J. Gerraty 		    "IsWaitingForOrder: Waiting for .ORDER node \"%s%s\"\n",
641e2eeea75SSimon J. Gerraty 		    ogn->name, ogn->cohort_num);
642b0c40a00SSimon J. Gerraty 		return true;
643e2eeea75SSimon J. Gerraty 	}
644b0c40a00SSimon J. Gerraty 	return false;
645e2eeea75SSimon J. Gerraty }
646e2eeea75SSimon J. Gerraty 
647dba7b0efSSimon J. Gerraty static void MakeBuildParent(GNode *, GNodeListNode *);
64806b9b3e0SSimon J. Gerraty 
64906b9b3e0SSimon J. Gerraty static void
65006b9b3e0SSimon J. Gerraty ScheduleOrderSuccessors(GNode *gn)
65106b9b3e0SSimon J. Gerraty {
65206b9b3e0SSimon J. Gerraty 	GNodeListNode *toBeMadeNext = toBeMade.first;
65306b9b3e0SSimon J. Gerraty 	GNodeListNode *ln;
65406b9b3e0SSimon J. Gerraty 
65506b9b3e0SSimon J. Gerraty 	for (ln = gn->order_succ.first; ln != NULL; ln = ln->next)
656dba7b0efSSimon J. Gerraty 		MakeBuildParent(ln->datum, toBeMadeNext);
65706b9b3e0SSimon J. Gerraty }
65806b9b3e0SSimon J. Gerraty 
65906b9b3e0SSimon J. Gerraty /*
66006b9b3e0SSimon J. Gerraty  * Perform update on the parents of a node. Used by JobFinish once
6613955d011SMarcel Moolenaar  * a node has been dealt with and by MakeStartJobs if it finds an
6623955d011SMarcel Moolenaar  * up-to-date node.
6633955d011SMarcel Moolenaar  *
6643955d011SMarcel Moolenaar  * The unmade field of pgn is decremented and pgn may be placed on
6653955d011SMarcel Moolenaar  * the toBeMade queue if this field becomes 0.
6663955d011SMarcel Moolenaar  *
6673955d011SMarcel Moolenaar  * If the child was made, the parent's flag CHILDMADE field will be
6683955d011SMarcel Moolenaar  * set true.
6693955d011SMarcel Moolenaar  *
6703955d011SMarcel Moolenaar  * If the child is not up-to-date and still does not exist,
6713955d011SMarcel Moolenaar  * set the FORCE flag on the parents.
6723955d011SMarcel Moolenaar  *
673956e45f6SSimon J. Gerraty  * If the child wasn't made, the youngestChild field of the parent will be
6743955d011SMarcel Moolenaar  * altered if the child's mtime is big enough.
6753955d011SMarcel Moolenaar  *
6763955d011SMarcel Moolenaar  * Finally, if the child is the implied source for the parent, the
6773955d011SMarcel Moolenaar  * parent's IMPSRC variable is set appropriately.
6783955d011SMarcel Moolenaar  */
6793955d011SMarcel Moolenaar void
6803955d011SMarcel Moolenaar Make_Update(GNode *cgn)
6813955d011SMarcel Moolenaar {
6822c3632d1SSimon J. Gerraty 	const char *cname;	/* the child's name */
6833955d011SMarcel Moolenaar 	time_t mtime = -1;
684956e45f6SSimon J. Gerraty 	GNodeList *parents;
685956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
6863955d011SMarcel Moolenaar 	GNode *centurion;
6873955d011SMarcel Moolenaar 
6883955d011SMarcel Moolenaar 	/* It is save to re-examine any nodes again */
689e2eeea75SSimon J. Gerraty 	checked_seqno++;
6903955d011SMarcel Moolenaar 
691956e45f6SSimon J. Gerraty 	cname = GNode_VarTarget(cgn);
6923955d011SMarcel Moolenaar 
693956e45f6SSimon J. Gerraty 	DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
6943955d011SMarcel Moolenaar 
6953955d011SMarcel Moolenaar 	/*
6963955d011SMarcel Moolenaar 	 * If the child was actually made, see what its modification time is
69706b9b3e0SSimon J. Gerraty 	 * now -- some rules won't actually update the file. If the file
69806b9b3e0SSimon J. Gerraty 	 * still doesn't exist, make its mtime now.
6993955d011SMarcel Moolenaar 	 */
7003955d011SMarcel Moolenaar 	if (cgn->made != UPTODATE) {
7013955d011SMarcel Moolenaar 		mtime = Make_Recheck(cgn);
7023955d011SMarcel Moolenaar 	}
7033955d011SMarcel Moolenaar 
7043955d011SMarcel Moolenaar 	/*
7053955d011SMarcel Moolenaar 	 * If this is a `::' node, we must consult its first instance
7063955d011SMarcel Moolenaar 	 * which is where all parents are linked.
7073955d011SMarcel Moolenaar 	 */
7083955d011SMarcel Moolenaar 	if ((centurion = cgn->centurion) != NULL) {
70906b9b3e0SSimon J. Gerraty 		if (!Lst_IsEmpty(&cgn->parents))
71006b9b3e0SSimon J. Gerraty 			Punt("%s%s: cohort has parents", cgn->name,
71106b9b3e0SSimon J. Gerraty 			    cgn->cohort_num);
712956e45f6SSimon J. Gerraty 		centurion->unmade_cohorts--;
7133955d011SMarcel Moolenaar 		if (centurion->unmade_cohorts < 0)
71406b9b3e0SSimon J. Gerraty 			Error("Graph cycles through centurion %s",
71506b9b3e0SSimon J. Gerraty 			    centurion->name);
7163955d011SMarcel Moolenaar 	} else {
7173955d011SMarcel Moolenaar 		centurion = cgn;
7183955d011SMarcel Moolenaar 	}
71906b9b3e0SSimon J. Gerraty 	parents = &centurion->parents;
7203955d011SMarcel Moolenaar 
7213955d011SMarcel Moolenaar 	/* If this was a .ORDER node, schedule the RHS */
72206b9b3e0SSimon J. Gerraty 	ScheduleOrderSuccessors(centurion);
7233955d011SMarcel Moolenaar 
7243955d011SMarcel Moolenaar 	/* Now mark all the parents as having one less unmade child */
725956e45f6SSimon J. Gerraty 	for (ln = parents->first; ln != NULL; ln = ln->next) {
726956e45f6SSimon J. Gerraty 		GNode *pgn = ln->datum;
727956e45f6SSimon J. Gerraty 
728e2eeea75SSimon J. Gerraty 		if (DEBUG(MAKE)) {
72906b9b3e0SSimon J. Gerraty 			debug_printf("inspect parent %s%s: ", pgn->name,
73006b9b3e0SSimon J. Gerraty 			    pgn->cohort_num);
731e2eeea75SSimon J. Gerraty 			GNode_FprintDetails(opts.debug_file, "", pgn, "");
732e2eeea75SSimon J. Gerraty 			debug_printf(", unmade %d ", pgn->unmade - 1);
733e2eeea75SSimon J. Gerraty 		}
7343955d011SMarcel Moolenaar 
735*12904384SSimon J. Gerraty 		if (!pgn->flags.remake) {
7363955d011SMarcel Moolenaar 			/* This parent isn't needed */
737956e45f6SSimon J. Gerraty 			DEBUG0(MAKE, "- not needed\n");
7383955d011SMarcel Moolenaar 			continue;
7393955d011SMarcel Moolenaar 		}
7403955d011SMarcel Moolenaar 		if (mtime == 0 && !(cgn->type & OP_WAIT))
741*12904384SSimon J. Gerraty 			pgn->flags.force = true;
7423955d011SMarcel Moolenaar 
7433955d011SMarcel Moolenaar 		/*
7443955d011SMarcel Moolenaar 		 * If the parent has the .MADE attribute, its timestamp got
745956e45f6SSimon J. Gerraty 		 * updated to that of its newest child, and its unmade
7463955d011SMarcel Moolenaar 		 * child count got set to zero in Make_ExpandUse().
7473955d011SMarcel Moolenaar 		 * However other things might cause us to build one of its
7483955d011SMarcel Moolenaar 		 * children - and so we mustn't do any processing here when
7493955d011SMarcel Moolenaar 		 * the child build finishes.
7503955d011SMarcel Moolenaar 		 */
7513955d011SMarcel Moolenaar 		if (pgn->type & OP_MADE) {
752956e45f6SSimon J. Gerraty 			DEBUG0(MAKE, "- .MADE\n");
7533955d011SMarcel Moolenaar 			continue;
7543955d011SMarcel Moolenaar 		}
7553955d011SMarcel Moolenaar 
7563955d011SMarcel Moolenaar 		if (!(cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE))) {
7573955d011SMarcel Moolenaar 			if (cgn->made == MADE)
758*12904384SSimon J. Gerraty 				pgn->flags.childMade = true;
759e2eeea75SSimon J. Gerraty 			GNode_UpdateYoungestChild(pgn, cgn);
7603955d011SMarcel Moolenaar 		}
7613955d011SMarcel Moolenaar 
7623955d011SMarcel Moolenaar 		/*
7633955d011SMarcel Moolenaar 		 * A parent must wait for the completion of all instances
7643955d011SMarcel Moolenaar 		 * of a `::' dependency.
7653955d011SMarcel Moolenaar 		 */
76606b9b3e0SSimon J. Gerraty 		if (centurion->unmade_cohorts != 0 ||
76706b9b3e0SSimon J. Gerraty 		    !GNode_IsDone(centurion)) {
76806b9b3e0SSimon J. Gerraty 			DEBUG2(MAKE,
76906b9b3e0SSimon J. Gerraty 			    "- centurion made %d, %d unmade cohorts\n",
7703955d011SMarcel Moolenaar 			    centurion->made, centurion->unmade_cohorts);
7713955d011SMarcel Moolenaar 			continue;
7723955d011SMarcel Moolenaar 		}
7733955d011SMarcel Moolenaar 
7743955d011SMarcel Moolenaar 		/* One more child of this parent is now made */
775956e45f6SSimon J. Gerraty 		pgn->unmade--;
7763955d011SMarcel Moolenaar 		if (pgn->unmade < 0) {
7773955d011SMarcel Moolenaar 			if (DEBUG(MAKE)) {
778956e45f6SSimon J. Gerraty 				debug_printf("Graph cycles through %s%s\n",
7793955d011SMarcel Moolenaar 				    pgn->name, pgn->cohort_num);
7803955d011SMarcel Moolenaar 				Targ_PrintGraph(2);
7813955d011SMarcel Moolenaar 			}
78206b9b3e0SSimon J. Gerraty 			Error("Graph cycles through %s%s", pgn->name,
78306b9b3e0SSimon J. Gerraty 			    pgn->cohort_num);
7843955d011SMarcel Moolenaar 		}
7853955d011SMarcel Moolenaar 
78606b9b3e0SSimon J. Gerraty 		/*
78706b9b3e0SSimon J. Gerraty 		 * We must always rescan the parents of .WAIT and .ORDER
78806b9b3e0SSimon J. Gerraty 		 * nodes.
78906b9b3e0SSimon J. Gerraty 		 */
7903955d011SMarcel Moolenaar 		if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
791*12904384SSimon J. Gerraty 		    && !centurion->flags.doneOrder) {
792956e45f6SSimon J. Gerraty 			DEBUG0(MAKE, "- unmade children\n");
7933955d011SMarcel Moolenaar 			continue;
7943955d011SMarcel Moolenaar 		}
7953955d011SMarcel Moolenaar 		if (pgn->made != DEFERRED) {
7963955d011SMarcel Moolenaar 			/*
79706b9b3e0SSimon J. Gerraty 			 * Either this parent is on a different branch of
79806b9b3e0SSimon J. Gerraty 			 * the tree, or it on the RHS of a .WAIT directive
7993955d011SMarcel Moolenaar 			 * or it is already on the toBeMade list.
8003955d011SMarcel Moolenaar 			 */
801956e45f6SSimon J. Gerraty 			DEBUG0(MAKE, "- not deferred\n");
8023955d011SMarcel Moolenaar 			continue;
8033955d011SMarcel Moolenaar 		}
804e2eeea75SSimon J. Gerraty 
805e2eeea75SSimon J. Gerraty 		if (IsWaitingForOrder(pgn))
8063955d011SMarcel Moolenaar 			continue;
807e2eeea75SSimon J. Gerraty 
8083955d011SMarcel Moolenaar 		if (DEBUG(MAKE)) {
809956e45f6SSimon J. Gerraty 			debug_printf("- %s%s made, schedule %s%s (made %d)\n",
8103955d011SMarcel Moolenaar 			    cgn->name, cgn->cohort_num,
8113955d011SMarcel Moolenaar 			    pgn->name, pgn->cohort_num, pgn->made);
812956e45f6SSimon J. Gerraty 			Targ_PrintNode(pgn, 2);
8133955d011SMarcel Moolenaar 		}
8143955d011SMarcel Moolenaar 		/* Ok, we can schedule the parent again */
8153955d011SMarcel Moolenaar 		pgn->made = REQUESTED;
81606b9b3e0SSimon J. Gerraty 		Lst_Enqueue(&toBeMade, pgn);
8173955d011SMarcel Moolenaar 	}
8183955d011SMarcel Moolenaar 
819956e45f6SSimon J. Gerraty 	UpdateImplicitParentsVars(cgn, cname);
820956e45f6SSimon J. Gerraty }
821956e45f6SSimon J. Gerraty 
822956e45f6SSimon J. Gerraty static void
823956e45f6SSimon J. Gerraty UnmarkChildren(GNode *gn)
8242c3632d1SSimon J. Gerraty {
825956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
8263955d011SMarcel Moolenaar 
82706b9b3e0SSimon J. Gerraty 	for (ln = gn->children.first; ln != NULL; ln = ln->next) {
828956e45f6SSimon J. Gerraty 		GNode *child = ln->datum;
829956e45f6SSimon J. Gerraty 		child->type &= ~OP_MARK;
8303955d011SMarcel Moolenaar 	}
8313955d011SMarcel Moolenaar }
8322c3632d1SSimon J. Gerraty 
83306b9b3e0SSimon J. Gerraty /*
83406b9b3e0SSimon J. Gerraty  * Add a child's name to the ALLSRC and OODATE variables of the given
835956e45f6SSimon J. Gerraty  * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
836956e45f6SSimon J. Gerraty  * attributes. .EXEC and .USE children are very rarely going to be files,
837956e45f6SSimon J. Gerraty  * so...
838956e45f6SSimon J. Gerraty  *
8393955d011SMarcel Moolenaar  * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
8403955d011SMarcel Moolenaar  *
8413955d011SMarcel Moolenaar  * A child is added to the OODATE variable if its modification time is
8423955d011SMarcel Moolenaar  * later than that of its parent, as defined by Make, except if the
8433955d011SMarcel Moolenaar  * parent is a .JOIN node. In that case, it is only added to the OODATE
8443955d011SMarcel Moolenaar  * variable if it was actually made (since .JOIN nodes don't have
8453955d011SMarcel Moolenaar  * modification times, the comparison is rather unfair...)..
8463955d011SMarcel Moolenaar  *
8473955d011SMarcel Moolenaar  * Input:
848956e45f6SSimon J. Gerraty  *	cgn		The child to add
849956e45f6SSimon J. Gerraty  *	pgn		The parent to whose ALLSRC variable it should
8503955d011SMarcel Moolenaar  *			be added
8513955d011SMarcel Moolenaar  */
852956e45f6SSimon J. Gerraty static void
853956e45f6SSimon J. Gerraty MakeAddAllSrc(GNode *cgn, GNode *pgn)
8543955d011SMarcel Moolenaar {
855dba7b0efSSimon J. Gerraty 	const char *child, *allsrc;
856dba7b0efSSimon J. Gerraty 
8573955d011SMarcel Moolenaar 	if (cgn->type & OP_MARK)
858956e45f6SSimon J. Gerraty 		return;
8593955d011SMarcel Moolenaar 	cgn->type |= OP_MARK;
8603955d011SMarcel Moolenaar 
861dba7b0efSSimon J. Gerraty 	if (cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE | OP_INVISIBLE))
862dba7b0efSSimon J. Gerraty 		return;
8633955d011SMarcel Moolenaar 
8643955d011SMarcel Moolenaar 	if (cgn->type & OP_ARCHV)
865956e45f6SSimon J. Gerraty 		child = GNode_VarMember(cgn);
8663955d011SMarcel Moolenaar 	else
867956e45f6SSimon J. Gerraty 		child = GNode_Path(cgn);
868dba7b0efSSimon J. Gerraty 
869dba7b0efSSimon J. Gerraty 	if (cgn->type & OP_JOIN)
870956e45f6SSimon J. Gerraty 		allsrc = GNode_VarAllsrc(cgn);
871dba7b0efSSimon J. Gerraty 	else
8723955d011SMarcel Moolenaar 		allsrc = child;
873dba7b0efSSimon J. Gerraty 
8743955d011SMarcel Moolenaar 	if (allsrc != NULL)
875dba7b0efSSimon J. Gerraty 		Var_Append(pgn, ALLSRC, allsrc);
876dba7b0efSSimon J. Gerraty 
8773955d011SMarcel Moolenaar 	if (pgn->type & OP_JOIN) {
878dba7b0efSSimon J. Gerraty 		if (cgn->made == MADE)
879dba7b0efSSimon J. Gerraty 			Var_Append(pgn, OODATE, child);
880dba7b0efSSimon J. Gerraty 
8813955d011SMarcel Moolenaar 	} else if ((pgn->mtime < cgn->mtime) ||
88206b9b3e0SSimon J. Gerraty 		   (cgn->mtime >= now && cgn->made == MADE)) {
8833955d011SMarcel Moolenaar 		/*
88406b9b3e0SSimon J. Gerraty 		 * It goes in the OODATE variable if the parent is
88506b9b3e0SSimon J. Gerraty 		 * younger than the child or if the child has been
88606b9b3e0SSimon J. Gerraty 		 * modified more recently than the start of the make.
88706b9b3e0SSimon J. Gerraty 		 * This is to keep pmake from getting confused if
88806b9b3e0SSimon J. Gerraty 		 * something else updates the parent after the make
88906b9b3e0SSimon J. Gerraty 		 * starts (shouldn't happen, I know, but sometimes it
89006b9b3e0SSimon J. Gerraty 		 * does). In such a case, if we've updated the child,
89106b9b3e0SSimon J. Gerraty 		 * the parent is likely to have a modification time
89206b9b3e0SSimon J. Gerraty 		 * later than that of the child and anything that
89306b9b3e0SSimon J. Gerraty 		 * relies on the OODATE variable will be hosed.
8943955d011SMarcel Moolenaar 		 *
89506b9b3e0SSimon J. Gerraty 		 * XXX: This will cause all made children to go in
89606b9b3e0SSimon J. Gerraty 		 * the OODATE variable, even if they're not touched,
89706b9b3e0SSimon J. Gerraty 		 * if RECHECK isn't defined, since cgn->mtime is set
89806b9b3e0SSimon J. Gerraty 		 * to now in Make_Update. According to some people,
89906b9b3e0SSimon J. Gerraty 		 * this is good...
9003955d011SMarcel Moolenaar 		 */
901dba7b0efSSimon J. Gerraty 		Var_Append(pgn, OODATE, child);
9023955d011SMarcel Moolenaar 	}
9033955d011SMarcel Moolenaar }
9042c3632d1SSimon J. Gerraty 
90506b9b3e0SSimon J. Gerraty /*
90606b9b3e0SSimon J. Gerraty  * Set up the ALLSRC and OODATE variables. Sad to say, it must be
9073955d011SMarcel Moolenaar  * done separately, rather than while traversing the graph. This is
9083955d011SMarcel Moolenaar  * because Make defined OODATE to contain all sources whose modification
9093955d011SMarcel Moolenaar  * times were later than that of the target, *not* those sources that
9103955d011SMarcel Moolenaar  * were out-of-date. Since in both compatibility and native modes,
9113955d011SMarcel Moolenaar  * the modification time of the parent isn't found until the child
9123955d011SMarcel Moolenaar  * has been dealt with, we have to wait until now to fill in the
9133955d011SMarcel Moolenaar  * variable. As for ALLSRC, the ordering is important and not
9143955d011SMarcel Moolenaar  * guaranteed when in native mode, so it must be set here, too.
9153955d011SMarcel Moolenaar  *
9163955d011SMarcel Moolenaar  * If the node is a .JOIN node, its TARGET variable will be set to
9173955d011SMarcel Moolenaar  * match its ALLSRC variable.
9183955d011SMarcel Moolenaar  */
9193955d011SMarcel Moolenaar void
920b0c40a00SSimon J. Gerraty GNode_SetLocalVars(GNode *gn)
9213955d011SMarcel Moolenaar {
922956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
923956e45f6SSimon J. Gerraty 
924*12904384SSimon J. Gerraty 	if (gn->flags.doneAllsrc)
9253955d011SMarcel Moolenaar 		return;
9263955d011SMarcel Moolenaar 
927956e45f6SSimon J. Gerraty 	UnmarkChildren(gn);
92806b9b3e0SSimon J. Gerraty 	for (ln = gn->children.first; ln != NULL; ln = ln->next)
929956e45f6SSimon J. Gerraty 		MakeAddAllSrc(ln->datum, gn);
9303955d011SMarcel Moolenaar 
931dba7b0efSSimon J. Gerraty 	if (!Var_Exists(gn, OODATE))
932dba7b0efSSimon J. Gerraty 		Var_Set(gn, OODATE, "");
933dba7b0efSSimon J. Gerraty 	if (!Var_Exists(gn, ALLSRC))
934dba7b0efSSimon J. Gerraty 		Var_Set(gn, ALLSRC, "");
9353955d011SMarcel Moolenaar 
936956e45f6SSimon J. Gerraty 	if (gn->type & OP_JOIN)
937dba7b0efSSimon J. Gerraty 		Var_Set(gn, TARGET, GNode_VarAllsrc(gn));
938*12904384SSimon J. Gerraty 	gn->flags.doneAllsrc = true;
9393955d011SMarcel Moolenaar }
9402c3632d1SSimon J. Gerraty 
941b0c40a00SSimon J. Gerraty static bool
94206b9b3e0SSimon J. Gerraty MakeBuildChild(GNode *cn, GNodeListNode *toBeMadeNext)
9433955d011SMarcel Moolenaar {
9443955d011SMarcel Moolenaar 
945e2eeea75SSimon J. Gerraty 	if (DEBUG(MAKE)) {
946e2eeea75SSimon J. Gerraty 		debug_printf("MakeBuildChild: inspect %s%s, ",
947e2eeea75SSimon J. Gerraty 		    cn->name, cn->cohort_num);
948e2eeea75SSimon J. Gerraty 		GNode_FprintDetails(opts.debug_file, "", cn, "\n");
949e2eeea75SSimon J. Gerraty 	}
95006b9b3e0SSimon J. Gerraty 	if (GNode_IsReady(cn))
951b0c40a00SSimon J. Gerraty 		return false;
9523955d011SMarcel Moolenaar 
9533955d011SMarcel Moolenaar 	/* If this node is on the RHS of a .ORDER, check LHSs. */
954e2eeea75SSimon J. Gerraty 	if (IsWaitingForOrder(cn)) {
9553955d011SMarcel Moolenaar 		/* Can't build this (or anything else in this child list) yet */
9563955d011SMarcel Moolenaar 		cn->made = DEFERRED;
957b0c40a00SSimon J. Gerraty 		return false;	/* but keep looking */
9583955d011SMarcel Moolenaar 	}
9593955d011SMarcel Moolenaar 
96006b9b3e0SSimon J. Gerraty 	DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n",
96106b9b3e0SSimon J. Gerraty 	    cn->name, cn->cohort_num);
9623955d011SMarcel Moolenaar 
9633955d011SMarcel Moolenaar 	cn->made = REQUESTED;
96406b9b3e0SSimon J. Gerraty 	if (toBeMadeNext == NULL)
96506b9b3e0SSimon J. Gerraty 		Lst_Append(&toBeMade, cn);
9663955d011SMarcel Moolenaar 	else
96706b9b3e0SSimon J. Gerraty 		Lst_InsertBefore(&toBeMade, toBeMadeNext, cn);
9683955d011SMarcel Moolenaar 
96906b9b3e0SSimon J. Gerraty 	if (cn->unmade_cohorts != 0) {
97006b9b3e0SSimon J. Gerraty 		ListNode *ln;
97106b9b3e0SSimon J. Gerraty 
97206b9b3e0SSimon J. Gerraty 		for (ln = cn->cohorts.first; ln != NULL; ln = ln->next)
973dba7b0efSSimon J. Gerraty 			if (MakeBuildChild(ln->datum, toBeMadeNext))
97406b9b3e0SSimon J. Gerraty 				break;
97506b9b3e0SSimon J. Gerraty 	}
9763955d011SMarcel Moolenaar 
9773955d011SMarcel Moolenaar 	/*
978956e45f6SSimon J. Gerraty 	 * If this node is a .WAIT node with unmade children
9793955d011SMarcel Moolenaar 	 * then don't add the next sibling.
9803955d011SMarcel Moolenaar 	 */
9813955d011SMarcel Moolenaar 	return cn->type & OP_WAIT && cn->unmade > 0;
9823955d011SMarcel Moolenaar }
9833955d011SMarcel Moolenaar 
984e2eeea75SSimon J. Gerraty /* When a .ORDER LHS node completes, we do this on each RHS. */
985dba7b0efSSimon J. Gerraty static void
98606b9b3e0SSimon J. Gerraty MakeBuildParent(GNode *pn, GNodeListNode *toBeMadeNext)
9873955d011SMarcel Moolenaar {
9883955d011SMarcel Moolenaar 	if (pn->made != DEFERRED)
989dba7b0efSSimon J. Gerraty 		return;
9903955d011SMarcel Moolenaar 
991dba7b0efSSimon J. Gerraty 	if (!MakeBuildChild(pn, toBeMadeNext)) {
99206b9b3e0SSimon J. Gerraty 		/* When this node is built, reschedule its parents. */
993*12904384SSimon J. Gerraty 		pn->flags.doneOrder = true;
9943955d011SMarcel Moolenaar 	}
9953955d011SMarcel Moolenaar }
9963955d011SMarcel Moolenaar 
99706b9b3e0SSimon J. Gerraty static void
99806b9b3e0SSimon J. Gerraty MakeChildren(GNode *gn)
99906b9b3e0SSimon J. Gerraty {
100006b9b3e0SSimon J. Gerraty 	GNodeListNode *toBeMadeNext = toBeMade.first;
100106b9b3e0SSimon J. Gerraty 	GNodeListNode *ln;
100206b9b3e0SSimon J. Gerraty 
100306b9b3e0SSimon J. Gerraty 	for (ln = gn->children.first; ln != NULL; ln = ln->next)
1004dba7b0efSSimon J. Gerraty 		if (MakeBuildChild(ln->datum, toBeMadeNext))
100506b9b3e0SSimon J. Gerraty 			break;
100606b9b3e0SSimon J. Gerraty }
100706b9b3e0SSimon J. Gerraty 
100806b9b3e0SSimon J. Gerraty /*
100906b9b3e0SSimon J. Gerraty  * Start as many jobs as possible, taking them from the toBeMade queue.
1010956e45f6SSimon J. Gerraty  *
1011e2eeea75SSimon J. Gerraty  * If the -q option was given, no job will be started,
1012956e45f6SSimon J. Gerraty  * but as soon as an out-of-date target is found, this function
1013b0c40a00SSimon J. Gerraty  * returns true. In all other cases, this function returns false.
1014956e45f6SSimon J. Gerraty  */
1015b0c40a00SSimon J. Gerraty static bool
10163955d011SMarcel Moolenaar MakeStartJobs(void)
10173955d011SMarcel Moolenaar {
10183955d011SMarcel Moolenaar 	GNode *gn;
1019b0c40a00SSimon J. Gerraty 	bool have_token = false;
10203955d011SMarcel Moolenaar 
102106b9b3e0SSimon J. Gerraty 	while (!Lst_IsEmpty(&toBeMade)) {
102206b9b3e0SSimon J. Gerraty 		/*
102306b9b3e0SSimon J. Gerraty 		 * Get token now to avoid cycling job-list when we only
102406b9b3e0SSimon J. Gerraty 		 * have 1 token
102506b9b3e0SSimon J. Gerraty 		 */
10263955d011SMarcel Moolenaar 		if (!have_token && !Job_TokenWithdraw())
10273955d011SMarcel Moolenaar 			break;
1028b0c40a00SSimon J. Gerraty 		have_token = true;
10293955d011SMarcel Moolenaar 
103006b9b3e0SSimon J. Gerraty 		gn = Lst_Dequeue(&toBeMade);
1031956e45f6SSimon J. Gerraty 		DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
10323955d011SMarcel Moolenaar 
10333955d011SMarcel Moolenaar 		if (gn->made != REQUESTED) {
103406b9b3e0SSimon J. Gerraty 			/*
103506b9b3e0SSimon J. Gerraty 			 * XXX: Replace %d with string representation;
103606b9b3e0SSimon J. Gerraty 			 * see made_name.
103706b9b3e0SSimon J. Gerraty 			 */
1038956e45f6SSimon J. Gerraty 			DEBUG1(MAKE, "state %d\n", gn->made);
10393955d011SMarcel Moolenaar 
10403955d011SMarcel Moolenaar 			make_abort(gn, __LINE__);
10413955d011SMarcel Moolenaar 		}
10423955d011SMarcel Moolenaar 
1043e2eeea75SSimon J. Gerraty 		if (gn->checked_seqno == checked_seqno) {
104406b9b3e0SSimon J. Gerraty 			/*
104506b9b3e0SSimon J. Gerraty 			 * We've already looked at this node since a job
104606b9b3e0SSimon J. Gerraty 			 * finished...
104706b9b3e0SSimon J. Gerraty 			 */
104806b9b3e0SSimon J. Gerraty 			DEBUG2(MAKE, "already checked %s%s\n", gn->name,
104906b9b3e0SSimon J. Gerraty 			    gn->cohort_num);
10503955d011SMarcel Moolenaar 			gn->made = DEFERRED;
10513955d011SMarcel Moolenaar 			continue;
10523955d011SMarcel Moolenaar 		}
1053e2eeea75SSimon J. Gerraty 		gn->checked_seqno = checked_seqno;
10543955d011SMarcel Moolenaar 
10553955d011SMarcel Moolenaar 		if (gn->unmade != 0) {
10563955d011SMarcel Moolenaar 			/*
105706b9b3e0SSimon J. Gerraty 			 * We can't build this yet, add all unmade children
105806b9b3e0SSimon J. Gerraty 			 * to toBeMade, just before the current first element.
10593955d011SMarcel Moolenaar 			 */
10603955d011SMarcel Moolenaar 			gn->made = DEFERRED;
106106b9b3e0SSimon J. Gerraty 
106206b9b3e0SSimon J. Gerraty 			MakeChildren(gn);
106306b9b3e0SSimon J. Gerraty 
10643955d011SMarcel Moolenaar 			/* and drop this node on the floor */
106506b9b3e0SSimon J. Gerraty 			DEBUG2(MAKE, "dropped %s%s\n", gn->name,
106606b9b3e0SSimon J. Gerraty 			    gn->cohort_num);
10673955d011SMarcel Moolenaar 			continue;
10683955d011SMarcel Moolenaar 		}
10693955d011SMarcel Moolenaar 
10703955d011SMarcel Moolenaar 		gn->made = BEINGMADE;
1071e2eeea75SSimon J. Gerraty 		if (GNode_IsOODate(gn)) {
1072956e45f6SSimon J. Gerraty 			DEBUG0(MAKE, "out-of-date\n");
1073e2eeea75SSimon J. Gerraty 			if (opts.queryFlag)
1074b0c40a00SSimon J. Gerraty 				return true;
1075b0c40a00SSimon J. Gerraty 			GNode_SetLocalVars(gn);
10763955d011SMarcel Moolenaar 			Job_Make(gn);
1077b0c40a00SSimon J. Gerraty 			have_token = false;
10783955d011SMarcel Moolenaar 		} else {
1079956e45f6SSimon J. Gerraty 			DEBUG0(MAKE, "up-to-date\n");
10803955d011SMarcel Moolenaar 			gn->made = UPTODATE;
10813955d011SMarcel Moolenaar 			if (gn->type & OP_JOIN) {
10823955d011SMarcel Moolenaar 				/*
108306b9b3e0SSimon J. Gerraty 				 * Even for an up-to-date .JOIN node, we
1084dba7b0efSSimon J. Gerraty 				 * need it to have its local variables so
108506b9b3e0SSimon J. Gerraty 				 * references to it get the correct value
1086dba7b0efSSimon J. Gerraty 				 * for .TARGET when building up the local
108706b9b3e0SSimon J. Gerraty 				 * variables of its parent(s)...
10883955d011SMarcel Moolenaar 				 */
1089b0c40a00SSimon J. Gerraty 				GNode_SetLocalVars(gn);
10903955d011SMarcel Moolenaar 			}
10913955d011SMarcel Moolenaar 			Make_Update(gn);
10923955d011SMarcel Moolenaar 		}
10933955d011SMarcel Moolenaar 	}
10943955d011SMarcel Moolenaar 
10953955d011SMarcel Moolenaar 	if (have_token)
10963955d011SMarcel Moolenaar 		Job_TokenReturn();
10973955d011SMarcel Moolenaar 
1098b0c40a00SSimon J. Gerraty 	return false;
10993955d011SMarcel Moolenaar }
11002c3632d1SSimon J. Gerraty 
1101e2eeea75SSimon J. Gerraty /* Print the status of a .ORDER node. */
1102956e45f6SSimon J. Gerraty static void
1103956e45f6SSimon J. Gerraty MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
11043955d011SMarcel Moolenaar {
110506b9b3e0SSimon J. Gerraty 	if (!GNode_IsWaitingFor(ogn))
1106956e45f6SSimon J. Gerraty 		return;
11073955d011SMarcel Moolenaar 
11082c3632d1SSimon J. Gerraty 	printf("    `%s%s' has .ORDER dependency against %s%s ",
11092c3632d1SSimon J. Gerraty 	    gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
11102c3632d1SSimon J. Gerraty 	GNode_FprintDetails(stdout, "(", ogn, ")\n");
11112c3632d1SSimon J. Gerraty 
1112956e45f6SSimon J. Gerraty 	if (DEBUG(MAKE) && opts.debug_file != stdout) {
1113956e45f6SSimon J. Gerraty 		debug_printf("    `%s%s' has .ORDER dependency against %s%s ",
11142c3632d1SSimon J. Gerraty 		    gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1115956e45f6SSimon J. Gerraty 		GNode_FprintDetails(opts.debug_file, "(", ogn, ")\n");
11162c3632d1SSimon J. Gerraty 	}
11173955d011SMarcel Moolenaar }
11183955d011SMarcel Moolenaar 
1119956e45f6SSimon J. Gerraty static void
1120956e45f6SSimon J. Gerraty MakePrintStatusOrder(GNode *gn)
11213955d011SMarcel Moolenaar {
1122956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
112306b9b3e0SSimon J. Gerraty 	for (ln = gn->order_pred.first; ln != NULL; ln = ln->next)
1124956e45f6SSimon J. Gerraty 		MakePrintStatusOrderNode(ln->datum, gn);
1125956e45f6SSimon J. Gerraty }
11263955d011SMarcel Moolenaar 
1127956e45f6SSimon J. Gerraty static void MakePrintStatusList(GNodeList *, int *);
1128956e45f6SSimon J. Gerraty 
112906b9b3e0SSimon J. Gerraty /*
113006b9b3e0SSimon J. Gerraty  * Print the status of a top-level node, viz. it being up-to-date already
1131956e45f6SSimon J. Gerraty  * or not created due to an error in a lower level.
1132956e45f6SSimon J. Gerraty  */
1133b0c40a00SSimon J. Gerraty static bool
1134956e45f6SSimon J. Gerraty MakePrintStatus(GNode *gn, int *errors)
1135956e45f6SSimon J. Gerraty {
1136*12904384SSimon J. Gerraty 	if (gn->flags.doneCycle) {
113706b9b3e0SSimon J. Gerraty 		/*
113806b9b3e0SSimon J. Gerraty 		 * We've completely processed this node before, don't do
113906b9b3e0SSimon J. Gerraty 		 * it again.
114006b9b3e0SSimon J. Gerraty 		 */
1141b0c40a00SSimon J. Gerraty 		return false;
114206b9b3e0SSimon J. Gerraty 	}
11433955d011SMarcel Moolenaar 
11443955d011SMarcel Moolenaar 	if (gn->unmade == 0) {
1145*12904384SSimon J. Gerraty 		gn->flags.doneCycle = true;
11463955d011SMarcel Moolenaar 		switch (gn->made) {
11473955d011SMarcel Moolenaar 		case UPTODATE:
114806b9b3e0SSimon J. Gerraty 			printf("`%s%s' is up to date.\n", gn->name,
114906b9b3e0SSimon J. Gerraty 			    gn->cohort_num);
11503955d011SMarcel Moolenaar 			break;
11513955d011SMarcel Moolenaar 		case MADE:
11523955d011SMarcel Moolenaar 			break;
11533955d011SMarcel Moolenaar 		case UNMADE:
11543955d011SMarcel Moolenaar 		case DEFERRED:
11553955d011SMarcel Moolenaar 		case REQUESTED:
11563955d011SMarcel Moolenaar 		case BEINGMADE:
11573955d011SMarcel Moolenaar 			(*errors)++;
115806b9b3e0SSimon J. Gerraty 			printf("`%s%s' was not built", gn->name,
115906b9b3e0SSimon J. Gerraty 			    gn->cohort_num);
11602c3632d1SSimon J. Gerraty 			GNode_FprintDetails(stdout, " (", gn, ")!\n");
1161956e45f6SSimon J. Gerraty 			if (DEBUG(MAKE) && opts.debug_file != stdout) {
116206b9b3e0SSimon J. Gerraty 				debug_printf("`%s%s' was not built", gn->name,
116306b9b3e0SSimon J. Gerraty 				    gn->cohort_num);
116406b9b3e0SSimon J. Gerraty 				GNode_FprintDetails(opts.debug_file, " (", gn,
116506b9b3e0SSimon J. Gerraty 				    ")!\n");
11662c3632d1SSimon J. Gerraty 			}
11673955d011SMarcel Moolenaar 			/* Most likely problem is actually caused by .ORDER */
1168956e45f6SSimon J. Gerraty 			MakePrintStatusOrder(gn);
11693955d011SMarcel Moolenaar 			break;
11703955d011SMarcel Moolenaar 		default:
11713955d011SMarcel Moolenaar 			/* Errors - already counted */
11723955d011SMarcel Moolenaar 			printf("`%s%s' not remade because of errors.\n",
11733955d011SMarcel Moolenaar 			    gn->name, gn->cohort_num);
1174956e45f6SSimon J. Gerraty 			if (DEBUG(MAKE) && opts.debug_file != stdout)
117506b9b3e0SSimon J. Gerraty 				debug_printf(
117606b9b3e0SSimon J. Gerraty 				    "`%s%s' not remade because of errors.\n",
11773955d011SMarcel Moolenaar 				    gn->name, gn->cohort_num);
11783955d011SMarcel Moolenaar 			break;
11793955d011SMarcel Moolenaar 		}
1180b0c40a00SSimon J. Gerraty 		return false;
11813955d011SMarcel Moolenaar 	}
11823955d011SMarcel Moolenaar 
1183956e45f6SSimon J. Gerraty 	DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
11843955d011SMarcel Moolenaar 	    gn->name, gn->cohort_num, gn->unmade);
11853955d011SMarcel Moolenaar 	/*
11863955d011SMarcel Moolenaar 	 * If printing cycles and came to one that has unmade children,
11873955d011SMarcel Moolenaar 	 * print out the cycle by recursing on its children.
11883955d011SMarcel Moolenaar 	 */
1189*12904384SSimon J. Gerraty 	if (!gn->flags.cycle) {
1190e2eeea75SSimon J. Gerraty 		/* First time we've seen this node, check all children */
1191*12904384SSimon J. Gerraty 		gn->flags.cycle = true;
119206b9b3e0SSimon J. Gerraty 		MakePrintStatusList(&gn->children, errors);
11933955d011SMarcel Moolenaar 		/* Mark that this node needn't be processed again */
1194*12904384SSimon J. Gerraty 		gn->flags.doneCycle = true;
1195b0c40a00SSimon J. Gerraty 		return false;
11963955d011SMarcel Moolenaar 	}
11973955d011SMarcel Moolenaar 
11983955d011SMarcel Moolenaar 	/* Only output the error once per node */
1199*12904384SSimon J. Gerraty 	gn->flags.doneCycle = true;
12003955d011SMarcel Moolenaar 	Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
12013955d011SMarcel Moolenaar 	if ((*errors)++ > 100)
12023955d011SMarcel Moolenaar 		/* Abandon the whole error report */
1203b0c40a00SSimon J. Gerraty 		return true;
12043955d011SMarcel Moolenaar 
12053955d011SMarcel Moolenaar 	/* Reporting for our children will give the rest of the loop */
120606b9b3e0SSimon J. Gerraty 	MakePrintStatusList(&gn->children, errors);
1207b0c40a00SSimon J. Gerraty 	return false;
12083955d011SMarcel Moolenaar }
12092c3632d1SSimon J. Gerraty 
1210956e45f6SSimon J. Gerraty static void
1211956e45f6SSimon J. Gerraty MakePrintStatusList(GNodeList *gnodes, int *errors)
1212956e45f6SSimon J. Gerraty {
1213956e45f6SSimon J. Gerraty 	GNodeListNode *ln;
121406b9b3e0SSimon J. Gerraty 
1215956e45f6SSimon J. Gerraty 	for (ln = gnodes->first; ln != NULL; ln = ln->next)
1216956e45f6SSimon J. Gerraty 		if (MakePrintStatus(ln->datum, errors))
1217956e45f6SSimon J. Gerraty 			break;
1218956e45f6SSimon J. Gerraty }
12193955d011SMarcel Moolenaar 
1220e2eeea75SSimon J. Gerraty static void
1221e2eeea75SSimon J. Gerraty ExamineLater(GNodeList *examine, GNodeList *toBeExamined)
1222e2eeea75SSimon J. Gerraty {
1223e2eeea75SSimon J. Gerraty 	ListNode *ln;
1224e2eeea75SSimon J. Gerraty 
1225e2eeea75SSimon J. Gerraty 	for (ln = toBeExamined->first; ln != NULL; ln = ln->next) {
1226e2eeea75SSimon J. Gerraty 		GNode *gn = ln->datum;
1227e2eeea75SSimon J. Gerraty 
1228*12904384SSimon J. Gerraty 		if (gn->flags.remake)
1229e2eeea75SSimon J. Gerraty 			continue;
1230e2eeea75SSimon J. Gerraty 		if (gn->type & (OP_USE | OP_USEBEFORE))
1231e2eeea75SSimon J. Gerraty 			continue;
1232e2eeea75SSimon J. Gerraty 
1233e2eeea75SSimon J. Gerraty 		DEBUG2(MAKE, "ExamineLater: need to examine \"%s%s\"\n",
1234e2eeea75SSimon J. Gerraty 		    gn->name, gn->cohort_num);
1235e2eeea75SSimon J. Gerraty 		Lst_Enqueue(examine, gn);
1236e2eeea75SSimon J. Gerraty 	}
1237e2eeea75SSimon J. Gerraty }
1238e2eeea75SSimon J. Gerraty 
123906b9b3e0SSimon J. Gerraty /*
124006b9b3e0SSimon J. Gerraty  * Expand .USE nodes and create a new targets list.
12413955d011SMarcel Moolenaar  *
12423955d011SMarcel Moolenaar  * Input:
12433955d011SMarcel Moolenaar  *	targs		the initial list of targets
12443955d011SMarcel Moolenaar  */
12453955d011SMarcel Moolenaar void
1246956e45f6SSimon J. Gerraty Make_ExpandUse(GNodeList *targs)
12473955d011SMarcel Moolenaar {
124806b9b3e0SSimon J. Gerraty 	GNodeList examine = LST_INIT;	/* Queue of targets to examine */
124906b9b3e0SSimon J. Gerraty 	Lst_AppendAll(&examine, targs);
12503955d011SMarcel Moolenaar 
12513955d011SMarcel Moolenaar 	/*
125206b9b3e0SSimon J. Gerraty 	 * Make an initial downward pass over the graph, marking nodes to
125306b9b3e0SSimon J. Gerraty 	 * be made as we go down.
125406b9b3e0SSimon J. Gerraty 	 *
125506b9b3e0SSimon J. Gerraty 	 * We call Suff_FindDeps to find where a node is and to get some
125606b9b3e0SSimon J. Gerraty 	 * children for it if it has none and also has no commands. If the
125706b9b3e0SSimon J. Gerraty 	 * node is a leaf, we stick it on the toBeMade queue to be looked
125806b9b3e0SSimon J. Gerraty 	 * at in a minute, otherwise we add its children to our queue and
125906b9b3e0SSimon J. Gerraty 	 * go on about our business.
12603955d011SMarcel Moolenaar 	 */
126106b9b3e0SSimon J. Gerraty 	while (!Lst_IsEmpty(&examine)) {
126206b9b3e0SSimon J. Gerraty 		GNode *gn = Lst_Dequeue(&examine);
12633955d011SMarcel Moolenaar 
1264*12904384SSimon J. Gerraty 		if (gn->flags.remake)
12653955d011SMarcel Moolenaar 			/* We've looked at this one already */
12663955d011SMarcel Moolenaar 			continue;
1267*12904384SSimon J. Gerraty 		gn->flags.remake = true;
1268956e45f6SSimon J. Gerraty 		DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
12693955d011SMarcel Moolenaar 		    gn->name, gn->cohort_num);
12703955d011SMarcel Moolenaar 
12712c3632d1SSimon J. Gerraty 		if (gn->type & OP_DOUBLEDEP)
127206b9b3e0SSimon J. Gerraty 			Lst_PrependAll(&examine, &gn->cohorts);
12733955d011SMarcel Moolenaar 
12743955d011SMarcel Moolenaar 		/*
127506b9b3e0SSimon J. Gerraty 		 * Apply any .USE rules before looking for implicit
127606b9b3e0SSimon J. Gerraty 		 * dependencies to make sure everything has commands that
127706b9b3e0SSimon J. Gerraty 		 * should.
127806b9b3e0SSimon J. Gerraty 		 *
12793955d011SMarcel Moolenaar 		 * Make sure that the TARGET is set, so that we can make
12803955d011SMarcel Moolenaar 		 * expansions.
12813955d011SMarcel Moolenaar 		 */
12823955d011SMarcel Moolenaar 		if (gn->type & OP_ARCHV) {
1283e2eeea75SSimon J. Gerraty 			char *eoa = strchr(gn->name, '(');
1284e2eeea75SSimon J. Gerraty 			char *eon = strchr(gn->name, ')');
12853955d011SMarcel Moolenaar 			if (eoa == NULL || eon == NULL)
12863955d011SMarcel Moolenaar 				continue;
12873955d011SMarcel Moolenaar 			*eoa = '\0';
12883955d011SMarcel Moolenaar 			*eon = '\0';
1289dba7b0efSSimon J. Gerraty 			Var_Set(gn, MEMBER, eoa + 1);
1290dba7b0efSSimon J. Gerraty 			Var_Set(gn, ARCHIVE, gn->name);
12913955d011SMarcel Moolenaar 			*eoa = '(';
12923955d011SMarcel Moolenaar 			*eon = ')';
12933955d011SMarcel Moolenaar 		}
12943955d011SMarcel Moolenaar 
1295b0c40a00SSimon J. Gerraty 		Dir_UpdateMTime(gn, false);
1296dba7b0efSSimon J. Gerraty 		Var_Set(gn, TARGET, GNode_Path(gn));
1297956e45f6SSimon J. Gerraty 		UnmarkChildren(gn);
1298956e45f6SSimon J. Gerraty 		HandleUseNodes(gn);
12993955d011SMarcel Moolenaar 
1300e2eeea75SSimon J. Gerraty 		if (!(gn->type & OP_MADE))
13013955d011SMarcel Moolenaar 			Suff_FindDeps(gn);
13023955d011SMarcel Moolenaar 		else {
1303e2eeea75SSimon J. Gerraty 			PretendAllChildrenAreMade(gn);
130406b9b3e0SSimon J. Gerraty 			if (gn->unmade != 0) {
130506b9b3e0SSimon J. Gerraty 				printf(
130606b9b3e0SSimon J. Gerraty 				    "Warning: "
130706b9b3e0SSimon J. Gerraty 				    "%s%s still has %d unmade children\n",
13083955d011SMarcel Moolenaar 				    gn->name, gn->cohort_num, gn->unmade);
13093955d011SMarcel Moolenaar 			}
13103955d011SMarcel Moolenaar 		}
13113955d011SMarcel Moolenaar 
131206b9b3e0SSimon J. Gerraty 		if (gn->unmade != 0)
131306b9b3e0SSimon J. Gerraty 			ExamineLater(&examine, &gn->children);
131406b9b3e0SSimon J. Gerraty 	}
131506b9b3e0SSimon J. Gerraty 
131606b9b3e0SSimon J. Gerraty 	Lst_Done(&examine);
13173955d011SMarcel Moolenaar }
13183955d011SMarcel Moolenaar 
1319956e45f6SSimon J. Gerraty /* Make the .WAIT node depend on the previous children */
1320956e45f6SSimon J. Gerraty static void
1321956e45f6SSimon J. Gerraty add_wait_dependency(GNodeListNode *owln, GNode *wn)
13223955d011SMarcel Moolenaar {
1323956e45f6SSimon J. Gerraty 	GNodeListNode *cln;
1324956e45f6SSimon J. Gerraty 	GNode *cn;
13253955d011SMarcel Moolenaar 
1326956e45f6SSimon J. Gerraty 	for (cln = owln; (cn = cln->datum) != wn; cln = cln->next) {
1327956e45f6SSimon J. Gerraty 		DEBUG3(MAKE, ".WAIT: add dependency %s%s -> %s\n",
13283955d011SMarcel Moolenaar 		    cn->name, cn->cohort_num, wn->name);
13293955d011SMarcel Moolenaar 
1330956e45f6SSimon J. Gerraty 		/* XXX: This pattern should be factored out, it repeats often */
133106b9b3e0SSimon J. Gerraty 		Lst_Append(&wn->children, cn);
13323955d011SMarcel Moolenaar 		wn->unmade++;
133306b9b3e0SSimon J. Gerraty 		Lst_Append(&cn->parents, wn);
1334956e45f6SSimon J. Gerraty 	}
13353955d011SMarcel Moolenaar }
13363955d011SMarcel Moolenaar 
1337956e45f6SSimon J. Gerraty /* Convert .WAIT nodes into dependencies. */
13383955d011SMarcel Moolenaar static void
1339956e45f6SSimon J. Gerraty Make_ProcessWait(GNodeList *targs)
13403955d011SMarcel Moolenaar {
13413955d011SMarcel Moolenaar 	GNode *pgn;		/* 'parent' node we are examining */
1342956e45f6SSimon J. Gerraty 	GNodeListNode *owln;	/* Previous .WAIT node */
134306b9b3e0SSimon J. Gerraty 	GNodeList examine;	/* List of targets to examine */
13443955d011SMarcel Moolenaar 
13453955d011SMarcel Moolenaar 	/*
13463955d011SMarcel Moolenaar 	 * We need all the nodes to have a common parent in order for the
13473955d011SMarcel Moolenaar 	 * .WAIT and .ORDER scheduling to work.
13483955d011SMarcel Moolenaar 	 * Perhaps this should be done earlier...
13493955d011SMarcel Moolenaar 	 */
13503955d011SMarcel Moolenaar 
1351e2eeea75SSimon J. Gerraty 	pgn = GNode_New(".MAIN");
1352*12904384SSimon J. Gerraty 	pgn->flags.remake = true;
13533955d011SMarcel Moolenaar 	pgn->type = OP_PHONY | OP_DEPENDS;
13543955d011SMarcel Moolenaar 	/* Get it displayed in the diag dumps */
13552c3632d1SSimon J. Gerraty 	Lst_Prepend(Targ_List(), pgn);
13563955d011SMarcel Moolenaar 
1357956e45f6SSimon J. Gerraty 	{
1358956e45f6SSimon J. Gerraty 		GNodeListNode *ln;
1359956e45f6SSimon J. Gerraty 		for (ln = targs->first; ln != NULL; ln = ln->next) {
1360956e45f6SSimon J. Gerraty 			GNode *cgn = ln->datum;
1361956e45f6SSimon J. Gerraty 
136206b9b3e0SSimon J. Gerraty 			Lst_Append(&pgn->children, cgn);
136306b9b3e0SSimon J. Gerraty 			Lst_Append(&cgn->parents, pgn);
1364956e45f6SSimon J. Gerraty 			pgn->unmade++;
1365956e45f6SSimon J. Gerraty 		}
1366956e45f6SSimon J. Gerraty 	}
13673955d011SMarcel Moolenaar 
13683955d011SMarcel Moolenaar 	/* Start building with the 'dummy' .MAIN' node */
13693955d011SMarcel Moolenaar 	MakeBuildChild(pgn, NULL);
13703955d011SMarcel Moolenaar 
137106b9b3e0SSimon J. Gerraty 	Lst_Init(&examine);
137206b9b3e0SSimon J. Gerraty 	Lst_Append(&examine, pgn);
13733955d011SMarcel Moolenaar 
137406b9b3e0SSimon J. Gerraty 	while (!Lst_IsEmpty(&examine)) {
1375956e45f6SSimon J. Gerraty 		GNodeListNode *ln;
1376956e45f6SSimon J. Gerraty 
137706b9b3e0SSimon J. Gerraty 		pgn = Lst_Dequeue(&examine);
13783955d011SMarcel Moolenaar 
13793955d011SMarcel Moolenaar 		/* We only want to process each child-list once */
1380*12904384SSimon J. Gerraty 		if (pgn->flags.doneWait)
13813955d011SMarcel Moolenaar 			continue;
1382*12904384SSimon J. Gerraty 		pgn->flags.doneWait = true;
1383956e45f6SSimon J. Gerraty 		DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
13843955d011SMarcel Moolenaar 
13852c3632d1SSimon J. Gerraty 		if (pgn->type & OP_DOUBLEDEP)
138606b9b3e0SSimon J. Gerraty 			Lst_PrependAll(&examine, &pgn->cohorts);
13873955d011SMarcel Moolenaar 
138806b9b3e0SSimon J. Gerraty 		owln = pgn->children.first;
138906b9b3e0SSimon J. Gerraty 		for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
1390956e45f6SSimon J. Gerraty 			GNode *cgn = ln->datum;
13913955d011SMarcel Moolenaar 			if (cgn->type & OP_WAIT) {
1392956e45f6SSimon J. Gerraty 				add_wait_dependency(owln, cgn);
13933955d011SMarcel Moolenaar 				owln = ln;
13943955d011SMarcel Moolenaar 			} else {
139506b9b3e0SSimon J. Gerraty 				Lst_Append(&examine, cgn);
13963955d011SMarcel Moolenaar 			}
13973955d011SMarcel Moolenaar 		}
13983955d011SMarcel Moolenaar 	}
13993955d011SMarcel Moolenaar 
140006b9b3e0SSimon J. Gerraty 	Lst_Done(&examine);
14013955d011SMarcel Moolenaar }
14023955d011SMarcel Moolenaar 
140306b9b3e0SSimon J. Gerraty /*
140406b9b3e0SSimon J. Gerraty  * Initialize the nodes to remake and the list of nodes which are ready to
140506b9b3e0SSimon J. Gerraty  * be made by doing a breadth-first traversal of the graph starting from the
140606b9b3e0SSimon J. Gerraty  * nodes in the given list. Once this traversal is finished, all the 'leaves'
140706b9b3e0SSimon J. Gerraty  * of the graph are in the toBeMade queue.
140806b9b3e0SSimon J. Gerraty  *
140906b9b3e0SSimon J. Gerraty  * Using this queue and the Job module, work back up the graph, calling on
141006b9b3e0SSimon J. Gerraty  * MakeStartJobs to keep the job table as full as possible.
14113955d011SMarcel Moolenaar  *
14123955d011SMarcel Moolenaar  * Input:
14133955d011SMarcel Moolenaar  *	targs		the initial list of targets
14143955d011SMarcel Moolenaar  *
14153955d011SMarcel Moolenaar  * Results:
1416b0c40a00SSimon J. Gerraty  *	True if work was done, false otherwise.
14173955d011SMarcel Moolenaar  *
14183955d011SMarcel Moolenaar  * Side Effects:
14193955d011SMarcel Moolenaar  *	The make field of all nodes involved in the creation of the given
14203955d011SMarcel Moolenaar  *	targets is set to 1. The toBeMade list is set to contain all the
14213955d011SMarcel Moolenaar  *	'leaves' of these subgraphs.
14223955d011SMarcel Moolenaar  */
1423b0c40a00SSimon J. Gerraty bool
1424956e45f6SSimon J. Gerraty Make_Run(GNodeList *targs)
14253955d011SMarcel Moolenaar {
14263955d011SMarcel Moolenaar 	int errors;		/* Number of errors the Job module reports */
14273955d011SMarcel Moolenaar 
14283955d011SMarcel Moolenaar 	/* Start trying to make the current targets... */
142906b9b3e0SSimon J. Gerraty 	Lst_Init(&toBeMade);
14303955d011SMarcel Moolenaar 
14313955d011SMarcel Moolenaar 	Make_ExpandUse(targs);
14323955d011SMarcel Moolenaar 	Make_ProcessWait(targs);
14333955d011SMarcel Moolenaar 
14343955d011SMarcel Moolenaar 	if (DEBUG(MAKE)) {
1435956e45f6SSimon J. Gerraty 		debug_printf("#***# full graph\n");
14363955d011SMarcel Moolenaar 		Targ_PrintGraph(1);
14373955d011SMarcel Moolenaar 	}
14383955d011SMarcel Moolenaar 
1439956e45f6SSimon J. Gerraty 	if (opts.queryFlag) {
14403955d011SMarcel Moolenaar 		/*
144106b9b3e0SSimon J. Gerraty 		 * We wouldn't do any work unless we could start some jobs
144206b9b3e0SSimon J. Gerraty 		 * in the next loop... (we won't actually start any, of
144306b9b3e0SSimon J. Gerraty 		 * course, this is just to see if any of the targets was out
144406b9b3e0SSimon J. Gerraty 		 * of date)
14453955d011SMarcel Moolenaar 		 */
14463841c287SSimon J. Gerraty 		return MakeStartJobs();
14473955d011SMarcel Moolenaar 	}
14483955d011SMarcel Moolenaar 	/*
14493955d011SMarcel Moolenaar 	 * Initialization. At the moment, no jobs are running and until some
14503955d011SMarcel Moolenaar 	 * get started, nothing will happen since the remaining upward
14513955d011SMarcel Moolenaar 	 * traversal of the graph is performed by the routines in job.c upon
14523955d011SMarcel Moolenaar 	 * the finishing of a job. So we fill the Job table as much as we can
14533955d011SMarcel Moolenaar 	 * before going into our loop.
14543955d011SMarcel Moolenaar 	 */
14553955d011SMarcel Moolenaar 	(void)MakeStartJobs();
14563955d011SMarcel Moolenaar 
14573955d011SMarcel Moolenaar 	/*
14583955d011SMarcel Moolenaar 	 * Main Loop: The idea here is that the ending of jobs will take
145906b9b3e0SSimon J. Gerraty 	 * care of the maintenance of data structures and the waiting for
146006b9b3e0SSimon J. Gerraty 	 * output will cause us to be idle most of the time while our
146106b9b3e0SSimon J. Gerraty 	 * children run as much as possible. Because the job table is kept
146206b9b3e0SSimon J. Gerraty 	 * as full as possible, the only time when it will be empty is when
146306b9b3e0SSimon J. Gerraty 	 * all the jobs which need running have been run, so that is the end
146406b9b3e0SSimon J. Gerraty 	 * condition of this loop. Note that the Job module will exit if
146506b9b3e0SSimon J. Gerraty 	 * there were any errors unless the keepgoing flag was given.
14663955d011SMarcel Moolenaar 	 */
146706b9b3e0SSimon J. Gerraty 	while (!Lst_IsEmpty(&toBeMade) || jobTokensRunning > 0) {
14683955d011SMarcel Moolenaar 		Job_CatchOutput();
14693955d011SMarcel Moolenaar 		(void)MakeStartJobs();
14703955d011SMarcel Moolenaar 	}
14713955d011SMarcel Moolenaar 
14723955d011SMarcel Moolenaar 	errors = Job_Finish();
14733955d011SMarcel Moolenaar 
14743955d011SMarcel Moolenaar 	/*
14753955d011SMarcel Moolenaar 	 * Print the final status of each target. E.g. if it wasn't made
14763955d011SMarcel Moolenaar 	 * because some inferior reported an error.
14773955d011SMarcel Moolenaar 	 */
1478956e45f6SSimon J. Gerraty 	DEBUG1(MAKE, "done: errors %d\n", errors);
14793955d011SMarcel Moolenaar 	if (errors == 0) {
1480956e45f6SSimon J. Gerraty 		MakePrintStatusList(targs, &errors);
14813955d011SMarcel Moolenaar 		if (DEBUG(MAKE)) {
1482956e45f6SSimon J. Gerraty 			debug_printf("done: errors %d\n", errors);
1483e2eeea75SSimon J. Gerraty 			if (errors > 0)
14843955d011SMarcel Moolenaar 				Targ_PrintGraph(4);
14853955d011SMarcel Moolenaar 		}
14863955d011SMarcel Moolenaar 	}
1487e2eeea75SSimon J. Gerraty 	return errors > 0;
14883955d011SMarcel Moolenaar }
1489