1321964Ssjg/*	$NetBSD: make.c,v 1.96 2016/11/10 23:41:58 sjg Exp $	*/
2236769Sobrien
3236769Sobrien/*
4236769Sobrien * Copyright (c) 1988, 1989, 1990, 1993
5236769Sobrien *	The Regents of the University of California.  All rights reserved.
6236769Sobrien *
7236769Sobrien * This code is derived from software contributed to Berkeley by
8236769Sobrien * Adam de Boor.
9236769Sobrien *
10236769Sobrien * Redistribution and use in source and binary forms, with or without
11236769Sobrien * modification, are permitted provided that the following conditions
12236769Sobrien * are met:
13236769Sobrien * 1. Redistributions of source code must retain the above copyright
14236769Sobrien *    notice, this list of conditions and the following disclaimer.
15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
16236769Sobrien *    notice, this list of conditions and the following disclaimer in the
17236769Sobrien *    documentation and/or other materials provided with the distribution.
18236769Sobrien * 3. Neither the name of the University nor the names of its contributors
19236769Sobrien *    may be used to endorse or promote products derived from this software
20236769Sobrien *    without specific prior written permission.
21236769Sobrien *
22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32236769Sobrien * SUCH DAMAGE.
33236769Sobrien */
34236769Sobrien
35236769Sobrien/*
36236769Sobrien * Copyright (c) 1989 by Berkeley Softworks
37236769Sobrien * All rights reserved.
38236769Sobrien *
39236769Sobrien * This code is derived from software contributed to Berkeley by
40236769Sobrien * Adam de Boor.
41236769Sobrien *
42236769Sobrien * Redistribution and use in source and binary forms, with or without
43236769Sobrien * modification, are permitted provided that the following conditions
44236769Sobrien * are met:
45236769Sobrien * 1. Redistributions of source code must retain the above copyright
46236769Sobrien *    notice, this list of conditions and the following disclaimer.
47236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
48236769Sobrien *    notice, this list of conditions and the following disclaimer in the
49236769Sobrien *    documentation and/or other materials provided with the distribution.
50236769Sobrien * 3. All advertising materials mentioning features or use of this software
51236769Sobrien *    must display the following acknowledgement:
52236769Sobrien *	This product includes software developed by the University of
53236769Sobrien *	California, Berkeley and its contributors.
54236769Sobrien * 4. Neither the name of the University nor the names of its contributors
55236769Sobrien *    may be used to endorse or promote products derived from this software
56236769Sobrien *    without specific prior written permission.
57236769Sobrien *
58236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68236769Sobrien * SUCH DAMAGE.
69236769Sobrien */
70236769Sobrien
71236769Sobrien#ifndef MAKE_NATIVE
72321964Ssjgstatic char rcsid[] = "$NetBSD: make.c,v 1.96 2016/11/10 23:41:58 sjg Exp $";
73236769Sobrien#else
74236769Sobrien#include <sys/cdefs.h>
75236769Sobrien#ifndef lint
76236769Sobrien#if 0
77236769Sobrienstatic char sccsid[] = "@(#)make.c	8.1 (Berkeley) 6/6/93";
78236769Sobrien#else
79321964Ssjg__RCSID("$NetBSD: make.c,v 1.96 2016/11/10 23:41:58 sjg Exp $");
80236769Sobrien#endif
81236769Sobrien#endif /* not lint */
82236769Sobrien#endif
83236769Sobrien
84236769Sobrien/*-
85236769Sobrien * make.c --
86236769Sobrien *	The functions which perform the examination of targets and
87236769Sobrien *	their suitability for creation
88236769Sobrien *
89236769Sobrien * Interface:
90236769Sobrien *	Make_Run 	    	Initialize things for the module and recreate
91236769Sobrien *	    	  	    	whatever needs recreating. Returns TRUE if
92236769Sobrien *	    	    	    	work was (or would have been) done and FALSE
93236769Sobrien *	    	  	    	otherwise.
94236769Sobrien *
95236769Sobrien *	Make_Update	    	Update all parents of a given child. Performs
96236769Sobrien *	    	  	    	various bookkeeping chores like the updating
97236769Sobrien *	    	  	    	of the cmgn field of the parent, filling
98236769Sobrien *	    	  	    	of the IMPSRC context variable, etc. It will
99236769Sobrien *	    	  	    	place the parent on the toBeMade queue if it
100236769Sobrien *	    	  	    	should be.
101236769Sobrien *
102236769Sobrien *	Make_TimeStamp	    	Function to set the parent's cmgn field
103236769Sobrien *	    	  	    	based on a child's modification time.
104236769Sobrien *
105236769Sobrien *	Make_DoAllVar	    	Set up the various local variables for a
106236769Sobrien *	    	  	    	target, including the .ALLSRC variable, making
107236769Sobrien *	    	  	    	sure that any variable that needs to exist
108236769Sobrien *	    	  	    	at the very least has the empty value.
109236769Sobrien *
110236769Sobrien *	Make_OODate 	    	Determine if a target is out-of-date.
111236769Sobrien *
112236769Sobrien *	Make_HandleUse	    	See if a child is a .USE node for a parent
113236769Sobrien *				and perform the .USE actions if so.
114236769Sobrien *
115236769Sobrien *	Make_ExpandUse	    	Expand .USE nodes
116236769Sobrien */
117236769Sobrien
118236769Sobrien#include    "make.h"
119236769Sobrien#include    "hash.h"
120236769Sobrien#include    "dir.h"
121236769Sobrien#include    "job.h"
122236769Sobrien
123236769Sobrienstatic unsigned int checked = 1;/* Sequence # to detect recursion */
124236769Sobrienstatic Lst     	toBeMade;	/* The current fringe of the graph. These
125236769Sobrien				 * are nodes which await examination by
126236769Sobrien				 * MakeOODate. It is added to by
127236769Sobrien				 * Make_Update and subtracted from by
128236769Sobrien				 * MakeStartJobs */
129236769Sobrien
130236769Sobrienstatic int MakeAddChild(void *, void *);
131236769Sobrienstatic int MakeFindChild(void *, void *);
132236769Sobrienstatic int MakeUnmark(void *, void *);
133236769Sobrienstatic int MakeAddAllSrc(void *, void *);
134236769Sobrienstatic int MakeTimeStamp(void *, void *);
135236769Sobrienstatic int MakeHandleUse(void *, void *);
136236769Sobrienstatic Boolean MakeStartJobs(void);
137236769Sobrienstatic int MakePrintStatus(void *, void *);
138236769Sobrienstatic int MakeCheckOrder(void *, void *);
139236769Sobrienstatic int MakeBuildChild(void *, void *);
140236769Sobrienstatic int MakeBuildParent(void *, void *);
141236769Sobrien
142237578SobrienMAKE_ATTR_DEAD static void
143236769Sobrienmake_abort(GNode *gn, int line)
144236769Sobrien{
145236769Sobrien    static int two = 2;
146236769Sobrien
147236769Sobrien    fprintf(debug_file, "make_abort from line %d\n", line);
148236769Sobrien    Targ_PrintNode(gn, &two);
149236769Sobrien    Lst_ForEach(toBeMade, Targ_PrintNode, &two);
150236769Sobrien    Targ_PrintGraph(3);
151236769Sobrien    abort();
152236769Sobrien}
153236769Sobrien
154236769Sobrien/*-
155236769Sobrien *-----------------------------------------------------------------------
156236769Sobrien * Make_TimeStamp --
157236769Sobrien *	Set the cmgn field of a parent node based on the mtime stamp in its
158236769Sobrien *	child. Called from MakeOODate via Lst_ForEach.
159236769Sobrien *
160236769Sobrien * Input:
161236769Sobrien *	pgn		the current parent
162236769Sobrien *	cgn		the child we've just examined
163236769Sobrien *
164236769Sobrien * Results:
165236769Sobrien *	Always returns 0.
166236769Sobrien *
167236769Sobrien * Side Effects:
168236769Sobrien *	The cmgn of the parent node will be changed if the mtime
169236769Sobrien *	field of the child is greater than it.
170236769Sobrien *-----------------------------------------------------------------------
171236769Sobrien */
172236769Sobrienint
173236769SobrienMake_TimeStamp(GNode *pgn, GNode *cgn)
174236769Sobrien{
175236769Sobrien    if (pgn->cmgn == NULL || cgn->mtime > pgn->cmgn->mtime) {
176236769Sobrien	pgn->cmgn = cgn;
177236769Sobrien    }
178236769Sobrien    return (0);
179236769Sobrien}
180236769Sobrien
181236769Sobrien/*
182236769Sobrien * Input:
183236769Sobrien *	pgn		the current parent
184236769Sobrien *	cgn		the child we've just examined
185236769Sobrien *
186236769Sobrien */
187236769Sobrienstatic int
188236769SobrienMakeTimeStamp(void *pgn, void *cgn)
189236769Sobrien{
190236769Sobrien    return Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
191236769Sobrien}
192236769Sobrien
193236769Sobrien/*-
194236769Sobrien *-----------------------------------------------------------------------
195236769Sobrien * Make_OODate --
196236769Sobrien *	See if a given node is out of date with respect to its sources.
197236769Sobrien *	Used by Make_Run when deciding which nodes to place on the
198236769Sobrien *	toBeMade queue initially and by Make_Update to screen out USE and
199236769Sobrien *	EXEC nodes. In the latter case, however, any other sort of node
200236769Sobrien *	must be considered out-of-date since at least one of its children
201236769Sobrien *	will have been recreated.
202236769Sobrien *
203236769Sobrien * Input:
204236769Sobrien *	gn		the node to check
205236769Sobrien *
206236769Sobrien * Results:
207236769Sobrien *	TRUE if the node is out of date. FALSE otherwise.
208236769Sobrien *
209236769Sobrien * Side Effects:
210236769Sobrien *	The mtime field of the node and the cmgn field of its parents
211236769Sobrien *	will/may be changed.
212236769Sobrien *-----------------------------------------------------------------------
213236769Sobrien */
214236769SobrienBoolean
215236769SobrienMake_OODate(GNode *gn)
216236769Sobrien{
217236769Sobrien    Boolean         oodate;
218236769Sobrien
219236769Sobrien    /*
220236769Sobrien     * Certain types of targets needn't even be sought as their datedness
221236769Sobrien     * doesn't depend on their modification time...
222236769Sobrien     */
223236769Sobrien    if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
224236769Sobrien	(void)Dir_MTime(gn, 1);
225236769Sobrien	if (DEBUG(MAKE)) {
226236769Sobrien	    if (gn->mtime != 0) {
227236769Sobrien		fprintf(debug_file, "modified %s...", Targ_FmtTime(gn->mtime));
228236769Sobrien	    } else {
229236769Sobrien		fprintf(debug_file, "non-existent...");
230236769Sobrien	    }
231236769Sobrien	}
232236769Sobrien    }
233236769Sobrien
234236769Sobrien    /*
235236769Sobrien     * A target is remade in one of the following circumstances:
236236769Sobrien     *	its modification time is smaller than that of its youngest child
237236769Sobrien     *	    and it would actually be run (has commands or type OP_NOP)
238236769Sobrien     *	it's the object of a force operator
239236769Sobrien     *	it has no children, was on the lhs of an operator and doesn't exist
240236769Sobrien     *	    already.
241236769Sobrien     *
242236769Sobrien     * Libraries are only considered out-of-date if the archive module says
243236769Sobrien     * they are.
244236769Sobrien     *
245236769Sobrien     * These weird rules are brought to you by Backward-Compatibility and
246236769Sobrien     * the strange people who wrote 'Make'.
247236769Sobrien     */
248236769Sobrien    if (gn->type & (OP_USE|OP_USEBEFORE)) {
249236769Sobrien	/*
250236769Sobrien	 * If the node is a USE node it is *never* out of date
251236769Sobrien	 * no matter *what*.
252236769Sobrien	 */
253236769Sobrien	if (DEBUG(MAKE)) {
254236769Sobrien	    fprintf(debug_file, ".USE node...");
255236769Sobrien	}
256236769Sobrien	oodate = FALSE;
257236769Sobrien    } else if ((gn->type & OP_LIB) &&
258236769Sobrien	       ((gn->mtime==0) || Arch_IsLib(gn))) {
259236769Sobrien	if (DEBUG(MAKE)) {
260236769Sobrien	    fprintf(debug_file, "library...");
261236769Sobrien	}
262236769Sobrien
263236769Sobrien	/*
264236769Sobrien	 * always out of date if no children and :: target
265236769Sobrien	 * or non-existent.
266236769Sobrien	 */
267236769Sobrien	oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
268236769Sobrien		  (gn->cmgn == NULL && (gn->type & OP_DOUBLEDEP)));
269236769Sobrien    } else if (gn->type & OP_JOIN) {
270236769Sobrien	/*
271236769Sobrien	 * A target with the .JOIN attribute is only considered
272236769Sobrien	 * out-of-date if any of its children was out-of-date.
273236769Sobrien	 */
274236769Sobrien	if (DEBUG(MAKE)) {
275236769Sobrien	    fprintf(debug_file, ".JOIN node...");
276236769Sobrien	}
277236769Sobrien	if (DEBUG(MAKE)) {
278236769Sobrien	    fprintf(debug_file, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
279236769Sobrien	}
280236769Sobrien	oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
281236769Sobrien    } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
282236769Sobrien	/*
283236769Sobrien	 * A node which is the object of the force (!) operator or which has
284236769Sobrien	 * the .EXEC attribute is always considered out-of-date.
285236769Sobrien	 */
286236769Sobrien	if (DEBUG(MAKE)) {
287236769Sobrien	    if (gn->type & OP_FORCE) {
288236769Sobrien		fprintf(debug_file, "! operator...");
289236769Sobrien	    } else if (gn->type & OP_PHONY) {
290236769Sobrien		fprintf(debug_file, ".PHONY node...");
291236769Sobrien	    } else {
292236769Sobrien		fprintf(debug_file, ".EXEC node...");
293236769Sobrien	    }
294236769Sobrien	}
295236769Sobrien	oodate = TRUE;
296236769Sobrien    } else if ((gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) ||
297236769Sobrien	       (gn->cmgn == NULL &&
298236769Sobrien		((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
299236769Sobrien		  || gn->type & OP_DOUBLEDEP)))
300236769Sobrien    {
301236769Sobrien	/*
302236769Sobrien	 * A node whose modification time is less than that of its
303236769Sobrien	 * youngest child or that has no children (cmgn == NULL) and
304236769Sobrien	 * either doesn't exist (mtime == 0) and it isn't optional
305236769Sobrien	 * or was the object of a * :: operator is out-of-date.
306236769Sobrien	 * Why? Because that's the way Make does it.
307236769Sobrien	 */
308236769Sobrien	if (DEBUG(MAKE)) {
309236769Sobrien	    if (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) {
310236769Sobrien		fprintf(debug_file, "modified before source %s...",
311321964Ssjg		    gn->cmgn->path ? gn->cmgn->path : gn->cmgn->name);
312236769Sobrien	    } else if (gn->mtime == 0) {
313236769Sobrien		fprintf(debug_file, "non-existent and no sources...");
314236769Sobrien	    } else {
315236769Sobrien		fprintf(debug_file, ":: operator and no sources...");
316236769Sobrien	    }
317236769Sobrien	}
318236769Sobrien	oodate = TRUE;
319236769Sobrien    } else {
320236769Sobrien	/*
321236769Sobrien	 * When a non-existing child with no sources
322236769Sobrien	 * (such as a typically used FORCE source) has been made and
323236769Sobrien	 * the target of the child (usually a directory) has the same
324236769Sobrien	 * timestamp as the timestamp just given to the non-existing child
325236769Sobrien	 * after it was considered made.
326236769Sobrien	 */
327236769Sobrien	if (DEBUG(MAKE)) {
328236769Sobrien	    if (gn->flags & FORCE)
329236769Sobrien		fprintf(debug_file, "non existing child...");
330236769Sobrien	}
331236769Sobrien	oodate = (gn->flags & FORCE) ? TRUE : FALSE;
332236769Sobrien    }
333236769Sobrien
334236769Sobrien#ifdef USE_META
335236769Sobrien    if (useMeta) {
336236769Sobrien	oodate = meta_oodate(gn, oodate);
337236769Sobrien    }
338236769Sobrien#endif
339236769Sobrien
340236769Sobrien    /*
341236769Sobrien     * If the target isn't out-of-date, the parents need to know its
342236769Sobrien     * modification time. Note that targets that appear to be out-of-date
343236769Sobrien     * but aren't, because they have no commands and aren't of type OP_NOP,
344236769Sobrien     * have their mtime stay below their children's mtime to keep parents from
345236769Sobrien     * thinking they're out-of-date.
346236769Sobrien     */
347236769Sobrien    if (!oodate) {
348236769Sobrien	Lst_ForEach(gn->parents, MakeTimeStamp, gn);
349236769Sobrien    }
350236769Sobrien
351236769Sobrien    return (oodate);
352236769Sobrien}
353236769Sobrien
354236769Sobrien/*-
355236769Sobrien *-----------------------------------------------------------------------
356236769Sobrien * MakeAddChild  --
357236769Sobrien *	Function used by Make_Run to add a child to the list l.
358236769Sobrien *	It will only add the child if its make field is FALSE.
359236769Sobrien *
360236769Sobrien * Input:
361236769Sobrien *	gnp		the node to add
362236769Sobrien *	lp		the list to which to add it
363236769Sobrien *
364236769Sobrien * Results:
365236769Sobrien *	Always returns 0
366236769Sobrien *
367236769Sobrien * Side Effects:
368236769Sobrien *	The given list is extended
369236769Sobrien *-----------------------------------------------------------------------
370236769Sobrien */
371236769Sobrienstatic int
372236769SobrienMakeAddChild(void *gnp, void *lp)
373236769Sobrien{
374236769Sobrien    GNode          *gn = (GNode *)gnp;
375236769Sobrien    Lst            l = (Lst) lp;
376236769Sobrien
377236769Sobrien    if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
378236769Sobrien	if (DEBUG(MAKE))
379236769Sobrien	    fprintf(debug_file, "MakeAddChild: need to examine %s%s\n",
380236769Sobrien		gn->name, gn->cohort_num);
381236769Sobrien	(void)Lst_EnQueue(l, gn);
382236769Sobrien    }
383236769Sobrien    return (0);
384236769Sobrien}
385236769Sobrien
386236769Sobrien/*-
387236769Sobrien *-----------------------------------------------------------------------
388236769Sobrien * MakeFindChild  --
389236769Sobrien *	Function used by Make_Run to find the pathname of a child
390236769Sobrien *	that was already made.
391236769Sobrien *
392236769Sobrien * Input:
393236769Sobrien *	gnp		the node to find
394236769Sobrien *
395236769Sobrien * Results:
396236769Sobrien *	Always returns 0
397236769Sobrien *
398236769Sobrien * Side Effects:
399236769Sobrien *	The path and mtime of the node and the cmgn of the parent are
400236769Sobrien *	updated; the unmade children count of the parent is decremented.
401236769Sobrien *-----------------------------------------------------------------------
402236769Sobrien */
403236769Sobrienstatic int
404236769SobrienMakeFindChild(void *gnp, void *pgnp)
405236769Sobrien{
406236769Sobrien    GNode          *gn = (GNode *)gnp;
407236769Sobrien    GNode          *pgn = (GNode *)pgnp;
408236769Sobrien
409236769Sobrien    (void)Dir_MTime(gn, 0);
410236769Sobrien    Make_TimeStamp(pgn, gn);
411236769Sobrien    pgn->unmade--;
412236769Sobrien
413236769Sobrien    return (0);
414236769Sobrien}
415236769Sobrien
416236769Sobrien/*-
417236769Sobrien *-----------------------------------------------------------------------
418236769Sobrien * Make_HandleUse --
419236769Sobrien *	Function called by Make_Run and SuffApplyTransform on the downward
420236769Sobrien *	pass to handle .USE and transformation nodes. It implements the
421236769Sobrien *	.USE and transformation functionality by copying the node's commands,
422236769Sobrien *	type flags and children to the parent node.
423236769Sobrien *
424236769Sobrien *	A .USE node is much like an explicit transformation rule, except
425236769Sobrien *	its commands are always added to the target node, even if the
426236769Sobrien *	target already has commands.
427236769Sobrien *
428236769Sobrien * Input:
429236769Sobrien *	cgn		The .USE node
430236769Sobrien *	pgn		The target of the .USE node
431236769Sobrien *
432236769Sobrien * Results:
433236769Sobrien *	none
434236769Sobrien *
435236769Sobrien * Side Effects:
436236769Sobrien *	Children and commands may be added to the parent and the parent's
437236769Sobrien *	type may be changed.
438236769Sobrien *
439236769Sobrien *-----------------------------------------------------------------------
440236769Sobrien */
441236769Sobrienvoid
442236769SobrienMake_HandleUse(GNode *cgn, GNode *pgn)
443236769Sobrien{
444236769Sobrien    LstNode	ln; 	/* An element in the children list */
445236769Sobrien
446236769Sobrien#ifdef DEBUG_SRC
447236769Sobrien    if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
448236769Sobrien	fprintf(debug_file, "Make_HandleUse: called for plain node %s\n", cgn->name);
449236769Sobrien	return;
450236769Sobrien    }
451236769Sobrien#endif
452236769Sobrien
453236769Sobrien    if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
454236769Sobrien	    if (cgn->type & OP_USEBEFORE) {
455236769Sobrien		/*
456236769Sobrien		 * .USEBEFORE --
457236769Sobrien		 *	prepend the child's commands to the parent.
458236769Sobrien		 */
459236769Sobrien		Lst cmds = pgn->commands;
460236769Sobrien		pgn->commands = Lst_Duplicate(cgn->commands, NULL);
461236769Sobrien		(void)Lst_Concat(pgn->commands, cmds, LST_CONCNEW);
462236769Sobrien		Lst_Destroy(cmds, NULL);
463236769Sobrien	    } else {
464236769Sobrien		/*
465236769Sobrien		 * .USE or target has no commands --
466236769Sobrien		 *	append the child's commands to the parent.
467236769Sobrien		 */
468236769Sobrien		(void)Lst_Concat(pgn->commands, cgn->commands, LST_CONCNEW);
469236769Sobrien	    }
470236769Sobrien    }
471236769Sobrien
472236769Sobrien    if (Lst_Open(cgn->children) == SUCCESS) {
473236769Sobrien	while ((ln = Lst_Next(cgn->children)) != NULL) {
474236769Sobrien	    GNode *tgn, *gn = (GNode *)Lst_Datum(ln);
475236769Sobrien
476236769Sobrien	    /*
477236769Sobrien	     * Expand variables in the .USE node's name
478236769Sobrien	     * and save the unexpanded form.
479236769Sobrien	     * We don't need to do this for commands.
480236769Sobrien	     * They get expanded properly when we execute.
481236769Sobrien	     */
482236769Sobrien	    if (gn->uname == NULL) {
483236769Sobrien		gn->uname = gn->name;
484236769Sobrien	    } else {
485321964Ssjg		free(gn->name);
486236769Sobrien	    }
487321964Ssjg	    gn->name = Var_Subst(NULL, gn->uname, pgn, VARF_WANTRES);
488236769Sobrien	    if (gn->name && gn->uname && strcmp(gn->name, gn->uname) != 0) {
489236769Sobrien		/* See if we have a target for this node. */
490236769Sobrien		tgn = Targ_FindNode(gn->name, TARG_NOCREATE);
491236769Sobrien		if (tgn != NULL)
492236769Sobrien		    gn = tgn;
493236769Sobrien	    }
494236769Sobrien
495236769Sobrien	    (void)Lst_AtEnd(pgn->children, gn);
496236769Sobrien	    (void)Lst_AtEnd(gn->parents, pgn);
497236769Sobrien	    pgn->unmade += 1;
498236769Sobrien	}
499236769Sobrien	Lst_Close(cgn->children);
500236769Sobrien    }
501236769Sobrien
502236769Sobrien    pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
503236769Sobrien}
504236769Sobrien
505236769Sobrien/*-
506236769Sobrien *-----------------------------------------------------------------------
507236769Sobrien * MakeHandleUse --
508236769Sobrien *	Callback function for Lst_ForEach, used by Make_Run on the downward
509236769Sobrien *	pass to handle .USE nodes. Should be called before the children
510236769Sobrien *	are enqueued to be looked at by MakeAddChild.
511236769Sobrien *	This function calls Make_HandleUse to copy the .USE node's commands,
512236769Sobrien *	type flags and children to the parent node.
513236769Sobrien *
514236769Sobrien * Input:
515236769Sobrien *	cgnp		the child we've just examined
516236769Sobrien *	pgnp		the current parent
517236769Sobrien *
518236769Sobrien * Results:
519236769Sobrien *	returns 0.
520236769Sobrien *
521236769Sobrien * Side Effects:
522236769Sobrien *	After expansion, .USE child nodes are removed from the parent
523236769Sobrien *
524236769Sobrien *-----------------------------------------------------------------------
525236769Sobrien */
526236769Sobrienstatic int
527236769SobrienMakeHandleUse(void *cgnp, void *pgnp)
528236769Sobrien{
529236769Sobrien    GNode	*cgn = (GNode *)cgnp;
530236769Sobrien    GNode	*pgn = (GNode *)pgnp;
531236769Sobrien    LstNode	ln; 	/* An element in the children list */
532236769Sobrien    int		unmarked;
533236769Sobrien
534236769Sobrien    unmarked = ((cgn->type & OP_MARK) == 0);
535236769Sobrien    cgn->type |= OP_MARK;
536236769Sobrien
537236769Sobrien    if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
538236769Sobrien	return (0);
539236769Sobrien
540236769Sobrien    if (unmarked)
541236769Sobrien	Make_HandleUse(cgn, pgn);
542236769Sobrien
543236769Sobrien    /*
544236769Sobrien     * This child node is now "made", so we decrement the count of
545236769Sobrien     * unmade children in the parent... We also remove the child
546236769Sobrien     * from the parent's list to accurately reflect the number of decent
547236769Sobrien     * children the parent has. This is used by Make_Run to decide
548236769Sobrien     * whether to queue the parent or examine its children...
549236769Sobrien     */
550236769Sobrien    if ((ln = Lst_Member(pgn->children, cgn)) != NULL) {
551236769Sobrien	Lst_Remove(pgn->children, ln);
552236769Sobrien	pgn->unmade--;
553236769Sobrien    }
554236769Sobrien    return (0);
555236769Sobrien}
556236769Sobrien
557236769Sobrien
558236769Sobrien/*-
559236769Sobrien *-----------------------------------------------------------------------
560236769Sobrien * Make_Recheck --
561236769Sobrien *	Check the modification time of a gnode, and update it as described
562236769Sobrien *	in the comments below.
563236769Sobrien *
564236769Sobrien * Results:
565292068Ssjg *	returns 0 if the gnode does not exist, or its filesystem
566236769Sobrien *	time if it does.
567236769Sobrien *
568236769Sobrien * Side Effects:
569236769Sobrien *	the gnode's modification time and path name are affected.
570236769Sobrien *
571236769Sobrien *-----------------------------------------------------------------------
572236769Sobrien */
573236769Sobrientime_t
574236769SobrienMake_Recheck(GNode *gn)
575236769Sobrien{
576236769Sobrien    time_t mtime = Dir_MTime(gn, 1);
577236769Sobrien
578236769Sobrien#ifndef RECHECK
579236769Sobrien    /*
580236769Sobrien     * We can't re-stat the thing, but we can at least take care of rules
581236769Sobrien     * where a target depends on a source that actually creates the
582236769Sobrien     * target, but only if it has changed, e.g.
583236769Sobrien     *
584236769Sobrien     * parse.h : parse.o
585236769Sobrien     *
586236769Sobrien     * parse.o : parse.y
587236769Sobrien     *  	yacc -d parse.y
588236769Sobrien     *  	cc -c y.tab.c
589236769Sobrien     *  	mv y.tab.o parse.o
590236769Sobrien     *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
591236769Sobrien     *
592236769Sobrien     * In this case, if the definitions produced by yacc haven't changed
593236769Sobrien     * from before, parse.h won't have been updated and gn->mtime will
594236769Sobrien     * reflect the current modification time for parse.h. This is
595236769Sobrien     * something of a kludge, I admit, but it's a useful one..
596236769Sobrien     * XXX: People like to use a rule like
597236769Sobrien     *
598236769Sobrien     * FRC:
599236769Sobrien     *
600236769Sobrien     * To force things that depend on FRC to be made, so we have to
601236769Sobrien     * check for gn->children being empty as well...
602236769Sobrien     */
603236769Sobrien    if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
604236769Sobrien	gn->mtime = now;
605236769Sobrien    }
606236769Sobrien#else
607236769Sobrien    /*
608236769Sobrien     * This is what Make does and it's actually a good thing, as it
609236769Sobrien     * allows rules like
610236769Sobrien     *
611236769Sobrien     *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
612236769Sobrien     *
613236769Sobrien     * to function as intended. Unfortunately, thanks to the stateless
614236769Sobrien     * nature of NFS (by which I mean the loose coupling of two clients
615236769Sobrien     * using the same file from a common server), there are times
616236769Sobrien     * when the modification time of a file created on a remote
617236769Sobrien     * machine will not be modified before the local stat() implied by
618236769Sobrien     * the Dir_MTime occurs, thus leading us to believe that the file
619236769Sobrien     * is unchanged, wreaking havoc with files that depend on this one.
620236769Sobrien     *
621236769Sobrien     * I have decided it is better to make too much than to make too
622236769Sobrien     * little, so this stuff is commented out unless you're sure it's ok.
623236769Sobrien     * -- ardeb 1/12/88
624236769Sobrien     */
625236769Sobrien    /*
626236769Sobrien     * Christos, 4/9/92: If we are  saving commands pretend that
627236769Sobrien     * the target is made now. Otherwise archives with ... rules
628236769Sobrien     * don't work!
629236769Sobrien     */
630236769Sobrien    if (NoExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
631236769Sobrien	    (mtime == 0 && !(gn->type & OP_WAIT))) {
632236769Sobrien	if (DEBUG(MAKE)) {
633236769Sobrien	    fprintf(debug_file, " recheck(%s): update time from %s to now\n",
634236769Sobrien		   gn->name, Targ_FmtTime(gn->mtime));
635236769Sobrien	}
636236769Sobrien	gn->mtime = now;
637236769Sobrien    }
638236769Sobrien    else {
639236769Sobrien	if (DEBUG(MAKE)) {
640236769Sobrien	    fprintf(debug_file, " recheck(%s): current update time: %s\n",
641236769Sobrien		   gn->name, Targ_FmtTime(gn->mtime));
642236769Sobrien	}
643236769Sobrien    }
644236769Sobrien#endif
645236769Sobrien    return mtime;
646236769Sobrien}
647236769Sobrien
648236769Sobrien/*-
649236769Sobrien *-----------------------------------------------------------------------
650236769Sobrien * Make_Update  --
651236769Sobrien *	Perform update on the parents of a node. Used by JobFinish once
652236769Sobrien *	a node has been dealt with and by MakeStartJobs if it finds an
653236769Sobrien *	up-to-date node.
654236769Sobrien *
655236769Sobrien * Input:
656236769Sobrien *	cgn		the child node
657236769Sobrien *
658236769Sobrien * Results:
659236769Sobrien *	Always returns 0
660236769Sobrien *
661236769Sobrien * Side Effects:
662236769Sobrien *	The unmade field of pgn is decremented and pgn may be placed on
663236769Sobrien *	the toBeMade queue if this field becomes 0.
664236769Sobrien *
665236769Sobrien * 	If the child was made, the parent's flag CHILDMADE field will be
666236769Sobrien *	set true.
667236769Sobrien *
668236769Sobrien *	If the child is not up-to-date and still does not exist,
669236769Sobrien *	set the FORCE flag on the parents.
670236769Sobrien *
671236769Sobrien *	If the child wasn't made, the cmgn field of the parent will be
672236769Sobrien *	altered if the child's mtime is big enough.
673236769Sobrien *
674236769Sobrien *	Finally, if the child is the implied source for the parent, the
675236769Sobrien *	parent's IMPSRC variable is set appropriately.
676236769Sobrien *
677236769Sobrien *-----------------------------------------------------------------------
678236769Sobrien */
679236769Sobrienvoid
680236769SobrienMake_Update(GNode *cgn)
681236769Sobrien{
682236769Sobrien    GNode 	*pgn;	/* the parent node */
683236769Sobrien    char  	*cname;	/* the child's name */
684236769Sobrien    LstNode	ln; 	/* Element in parents and iParents lists */
685236769Sobrien    time_t	mtime = -1;
686236769Sobrien    char	*p1;
687236769Sobrien    Lst		parents;
688236769Sobrien    GNode	*centurion;
689236769Sobrien
690236769Sobrien    /* It is save to re-examine any nodes again */
691236769Sobrien    checked++;
692236769Sobrien
693236769Sobrien    cname = Var_Value(TARGET, cgn, &p1);
694321964Ssjg    free(p1);
695236769Sobrien
696236769Sobrien    if (DEBUG(MAKE))
697236769Sobrien	fprintf(debug_file, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
698236769Sobrien
699236769Sobrien    /*
700236769Sobrien     * If the child was actually made, see what its modification time is
701236769Sobrien     * now -- some rules won't actually update the file. If the file still
702236769Sobrien     * doesn't exist, make its mtime now.
703236769Sobrien     */
704236769Sobrien    if (cgn->made != UPTODATE) {
705236769Sobrien	mtime = Make_Recheck(cgn);
706236769Sobrien    }
707236769Sobrien
708236769Sobrien    /*
709236769Sobrien     * If this is a `::' node, we must consult its first instance
710236769Sobrien     * which is where all parents are linked.
711236769Sobrien     */
712236769Sobrien    if ((centurion = cgn->centurion) != NULL) {
713236769Sobrien	if (!Lst_IsEmpty(cgn->parents))
714236769Sobrien		Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
715236769Sobrien	centurion->unmade_cohorts -= 1;
716236769Sobrien	if (centurion->unmade_cohorts < 0)
717236769Sobrien	    Error("Graph cycles through centurion %s", centurion->name);
718236769Sobrien    } else {
719236769Sobrien	centurion = cgn;
720236769Sobrien    }
721236769Sobrien    parents = centurion->parents;
722236769Sobrien
723236769Sobrien    /* If this was a .ORDER node, schedule the RHS */
724236769Sobrien    Lst_ForEach(centurion->order_succ, MakeBuildParent, Lst_First(toBeMade));
725236769Sobrien
726236769Sobrien    /* Now mark all the parents as having one less unmade child */
727236769Sobrien    if (Lst_Open(parents) == SUCCESS) {
728236769Sobrien	while ((ln = Lst_Next(parents)) != NULL) {
729236769Sobrien	    pgn = (GNode *)Lst_Datum(ln);
730236769Sobrien	    if (DEBUG(MAKE))
731236769Sobrien		fprintf(debug_file, "inspect parent %s%s: flags %x, "
732236769Sobrien			    "type %x, made %d, unmade %d ",
733236769Sobrien			pgn->name, pgn->cohort_num, pgn->flags,
734236769Sobrien			pgn->type, pgn->made, pgn->unmade-1);
735236769Sobrien
736236769Sobrien	    if (!(pgn->flags & REMAKE)) {
737236769Sobrien		/* This parent isn't needed */
738236769Sobrien		if (DEBUG(MAKE))
739236769Sobrien		    fprintf(debug_file, "- not needed\n");
740236769Sobrien		continue;
741236769Sobrien	    }
742236769Sobrien	    if (mtime == 0 && !(cgn->type & OP_WAIT))
743236769Sobrien		pgn->flags |= FORCE;
744236769Sobrien
745236769Sobrien	    /*
746236769Sobrien	     * If the parent has the .MADE attribute, its timestamp got
747236769Sobrien	     * updated to that of its newest child, and its unmake
748236769Sobrien	     * child count got set to zero in Make_ExpandUse().
749236769Sobrien	     * However other things might cause us to build one of its
750236769Sobrien	     * children - and so we mustn't do any processing here when
751236769Sobrien	     * the child build finishes.
752236769Sobrien	     */
753236769Sobrien	    if (pgn->type & OP_MADE) {
754236769Sobrien		if (DEBUG(MAKE))
755236769Sobrien		    fprintf(debug_file, "- .MADE\n");
756236769Sobrien		continue;
757236769Sobrien	    }
758236769Sobrien
759236769Sobrien	    if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
760236769Sobrien		if (cgn->made == MADE)
761236769Sobrien		    pgn->flags |= CHILDMADE;
762236769Sobrien		(void)Make_TimeStamp(pgn, cgn);
763236769Sobrien	    }
764236769Sobrien
765236769Sobrien	    /*
766236769Sobrien	     * A parent must wait for the completion of all instances
767236769Sobrien	     * of a `::' dependency.
768236769Sobrien	     */
769236769Sobrien	    if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
770236769Sobrien		if (DEBUG(MAKE))
771236769Sobrien		    fprintf(debug_file,
772236769Sobrien			    "- centurion made %d, %d unmade cohorts\n",
773236769Sobrien			    centurion->made, centurion->unmade_cohorts);
774236769Sobrien		continue;
775236769Sobrien	    }
776236769Sobrien
777236769Sobrien	    /* One more child of this parent is now made */
778236769Sobrien	    pgn->unmade -= 1;
779236769Sobrien	    if (pgn->unmade < 0) {
780236769Sobrien		if (DEBUG(MAKE)) {
781236769Sobrien		    fprintf(debug_file, "Graph cycles through %s%s\n",
782236769Sobrien			pgn->name, pgn->cohort_num);
783236769Sobrien		    Targ_PrintGraph(2);
784236769Sobrien		}
785236769Sobrien		Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
786236769Sobrien	    }
787236769Sobrien
788236769Sobrien	    /* We must always rescan the parents of .WAIT and .ORDER nodes. */
789236769Sobrien	    if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
790236769Sobrien		    && !(centurion->flags & DONE_ORDER)) {
791236769Sobrien		if (DEBUG(MAKE))
792236769Sobrien		    fprintf(debug_file, "- unmade children\n");
793236769Sobrien		continue;
794236769Sobrien	    }
795236769Sobrien	    if (pgn->made != DEFERRED) {
796236769Sobrien		/*
797236769Sobrien		 * Either this parent is on a different branch of the tree,
798236769Sobrien		 * or it on the RHS of a .WAIT directive
799236769Sobrien		 * or it is already on the toBeMade list.
800236769Sobrien		 */
801236769Sobrien		if (DEBUG(MAKE))
802236769Sobrien		    fprintf(debug_file, "- not deferred\n");
803236769Sobrien		continue;
804236769Sobrien	    }
805236769Sobrien	    if (pgn->order_pred
806236769Sobrien		    && Lst_ForEach(pgn->order_pred, MakeCheckOrder, 0)) {
807236769Sobrien		/* A .ORDER rule stops us building this */
808236769Sobrien		continue;
809236769Sobrien	    }
810236769Sobrien	    if (DEBUG(MAKE)) {
811236769Sobrien		static int two = 2;
812236769Sobrien		fprintf(debug_file, "- %s%s made, schedule %s%s (made %d)\n",
813236769Sobrien			cgn->name, cgn->cohort_num,
814236769Sobrien			pgn->name, pgn->cohort_num, pgn->made);
815236769Sobrien		Targ_PrintNode(pgn, &two);
816236769Sobrien	    }
817236769Sobrien	    /* Ok, we can schedule the parent again */
818236769Sobrien	    pgn->made = REQUESTED;
819236769Sobrien	    (void)Lst_EnQueue(toBeMade, pgn);
820236769Sobrien	}
821236769Sobrien	Lst_Close(parents);
822236769Sobrien    }
823236769Sobrien
824236769Sobrien    /*
825236769Sobrien     * Set the .PREFIX and .IMPSRC variables for all the implied parents
826236769Sobrien     * of this node.
827236769Sobrien     */
828236769Sobrien    if (Lst_Open(cgn->iParents) == SUCCESS) {
829236769Sobrien	char	*cpref = Var_Value(PREFIX, cgn, &p1);
830236769Sobrien
831236769Sobrien	while ((ln = Lst_Next(cgn->iParents)) != NULL) {
832236769Sobrien	    pgn = (GNode *)Lst_Datum(ln);
833236769Sobrien	    if (pgn->flags & REMAKE) {
834236769Sobrien		Var_Set(IMPSRC, cname, pgn, 0);
835236769Sobrien		if (cpref != NULL)
836236769Sobrien		    Var_Set(PREFIX, cpref, pgn, 0);
837236769Sobrien	    }
838236769Sobrien	}
839321964Ssjg	free(p1);
840236769Sobrien	Lst_Close(cgn->iParents);
841236769Sobrien    }
842236769Sobrien}
843236769Sobrien
844236769Sobrien/*-
845236769Sobrien *-----------------------------------------------------------------------
846236769Sobrien * MakeAddAllSrc --
847236769Sobrien *	Add a child's name to the ALLSRC and OODATE variables of the given
848236769Sobrien *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
849236769Sobrien *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
850236769Sobrien *	.EXEC and .USE children are very rarely going to be files, so...
851236769Sobrien *	If the child is a .JOIN node, its ALLSRC is propagated to the parent.
852236769Sobrien *
853236769Sobrien *	A child is added to the OODATE variable if its modification time is
854236769Sobrien *	later than that of its parent, as defined by Make, except if the
855236769Sobrien *	parent is a .JOIN node. In that case, it is only added to the OODATE
856236769Sobrien *	variable if it was actually made (since .JOIN nodes don't have
857236769Sobrien *	modification times, the comparison is rather unfair...)..
858236769Sobrien *
859236769Sobrien * Results:
860236769Sobrien *	Always returns 0
861236769Sobrien *
862236769Sobrien * Side Effects:
863236769Sobrien *	The ALLSRC variable for the given node is extended.
864236769Sobrien *-----------------------------------------------------------------------
865236769Sobrien */
866236769Sobrienstatic int
867237578SobrienMakeUnmark(void *cgnp, void *pgnp MAKE_ATTR_UNUSED)
868236769Sobrien{
869236769Sobrien    GNode	*cgn = (GNode *)cgnp;
870236769Sobrien
871236769Sobrien    cgn->type &= ~OP_MARK;
872236769Sobrien    return (0);
873236769Sobrien}
874236769Sobrien
875236769Sobrien/*
876236769Sobrien * Input:
877236769Sobrien *	cgnp		The child to add
878236769Sobrien *	pgnp		The parent to whose ALLSRC variable it should
879236769Sobrien *			be added
880236769Sobrien *
881236769Sobrien */
882236769Sobrienstatic int
883236769SobrienMakeAddAllSrc(void *cgnp, void *pgnp)
884236769Sobrien{
885236769Sobrien    GNode	*cgn = (GNode *)cgnp;
886236769Sobrien    GNode	*pgn = (GNode *)pgnp;
887236769Sobrien
888236769Sobrien    if (cgn->type & OP_MARK)
889236769Sobrien	return (0);
890236769Sobrien    cgn->type |= OP_MARK;
891236769Sobrien
892236769Sobrien    if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
893236769Sobrien	char *child, *allsrc;
894236769Sobrien	char *p1 = NULL, *p2 = NULL;
895236769Sobrien
896236769Sobrien	if (cgn->type & OP_ARCHV)
897236769Sobrien	    child = Var_Value(MEMBER, cgn, &p1);
898236769Sobrien	else
899236769Sobrien	    child = cgn->path ? cgn->path : cgn->name;
900236769Sobrien	if (cgn->type & OP_JOIN) {
901236769Sobrien	    allsrc = Var_Value(ALLSRC, cgn, &p2);
902236769Sobrien	} else {
903236769Sobrien	    allsrc = child;
904236769Sobrien	}
905236769Sobrien	if (allsrc != NULL)
906236769Sobrien		Var_Append(ALLSRC, allsrc, pgn);
907321964Ssjg	free(p2);
908236769Sobrien	if (pgn->type & OP_JOIN) {
909236769Sobrien	    if (cgn->made == MADE) {
910236769Sobrien		Var_Append(OODATE, child, pgn);
911236769Sobrien	    }
912236769Sobrien	} else if ((pgn->mtime < cgn->mtime) ||
913236769Sobrien		   (cgn->mtime >= now && cgn->made == MADE))
914236769Sobrien	{
915236769Sobrien	    /*
916236769Sobrien	     * It goes in the OODATE variable if the parent is younger than the
917236769Sobrien	     * child or if the child has been modified more recently than
918236769Sobrien	     * the start of the make. This is to keep pmake from getting
919236769Sobrien	     * confused if something else updates the parent after the
920236769Sobrien	     * make starts (shouldn't happen, I know, but sometimes it
921236769Sobrien	     * does). In such a case, if we've updated the kid, the parent
922236769Sobrien	     * is likely to have a modification time later than that of
923236769Sobrien	     * the kid and anything that relies on the OODATE variable will
924236769Sobrien	     * be hosed.
925236769Sobrien	     *
926236769Sobrien	     * XXX: This will cause all made children to go in the OODATE
927236769Sobrien	     * variable, even if they're not touched, if RECHECK isn't defined,
928236769Sobrien	     * since cgn->mtime is set to now in Make_Update. According to
929236769Sobrien	     * some people, this is good...
930236769Sobrien	     */
931236769Sobrien	    Var_Append(OODATE, child, pgn);
932236769Sobrien	}
933321964Ssjg	free(p1);
934236769Sobrien    }
935236769Sobrien    return (0);
936236769Sobrien}
937236769Sobrien
938236769Sobrien/*-
939236769Sobrien *-----------------------------------------------------------------------
940236769Sobrien * Make_DoAllVar --
941236769Sobrien *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
942236769Sobrien *	done separately, rather than while traversing the graph. This is
943236769Sobrien *	because Make defined OODATE to contain all sources whose modification
944236769Sobrien *	times were later than that of the target, *not* those sources that
945236769Sobrien *	were out-of-date. Since in both compatibility and native modes,
946236769Sobrien *	the modification time of the parent isn't found until the child
947236769Sobrien *	has been dealt with, we have to wait until now to fill in the
948236769Sobrien *	variable. As for ALLSRC, the ordering is important and not
949236769Sobrien *	guaranteed when in native mode, so it must be set here, too.
950236769Sobrien *
951236769Sobrien * Results:
952236769Sobrien *	None
953236769Sobrien *
954236769Sobrien * Side Effects:
955236769Sobrien *	The ALLSRC and OODATE variables of the given node is filled in.
956236769Sobrien *	If the node is a .JOIN node, its TARGET variable will be set to
957236769Sobrien * 	match its ALLSRC variable.
958236769Sobrien *-----------------------------------------------------------------------
959236769Sobrien */
960236769Sobrienvoid
961236769SobrienMake_DoAllVar(GNode *gn)
962236769Sobrien{
963236769Sobrien    if (gn->flags & DONE_ALLSRC)
964236769Sobrien	return;
965236769Sobrien
966236769Sobrien    Lst_ForEach(gn->children, MakeUnmark, gn);
967236769Sobrien    Lst_ForEach(gn->children, MakeAddAllSrc, gn);
968236769Sobrien
969236769Sobrien    if (!Var_Exists (OODATE, gn)) {
970236769Sobrien	Var_Set(OODATE, "", gn, 0);
971236769Sobrien    }
972236769Sobrien    if (!Var_Exists (ALLSRC, gn)) {
973236769Sobrien	Var_Set(ALLSRC, "", gn, 0);
974236769Sobrien    }
975236769Sobrien
976236769Sobrien    if (gn->type & OP_JOIN) {
977236769Sobrien	char *p1;
978236769Sobrien	Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn, 0);
979321964Ssjg	free(p1);
980236769Sobrien    }
981236769Sobrien    gn->flags |= DONE_ALLSRC;
982236769Sobrien}
983236769Sobrien
984236769Sobrien/*-
985236769Sobrien *-----------------------------------------------------------------------
986236769Sobrien * MakeStartJobs --
987236769Sobrien *	Start as many jobs as possible.
988236769Sobrien *
989236769Sobrien * Results:
990236769Sobrien *	If the query flag was given to pmake, no job will be started,
991236769Sobrien *	but as soon as an out-of-date target is found, this function
992236769Sobrien *	returns TRUE. At all other times, this function returns FALSE.
993236769Sobrien *
994236769Sobrien * Side Effects:
995236769Sobrien *	Nodes are removed from the toBeMade queue and job table slots
996236769Sobrien *	are filled.
997236769Sobrien *
998236769Sobrien *-----------------------------------------------------------------------
999236769Sobrien */
1000236769Sobrien
1001236769Sobrienstatic int
1002237578SobrienMakeCheckOrder(void *v_bn, void *ignore MAKE_ATTR_UNUSED)
1003236769Sobrien{
1004236769Sobrien    GNode *bn = v_bn;
1005236769Sobrien
1006236769Sobrien    if (bn->made >= MADE || !(bn->flags & REMAKE))
1007236769Sobrien	return 0;
1008236769Sobrien    if (DEBUG(MAKE))
1009236769Sobrien	fprintf(debug_file, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
1010236769Sobrien		bn->name, bn->cohort_num);
1011236769Sobrien    return 1;
1012236769Sobrien}
1013236769Sobrien
1014236769Sobrienstatic int
1015236769SobrienMakeBuildChild(void *v_cn, void *toBeMade_next)
1016236769Sobrien{
1017236769Sobrien    GNode *cn = v_cn;
1018236769Sobrien
1019236769Sobrien    if (DEBUG(MAKE))
1020236769Sobrien	fprintf(debug_file, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
1021236769Sobrien	    cn->name, cn->cohort_num, cn->made, cn->type);
1022236769Sobrien    if (cn->made > DEFERRED)
1023236769Sobrien	return 0;
1024236769Sobrien
1025236769Sobrien    /* If this node is on the RHS of a .ORDER, check LHSs. */
1026236769Sobrien    if (cn->order_pred && Lst_ForEach(cn->order_pred, MakeCheckOrder, 0)) {
1027236769Sobrien	/* Can't build this (or anything else in this child list) yet */
1028236769Sobrien	cn->made = DEFERRED;
1029243115Ssjg	return 0;			/* but keep looking */
1030236769Sobrien    }
1031236769Sobrien
1032236769Sobrien    if (DEBUG(MAKE))
1033236769Sobrien	fprintf(debug_file, "MakeBuildChild: schedule %s%s\n",
1034236769Sobrien		cn->name, cn->cohort_num);
1035236769Sobrien
1036236769Sobrien    cn->made = REQUESTED;
1037236769Sobrien    if (toBeMade_next == NULL)
1038236769Sobrien	Lst_AtEnd(toBeMade, cn);
1039236769Sobrien    else
1040236769Sobrien	Lst_InsertBefore(toBeMade, toBeMade_next, cn);
1041236769Sobrien
1042236769Sobrien    if (cn->unmade_cohorts != 0)
1043236769Sobrien	Lst_ForEach(cn->cohorts, MakeBuildChild, toBeMade_next);
1044236769Sobrien
1045236769Sobrien    /*
1046236769Sobrien     * If this node is a .WAIT node with unmade chlidren
1047236769Sobrien     * then don't add the next sibling.
1048236769Sobrien     */
1049236769Sobrien    return cn->type & OP_WAIT && cn->unmade > 0;
1050236769Sobrien}
1051236769Sobrien
1052243115Ssjg/* When a .ORDER LHS node completes we do this on each RHS */
1053236769Sobrienstatic int
1054236769SobrienMakeBuildParent(void *v_pn, void *toBeMade_next)
1055236769Sobrien{
1056236769Sobrien    GNode *pn = v_pn;
1057236769Sobrien
1058236769Sobrien    if (pn->made != DEFERRED)
1059236769Sobrien	return 0;
1060236769Sobrien
1061236769Sobrien    if (MakeBuildChild(pn, toBeMade_next) == 0) {
1062236769Sobrien	/* Mark so that when this node is built we reschedule its parents */
1063236769Sobrien	pn->flags |= DONE_ORDER;
1064236769Sobrien    }
1065236769Sobrien
1066236769Sobrien    return 0;
1067236769Sobrien}
1068236769Sobrien
1069236769Sobrienstatic Boolean
1070236769SobrienMakeStartJobs(void)
1071236769Sobrien{
1072236769Sobrien    GNode	*gn;
1073236769Sobrien    int		have_token = 0;
1074236769Sobrien
1075236769Sobrien    while (!Lst_IsEmpty (toBeMade)) {
1076236769Sobrien	/* Get token now to avoid cycling job-list when we only have 1 token */
1077236769Sobrien	if (!have_token && !Job_TokenWithdraw())
1078236769Sobrien	    break;
1079236769Sobrien	have_token = 1;
1080236769Sobrien
1081236769Sobrien	gn = (GNode *)Lst_DeQueue(toBeMade);
1082236769Sobrien	if (DEBUG(MAKE))
1083236769Sobrien	    fprintf(debug_file, "Examining %s%s...\n",
1084236769Sobrien		    gn->name, gn->cohort_num);
1085236769Sobrien
1086236769Sobrien	if (gn->made != REQUESTED) {
1087236769Sobrien	    if (DEBUG(MAKE))
1088236769Sobrien		fprintf(debug_file, "state %d\n", gn->made);
1089236769Sobrien
1090236769Sobrien	    make_abort(gn, __LINE__);
1091236769Sobrien	}
1092236769Sobrien
1093236769Sobrien	if (gn->checked == checked) {
1094236769Sobrien	    /* We've already looked at this node since a job finished... */
1095236769Sobrien	    if (DEBUG(MAKE))
1096236769Sobrien		fprintf(debug_file, "already checked %s%s\n",
1097236769Sobrien			gn->name, gn->cohort_num);
1098236769Sobrien	    gn->made = DEFERRED;
1099236769Sobrien	    continue;
1100236769Sobrien	}
1101236769Sobrien	gn->checked = checked;
1102236769Sobrien
1103236769Sobrien	if (gn->unmade != 0) {
1104236769Sobrien	    /*
1105236769Sobrien	     * We can't build this yet, add all unmade children to toBeMade,
1106236769Sobrien	     * just before the current first element.
1107236769Sobrien	     */
1108236769Sobrien	    gn->made = DEFERRED;
1109236769Sobrien	    Lst_ForEach(gn->children, MakeBuildChild, Lst_First(toBeMade));
1110236769Sobrien	    /* and drop this node on the floor */
1111236769Sobrien	    if (DEBUG(MAKE))
1112236769Sobrien		fprintf(debug_file, "dropped %s%s\n", gn->name, gn->cohort_num);
1113236769Sobrien	    continue;
1114236769Sobrien	}
1115236769Sobrien
1116236769Sobrien	gn->made = BEINGMADE;
1117236769Sobrien	if (Make_OODate(gn)) {
1118236769Sobrien	    if (DEBUG(MAKE)) {
1119236769Sobrien		fprintf(debug_file, "out-of-date\n");
1120236769Sobrien	    }
1121236769Sobrien	    if (queryFlag) {
1122236769Sobrien		return (TRUE);
1123236769Sobrien	    }
1124236769Sobrien	    Make_DoAllVar(gn);
1125236769Sobrien	    Job_Make(gn);
1126236769Sobrien	    have_token = 0;
1127236769Sobrien	} else {
1128236769Sobrien	    if (DEBUG(MAKE)) {
1129236769Sobrien		fprintf(debug_file, "up-to-date\n");
1130236769Sobrien	    }
1131236769Sobrien	    gn->made = UPTODATE;
1132236769Sobrien	    if (gn->type & OP_JOIN) {
1133236769Sobrien		/*
1134236769Sobrien		 * Even for an up-to-date .JOIN node, we need it to have its
1135236769Sobrien		 * context variables so references to it get the correct
1136236769Sobrien		 * value for .TARGET when building up the context variables
1137236769Sobrien		 * of its parent(s)...
1138236769Sobrien		 */
1139236769Sobrien		Make_DoAllVar(gn);
1140236769Sobrien	    }
1141236769Sobrien	    Make_Update(gn);
1142236769Sobrien	}
1143236769Sobrien    }
1144236769Sobrien
1145236769Sobrien    if (have_token)
1146236769Sobrien	Job_TokenReturn();
1147236769Sobrien
1148236769Sobrien    return (FALSE);
1149236769Sobrien}
1150236769Sobrien
1151236769Sobrien/*-
1152236769Sobrien *-----------------------------------------------------------------------
1153236769Sobrien * MakePrintStatus --
1154236769Sobrien *	Print the status of a top-level node, viz. it being up-to-date
1155236769Sobrien *	already or not created due to an error in a lower level.
1156236769Sobrien *	Callback function for Make_Run via Lst_ForEach.
1157236769Sobrien *
1158236769Sobrien * Input:
1159236769Sobrien *	gnp		Node to examine
1160236769Sobrien *	cyclep		True if gn->unmade being non-zero implies a
1161236769Sobrien *			cycle in the graph, not an error in an
1162236769Sobrien *			inferior.
1163236769Sobrien *
1164236769Sobrien * Results:
1165236769Sobrien *	Always returns 0.
1166236769Sobrien *
1167236769Sobrien * Side Effects:
1168236769Sobrien *	A message may be printed.
1169236769Sobrien *
1170236769Sobrien *-----------------------------------------------------------------------
1171236769Sobrien */
1172236769Sobrienstatic int
1173236769SobrienMakePrintStatusOrder(void *ognp, void *gnp)
1174236769Sobrien{
1175236769Sobrien    GNode *ogn = ognp;
1176236769Sobrien    GNode *gn = gnp;
1177236769Sobrien
1178236769Sobrien    if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
1179236769Sobrien	/* not waiting for this one */
1180236769Sobrien	return 0;
1181236769Sobrien
1182236769Sobrien    printf("    `%s%s' has .ORDER dependency against %s%s "
1183236769Sobrien		"(made %d, flags %x, type %x)\n",
1184236769Sobrien	    gn->name, gn->cohort_num,
1185236769Sobrien	    ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
1186236769Sobrien    if (DEBUG(MAKE) && debug_file != stdout)
1187236769Sobrien	fprintf(debug_file, "    `%s%s' has .ORDER dependency against %s%s "
1188236769Sobrien		    "(made %d, flags %x, type %x)\n",
1189236769Sobrien		gn->name, gn->cohort_num,
1190236769Sobrien		ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
1191236769Sobrien    return 0;
1192236769Sobrien}
1193236769Sobrien
1194236769Sobrienstatic int
1195236769SobrienMakePrintStatus(void *gnp, void *v_errors)
1196236769Sobrien{
1197236769Sobrien    GNode   	*gn = (GNode *)gnp;
1198236769Sobrien    int 	*errors = v_errors;
1199236769Sobrien
1200236769Sobrien    if (gn->flags & DONECYCLE)
1201236769Sobrien	/* We've completely processed this node before, don't do it again. */
1202236769Sobrien	return 0;
1203236769Sobrien
1204236769Sobrien    if (gn->unmade == 0) {
1205236769Sobrien	gn->flags |= DONECYCLE;
1206236769Sobrien	switch (gn->made) {
1207236769Sobrien	case UPTODATE:
1208236769Sobrien	    printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
1209236769Sobrien	    break;
1210236769Sobrien	case MADE:
1211236769Sobrien	    break;
1212236769Sobrien	case UNMADE:
1213236769Sobrien	case DEFERRED:
1214236769Sobrien	case REQUESTED:
1215236769Sobrien	case BEINGMADE:
1216236769Sobrien	    (*errors)++;
1217236769Sobrien	    printf("`%s%s' was not built (made %d, flags %x, type %x)!\n",
1218236769Sobrien		    gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
1219236769Sobrien	    if (DEBUG(MAKE) && debug_file != stdout)
1220236769Sobrien		fprintf(debug_file,
1221236769Sobrien			"`%s%s' was not built (made %d, flags %x, type %x)!\n",
1222236769Sobrien			gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
1223236769Sobrien	    /* Most likely problem is actually caused by .ORDER */
1224236769Sobrien	    Lst_ForEach(gn->order_pred, MakePrintStatusOrder, gn);
1225236769Sobrien	    break;
1226236769Sobrien	default:
1227236769Sobrien	    /* Errors - already counted */
1228236769Sobrien	    printf("`%s%s' not remade because of errors.\n",
1229236769Sobrien		    gn->name, gn->cohort_num);
1230236769Sobrien	    if (DEBUG(MAKE) && debug_file != stdout)
1231236769Sobrien		fprintf(debug_file, "`%s%s' not remade because of errors.\n",
1232236769Sobrien			gn->name, gn->cohort_num);
1233236769Sobrien	    break;
1234236769Sobrien	}
1235236769Sobrien	return 0;
1236236769Sobrien    }
1237236769Sobrien
1238236769Sobrien    if (DEBUG(MAKE))
1239236769Sobrien	fprintf(debug_file, "MakePrintStatus: %s%s has %d unmade children\n",
1240236769Sobrien		gn->name, gn->cohort_num, gn->unmade);
1241236769Sobrien    /*
1242236769Sobrien     * If printing cycles and came to one that has unmade children,
1243236769Sobrien     * print out the cycle by recursing on its children.
1244236769Sobrien     */
1245236769Sobrien    if (!(gn->flags & CYCLE)) {
1246236769Sobrien	/* Fist time we've seen this node, check all children */
1247236769Sobrien	gn->flags |= CYCLE;
1248236769Sobrien	Lst_ForEach(gn->children, MakePrintStatus, errors);
1249236769Sobrien	/* Mark that this node needn't be processed again */
1250236769Sobrien	gn->flags |= DONECYCLE;
1251236769Sobrien	return 0;
1252236769Sobrien    }
1253236769Sobrien
1254236769Sobrien    /* Only output the error once per node */
1255236769Sobrien    gn->flags |= DONECYCLE;
1256236769Sobrien    Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
1257236769Sobrien    if ((*errors)++ > 100)
1258236769Sobrien	/* Abandon the whole error report */
1259236769Sobrien	return 1;
1260236769Sobrien
1261236769Sobrien    /* Reporting for our children will give the rest of the loop */
1262236769Sobrien    Lst_ForEach(gn->children, MakePrintStatus, errors);
1263236769Sobrien    return 0;
1264236769Sobrien}
1265236769Sobrien
1266236769Sobrien
1267236769Sobrien/*-
1268236769Sobrien *-----------------------------------------------------------------------
1269236769Sobrien * Make_ExpandUse --
1270236769Sobrien *	Expand .USE nodes and create a new targets list
1271236769Sobrien *
1272236769Sobrien * Input:
1273236769Sobrien *	targs		the initial list of targets
1274236769Sobrien *
1275236769Sobrien * Side Effects:
1276236769Sobrien *-----------------------------------------------------------------------
1277236769Sobrien */
1278236769Sobrienvoid
1279236769SobrienMake_ExpandUse(Lst targs)
1280236769Sobrien{
1281236769Sobrien    GNode  *gn;		/* a temporary pointer */
1282236769Sobrien    Lst    examine; 	/* List of targets to examine */
1283236769Sobrien
1284236769Sobrien    examine = Lst_Duplicate(targs, NULL);
1285236769Sobrien
1286236769Sobrien    /*
1287236769Sobrien     * Make an initial downward pass over the graph, marking nodes to be made
1288236769Sobrien     * as we go down. We call Suff_FindDeps to find where a node is and
1289236769Sobrien     * to get some children for it if it has none and also has no commands.
1290236769Sobrien     * If the node is a leaf, we stick it on the toBeMade queue to
1291236769Sobrien     * be looked at in a minute, otherwise we add its children to our queue
1292236769Sobrien     * and go on about our business.
1293236769Sobrien     */
1294236769Sobrien    while (!Lst_IsEmpty (examine)) {
1295236769Sobrien	gn = (GNode *)Lst_DeQueue(examine);
1296236769Sobrien
1297236769Sobrien	if (gn->flags & REMAKE)
1298236769Sobrien	    /* We've looked at this one already */
1299236769Sobrien	    continue;
1300236769Sobrien	gn->flags |= REMAKE;
1301236769Sobrien	if (DEBUG(MAKE))
1302236769Sobrien	    fprintf(debug_file, "Make_ExpandUse: examine %s%s\n",
1303236769Sobrien		    gn->name, gn->cohort_num);
1304236769Sobrien
1305236769Sobrien	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) {
1306236769Sobrien	    /* Append all the 'cohorts' to the list of things to examine */
1307236769Sobrien	    Lst new;
1308236769Sobrien	    new = Lst_Duplicate(gn->cohorts, NULL);
1309236769Sobrien	    Lst_Concat(new, examine, LST_CONCLINK);
1310236769Sobrien	    examine = new;
1311236769Sobrien	}
1312236769Sobrien
1313236769Sobrien	/*
1314236769Sobrien	 * Apply any .USE rules before looking for implicit dependencies
1315236769Sobrien	 * to make sure everything has commands that should...
1316236769Sobrien	 * Make sure that the TARGET is set, so that we can make
1317236769Sobrien	 * expansions.
1318236769Sobrien	 */
1319236769Sobrien	if (gn->type & OP_ARCHV) {
1320236769Sobrien	    char *eoa, *eon;
1321236769Sobrien	    eoa = strchr(gn->name, '(');
1322236769Sobrien	    eon = strchr(gn->name, ')');
1323236769Sobrien	    if (eoa == NULL || eon == NULL)
1324236769Sobrien		continue;
1325236769Sobrien	    *eoa = '\0';
1326236769Sobrien	    *eon = '\0';
1327236769Sobrien	    Var_Set(MEMBER, eoa + 1, gn, 0);
1328236769Sobrien	    Var_Set(ARCHIVE, gn->name, gn, 0);
1329236769Sobrien	    *eoa = '(';
1330236769Sobrien	    *eon = ')';
1331236769Sobrien	}
1332236769Sobrien
1333236769Sobrien	(void)Dir_MTime(gn, 0);
1334236769Sobrien	Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
1335236769Sobrien	Lst_ForEach(gn->children, MakeUnmark, gn);
1336236769Sobrien	Lst_ForEach(gn->children, MakeHandleUse, gn);
1337236769Sobrien
1338236769Sobrien	if ((gn->type & OP_MADE) == 0)
1339236769Sobrien	    Suff_FindDeps(gn);
1340236769Sobrien	else {
1341236769Sobrien	    /* Pretend we made all this node's children */
1342236769Sobrien	    Lst_ForEach(gn->children, MakeFindChild, gn);
1343236769Sobrien	    if (gn->unmade != 0)
1344236769Sobrien		    printf("Warning: %s%s still has %d unmade children\n",
1345236769Sobrien			    gn->name, gn->cohort_num, gn->unmade);
1346236769Sobrien	}
1347236769Sobrien
1348236769Sobrien	if (gn->unmade != 0)
1349236769Sobrien	    Lst_ForEach(gn->children, MakeAddChild, examine);
1350236769Sobrien    }
1351236769Sobrien
1352236769Sobrien    Lst_Destroy(examine, NULL);
1353236769Sobrien}
1354236769Sobrien
1355236769Sobrien/*-
1356236769Sobrien *-----------------------------------------------------------------------
1357236769Sobrien * Make_ProcessWait --
1358236769Sobrien *	Convert .WAIT nodes into dependencies
1359236769Sobrien *
1360236769Sobrien * Input:
1361236769Sobrien *	targs		the initial list of targets
1362236769Sobrien *
1363236769Sobrien *-----------------------------------------------------------------------
1364236769Sobrien */
1365236769Sobrien
1366236769Sobrienstatic int
1367236769Sobrienlink_parent(void *cnp, void *pnp)
1368236769Sobrien{
1369236769Sobrien    GNode *cn = cnp;
1370236769Sobrien    GNode *pn = pnp;
1371236769Sobrien
1372236769Sobrien    Lst_AtEnd(pn->children, cn);
1373236769Sobrien    Lst_AtEnd(cn->parents, pn);
1374236769Sobrien    pn->unmade++;
1375236769Sobrien    return 0;
1376236769Sobrien}
1377236769Sobrien
1378236769Sobrienstatic int
1379236769Sobrienadd_wait_dep(void *v_cn, void *v_wn)
1380236769Sobrien{
1381236769Sobrien    GNode *cn = v_cn;
1382236769Sobrien    GNode *wn = v_wn;
1383236769Sobrien
1384236769Sobrien    if (cn == wn)
1385236769Sobrien	return 1;
1386236769Sobrien
1387236769Sobrien    if (cn == NULL || wn == NULL) {
1388236769Sobrien	printf("bad wait dep %p %p\n", cn, wn);
1389236769Sobrien	exit(4);
1390236769Sobrien    }
1391236769Sobrien    if (DEBUG(MAKE))
1392236769Sobrien	 fprintf(debug_file, ".WAIT: add dependency %s%s -> %s\n",
1393236769Sobrien		cn->name, cn->cohort_num, wn->name);
1394236769Sobrien
1395236769Sobrien    Lst_AtEnd(wn->children, cn);
1396236769Sobrien    wn->unmade++;
1397236769Sobrien    Lst_AtEnd(cn->parents, wn);
1398236769Sobrien    return 0;
1399236769Sobrien}
1400236769Sobrien
1401236769Sobrienstatic void
1402236769SobrienMake_ProcessWait(Lst targs)
1403236769Sobrien{
1404236769Sobrien    GNode  *pgn;	/* 'parent' node we are examining */
1405236769Sobrien    GNode  *cgn;	/* Each child in turn */
1406236769Sobrien    LstNode owln;	/* Previous .WAIT node */
1407236769Sobrien    Lst    examine; 	/* List of targets to examine */
1408236769Sobrien    LstNode ln;
1409236769Sobrien
1410236769Sobrien    /*
1411236769Sobrien     * We need all the nodes to have a common parent in order for the
1412236769Sobrien     * .WAIT and .ORDER scheduling to work.
1413236769Sobrien     * Perhaps this should be done earlier...
1414236769Sobrien     */
1415236769Sobrien
1416236769Sobrien    pgn = Targ_NewGN(".MAIN");
1417236769Sobrien    pgn->flags = REMAKE;
1418236769Sobrien    pgn->type = OP_PHONY | OP_DEPENDS;
1419236769Sobrien    /* Get it displayed in the diag dumps */
1420236769Sobrien    Lst_AtFront(Targ_List(), pgn);
1421236769Sobrien
1422236769Sobrien    Lst_ForEach(targs, link_parent, pgn);
1423236769Sobrien
1424236769Sobrien    /* Start building with the 'dummy' .MAIN' node */
1425236769Sobrien    MakeBuildChild(pgn, NULL);
1426236769Sobrien
1427236769Sobrien    examine = Lst_Init(FALSE);
1428236769Sobrien    Lst_AtEnd(examine, pgn);
1429236769Sobrien
1430236769Sobrien    while (!Lst_IsEmpty (examine)) {
1431236769Sobrien	pgn = Lst_DeQueue(examine);
1432236769Sobrien
1433236769Sobrien	/* We only want to process each child-list once */
1434236769Sobrien	if (pgn->flags & DONE_WAIT)
1435236769Sobrien	    continue;
1436236769Sobrien	pgn->flags |= DONE_WAIT;
1437236769Sobrien	if (DEBUG(MAKE))
1438236769Sobrien	    fprintf(debug_file, "Make_ProcessWait: examine %s\n", pgn->name);
1439236769Sobrien
1440236769Sobrien	if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts)) {
1441236769Sobrien	    /* Append all the 'cohorts' to the list of things to examine */
1442236769Sobrien	    Lst new;
1443236769Sobrien	    new = Lst_Duplicate(pgn->cohorts, NULL);
1444236769Sobrien	    Lst_Concat(new, examine, LST_CONCLINK);
1445236769Sobrien	    examine = new;
1446236769Sobrien	}
1447236769Sobrien
1448236769Sobrien	owln = Lst_First(pgn->children);
1449236769Sobrien	Lst_Open(pgn->children);
1450236769Sobrien	for (; (ln = Lst_Next(pgn->children)) != NULL; ) {
1451236769Sobrien	    cgn = Lst_Datum(ln);
1452236769Sobrien	    if (cgn->type & OP_WAIT) {
1453236769Sobrien		/* Make the .WAIT node depend on the previous children */
1454236769Sobrien		Lst_ForEachFrom(pgn->children, owln, add_wait_dep, cgn);
1455236769Sobrien		owln = ln;
1456236769Sobrien	    } else {
1457236769Sobrien		Lst_AtEnd(examine, cgn);
1458236769Sobrien	    }
1459236769Sobrien	}
1460236769Sobrien	Lst_Close(pgn->children);
1461236769Sobrien    }
1462236769Sobrien
1463236769Sobrien    Lst_Destroy(examine, NULL);
1464236769Sobrien}
1465236769Sobrien
1466236769Sobrien/*-
1467236769Sobrien *-----------------------------------------------------------------------
1468236769Sobrien * Make_Run --
1469236769Sobrien *	Initialize the nodes to remake and the list of nodes which are
1470236769Sobrien *	ready to be made by doing a breadth-first traversal of the graph
1471236769Sobrien *	starting from the nodes in the given list. Once this traversal
1472236769Sobrien *	is finished, all the 'leaves' of the graph are in the toBeMade
1473236769Sobrien *	queue.
1474236769Sobrien *	Using this queue and the Job module, work back up the graph,
1475236769Sobrien *	calling on MakeStartJobs to keep the job table as full as
1476236769Sobrien *	possible.
1477236769Sobrien *
1478236769Sobrien * Input:
1479236769Sobrien *	targs		the initial list of targets
1480236769Sobrien *
1481236769Sobrien * Results:
1482236769Sobrien *	TRUE if work was done. FALSE otherwise.
1483236769Sobrien *
1484236769Sobrien * Side Effects:
1485236769Sobrien *	The make field of all nodes involved in the creation of the given
1486236769Sobrien *	targets is set to 1. The toBeMade list is set to contain all the
1487236769Sobrien *	'leaves' of these subgraphs.
1488236769Sobrien *-----------------------------------------------------------------------
1489236769Sobrien */
1490236769SobrienBoolean
1491236769SobrienMake_Run(Lst targs)
1492236769Sobrien{
1493236769Sobrien    int	    	    errors; 	/* Number of errors the Job module reports */
1494236769Sobrien
1495236769Sobrien    /* Start trying to make the current targets... */
1496236769Sobrien    toBeMade = Lst_Init(FALSE);
1497236769Sobrien
1498236769Sobrien    Make_ExpandUse(targs);
1499236769Sobrien    Make_ProcessWait(targs);
1500236769Sobrien
1501236769Sobrien    if (DEBUG(MAKE)) {
1502236769Sobrien	 fprintf(debug_file, "#***# full graph\n");
1503236769Sobrien	 Targ_PrintGraph(1);
1504236769Sobrien    }
1505236769Sobrien
1506236769Sobrien    if (queryFlag) {
1507236769Sobrien	/*
1508236769Sobrien	 * We wouldn't do any work unless we could start some jobs in the
1509236769Sobrien	 * next loop... (we won't actually start any, of course, this is just
1510236769Sobrien	 * to see if any of the targets was out of date)
1511236769Sobrien	 */
1512236769Sobrien	return (MakeStartJobs());
1513236769Sobrien    }
1514236769Sobrien    /*
1515236769Sobrien     * Initialization. At the moment, no jobs are running and until some
1516236769Sobrien     * get started, nothing will happen since the remaining upward
1517236769Sobrien     * traversal of the graph is performed by the routines in job.c upon
1518236769Sobrien     * the finishing of a job. So we fill the Job table as much as we can
1519236769Sobrien     * before going into our loop.
1520236769Sobrien     */
1521236769Sobrien    (void)MakeStartJobs();
1522236769Sobrien
1523236769Sobrien    /*
1524236769Sobrien     * Main Loop: The idea here is that the ending of jobs will take
1525236769Sobrien     * care of the maintenance of data structures and the waiting for output
1526236769Sobrien     * will cause us to be idle most of the time while our children run as
1527236769Sobrien     * much as possible. Because the job table is kept as full as possible,
1528236769Sobrien     * the only time when it will be empty is when all the jobs which need
1529236769Sobrien     * running have been run, so that is the end condition of this loop.
1530236769Sobrien     * Note that the Job module will exit if there were any errors unless the
1531236769Sobrien     * keepgoing flag was given.
1532236769Sobrien     */
1533236769Sobrien    while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
1534236769Sobrien	Job_CatchOutput();
1535236769Sobrien	(void)MakeStartJobs();
1536236769Sobrien    }
1537236769Sobrien
1538236769Sobrien    errors = Job_Finish();
1539236769Sobrien
1540236769Sobrien    /*
1541236769Sobrien     * Print the final status of each target. E.g. if it wasn't made
1542236769Sobrien     * because some inferior reported an error.
1543236769Sobrien     */
1544236769Sobrien    if (DEBUG(MAKE))
1545236769Sobrien	 fprintf(debug_file, "done: errors %d\n", errors);
1546236769Sobrien    if (errors == 0) {
1547236769Sobrien	Lst_ForEach(targs, MakePrintStatus, &errors);
1548236769Sobrien	if (DEBUG(MAKE)) {
1549236769Sobrien	    fprintf(debug_file, "done: errors %d\n", errors);
1550236769Sobrien	    if (errors)
1551236769Sobrien		Targ_PrintGraph(4);
1552236769Sobrien	}
1553236769Sobrien    }
1554236769Sobrien    return errors != 0;
1555236769Sobrien}
1556