make.c revision 103545
1191739Sobrien/*
2191739Sobrien * Copyright (c) 1988, 1989, 1990, 1993
3191739Sobrien *	The Regents of the University of California.  All rights reserved.
4191739Sobrien * Copyright (c) 1989 by Berkeley Softworks
5191739Sobrien * All rights reserved.
6191739Sobrien *
7191739Sobrien * This code is derived from software contributed to Berkeley by
8191739Sobrien * Adam de Boor.
9191739Sobrien *
10191739Sobrien * Redistribution and use in source and binary forms, with or without
11191739Sobrien * modification, are permitted provided that the following conditions
12191739Sobrien * are met:
13191739Sobrien * 1. Redistributions of source code must retain the above copyright
14191739Sobrien *    notice, this list of conditions and the following disclaimer.
15191739Sobrien * 2. Redistributions in binary form must reproduce the above copyright
16191739Sobrien *    notice, this list of conditions and the following disclaimer in the
17191739Sobrien *    documentation and/or other materials provided with the distribution.
18191739Sobrien * 3. All advertising materials mentioning features or use of this software
19191739Sobrien *    must display the following acknowledgement:
20191739Sobrien *	This product includes software developed by the University of
21191739Sobrien *	California, Berkeley and its contributors.
22191739Sobrien * 4. Neither the name of the University nor the names of its contributors
23191739Sobrien *    may be used to endorse or promote products derived from this software
24191739Sobrien *    without specific prior written permission.
25191739Sobrien *
26191739Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27226048Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28226048Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29226048Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30226048Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31226048Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32226048Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33191739Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34191739Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35191739Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36191739Sobrien * SUCH DAMAGE.
37191739Sobrien *
38226048Sobrien * @(#)make.c	8.1 (Berkeley) 6/6/93
39226048Sobrien */
40226048Sobrien
41226048Sobrien#include <sys/cdefs.h>
42226048Sobrien__FBSDID("$FreeBSD: head/usr.bin/make/make.c 103545 2002-09-18 16:13:03Z jmallett $");
43226048Sobrien
44226048Sobrien/*-
45226048Sobrien * make.c --
46226048Sobrien *	The functions which perform the examination of targets and
47226048Sobrien *	their suitability for creation
48191739Sobrien *
49191739Sobrien * Interface:
50191739Sobrien *	Make_Run 	    	Initialize things for the module and recreate
51191739Sobrien *	    	  	    	whatever needs recreating. Returns TRUE if
52191739Sobrien *	    	    	    	work was (or would have been) done and FALSE
53191739Sobrien *	    	  	    	otherwise.
54234250Sobrien *
55234250Sobrien *	Make_Update	    	Update all parents of a given child. Performs
56191739Sobrien *	    	  	    	various bookkeeping chores like the updating
57191739Sobrien *	    	  	    	of the cmtime field of the parent, filling
58191739Sobrien *	    	  	    	of the IMPSRC context variable, etc. It will
59234250Sobrien *	    	  	    	place the parent on the toBeMade queue if it
60191739Sobrien *	    	  	    	should be.
61234250Sobrien *
62234250Sobrien *	Make_TimeStamp	    	Function to set the parent's cmtime field
63234250Sobrien *	    	  	    	based on a child's modification time.
64234250Sobrien *
65234250Sobrien *	Make_DoAllVar	    	Set up the various local variables for a
66234250Sobrien *	    	  	    	target, including the .ALLSRC variable, making
67234250Sobrien *	    	  	    	sure that any variable that needs to exist
68234250Sobrien *	    	  	    	at the very least has the empty value.
69234250Sobrien *
70234250Sobrien *	Make_OODate 	    	Determine if a target is out-of-date.
71234250Sobrien *
72234250Sobrien *	Make_HandleUse		See if a child is a .USE node for a parent
73234250Sobrien *				and perform the .USE actions if so.
74234250Sobrien */
75234250Sobrien
76234250Sobrien#include    "make.h"
77191739Sobrien#include    "hash.h"
78191739Sobrien#include    "dir.h"
79234250Sobrien#include    "job.h"
80191739Sobrien
81234250Sobrienstatic Lst     	toBeMade;	/* The current fringe of the graph. These
82191739Sobrien				 * are nodes which await examination by
83191739Sobrien				 * MakeOODate. It is added to by
84234250Sobrien				 * Make_Update and subtracted from by
85191739Sobrien				 * MakeStartJobs */
86191739Sobrienstatic int  	numNodes;   	/* Number of nodes to be processed. If this
87234250Sobrien				 * is non-zero when Job_Empty() returns
88191739Sobrien				 * TRUE, there's a cycle in the graph */
89191739Sobrien
90191739Sobrienstatic int MakeAddChild(void *, void *);
91191739Sobrienstatic int MakeAddAllSrc(void *, void *);
92234250Sobrienstatic int MakeTimeStamp(void *, void *);
93234250Sobrienstatic int MakeHandleUse(void *, void *);
94234250Sobrienstatic Boolean MakeStartJobs(void);
95191739Sobrienstatic int MakePrintStatus(void *, void *);
96234250Sobrien/*-
97234250Sobrien *-----------------------------------------------------------------------
98234250Sobrien * Make_TimeStamp --
99234250Sobrien *	Set the cmtime field of a parent node based on the mtime stamp in its
100234250Sobrien *	child. Called from MakeOODate via Lst_ForEach.
101234250Sobrien *
102191739Sobrien * Results:
103191739Sobrien *	Always returns 0.
104234250Sobrien *
105234250Sobrien * Side Effects:
106234250Sobrien *	The cmtime of the parent node will be changed if the mtime
107234250Sobrien *	field of the child is greater than it.
108234250Sobrien *-----------------------------------------------------------------------
109234250Sobrien */
110234250Sobrienint
111234250SobrienMake_TimeStamp (pgn, cgn)
112234250Sobrien    GNode *pgn;	/* the current parent */
113234250Sobrien    GNode *cgn;	/* the child we've just examined */
114191739Sobrien{
115191739Sobrien    if (cgn->mtime > pgn->cmtime) {
116191739Sobrien	pgn->cmtime = cgn->mtime;
117191739Sobrien    }
118191739Sobrien    return (0);
119234250Sobrien}
120234250Sobrien
121191739Sobrienstatic int
122191739SobrienMakeTimeStamp (pgn, cgn)
123191739Sobrien    void * pgn;	/* the current parent */
124234250Sobrien    void * cgn;	/* the child we've just examined */
125234250Sobrien{
126191739Sobrien    return Make_TimeStamp((GNode *) pgn, (GNode *) cgn);
127191739Sobrien}
128191739Sobrien
129234250Sobrien/*-
130234250Sobrien *-----------------------------------------------------------------------
131234250Sobrien * Make_OODate --
132191739Sobrien *	See if a given node is out of date with respect to its sources.
133191739Sobrien *	Used by Make_Run when deciding which nodes to place on the
134191739Sobrien *	toBeMade queue initially and by Make_Update to screen out USE and
135234250Sobrien *	EXEC nodes. In the latter case, however, any other sort of node
136234250Sobrien *	must be considered out-of-date since at least one of its children
137234250Sobrien *	will have been recreated.
138234250Sobrien *
139191739Sobrien * Results:
140191739Sobrien *	TRUE if the node is out of date. FALSE otherwise.
141191739Sobrien *
142234250Sobrien * Side Effects:
143234250Sobrien *	The mtime field of the node and the cmtime field of its parents
144234250Sobrien *	will/may be changed.
145234250Sobrien *-----------------------------------------------------------------------
146234250Sobrien */
147234250SobrienBoolean
148191739SobrienMake_OODate (gn)
149191739Sobrien    GNode *gn;		      /* the node to check */
150191739Sobrien{
151191739Sobrien    Boolean         oodate;
152191739Sobrien
153234250Sobrien    /*
154234250Sobrien     * Certain types of targets needn't even be sought as their datedness
155191739Sobrien     * doesn't depend on their modification time...
156191739Sobrien     */
157191739Sobrien    if ((gn->type & (OP_JOIN|OP_USE|OP_EXEC)) == 0) {
158234250Sobrien	(void) Dir_MTime (gn);
159234250Sobrien	if (gn->mtime != 0) {
160191739Sobrien	    DEBUGF(MAKE, ("modified %s...", Targ_FmtTime(gn->mtime)));
161191739Sobrien	} else {
162191739Sobrien	    DEBUGF(MAKE, ("non-existent..."));
163234250Sobrien	}
164234250Sobrien    }
165234250Sobrien
166234250Sobrien    /*
167234250Sobrien     * A target is remade in one of the following circumstances:
168234250Sobrien     *	its modification time is smaller than that of its youngest child
169234250Sobrien     *	    and it would actually be run (has commands or type OP_NOP)
170234250Sobrien     *	it's the object of a force operator
171234250Sobrien     *	it has no children, was on the lhs of an operator and doesn't exist
172234250Sobrien     *	    already.
173234250Sobrien     *
174234250Sobrien     * Libraries are only considered out-of-date if the archive module says
175234250Sobrien     * they are.
176234250Sobrien     *
177234250Sobrien     * These weird rules are brought to you by Backward-Compatability and
178234250Sobrien     * the strange people who wrote 'Make'.
179234250Sobrien     */
180191739Sobrien    if (gn->type & OP_USE) {
181191739Sobrien	/*
182191739Sobrien	 * If the node is a USE node it is *never* out of date
183191739Sobrien	 * no matter *what*.
184191739Sobrien	 */
185191739Sobrien	DEBUGF(MAKE, (".USE node..."));
186234250Sobrien	oodate = FALSE;
187234250Sobrien    } else if (gn->type & OP_LIB) {
188191739Sobrien	DEBUGF(MAKE, ("library..."));
189191739Sobrien
190191739Sobrien	/*
191191739Sobrien	 * always out of date if no children and :: target
192191739Sobrien	 */
193191739Sobrien
194191739Sobrien	oodate = Arch_LibOODate (gn) ||
195191739Sobrien	    ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
196234250Sobrien    } else if (gn->type & OP_JOIN) {
197191739Sobrien	/*
198191739Sobrien	 * A target with the .JOIN attribute is only considered
199191739Sobrien	 * out-of-date if any of its children was out-of-date.
200191739Sobrien	 */
201191739Sobrien	DEBUGF(MAKE, (".JOIN node..."));
202234250Sobrien	oodate = gn->childMade;
203191739Sobrien    } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
204191739Sobrien	/*
205191739Sobrien	 * A node which is the object of the force (!) operator or which has
206191739Sobrien	 * the .EXEC attribute is always considered out-of-date.
207191739Sobrien	 */
208191739Sobrien	if (gn->type & OP_FORCE) {
209191739Sobrien	    DEBUGF(MAKE, ("! operator..."));
210191739Sobrien	} else if (gn->type & OP_PHONY) {
211191739Sobrien	    DEBUGF(MAKE, (".PHONY node..."));
212191739Sobrien	} else {
213234250Sobrien	    DEBUGF(MAKE, (".EXEC node..."));
214191739Sobrien	}
215191739Sobrien	oodate = TRUE;
216191739Sobrien    } else if ((gn->mtime < gn->cmtime) ||
217191739Sobrien	       ((gn->cmtime == 0) &&
218191739Sobrien		((gn->mtime==0) || (gn->type & OP_DOUBLEDEP))))
219191739Sobrien    {
220191739Sobrien	/*
221191739Sobrien	 * A node whose modification time is less than that of its
222191739Sobrien	 * youngest child or that has no children (cmtime == 0) and
223191739Sobrien	 * either doesn't exist (mtime == 0) or was the object of a
224191739Sobrien	 * :: operator is out-of-date. Why? Because that's the way Make does
225191739Sobrien	 * it.
226191739Sobrien	 */
227191739Sobrien	if (gn->mtime < gn->cmtime) {
228191739Sobrien	    DEBUGF(MAKE, ("modified before source..."));
229191739Sobrien	} else if (gn->mtime == 0) {
230191739Sobrien	    DEBUGF(MAKE, ("non-existent and no sources..."));
231191739Sobrien	} else {
232191739Sobrien	    DEBUGF(MAKE, (":: operator and no sources..."));
233191739Sobrien	}
234191739Sobrien	oodate = TRUE;
235191739Sobrien    } else {
236191739Sobrien#if 0
237191739Sobrien	/* WHY? */
238191739Sobrien	DEBUGF(MAKE, ("source %smade...", gn->childMade ? "" : "not "));
239191739Sobrien	oodate = gn->childMade;
240191739Sobrien#else
241191739Sobrien	oodate = FALSE;
242191739Sobrien#endif /* 0 */
243191739Sobrien    }
244191739Sobrien
245191739Sobrien    /*
246191739Sobrien     * If the target isn't out-of-date, the parents need to know its
247191739Sobrien     * modification time. Note that targets that appear to be out-of-date
248234250Sobrien     * but aren't, because they have no commands and aren't of type OP_NOP,
249191739Sobrien     * have their mtime stay below their children's mtime to keep parents from
250191739Sobrien     * thinking they're out-of-date.
251191739Sobrien     */
252191739Sobrien    if (!oodate) {
253191739Sobrien	Lst_ForEach (gn->parents, MakeTimeStamp, (void *)gn);
254191739Sobrien    }
255191739Sobrien
256191739Sobrien    return (oodate);
257191739Sobrien}
258191739Sobrien
259191739Sobrien/*-
260191739Sobrien *-----------------------------------------------------------------------
261191739Sobrien * MakeAddChild  --
262191739Sobrien *	Function used by Make_Run to add a child to the list l.
263191739Sobrien *	It will only add the child if its make field is FALSE.
264192348Sdelphij *
265234250Sobrien * Results:
266234250Sobrien *	Always returns 0
267234250Sobrien *
268192348Sdelphij * Side Effects:
269192348Sdelphij *	The given list is extended
270276415Sdelphij *-----------------------------------------------------------------------
271276415Sdelphij */
272276415Sdelphijstatic int
273276415SdelphijMakeAddChild (gnp, lp)
274276415Sdelphij    void *     gnp;		/* the node to add */
275276415Sdelphij    void *     lp;		/* the list to which to add it */
276276415Sdelphij{
277276415Sdelphij    GNode          *gn = (GNode *) gnp;
278276415Sdelphij    Lst            l = (Lst) lp;
279276415Sdelphij
280276415Sdelphij    if (!gn->make && !(gn->type & OP_USE)) {
281276415Sdelphij	(void)Lst_EnQueue (l, (void *)gn);
282276415Sdelphij    }
283191739Sobrien    return (0);
284191739Sobrien}
285191739Sobrien
286192348Sdelphij/*-
287191739Sobrien *-----------------------------------------------------------------------
288191739Sobrien * Make_HandleUse --
289191739Sobrien *	Function called by Make_Run and SuffApplyTransform on the downward
290191739Sobrien *	pass to handle .USE and transformation nodes. A callback function
291191739Sobrien *	for Lst_ForEach, it implements the .USE and transformation
292192348Sdelphij *	functionality by copying the node's commands, type flags
293192348Sdelphij *	and children to the parent node. Should be called before the
294191739Sobrien *	children are enqueued to be looked at by MakeAddChild.
295191739Sobrien *
296192348Sdelphij *	A .USE node is much like an explicit transformation rule, except
297192348Sdelphij *	its commands are always added to the target node, even if the
298192348Sdelphij *	target already has commands.
299191739Sobrien *
300191739Sobrien * Results:
301191739Sobrien *	returns 0.
302192348Sdelphij *
303191739Sobrien * Side Effects:
304191739Sobrien *	Children and commands may be added to the parent and the parent's
305192348Sdelphij *	type may be changed.
306192348Sdelphij *
307192348Sdelphij *-----------------------------------------------------------------------
308192348Sdelphij */
309192348Sdelphijint
310267843SdelphijMake_HandleUse (cgn, pgn)
311267843Sdelphij    GNode	*cgn;		/* The .USE node */
312226048Sobrien    GNode   	*pgn;		/* The target of the .USE node */
313191739Sobrien{
314267843Sdelphij    GNode	*gn;	 	/* A child of the .USE node */
315267843Sdelphij    LstNode	ln;	 	/* An element in the children list */
316267843Sdelphij
317284778Sdelphij    if (cgn->type & (OP_USE|OP_TRANSFORM)) {
318192348Sdelphij	if ((cgn->type & OP_USE) || Lst_IsEmpty(pgn->commands)) {
319192348Sdelphij	    /*
320192348Sdelphij	     * .USE or transformation and target has no commands -- append
321226048Sobrien	     * the child's commands to the parent.
322226048Sobrien	     */
323276415Sdelphij	    (void) Lst_Concat (pgn->commands, cgn->commands, LST_CONCNEW);
324276415Sdelphij	}
325191739Sobrien
326191739Sobrien	if (Lst_Open (cgn->children) == SUCCESS) {
327191739Sobrien	    while ((ln = Lst_Next (cgn->children)) != NULL) {
328191739Sobrien		gn = (GNode *)Lst_Datum (ln);
329191739Sobrien
330191739Sobrien		if (Lst_Member (pgn->children, gn) == NULL) {
331267843Sdelphij		    (void) Lst_AtEnd (pgn->children, gn);
332276415Sdelphij		    (void) Lst_AtEnd (gn->parents, pgn);
333191739Sobrien		    pgn->unmade += 1;
334191739Sobrien		}
335191739Sobrien	    }
336192348Sdelphij	    Lst_Close (cgn->children);
337284778Sdelphij	}
338191739Sobrien
339192348Sdelphij	pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_TRANSFORM);
340191739Sobrien
341191739Sobrien	/*
342191739Sobrien	 * This child node is now "made", so we decrement the count of
343276415Sdelphij	 * unmade children in the parent... We also remove the child
344191739Sobrien	 * from the parent's list to accurately reflect the number of decent
345191739Sobrien	 * children the parent has. This is used by Make_Run to decide
346191739Sobrien	 * whether to queue the parent or examine its children...
347191739Sobrien	 */
348	if (cgn->type & OP_USE) {
349	    pgn->unmade--;
350	}
351    }
352    return (0);
353}
354static int
355MakeHandleUse (pgn, cgn)
356    void * pgn;	/* the current parent */
357    void * cgn;	/* the child we've just examined */
358{
359    return Make_HandleUse((GNode *) pgn, (GNode *) cgn);
360}
361
362/*-
363 *-----------------------------------------------------------------------
364 * Make_Update  --
365 *	Perform update on the parents of a node. Used by JobFinish once
366 *	a node has been dealt with and by MakeStartJobs if it finds an
367 *	up-to-date node.
368 *
369 * Results:
370 *	Always returns 0
371 *
372 * Side Effects:
373 *	The unmade field of pgn is decremented and pgn may be placed on
374 *	the toBeMade queue if this field becomes 0.
375 *
376 * 	If the child was made, the parent's childMade field will be set true
377 *	and its cmtime set to now.
378 *
379 *	If the child wasn't made, the cmtime field of the parent will be
380 *	altered if the child's mtime is big enough.
381 *
382 *	Finally, if the child is the implied source for the parent, the
383 *	parent's IMPSRC variable is set appropriately.
384 *
385 *-----------------------------------------------------------------------
386 */
387void
388Make_Update (cgn)
389    GNode *cgn;			/* the child node */
390{
391    GNode 	*pgn;		/* the parent node */
392    char  	*cname;		/* the child's name */
393    LstNode	ln;	 	/* Element in parents and iParents lists */
394    char	*p1;
395
396    cname = Var_Value (TARGET, cgn, &p1);
397    efree(p1);
398
399    /*
400     * If the child was actually made, see what its modification time is
401     * now -- some rules won't actually update the file. If the file still
402     * doesn't exist, make its mtime now.
403     */
404    if (cgn->made != UPTODATE) {
405#ifndef RECHECK
406	/*
407	 * We can't re-stat the thing, but we can at least take care of rules
408	 * where a target depends on a source that actually creates the
409	 * target, but only if it has changed, e.g.
410	 *
411	 * parse.h : parse.o
412	 *
413	 * parse.o : parse.y
414	 *  	yacc -d parse.y
415	 *  	cc -c y.tab.c
416	 *  	mv y.tab.o parse.o
417	 *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
418	 *
419	 * In this case, if the definitions produced by yacc haven't changed
420	 * from before, parse.h won't have been updated and cgn->mtime will
421	 * reflect the current modification time for parse.h. This is
422	 * something of a kludge, I admit, but it's a useful one..
423	 * XXX: People like to use a rule like
424	 *
425	 * FRC:
426	 *
427	 * To force things that depend on FRC to be made, so we have to
428	 * check for gn->children being empty as well...
429	 */
430	if (!Lst_IsEmpty(cgn->commands) || Lst_IsEmpty(cgn->children)) {
431	    cgn->mtime = now;
432	}
433#else
434	/*
435	 * This is what Make does and it's actually a good thing, as it
436	 * allows rules like
437	 *
438	 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
439	 *
440	 * to function as intended. Unfortunately, thanks to the stateless
441	 * nature of NFS (by which I mean the loose coupling of two clients
442	 * using the same file from a common server), there are times
443	 * when the modification time of a file created on a remote
444	 * machine will not be modified before the local stat() implied by
445	 * the Dir_MTime occurs, thus leading us to believe that the file
446	 * is unchanged, wreaking havoc with files that depend on this one.
447	 *
448	 * I have decided it is better to make too much than to make too
449	 * little, so this stuff is commented out unless you're sure it's ok.
450	 * -- ardeb 1/12/88
451	 */
452	/*
453	 * Christos, 4/9/92: If we are  saving commands pretend that
454	 * the target is made now. Otherwise archives with ... rules
455	 * don't work!
456	 */
457	if (noExecute || (cgn->type & OP_SAVE_CMDS) || Dir_MTime(cgn) == 0) {
458	    cgn->mtime = now;
459	}
460	DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(cgn->mtime)));
461#endif
462    }
463
464    if (Lst_Open (cgn->parents) == SUCCESS) {
465	while ((ln = Lst_Next (cgn->parents)) != NULL) {
466	    pgn = (GNode *)Lst_Datum (ln);
467	    if (pgn->make) {
468		pgn->unmade -= 1;
469
470		if ( ! (cgn->type & (OP_EXEC|OP_USE))) {
471		    if (cgn->made == MADE) {
472			pgn->childMade = TRUE;
473			if (pgn->cmtime < cgn->mtime) {
474			    pgn->cmtime = cgn->mtime;
475			}
476		    } else {
477			(void)Make_TimeStamp (pgn, cgn);
478		    }
479		}
480		if (pgn->unmade == 0) {
481		    /*
482		     * Queue the node up -- any unmade predecessors will
483		     * be dealt with in MakeStartJobs.
484		     */
485		    (void)Lst_EnQueue (toBeMade, (void *)pgn);
486		} else if (pgn->unmade < 0) {
487		    Error ("Graph cycles through %s", pgn->name);
488		}
489	    }
490	}
491	Lst_Close (cgn->parents);
492    }
493    /*
494     * Deal with successor nodes. If any is marked for making and has an unmade
495     * count of 0, has not been made and isn't in the examination queue,
496     * it means we need to place it in the queue as it restrained itself
497     * before.
498     */
499    for (ln = Lst_First(cgn->successors); ln != NULL; ln = Lst_Succ(ln)) {
500	GNode	*succ = (GNode *)Lst_Datum(ln);
501
502	if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
503	    Lst_Member(toBeMade, (void *)succ) == NULL)
504	{
505	    (void)Lst_EnQueue(toBeMade, (void *)succ);
506	}
507    }
508
509    /*
510     * Set the .PREFIX and .IMPSRC variables for all the implied parents
511     * of this node.
512     */
513    if (Lst_Open (cgn->iParents) == SUCCESS) {
514	char    *p1;
515	char	*cpref = Var_Value(PREFIX, cgn, &p1);
516
517	while ((ln = Lst_Next (cgn->iParents)) != NULL) {
518	    pgn = (GNode *)Lst_Datum (ln);
519	    if (pgn->make) {
520		Var_Set (IMPSRC, cname, pgn);
521		Var_Set (PREFIX, cpref, pgn);
522	    }
523	}
524	efree(p1);
525	Lst_Close (cgn->iParents);
526    }
527}
528
529/*-
530 *-----------------------------------------------------------------------
531 * MakeAddAllSrc --
532 *	Add a child's name to the ALLSRC and OODATE variables of the given
533 *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
534 *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
535 *	.EXEC and .USE children are very rarely going to be files, so...
536 *	A child is added to the OODATE variable if its modification time is
537 *	later than that of its parent, as defined by Make, except if the
538 *	parent is a .JOIN node. In that case, it is only added to the OODATE
539 *	variable if it was actually made (since .JOIN nodes don't have
540 *	modification times, the comparison is rather unfair...)..
541 *
542 * Results:
543 *	Always returns 0
544 *
545 * Side Effects:
546 *	The ALLSRC variable for the given node is extended.
547 *-----------------------------------------------------------------------
548 */
549static int
550MakeAddAllSrc (cgnp, pgnp)
551    void *	cgnp;	/* The child to add */
552    void *	pgnp;	/* The parent to whose ALLSRC variable it should be */
553			/* added */
554{
555    GNode	*cgn = (GNode *) cgnp;
556    GNode	*pgn = (GNode *) pgnp;
557    if ((cgn->type & (OP_EXEC|OP_USE|OP_INVISIBLE)) == 0) {
558	char *child;
559	char *p1 = NULL;
560
561	if (OP_NOP(cgn->type)) {
562	    /*
563	     * this node is only source; use the specific pathname for it
564	     */
565	    child = cgn->path ? cgn->path : cgn->name;
566	}
567	else
568	    child = Var_Value(TARGET, cgn, &p1);
569	Var_Append (ALLSRC, child, pgn);
570	if (pgn->type & OP_JOIN) {
571	    if (cgn->made == MADE) {
572		Var_Append(OODATE, child, pgn);
573	    }
574	} else if ((pgn->mtime < cgn->mtime) ||
575		   (cgn->mtime >= now && cgn->made == MADE))
576	{
577	    /*
578	     * It goes in the OODATE variable if the parent is younger than the
579	     * child or if the child has been modified more recently than
580	     * the start of the make. This is to keep pmake from getting
581	     * confused if something else updates the parent after the
582	     * make starts (shouldn't happen, I know, but sometimes it
583	     * does). In such a case, if we've updated the kid, the parent
584	     * is likely to have a modification time later than that of
585	     * the kid and anything that relies on the OODATE variable will
586	     * be hosed.
587	     *
588	     * XXX: This will cause all made children to go in the OODATE
589	     * variable, even if they're not touched, if RECHECK isn't defined,
590	     * since cgn->mtime is set to now in Make_Update. According to
591	     * some people, this is good...
592	     */
593	    Var_Append(OODATE, child, pgn);
594	}
595	efree(p1);
596    }
597    return (0);
598}
599
600/*-
601 *-----------------------------------------------------------------------
602 * Make_DoAllVar --
603 *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
604 *	done separately, rather than while traversing the graph. This is
605 *	because Make defined OODATE to contain all sources whose modification
606 *	times were later than that of the target, *not* those sources that
607 *	were out-of-date. Since in both compatibility and native modes,
608 *	the modification time of the parent isn't found until the child
609 *	has been dealt with, we have to wait until now to fill in the
610 *	variable. As for ALLSRC, the ordering is important and not
611 *	guaranteed when in native mode, so it must be set here, too.
612 *
613 * Results:
614 *	None
615 *
616 * Side Effects:
617 *	The ALLSRC and OODATE variables of the given node is filled in.
618 *	If the node is a .JOIN node, its TARGET variable will be set to
619 * 	match its ALLSRC variable.
620 *-----------------------------------------------------------------------
621 */
622void
623Make_DoAllVar (gn)
624    GNode	*gn;
625{
626    Lst_ForEach (gn->children, MakeAddAllSrc, (void *) gn);
627
628    if (!Var_Exists (OODATE, gn)) {
629	Var_Set (OODATE, "", gn);
630    }
631    if (!Var_Exists (ALLSRC, gn)) {
632	Var_Set (ALLSRC, "", gn);
633    }
634
635    if (gn->type & OP_JOIN) {
636	char *p1;
637	Var_Set (TARGET, Var_Value (ALLSRC, gn, &p1), gn);
638	efree(p1);
639    }
640}
641
642/*-
643 *-----------------------------------------------------------------------
644 * MakeStartJobs --
645 *	Start as many jobs as possible.
646 *
647 * Results:
648 *	If the query flag was given to pmake, no job will be started,
649 *	but as soon as an out-of-date target is found, this function
650 *	returns TRUE. At all other times, this function returns FALSE.
651 *
652 * Side Effects:
653 *	Nodes are removed from the toBeMade queue and job table slots
654 *	are filled.
655 *
656 *-----------------------------------------------------------------------
657 */
658static Boolean
659MakeStartJobs ()
660{
661    GNode	*gn;
662
663    while (!Job_Full() && !Lst_IsEmpty (toBeMade)) {
664	gn = (GNode *) Lst_DeQueue (toBeMade);
665	DEBUGF(MAKE, ("Examining %s...", gn->name));
666	/*
667	 * Make sure any and all predecessors that are going to be made,
668	 * have been.
669	 */
670	if (!Lst_IsEmpty(gn->preds)) {
671	    LstNode ln;
672
673	    for (ln = Lst_First(gn->preds); ln != NULL; ln = Lst_Succ(ln)){
674		GNode	*pgn = (GNode *)Lst_Datum(ln);
675
676		if (pgn->make && pgn->made == UNMADE) {
677		    DEBUGF(MAKE, ("predecessor %s not made yet.\n", pgn->name));
678		    break;
679		}
680	    }
681	    /*
682	     * If ln isn't NULL, there's a predecessor as yet unmade, so we
683	     * just drop this node on the floor. When the node in question
684	     * has been made, it will notice this node as being ready to
685	     * make but as yet unmade and will place the node on the queue.
686	     */
687	    if (ln != NULL) {
688		continue;
689	    }
690	}
691
692	numNodes--;
693	if (Make_OODate (gn)) {
694	    DEBUGF(MAKE, ("out-of-date\n"));
695	    if (queryFlag) {
696		return (TRUE);
697	    }
698	    Make_DoAllVar (gn);
699	    Job_Make (gn);
700	} else {
701	    DEBUGF(MAKE, ("up-to-date\n"));
702	    gn->made = UPTODATE;
703	    if (gn->type & OP_JOIN) {
704		/*
705		 * Even for an up-to-date .JOIN node, we need it to have its
706		 * context variables so references to it get the correct
707		 * value for .TARGET when building up the context variables
708		 * of its parent(s)...
709		 */
710		Make_DoAllVar (gn);
711	    }
712
713	    Make_Update (gn);
714	}
715    }
716    return (FALSE);
717}
718
719/*-
720 *-----------------------------------------------------------------------
721 * MakePrintStatus --
722 *	Print the status of a top-level node, viz. it being up-to-date
723 *	already or not created due to an error in a lower level.
724 *	Callback function for Make_Run via Lst_ForEach.
725 *
726 * Results:
727 *	Always returns 0.
728 *
729 * Side Effects:
730 *	A message may be printed.
731 *
732 *-----------------------------------------------------------------------
733 */
734static int
735MakePrintStatus(gnp, cyclep)
736    void *  gnp;	    /* Node to examine */
737    void * 	cyclep;	    /* True if gn->unmade being non-zero implies
738			     * a cycle in the graph, not an error in an
739			     * inferior */
740{
741    GNode   	*gn = (GNode *) gnp;
742    Boolean 	cycle = *(Boolean *) cyclep;
743    if (gn->made == UPTODATE) {
744	printf ("`%s' is up to date.\n", gn->name);
745    } else if (gn->unmade != 0) {
746	if (cycle) {
747	    Boolean t = TRUE;
748	    /*
749	     * If printing cycles and came to one that has unmade children,
750	     * print out the cycle by recursing on its children. Note a
751	     * cycle like:
752	     *	a : b
753	     *	b : c
754	     *	c : b
755	     * will cause this to erroneously complain about a being in
756	     * the cycle, but this is a good approximation.
757	     */
758	    if (gn->made == CYCLE) {
759		Error("Graph cycles through `%s'", gn->name);
760		gn->made = ENDCYCLE;
761		Lst_ForEach(gn->children, MakePrintStatus, (void *) &t);
762		gn->made = UNMADE;
763	    } else if (gn->made != ENDCYCLE) {
764		gn->made = CYCLE;
765		Lst_ForEach(gn->children, MakePrintStatus, (void *) &t);
766	    }
767	} else {
768	    printf ("`%s' not remade because of errors.\n", gn->name);
769	}
770    }
771    return (0);
772}
773
774/*-
775 *-----------------------------------------------------------------------
776 * Make_Run --
777 *	Initialize the nodes to remake and the list of nodes which are
778 *	ready to be made by doing a breadth-first traversal of the graph
779 *	starting from the nodes in the given list. Once this traversal
780 *	is finished, all the 'leaves' of the graph are in the toBeMade
781 *	queue.
782 *	Using this queue and the Job module, work back up the graph,
783 *	calling on MakeStartJobs to keep the job table as full as
784 *	possible.
785 *
786 * Results:
787 *	TRUE if work was done. FALSE otherwise.
788 *
789 * Side Effects:
790 *	The make field of all nodes involved in the creation of the given
791 *	targets is set to 1. The toBeMade list is set to contain all the
792 *	'leaves' of these subgraphs.
793 *-----------------------------------------------------------------------
794 */
795Boolean
796Make_Run (targs)
797    Lst             targs;	/* the initial list of targets */
798{
799    GNode	    *gn;	/* a temporary pointer */
800    Lst		    examine; 	/* List of targets to examine */
801    int	    	    errors; 	/* Number of errors the Job module reports */
802
803    toBeMade = Lst_Init (FALSE);
804
805    examine = Lst_Duplicate(targs, NOCOPY);
806    numNodes = 0;
807
808    /*
809     * Make an initial downward pass over the graph, marking nodes to be made
810     * as we go down. We call Suff_FindDeps to find where a node is and
811     * to get some children for it if it has none and also has no commands.
812     * If the node is a leaf, we stick it on the toBeMade queue to
813     * be looked at in a minute, otherwise we add its children to our queue
814     * and go on about our business.
815     */
816    while (!Lst_IsEmpty (examine)) {
817	gn = (GNode *) Lst_DeQueue (examine);
818
819	if (!gn->make) {
820	    gn->make = TRUE;
821	    numNodes++;
822
823	    /*
824	     * Apply any .USE rules before looking for implicit dependencies
825	     * to make sure everything has commands that should...
826	     */
827	    Lst_ForEach (gn->children, MakeHandleUse, (void *)gn);
828	    Suff_FindDeps (gn);
829
830	    if (gn->unmade != 0) {
831		Lst_ForEach (gn->children, MakeAddChild, (void *)examine);
832	    } else {
833		(void)Lst_EnQueue (toBeMade, (void *)gn);
834	    }
835	}
836    }
837
838    Lst_Destroy (examine, NOFREE);
839
840    if (queryFlag) {
841	/*
842	 * We wouldn't do any work unless we could start some jobs in the
843	 * next loop... (we won't actually start any, of course, this is just
844	 * to see if any of the targets was out of date)
845	 */
846	return (MakeStartJobs());
847    } else {
848	/*
849	 * Initialization. At the moment, no jobs are running and until some
850	 * get started, nothing will happen since the remaining upward
851	 * traversal of the graph is performed by the routines in job.c upon
852	 * the finishing of a job. So we fill the Job table as much as we can
853	 * before going into our loop.
854	 */
855	(void) MakeStartJobs();
856    }
857
858    /*
859     * Main Loop: The idea here is that the ending of jobs will take
860     * care of the maintenance of data structures and the waiting for output
861     * will cause us to be idle most of the time while our children run as
862     * much as possible. Because the job table is kept as full as possible,
863     * the only time when it will be empty is when all the jobs which need
864     * running have been run, so that is the end condition of this loop.
865     * Note that the Job module will exit if there were any errors unless the
866     * keepgoing flag was given.
867     */
868    while (!Job_Empty ()) {
869	Job_CatchOutput ();
870	Job_CatchChildren (!usePipes);
871	(void)MakeStartJobs();
872    }
873
874    errors = Job_Finish();
875
876    /*
877     * Print the final status of each target. E.g. if it wasn't made
878     * because some inferior reported an error.
879     */
880    errors = ((errors == 0) && (numNodes != 0));
881    Lst_ForEach(targs, MakePrintStatus, (void *) &errors);
882
883    return (TRUE);
884}
885