make.c revision 146580
1184610Salfred/*-
2184610Salfred * Copyright (c) 1988, 1989, 1990, 1993
3184610Salfred *	The Regents of the University of California.  All rights reserved.
4184610Salfred * Copyright (c) 1989 by Berkeley Softworks
5184610Salfred * All rights reserved.
6184610Salfred *
7184610Salfred * This code is derived from software contributed to Berkeley by
8184610Salfred * Adam de Boor.
9184610Salfred *
10184610Salfred * Redistribution and use in source and binary forms, with or without
11184610Salfred * modification, are permitted provided that the following conditions
12184610Salfred * are met:
13184610Salfred * 1. Redistributions of source code must retain the above copyright
14184610Salfred *    notice, this list of conditions and the following disclaimer.
15184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
16184610Salfred *    notice, this list of conditions and the following disclaimer in the
17184610Salfred *    documentation and/or other materials provided with the distribution.
18184610Salfred * 3. All advertising materials mentioning features or use of this software
19184610Salfred *    must display the following acknowledgement:
20184610Salfred *	This product includes software developed by the University of
21184610Salfred *	California, Berkeley and its contributors.
22184610Salfred * 4. Neither the name of the University nor the names of its contributors
23184610Salfred *    may be used to endorse or promote products derived from this software
24184610Salfred *    without specific prior written permission.
25184610Salfred *
26184610Salfred * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36184610Salfred * SUCH DAMAGE.
37184610Salfred *
38184610Salfred * @(#)make.c	8.1 (Berkeley) 6/6/93
39184610Salfred */
40184610Salfred
41184610Salfred#include <sys/cdefs.h>
42184610Salfred__FBSDID("$FreeBSD: head/usr.bin/make/make.c 146580 2005-05-24 15:58:35Z harti $");
43184610Salfred
44184610Salfred/*
45184610Salfred * make.c
46184610Salfred *	The functions which perform the examination of targets and
47194677Sthompsa *	their suitability for creation
48194677Sthompsa *
49194677Sthompsa * Interface:
50194677Sthompsa *	Make_Run	Initialize things for the module and recreate
51194677Sthompsa *			whatever needs recreating. Returns TRUE if
52194677Sthompsa *			work was (or would have been) done and FALSE
53194677Sthompsa *			otherwise.
54194677Sthompsa *
55194677Sthompsa *	Make_Update	Update all parents of a given child. Performs
56194677Sthompsa *			various bookkeeping chores like the updating
57194677Sthompsa *			of the cmtime field of the parent, filling
58194677Sthompsa *			of the IMPSRC context variable, etc. It will
59194677Sthompsa *			place the parent on the toBeMade queue if it should be.
60194677Sthompsa *
61194677Sthompsa *	Make_TimeStamp	Function to set the parent's cmtime field
62194677Sthompsa *			based on a child's modification time.
63194677Sthompsa *
64194677Sthompsa *	Make_DoAllVar	Set up the various local variables for a
65194677Sthompsa *			target, including the .ALLSRC variable, making
66194677Sthompsa *			sure that any variable that needs to exist
67194677Sthompsa *			at the very least has the empty value.
68194677Sthompsa *
69194677Sthompsa *	Make_OODate	Determine if a target is out-of-date.
70188746Sthompsa *
71194677Sthompsa *	Make_HandleUse	See if a child is a .USE node for a parent
72188942Sthompsa *			and perform the .USE actions if so.
73194677Sthompsa */
74184610Salfred
75184610Salfred#include <stdlib.h>
76188942Sthompsa
77184610Salfred#include "arch.h"
78194677Sthompsa#include "config.h"
79194677Sthompsa#include "dir.h"
80207077Sthompsa#include "globals.h"
81184610Salfred#include "GNode.h"
82184610Salfred#include "job.h"
83227309Sed#include "make.h"
84276701Shselasky#include "parse.h"
85184610Salfred#include "suff.h"
86184610Salfred#include "targ.h"
87184610Salfred#include "util.h"
88184610Salfred#include "var.h"
89184610Salfred
90184610Salfred/* The current fringe of the graph. These are nodes which await examination
91184610Salfred * by MakeOODate. It is added to by Make_Update and subtracted from by
92184610Salfred * MakeStartJobs */
93184610Salfredstatic Lst toBeMade = Lst_Initializer(toBeMade);
94184610Salfred
95184610Salfred/*
96184610Salfred * Number of nodes to be processed. If this is non-zero when Job_Empty()
97184610Salfred * returns TRUE, there's a cycle in the graph.
98192984Sthompsa */
99184610Salfredstatic int numNodes;
100184610Salfred
101192984Sthompsastatic Boolean MakeStartJobs(void);
102192984Sthompsa
103184610Salfred/**
104184610Salfred * Make_TimeStamp
105184610Salfred *	Set the cmtime field of a parent node based on the mtime stamp in its
106184610Salfred *	child. Called from MakeOODate via LST_FOREACH.
107184610Salfred *
108184610Salfred * Results:
109184610Salfred *	Always returns 0.
110184610Salfred *
111184610Salfred * Side Effects:
112184610Salfred *	The cmtime of the parent node will be changed if the mtime
113184610Salfred *	field of the child is greater than it.
114184610Salfred */
115184610Salfredint
116184610SalfredMake_TimeStamp(GNode *pgn, GNode *cgn)
117193045Sthompsa{
118193045Sthompsa
119193045Sthompsa	if (cgn->mtime > pgn->cmtime) {
120193045Sthompsa		pgn->cmtime = cgn->mtime;
121184610Salfred	}
122193045Sthompsa	return (0);
123193045Sthompsa}
124193045Sthompsa
125193045Sthompsa/**
126193045Sthompsa * Make_OODate
127193045Sthompsa *	See if a given node is out of date with respect to its sources.
128193045Sthompsa *	Used by Make_Run when deciding which nodes to place on the
129184610Salfred *	toBeMade queue initially and by Make_Update to screen out USE and
130192984Sthompsa *	EXEC nodes. In the latter case, however, any other sort of node
131184610Salfred *	must be considered out-of-date since at least one of its children
132184610Salfred *	will have been recreated.
133184610Salfred *
134184610Salfred * Results:
135184610Salfred *	TRUE if the node is out of date. FALSE otherwise.
136184610Salfred *
137184610Salfred * Side Effects:
138184610Salfred *	The mtime field of the node and the cmtime field of its parents
139184610Salfred *	will/may be changed.
140184610Salfred */
141192984SthompsaBoolean
142184610SalfredMake_OODate(GNode *gn)
143184610Salfred{
144184610Salfred	Boolean	oodate;
145184610Salfred	LstNode	*ln;
146190734Sthompsa
147190734Sthompsa	/*
148190734Sthompsa	 * Certain types of targets needn't even be sought as their datedness
149184610Salfred	 * doesn't depend on their modification time...
150184610Salfred	 */
151184610Salfred	if ((gn->type & (OP_JOIN | OP_USE | OP_EXEC)) == 0) {
152184610Salfred		Dir_MTime(gn);
153184610Salfred		if (gn->mtime != 0) {
154184610Salfred			DEBUGF(MAKE, ("modified %s...",
155190734Sthompsa			    Targ_FmtTime(gn->mtime)));
156190734Sthompsa		} else {
157190734Sthompsa			DEBUGF(MAKE, ("non-existent..."));
158184610Salfred		}
159184610Salfred	}
160184610Salfred
161184610Salfred	/*
162184610Salfred	 * A target is remade in one of the following circumstances:
163184610Salfred	 *	its modification time is smaller than that of its youngest child
164192984Sthompsa	 *	    and it would actually be run (has commands or type OP_NOP)
165190734Sthompsa	 *	it's the object of a force operator
166190734Sthompsa	 *	it has no children, was on the lhs of an operator and doesn't
167190734Sthompsa	 *	    exist already.
168184610Salfred	 *
169184610Salfred	 * Libraries are only considered out-of-date if the archive module says
170184610Salfred	 * they are.
171184610Salfred	 *
172184610Salfred	 * These weird rules are brought to you by Backward-Compatibility and
173184610Salfred	 * the strange people who wrote 'Make'.
174192984Sthompsa	 */
175190734Sthompsa	if (gn->type & OP_USE) {
176190734Sthompsa		/*
177190734Sthompsa		 * If the node is a USE node it is *never* out of date
178184610Salfred		 * no matter *what*.
179184610Salfred		 */
180184610Salfred		DEBUGF(MAKE, (".USE node..."));
181184610Salfred		oodate = FALSE;
182184610Salfred
183184610Salfred	} else if (gn->type & OP_LIB) {
184184610Salfred		DEBUGF(MAKE, ("library..."));
185184610Salfred
186184610Salfred		/*
187184610Salfred		 * always out of date if no children and :: target
188246128Ssbz		 */
189246128Ssbz		oodate = Arch_LibOODate(gn) ||
190184610Salfred		    ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
191184610Salfred
192184610Salfred	} else if (gn->type & OP_JOIN) {
193184610Salfred		/*
194184610Salfred		 * A target with the .JOIN attribute is only considered
195184610Salfred		 * out-of-date if any of its children was out-of-date.
196184610Salfred		 */
197184610Salfred		DEBUGF(MAKE, (".JOIN node..."));
198223511Shselasky		oodate = gn->childMade;
199223511Shselasky
200223511Shselasky	} else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
201223511Shselasky		/*
202223511Shselasky		 * A node which is the object of the force (!) operator or
203223511Shselasky		 * which has the .EXEC attribute is always considered
204292080Simp		 * out-of-date.
205292080Simp		 */
206292080Simp		if (gn->type & OP_FORCE) {
207292080Simp			DEBUGF(MAKE, ("! operator..."));
208292080Simp		} else if (gn->type & OP_PHONY) {
209184610Salfred			DEBUGF(MAKE, (".PHONY node..."));
210184610Salfred		} else {
211184610Salfred			DEBUGF(MAKE, (".EXEC node..."));
212192984Sthompsa		}
213184610Salfred		oodate = TRUE;
214223511Shselasky
215184610Salfred	} else if ((gn->mtime < gn->cmtime) ||
216223511Shselasky	    ((gn->cmtime == 0) &&
217184610Salfred	    ((gn->mtime==0) || (gn->type & OP_DOUBLEDEP)))) {
218223511Shselasky		/*
219223511Shselasky		 * A node whose modification time is less than that of its
220223511Shselasky		 * youngest child or that has no children (cmtime == 0) and
221223511Shselasky		 * either doesn't exist (mtime == 0) or was the object of a
222184610Salfred		 * :: operator is out-of-date. Why? Because that's the way
223184610Salfred		 * Make does it.
224184610Salfred		 */
225184610Salfred		if (gn->mtime < gn->cmtime) {
226184610Salfred			DEBUGF(MAKE, ("modified before source..."));
227192984Sthompsa		} else if (gn->mtime == 0) {
228184610Salfred			DEBUGF(MAKE, ("non-existent and no sources..."));
229184610Salfred		} else {
230184610Salfred			DEBUGF(MAKE, (":: operator and no sources..."));
231194228Sthompsa		}
232184610Salfred		oodate = TRUE;
233184610Salfred	} else
234184610Salfred		oodate = FALSE;
235184610Salfred
236184610Salfred	/*
237184610Salfred	 * If the target isn't out-of-date, the parents need to know its
238184610Salfred	 * modification time. Note that targets that appear to be out-of-date
239184610Salfred	 * but aren't, because they have no commands and aren't of type OP_NOP,
240194228Sthompsa	 * have their mtime stay below their children's mtime to keep parents
241184610Salfred	 * from thinking they're out-of-date.
242184610Salfred	 */
243184610Salfred	if (!oodate) {
244184610Salfred		LST_FOREACH(ln, &gn->parents)
245194228Sthompsa			if (Make_TimeStamp(Lst_Datum(ln), gn))
246184610Salfred				break;
247184610Salfred	}
248184610Salfred
249194228Sthompsa	return (oodate);
250184610Salfred}
251233774Shselasky
252189110Sthompsa/**
253184610Salfred * Make_HandleUse
254184610Salfred *	Function called by Make_Run and SuffApplyTransform on the downward
255184610Salfred *	pass to handle .USE and transformation nodes. A callback function
256184610Salfred *	for LST_FOREACH, it implements the .USE and transformation
257184610Salfred *	functionality by copying the node's commands, type flags
258184610Salfred *	and children to the parent node. Should be called before the
259184610Salfred *	children are enqueued to be looked at.
260184610Salfred *
261184610Salfred *	A .USE node is much like an explicit transformation rule, except
262184610Salfred *	its commands are always added to the target node, even if the
263184610Salfred *	target already has commands.
264194677Sthompsa *
265184610Salfred * Results:
266194677Sthompsa *	returns 0.
267192984Sthompsa *
268194677Sthompsa * Side Effects:
269184610Salfred *	Children and commands may be added to the parent and the parent's
270184610Salfred *	type may be changed.
271184610Salfred *
272184610Salfred *-----------------------------------------------------------------------
273184610Salfred */
274184610Salfredint
275194228SthompsaMake_HandleUse(GNode *cgn, GNode *pgn)
276184610Salfred{
277184610Salfred	GNode	*gn;	/* A child of the .USE node */
278194677Sthompsa	LstNode	*ln;	/* An element in the children list */
279194677Sthompsa
280194677Sthompsa	if (cgn->type & (OP_USE | OP_TRANSFORM)) {
281184610Salfred		if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
282194677Sthompsa			/*
283194228Sthompsa			 * .USE or transformation and target has no commands --
284184610Salfred			 * append the child's commands to the parent.
285184610Salfred			 */
286184610Salfred			Lst_Concat(&pgn->commands, &cgn->commands, LST_CONCNEW);
287184610Salfred		}
288194677Sthompsa
289184610Salfred		for (ln = Lst_First(&cgn->children); ln != NULL;
290184610Salfred		    ln = Lst_Succ(ln)) {
291194228Sthompsa			gn = Lst_Datum(ln);
292184610Salfred
293184610Salfred			if (Lst_Member(&pgn->children, gn) == NULL) {
294184610Salfred				Lst_AtEnd(&pgn->children, gn);
295184610Salfred				Lst_AtEnd(&gn->parents, pgn);
296184610Salfred				pgn->unmade += 1;
297184610Salfred			}
298194677Sthompsa		}
299184610Salfred
300194677Sthompsa		pgn->type |= cgn->type & ~(OP_OPMASK | OP_USE | OP_TRANSFORM);
301192984Sthompsa
302184610Salfred		/*
303194228Sthompsa		 * This child node is now "made", so we decrement the count of
304184610Salfred		 * unmade children in the parent... We also remove the child
305184610Salfred		 * from the parent's list to accurately reflect the number of
306194228Sthompsa		 * decent children the parent has. This is used by Make_Run to
307184610Salfred		 * decide whether to queue the parent or examine its children...
308184610Salfred		 */
309184610Salfred		if (cgn->type & OP_USE) {
310184610Salfred			pgn->unmade--;
311194677Sthompsa		}
312184610Salfred	}
313194677Sthompsa	return (0);
314192984Sthompsa}
315194677Sthompsa
316194677Sthompsa/**
317184610Salfred * Make_Update
318194677Sthompsa *	Perform update on the parents of a node. Used by JobFinish once
319194677Sthompsa *	a node has been dealt with and by MakeStartJobs if it finds an
320184610Salfred *	up-to-date node.
321184610Salfred *
322194677Sthompsa * Results:
323194677Sthompsa *	Always returns 0
324184610Salfred *
325184610Salfred * Side Effects:
326184610Salfred *	The unmade field of pgn is decremented and pgn may be placed on
327194228Sthompsa *	the toBeMade queue if this field becomes 0.
328184610Salfred *
329184610Salfred * 	If the child was made, the parent's childMade field will be set true
330194228Sthompsa *	and its cmtime set to now.
331194677Sthompsa *
332194228Sthompsa *	If the child wasn't made, the cmtime field of the parent will be
333184610Salfred *	altered if the child's mtime is big enough.
334184610Salfred *
335184610Salfred *	Finally, if the child is the implied source for the parent, the
336184610Salfred *	parent's IMPSRC variable is set appropriately.
337194677Sthompsa */
338184610Salfredvoid
339184610SalfredMake_Update(GNode *cgn)
340194228Sthompsa{
341184610Salfred	GNode	*pgn;	/* the parent node */
342184610Salfred	char	*cname;	/* the child's name */
343184610Salfred	LstNode	*ln;	/* Element in parents and iParents lists */
344184610Salfred	char	*cpref;
345184610Salfred
346184610Salfred	cname = Var_Value(TARGET, cgn);
347194677Sthompsa
348184610Salfred	/*
349194677Sthompsa	 * If the child was actually made, see what its modification time is
350192984Sthompsa	 * now -- some rules won't actually update the file. If the file still
351184610Salfred	 * doesn't exist, make its mtime now.
352194228Sthompsa	 */
353184610Salfred	if (cgn->made != UPTODATE) {
354184610Salfred#ifndef RECHECK
355194228Sthompsa		/*
356184610Salfred		 * We can't re-stat the thing, but we can at least take care
357184610Salfred		 * of rules where a target depends on a source that actually
358184610Salfred		 * creates the target, but only if it has changed, e.g.
359184610Salfred		 *
360192984Sthompsa		 * parse.h : parse.o
361184610Salfred		 *
362194677Sthompsa		 * parse.o : parse.y
363184610Salfred		 *  	yacc -d parse.y
364194228Sthompsa		 *  	cc -c y.tab.c
365184610Salfred		 *  	mv y.tab.o parse.o
366184610Salfred		 *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
367184610Salfred		 *
368192984Sthompsa		 * In this case, if the definitions produced by yacc haven't
369184610Salfred		 * changed from before, parse.h won't have been updated and
370194677Sthompsa		 * cgn->mtime will reflect the current modification time for
371184610Salfred		 * parse.h. This is something of a kludge, I admit, but it's a
372194228Sthompsa		 * useful one..
373194228Sthompsa		 * XXX: People like to use a rule like
374184610Salfred		 *
375184610Salfred		 * FRC:
376184610Salfred		 *
377192984Sthompsa		 * To force things that depend on FRC to be made, so we have to
378184610Salfred		 * check for gn->children being empty as well...
379194677Sthompsa		 */
380184610Salfred		if (!Lst_IsEmpty(&cgn->commands) ||
381194228Sthompsa		    Lst_IsEmpty(&cgn->children)) {
382184610Salfred			cgn->mtime = now;
383184610Salfred		}
384184610Salfred	#else
385192984Sthompsa		/*
386184610Salfred		 * This is what Make does and it's actually a good thing, as it
387194677Sthompsa		 * allows rules like
388184610Salfred		 *
389194228Sthompsa		 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
390194228Sthompsa		 *
391184610Salfred		 * to function as intended. Unfortunately, thanks to the
392184610Salfred		 * stateless nature of NFS (by which I mean the loose coupling
393184610Salfred		 * of two clients using the same file from a common server),
394192984Sthompsa		 * there are times when the modification time of a file created
395184610Salfred		 * on a remote machine will not be modified before the local
396194677Sthompsa		 * stat() implied by the Dir_MTime occurs, thus leading us to
397184610Salfred		 * believe that the file is unchanged, wreaking havoc with
398184610Salfred		 * files that depend on this one.
399184610Salfred		 *
400184610Salfred		 * I have decided it is better to make too much than to make too
401184610Salfred		 * little, so this stuff is commented out unless you're sure
402184610Salfred		 * it's ok.
403184610Salfred		 * -- ardeb 1/12/88
404194228Sthompsa		 */
405194677Sthompsa		/*
406184610Salfred		 * Christos, 4/9/92: If we are  saving commands pretend that
407184610Salfred		 * the target is made now. Otherwise archives with ... rules
408184610Salfred		 * don't work!
409184610Salfred		 */
410184610Salfred		if (noExecute || (cgn->type & OP_SAVE_CMDS) ||
411184610Salfred		    Dir_MTime(cgn) == 0) {
412184610Salfred			cgn->mtime = now;
413184610Salfred		}
414194228Sthompsa		DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(cgn->mtime)));
415194677Sthompsa#endif
416184610Salfred	}
417184610Salfred
418184610Salfred	for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Succ(ln)) {
419184610Salfred		pgn = Lst_Datum(ln);
420184610Salfred		if (pgn->make) {
421184610Salfred			pgn->unmade -= 1;
422184610Salfred
423184610Salfred			if (!(cgn->type & (OP_EXEC | OP_USE))) {
424192984Sthompsa				if (cgn->made == MADE) {
425184610Salfred					pgn->childMade = TRUE;
426184610Salfred					if (pgn->cmtime < cgn->mtime) {
427194228Sthompsa						pgn->cmtime = cgn->mtime;
428184610Salfred					}
429184610Salfred				} else {
430184610Salfred					Make_TimeStamp(pgn, cgn);
431184610Salfred				}
432192984Sthompsa			}
433189110Sthompsa			if (pgn->unmade == 0) {
434184610Salfred				/*
435192984Sthompsa				 * Queue the node up -- any unmade predecessors
436184610Salfred				 * will be dealt with in MakeStartJobs.
437184610Salfred				 */
438184610Salfred				Lst_EnQueue(&toBeMade, pgn);
439184610Salfred			} else if (pgn->unmade < 0) {
440184610Salfred				Error("Graph cycles through %s", pgn->name);
441184610Salfred			}
442184610Salfred		}
443184610Salfred	}
444184610Salfred
445227461Shselasky	/*
446184610Salfred	 * Deal with successor nodes. If any is marked for making and has an
447184610Salfred	 * unmade count of 0, has not been made and isn't in the examination
448184610Salfred	 * queue, it means we need to place it in the queue as it restrained
449184610Salfred	 * itself before.
450184610Salfred	 */
451184610Salfred	for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Succ(ln)) {
452184610Salfred		GNode	*succ = Lst_Datum(ln);
453184610Salfred
454184610Salfred		if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
455184610Salfred		    Lst_Member(&toBeMade, succ) == NULL) {
456227461Shselasky			Lst_EnQueue(&toBeMade, succ);
457184610Salfred		}
458184610Salfred	}
459184610Salfred
460184610Salfred	/*
461184610Salfred	 * Set the .PREFIX and .IMPSRC variables for all the implied parents
462184610Salfred	 * of this node.
463184610Salfred	 */
464184610Salfred	cpref = Var_Value(PREFIX, cgn);
465184610Salfred	for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Succ(ln)) {
466184610Salfred		pgn = Lst_Datum(ln);
467184610Salfred		if (pgn->make) {
468184610Salfred			Var_Set(IMPSRC, cname, pgn);
469184610Salfred			Var_Set(PREFIX, cpref, pgn);
470184610Salfred		}
471184610Salfred	}
472184610Salfred}
473184610Salfred
474184610Salfred/**
475184610Salfred * Make_DoAllVar
476184610Salfred *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
477184610Salfred *	done separately, rather than while traversing the graph. This is
478184610Salfred *	because Make defined OODATE to contain all sources whose modification
479184610Salfred *	times were later than that of the target, *not* those sources that
480184610Salfred *	were out-of-date. Since in both compatibility and native modes,
481184610Salfred *	the modification time of the parent isn't found until the child
482184610Salfred *	has been dealt with, we have to wait until now to fill in the
483184610Salfred *	variable. As for ALLSRC, the ordering is important and not
484184610Salfred *	guaranteed when in native mode, so it must be set here, too.
485184610Salfred *
486184610Salfred * Side Effects:
487184610Salfred *	The ALLSRC and OODATE variables of the given node is filled in.
488184610Salfred *	If the node is a .JOIN node, its TARGET variable will be set to
489184610Salfred * 	match its ALLSRC variable.
490194228Sthompsa */
491184610Salfredvoid
492194228SthompsaMake_DoAllVar(GNode *gn)
493184610Salfred{
494184610Salfred	LstNode	*ln;
495184610Salfred	GNode	*cgn;
496184610Salfred	char	*child;
497184610Salfred
498	LST_FOREACH(ln, &gn->children) {
499		/*
500		 * Add the child's name to the ALLSRC and OODATE variables of
501		 * the given node. The child is added only if it has not been
502		 * given the .EXEC, .USE or .INVISIBLE attributes. .EXEC and
503		 * .USE children are very rarely going to be files, so...
504		 *
505		 * A child is added to the OODATE variable if its modification
506		 * time is later than that of its parent, as defined by Make,
507		 * except if the parent is a .JOIN node. In that case, it is
508		 * only added to the OODATE variable if it was actually made
509		 * (since .JOIN nodes don't have modification times, the
510		 * comparison is rather unfair...).
511		 */
512		cgn = Lst_Datum(ln);
513
514		if ((cgn->type & (OP_EXEC | OP_USE | OP_INVISIBLE)) == 0) {
515			if (OP_NOP(cgn->type)) {
516				/*
517				 * this node is only source; use the specific
518				 * pathname for it
519				 */
520				child = cgn->path ? cgn->path : cgn->name;
521			} else
522				child = Var_Value(TARGET, cgn);
523			Var_Append(ALLSRC, child, gn);
524			if (gn->type & OP_JOIN) {
525				if (cgn->made == MADE) {
526					Var_Append(OODATE, child, gn);
527				}
528			} else if (gn->mtime < cgn->mtime ||
529			    (cgn->mtime >= now && cgn->made == MADE)) {
530				/*
531				 * It goes in the OODATE variable if the parent
532				 * is younger than the child or if the child has
533				 * been modified more recently than the start of
534				 * the make. This is to keep pmake from getting
535				 * confused if something else updates the parent
536				 * after the make starts (shouldn't happen, I
537				 * know, but sometimes it does). In such a case,
538				 * if we've updated the kid, the parent is
539				 * likely to have a modification time later than
540				 * that of the kid and anything that relies on
541				 * the OODATE variable will be hosed.
542				 *
543				 * XXX: This will cause all made children to
544				 * go in the OODATE variable, even if they're
545				 * not touched, if RECHECK isn't defined, since
546				 * cgn->mtime is set to now in Make_Update.
547				 * According to some people, this is good...
548				 */
549				Var_Append(OODATE, child, gn);
550			}
551		}
552	}
553
554	if (!Var_Exists (OODATE, gn)) {
555		Var_Set(OODATE, "", gn);
556	}
557	if (!Var_Exists (ALLSRC, gn)) {
558		Var_Set(ALLSRC, "", gn);
559	}
560
561	if (gn->type & OP_JOIN) {
562		Var_Set(TARGET, Var_Value(ALLSRC, gn), gn);
563	}
564}
565
566/**
567 * MakeStartJobs
568 *	Start as many jobs as possible.
569 *
570 * Results:
571 *	If the query flag was given to pmake, no job will be started,
572 *	but as soon as an out-of-date target is found, this function
573 *	returns TRUE. At all other times, this function returns FALSE.
574 *
575 * Side Effects:
576 *	Nodes are removed from the toBeMade queue and job table slots
577 *	are filled.
578 */
579static Boolean
580MakeStartJobs(void)
581{
582	GNode	*gn;
583
584	while (!Lst_IsEmpty(&toBeMade) && !Job_Full()) {
585		gn = Lst_DeQueue(&toBeMade);
586		DEBUGF(MAKE, ("Examining %s...", gn->name));
587
588		/*
589		 * Make sure any and all predecessors that are going to be made,
590		 * have been.
591		 */
592		if (!Lst_IsEmpty(&gn->preds)) {
593			LstNode *ln;
594
595			for (ln = Lst_First(&gn->preds); ln != NULL;
596			    ln = Lst_Succ(ln)){
597				GNode	*pgn = Lst_Datum(ln);
598
599				if (pgn->make && pgn->made == UNMADE) {
600					DEBUGF(MAKE, ("predecessor %s not made "
601					    "yet.\n", pgn->name));
602					break;
603				}
604			}
605			/*
606			 * If ln isn't NULL, there's a predecessor as yet
607			 * unmade, so we just drop this node on the floor.
608			 * When the node in question has been made, it will
609			 * notice this node as being ready to make but as yet
610			 * unmade and will place the node on the queue.
611			 */
612			if (ln != NULL) {
613				continue;
614			}
615		}
616
617		numNodes--;
618		if (Make_OODate(gn)) {
619			DEBUGF(MAKE, ("out-of-date\n"));
620			if (queryFlag) {
621				return (TRUE);
622			}
623			Make_DoAllVar(gn);
624			Job_Make(gn);
625		} else {
626			DEBUGF(MAKE, ("up-to-date\n"));
627			gn->made = UPTODATE;
628			if (gn->type & OP_JOIN) {
629				/*
630				 * Even for an up-to-date .JOIN node, we need
631				 * it to have its context variables so
632				 * references to it get the correct value for
633				 * .TARGET when building up the context
634				 * variables of its parent(s)...
635				 */
636				Make_DoAllVar(gn);
637			}
638
639			Make_Update(gn);
640		}
641	}
642	return (FALSE);
643}
644
645/**
646 * MakePrintStatus
647 *	Print the status of a top-level node, viz. it being up-to-date
648 *	already or not created due to an error in a lower level.
649 *	Callback function for Make_Run via LST_FOREACH.  If gn->unmade is
650 *	nonzero and that is meant to imply a cycle in the graph, then
651 *	cycle is TRUE.
652 *
653 * Side Effects:
654 *	A message may be printed.
655 */
656static void
657MakePrintStatus(GNode *gn, Boolean cycle)
658{
659	LstNode	*ln;
660
661	if (gn->made == UPTODATE) {
662		printf("`%s' is up to date.\n", gn->name);
663
664	} else if (gn->unmade != 0) {
665		if (cycle) {
666			/*
667			 * If printing cycles and came to one that has unmade
668			 * children, print out the cycle by recursing on its
669			 * children. Note a cycle like:
670			 *	a : b
671			 *	b : c
672			 *	c : b
673			 * will cause this to erroneously complain about a
674			 * being in the cycle, but this is a good approximation.
675			 */
676			if (gn->made == CYCLE) {
677				Error("Graph cycles through `%s'", gn->name);
678				gn->made = ENDCYCLE;
679				LST_FOREACH(ln, &gn->children)
680					MakePrintStatus(Lst_Datum(ln), TRUE);
681				gn->made = UNMADE;
682			} else if (gn->made != ENDCYCLE) {
683				gn->made = CYCLE;
684				LST_FOREACH(ln, &gn->children)
685					MakePrintStatus(Lst_Datum(ln), TRUE);
686			}
687		} else {
688			printf("`%s' not remade because of errors.\n",
689			    gn->name);
690		}
691	}
692}
693
694/**
695 * Make_Run
696 *	Initialize the nodes to remake and the list of nodes which are
697 *	ready to be made by doing a breadth-first traversal of the graph
698 *	starting from the nodes in the given list. Once this traversal
699 *	is finished, all the 'leaves' of the graph are in the toBeMade
700 *	queue.
701 *	Using this queue and the Job module, work back up the graph,
702 *	calling on MakeStartJobs to keep the job table as full as
703 *	possible.
704 *
705 * Results:
706 *	TRUE if work was done. FALSE otherwise.
707 *
708 * Side Effects:
709 *	The make field of all nodes involved in the creation of the given
710 *	targets is set to 1. The toBeMade list is set to contain all the
711 *	'leaves' of these subgraphs.
712 */
713Boolean
714Make_Run(Lst *targs)
715{
716	GNode	*gn;		/* a temporary pointer */
717	GNode	*cgn;
718	Lst	examine;	/* List of targets to examine */
719	int	errors;		/* Number of errors the Job module reports */
720	LstNode	*ln;
721
722	Lst_Init(&examine);
723	Lst_Duplicate(&examine, targs, NOCOPY);
724	numNodes = 0;
725
726	/*
727	 * Make an initial downward pass over the graph, marking nodes to be
728	 * made as we go down. We call Suff_FindDeps to find where a node is and
729	 * to get some children for it if it has none and also has no commands.
730	 * If the node is a leaf, we stick it on the toBeMade queue to
731	 * be looked at in a minute, otherwise we add its children to our queue
732	 * and go on about our business.
733	 */
734	while (!Lst_IsEmpty(&examine)) {
735		gn = Lst_DeQueue(&examine);
736
737		if (!gn->make) {
738			gn->make = TRUE;
739			numNodes++;
740
741			/*
742			 * Apply any .USE rules before looking for implicit
743			 * dependencies to make sure everything has commands
744			 * that should...
745			 */
746			LST_FOREACH(ln, &gn->children)
747				if (Make_HandleUse(Lst_Datum(ln), gn))
748					break;
749
750			Suff_FindDeps(gn);
751
752			if (gn->unmade != 0) {
753				LST_FOREACH(ln, &gn->children) {
754					cgn = Lst_Datum(ln);
755					if (!cgn->make && !(cgn->type & OP_USE))
756						Lst_EnQueue(&examine, cgn);
757				}
758			} else {
759				Lst_EnQueue(&toBeMade, gn);
760			}
761		}
762	}
763
764	if (queryFlag) {
765		/*
766		 * We wouldn't do any work unless we could start some jobs in
767		 * the next loop... (we won't actually start any, of course,
768		 * this is just to see if any of the targets was out of date)
769		 */
770		return (MakeStartJobs());
771
772	} else {
773		/*
774		 * Initialization. At the moment, no jobs are running and
775		 * until some get started, nothing will happen since the
776		 * remaining upward traversal of the graph is performed by the
777		 * routines in job.c upon the finishing of a job. So we fill
778		 * the Job table as much as we can before going into our loop.
779		 */
780		MakeStartJobs();
781	}
782
783	/*
784	 * Main Loop: The idea here is that the ending of jobs will take
785	 * care of the maintenance of data structures and the waiting for output
786	 * will cause us to be idle most of the time while our children run as
787	 * much as possible. Because the job table is kept as full as possible,
788	 * the only time when it will be empty is when all the jobs which need
789	 * running have been run, so that is the end condition of this loop.
790	 * Note that the Job module will exit if there were any errors unless
791	 * the keepgoing flag was given.
792	 */
793	while (!Job_Empty()) {
794		Job_CatchOutput(!Lst_IsEmpty(&toBeMade));
795		Job_CatchChildren(!usePipes);
796		MakeStartJobs();
797	}
798
799	errors = Job_Finish();
800
801	/*
802	 * Print the final status of each target. E.g. if it wasn't made
803	 * because some inferior reported an error.
804	 */
805	errors = ((errors == 0) && (numNodes != 0));
806	LST_FOREACH(ln, targs)
807		MakePrintStatus(Lst_Datum(ln), errors);
808
809	return (TRUE);
810}
811