xref: /dflybsd-src/contrib/bmake/make.c (revision 9e7ae5a0527a977cab412aede3a532cfe2903bbb)
1*6eef5f0cSAntonio Huete Jimenez /*	$NetBSD: make.c,v 1.257 2022/09/27 17:46:58 rillig Exp $	*/
201e196c8SJohn Marino 
301e196c8SJohn Marino /*
401e196c8SJohn Marino  * Copyright (c) 1988, 1989, 1990, 1993
501e196c8SJohn Marino  *	The Regents of the University of California.  All rights reserved.
601e196c8SJohn Marino  *
701e196c8SJohn Marino  * This code is derived from software contributed to Berkeley by
801e196c8SJohn Marino  * Adam de Boor.
901e196c8SJohn Marino  *
1001e196c8SJohn Marino  * Redistribution and use in source and binary forms, with or without
1101e196c8SJohn Marino  * modification, are permitted provided that the following conditions
1201e196c8SJohn Marino  * are met:
1301e196c8SJohn Marino  * 1. Redistributions of source code must retain the above copyright
1401e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer.
1501e196c8SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
1601e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
1701e196c8SJohn Marino  *    documentation and/or other materials provided with the distribution.
1801e196c8SJohn Marino  * 3. Neither the name of the University nor the names of its contributors
1901e196c8SJohn Marino  *    may be used to endorse or promote products derived from this software
2001e196c8SJohn Marino  *    without specific prior written permission.
2101e196c8SJohn Marino  *
2201e196c8SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2301e196c8SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2401e196c8SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2501e196c8SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2601e196c8SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2701e196c8SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2801e196c8SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2901e196c8SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3001e196c8SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3101e196c8SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3201e196c8SJohn Marino  * SUCH DAMAGE.
3301e196c8SJohn Marino  */
3401e196c8SJohn Marino 
3501e196c8SJohn Marino /*
3601e196c8SJohn Marino  * Copyright (c) 1989 by Berkeley Softworks
3701e196c8SJohn Marino  * All rights reserved.
3801e196c8SJohn Marino  *
3901e196c8SJohn Marino  * This code is derived from software contributed to Berkeley by
4001e196c8SJohn Marino  * Adam de Boor.
4101e196c8SJohn Marino  *
4201e196c8SJohn Marino  * Redistribution and use in source and binary forms, with or without
4301e196c8SJohn Marino  * modification, are permitted provided that the following conditions
4401e196c8SJohn Marino  * are met:
4501e196c8SJohn Marino  * 1. Redistributions of source code must retain the above copyright
4601e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer.
4701e196c8SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
4801e196c8SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
4901e196c8SJohn Marino  *    documentation and/or other materials provided with the distribution.
5001e196c8SJohn Marino  * 3. All advertising materials mentioning features or use of this software
5101e196c8SJohn Marino  *    must display the following acknowledgement:
5201e196c8SJohn Marino  *	This product includes software developed by the University of
5301e196c8SJohn Marino  *	California, Berkeley and its contributors.
5401e196c8SJohn Marino  * 4. Neither the name of the University nor the names of its contributors
5501e196c8SJohn Marino  *    may be used to endorse or promote products derived from this software
5601e196c8SJohn Marino  *    without specific prior written permission.
5701e196c8SJohn Marino  *
5801e196c8SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5901e196c8SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6001e196c8SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6101e196c8SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6201e196c8SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6301e196c8SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6401e196c8SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6501e196c8SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6601e196c8SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6701e196c8SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6801e196c8SJohn Marino  * SUCH DAMAGE.
6901e196c8SJohn Marino  */
7001e196c8SJohn Marino 
71a34d5fb1SAntonio Huete Jimenez /*
72a34d5fb1SAntonio Huete Jimenez  * Examination of targets and their suitability for creation.
7301e196c8SJohn Marino  *
7401e196c8SJohn Marino  * Interface:
75*6eef5f0cSAntonio Huete Jimenez  *	Make_Run	Initialize things for the module. Returns true if
76a34d5fb1SAntonio Huete Jimenez  *			work was (or would have been) done.
7701e196c8SJohn Marino  *
78a34d5fb1SAntonio Huete Jimenez  *	Make_Update	After a target is made, update all its parents.
79a34d5fb1SAntonio Huete Jimenez  *			Perform various bookkeeping chores like the updating
80a34d5fb1SAntonio Huete Jimenez  *			of the youngestChild field of the parent, filling
81a34d5fb1SAntonio Huete Jimenez  *			of the IMPSRC variable, etc. Place the parent on the
82a34d5fb1SAntonio Huete Jimenez  *			toBeMade queue if it should be.
8301e196c8SJohn Marino  *
84a34d5fb1SAntonio Huete Jimenez  *	GNode_UpdateYoungestChild
85a34d5fb1SAntonio Huete Jimenez  *			Update the node's youngestChild field based on the
86a34d5fb1SAntonio Huete Jimenez  *			child's modification time.
8701e196c8SJohn Marino  *
88*6eef5f0cSAntonio Huete Jimenez  *	GNode_SetLocalVars
89*6eef5f0cSAntonio Huete Jimenez  *			Set up the various local variables for a
9001e196c8SJohn Marino  *			target, including the .ALLSRC variable, making
9101e196c8SJohn Marino  *			sure that any variable that needs to exist
9201e196c8SJohn Marino  *			at the very least has the empty value.
9301e196c8SJohn Marino  *
94a34d5fb1SAntonio Huete Jimenez  *	GNode_IsOODate	Determine if a target is out-of-date.
9501e196c8SJohn Marino  *
9601e196c8SJohn Marino  *	Make_HandleUse	See if a child is a .USE node for a parent
9701e196c8SJohn Marino  *			and perform the .USE actions if so.
9801e196c8SJohn Marino  *
9901e196c8SJohn Marino  *	Make_ExpandUse	Expand .USE nodes
10001e196c8SJohn Marino  */
10101e196c8SJohn Marino 
10201e196c8SJohn Marino #include "make.h"
10301e196c8SJohn Marino #include "dir.h"
10401e196c8SJohn Marino #include "job.h"
10501e196c8SJohn Marino 
106a34d5fb1SAntonio Huete Jimenez /*	"@(#)make.c	8.1 (Berkeley) 6/6/93"	*/
107*6eef5f0cSAntonio Huete Jimenez MAKE_RCSID("$NetBSD: make.c,v 1.257 2022/09/27 17:46:58 rillig Exp $");
10801e196c8SJohn Marino 
109a34d5fb1SAntonio Huete Jimenez /* Sequence # to detect recursion. */
110a34d5fb1SAntonio Huete Jimenez static unsigned int checked_seqno = 1;
111a34d5fb1SAntonio Huete Jimenez 
112a34d5fb1SAntonio Huete Jimenez /*
113a34d5fb1SAntonio Huete Jimenez  * The current fringe of the graph.
114a34d5fb1SAntonio Huete Jimenez  * These are nodes which await examination by MakeOODate.
115a34d5fb1SAntonio Huete Jimenez  * It is added to by Make_Update and subtracted from by MakeStartJobs
116a34d5fb1SAntonio Huete Jimenez  */
117a34d5fb1SAntonio Huete Jimenez static GNodeList toBeMade = LST_INIT;
118a34d5fb1SAntonio Huete Jimenez 
119a34d5fb1SAntonio Huete Jimenez 
120a34d5fb1SAntonio Huete Jimenez void
debug_printf(const char * fmt,...)121a34d5fb1SAntonio Huete Jimenez debug_printf(const char *fmt, ...)
122a34d5fb1SAntonio Huete Jimenez {
123*6eef5f0cSAntonio Huete Jimenez 	va_list ap;
124a34d5fb1SAntonio Huete Jimenez 
125*6eef5f0cSAntonio Huete Jimenez 	va_start(ap, fmt);
126*6eef5f0cSAntonio Huete Jimenez 	vfprintf(opts.debug_file, fmt, ap);
127*6eef5f0cSAntonio Huete Jimenez 	va_end(ap);
128a34d5fb1SAntonio Huete Jimenez }
12901e196c8SJohn Marino 
130*6eef5f0cSAntonio Huete Jimenez static const char *
GNodeType_ToString(GNodeType type,void ** freeIt)131*6eef5f0cSAntonio Huete Jimenez GNodeType_ToString(GNodeType type, void **freeIt)
13201e196c8SJohn Marino {
133*6eef5f0cSAntonio Huete Jimenez 	Buffer buf;
13401e196c8SJohn Marino 
135*6eef5f0cSAntonio Huete Jimenez 	Buf_InitSize(&buf, 32);
136*6eef5f0cSAntonio Huete Jimenez #define ADD(flag) Buf_AddFlag(&buf, (type & (flag)) != OP_NONE, #flag)
137*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_DEPENDS);
138*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_FORCE);
139*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_DOUBLEDEP);
140*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_OPTIONAL);
141*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_USE);
142*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_EXEC);
143*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_IGNORE);
144*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_PRECIOUS);
145*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_SILENT);
146*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_MAKE);
147*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_JOIN);
148*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_MADE);
149*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_SPECIAL);
150*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_USEBEFORE);
151*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_INVISIBLE);
152*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_NOTMAIN);
153*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_PHONY);
154*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_NOPATH);
155*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_WAIT);
156*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_NOMETA);
157*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_META);
158*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_NOMETA_CMP);
159*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_SUBMAKE);
160*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_TRANSFORM);
161*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_MEMBER);
162*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_LIB);
163*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_ARCHV);
164*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_HAS_COMMANDS);
165*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_SAVE_CMDS);
166*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_DEPS_FOUND);
167*6eef5f0cSAntonio Huete Jimenez 	ADD(OP_MARK);
168*6eef5f0cSAntonio Huete Jimenez #undef ADD
169*6eef5f0cSAntonio Huete Jimenez 	return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
17001e196c8SJohn Marino }
17101e196c8SJohn Marino 
172*6eef5f0cSAntonio Huete Jimenez static const char *
GNodeFlags_ToString(GNodeFlags flags,void ** freeIt)173*6eef5f0cSAntonio Huete Jimenez GNodeFlags_ToString(GNodeFlags flags, void **freeIt)
174*6eef5f0cSAntonio Huete Jimenez {
175*6eef5f0cSAntonio Huete Jimenez 	Buffer buf;
176a34d5fb1SAntonio Huete Jimenez 
177*6eef5f0cSAntonio Huete Jimenez 	Buf_InitSize(&buf, 32);
178*6eef5f0cSAntonio Huete Jimenez #define ADD(flag, name) Buf_AddFlag(&buf, flags.flag, name)
179*6eef5f0cSAntonio Huete Jimenez 	ADD(remake, "REMAKE");
180*6eef5f0cSAntonio Huete Jimenez 	ADD(childMade, "CHILDMADE");
181*6eef5f0cSAntonio Huete Jimenez 	ADD(force, "FORCE");
182*6eef5f0cSAntonio Huete Jimenez 	ADD(doneWait, "DONE_WAIT");
183*6eef5f0cSAntonio Huete Jimenez 	ADD(doneOrder, "DONE_ORDER");
184*6eef5f0cSAntonio Huete Jimenez 	ADD(fromDepend, "FROM_DEPEND");
185*6eef5f0cSAntonio Huete Jimenez 	ADD(doneAllsrc, "DONE_ALLSRC");
186*6eef5f0cSAntonio Huete Jimenez 	ADD(cycle, "CYCLE");
187*6eef5f0cSAntonio Huete Jimenez 	ADD(doneCycle, "DONECYCLE");
188*6eef5f0cSAntonio Huete Jimenez #undef ADD
189*6eef5f0cSAntonio Huete Jimenez 	return buf.len == 0 ? "none" : (*freeIt = Buf_DoneData(&buf));
190*6eef5f0cSAntonio Huete Jimenez }
191a34d5fb1SAntonio Huete Jimenez 
192a34d5fb1SAntonio Huete Jimenez void
GNode_FprintDetails(FILE * f,const char * prefix,const GNode * gn,const char * suffix)193a34d5fb1SAntonio Huete Jimenez GNode_FprintDetails(FILE *f, const char *prefix, const GNode *gn,
194a34d5fb1SAntonio Huete Jimenez 		    const char *suffix)
19501e196c8SJohn Marino {
196*6eef5f0cSAntonio Huete Jimenez 	void *type_freeIt = NULL;
197*6eef5f0cSAntonio Huete Jimenez 	void *flags_freeIt = NULL;
198a34d5fb1SAntonio Huete Jimenez 
199a34d5fb1SAntonio Huete Jimenez 	fprintf(f, "%s%s, type %s, flags %s%s",
200a34d5fb1SAntonio Huete Jimenez 	    prefix,
201a34d5fb1SAntonio Huete Jimenez 	    GNodeMade_Name(gn->made),
202*6eef5f0cSAntonio Huete Jimenez 	    GNodeType_ToString(gn->type, &type_freeIt),
203*6eef5f0cSAntonio Huete Jimenez 	    GNodeFlags_ToString(gn->flags, &flags_freeIt),
204a34d5fb1SAntonio Huete Jimenez 	    suffix);
205*6eef5f0cSAntonio Huete Jimenez 	free(type_freeIt);
206*6eef5f0cSAntonio Huete Jimenez 	free(flags_freeIt);
20701e196c8SJohn Marino }
208a34d5fb1SAntonio Huete Jimenez 
209*6eef5f0cSAntonio Huete Jimenez bool
GNode_ShouldExecute(GNode * gn)210a34d5fb1SAntonio Huete Jimenez GNode_ShouldExecute(GNode *gn)
211a34d5fb1SAntonio Huete Jimenez {
212a34d5fb1SAntonio Huete Jimenez 	return !((gn->type & OP_MAKE)
213a34d5fb1SAntonio Huete Jimenez 	    ? opts.noRecursiveExecute
214a34d5fb1SAntonio Huete Jimenez 	    : opts.noExecute);
215a34d5fb1SAntonio Huete Jimenez }
216a34d5fb1SAntonio Huete Jimenez 
217a34d5fb1SAntonio Huete Jimenez /* Update the youngest child of the node, according to the given child. */
218a34d5fb1SAntonio Huete Jimenez void
GNode_UpdateYoungestChild(GNode * gn,GNode * cgn)219a34d5fb1SAntonio Huete Jimenez GNode_UpdateYoungestChild(GNode *gn, GNode *cgn)
220a34d5fb1SAntonio Huete Jimenez {
221a34d5fb1SAntonio Huete Jimenez 	if (gn->youngestChild == NULL || cgn->mtime > gn->youngestChild->mtime)
222a34d5fb1SAntonio Huete Jimenez 		gn->youngestChild = cgn;
223a34d5fb1SAntonio Huete Jimenez }
224a34d5fb1SAntonio Huete Jimenez 
225*6eef5f0cSAntonio Huete Jimenez static bool
IsOODateRegular(GNode * gn)226a34d5fb1SAntonio Huete Jimenez IsOODateRegular(GNode *gn)
227a34d5fb1SAntonio Huete Jimenez {
228a34d5fb1SAntonio Huete Jimenez 	/* These rules are inherited from the original Make. */
229a34d5fb1SAntonio Huete Jimenez 
230a34d5fb1SAntonio Huete Jimenez 	if (gn->youngestChild != NULL) {
231a34d5fb1SAntonio Huete Jimenez 		if (gn->mtime < gn->youngestChild->mtime) {
232a34d5fb1SAntonio Huete Jimenez 			DEBUG1(MAKE, "modified before source \"%s\"...",
233a34d5fb1SAntonio Huete Jimenez 			    GNode_Path(gn->youngestChild));
234*6eef5f0cSAntonio Huete Jimenez 			return true;
235a34d5fb1SAntonio Huete Jimenez 		}
236*6eef5f0cSAntonio Huete Jimenez 		return false;
237a34d5fb1SAntonio Huete Jimenez 	}
238a34d5fb1SAntonio Huete Jimenez 
239a34d5fb1SAntonio Huete Jimenez 	if (gn->mtime == 0 && !(gn->type & OP_OPTIONAL)) {
240a34d5fb1SAntonio Huete Jimenez 		DEBUG0(MAKE, "nonexistent and no sources...");
241*6eef5f0cSAntonio Huete Jimenez 		return true;
242a34d5fb1SAntonio Huete Jimenez 	}
243a34d5fb1SAntonio Huete Jimenez 
244a34d5fb1SAntonio Huete Jimenez 	if (gn->type & OP_DOUBLEDEP) {
245a34d5fb1SAntonio Huete Jimenez 		DEBUG0(MAKE, ":: operator and no sources...");
246*6eef5f0cSAntonio Huete Jimenez 		return true;
247a34d5fb1SAntonio Huete Jimenez 	}
248a34d5fb1SAntonio Huete Jimenez 
249*6eef5f0cSAntonio Huete Jimenez 	return false;
25001e196c8SJohn Marino }
25101e196c8SJohn Marino 
25201e196c8SJohn Marino /*
253a34d5fb1SAntonio Huete Jimenez  * See if the node is out of date with respect to its sources.
25401e196c8SJohn Marino  *
25501e196c8SJohn Marino  * Used by Make_Run when deciding which nodes to place on the
256a34d5fb1SAntonio Huete Jimenez  * toBeMade queue initially and by Make_Update to screen out .USE and
257a34d5fb1SAntonio Huete Jimenez  * .EXEC nodes. In the latter case, however, any other sort of node
25801e196c8SJohn Marino  * must be considered out-of-date since at least one of its children
25901e196c8SJohn Marino  * will have been recreated.
26001e196c8SJohn Marino  *
261a34d5fb1SAntonio Huete Jimenez  * The mtime field of the node and the youngestChild field of its parents
262a34d5fb1SAntonio Huete Jimenez  * may be changed.
26301e196c8SJohn Marino  */
264*6eef5f0cSAntonio Huete Jimenez bool
GNode_IsOODate(GNode * gn)265a34d5fb1SAntonio Huete Jimenez GNode_IsOODate(GNode *gn)
26601e196c8SJohn Marino {
267*6eef5f0cSAntonio Huete Jimenez 	bool oodate;
26801e196c8SJohn Marino 
26901e196c8SJohn Marino 	/*
27001e196c8SJohn Marino 	 * Certain types of targets needn't even be sought as their datedness
27101e196c8SJohn Marino 	 * doesn't depend on their modification time...
27201e196c8SJohn Marino 	 */
273a34d5fb1SAntonio Huete Jimenez 	if (!(gn->type & (OP_JOIN | OP_USE | OP_USEBEFORE | OP_EXEC))) {
274*6eef5f0cSAntonio Huete Jimenez 		Dir_UpdateMTime(gn, true);
27501e196c8SJohn Marino 		if (DEBUG(MAKE)) {
276a34d5fb1SAntonio Huete Jimenez 			if (gn->mtime != 0)
277a34d5fb1SAntonio Huete Jimenez 				debug_printf("modified %s...",
278a34d5fb1SAntonio Huete Jimenez 				    Targ_FmtTime(gn->mtime));
279a34d5fb1SAntonio Huete Jimenez 			else
280a34d5fb1SAntonio Huete Jimenez 				debug_printf("nonexistent...");
28101e196c8SJohn Marino 		}
28201e196c8SJohn Marino 	}
28301e196c8SJohn Marino 
28401e196c8SJohn Marino 	/*
28501e196c8SJohn Marino 	 * A target is remade in one of the following circumstances:
286a34d5fb1SAntonio Huete Jimenez 	 *
287a34d5fb1SAntonio Huete Jimenez 	 *	its modification time is smaller than that of its youngest
288a34d5fb1SAntonio Huete Jimenez 	 *	child and it would actually be run (has commands or is not
289a34d5fb1SAntonio Huete Jimenez 	 *	GNode_IsTarget)
290a34d5fb1SAntonio Huete Jimenez 	 *
29101e196c8SJohn Marino 	 *	it's the object of a force operator
29201e196c8SJohn Marino 	 *
293a34d5fb1SAntonio Huete Jimenez 	 *	it has no children, was on the lhs of an operator and doesn't
294a34d5fb1SAntonio Huete Jimenez 	 *	exist already.
29501e196c8SJohn Marino 	 *
296a34d5fb1SAntonio Huete Jimenez 	 * Libraries are only considered out-of-date if the archive module
297a34d5fb1SAntonio Huete Jimenez 	 * says they are.
298a34d5fb1SAntonio Huete Jimenez 	 *
299a34d5fb1SAntonio Huete Jimenez 	 * These weird rules are brought to you by Backward-Compatibility
300a34d5fb1SAntonio Huete Jimenez 	 * and the strange people who wrote 'Make'.
30101e196c8SJohn Marino 	 */
30201e196c8SJohn Marino 	if (gn->type & (OP_USE | OP_USEBEFORE)) {
30301e196c8SJohn Marino 		/*
30401e196c8SJohn Marino 		 * If the node is a USE node it is *never* out of date
30501e196c8SJohn Marino 		 * no matter *what*.
30601e196c8SJohn Marino 		 */
307a34d5fb1SAntonio Huete Jimenez 		DEBUG0(MAKE, ".USE node...");
308*6eef5f0cSAntonio Huete Jimenez 		oodate = false;
309a34d5fb1SAntonio Huete Jimenez 	} else if ((gn->type & OP_LIB) && (gn->mtime == 0 || Arch_IsLib(gn))) {
310a34d5fb1SAntonio Huete Jimenez 		DEBUG0(MAKE, "library...");
31101e196c8SJohn Marino 
31201e196c8SJohn Marino 		/*
31301e196c8SJohn Marino 		 * always out of date if no children and :: target
314a34d5fb1SAntonio Huete Jimenez 		 * or nonexistent.
31501e196c8SJohn Marino 		 */
31601e196c8SJohn Marino 		oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
317a34d5fb1SAntonio Huete Jimenez 			  (gn->youngestChild == NULL &&
318a34d5fb1SAntonio Huete Jimenez 			   (gn->type & OP_DOUBLEDEP)));
31901e196c8SJohn Marino 	} else if (gn->type & OP_JOIN) {
32001e196c8SJohn Marino 		/*
32101e196c8SJohn Marino 		 * A target with the .JOIN attribute is only considered
32201e196c8SJohn Marino 		 * out-of-date if any of its children was out-of-date.
32301e196c8SJohn Marino 		 */
324a34d5fb1SAntonio Huete Jimenez 		DEBUG0(MAKE, ".JOIN node...");
325a34d5fb1SAntonio Huete Jimenez 		DEBUG1(MAKE, "source %smade...",
326*6eef5f0cSAntonio Huete Jimenez 		    gn->flags.childMade ? "" : "not ");
327*6eef5f0cSAntonio Huete Jimenez 		oodate = gn->flags.childMade;
32801e196c8SJohn Marino 	} else if (gn->type & (OP_FORCE | OP_EXEC | OP_PHONY)) {
32901e196c8SJohn Marino 		/*
330a34d5fb1SAntonio Huete Jimenez 		 * A node which is the object of the force (!) operator or
331a34d5fb1SAntonio Huete Jimenez 		 * which has the .EXEC attribute is always considered
332a34d5fb1SAntonio Huete Jimenez 		 * out-of-date.
33301e196c8SJohn Marino 		 */
33401e196c8SJohn Marino 		if (DEBUG(MAKE)) {
33501e196c8SJohn Marino 			if (gn->type & OP_FORCE) {
336a34d5fb1SAntonio Huete Jimenez 				debug_printf("! operator...");
33701e196c8SJohn Marino 			} else if (gn->type & OP_PHONY) {
338a34d5fb1SAntonio Huete Jimenez 				debug_printf(".PHONY node...");
33901e196c8SJohn Marino 			} else {
340a34d5fb1SAntonio Huete Jimenez 				debug_printf(".EXEC node...");
34101e196c8SJohn Marino 			}
34201e196c8SJohn Marino 		}
343*6eef5f0cSAntonio Huete Jimenez 		oodate = true;
344a34d5fb1SAntonio Huete Jimenez 	} else if (IsOODateRegular(gn)) {
345*6eef5f0cSAntonio Huete Jimenez 		oodate = true;
34601e196c8SJohn Marino 	} else {
34701e196c8SJohn Marino 		/*
348a34d5fb1SAntonio Huete Jimenez 		 * When a nonexistent child with no sources
34901e196c8SJohn Marino 		 * (such as a typically used FORCE source) has been made and
35001e196c8SJohn Marino 		 * the target of the child (usually a directory) has the same
351a34d5fb1SAntonio Huete Jimenez 		 * timestamp as the timestamp just given to the nonexistent
352a34d5fb1SAntonio Huete Jimenez 		 * child after it was considered made.
35301e196c8SJohn Marino 		 */
35401e196c8SJohn Marino 		if (DEBUG(MAKE)) {
355*6eef5f0cSAntonio Huete Jimenez 			if (gn->flags.force)
356a34d5fb1SAntonio Huete Jimenez 				debug_printf("non existing child...");
35701e196c8SJohn Marino 		}
358*6eef5f0cSAntonio Huete Jimenez 		oodate = gn->flags.force;
35901e196c8SJohn Marino 	}
36001e196c8SJohn Marino 
36101e196c8SJohn Marino #ifdef USE_META
362*6eef5f0cSAntonio Huete Jimenez 	if (useMeta)
36301e196c8SJohn Marino 		oodate = meta_oodate(gn, oodate);
36401e196c8SJohn Marino #endif
36501e196c8SJohn Marino 
36601e196c8SJohn Marino 	/*
36701e196c8SJohn Marino 	 * If the target isn't out-of-date, the parents need to know its
36801e196c8SJohn Marino 	 * modification time. Note that targets that appear to be out-of-date
369a34d5fb1SAntonio Huete Jimenez 	 * but aren't, because they have no commands and are GNode_IsTarget,
370a34d5fb1SAntonio Huete Jimenez 	 * have their mtime stay below their children's mtime to keep parents
371a34d5fb1SAntonio Huete Jimenez 	 * from thinking they're out-of-date.
37201e196c8SJohn Marino 	 */
37301e196c8SJohn Marino 	if (!oodate) {
374a34d5fb1SAntonio Huete Jimenez 		GNodeListNode *ln;
375a34d5fb1SAntonio Huete Jimenez 		for (ln = gn->parents.first; ln != NULL; ln = ln->next)
376a34d5fb1SAntonio Huete Jimenez 			GNode_UpdateYoungestChild(ln->datum, gn);
37701e196c8SJohn Marino 	}
37801e196c8SJohn Marino 
379ca58f742SDaniel Fojt 	return oodate;
38001e196c8SJohn Marino }
38101e196c8SJohn Marino 
382a34d5fb1SAntonio Huete Jimenez static void
PretendAllChildrenAreMade(GNode * pgn)383a34d5fb1SAntonio Huete Jimenez PretendAllChildrenAreMade(GNode *pgn)
38401e196c8SJohn Marino {
385a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
38601e196c8SJohn Marino 
387a34d5fb1SAntonio Huete Jimenez 	for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
388a34d5fb1SAntonio Huete Jimenez 		GNode *cgn = ln->datum;
389a34d5fb1SAntonio Huete Jimenez 
390a34d5fb1SAntonio Huete Jimenez 		/* This may also update cgn->path. */
391*6eef5f0cSAntonio Huete Jimenez 		Dir_UpdateMTime(cgn, false);
392a34d5fb1SAntonio Huete Jimenez 		GNode_UpdateYoungestChild(pgn, cgn);
39301e196c8SJohn Marino 		pgn->unmade--;
39401e196c8SJohn Marino 	}
395a34d5fb1SAntonio Huete Jimenez }
396a34d5fb1SAntonio Huete Jimenez 
397a34d5fb1SAntonio Huete Jimenez /*
398a34d5fb1SAntonio Huete Jimenez  * Called by Make_Run and SuffApplyTransform on the downward pass to handle
399a34d5fb1SAntonio Huete Jimenez  * .USE and transformation nodes, by copying the child node's commands, type
400a34d5fb1SAntonio Huete Jimenez  * flags and children to the parent node.
40101e196c8SJohn Marino  *
402a34d5fb1SAntonio Huete Jimenez  * A .USE node is much like an explicit transformation rule, except its
403a34d5fb1SAntonio Huete Jimenez  * commands are always added to the target node, even if the target already
404a34d5fb1SAntonio Huete Jimenez  * has commands.
40501e196c8SJohn Marino  *
40601e196c8SJohn Marino  * Input:
407a34d5fb1SAntonio Huete Jimenez  *	cgn		The source node, which is either a .USE/.USEBEFORE
408a34d5fb1SAntonio Huete Jimenez  *			node or a transformation node (OP_TRANSFORM).
409a34d5fb1SAntonio Huete Jimenez  *	pgn		The target node
41001e196c8SJohn Marino  */
41101e196c8SJohn Marino void
Make_HandleUse(GNode * cgn,GNode * pgn)41201e196c8SJohn Marino Make_HandleUse(GNode *cgn, GNode *pgn)
41301e196c8SJohn Marino {
414a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;	/* An element in the children list */
41501e196c8SJohn Marino 
41601e196c8SJohn Marino #ifdef DEBUG_SRC
417a34d5fb1SAntonio Huete Jimenez 	if (!(cgn->type & (OP_USE | OP_USEBEFORE | OP_TRANSFORM))) {
418a34d5fb1SAntonio Huete Jimenez 		debug_printf("Make_HandleUse: called for plain node %s\n",
419a34d5fb1SAntonio Huete Jimenez 		    cgn->name);
420a34d5fb1SAntonio Huete Jimenez 		/* XXX: debug mode should not affect control flow */
42101e196c8SJohn Marino 		return;
42201e196c8SJohn Marino 	}
42301e196c8SJohn Marino #endif
42401e196c8SJohn Marino 
425a34d5fb1SAntonio Huete Jimenez 	if ((cgn->type & (OP_USE | OP_USEBEFORE)) ||
426a34d5fb1SAntonio Huete Jimenez 	    Lst_IsEmpty(&pgn->commands)) {
42701e196c8SJohn Marino 		if (cgn->type & OP_USEBEFORE) {
428a34d5fb1SAntonio Huete Jimenez 			/* .USEBEFORE */
429a34d5fb1SAntonio Huete Jimenez 			Lst_PrependAll(&pgn->commands, &cgn->commands);
43001e196c8SJohn Marino 		} else {
431a34d5fb1SAntonio Huete Jimenez 			/* .USE, or target has no commands */
432a34d5fb1SAntonio Huete Jimenez 			Lst_AppendAll(&pgn->commands, &cgn->commands);
43301e196c8SJohn Marino 		}
43401e196c8SJohn Marino 	}
43501e196c8SJohn Marino 
436a34d5fb1SAntonio Huete Jimenez 	for (ln = cgn->children.first; ln != NULL; ln = ln->next) {
437a34d5fb1SAntonio Huete Jimenez 		GNode *gn = ln->datum;
43801e196c8SJohn Marino 
43901e196c8SJohn Marino 		/*
44001e196c8SJohn Marino 		 * Expand variables in the .USE node's name
44101e196c8SJohn Marino 		 * and save the unexpanded form.
44201e196c8SJohn Marino 		 * We don't need to do this for commands.
44301e196c8SJohn Marino 		 * They get expanded properly when we execute.
44401e196c8SJohn Marino 		 */
44501e196c8SJohn Marino 		if (gn->uname == NULL) {
44601e196c8SJohn Marino 			gn->uname = gn->name;
44701e196c8SJohn Marino 		} else {
44801e196c8SJohn Marino 			free(gn->name);
44901e196c8SJohn Marino 		}
450a34d5fb1SAntonio Huete Jimenez 		(void)Var_Subst(gn->uname, pgn, VARE_WANTRES, &gn->name);
451a34d5fb1SAntonio Huete Jimenez 		/* TODO: handle errors */
452a34d5fb1SAntonio Huete Jimenez 		if (gn->uname != NULL && strcmp(gn->name, gn->uname) != 0) {
45301e196c8SJohn Marino 			/* See if we have a target for this node. */
454a34d5fb1SAntonio Huete Jimenez 			GNode *tgn = Targ_FindNode(gn->name);
45501e196c8SJohn Marino 			if (tgn != NULL)
45601e196c8SJohn Marino 				gn = tgn;
45701e196c8SJohn Marino 		}
45801e196c8SJohn Marino 
459a34d5fb1SAntonio Huete Jimenez 		Lst_Append(&pgn->children, gn);
460a34d5fb1SAntonio Huete Jimenez 		Lst_Append(&gn->parents, pgn);
461a34d5fb1SAntonio Huete Jimenez 		pgn->unmade++;
46201e196c8SJohn Marino 	}
46301e196c8SJohn Marino 
464a34d5fb1SAntonio Huete Jimenez 	pgn->type |=
465*6eef5f0cSAntonio Huete Jimenez 	    cgn->type & (unsigned)~(OP_OPMASK | OP_USE | OP_USEBEFORE | OP_TRANSFORM);
46601e196c8SJohn Marino }
46701e196c8SJohn Marino 
468a34d5fb1SAntonio Huete Jimenez /*
469a34d5fb1SAntonio Huete Jimenez  * Used by Make_Run on the downward pass to handle .USE nodes. Should be
470a34d5fb1SAntonio Huete Jimenez  * called before the children are enqueued to be looked at by MakeAddChild.
471a34d5fb1SAntonio Huete Jimenez  *
472a34d5fb1SAntonio Huete Jimenez  * For a .USE child, the commands, type flags and children are copied to the
473a34d5fb1SAntonio Huete Jimenez  * parent node, and since the relation to the .USE node is then no longer
474a34d5fb1SAntonio Huete Jimenez  * needed, that relation is removed.
47501e196c8SJohn Marino  *
47601e196c8SJohn Marino  * Input:
477a34d5fb1SAntonio Huete Jimenez  *	cgn		the child, which may be a .USE node
478a34d5fb1SAntonio Huete Jimenez  *	pgn		the current parent
47901e196c8SJohn Marino  */
480a34d5fb1SAntonio Huete Jimenez static void
MakeHandleUse(GNode * cgn,GNode * pgn,GNodeListNode * ln)481a34d5fb1SAntonio Huete Jimenez MakeHandleUse(GNode *cgn, GNode *pgn, GNodeListNode *ln)
48201e196c8SJohn Marino {
483*6eef5f0cSAntonio Huete Jimenez 	bool unmarked;
48401e196c8SJohn Marino 
485a34d5fb1SAntonio Huete Jimenez 	unmarked = !(cgn->type & OP_MARK);
48601e196c8SJohn Marino 	cgn->type |= OP_MARK;
48701e196c8SJohn Marino 
488a34d5fb1SAntonio Huete Jimenez 	if (!(cgn->type & (OP_USE | OP_USEBEFORE)))
489a34d5fb1SAntonio Huete Jimenez 		return;
49001e196c8SJohn Marino 
49101e196c8SJohn Marino 	if (unmarked)
49201e196c8SJohn Marino 		Make_HandleUse(cgn, pgn);
49301e196c8SJohn Marino 
49401e196c8SJohn Marino 	/*
49501e196c8SJohn Marino 	 * This child node is now "made", so we decrement the count of
49601e196c8SJohn Marino 	 * unmade children in the parent... We also remove the child
49701e196c8SJohn Marino 	 * from the parent's list to accurately reflect the number of decent
49801e196c8SJohn Marino 	 * children the parent has. This is used by Make_Run to decide
49901e196c8SJohn Marino 	 * whether to queue the parent or examine its children...
50001e196c8SJohn Marino 	 */
501a34d5fb1SAntonio Huete Jimenez 	Lst_Remove(&pgn->children, ln);
50201e196c8SJohn Marino 	pgn->unmade--;
50301e196c8SJohn Marino }
504a34d5fb1SAntonio Huete Jimenez 
505a34d5fb1SAntonio Huete Jimenez static void
HandleUseNodes(GNode * gn)506a34d5fb1SAntonio Huete Jimenez HandleUseNodes(GNode *gn)
507a34d5fb1SAntonio Huete Jimenez {
508a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln, *nln;
509a34d5fb1SAntonio Huete Jimenez 	for (ln = gn->children.first; ln != NULL; ln = nln) {
510a34d5fb1SAntonio Huete Jimenez 		nln = ln->next;
511a34d5fb1SAntonio Huete Jimenez 		MakeHandleUse(ln->datum, gn, ln);
512a34d5fb1SAntonio Huete Jimenez 	}
51301e196c8SJohn Marino }
51401e196c8SJohn Marino 
51501e196c8SJohn Marino 
516a34d5fb1SAntonio Huete Jimenez /*
517a34d5fb1SAntonio Huete Jimenez  * Check the modification time of a gnode, and update it if necessary.
518a34d5fb1SAntonio Huete Jimenez  * Return 0 if the gnode does not exist, or its filesystem time if it does.
51901e196c8SJohn Marino  */
52001e196c8SJohn Marino time_t
Make_Recheck(GNode * gn)52101e196c8SJohn Marino Make_Recheck(GNode *gn)
52201e196c8SJohn Marino {
523a34d5fb1SAntonio Huete Jimenez 	time_t mtime;
524a34d5fb1SAntonio Huete Jimenez 
525*6eef5f0cSAntonio Huete Jimenez 	Dir_UpdateMTime(gn, true);
526a34d5fb1SAntonio Huete Jimenez 	mtime = gn->mtime;
52701e196c8SJohn Marino 
52801e196c8SJohn Marino #ifndef RECHECK
52901e196c8SJohn Marino 	/*
53001e196c8SJohn Marino 	 * We can't re-stat the thing, but we can at least take care of rules
53101e196c8SJohn Marino 	 * where a target depends on a source that actually creates the
53201e196c8SJohn Marino 	 * target, but only if it has changed, e.g.
53301e196c8SJohn Marino 	 *
53401e196c8SJohn Marino 	 * parse.h : parse.o
53501e196c8SJohn Marino 	 *
53601e196c8SJohn Marino 	 * parse.o : parse.y
53701e196c8SJohn Marino 	 *		yacc -d parse.y
53801e196c8SJohn Marino 	 *		cc -c y.tab.c
53901e196c8SJohn Marino 	 *		mv y.tab.o parse.o
54001e196c8SJohn Marino 	 *		cmp -s y.tab.h parse.h || mv y.tab.h parse.h
54101e196c8SJohn Marino 	 *
54201e196c8SJohn Marino 	 * In this case, if the definitions produced by yacc haven't changed
54301e196c8SJohn Marino 	 * from before, parse.h won't have been updated and gn->mtime will
54401e196c8SJohn Marino 	 * reflect the current modification time for parse.h. This is
545a34d5fb1SAntonio Huete Jimenez 	 * something of a kludge, I admit, but it's a useful one.
54601e196c8SJohn Marino 	 *
547a34d5fb1SAntonio Huete Jimenez 	 * XXX: People like to use a rule like "FRC:" to force things that
548a34d5fb1SAntonio Huete Jimenez 	 * depend on FRC to be made, so we have to check for gn->children
549a34d5fb1SAntonio Huete Jimenez 	 * being empty as well.
55001e196c8SJohn Marino 	 */
55101e196c8SJohn Marino 	if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
55201e196c8SJohn Marino 		gn->mtime = now;
55301e196c8SJohn Marino 	}
55401e196c8SJohn Marino #else
55501e196c8SJohn Marino 	/*
55601e196c8SJohn Marino 	 * This is what Make does and it's actually a good thing, as it
55701e196c8SJohn Marino 	 * allows rules like
55801e196c8SJohn Marino 	 *
55901e196c8SJohn Marino 	 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
56001e196c8SJohn Marino 	 *
56101e196c8SJohn Marino 	 * to function as intended. Unfortunately, thanks to the stateless
56201e196c8SJohn Marino 	 * nature of NFS (by which I mean the loose coupling of two clients
563a34d5fb1SAntonio Huete Jimenez 	 * using the same file from a common server), there are times when
564a34d5fb1SAntonio Huete Jimenez 	 * the modification time of a file created on a remote machine
565a34d5fb1SAntonio Huete Jimenez 	 * will not be modified before the local stat() implied by the
566a34d5fb1SAntonio Huete Jimenez 	 * Dir_UpdateMTime occurs, thus leading us to believe that the file
56701e196c8SJohn Marino 	 * is unchanged, wreaking havoc with files that depend on this one.
56801e196c8SJohn Marino 	 *
56901e196c8SJohn Marino 	 * I have decided it is better to make too much than to make too
57001e196c8SJohn Marino 	 * little, so this stuff is commented out unless you're sure it's ok.
57101e196c8SJohn Marino 	 * -- ardeb 1/12/88
57201e196c8SJohn Marino 	 */
57301e196c8SJohn Marino 	/*
574a34d5fb1SAntonio Huete Jimenez 	 * Christos, 4/9/92: If we are saving commands, pretend that
575a34d5fb1SAntonio Huete Jimenez 	 * the target is made now. Otherwise archives with '...' rules
57601e196c8SJohn Marino 	 * don't work!
57701e196c8SJohn Marino 	 */
578a34d5fb1SAntonio Huete Jimenez 	if (!GNode_ShouldExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
57901e196c8SJohn Marino 	    (mtime == 0 && !(gn->type & OP_WAIT))) {
580a34d5fb1SAntonio Huete Jimenez 		DEBUG2(MAKE, " recheck(%s): update time from %s to now\n",
581a34d5fb1SAntonio Huete Jimenez 		    gn->name,
582a34d5fb1SAntonio Huete Jimenez 		    gn->mtime == 0 ? "nonexistent" : Targ_FmtTime(gn->mtime));
58301e196c8SJohn Marino 		gn->mtime = now;
584a34d5fb1SAntonio Huete Jimenez 	} else {
585a34d5fb1SAntonio Huete Jimenez 		DEBUG2(MAKE, " recheck(%s): current update time: %s\n",
58601e196c8SJohn Marino 		    gn->name, Targ_FmtTime(gn->mtime));
58701e196c8SJohn Marino 	}
58801e196c8SJohn Marino #endif
589a34d5fb1SAntonio Huete Jimenez 
590*6eef5f0cSAntonio Huete Jimenez 	/*
591*6eef5f0cSAntonio Huete Jimenez 	 * XXX: The returned mtime may differ from gn->mtime. Intentionally?
592*6eef5f0cSAntonio Huete Jimenez 	 */
59301e196c8SJohn Marino 	return mtime;
59401e196c8SJohn Marino }
59501e196c8SJohn Marino 
596a34d5fb1SAntonio Huete Jimenez /*
597a34d5fb1SAntonio Huete Jimenez  * Set the .PREFIX and .IMPSRC variables for all the implied parents
598a34d5fb1SAntonio Huete Jimenez  * of this node.
599a34d5fb1SAntonio Huete Jimenez  */
600a34d5fb1SAntonio Huete Jimenez static void
UpdateImplicitParentsVars(GNode * cgn,const char * cname)601a34d5fb1SAntonio Huete Jimenez UpdateImplicitParentsVars(GNode *cgn, const char *cname)
602a34d5fb1SAntonio Huete Jimenez {
603a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
604a34d5fb1SAntonio Huete Jimenez 	const char *cpref = GNode_VarPrefix(cgn);
605a34d5fb1SAntonio Huete Jimenez 
606a34d5fb1SAntonio Huete Jimenez 	for (ln = cgn->implicitParents.first; ln != NULL; ln = ln->next) {
607a34d5fb1SAntonio Huete Jimenez 		GNode *pgn = ln->datum;
608*6eef5f0cSAntonio Huete Jimenez 		if (pgn->flags.remake) {
609a34d5fb1SAntonio Huete Jimenez 			Var_Set(pgn, IMPSRC, cname);
610a34d5fb1SAntonio Huete Jimenez 			if (cpref != NULL)
611a34d5fb1SAntonio Huete Jimenez 				Var_Set(pgn, PREFIX, cpref);
612a34d5fb1SAntonio Huete Jimenez 		}
613a34d5fb1SAntonio Huete Jimenez 	}
614a34d5fb1SAntonio Huete Jimenez }
615a34d5fb1SAntonio Huete Jimenez 
616a34d5fb1SAntonio Huete Jimenez /* See if a .ORDER rule stops us from building this node. */
617*6eef5f0cSAntonio Huete Jimenez static bool
IsWaitingForOrder(GNode * gn)618a34d5fb1SAntonio Huete Jimenez IsWaitingForOrder(GNode *gn)
619a34d5fb1SAntonio Huete Jimenez {
620a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
621a34d5fb1SAntonio Huete Jimenez 
622a34d5fb1SAntonio Huete Jimenez 	for (ln = gn->order_pred.first; ln != NULL; ln = ln->next) {
623a34d5fb1SAntonio Huete Jimenez 		GNode *ogn = ln->datum;
624a34d5fb1SAntonio Huete Jimenez 
625*6eef5f0cSAntonio Huete Jimenez 		if (GNode_IsDone(ogn) || !ogn->flags.remake)
626a34d5fb1SAntonio Huete Jimenez 			continue;
627a34d5fb1SAntonio Huete Jimenez 
628a34d5fb1SAntonio Huete Jimenez 		DEBUG2(MAKE,
629a34d5fb1SAntonio Huete Jimenez 		    "IsWaitingForOrder: Waiting for .ORDER node \"%s%s\"\n",
630a34d5fb1SAntonio Huete Jimenez 		    ogn->name, ogn->cohort_num);
631*6eef5f0cSAntonio Huete Jimenez 		return true;
632a34d5fb1SAntonio Huete Jimenez 	}
633*6eef5f0cSAntonio Huete Jimenez 	return false;
634a34d5fb1SAntonio Huete Jimenez }
635a34d5fb1SAntonio Huete Jimenez 
636*6eef5f0cSAntonio Huete Jimenez static bool MakeBuildChild(GNode *, GNodeListNode *);
637a34d5fb1SAntonio Huete Jimenez 
638a34d5fb1SAntonio Huete Jimenez static void
ScheduleOrderSuccessors(GNode * gn)639a34d5fb1SAntonio Huete Jimenez ScheduleOrderSuccessors(GNode *gn)
640a34d5fb1SAntonio Huete Jimenez {
641a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *toBeMadeNext = toBeMade.first;
642a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
643a34d5fb1SAntonio Huete Jimenez 
644*6eef5f0cSAntonio Huete Jimenez 	for (ln = gn->order_succ.first; ln != NULL; ln = ln->next) {
645*6eef5f0cSAntonio Huete Jimenez 		GNode *succ = ln->datum;
646*6eef5f0cSAntonio Huete Jimenez 
647*6eef5f0cSAntonio Huete Jimenez 		if (succ->made == DEFERRED &&
648*6eef5f0cSAntonio Huete Jimenez 		    !MakeBuildChild(succ, toBeMadeNext))
649*6eef5f0cSAntonio Huete Jimenez 			succ->flags.doneOrder = true;
650*6eef5f0cSAntonio Huete Jimenez 	}
651a34d5fb1SAntonio Huete Jimenez }
652a34d5fb1SAntonio Huete Jimenez 
653a34d5fb1SAntonio Huete Jimenez /*
65401e196c8SJohn Marino  * Perform update on the parents of a node. Used by JobFinish once
65501e196c8SJohn Marino  * a node has been dealt with and by MakeStartJobs if it finds an
65601e196c8SJohn Marino  * up-to-date node.
65701e196c8SJohn Marino  *
65801e196c8SJohn Marino  * The unmade field of pgn is decremented and pgn may be placed on
65901e196c8SJohn Marino  * the toBeMade queue if this field becomes 0.
66001e196c8SJohn Marino  *
66101e196c8SJohn Marino  * If the child was made, the parent's flag CHILDMADE field will be
66201e196c8SJohn Marino  * set true.
66301e196c8SJohn Marino  *
66401e196c8SJohn Marino  * If the child is not up-to-date and still does not exist,
66501e196c8SJohn Marino  * set the FORCE flag on the parents.
66601e196c8SJohn Marino  *
667a34d5fb1SAntonio Huete Jimenez  * If the child wasn't made, the youngestChild field of the parent will be
66801e196c8SJohn Marino  * altered if the child's mtime is big enough.
66901e196c8SJohn Marino  *
67001e196c8SJohn Marino  * Finally, if the child is the implied source for the parent, the
67101e196c8SJohn Marino  * parent's IMPSRC variable is set appropriately.
67201e196c8SJohn Marino  */
67301e196c8SJohn Marino void
Make_Update(GNode * cgn)67401e196c8SJohn Marino Make_Update(GNode *cgn)
67501e196c8SJohn Marino {
676a34d5fb1SAntonio Huete Jimenez 	const char *cname;	/* the child's name */
67701e196c8SJohn Marino 	time_t mtime = -1;
678a34d5fb1SAntonio Huete Jimenez 	GNodeList *parents;
679a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
68001e196c8SJohn Marino 	GNode *centurion;
68101e196c8SJohn Marino 
68201e196c8SJohn Marino 	/* It is save to re-examine any nodes again */
683a34d5fb1SAntonio Huete Jimenez 	checked_seqno++;
68401e196c8SJohn Marino 
685a34d5fb1SAntonio Huete Jimenez 	cname = GNode_VarTarget(cgn);
68601e196c8SJohn Marino 
687a34d5fb1SAntonio Huete Jimenez 	DEBUG2(MAKE, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
68801e196c8SJohn Marino 
68901e196c8SJohn Marino 	/*
69001e196c8SJohn Marino 	 * If the child was actually made, see what its modification time is
691a34d5fb1SAntonio Huete Jimenez 	 * now -- some rules won't actually update the file. If the file
692a34d5fb1SAntonio Huete Jimenez 	 * still doesn't exist, make its mtime now.
69301e196c8SJohn Marino 	 */
69401e196c8SJohn Marino 	if (cgn->made != UPTODATE) {
69501e196c8SJohn Marino 		mtime = Make_Recheck(cgn);
69601e196c8SJohn Marino 	}
69701e196c8SJohn Marino 
69801e196c8SJohn Marino 	/*
69901e196c8SJohn Marino 	 * If this is a `::' node, we must consult its first instance
70001e196c8SJohn Marino 	 * which is where all parents are linked.
70101e196c8SJohn Marino 	 */
70201e196c8SJohn Marino 	if ((centurion = cgn->centurion) != NULL) {
703a34d5fb1SAntonio Huete Jimenez 		if (!Lst_IsEmpty(&cgn->parents))
704a34d5fb1SAntonio Huete Jimenez 			Punt("%s%s: cohort has parents", cgn->name,
705a34d5fb1SAntonio Huete Jimenez 			    cgn->cohort_num);
706a34d5fb1SAntonio Huete Jimenez 		centurion->unmade_cohorts--;
70701e196c8SJohn Marino 		if (centurion->unmade_cohorts < 0)
708a34d5fb1SAntonio Huete Jimenez 			Error("Graph cycles through centurion %s",
709a34d5fb1SAntonio Huete Jimenez 			    centurion->name);
71001e196c8SJohn Marino 	} else {
71101e196c8SJohn Marino 		centurion = cgn;
71201e196c8SJohn Marino 	}
713a34d5fb1SAntonio Huete Jimenez 	parents = &centurion->parents;
71401e196c8SJohn Marino 
71501e196c8SJohn Marino 	/* If this was a .ORDER node, schedule the RHS */
716a34d5fb1SAntonio Huete Jimenez 	ScheduleOrderSuccessors(centurion);
71701e196c8SJohn Marino 
71801e196c8SJohn Marino 	/* Now mark all the parents as having one less unmade child */
719a34d5fb1SAntonio Huete Jimenez 	for (ln = parents->first; ln != NULL; ln = ln->next) {
720a34d5fb1SAntonio Huete Jimenez 		GNode *pgn = ln->datum;
721a34d5fb1SAntonio Huete Jimenez 
722a34d5fb1SAntonio Huete Jimenez 		if (DEBUG(MAKE)) {
723a34d5fb1SAntonio Huete Jimenez 			debug_printf("inspect parent %s%s: ", pgn->name,
724a34d5fb1SAntonio Huete Jimenez 			    pgn->cohort_num);
725a34d5fb1SAntonio Huete Jimenez 			GNode_FprintDetails(opts.debug_file, "", pgn, "");
726a34d5fb1SAntonio Huete Jimenez 			debug_printf(", unmade %d ", pgn->unmade - 1);
727a34d5fb1SAntonio Huete Jimenez 		}
72801e196c8SJohn Marino 
729*6eef5f0cSAntonio Huete Jimenez 		if (!pgn->flags.remake) {
73001e196c8SJohn Marino 			/* This parent isn't needed */
731a34d5fb1SAntonio Huete Jimenez 			DEBUG0(MAKE, "- not needed\n");
73201e196c8SJohn Marino 			continue;
73301e196c8SJohn Marino 		}
73401e196c8SJohn Marino 		if (mtime == 0 && !(cgn->type & OP_WAIT))
735*6eef5f0cSAntonio Huete Jimenez 			pgn->flags.force = true;
73601e196c8SJohn Marino 
73701e196c8SJohn Marino 		/*
73801e196c8SJohn Marino 		 * If the parent has the .MADE attribute, its timestamp got
739a34d5fb1SAntonio Huete Jimenez 		 * updated to that of its newest child, and its unmade
74001e196c8SJohn Marino 		 * child count got set to zero in Make_ExpandUse().
74101e196c8SJohn Marino 		 * However other things might cause us to build one of its
74201e196c8SJohn Marino 		 * children - and so we mustn't do any processing here when
74301e196c8SJohn Marino 		 * the child build finishes.
74401e196c8SJohn Marino 		 */
74501e196c8SJohn Marino 		if (pgn->type & OP_MADE) {
746a34d5fb1SAntonio Huete Jimenez 			DEBUG0(MAKE, "- .MADE\n");
74701e196c8SJohn Marino 			continue;
74801e196c8SJohn Marino 		}
74901e196c8SJohn Marino 
75001e196c8SJohn Marino 		if (!(cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE))) {
75101e196c8SJohn Marino 			if (cgn->made == MADE)
752*6eef5f0cSAntonio Huete Jimenez 				pgn->flags.childMade = true;
753a34d5fb1SAntonio Huete Jimenez 			GNode_UpdateYoungestChild(pgn, cgn);
75401e196c8SJohn Marino 		}
75501e196c8SJohn Marino 
75601e196c8SJohn Marino 		/*
75701e196c8SJohn Marino 		 * A parent must wait for the completion of all instances
75801e196c8SJohn Marino 		 * of a `::' dependency.
75901e196c8SJohn Marino 		 */
760a34d5fb1SAntonio Huete Jimenez 		if (centurion->unmade_cohorts != 0 ||
761a34d5fb1SAntonio Huete Jimenez 		    !GNode_IsDone(centurion)) {
762a34d5fb1SAntonio Huete Jimenez 			DEBUG2(MAKE,
76301e196c8SJohn Marino 			    "- centurion made %d, %d unmade cohorts\n",
76401e196c8SJohn Marino 			    centurion->made, centurion->unmade_cohorts);
76501e196c8SJohn Marino 			continue;
76601e196c8SJohn Marino 		}
76701e196c8SJohn Marino 
76801e196c8SJohn Marino 		/* One more child of this parent is now made */
769a34d5fb1SAntonio Huete Jimenez 		pgn->unmade--;
77001e196c8SJohn Marino 		if (pgn->unmade < 0) {
77101e196c8SJohn Marino 			if (DEBUG(MAKE)) {
772a34d5fb1SAntonio Huete Jimenez 				debug_printf("Graph cycles through %s%s\n",
77301e196c8SJohn Marino 				    pgn->name, pgn->cohort_num);
77401e196c8SJohn Marino 				Targ_PrintGraph(2);
77501e196c8SJohn Marino 			}
776a34d5fb1SAntonio Huete Jimenez 			Error("Graph cycles through %s%s", pgn->name,
777a34d5fb1SAntonio Huete Jimenez 			    pgn->cohort_num);
77801e196c8SJohn Marino 		}
77901e196c8SJohn Marino 
780a34d5fb1SAntonio Huete Jimenez 		/*
781a34d5fb1SAntonio Huete Jimenez 		 * We must always rescan the parents of .WAIT and .ORDER
782a34d5fb1SAntonio Huete Jimenez 		 * nodes.
783a34d5fb1SAntonio Huete Jimenez 		 */
78401e196c8SJohn Marino 		if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
785*6eef5f0cSAntonio Huete Jimenez 		    && !centurion->flags.doneOrder) {
786a34d5fb1SAntonio Huete Jimenez 			DEBUG0(MAKE, "- unmade children\n");
78701e196c8SJohn Marino 			continue;
78801e196c8SJohn Marino 		}
78901e196c8SJohn Marino 		if (pgn->made != DEFERRED) {
79001e196c8SJohn Marino 			/*
791a34d5fb1SAntonio Huete Jimenez 			 * Either this parent is on a different branch of
792a34d5fb1SAntonio Huete Jimenez 			 * the tree, or it on the RHS of a .WAIT directive
79301e196c8SJohn Marino 			 * or it is already on the toBeMade list.
79401e196c8SJohn Marino 			 */
795a34d5fb1SAntonio Huete Jimenez 			DEBUG0(MAKE, "- not deferred\n");
79601e196c8SJohn Marino 			continue;
79701e196c8SJohn Marino 		}
798a34d5fb1SAntonio Huete Jimenez 
799a34d5fb1SAntonio Huete Jimenez 		if (IsWaitingForOrder(pgn))
80001e196c8SJohn Marino 			continue;
801a34d5fb1SAntonio Huete Jimenez 
80201e196c8SJohn Marino 		if (DEBUG(MAKE)) {
803a34d5fb1SAntonio Huete Jimenez 			debug_printf("- %s%s made, schedule %s%s (made %d)\n",
80401e196c8SJohn Marino 			    cgn->name, cgn->cohort_num,
80501e196c8SJohn Marino 			    pgn->name, pgn->cohort_num, pgn->made);
806a34d5fb1SAntonio Huete Jimenez 			Targ_PrintNode(pgn, 2);
80701e196c8SJohn Marino 		}
80801e196c8SJohn Marino 		/* Ok, we can schedule the parent again */
80901e196c8SJohn Marino 		pgn->made = REQUESTED;
810a34d5fb1SAntonio Huete Jimenez 		Lst_Enqueue(&toBeMade, pgn);
81101e196c8SJohn Marino 	}
812a34d5fb1SAntonio Huete Jimenez 
813a34d5fb1SAntonio Huete Jimenez 	UpdateImplicitParentsVars(cgn, cname);
814a34d5fb1SAntonio Huete Jimenez }
815a34d5fb1SAntonio Huete Jimenez 
816a34d5fb1SAntonio Huete Jimenez static void
UnmarkChildren(GNode * gn)817a34d5fb1SAntonio Huete Jimenez UnmarkChildren(GNode *gn)
818a34d5fb1SAntonio Huete Jimenez {
819a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
820a34d5fb1SAntonio Huete Jimenez 
821a34d5fb1SAntonio Huete Jimenez 	for (ln = gn->children.first; ln != NULL; ln = ln->next) {
822a34d5fb1SAntonio Huete Jimenez 		GNode *child = ln->datum;
823*6eef5f0cSAntonio Huete Jimenez 		child->type &= (unsigned)~OP_MARK;
824a34d5fb1SAntonio Huete Jimenez 	}
82501e196c8SJohn Marino }
82601e196c8SJohn Marino 
82701e196c8SJohn Marino /*
82801e196c8SJohn Marino  * Add a child's name to the ALLSRC and OODATE variables of the given
829a34d5fb1SAntonio Huete Jimenez  * node, but only if it has not been given the .EXEC, .USE or .INVISIBLE
830a34d5fb1SAntonio Huete Jimenez  * attributes. .EXEC and .USE children are very rarely going to be files,
831a34d5fb1SAntonio Huete Jimenez  * so...
832a34d5fb1SAntonio Huete Jimenez  *
83301e196c8SJohn Marino  * If the child is a .JOIN node, its ALLSRC is propagated to the parent.
83401e196c8SJohn Marino  *
83501e196c8SJohn Marino  * A child is added to the OODATE variable if its modification time is
83601e196c8SJohn Marino  * later than that of its parent, as defined by Make, except if the
83701e196c8SJohn Marino  * parent is a .JOIN node. In that case, it is only added to the OODATE
83801e196c8SJohn Marino  * variable if it was actually made (since .JOIN nodes don't have
83901e196c8SJohn Marino  * modification times, the comparison is rather unfair...)..
84001e196c8SJohn Marino  *
84101e196c8SJohn Marino  * Input:
842a34d5fb1SAntonio Huete Jimenez  *	cgn		The child to add
843a34d5fb1SAntonio Huete Jimenez  *	pgn		The parent to whose ALLSRC variable it should
84401e196c8SJohn Marino  *			be added
84501e196c8SJohn Marino  */
846a34d5fb1SAntonio Huete Jimenez static void
MakeAddAllSrc(GNode * cgn,GNode * pgn)847a34d5fb1SAntonio Huete Jimenez MakeAddAllSrc(GNode *cgn, GNode *pgn)
84801e196c8SJohn Marino {
849a34d5fb1SAntonio Huete Jimenez 	const char *child, *allsrc;
85001e196c8SJohn Marino 
85101e196c8SJohn Marino 	if (cgn->type & OP_MARK)
852a34d5fb1SAntonio Huete Jimenez 		return;
85301e196c8SJohn Marino 	cgn->type |= OP_MARK;
85401e196c8SJohn Marino 
855a34d5fb1SAntonio Huete Jimenez 	if (cgn->type & (OP_EXEC | OP_USE | OP_USEBEFORE | OP_INVISIBLE))
856a34d5fb1SAntonio Huete Jimenez 		return;
85701e196c8SJohn Marino 
85801e196c8SJohn Marino 	if (cgn->type & OP_ARCHV)
859a34d5fb1SAntonio Huete Jimenez 		child = GNode_VarMember(cgn);
86001e196c8SJohn Marino 	else
861a34d5fb1SAntonio Huete Jimenez 		child = GNode_Path(cgn);
862a34d5fb1SAntonio Huete Jimenez 
863a34d5fb1SAntonio Huete Jimenez 	if (cgn->type & OP_JOIN)
864a34d5fb1SAntonio Huete Jimenez 		allsrc = GNode_VarAllsrc(cgn);
865a34d5fb1SAntonio Huete Jimenez 	else
86601e196c8SJohn Marino 		allsrc = child;
867a34d5fb1SAntonio Huete Jimenez 
86801e196c8SJohn Marino 	if (allsrc != NULL)
869a34d5fb1SAntonio Huete Jimenez 		Var_Append(pgn, ALLSRC, allsrc);
870a34d5fb1SAntonio Huete Jimenez 
87101e196c8SJohn Marino 	if (pgn->type & OP_JOIN) {
872a34d5fb1SAntonio Huete Jimenez 		if (cgn->made == MADE)
873a34d5fb1SAntonio Huete Jimenez 			Var_Append(pgn, OODATE, child);
874a34d5fb1SAntonio Huete Jimenez 
87501e196c8SJohn Marino 	} else if ((pgn->mtime < cgn->mtime) ||
876a34d5fb1SAntonio Huete Jimenez 		   (cgn->mtime >= now && cgn->made == MADE)) {
87701e196c8SJohn Marino 		/*
878a34d5fb1SAntonio Huete Jimenez 		 * It goes in the OODATE variable if the parent is
879a34d5fb1SAntonio Huete Jimenez 		 * younger than the child or if the child has been
880a34d5fb1SAntonio Huete Jimenez 		 * modified more recently than the start of the make.
881a34d5fb1SAntonio Huete Jimenez 		 * This is to keep pmake from getting confused if
882a34d5fb1SAntonio Huete Jimenez 		 * something else updates the parent after the make
883a34d5fb1SAntonio Huete Jimenez 		 * starts (shouldn't happen, I know, but sometimes it
884a34d5fb1SAntonio Huete Jimenez 		 * does). In such a case, if we've updated the child,
885a34d5fb1SAntonio Huete Jimenez 		 * the parent is likely to have a modification time
886a34d5fb1SAntonio Huete Jimenez 		 * later than that of the child and anything that
887a34d5fb1SAntonio Huete Jimenez 		 * relies on the OODATE variable will be hosed.
88801e196c8SJohn Marino 		 *
889a34d5fb1SAntonio Huete Jimenez 		 * XXX: This will cause all made children to go in
890a34d5fb1SAntonio Huete Jimenez 		 * the OODATE variable, even if they're not touched,
891a34d5fb1SAntonio Huete Jimenez 		 * if RECHECK isn't defined, since cgn->mtime is set
892a34d5fb1SAntonio Huete Jimenez 		 * to now in Make_Update. According to some people,
893a34d5fb1SAntonio Huete Jimenez 		 * this is good...
89401e196c8SJohn Marino 		 */
895a34d5fb1SAntonio Huete Jimenez 		Var_Append(pgn, OODATE, child);
89601e196c8SJohn Marino 	}
89701e196c8SJohn Marino }
898a34d5fb1SAntonio Huete Jimenez 
899a34d5fb1SAntonio Huete Jimenez /*
90001e196c8SJohn Marino  * Set up the ALLSRC and OODATE variables. Sad to say, it must be
90101e196c8SJohn Marino  * done separately, rather than while traversing the graph. This is
90201e196c8SJohn Marino  * because Make defined OODATE to contain all sources whose modification
90301e196c8SJohn Marino  * times were later than that of the target, *not* those sources that
90401e196c8SJohn Marino  * were out-of-date. Since in both compatibility and native modes,
90501e196c8SJohn Marino  * the modification time of the parent isn't found until the child
90601e196c8SJohn Marino  * has been dealt with, we have to wait until now to fill in the
90701e196c8SJohn Marino  * variable. As for ALLSRC, the ordering is important and not
90801e196c8SJohn Marino  * guaranteed when in native mode, so it must be set here, too.
90901e196c8SJohn Marino  *
91001e196c8SJohn Marino  * If the node is a .JOIN node, its TARGET variable will be set to
91101e196c8SJohn Marino  * match its ALLSRC variable.
91201e196c8SJohn Marino  */
91301e196c8SJohn Marino void
GNode_SetLocalVars(GNode * gn)914*6eef5f0cSAntonio Huete Jimenez GNode_SetLocalVars(GNode *gn)
91501e196c8SJohn Marino {
916a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
917a34d5fb1SAntonio Huete Jimenez 
918*6eef5f0cSAntonio Huete Jimenez 	if (gn->flags.doneAllsrc)
91901e196c8SJohn Marino 		return;
92001e196c8SJohn Marino 
921a34d5fb1SAntonio Huete Jimenez 	UnmarkChildren(gn);
922a34d5fb1SAntonio Huete Jimenez 	for (ln = gn->children.first; ln != NULL; ln = ln->next)
923a34d5fb1SAntonio Huete Jimenez 		MakeAddAllSrc(ln->datum, gn);
92401e196c8SJohn Marino 
925a34d5fb1SAntonio Huete Jimenez 	if (!Var_Exists(gn, OODATE))
926a34d5fb1SAntonio Huete Jimenez 		Var_Set(gn, OODATE, "");
927a34d5fb1SAntonio Huete Jimenez 	if (!Var_Exists(gn, ALLSRC))
928a34d5fb1SAntonio Huete Jimenez 		Var_Set(gn, ALLSRC, "");
92901e196c8SJohn Marino 
930a34d5fb1SAntonio Huete Jimenez 	if (gn->type & OP_JOIN)
931a34d5fb1SAntonio Huete Jimenez 		Var_Set(gn, TARGET, GNode_VarAllsrc(gn));
932*6eef5f0cSAntonio Huete Jimenez 	gn->flags.doneAllsrc = true;
93301e196c8SJohn Marino }
93401e196c8SJohn Marino 
935*6eef5f0cSAntonio Huete Jimenez static void
ScheduleRandomly(GNode * gn)936*6eef5f0cSAntonio Huete Jimenez ScheduleRandomly(GNode *gn)
937*6eef5f0cSAntonio Huete Jimenez {
938*6eef5f0cSAntonio Huete Jimenez 	GNodeListNode *ln;
939*6eef5f0cSAntonio Huete Jimenez 	size_t i, n;
940*6eef5f0cSAntonio Huete Jimenez 
941*6eef5f0cSAntonio Huete Jimenez 	n = 0;
942*6eef5f0cSAntonio Huete Jimenez 	for (ln = toBeMade.first; ln != NULL; ln = ln->next)
943*6eef5f0cSAntonio Huete Jimenez 		n++;
944*6eef5f0cSAntonio Huete Jimenez 	i = n > 0 ? (size_t)random() % (n + 1) : 0;
945*6eef5f0cSAntonio Huete Jimenez 
946*6eef5f0cSAntonio Huete Jimenez 	if (i == 0) {
947*6eef5f0cSAntonio Huete Jimenez 		Lst_Append(&toBeMade, gn);
948*6eef5f0cSAntonio Huete Jimenez 		return;
949*6eef5f0cSAntonio Huete Jimenez 	}
950*6eef5f0cSAntonio Huete Jimenez 	i--;
951*6eef5f0cSAntonio Huete Jimenez 
952*6eef5f0cSAntonio Huete Jimenez 	for (ln = toBeMade.first; i > 0; ln = ln->next)
953*6eef5f0cSAntonio Huete Jimenez 		i--;
954*6eef5f0cSAntonio Huete Jimenez 	Lst_InsertBefore(&toBeMade, ln, gn);
955*6eef5f0cSAntonio Huete Jimenez }
956*6eef5f0cSAntonio Huete Jimenez 
957*6eef5f0cSAntonio Huete Jimenez static bool
MakeBuildChild(GNode * cn,GNodeListNode * toBeMadeNext)958a34d5fb1SAntonio Huete Jimenez MakeBuildChild(GNode *cn, GNodeListNode *toBeMadeNext)
95901e196c8SJohn Marino {
96001e196c8SJohn Marino 
961a34d5fb1SAntonio Huete Jimenez 	if (DEBUG(MAKE)) {
962a34d5fb1SAntonio Huete Jimenez 		debug_printf("MakeBuildChild: inspect %s%s, ",
963a34d5fb1SAntonio Huete Jimenez 		    cn->name, cn->cohort_num);
964a34d5fb1SAntonio Huete Jimenez 		GNode_FprintDetails(opts.debug_file, "", cn, "\n");
96501e196c8SJohn Marino 	}
966a34d5fb1SAntonio Huete Jimenez 	if (GNode_IsReady(cn))
967*6eef5f0cSAntonio Huete Jimenez 		return false;
96801e196c8SJohn Marino 
96901e196c8SJohn Marino 	/* If this node is on the RHS of a .ORDER, check LHSs. */
970a34d5fb1SAntonio Huete Jimenez 	if (IsWaitingForOrder(cn)) {
971*6eef5f0cSAntonio Huete Jimenez 		/*
972*6eef5f0cSAntonio Huete Jimenez 		 * Can't build this (or anything else in this child list) yet
973*6eef5f0cSAntonio Huete Jimenez 		 */
97401e196c8SJohn Marino 		cn->made = DEFERRED;
975*6eef5f0cSAntonio Huete Jimenez 		return false;	/* but keep looking */
97601e196c8SJohn Marino 	}
97701e196c8SJohn Marino 
978a34d5fb1SAntonio Huete Jimenez 	DEBUG2(MAKE, "MakeBuildChild: schedule %s%s\n",
97901e196c8SJohn Marino 	    cn->name, cn->cohort_num);
98001e196c8SJohn Marino 
98101e196c8SJohn Marino 	cn->made = REQUESTED;
982*6eef5f0cSAntonio Huete Jimenez 	if (opts.randomizeTargets && !(cn->type & OP_WAIT))
983*6eef5f0cSAntonio Huete Jimenez 		ScheduleRandomly(cn);
984*6eef5f0cSAntonio Huete Jimenez 	else if (toBeMadeNext == NULL)
985a34d5fb1SAntonio Huete Jimenez 		Lst_Append(&toBeMade, cn);
98601e196c8SJohn Marino 	else
987a34d5fb1SAntonio Huete Jimenez 		Lst_InsertBefore(&toBeMade, toBeMadeNext, cn);
98801e196c8SJohn Marino 
989a34d5fb1SAntonio Huete Jimenez 	if (cn->unmade_cohorts != 0) {
990a34d5fb1SAntonio Huete Jimenez 		ListNode *ln;
991a34d5fb1SAntonio Huete Jimenez 
992a34d5fb1SAntonio Huete Jimenez 		for (ln = cn->cohorts.first; ln != NULL; ln = ln->next)
993a34d5fb1SAntonio Huete Jimenez 			if (MakeBuildChild(ln->datum, toBeMadeNext))
994a34d5fb1SAntonio Huete Jimenez 				break;
995a34d5fb1SAntonio Huete Jimenez 	}
99601e196c8SJohn Marino 
99701e196c8SJohn Marino 	/*
998a34d5fb1SAntonio Huete Jimenez 	 * If this node is a .WAIT node with unmade children
99901e196c8SJohn Marino 	 * then don't add the next sibling.
100001e196c8SJohn Marino 	 */
100101e196c8SJohn Marino 	return cn->type & OP_WAIT && cn->unmade > 0;
100201e196c8SJohn Marino }
100301e196c8SJohn Marino 
1004a34d5fb1SAntonio Huete Jimenez static void
MakeChildren(GNode * gn)1005a34d5fb1SAntonio Huete Jimenez MakeChildren(GNode *gn)
1006a34d5fb1SAntonio Huete Jimenez {
1007a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *toBeMadeNext = toBeMade.first;
1008a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
1009a34d5fb1SAntonio Huete Jimenez 
1010a34d5fb1SAntonio Huete Jimenez 	for (ln = gn->children.first; ln != NULL; ln = ln->next)
1011a34d5fb1SAntonio Huete Jimenez 		if (MakeBuildChild(ln->datum, toBeMadeNext))
1012a34d5fb1SAntonio Huete Jimenez 			break;
1013a34d5fb1SAntonio Huete Jimenez }
1014a34d5fb1SAntonio Huete Jimenez 
1015a34d5fb1SAntonio Huete Jimenez /*
1016a34d5fb1SAntonio Huete Jimenez  * Start as many jobs as possible, taking them from the toBeMade queue.
1017a34d5fb1SAntonio Huete Jimenez  *
1018a34d5fb1SAntonio Huete Jimenez  * If the -q option was given, no job will be started,
1019a34d5fb1SAntonio Huete Jimenez  * but as soon as an out-of-date target is found, this function
1020*6eef5f0cSAntonio Huete Jimenez  * returns true. In all other cases, this function returns false.
1021a34d5fb1SAntonio Huete Jimenez  */
1022*6eef5f0cSAntonio Huete Jimenez static bool
MakeStartJobs(void)102301e196c8SJohn Marino MakeStartJobs(void)
102401e196c8SJohn Marino {
102501e196c8SJohn Marino 	GNode *gn;
1026*6eef5f0cSAntonio Huete Jimenez 	bool have_token = false;
102701e196c8SJohn Marino 
1028a34d5fb1SAntonio Huete Jimenez 	while (!Lst_IsEmpty(&toBeMade)) {
1029a34d5fb1SAntonio Huete Jimenez 		/*
1030a34d5fb1SAntonio Huete Jimenez 		 * Get token now to avoid cycling job-list when we only
1031a34d5fb1SAntonio Huete Jimenez 		 * have 1 token
1032a34d5fb1SAntonio Huete Jimenez 		 */
103301e196c8SJohn Marino 		if (!have_token && !Job_TokenWithdraw())
103401e196c8SJohn Marino 			break;
1035*6eef5f0cSAntonio Huete Jimenez 		have_token = true;
103601e196c8SJohn Marino 
1037a34d5fb1SAntonio Huete Jimenez 		gn = Lst_Dequeue(&toBeMade);
1038a34d5fb1SAntonio Huete Jimenez 		DEBUG2(MAKE, "Examining %s%s...\n", gn->name, gn->cohort_num);
103901e196c8SJohn Marino 
104001e196c8SJohn Marino 		if (gn->made != REQUESTED) {
1041*6eef5f0cSAntonio Huete Jimenez 			debug_printf("internal error: made = %s\n",
1042*6eef5f0cSAntonio Huete Jimenez 			    GNodeMade_Name(gn->made));
1043*6eef5f0cSAntonio Huete Jimenez 			Targ_PrintNode(gn, 2);
1044*6eef5f0cSAntonio Huete Jimenez 			Targ_PrintNodes(&toBeMade, 2);
1045*6eef5f0cSAntonio Huete Jimenez 			Targ_PrintGraph(3);
1046*6eef5f0cSAntonio Huete Jimenez 			abort();
104701e196c8SJohn Marino 		}
104801e196c8SJohn Marino 
1049a34d5fb1SAntonio Huete Jimenez 		if (gn->checked_seqno == checked_seqno) {
1050a34d5fb1SAntonio Huete Jimenez 			/*
1051a34d5fb1SAntonio Huete Jimenez 			 * We've already looked at this node since a job
1052a34d5fb1SAntonio Huete Jimenez 			 * finished...
1053a34d5fb1SAntonio Huete Jimenez 			 */
1054a34d5fb1SAntonio Huete Jimenez 			DEBUG2(MAKE, "already checked %s%s\n", gn->name,
1055a34d5fb1SAntonio Huete Jimenez 			    gn->cohort_num);
105601e196c8SJohn Marino 			gn->made = DEFERRED;
105701e196c8SJohn Marino 			continue;
105801e196c8SJohn Marino 		}
1059a34d5fb1SAntonio Huete Jimenez 		gn->checked_seqno = checked_seqno;
106001e196c8SJohn Marino 
106101e196c8SJohn Marino 		if (gn->unmade != 0) {
106201e196c8SJohn Marino 			/*
1063a34d5fb1SAntonio Huete Jimenez 			 * We can't build this yet, add all unmade children
1064a34d5fb1SAntonio Huete Jimenez 			 * to toBeMade, just before the current first element.
106501e196c8SJohn Marino 			 */
106601e196c8SJohn Marino 			gn->made = DEFERRED;
1067a34d5fb1SAntonio Huete Jimenez 
1068a34d5fb1SAntonio Huete Jimenez 			MakeChildren(gn);
1069a34d5fb1SAntonio Huete Jimenez 
107001e196c8SJohn Marino 			/* and drop this node on the floor */
1071a34d5fb1SAntonio Huete Jimenez 			DEBUG2(MAKE, "dropped %s%s\n", gn->name,
1072a34d5fb1SAntonio Huete Jimenez 			    gn->cohort_num);
107301e196c8SJohn Marino 			continue;
107401e196c8SJohn Marino 		}
107501e196c8SJohn Marino 
107601e196c8SJohn Marino 		gn->made = BEINGMADE;
1077a34d5fb1SAntonio Huete Jimenez 		if (GNode_IsOODate(gn)) {
1078a34d5fb1SAntonio Huete Jimenez 			DEBUG0(MAKE, "out-of-date\n");
1079*6eef5f0cSAntonio Huete Jimenez 			if (opts.query)
1080*6eef5f0cSAntonio Huete Jimenez 				return strcmp(gn->name, ".MAIN") != 0;
1081*6eef5f0cSAntonio Huete Jimenez 			GNode_SetLocalVars(gn);
108201e196c8SJohn Marino 			Job_Make(gn);
1083*6eef5f0cSAntonio Huete Jimenez 			have_token = false;
108401e196c8SJohn Marino 		} else {
1085a34d5fb1SAntonio Huete Jimenez 			DEBUG0(MAKE, "up-to-date\n");
108601e196c8SJohn Marino 			gn->made = UPTODATE;
108701e196c8SJohn Marino 			if (gn->type & OP_JOIN) {
108801e196c8SJohn Marino 				/*
1089a34d5fb1SAntonio Huete Jimenez 				 * Even for an up-to-date .JOIN node, we
1090a34d5fb1SAntonio Huete Jimenez 				 * need it to have its local variables so
1091a34d5fb1SAntonio Huete Jimenez 				 * references to it get the correct value
1092a34d5fb1SAntonio Huete Jimenez 				 * for .TARGET when building up the local
1093a34d5fb1SAntonio Huete Jimenez 				 * variables of its parent(s)...
109401e196c8SJohn Marino 				 */
1095*6eef5f0cSAntonio Huete Jimenez 				GNode_SetLocalVars(gn);
109601e196c8SJohn Marino 			}
109701e196c8SJohn Marino 			Make_Update(gn);
109801e196c8SJohn Marino 		}
109901e196c8SJohn Marino 	}
110001e196c8SJohn Marino 
110101e196c8SJohn Marino 	if (have_token)
110201e196c8SJohn Marino 		Job_TokenReturn();
110301e196c8SJohn Marino 
1104*6eef5f0cSAntonio Huete Jimenez 	return false;
110501e196c8SJohn Marino }
1106a34d5fb1SAntonio Huete Jimenez 
1107a34d5fb1SAntonio Huete Jimenez /* Print the status of a .ORDER node. */
1108a34d5fb1SAntonio Huete Jimenez static void
MakePrintStatusOrderNode(GNode * ogn,GNode * gn)1109a34d5fb1SAntonio Huete Jimenez MakePrintStatusOrderNode(GNode *ogn, GNode *gn)
111001e196c8SJohn Marino {
1111a34d5fb1SAntonio Huete Jimenez 	if (!GNode_IsWaitingFor(ogn))
1112a34d5fb1SAntonio Huete Jimenez 		return;
111301e196c8SJohn Marino 
1114a34d5fb1SAntonio Huete Jimenez 	printf("    `%s%s' has .ORDER dependency against %s%s ",
1115a34d5fb1SAntonio Huete Jimenez 	    gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1116a34d5fb1SAntonio Huete Jimenez 	GNode_FprintDetails(stdout, "(", ogn, ")\n");
111701e196c8SJohn Marino 
1118a34d5fb1SAntonio Huete Jimenez 	if (DEBUG(MAKE) && opts.debug_file != stdout) {
1119a34d5fb1SAntonio Huete Jimenez 		debug_printf("    `%s%s' has .ORDER dependency against %s%s ",
1120a34d5fb1SAntonio Huete Jimenez 		    gn->name, gn->cohort_num, ogn->name, ogn->cohort_num);
1121a34d5fb1SAntonio Huete Jimenez 		GNode_FprintDetails(opts.debug_file, "(", ogn, ")\n");
1122a34d5fb1SAntonio Huete Jimenez 	}
112301e196c8SJohn Marino }
112401e196c8SJohn Marino 
1125a34d5fb1SAntonio Huete Jimenez static void
MakePrintStatusOrder(GNode * gn)1126a34d5fb1SAntonio Huete Jimenez MakePrintStatusOrder(GNode *gn)
112701e196c8SJohn Marino {
1128a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
1129a34d5fb1SAntonio Huete Jimenez 	for (ln = gn->order_pred.first; ln != NULL; ln = ln->next)
1130a34d5fb1SAntonio Huete Jimenez 		MakePrintStatusOrderNode(ln->datum, gn);
1131a34d5fb1SAntonio Huete Jimenez }
113201e196c8SJohn Marino 
1133a34d5fb1SAntonio Huete Jimenez static void MakePrintStatusList(GNodeList *, int *);
1134a34d5fb1SAntonio Huete Jimenez 
1135a34d5fb1SAntonio Huete Jimenez /*
1136a34d5fb1SAntonio Huete Jimenez  * Print the status of a top-level node, viz. it being up-to-date already
1137a34d5fb1SAntonio Huete Jimenez  * or not created due to an error in a lower level.
1138a34d5fb1SAntonio Huete Jimenez  */
1139*6eef5f0cSAntonio Huete Jimenez static bool
MakePrintStatus(GNode * gn,int * errors)1140a34d5fb1SAntonio Huete Jimenez MakePrintStatus(GNode *gn, int *errors)
1141a34d5fb1SAntonio Huete Jimenez {
1142*6eef5f0cSAntonio Huete Jimenez 	if (gn->flags.doneCycle) {
1143a34d5fb1SAntonio Huete Jimenez 		/*
1144a34d5fb1SAntonio Huete Jimenez 		 * We've completely processed this node before, don't do
1145a34d5fb1SAntonio Huete Jimenez 		 * it again.
1146a34d5fb1SAntonio Huete Jimenez 		 */
1147*6eef5f0cSAntonio Huete Jimenez 		return false;
1148a34d5fb1SAntonio Huete Jimenez 	}
114901e196c8SJohn Marino 
115001e196c8SJohn Marino 	if (gn->unmade == 0) {
1151*6eef5f0cSAntonio Huete Jimenez 		gn->flags.doneCycle = true;
115201e196c8SJohn Marino 		switch (gn->made) {
115301e196c8SJohn Marino 		case UPTODATE:
1154a34d5fb1SAntonio Huete Jimenez 			printf("`%s%s' is up to date.\n", gn->name,
1155a34d5fb1SAntonio Huete Jimenez 			    gn->cohort_num);
115601e196c8SJohn Marino 			break;
115701e196c8SJohn Marino 		case MADE:
115801e196c8SJohn Marino 			break;
115901e196c8SJohn Marino 		case UNMADE:
116001e196c8SJohn Marino 		case DEFERRED:
116101e196c8SJohn Marino 		case REQUESTED:
116201e196c8SJohn Marino 		case BEINGMADE:
116301e196c8SJohn Marino 			(*errors)++;
1164a34d5fb1SAntonio Huete Jimenez 			printf("`%s%s' was not built", gn->name,
1165a34d5fb1SAntonio Huete Jimenez 			    gn->cohort_num);
1166a34d5fb1SAntonio Huete Jimenez 			GNode_FprintDetails(stdout, " (", gn, ")!\n");
1167a34d5fb1SAntonio Huete Jimenez 			if (DEBUG(MAKE) && opts.debug_file != stdout) {
1168a34d5fb1SAntonio Huete Jimenez 				debug_printf("`%s%s' was not built", gn->name,
1169a34d5fb1SAntonio Huete Jimenez 				    gn->cohort_num);
1170a34d5fb1SAntonio Huete Jimenez 				GNode_FprintDetails(opts.debug_file, " (", gn,
1171a34d5fb1SAntonio Huete Jimenez 				    ")!\n");
1172a34d5fb1SAntonio Huete Jimenez 			}
117301e196c8SJohn Marino 			/* Most likely problem is actually caused by .ORDER */
1174a34d5fb1SAntonio Huete Jimenez 			MakePrintStatusOrder(gn);
117501e196c8SJohn Marino 			break;
117601e196c8SJohn Marino 		default:
117701e196c8SJohn Marino 			/* Errors - already counted */
117801e196c8SJohn Marino 			printf("`%s%s' not remade because of errors.\n",
117901e196c8SJohn Marino 			    gn->name, gn->cohort_num);
1180a34d5fb1SAntonio Huete Jimenez 			if (DEBUG(MAKE) && opts.debug_file != stdout)
1181a34d5fb1SAntonio Huete Jimenez 				debug_printf(
1182a34d5fb1SAntonio Huete Jimenez 				    "`%s%s' not remade because of errors.\n",
118301e196c8SJohn Marino 				    gn->name, gn->cohort_num);
118401e196c8SJohn Marino 			break;
118501e196c8SJohn Marino 		}
1186*6eef5f0cSAntonio Huete Jimenez 		return false;
118701e196c8SJohn Marino 	}
118801e196c8SJohn Marino 
1189a34d5fb1SAntonio Huete Jimenez 	DEBUG3(MAKE, "MakePrintStatus: %s%s has %d unmade children\n",
119001e196c8SJohn Marino 	    gn->name, gn->cohort_num, gn->unmade);
119101e196c8SJohn Marino 	/*
119201e196c8SJohn Marino 	 * If printing cycles and came to one that has unmade children,
119301e196c8SJohn Marino 	 * print out the cycle by recursing on its children.
119401e196c8SJohn Marino 	 */
1195*6eef5f0cSAntonio Huete Jimenez 	if (!gn->flags.cycle) {
1196a34d5fb1SAntonio Huete Jimenez 		/* First time we've seen this node, check all children */
1197*6eef5f0cSAntonio Huete Jimenez 		gn->flags.cycle = true;
1198a34d5fb1SAntonio Huete Jimenez 		MakePrintStatusList(&gn->children, errors);
119901e196c8SJohn Marino 		/* Mark that this node needn't be processed again */
1200*6eef5f0cSAntonio Huete Jimenez 		gn->flags.doneCycle = true;
1201*6eef5f0cSAntonio Huete Jimenez 		return false;
120201e196c8SJohn Marino 	}
120301e196c8SJohn Marino 
120401e196c8SJohn Marino 	/* Only output the error once per node */
1205*6eef5f0cSAntonio Huete Jimenez 	gn->flags.doneCycle = true;
120601e196c8SJohn Marino 	Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
120701e196c8SJohn Marino 	if ((*errors)++ > 100)
120801e196c8SJohn Marino 		/* Abandon the whole error report */
1209*6eef5f0cSAntonio Huete Jimenez 		return true;
121001e196c8SJohn Marino 
121101e196c8SJohn Marino 	/* Reporting for our children will give the rest of the loop */
1212a34d5fb1SAntonio Huete Jimenez 	MakePrintStatusList(&gn->children, errors);
1213*6eef5f0cSAntonio Huete Jimenez 	return false;
121401e196c8SJohn Marino }
121501e196c8SJohn Marino 
1216a34d5fb1SAntonio Huete Jimenez static void
MakePrintStatusList(GNodeList * gnodes,int * errors)1217a34d5fb1SAntonio Huete Jimenez MakePrintStatusList(GNodeList *gnodes, int *errors)
1218a34d5fb1SAntonio Huete Jimenez {
1219a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *ln;
1220a34d5fb1SAntonio Huete Jimenez 
1221a34d5fb1SAntonio Huete Jimenez 	for (ln = gnodes->first; ln != NULL; ln = ln->next)
1222a34d5fb1SAntonio Huete Jimenez 		if (MakePrintStatus(ln->datum, errors))
1223a34d5fb1SAntonio Huete Jimenez 			break;
1224a34d5fb1SAntonio Huete Jimenez }
1225a34d5fb1SAntonio Huete Jimenez 
1226a34d5fb1SAntonio Huete Jimenez static void
ExamineLater(GNodeList * examine,GNodeList * toBeExamined)1227a34d5fb1SAntonio Huete Jimenez ExamineLater(GNodeList *examine, GNodeList *toBeExamined)
1228a34d5fb1SAntonio Huete Jimenez {
1229a34d5fb1SAntonio Huete Jimenez 	ListNode *ln;
1230a34d5fb1SAntonio Huete Jimenez 
1231a34d5fb1SAntonio Huete Jimenez 	for (ln = toBeExamined->first; ln != NULL; ln = ln->next) {
1232a34d5fb1SAntonio Huete Jimenez 		GNode *gn = ln->datum;
1233a34d5fb1SAntonio Huete Jimenez 
1234*6eef5f0cSAntonio Huete Jimenez 		if (gn->flags.remake)
1235a34d5fb1SAntonio Huete Jimenez 			continue;
1236a34d5fb1SAntonio Huete Jimenez 		if (gn->type & (OP_USE | OP_USEBEFORE))
1237a34d5fb1SAntonio Huete Jimenez 			continue;
1238a34d5fb1SAntonio Huete Jimenez 
1239a34d5fb1SAntonio Huete Jimenez 		DEBUG2(MAKE, "ExamineLater: need to examine \"%s%s\"\n",
1240a34d5fb1SAntonio Huete Jimenez 		    gn->name, gn->cohort_num);
1241a34d5fb1SAntonio Huete Jimenez 		Lst_Enqueue(examine, gn);
1242a34d5fb1SAntonio Huete Jimenez 	}
1243a34d5fb1SAntonio Huete Jimenez }
1244a34d5fb1SAntonio Huete Jimenez 
1245a34d5fb1SAntonio Huete Jimenez /*
1246a34d5fb1SAntonio Huete Jimenez  * Expand .USE nodes and create a new targets list.
124701e196c8SJohn Marino  *
124801e196c8SJohn Marino  * Input:
124901e196c8SJohn Marino  *	targs		the initial list of targets
125001e196c8SJohn Marino  */
125101e196c8SJohn Marino void
Make_ExpandUse(GNodeList * targs)1252a34d5fb1SAntonio Huete Jimenez Make_ExpandUse(GNodeList *targs)
125301e196c8SJohn Marino {
1254a34d5fb1SAntonio Huete Jimenez 	GNodeList examine = LST_INIT;	/* Queue of targets to examine */
1255a34d5fb1SAntonio Huete Jimenez 	Lst_AppendAll(&examine, targs);
125601e196c8SJohn Marino 
125701e196c8SJohn Marino 	/*
1258a34d5fb1SAntonio Huete Jimenez 	 * Make an initial downward pass over the graph, marking nodes to
1259a34d5fb1SAntonio Huete Jimenez 	 * be made as we go down.
1260a34d5fb1SAntonio Huete Jimenez 	 *
1261a34d5fb1SAntonio Huete Jimenez 	 * We call Suff_FindDeps to find where a node is and to get some
1262a34d5fb1SAntonio Huete Jimenez 	 * children for it if it has none and also has no commands. If the
1263a34d5fb1SAntonio Huete Jimenez 	 * node is a leaf, we stick it on the toBeMade queue to be looked
1264a34d5fb1SAntonio Huete Jimenez 	 * at in a minute, otherwise we add its children to our queue and
1265a34d5fb1SAntonio Huete Jimenez 	 * go on about our business.
126601e196c8SJohn Marino 	 */
1267a34d5fb1SAntonio Huete Jimenez 	while (!Lst_IsEmpty(&examine)) {
1268a34d5fb1SAntonio Huete Jimenez 		GNode *gn = Lst_Dequeue(&examine);
126901e196c8SJohn Marino 
1270*6eef5f0cSAntonio Huete Jimenez 		if (gn->flags.remake)
127101e196c8SJohn Marino 			/* We've looked at this one already */
127201e196c8SJohn Marino 			continue;
1273*6eef5f0cSAntonio Huete Jimenez 		gn->flags.remake = true;
1274a34d5fb1SAntonio Huete Jimenez 		DEBUG2(MAKE, "Make_ExpandUse: examine %s%s\n",
127501e196c8SJohn Marino 		    gn->name, gn->cohort_num);
127601e196c8SJohn Marino 
1277a34d5fb1SAntonio Huete Jimenez 		if (gn->type & OP_DOUBLEDEP)
1278a34d5fb1SAntonio Huete Jimenez 			Lst_PrependAll(&examine, &gn->cohorts);
127901e196c8SJohn Marino 
128001e196c8SJohn Marino 		/*
1281a34d5fb1SAntonio Huete Jimenez 		 * Apply any .USE rules before looking for implicit
1282a34d5fb1SAntonio Huete Jimenez 		 * dependencies to make sure everything has commands that
1283a34d5fb1SAntonio Huete Jimenez 		 * should.
1284a34d5fb1SAntonio Huete Jimenez 		 *
128501e196c8SJohn Marino 		 * Make sure that the TARGET is set, so that we can make
128601e196c8SJohn Marino 		 * expansions.
128701e196c8SJohn Marino 		 */
128801e196c8SJohn Marino 		if (gn->type & OP_ARCHV) {
1289a34d5fb1SAntonio Huete Jimenez 			char *eoa = strchr(gn->name, '(');
1290a34d5fb1SAntonio Huete Jimenez 			char *eon = strchr(gn->name, ')');
129101e196c8SJohn Marino 			if (eoa == NULL || eon == NULL)
129201e196c8SJohn Marino 				continue;
129301e196c8SJohn Marino 			*eoa = '\0';
129401e196c8SJohn Marino 			*eon = '\0';
1295a34d5fb1SAntonio Huete Jimenez 			Var_Set(gn, MEMBER, eoa + 1);
1296a34d5fb1SAntonio Huete Jimenez 			Var_Set(gn, ARCHIVE, gn->name);
129701e196c8SJohn Marino 			*eoa = '(';
129801e196c8SJohn Marino 			*eon = ')';
129901e196c8SJohn Marino 		}
130001e196c8SJohn Marino 
1301*6eef5f0cSAntonio Huete Jimenez 		Dir_UpdateMTime(gn, false);
1302a34d5fb1SAntonio Huete Jimenez 		Var_Set(gn, TARGET, GNode_Path(gn));
1303a34d5fb1SAntonio Huete Jimenez 		UnmarkChildren(gn);
1304a34d5fb1SAntonio Huete Jimenez 		HandleUseNodes(gn);
130501e196c8SJohn Marino 
1306a34d5fb1SAntonio Huete Jimenez 		if (!(gn->type & OP_MADE))
130701e196c8SJohn Marino 			Suff_FindDeps(gn);
130801e196c8SJohn Marino 		else {
1309a34d5fb1SAntonio Huete Jimenez 			PretendAllChildrenAreMade(gn);
1310a34d5fb1SAntonio Huete Jimenez 			if (gn->unmade != 0) {
1311a34d5fb1SAntonio Huete Jimenez 				printf(
1312a34d5fb1SAntonio Huete Jimenez 				    "Warning: "
1313a34d5fb1SAntonio Huete Jimenez 				    "%s%s still has %d unmade children\n",
131401e196c8SJohn Marino 				    gn->name, gn->cohort_num, gn->unmade);
131501e196c8SJohn Marino 			}
1316a34d5fb1SAntonio Huete Jimenez 		}
131701e196c8SJohn Marino 
131801e196c8SJohn Marino 		if (gn->unmade != 0)
1319a34d5fb1SAntonio Huete Jimenez 			ExamineLater(&examine, &gn->children);
132001e196c8SJohn Marino 	}
132101e196c8SJohn Marino 
1322a34d5fb1SAntonio Huete Jimenez 	Lst_Done(&examine);
132301e196c8SJohn Marino }
132401e196c8SJohn Marino 
1325a34d5fb1SAntonio Huete Jimenez /* Make the .WAIT node depend on the previous children */
1326a34d5fb1SAntonio Huete Jimenez static void
add_wait_dependency(GNodeListNode * owln,GNode * wn)1327a34d5fb1SAntonio Huete Jimenez add_wait_dependency(GNodeListNode *owln, GNode *wn)
132801e196c8SJohn Marino {
1329a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *cln;
1330a34d5fb1SAntonio Huete Jimenez 	GNode *cn;
133101e196c8SJohn Marino 
1332a34d5fb1SAntonio Huete Jimenez 	for (cln = owln; (cn = cln->datum) != wn; cln = cln->next) {
1333a34d5fb1SAntonio Huete Jimenez 		DEBUG3(MAKE, ".WAIT: add dependency %s%s -> %s\n",
133401e196c8SJohn Marino 		    cn->name, cn->cohort_num, wn->name);
133501e196c8SJohn Marino 
1336*6eef5f0cSAntonio Huete Jimenez 		/*
1337*6eef5f0cSAntonio Huete Jimenez 		 * XXX: This pattern should be factored out, it repeats often
1338*6eef5f0cSAntonio Huete Jimenez 		 */
1339a34d5fb1SAntonio Huete Jimenez 		Lst_Append(&wn->children, cn);
134001e196c8SJohn Marino 		wn->unmade++;
1341a34d5fb1SAntonio Huete Jimenez 		Lst_Append(&cn->parents, wn);
1342a34d5fb1SAntonio Huete Jimenez 	}
134301e196c8SJohn Marino }
134401e196c8SJohn Marino 
1345a34d5fb1SAntonio Huete Jimenez /* Convert .WAIT nodes into dependencies. */
134601e196c8SJohn Marino static void
Make_ProcessWait(GNodeList * targs)1347a34d5fb1SAntonio Huete Jimenez Make_ProcessWait(GNodeList *targs)
134801e196c8SJohn Marino {
134901e196c8SJohn Marino 	GNode *pgn;		/* 'parent' node we are examining */
1350a34d5fb1SAntonio Huete Jimenez 	GNodeListNode *owln;	/* Previous .WAIT node */
1351a34d5fb1SAntonio Huete Jimenez 	GNodeList examine;	/* List of targets to examine */
135201e196c8SJohn Marino 
135301e196c8SJohn Marino 	/*
135401e196c8SJohn Marino 	 * We need all the nodes to have a common parent in order for the
135501e196c8SJohn Marino 	 * .WAIT and .ORDER scheduling to work.
135601e196c8SJohn Marino 	 * Perhaps this should be done earlier...
135701e196c8SJohn Marino 	 */
135801e196c8SJohn Marino 
1359a34d5fb1SAntonio Huete Jimenez 	pgn = GNode_New(".MAIN");
1360*6eef5f0cSAntonio Huete Jimenez 	pgn->flags.remake = true;
136101e196c8SJohn Marino 	pgn->type = OP_PHONY | OP_DEPENDS;
136201e196c8SJohn Marino 	/* Get it displayed in the diag dumps */
1363a34d5fb1SAntonio Huete Jimenez 	Lst_Prepend(Targ_List(), pgn);
136401e196c8SJohn Marino 
1365a34d5fb1SAntonio Huete Jimenez 	{
1366a34d5fb1SAntonio Huete Jimenez 		GNodeListNode *ln;
1367a34d5fb1SAntonio Huete Jimenez 		for (ln = targs->first; ln != NULL; ln = ln->next) {
1368a34d5fb1SAntonio Huete Jimenez 			GNode *cgn = ln->datum;
1369a34d5fb1SAntonio Huete Jimenez 
1370a34d5fb1SAntonio Huete Jimenez 			Lst_Append(&pgn->children, cgn);
1371a34d5fb1SAntonio Huete Jimenez 			Lst_Append(&cgn->parents, pgn);
1372a34d5fb1SAntonio Huete Jimenez 			pgn->unmade++;
1373a34d5fb1SAntonio Huete Jimenez 		}
1374a34d5fb1SAntonio Huete Jimenez 	}
137501e196c8SJohn Marino 
137601e196c8SJohn Marino 	/* Start building with the 'dummy' .MAIN' node */
137701e196c8SJohn Marino 	MakeBuildChild(pgn, NULL);
137801e196c8SJohn Marino 
1379a34d5fb1SAntonio Huete Jimenez 	Lst_Init(&examine);
1380a34d5fb1SAntonio Huete Jimenez 	Lst_Append(&examine, pgn);
138101e196c8SJohn Marino 
1382a34d5fb1SAntonio Huete Jimenez 	while (!Lst_IsEmpty(&examine)) {
1383a34d5fb1SAntonio Huete Jimenez 		GNodeListNode *ln;
1384a34d5fb1SAntonio Huete Jimenez 
1385a34d5fb1SAntonio Huete Jimenez 		pgn = Lst_Dequeue(&examine);
138601e196c8SJohn Marino 
138701e196c8SJohn Marino 		/* We only want to process each child-list once */
1388*6eef5f0cSAntonio Huete Jimenez 		if (pgn->flags.doneWait)
138901e196c8SJohn Marino 			continue;
1390*6eef5f0cSAntonio Huete Jimenez 		pgn->flags.doneWait = true;
1391a34d5fb1SAntonio Huete Jimenez 		DEBUG1(MAKE, "Make_ProcessWait: examine %s\n", pgn->name);
139201e196c8SJohn Marino 
1393a34d5fb1SAntonio Huete Jimenez 		if (pgn->type & OP_DOUBLEDEP)
1394a34d5fb1SAntonio Huete Jimenez 			Lst_PrependAll(&examine, &pgn->cohorts);
139501e196c8SJohn Marino 
1396a34d5fb1SAntonio Huete Jimenez 		owln = pgn->children.first;
1397a34d5fb1SAntonio Huete Jimenez 		for (ln = pgn->children.first; ln != NULL; ln = ln->next) {
1398a34d5fb1SAntonio Huete Jimenez 			GNode *cgn = ln->datum;
139901e196c8SJohn Marino 			if (cgn->type & OP_WAIT) {
1400a34d5fb1SAntonio Huete Jimenez 				add_wait_dependency(owln, cgn);
140101e196c8SJohn Marino 				owln = ln;
140201e196c8SJohn Marino 			} else {
1403a34d5fb1SAntonio Huete Jimenez 				Lst_Append(&examine, cgn);
140401e196c8SJohn Marino 			}
140501e196c8SJohn Marino 		}
140601e196c8SJohn Marino 	}
140701e196c8SJohn Marino 
1408a34d5fb1SAntonio Huete Jimenez 	Lst_Done(&examine);
140901e196c8SJohn Marino }
141001e196c8SJohn Marino 
1411a34d5fb1SAntonio Huete Jimenez /*
1412a34d5fb1SAntonio Huete Jimenez  * Initialize the nodes to remake and the list of nodes which are ready to
1413a34d5fb1SAntonio Huete Jimenez  * be made by doing a breadth-first traversal of the graph starting from the
1414a34d5fb1SAntonio Huete Jimenez  * nodes in the given list. Once this traversal is finished, all the 'leaves'
1415a34d5fb1SAntonio Huete Jimenez  * of the graph are in the toBeMade queue.
1416a34d5fb1SAntonio Huete Jimenez  *
1417a34d5fb1SAntonio Huete Jimenez  * Using this queue and the Job module, work back up the graph, calling on
1418a34d5fb1SAntonio Huete Jimenez  * MakeStartJobs to keep the job table as full as possible.
141901e196c8SJohn Marino  *
142001e196c8SJohn Marino  * Input:
142101e196c8SJohn Marino  *	targs		the initial list of targets
142201e196c8SJohn Marino  *
142301e196c8SJohn Marino  * Results:
1424*6eef5f0cSAntonio Huete Jimenez  *	True if work was done, false otherwise.
142501e196c8SJohn Marino  *
142601e196c8SJohn Marino  * Side Effects:
142701e196c8SJohn Marino  *	The make field of all nodes involved in the creation of the given
142801e196c8SJohn Marino  *	targets is set to 1. The toBeMade list is set to contain all the
142901e196c8SJohn Marino  *	'leaves' of these subgraphs.
143001e196c8SJohn Marino  */
1431*6eef5f0cSAntonio Huete Jimenez bool
Make_Run(GNodeList * targs)1432a34d5fb1SAntonio Huete Jimenez Make_Run(GNodeList *targs)
143301e196c8SJohn Marino {
143401e196c8SJohn Marino 	int errors;		/* Number of errors the Job module reports */
143501e196c8SJohn Marino 
143601e196c8SJohn Marino 	/* Start trying to make the current targets... */
1437a34d5fb1SAntonio Huete Jimenez 	Lst_Init(&toBeMade);
143801e196c8SJohn Marino 
143901e196c8SJohn Marino 	Make_ExpandUse(targs);
144001e196c8SJohn Marino 	Make_ProcessWait(targs);
144101e196c8SJohn Marino 
144201e196c8SJohn Marino 	if (DEBUG(MAKE)) {
1443a34d5fb1SAntonio Huete Jimenez 		debug_printf("#***# full graph\n");
144401e196c8SJohn Marino 		Targ_PrintGraph(1);
144501e196c8SJohn Marino 	}
144601e196c8SJohn Marino 
1447*6eef5f0cSAntonio Huete Jimenez 	if (opts.query) {
144801e196c8SJohn Marino 		/*
1449a34d5fb1SAntonio Huete Jimenez 		 * We wouldn't do any work unless we could start some jobs
1450a34d5fb1SAntonio Huete Jimenez 		 * in the next loop... (we won't actually start any, of
1451a34d5fb1SAntonio Huete Jimenez 		 * course, this is just to see if any of the targets was out
1452a34d5fb1SAntonio Huete Jimenez 		 * of date)
145301e196c8SJohn Marino 		 */
1454ca58f742SDaniel Fojt 		return MakeStartJobs();
145501e196c8SJohn Marino 	}
145601e196c8SJohn Marino 	/*
145701e196c8SJohn Marino 	 * Initialization. At the moment, no jobs are running and until some
145801e196c8SJohn Marino 	 * get started, nothing will happen since the remaining upward
145901e196c8SJohn Marino 	 * traversal of the graph is performed by the routines in job.c upon
146001e196c8SJohn Marino 	 * the finishing of a job. So we fill the Job table as much as we can
146101e196c8SJohn Marino 	 * before going into our loop.
146201e196c8SJohn Marino 	 */
146301e196c8SJohn Marino 	(void)MakeStartJobs();
146401e196c8SJohn Marino 
146501e196c8SJohn Marino 	/*
146601e196c8SJohn Marino 	 * Main Loop: The idea here is that the ending of jobs will take
1467a34d5fb1SAntonio Huete Jimenez 	 * care of the maintenance of data structures and the waiting for
1468a34d5fb1SAntonio Huete Jimenez 	 * output will cause us to be idle most of the time while our
1469a34d5fb1SAntonio Huete Jimenez 	 * children run as much as possible. Because the job table is kept
1470a34d5fb1SAntonio Huete Jimenez 	 * as full as possible, the only time when it will be empty is when
1471a34d5fb1SAntonio Huete Jimenez 	 * all the jobs which need running have been run, so that is the end
1472a34d5fb1SAntonio Huete Jimenez 	 * condition of this loop. Note that the Job module will exit if
1473a34d5fb1SAntonio Huete Jimenez 	 * there were any errors unless the keepgoing flag was given.
147401e196c8SJohn Marino 	 */
1475a34d5fb1SAntonio Huete Jimenez 	while (!Lst_IsEmpty(&toBeMade) || jobTokensRunning > 0) {
147601e196c8SJohn Marino 		Job_CatchOutput();
147701e196c8SJohn Marino 		(void)MakeStartJobs();
147801e196c8SJohn Marino 	}
147901e196c8SJohn Marino 
148001e196c8SJohn Marino 	errors = Job_Finish();
148101e196c8SJohn Marino 
148201e196c8SJohn Marino 	/*
148301e196c8SJohn Marino 	 * Print the final status of each target. E.g. if it wasn't made
148401e196c8SJohn Marino 	 * because some inferior reported an error.
148501e196c8SJohn Marino 	 */
1486a34d5fb1SAntonio Huete Jimenez 	DEBUG1(MAKE, "done: errors %d\n", errors);
148701e196c8SJohn Marino 	if (errors == 0) {
1488a34d5fb1SAntonio Huete Jimenez 		MakePrintStatusList(targs, &errors);
148901e196c8SJohn Marino 		if (DEBUG(MAKE)) {
1490a34d5fb1SAntonio Huete Jimenez 			debug_printf("done: errors %d\n", errors);
1491a34d5fb1SAntonio Huete Jimenez 			if (errors > 0)
149201e196c8SJohn Marino 				Targ_PrintGraph(4);
149301e196c8SJohn Marino 		}
149401e196c8SJohn Marino 	}
1495a34d5fb1SAntonio Huete Jimenez 	return errors > 0;
149601e196c8SJohn Marino }
1497