1141104Sharti/*-
21590Srgrimes * Copyright (c) 1988, 1989, 1990, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes * Copyright (c) 1989 by Berkeley Softworks
51590Srgrimes * All rights reserved.
61590Srgrimes *
71590Srgrimes * This code is derived from software contributed to Berkeley by
81590Srgrimes * Adam de Boor.
91590Srgrimes *
101590Srgrimes * Redistribution and use in source and binary forms, with or without
111590Srgrimes * modification, are permitted provided that the following conditions
121590Srgrimes * are met:
131590Srgrimes * 1. Redistributions of source code must retain the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer.
151590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161590Srgrimes *    notice, this list of conditions and the following disclaimer in the
171590Srgrimes *    documentation and/or other materials provided with the distribution.
181590Srgrimes * 3. All advertising materials mentioning features or use of this software
191590Srgrimes *    must display the following acknowledgement:
201590Srgrimes *	This product includes software developed by the University of
211590Srgrimes *	California, Berkeley and its contributors.
221590Srgrimes * 4. Neither the name of the University nor the names of its contributors
231590Srgrimes *    may be used to endorse or promote products derived from this software
241590Srgrimes *    without specific prior written permission.
251590Srgrimes *
261590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361590Srgrimes * SUCH DAMAGE.
3762833Swsanchez *
3862833Swsanchez * @(#)make.c	8.1 (Berkeley) 6/6/93
391590Srgrimes */
401590Srgrimes
4162833Swsanchez#include <sys/cdefs.h>
4294587Sobrien__FBSDID("$FreeBSD$");
431590Srgrimes
44144478Sharti/*
45144478Sharti * make.c
461590Srgrimes *	The functions which perform the examination of targets and
471590Srgrimes *	their suitability for creation
481590Srgrimes *
491590Srgrimes * Interface:
50144478Sharti *	Make_Run	Initialize things for the module and recreate
51144478Sharti *			whatever needs recreating. Returns TRUE if
52144478Sharti *			work was (or would have been) done and FALSE
53144478Sharti *			otherwise.
541590Srgrimes *
55144478Sharti *	Make_Update	Update all parents of a given child. Performs
56144478Sharti *			various bookkeeping chores like the updating
57144478Sharti *			of the cmtime field of the parent, filling
58144478Sharti *			of the IMPSRC context variable, etc. It will
59144478Sharti *			place the parent on the toBeMade queue if it should be.
601590Srgrimes *
61144478Sharti *	Make_TimeStamp	Function to set the parent's cmtime field
62144478Sharti *			based on a child's modification time.
631590Srgrimes *
64144478Sharti *	Make_DoAllVar	Set up the various local variables for a
65144478Sharti *			target, including the .ALLSRC variable, making
66144478Sharti *			sure that any variable that needs to exist
67144478Sharti *			at the very least has the empty value.
681590Srgrimes *
69144478Sharti *	Make_OODate	Determine if a target is out-of-date.
701590Srgrimes *
71144478Sharti *	Make_HandleUse	See if a child is a .USE node for a parent
72144478Sharti *			and perform the .USE actions if so.
731590Srgrimes */
741590Srgrimes
75141104Sharti#include "arch.h"
76200630Sstas#include "config.h"
77141104Sharti#include "dir.h"
78141104Sharti#include "globals.h"
79141104Sharti#include "GNode.h"
80141104Sharti#include "job.h"
81141104Sharti#include "make.h"
82146066Sharti#include "parse.h"
83141104Sharti#include "suff.h"
84141104Sharti#include "targ.h"
85141104Sharti#include "util.h"
86141104Sharti#include "var.h"
87141104Sharti
88138916Sharti/* The current fringe of the graph. These are nodes which await examination
89138916Sharti * by MakeOODate. It is added to by Make_Update and subtracted from by
90138916Sharti * MakeStartJobs */
91138916Shartistatic Lst toBeMade = Lst_Initializer(toBeMade);
92138916Sharti
93144478Sharti/*
94144478Sharti * Number of nodes to be processed. If this is non-zero when Job_Empty()
95144478Sharti * returns TRUE, there's a cycle in the graph.
96144478Sharti */
97144478Shartistatic int numNodes;
981590Srgrimes
9992921Simpstatic Boolean MakeStartJobs(void);
100138232Sharti
101144478Sharti/**
102144478Sharti * Make_TimeStamp
1031590Srgrimes *	Set the cmtime field of a parent node based on the mtime stamp in its
104143694Sharti *	child. Called from MakeOODate via LST_FOREACH.
1051590Srgrimes *
1061590Srgrimes * Results:
1078874Srgrimes *	Always returns 0.
1081590Srgrimes *
1091590Srgrimes * Side Effects:
1101590Srgrimes *	The cmtime of the parent node will be changed if the mtime
1111590Srgrimes *	field of the child is greater than it.
1121590Srgrimes */
1131590Srgrimesint
114138232ShartiMake_TimeStamp(GNode *pgn, GNode *cgn)
1151590Srgrimes{
116138232Sharti
117144478Sharti	if (cgn->mtime > pgn->cmtime) {
118144478Sharti		pgn->cmtime = cgn->mtime;
119168893Sfjoe		pgn->cmtime_gn = cgn;
120144478Sharti	}
121144478Sharti	return (0);
1221590Srgrimes}
1235814Sjkh
124144478Sharti/**
125144478Sharti * Make_OODate
1261590Srgrimes *	See if a given node is out of date with respect to its sources.
1271590Srgrimes *	Used by Make_Run when deciding which nodes to place on the
1281590Srgrimes *	toBeMade queue initially and by Make_Update to screen out USE and
1291590Srgrimes *	EXEC nodes. In the latter case, however, any other sort of node
1301590Srgrimes *	must be considered out-of-date since at least one of its children
1311590Srgrimes *	will have been recreated.
1321590Srgrimes *
1331590Srgrimes * Results:
1348874Srgrimes *	TRUE if the node is out of date. FALSE otherwise.
1351590Srgrimes *
1361590Srgrimes * Side Effects:
1371590Srgrimes *	The mtime field of the node and the cmtime field of its parents
1381590Srgrimes *	will/may be changed.
1391590Srgrimes */
1401590SrgrimesBoolean
141138232ShartiMake_OODate(GNode *gn)
1421590Srgrimes{
143144478Sharti	Boolean	oodate;
144144478Sharti	LstNode	*ln;
1451590Srgrimes
1461590Srgrimes	/*
147144478Sharti	 * Certain types of targets needn't even be sought as their datedness
148144478Sharti	 * doesn't depend on their modification time...
1491590Srgrimes	 */
150144478Sharti	if ((gn->type & (OP_JOIN | OP_USE | OP_EXEC)) == 0) {
151144478Sharti		Dir_MTime(gn);
152144478Sharti		if (gn->mtime != 0) {
153144478Sharti			DEBUGF(MAKE, ("modified %s...",
154144478Sharti			    Targ_FmtTime(gn->mtime)));
155144478Sharti		} else {
156144478Sharti			DEBUGF(MAKE, ("non-existent..."));
157144478Sharti		}
158144478Sharti	}
1595814Sjkh
1605814Sjkh	/*
161144478Sharti	 * A target is remade in one of the following circumstances:
162144478Sharti	 *	its modification time is smaller than that of its youngest child
163144478Sharti	 *	    and it would actually be run (has commands or type OP_NOP)
164144478Sharti	 *	it's the object of a force operator
165144478Sharti	 *	it has no children, was on the lhs of an operator and doesn't
166144478Sharti	 *	    exist already.
167144478Sharti	 *
168144478Sharti	 * Libraries are only considered out-of-date if the archive module says
169144478Sharti	 * they are.
170144478Sharti	 *
171144478Sharti	 * These weird rules are brought to you by Backward-Compatibility and
172144478Sharti	 * the strange people who wrote 'Make'.
1735814Sjkh	 */
174144478Sharti	if (gn->type & OP_USE) {
175144478Sharti		/*
176144478Sharti		 * If the node is a USE node it is *never* out of date
177144478Sharti		 * no matter *what*.
178144478Sharti		 */
179144478Sharti		DEBUGF(MAKE, (".USE node..."));
180144478Sharti		oodate = FALSE;
1815814Sjkh
182144478Sharti	} else if (gn->type & OP_LIB) {
183144478Sharti		DEBUGF(MAKE, ("library..."));
184144478Sharti
185144478Sharti		/*
186144478Sharti		 * always out of date if no children and :: target
187144478Sharti		 */
188144478Sharti		oodate = Arch_LibOODate(gn) ||
189144478Sharti		    ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
190144478Sharti
191144478Sharti	} else if (gn->type & OP_JOIN) {
192144478Sharti		/*
193144478Sharti		 * A target with the .JOIN attribute is only considered
194144478Sharti		 * out-of-date if any of its children was out-of-date.
195144478Sharti		 */
196144478Sharti		DEBUGF(MAKE, (".JOIN node..."));
197144478Sharti		oodate = gn->childMade;
198144478Sharti
199144478Sharti	} else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
200144478Sharti		/*
201144478Sharti		 * A node which is the object of the force (!) operator or
202144478Sharti		 * which has the .EXEC attribute is always considered
203144478Sharti		 * out-of-date.
204144478Sharti		 */
205144478Sharti		if (gn->type & OP_FORCE) {
206144478Sharti			DEBUGF(MAKE, ("! operator..."));
207144478Sharti		} else if (gn->type & OP_PHONY) {
208144478Sharti			DEBUGF(MAKE, (".PHONY node..."));
209144478Sharti		} else {
210144478Sharti			DEBUGF(MAKE, (".EXEC node..."));
211144478Sharti		}
212144478Sharti
213190821Sfjoe		if (remakingMakefiles) {
214190821Sfjoe			DEBUGF(MAKE, ("skipping (remaking makefiles)..."));
215190821Sfjoe			oodate = FALSE;
216190821Sfjoe		} else {
217190821Sfjoe			oodate = TRUE;
218190821Sfjoe		}
219190821Sfjoe	} else if (gn->mtime < gn->cmtime ||
220190821Sfjoe	    (gn->cmtime == 0 && (gn->mtime == 0 || (gn->type & OP_DOUBLEDEP)))) {
221144478Sharti		/*
222144478Sharti		 * A node whose modification time is less than that of its
223144478Sharti		 * youngest child or that has no children (cmtime == 0) and
224144478Sharti		 * either doesn't exist (mtime == 0) or was the object of a
225144478Sharti		 * :: operator is out-of-date. Why? Because that's the way
226144478Sharti		 * Make does it.
227144478Sharti		 */
228144478Sharti		if (gn->mtime < gn->cmtime) {
229168893Sfjoe			DEBUGF(MAKE, ("modified before source (%s)...",
230168893Sfjoe			    gn->cmtime_gn ? gn->cmtime_gn->path : "???"));
231190821Sfjoe			oodate = TRUE;
232144478Sharti		} else if (gn->mtime == 0) {
233144478Sharti			DEBUGF(MAKE, ("non-existent and no sources..."));
234190821Sfjoe			if (remakingMakefiles && Lst_IsEmpty(&gn->commands)) {
235190821Sfjoe				DEBUGF(MAKE, ("skipping (no commands and remaking makefiles)..."));
236190821Sfjoe				oodate = FALSE;
237190821Sfjoe			} else {
238190821Sfjoe				oodate = TRUE;
239190821Sfjoe			}
240144478Sharti		} else {
241144478Sharti			DEBUGF(MAKE, (":: operator and no sources..."));
242190821Sfjoe			if (remakingMakefiles) {
243190821Sfjoe				DEBUGF(MAKE, ("skipping (remaking makefiles)..."));
244190821Sfjoe				oodate = FALSE;
245190821Sfjoe			} else {
246190821Sfjoe				oodate = TRUE;
247190821Sfjoe			}
248144478Sharti		}
249144478Sharti	} else
250144478Sharti		oodate = FALSE;
251144478Sharti
2521590Srgrimes	/*
253144478Sharti	 * If the target isn't out-of-date, the parents need to know its
254144478Sharti	 * modification time. Note that targets that appear to be out-of-date
255144478Sharti	 * but aren't, because they have no commands and aren't of type OP_NOP,
256144478Sharti	 * have their mtime stay below their children's mtime to keep parents
257144478Sharti	 * from thinking they're out-of-date.
2581590Srgrimes	 */
259144478Sharti	if (!oodate) {
260144478Sharti		LST_FOREACH(ln, &gn->parents)
261144478Sharti			if (Make_TimeStamp(Lst_Datum(ln), gn))
262144478Sharti				break;
2631590Srgrimes	}
2641590Srgrimes
265144478Sharti	return (oodate);
2661590Srgrimes}
267138232Sharti
268144478Sharti/**
269144478Sharti * Make_HandleUse
2701590Srgrimes *	Function called by Make_Run and SuffApplyTransform on the downward
2711590Srgrimes *	pass to handle .USE and transformation nodes. A callback function
272143694Sharti *	for LST_FOREACH, it implements the .USE and transformation
2731590Srgrimes *	functionality by copying the node's commands, type flags
2741590Srgrimes *	and children to the parent node. Should be called before the
275143694Sharti *	children are enqueued to be looked at.
2761590Srgrimes *
2771590Srgrimes *	A .USE node is much like an explicit transformation rule, except
2781590Srgrimes *	its commands are always added to the target node, even if the
2791590Srgrimes *	target already has commands.
2801590Srgrimes *
2811590Srgrimes * Results:
2821590Srgrimes *	returns 0.
2831590Srgrimes *
2841590Srgrimes * Side Effects:
2851590Srgrimes *	Children and commands may be added to the parent and the parent's
2861590Srgrimes *	type may be changed.
2871590Srgrimes *
2881590Srgrimes *-----------------------------------------------------------------------
2891590Srgrimes */
2901590Srgrimesint
291138232ShartiMake_HandleUse(GNode *cgn, GNode *pgn)
2921590Srgrimes{
293144478Sharti	GNode	*gn;	/* A child of the .USE node */
294144478Sharti	LstNode	*ln;	/* An element in the children list */
2951590Srgrimes
296144478Sharti	if (cgn->type & (OP_USE | OP_TRANSFORM)) {
297144478Sharti		if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
298144478Sharti			/*
299144478Sharti			 * .USE or transformation and target has no commands --
300144478Sharti			 * append the child's commands to the parent.
301144478Sharti			 */
302144478Sharti			Lst_Concat(&pgn->commands, &cgn->commands, LST_CONCNEW);
303144478Sharti		}
3048874Srgrimes
305144478Sharti		for (ln = Lst_First(&cgn->children); ln != NULL;
306144478Sharti		    ln = Lst_Succ(ln)) {
307144478Sharti			gn = Lst_Datum(ln);
3081590Srgrimes
309144478Sharti			if (Lst_Member(&pgn->children, gn) == NULL) {
310144478Sharti				Lst_AtEnd(&pgn->children, gn);
311144478Sharti				Lst_AtEnd(&gn->parents, pgn);
312144478Sharti				pgn->unmade += 1;
313144478Sharti			}
314144478Sharti		}
3158874Srgrimes
316144478Sharti		pgn->type |= cgn->type & ~(OP_OPMASK | OP_USE | OP_TRANSFORM);
3171590Srgrimes
318144478Sharti		/*
319144478Sharti		 * This child node is now "made", so we decrement the count of
320144478Sharti		 * unmade children in the parent... We also remove the child
321144478Sharti		 * from the parent's list to accurately reflect the number of
322144478Sharti		 * decent children the parent has. This is used by Make_Run to
323144478Sharti		 * decide whether to queue the parent or examine its children...
324144478Sharti		 */
325144478Sharti		if (cgn->type & OP_USE) {
326144478Sharti			pgn->unmade--;
327144478Sharti		}
3281590Srgrimes	}
329144478Sharti	return (0);
3301590Srgrimes}
331138232Sharti
332144478Sharti/**
333144478Sharti * Make_Update
3341590Srgrimes *	Perform update on the parents of a node. Used by JobFinish once
3351590Srgrimes *	a node has been dealt with and by MakeStartJobs if it finds an
3368874Srgrimes *	up-to-date node.
3371590Srgrimes *
3381590Srgrimes * Results:
3391590Srgrimes *	Always returns 0
3401590Srgrimes *
3411590Srgrimes * Side Effects:
3421590Srgrimes *	The unmade field of pgn is decremented and pgn may be placed on
3431590Srgrimes *	the toBeMade queue if this field becomes 0.
3441590Srgrimes *
3451590Srgrimes * 	If the child was made, the parent's childMade field will be set true
3461590Srgrimes *	and its cmtime set to now.
3471590Srgrimes *
3481590Srgrimes *	If the child wasn't made, the cmtime field of the parent will be
3491590Srgrimes *	altered if the child's mtime is big enough.
3501590Srgrimes *
3511590Srgrimes *	Finally, if the child is the implied source for the parent, the
3521590Srgrimes *	parent's IMPSRC variable is set appropriately.
3531590Srgrimes */
3541590Srgrimesvoid
355138232ShartiMake_Update(GNode *cgn)
3561590Srgrimes{
357146581Sharti	GNode		*pgn;	/* the parent node */
358146581Sharti	const char	*cname;	/* the child's name */
359146581Sharti	LstNode		*ln;	/* Element in parents and iParents lists */
360146581Sharti	const char	*cpref;
3611590Srgrimes
362146580Sharti	cname = Var_Value(TARGET, cgn);
3631590Srgrimes
3641590Srgrimes	/*
365144478Sharti	 * If the child was actually made, see what its modification time is
366144478Sharti	 * now -- some rules won't actually update the file. If the file still
367144478Sharti	 * doesn't exist, make its mtime now.
3681590Srgrimes	 */
369144478Sharti	if (cgn->made != UPTODATE) {
370144478Sharti#ifndef RECHECK
371144478Sharti		/*
372144478Sharti		 * We can't re-stat the thing, but we can at least take care
373144478Sharti		 * of rules where a target depends on a source that actually
374144478Sharti		 * creates the target, but only if it has changed, e.g.
375144478Sharti		 *
376144478Sharti		 * parse.h : parse.o
377144478Sharti		 *
378144478Sharti		 * parse.o : parse.y
379144478Sharti		 *  	yacc -d parse.y
380144478Sharti		 *  	cc -c y.tab.c
381144478Sharti		 *  	mv y.tab.o parse.o
382144478Sharti		 *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
383144478Sharti		 *
384144478Sharti		 * In this case, if the definitions produced by yacc haven't
385144478Sharti		 * changed from before, parse.h won't have been updated and
386144478Sharti		 * cgn->mtime will reflect the current modification time for
387144478Sharti		 * parse.h. This is something of a kludge, I admit, but it's a
388144478Sharti		 * useful one..
389144478Sharti		 * XXX: People like to use a rule like
390144478Sharti		 *
391144478Sharti		 * FRC:
392144478Sharti		 *
393144478Sharti		 * To force things that depend on FRC to be made, so we have to
394144478Sharti		 * check for gn->children being empty as well...
395144478Sharti		 */
396144478Sharti		if (!Lst_IsEmpty(&cgn->commands) ||
397144478Sharti		    Lst_IsEmpty(&cgn->children)) {
398144478Sharti			cgn->mtime = now;
399144478Sharti		}
400144478Sharti	#else
401144478Sharti		/*
402144478Sharti		 * This is what Make does and it's actually a good thing, as it
403144478Sharti		 * allows rules like
404144478Sharti		 *
405144478Sharti		 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
406144478Sharti		 *
407144478Sharti		 * to function as intended. Unfortunately, thanks to the
408144478Sharti		 * stateless nature of NFS (by which I mean the loose coupling
409144478Sharti		 * of two clients using the same file from a common server),
410144478Sharti		 * there are times when the modification time of a file created
411144478Sharti		 * on a remote machine will not be modified before the local
412144478Sharti		 * stat() implied by the Dir_MTime occurs, thus leading us to
413144478Sharti		 * believe that the file is unchanged, wreaking havoc with
414144478Sharti		 * files that depend on this one.
415144478Sharti		 *
416144478Sharti		 * I have decided it is better to make too much than to make too
417144478Sharti		 * little, so this stuff is commented out unless you're sure
418144478Sharti		 * it's ok.
419144478Sharti		 * -- ardeb 1/12/88
420144478Sharti		 */
421144478Sharti		/*
422144478Sharti		 * Christos, 4/9/92: If we are  saving commands pretend that
423144478Sharti		 * the target is made now. Otherwise archives with ... rules
424144478Sharti		 * don't work!
425144478Sharti		 */
426144478Sharti		if (noExecute || (cgn->type & OP_SAVE_CMDS) ||
427144478Sharti		    Dir_MTime(cgn) == 0) {
428144478Sharti			cgn->mtime = now;
429144478Sharti		}
430144478Sharti		DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(cgn->mtime)));
431144478Sharti#endif
4321590Srgrimes	}
4338874Srgrimes
434144478Sharti	for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Succ(ln)) {
435144478Sharti		pgn = Lst_Datum(ln);
436144478Sharti		if (pgn->make) {
437144478Sharti			pgn->unmade -= 1;
4381590Srgrimes
439144478Sharti			if (!(cgn->type & (OP_EXEC | OP_USE))) {
440168893Sfjoe				if (cgn->made == MADE)
441144478Sharti					pgn->childMade = TRUE;
442168893Sfjoe				Make_TimeStamp(pgn, cgn);
443144478Sharti			}
444144478Sharti			if (pgn->unmade == 0) {
445144478Sharti				/*
446144478Sharti				 * Queue the node up -- any unmade predecessors
447144478Sharti				 * will be dealt with in MakeStartJobs.
448144478Sharti				 */
449144478Sharti				Lst_EnQueue(&toBeMade, pgn);
450144478Sharti			} else if (pgn->unmade < 0) {
451144478Sharti				Error("Graph cycles through %s", pgn->name);
452144478Sharti			}
4531590Srgrimes		}
4541590Srgrimes	}
455138564Sharti
456144478Sharti	/*
457144478Sharti	 * Deal with successor nodes. If any is marked for making and has an
458144478Sharti	 * unmade count of 0, has not been made and isn't in the examination
459144478Sharti	 * queue, it means we need to place it in the queue as it restrained
460144478Sharti	 * itself before.
461144478Sharti	 */
462144478Sharti	for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Succ(ln)) {
463144478Sharti		GNode	*succ = Lst_Datum(ln);
4641590Srgrimes
465144478Sharti		if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
466144478Sharti		    Lst_Member(&toBeMade, succ) == NULL) {
467144478Sharti			Lst_EnQueue(&toBeMade, succ);
468144478Sharti		}
4691590Srgrimes	}
4708874Srgrimes
471144478Sharti	/*
472144478Sharti	 * Set the .PREFIX and .IMPSRC variables for all the implied parents
473144478Sharti	 * of this node.
474144478Sharti	 */
475146580Sharti	cpref = Var_Value(PREFIX, cgn);
476144478Sharti	for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Succ(ln)) {
477144478Sharti		pgn = Lst_Datum(ln);
478144478Sharti		if (pgn->make) {
479144478Sharti			Var_Set(IMPSRC, cname, pgn);
480144478Sharti			Var_Set(PREFIX, cpref, pgn);
481144478Sharti		}
4821590Srgrimes	}
4831590Srgrimes}
484138232Sharti
485144478Sharti/**
486144478Sharti * Make_DoAllVar
4871590Srgrimes *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
4881590Srgrimes *	done separately, rather than while traversing the graph. This is
4891590Srgrimes *	because Make defined OODATE to contain all sources whose modification
4901590Srgrimes *	times were later than that of the target, *not* those sources that
4911590Srgrimes *	were out-of-date. Since in both compatibility and native modes,
4921590Srgrimes *	the modification time of the parent isn't found until the child
4931590Srgrimes *	has been dealt with, we have to wait until now to fill in the
4941590Srgrimes *	variable. As for ALLSRC, the ordering is important and not
4951590Srgrimes *	guaranteed when in native mode, so it must be set here, too.
4961590Srgrimes *
4971590Srgrimes * Side Effects:
4981590Srgrimes *	The ALLSRC and OODATE variables of the given node is filled in.
4991590Srgrimes *	If the node is a .JOIN node, its TARGET variable will be set to
5001590Srgrimes * 	match its ALLSRC variable.
5011590Srgrimes */
5021590Srgrimesvoid
503138232ShartiMake_DoAllVar(GNode *gn)
5041590Srgrimes{
505146581Sharti	LstNode		*ln;
506146581Sharti	GNode		*cgn;
507146581Sharti	const char	*child;
5081590Srgrimes
509143694Sharti	LST_FOREACH(ln, &gn->children) {
510143694Sharti		/*
511143694Sharti		 * Add the child's name to the ALLSRC and OODATE variables of
512143694Sharti		 * the given node. The child is added only if it has not been
513143694Sharti		 * given the .EXEC, .USE or .INVISIBLE attributes. .EXEC and
514143694Sharti		 * .USE children are very rarely going to be files, so...
515143694Sharti		 *
516143694Sharti		 * A child is added to the OODATE variable if its modification
517143694Sharti		 * time is later than that of its parent, as defined by Make,
518143694Sharti		 * except if the parent is a .JOIN node. In that case, it is
519143694Sharti		 * only added to the OODATE variable if it was actually made
520143694Sharti		 * (since .JOIN nodes don't have modification times, the
521143694Sharti		 * comparison is rather unfair...).
522143694Sharti		 */
523143694Sharti		cgn = Lst_Datum(ln);
524138232Sharti
525143694Sharti		if ((cgn->type & (OP_EXEC | OP_USE | OP_INVISIBLE)) == 0) {
526143694Sharti			if (OP_NOP(cgn->type)) {
527143694Sharti				/*
528143694Sharti				 * this node is only source; use the specific
529143694Sharti				 * pathname for it
530143694Sharti				 */
531143694Sharti				child = cgn->path ? cgn->path : cgn->name;
532143694Sharti			} else
533146580Sharti				child = Var_Value(TARGET, cgn);
534143694Sharti			Var_Append(ALLSRC, child, gn);
535143694Sharti			if (gn->type & OP_JOIN) {
536143694Sharti				if (cgn->made == MADE) {
537143694Sharti					Var_Append(OODATE, child, gn);
538143694Sharti				}
539143694Sharti			} else if (gn->mtime < cgn->mtime ||
540143694Sharti			    (cgn->mtime >= now && cgn->made == MADE)) {
541143694Sharti				/*
542143694Sharti				 * It goes in the OODATE variable if the parent
543143694Sharti				 * is younger than the child or if the child has
544143694Sharti				 * been modified more recently than the start of
545143694Sharti				 * the make. This is to keep pmake from getting
546143694Sharti				 * confused if something else updates the parent
547143694Sharti				 * after the make starts (shouldn't happen, I
548143694Sharti				 * know, but sometimes it does). In such a case,
549143694Sharti				 * if we've updated the kid, the parent is
550143694Sharti				 * likely to have a modification time later than
551143694Sharti				 * that of the kid and anything that relies on
552143694Sharti				 * the OODATE variable will be hosed.
553143694Sharti				 *
554143694Sharti				 * XXX: This will cause all made children to
555143694Sharti				 * go in the OODATE variable, even if they're
556143694Sharti				 * not touched, if RECHECK isn't defined, since
557143694Sharti				 * cgn->mtime is set to now in Make_Update.
558143694Sharti				 * According to some people, this is good...
559143694Sharti				 */
560143694Sharti				Var_Append(OODATE, child, gn);
561143694Sharti			}
562143694Sharti		}
563143694Sharti	}
5641590Srgrimes
565143694Sharti	if (!Var_Exists (OODATE, gn)) {
566143694Sharti		Var_Set(OODATE, "", gn);
567143694Sharti	}
568143694Sharti	if (!Var_Exists (ALLSRC, gn)) {
569143694Sharti		Var_Set(ALLSRC, "", gn);
570143694Sharti	}
571138232Sharti
572143694Sharti	if (gn->type & OP_JOIN) {
573146580Sharti		Var_Set(TARGET, Var_Value(ALLSRC, gn), gn);
574143694Sharti	}
5751590Srgrimes}
576138232Sharti
577144478Sharti/**
578144478Sharti * MakeStartJobs
5791590Srgrimes *	Start as many jobs as possible.
5801590Srgrimes *
5811590Srgrimes * Results:
5821590Srgrimes *	If the query flag was given to pmake, no job will be started,
5831590Srgrimes *	but as soon as an out-of-date target is found, this function
5841590Srgrimes *	returns TRUE. At all other times, this function returns FALSE.
5851590Srgrimes *
5861590Srgrimes * Side Effects:
5871590Srgrimes *	Nodes are removed from the toBeMade queue and job table slots
5881590Srgrimes *	are filled.
5891590Srgrimes */
5901590Srgrimesstatic Boolean
591138232ShartiMakeStartJobs(void)
5921590Srgrimes{
593144478Sharti	GNode	*gn;
5948874Srgrimes
595144478Sharti	while (!Lst_IsEmpty(&toBeMade) && !Job_Full()) {
596144478Sharti		gn = Lst_DeQueue(&toBeMade);
597144478Sharti		DEBUGF(MAKE, ("Examining %s...", gn->name));
5981590Srgrimes
599144478Sharti		/*
600144478Sharti		 * Make sure any and all predecessors that are going to be made,
601144478Sharti		 * have been.
602144478Sharti		 */
603144478Sharti		if (!Lst_IsEmpty(&gn->preds)) {
604144478Sharti			LstNode *ln;
6051590Srgrimes
606144478Sharti			for (ln = Lst_First(&gn->preds); ln != NULL;
607144478Sharti			    ln = Lst_Succ(ln)){
608144478Sharti				GNode	*pgn = Lst_Datum(ln);
609144478Sharti
610144478Sharti				if (pgn->make && pgn->made == UNMADE) {
611144478Sharti					DEBUGF(MAKE, ("predecessor %s not made "
612144478Sharti					    "yet.\n", pgn->name));
613144478Sharti					break;
614144478Sharti				}
615144478Sharti			}
616144478Sharti			/*
617144478Sharti			 * If ln isn't NULL, there's a predecessor as yet
618144478Sharti			 * unmade, so we just drop this node on the floor.
619144478Sharti			 * When the node in question has been made, it will
620144478Sharti			 * notice this node as being ready to make but as yet
621144478Sharti			 * unmade and will place the node on the queue.
622144478Sharti			 */
623144478Sharti			if (ln != NULL) {
624144478Sharti				continue;
625144478Sharti			}
6261590Srgrimes		}
6278874Srgrimes
628144478Sharti		numNodes--;
629144478Sharti		if (Make_OODate(gn)) {
630144478Sharti			DEBUGF(MAKE, ("out-of-date\n"));
631144478Sharti			if (queryFlag) {
632144478Sharti				return (TRUE);
633144478Sharti			}
634144478Sharti			Make_DoAllVar(gn);
635144478Sharti			Job_Make(gn);
636144478Sharti		} else {
637144478Sharti			DEBUGF(MAKE, ("up-to-date\n"));
638144478Sharti			gn->made = UPTODATE;
639144478Sharti			if (gn->type & OP_JOIN) {
640144478Sharti				/*
641144478Sharti				 * Even for an up-to-date .JOIN node, we need
642144478Sharti				 * it to have its context variables so
643144478Sharti				 * references to it get the correct value for
644144478Sharti				 * .TARGET when building up the context
645144478Sharti				 * variables of its parent(s)...
646144478Sharti				 */
647144478Sharti				Make_DoAllVar(gn);
648144478Sharti			}
6498874Srgrimes
650144478Sharti			Make_Update(gn);
651144478Sharti		}
6521590Srgrimes	}
653144478Sharti	return (FALSE);
6541590Srgrimes}
655138232Sharti
656144478Sharti/**
657144478Sharti * MakePrintStatus
6581590Srgrimes *	Print the status of a top-level node, viz. it being up-to-date
6591590Srgrimes *	already or not created due to an error in a lower level.
660143694Sharti *	Callback function for Make_Run via LST_FOREACH.  If gn->unmade is
661104696Sjmallett *	nonzero and that is meant to imply a cycle in the graph, then
662104696Sjmallett *	cycle is TRUE.
6631590Srgrimes *
6641590Srgrimes * Side Effects:
6651590Srgrimes *	A message may be printed.
6661590Srgrimes */
667143694Shartistatic void
668143694ShartiMakePrintStatus(GNode *gn, Boolean cycle)
6691590Srgrimes{
670144478Sharti	LstNode	*ln;
671138232Sharti
672144478Sharti	if (gn->made == UPTODATE) {
673144478Sharti		printf("`%s' is up to date.\n", gn->name);
674144478Sharti
675144478Sharti	} else if (gn->unmade != 0) {
676144478Sharti		if (cycle) {
677144478Sharti			/*
678144478Sharti			 * If printing cycles and came to one that has unmade
679144478Sharti			 * children, print out the cycle by recursing on its
680144478Sharti			 * children. Note a cycle like:
681144478Sharti			 *	a : b
682144478Sharti			 *	b : c
683144478Sharti			 *	c : b
684144478Sharti			 * will cause this to erroneously complain about a
685144478Sharti			 * being in the cycle, but this is a good approximation.
686144478Sharti			 */
687144478Sharti			if (gn->made == CYCLE) {
688144478Sharti				Error("Graph cycles through `%s'", gn->name);
689144478Sharti				gn->made = ENDCYCLE;
690144478Sharti				LST_FOREACH(ln, &gn->children)
691144478Sharti					MakePrintStatus(Lst_Datum(ln), TRUE);
692144478Sharti				gn->made = UNMADE;
693144478Sharti			} else if (gn->made != ENDCYCLE) {
694144478Sharti				gn->made = CYCLE;
695144478Sharti				LST_FOREACH(ln, &gn->children)
696144478Sharti					MakePrintStatus(Lst_Datum(ln), TRUE);
697144478Sharti			}
698144478Sharti		} else {
699144478Sharti			printf("`%s' not remade because of errors.\n",
700144478Sharti			    gn->name);
701144478Sharti		}
7021590Srgrimes	}
7031590Srgrimes}
704138232Sharti
705144478Sharti/**
706144478Sharti * Make_Run
7071590Srgrimes *	Initialize the nodes to remake and the list of nodes which are
7081590Srgrimes *	ready to be made by doing a breadth-first traversal of the graph
7091590Srgrimes *	starting from the nodes in the given list. Once this traversal
7101590Srgrimes *	is finished, all the 'leaves' of the graph are in the toBeMade
7111590Srgrimes *	queue.
7121590Srgrimes *	Using this queue and the Job module, work back up the graph,
7131590Srgrimes *	calling on MakeStartJobs to keep the job table as full as
7141590Srgrimes *	possible.
7151590Srgrimes *
7161590Srgrimes * Results:
7171590Srgrimes *	TRUE if work was done. FALSE otherwise.
7181590Srgrimes *
7191590Srgrimes * Side Effects:
7201590Srgrimes *	The make field of all nodes involved in the creation of the given
7211590Srgrimes *	targets is set to 1. The toBeMade list is set to contain all the
7221590Srgrimes *	'leaves' of these subgraphs.
7231590Srgrimes */
7241590SrgrimesBoolean
725138512ShartiMake_Run(Lst *targs)
7261590Srgrimes{
727144478Sharti	GNode	*gn;		/* a temporary pointer */
728144478Sharti	GNode	*cgn;
729144478Sharti	Lst	examine;	/* List of targets to examine */
730144478Sharti	LstNode	*ln;
7311590Srgrimes
732144478Sharti	Lst_Init(&examine);
733144478Sharti	Lst_Duplicate(&examine, targs, NOCOPY);
734144478Sharti	numNodes = 0;
7358874Srgrimes
736144478Sharti	/*
737144478Sharti	 * Make an initial downward pass over the graph, marking nodes to be
738144478Sharti	 * made as we go down. We call Suff_FindDeps to find where a node is and
739144478Sharti	 * to get some children for it if it has none and also has no commands.
740144478Sharti	 * If the node is a leaf, we stick it on the toBeMade queue to
741144478Sharti	 * be looked at in a minute, otherwise we add its children to our queue
742144478Sharti	 * and go on about our business.
743144478Sharti	 */
744144478Sharti	while (!Lst_IsEmpty(&examine)) {
745144478Sharti		gn = Lst_DeQueue(&examine);
7468874Srgrimes
747144478Sharti		if (!gn->make) {
748144478Sharti			gn->make = TRUE;
749144478Sharti			numNodes++;
7508874Srgrimes
751144478Sharti			/*
752144478Sharti			 * Apply any .USE rules before looking for implicit
753144478Sharti			 * dependencies to make sure everything has commands
754144478Sharti			 * that should...
755144478Sharti			 */
756144478Sharti			LST_FOREACH(ln, &gn->children)
757144478Sharti				if (Make_HandleUse(Lst_Datum(ln), gn))
758144478Sharti					break;
759143694Sharti
760144478Sharti			Suff_FindDeps(gn);
7611590Srgrimes
762144478Sharti			if (gn->unmade != 0) {
763144478Sharti				LST_FOREACH(ln, &gn->children) {
764144478Sharti					cgn = Lst_Datum(ln);
765144478Sharti					if (!cgn->make && !(cgn->type & OP_USE))
766144478Sharti						Lst_EnQueue(&examine, cgn);
767144478Sharti				}
768144478Sharti			} else {
769144478Sharti				Lst_EnQueue(&toBeMade, gn);
770144478Sharti			}
771143694Sharti		}
7721590Srgrimes	}
7738874Srgrimes
774144478Sharti	if (queryFlag) {
775144478Sharti		/*
776144478Sharti		 * We wouldn't do any work unless we could start some jobs in
777144478Sharti		 * the next loop... (we won't actually start any, of course,
778144478Sharti		 * this is just to see if any of the targets was out of date)
779144478Sharti		 */
780144478Sharti		return (MakeStartJobs());
781144478Sharti
782144478Sharti	} else {
783144478Sharti		/*
784144478Sharti		 * Initialization. At the moment, no jobs are running and
785144478Sharti		 * until some get started, nothing will happen since the
786144478Sharti		 * remaining upward traversal of the graph is performed by the
787144478Sharti		 * routines in job.c upon the finishing of a job. So we fill
788144478Sharti		 * the Job table as much as we can before going into our loop.
789144478Sharti		 */
790144478Sharti		MakeStartJobs();
791144478Sharti	}
792144478Sharti
7931590Srgrimes	/*
794144478Sharti	 * Main Loop: The idea here is that the ending of jobs will take
795144478Sharti	 * care of the maintenance of data structures and the waiting for output
796144478Sharti	 * will cause us to be idle most of the time while our children run as
797144478Sharti	 * much as possible. Because the job table is kept as full as possible,
798144478Sharti	 * the only time when it will be empty is when all the jobs which need
799144478Sharti	 * running have been run, so that is the end condition of this loop.
800144478Sharti	 * Note that the Job module will exit if there were any errors unless
801144478Sharti	 * the keepgoing flag was given.
8021590Srgrimes	 */
803144478Sharti	while (!Job_Empty()) {
804144478Sharti		Job_CatchOutput(!Lst_IsEmpty(&toBeMade));
805144478Sharti		Job_CatchChildren(!usePipes);
806144478Sharti		MakeStartJobs();
807144478Sharti	}
808144478Sharti
809186279Sfjoe	Job_Finish();
810144478Sharti
8111590Srgrimes	/*
812144478Sharti	 * Print the final status of each target. E.g. if it wasn't made
813144478Sharti	 * because some inferior reported an error.
8141590Srgrimes	 */
815144478Sharti	LST_FOREACH(ln, targs)
816186279Sfjoe		MakePrintStatus(Lst_Datum(ln), (makeErrors == 0) && (numNodes != 0));
8171590Srgrimes
818144478Sharti	return (TRUE);
8191590Srgrimes}
820