make.c revision 144478
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: head/usr.bin/make/make.c 144478 2005-04-01 13:02:17Z harti $");
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 <stdlib.h>
761590Srgrimes
77141104Sharti#include "arch.h"
78141104Sharti#include "config.h"
79141104Sharti#include "dir.h"
80141104Sharti#include "globals.h"
81141104Sharti#include "GNode.h"
82141104Sharti#include "job.h"
83141104Sharti#include "make.h"
84141104Sharti#include "suff.h"
85141104Sharti#include "targ.h"
86141104Sharti#include "util.h"
87141104Sharti#include "var.h"
88141104Sharti
89138916Sharti/* The current fringe of the graph. These are nodes which await examination
90138916Sharti * by MakeOODate. It is added to by Make_Update and subtracted from by
91138916Sharti * MakeStartJobs */
92138916Shartistatic Lst toBeMade = Lst_Initializer(toBeMade);
93138916Sharti
94144478Sharti/*
95144478Sharti * Number of nodes to be processed. If this is non-zero when Job_Empty()
96144478Sharti * returns TRUE, there's a cycle in the graph.
97144478Sharti */
98144478Shartistatic int numNodes;
991590Srgrimes
10092921Simpstatic Boolean MakeStartJobs(void);
101138232Sharti
102144478Sharti/**
103144478Sharti * Make_TimeStamp
1041590Srgrimes *	Set the cmtime field of a parent node based on the mtime stamp in its
105143694Sharti *	child. Called from MakeOODate via LST_FOREACH.
1061590Srgrimes *
1071590Srgrimes * Results:
1088874Srgrimes *	Always returns 0.
1091590Srgrimes *
1101590Srgrimes * Side Effects:
1111590Srgrimes *	The cmtime of the parent node will be changed if the mtime
1121590Srgrimes *	field of the child is greater than it.
1131590Srgrimes */
1141590Srgrimesint
115138232ShartiMake_TimeStamp(GNode *pgn, GNode *cgn)
1161590Srgrimes{
117138232Sharti
118144478Sharti	if (cgn->mtime > pgn->cmtime) {
119144478Sharti		pgn->cmtime = cgn->mtime;
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		oodate = TRUE;
213144478Sharti
214144478Sharti	} else if ((gn->mtime < gn->cmtime) ||
215144478Sharti	    ((gn->cmtime == 0) &&
216144478Sharti	    ((gn->mtime==0) || (gn->type & OP_DOUBLEDEP)))) {
217144478Sharti		/*
218144478Sharti		 * A node whose modification time is less than that of its
219144478Sharti		 * youngest child or that has no children (cmtime == 0) and
220144478Sharti		 * either doesn't exist (mtime == 0) or was the object of a
221144478Sharti		 * :: operator is out-of-date. Why? Because that's the way
222144478Sharti		 * Make does it.
223144478Sharti		 */
224144478Sharti		if (gn->mtime < gn->cmtime) {
225144478Sharti			DEBUGF(MAKE, ("modified before source..."));
226144478Sharti		} else if (gn->mtime == 0) {
227144478Sharti			DEBUGF(MAKE, ("non-existent and no sources..."));
228144478Sharti		} else {
229144478Sharti			DEBUGF(MAKE, (":: operator and no sources..."));
230144478Sharti		}
231144478Sharti		oodate = TRUE;
232144478Sharti	} else
233144478Sharti		oodate = FALSE;
234144478Sharti
2351590Srgrimes	/*
236144478Sharti	 * If the target isn't out-of-date, the parents need to know its
237144478Sharti	 * modification time. Note that targets that appear to be out-of-date
238144478Sharti	 * but aren't, because they have no commands and aren't of type OP_NOP,
239144478Sharti	 * have their mtime stay below their children's mtime to keep parents
240144478Sharti	 * from thinking they're out-of-date.
2411590Srgrimes	 */
242144478Sharti	if (!oodate) {
243144478Sharti		LST_FOREACH(ln, &gn->parents)
244144478Sharti			if (Make_TimeStamp(Lst_Datum(ln), gn))
245144478Sharti				break;
2461590Srgrimes	}
2471590Srgrimes
248144478Sharti	return (oodate);
2491590Srgrimes}
250138232Sharti
251144478Sharti/**
252144478Sharti * Make_HandleUse
2531590Srgrimes *	Function called by Make_Run and SuffApplyTransform on the downward
2541590Srgrimes *	pass to handle .USE and transformation nodes. A callback function
255143694Sharti *	for LST_FOREACH, it implements the .USE and transformation
2561590Srgrimes *	functionality by copying the node's commands, type flags
2571590Srgrimes *	and children to the parent node. Should be called before the
258143694Sharti *	children are enqueued to be looked at.
2591590Srgrimes *
2601590Srgrimes *	A .USE node is much like an explicit transformation rule, except
2611590Srgrimes *	its commands are always added to the target node, even if the
2621590Srgrimes *	target already has commands.
2631590Srgrimes *
2641590Srgrimes * Results:
2651590Srgrimes *	returns 0.
2661590Srgrimes *
2671590Srgrimes * Side Effects:
2681590Srgrimes *	Children and commands may be added to the parent and the parent's
2691590Srgrimes *	type may be changed.
2701590Srgrimes *
2711590Srgrimes *-----------------------------------------------------------------------
2721590Srgrimes */
2731590Srgrimesint
274138232ShartiMake_HandleUse(GNode *cgn, GNode *pgn)
2751590Srgrimes{
276144478Sharti	GNode	*gn;	/* A child of the .USE node */
277144478Sharti	LstNode	*ln;	/* An element in the children list */
2781590Srgrimes
279144478Sharti	if (cgn->type & (OP_USE | OP_TRANSFORM)) {
280144478Sharti		if ((cgn->type & OP_USE) || Lst_IsEmpty(&pgn->commands)) {
281144478Sharti			/*
282144478Sharti			 * .USE or transformation and target has no commands --
283144478Sharti			 * append the child's commands to the parent.
284144478Sharti			 */
285144478Sharti			Lst_Concat(&pgn->commands, &cgn->commands, LST_CONCNEW);
286144478Sharti		}
2878874Srgrimes
288144478Sharti		for (ln = Lst_First(&cgn->children); ln != NULL;
289144478Sharti		    ln = Lst_Succ(ln)) {
290144478Sharti			gn = Lst_Datum(ln);
2911590Srgrimes
292144478Sharti			if (Lst_Member(&pgn->children, gn) == NULL) {
293144478Sharti				Lst_AtEnd(&pgn->children, gn);
294144478Sharti				Lst_AtEnd(&gn->parents, pgn);
295144478Sharti				pgn->unmade += 1;
296144478Sharti			}
297144478Sharti		}
2988874Srgrimes
299144478Sharti		pgn->type |= cgn->type & ~(OP_OPMASK | OP_USE | OP_TRANSFORM);
3001590Srgrimes
301144478Sharti		/*
302144478Sharti		 * This child node is now "made", so we decrement the count of
303144478Sharti		 * unmade children in the parent... We also remove the child
304144478Sharti		 * from the parent's list to accurately reflect the number of
305144478Sharti		 * decent children the parent has. This is used by Make_Run to
306144478Sharti		 * decide whether to queue the parent or examine its children...
307144478Sharti		 */
308144478Sharti		if (cgn->type & OP_USE) {
309144478Sharti			pgn->unmade--;
310144478Sharti		}
3111590Srgrimes	}
312144478Sharti	return (0);
3131590Srgrimes}
314138232Sharti
315144478Sharti/**
316144478Sharti * Make_Update
3171590Srgrimes *	Perform update on the parents of a node. Used by JobFinish once
3181590Srgrimes *	a node has been dealt with and by MakeStartJobs if it finds an
3198874Srgrimes *	up-to-date node.
3201590Srgrimes *
3211590Srgrimes * Results:
3221590Srgrimes *	Always returns 0
3231590Srgrimes *
3241590Srgrimes * Side Effects:
3251590Srgrimes *	The unmade field of pgn is decremented and pgn may be placed on
3261590Srgrimes *	the toBeMade queue if this field becomes 0.
3271590Srgrimes *
3281590Srgrimes * 	If the child was made, the parent's childMade field will be set true
3291590Srgrimes *	and its cmtime set to now.
3301590Srgrimes *
3311590Srgrimes *	If the child wasn't made, the cmtime field of the parent will be
3321590Srgrimes *	altered if the child's mtime is big enough.
3331590Srgrimes *
3341590Srgrimes *	Finally, if the child is the implied source for the parent, the
3351590Srgrimes *	parent's IMPSRC variable is set appropriately.
3361590Srgrimes */
3371590Srgrimesvoid
338138232ShartiMake_Update(GNode *cgn)
3391590Srgrimes{
340144478Sharti	GNode	*pgn;	/* the parent node */
341144478Sharti	char	*cname;	/* the child's name */
342144478Sharti	LstNode	*ln;	/* Element in parents and iParents lists */
343144478Sharti	char	*p1;
344144478Sharti	char	*ptr;
345144478Sharti	char	*cpref;
3461590Srgrimes
347144478Sharti	cname = Var_Value(TARGET, cgn, &p1);
348144478Sharti	free(p1);
3491590Srgrimes
3501590Srgrimes	/*
351144478Sharti	 * If the child was actually made, see what its modification time is
352144478Sharti	 * now -- some rules won't actually update the file. If the file still
353144478Sharti	 * doesn't exist, make its mtime now.
3541590Srgrimes	 */
355144478Sharti	if (cgn->made != UPTODATE) {
356144478Sharti#ifndef RECHECK
357144478Sharti		/*
358144478Sharti		 * We can't re-stat the thing, but we can at least take care
359144478Sharti		 * of rules where a target depends on a source that actually
360144478Sharti		 * creates the target, but only if it has changed, e.g.
361144478Sharti		 *
362144478Sharti		 * parse.h : parse.o
363144478Sharti		 *
364144478Sharti		 * parse.o : parse.y
365144478Sharti		 *  	yacc -d parse.y
366144478Sharti		 *  	cc -c y.tab.c
367144478Sharti		 *  	mv y.tab.o parse.o
368144478Sharti		 *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
369144478Sharti		 *
370144478Sharti		 * In this case, if the definitions produced by yacc haven't
371144478Sharti		 * changed from before, parse.h won't have been updated and
372144478Sharti		 * cgn->mtime will reflect the current modification time for
373144478Sharti		 * parse.h. This is something of a kludge, I admit, but it's a
374144478Sharti		 * useful one..
375144478Sharti		 * XXX: People like to use a rule like
376144478Sharti		 *
377144478Sharti		 * FRC:
378144478Sharti		 *
379144478Sharti		 * To force things that depend on FRC to be made, so we have to
380144478Sharti		 * check for gn->children being empty as well...
381144478Sharti		 */
382144478Sharti		if (!Lst_IsEmpty(&cgn->commands) ||
383144478Sharti		    Lst_IsEmpty(&cgn->children)) {
384144478Sharti			cgn->mtime = now;
385144478Sharti		}
386144478Sharti	#else
387144478Sharti		/*
388144478Sharti		 * This is what Make does and it's actually a good thing, as it
389144478Sharti		 * allows rules like
390144478Sharti		 *
391144478Sharti		 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
392144478Sharti		 *
393144478Sharti		 * to function as intended. Unfortunately, thanks to the
394144478Sharti		 * stateless nature of NFS (by which I mean the loose coupling
395144478Sharti		 * of two clients using the same file from a common server),
396144478Sharti		 * there are times when the modification time of a file created
397144478Sharti		 * on a remote machine will not be modified before the local
398144478Sharti		 * stat() implied by the Dir_MTime occurs, thus leading us to
399144478Sharti		 * believe that the file is unchanged, wreaking havoc with
400144478Sharti		 * files that depend on this one.
401144478Sharti		 *
402144478Sharti		 * I have decided it is better to make too much than to make too
403144478Sharti		 * little, so this stuff is commented out unless you're sure
404144478Sharti		 * it's ok.
405144478Sharti		 * -- ardeb 1/12/88
406144478Sharti		 */
407144478Sharti		/*
408144478Sharti		 * Christos, 4/9/92: If we are  saving commands pretend that
409144478Sharti		 * the target is made now. Otherwise archives with ... rules
410144478Sharti		 * don't work!
411144478Sharti		 */
412144478Sharti		if (noExecute || (cgn->type & OP_SAVE_CMDS) ||
413144478Sharti		    Dir_MTime(cgn) == 0) {
414144478Sharti			cgn->mtime = now;
415144478Sharti		}
416144478Sharti		DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(cgn->mtime)));
417144478Sharti#endif
4181590Srgrimes	}
4198874Srgrimes
420144478Sharti	for (ln = Lst_First(&cgn->parents); ln != NULL; ln = Lst_Succ(ln)) {
421144478Sharti		pgn = Lst_Datum(ln);
422144478Sharti		if (pgn->make) {
423144478Sharti			pgn->unmade -= 1;
4241590Srgrimes
425144478Sharti			if (!(cgn->type & (OP_EXEC | OP_USE))) {
426144478Sharti				if (cgn->made == MADE) {
427144478Sharti					pgn->childMade = TRUE;
428144478Sharti					if (pgn->cmtime < cgn->mtime) {
429144478Sharti						pgn->cmtime = cgn->mtime;
430144478Sharti					}
431144478Sharti				} else {
432144478Sharti					Make_TimeStamp(pgn, cgn);
433144478Sharti				}
434144478Sharti			}
435144478Sharti			if (pgn->unmade == 0) {
436144478Sharti				/*
437144478Sharti				 * Queue the node up -- any unmade predecessors
438144478Sharti				 * will be dealt with in MakeStartJobs.
439144478Sharti				 */
440144478Sharti				Lst_EnQueue(&toBeMade, pgn);
441144478Sharti			} else if (pgn->unmade < 0) {
442144478Sharti				Error("Graph cycles through %s", pgn->name);
443144478Sharti			}
4441590Srgrimes		}
4451590Srgrimes	}
446138564Sharti
447144478Sharti	/*
448144478Sharti	 * Deal with successor nodes. If any is marked for making and has an
449144478Sharti	 * unmade count of 0, has not been made and isn't in the examination
450144478Sharti	 * queue, it means we need to place it in the queue as it restrained
451144478Sharti	 * itself before.
452144478Sharti	 */
453144478Sharti	for (ln = Lst_First(&cgn->successors); ln != NULL; ln = Lst_Succ(ln)) {
454144478Sharti		GNode	*succ = Lst_Datum(ln);
4551590Srgrimes
456144478Sharti		if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
457144478Sharti		    Lst_Member(&toBeMade, succ) == NULL) {
458144478Sharti			Lst_EnQueue(&toBeMade, succ);
459144478Sharti		}
4601590Srgrimes	}
4618874Srgrimes
462144478Sharti	/*
463144478Sharti	 * Set the .PREFIX and .IMPSRC variables for all the implied parents
464144478Sharti	 * of this node.
465144478Sharti	 */
466144478Sharti	cpref = Var_Value(PREFIX, cgn, &ptr);
467144478Sharti	for (ln = Lst_First(&cgn->iParents); ln != NULL; ln = Lst_Succ(ln)) {
468144478Sharti		pgn = Lst_Datum(ln);
469144478Sharti		if (pgn->make) {
470144478Sharti			Var_Set(IMPSRC, cname, pgn);
471144478Sharti			Var_Set(PREFIX, cpref, pgn);
472144478Sharti		}
4731590Srgrimes	}
474144478Sharti	free(ptr);
4751590Srgrimes}
476138232Sharti
477144478Sharti/**
478144478Sharti * Make_DoAllVar
4791590Srgrimes *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
4801590Srgrimes *	done separately, rather than while traversing the graph. This is
4811590Srgrimes *	because Make defined OODATE to contain all sources whose modification
4821590Srgrimes *	times were later than that of the target, *not* those sources that
4831590Srgrimes *	were out-of-date. Since in both compatibility and native modes,
4841590Srgrimes *	the modification time of the parent isn't found until the child
4851590Srgrimes *	has been dealt with, we have to wait until now to fill in the
4861590Srgrimes *	variable. As for ALLSRC, the ordering is important and not
4871590Srgrimes *	guaranteed when in native mode, so it must be set here, too.
4881590Srgrimes *
4891590Srgrimes * Side Effects:
4901590Srgrimes *	The ALLSRC and OODATE variables of the given node is filled in.
4911590Srgrimes *	If the node is a .JOIN node, its TARGET variable will be set to
4921590Srgrimes * 	match its ALLSRC variable.
4931590Srgrimes */
4941590Srgrimesvoid
495138232ShartiMake_DoAllVar(GNode *gn)
4961590Srgrimes{
497143694Sharti	LstNode	*ln;
498143694Sharti	GNode	*cgn;
499143694Sharti	char	*child;
500143694Sharti	char	*p1;
5011590Srgrimes
502143694Sharti	LST_FOREACH(ln, &gn->children) {
503143694Sharti		/*
504143694Sharti		 * Add the child's name to the ALLSRC and OODATE variables of
505143694Sharti		 * the given node. The child is added only if it has not been
506143694Sharti		 * given the .EXEC, .USE or .INVISIBLE attributes. .EXEC and
507143694Sharti		 * .USE children are very rarely going to be files, so...
508143694Sharti		 *
509143694Sharti		 * A child is added to the OODATE variable if its modification
510143694Sharti		 * time is later than that of its parent, as defined by Make,
511143694Sharti		 * except if the parent is a .JOIN node. In that case, it is
512143694Sharti		 * only added to the OODATE variable if it was actually made
513143694Sharti		 * (since .JOIN nodes don't have modification times, the
514143694Sharti		 * comparison is rather unfair...).
515143694Sharti		 */
516143694Sharti		cgn = Lst_Datum(ln);
517138232Sharti
518143694Sharti		if ((cgn->type & (OP_EXEC | OP_USE | OP_INVISIBLE)) == 0) {
519143694Sharti			p1 = NULL;
520143694Sharti			if (OP_NOP(cgn->type)) {
521143694Sharti				/*
522143694Sharti				 * this node is only source; use the specific
523143694Sharti				 * pathname for it
524143694Sharti				 */
525143694Sharti				child = cgn->path ? cgn->path : cgn->name;
526143694Sharti			} else
527143694Sharti				child = Var_Value(TARGET, cgn, &p1);
528143694Sharti			Var_Append(ALLSRC, child, gn);
529143694Sharti			if (gn->type & OP_JOIN) {
530143694Sharti				if (cgn->made == MADE) {
531143694Sharti					Var_Append(OODATE, child, gn);
532143694Sharti				}
533143694Sharti			} else if (gn->mtime < cgn->mtime ||
534143694Sharti			    (cgn->mtime >= now && cgn->made == MADE)) {
535143694Sharti				/*
536143694Sharti				 * It goes in the OODATE variable if the parent
537143694Sharti				 * is younger than the child or if the child has
538143694Sharti				 * been modified more recently than the start of
539143694Sharti				 * the make. This is to keep pmake from getting
540143694Sharti				 * confused if something else updates the parent
541143694Sharti				 * after the make starts (shouldn't happen, I
542143694Sharti				 * know, but sometimes it does). In such a case,
543143694Sharti				 * if we've updated the kid, the parent is
544143694Sharti				 * likely to have a modification time later than
545143694Sharti				 * that of the kid and anything that relies on
546143694Sharti				 * the OODATE variable will be hosed.
547143694Sharti				 *
548143694Sharti				 * XXX: This will cause all made children to
549143694Sharti				 * go in the OODATE variable, even if they're
550143694Sharti				 * not touched, if RECHECK isn't defined, since
551143694Sharti				 * cgn->mtime is set to now in Make_Update.
552143694Sharti				 * According to some people, this is good...
553143694Sharti				 */
554143694Sharti				Var_Append(OODATE, child, gn);
555143694Sharti			}
556143694Sharti			free(p1);
557143694Sharti		}
558143694Sharti	}
5591590Srgrimes
560143694Sharti	if (!Var_Exists (OODATE, gn)) {
561143694Sharti		Var_Set(OODATE, "", gn);
562143694Sharti	}
563143694Sharti	if (!Var_Exists (ALLSRC, gn)) {
564143694Sharti		Var_Set(ALLSRC, "", gn);
565143694Sharti	}
566138232Sharti
567143694Sharti	if (gn->type & OP_JOIN) {
568143694Sharti		Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn);
569143694Sharti		free(p1);
570143694Sharti	}
5711590Srgrimes}
572138232Sharti
573144478Sharti/**
574144478Sharti * MakeStartJobs
5751590Srgrimes *	Start as many jobs as possible.
5761590Srgrimes *
5771590Srgrimes * Results:
5781590Srgrimes *	If the query flag was given to pmake, no job will be started,
5791590Srgrimes *	but as soon as an out-of-date target is found, this function
5801590Srgrimes *	returns TRUE. At all other times, this function returns FALSE.
5811590Srgrimes *
5821590Srgrimes * Side Effects:
5831590Srgrimes *	Nodes are removed from the toBeMade queue and job table slots
5841590Srgrimes *	are filled.
5851590Srgrimes */
5861590Srgrimesstatic Boolean
587138232ShartiMakeStartJobs(void)
5881590Srgrimes{
589144478Sharti	GNode	*gn;
5908874Srgrimes
591144478Sharti	while (!Lst_IsEmpty(&toBeMade) && !Job_Full()) {
592144478Sharti		gn = Lst_DeQueue(&toBeMade);
593144478Sharti		DEBUGF(MAKE, ("Examining %s...", gn->name));
5941590Srgrimes
595144478Sharti		/*
596144478Sharti		 * Make sure any and all predecessors that are going to be made,
597144478Sharti		 * have been.
598144478Sharti		 */
599144478Sharti		if (!Lst_IsEmpty(&gn->preds)) {
600144478Sharti			LstNode *ln;
6011590Srgrimes
602144478Sharti			for (ln = Lst_First(&gn->preds); ln != NULL;
603144478Sharti			    ln = Lst_Succ(ln)){
604144478Sharti				GNode	*pgn = Lst_Datum(ln);
605144478Sharti
606144478Sharti				if (pgn->make && pgn->made == UNMADE) {
607144478Sharti					DEBUGF(MAKE, ("predecessor %s not made "
608144478Sharti					    "yet.\n", pgn->name));
609144478Sharti					break;
610144478Sharti				}
611144478Sharti			}
612144478Sharti			/*
613144478Sharti			 * If ln isn't NULL, there's a predecessor as yet
614144478Sharti			 * unmade, so we just drop this node on the floor.
615144478Sharti			 * When the node in question has been made, it will
616144478Sharti			 * notice this node as being ready to make but as yet
617144478Sharti			 * unmade and will place the node on the queue.
618144478Sharti			 */
619144478Sharti			if (ln != NULL) {
620144478Sharti				continue;
621144478Sharti			}
6221590Srgrimes		}
6238874Srgrimes
624144478Sharti		numNodes--;
625144478Sharti		if (Make_OODate(gn)) {
626144478Sharti			DEBUGF(MAKE, ("out-of-date\n"));
627144478Sharti			if (queryFlag) {
628144478Sharti				return (TRUE);
629144478Sharti			}
630144478Sharti			Make_DoAllVar(gn);
631144478Sharti			Job_Make(gn);
632144478Sharti		} else {
633144478Sharti			DEBUGF(MAKE, ("up-to-date\n"));
634144478Sharti			gn->made = UPTODATE;
635144478Sharti			if (gn->type & OP_JOIN) {
636144478Sharti				/*
637144478Sharti				 * Even for an up-to-date .JOIN node, we need
638144478Sharti				 * it to have its context variables so
639144478Sharti				 * references to it get the correct value for
640144478Sharti				 * .TARGET when building up the context
641144478Sharti				 * variables of its parent(s)...
642144478Sharti				 */
643144478Sharti				Make_DoAllVar(gn);
644144478Sharti			}
6458874Srgrimes
646144478Sharti			Make_Update(gn);
647144478Sharti		}
6481590Srgrimes	}
649144478Sharti	return (FALSE);
6501590Srgrimes}
651138232Sharti
652144478Sharti/**
653144478Sharti * MakePrintStatus
6541590Srgrimes *	Print the status of a top-level node, viz. it being up-to-date
6551590Srgrimes *	already or not created due to an error in a lower level.
656143694Sharti *	Callback function for Make_Run via LST_FOREACH.  If gn->unmade is
657104696Sjmallett *	nonzero and that is meant to imply a cycle in the graph, then
658104696Sjmallett *	cycle is TRUE.
6591590Srgrimes *
6601590Srgrimes * Side Effects:
6611590Srgrimes *	A message may be printed.
6621590Srgrimes */
663143694Shartistatic void
664143694ShartiMakePrintStatus(GNode *gn, Boolean cycle)
6651590Srgrimes{
666144478Sharti	LstNode	*ln;
667138232Sharti
668144478Sharti	if (gn->made == UPTODATE) {
669144478Sharti		printf("`%s' is up to date.\n", gn->name);
670144478Sharti
671144478Sharti	} else if (gn->unmade != 0) {
672144478Sharti		if (cycle) {
673144478Sharti			/*
674144478Sharti			 * If printing cycles and came to one that has unmade
675144478Sharti			 * children, print out the cycle by recursing on its
676144478Sharti			 * children. Note a cycle like:
677144478Sharti			 *	a : b
678144478Sharti			 *	b : c
679144478Sharti			 *	c : b
680144478Sharti			 * will cause this to erroneously complain about a
681144478Sharti			 * being in the cycle, but this is a good approximation.
682144478Sharti			 */
683144478Sharti			if (gn->made == CYCLE) {
684144478Sharti				Error("Graph cycles through `%s'", gn->name);
685144478Sharti				gn->made = ENDCYCLE;
686144478Sharti				LST_FOREACH(ln, &gn->children)
687144478Sharti					MakePrintStatus(Lst_Datum(ln), TRUE);
688144478Sharti				gn->made = UNMADE;
689144478Sharti			} else if (gn->made != ENDCYCLE) {
690144478Sharti				gn->made = CYCLE;
691144478Sharti				LST_FOREACH(ln, &gn->children)
692144478Sharti					MakePrintStatus(Lst_Datum(ln), TRUE);
693144478Sharti			}
694144478Sharti		} else {
695144478Sharti			printf("`%s' not remade because of errors.\n",
696144478Sharti			    gn->name);
697144478Sharti		}
6981590Srgrimes	}
6991590Srgrimes}
700138232Sharti
701144478Sharti/**
702144478Sharti * Make_Run
7031590Srgrimes *	Initialize the nodes to remake and the list of nodes which are
7041590Srgrimes *	ready to be made by doing a breadth-first traversal of the graph
7051590Srgrimes *	starting from the nodes in the given list. Once this traversal
7061590Srgrimes *	is finished, all the 'leaves' of the graph are in the toBeMade
7071590Srgrimes *	queue.
7081590Srgrimes *	Using this queue and the Job module, work back up the graph,
7091590Srgrimes *	calling on MakeStartJobs to keep the job table as full as
7101590Srgrimes *	possible.
7111590Srgrimes *
7121590Srgrimes * Results:
7131590Srgrimes *	TRUE if work was done. FALSE otherwise.
7141590Srgrimes *
7151590Srgrimes * Side Effects:
7161590Srgrimes *	The make field of all nodes involved in the creation of the given
7171590Srgrimes *	targets is set to 1. The toBeMade list is set to contain all the
7181590Srgrimes *	'leaves' of these subgraphs.
7191590Srgrimes */
7201590SrgrimesBoolean
721138512ShartiMake_Run(Lst *targs)
7221590Srgrimes{
723144478Sharti	GNode	*gn;		/* a temporary pointer */
724144478Sharti	GNode	*cgn;
725144478Sharti	Lst	examine;	/* List of targets to examine */
726144478Sharti	int	errors;		/* Number of errors the Job module reports */
727144478Sharti	LstNode	*ln;
7281590Srgrimes
729144478Sharti	Lst_Init(&examine);
730144478Sharti	Lst_Duplicate(&examine, targs, NOCOPY);
731144478Sharti	numNodes = 0;
7328874Srgrimes
733144478Sharti	/*
734144478Sharti	 * Make an initial downward pass over the graph, marking nodes to be
735144478Sharti	 * made as we go down. We call Suff_FindDeps to find where a node is and
736144478Sharti	 * to get some children for it if it has none and also has no commands.
737144478Sharti	 * If the node is a leaf, we stick it on the toBeMade queue to
738144478Sharti	 * be looked at in a minute, otherwise we add its children to our queue
739144478Sharti	 * and go on about our business.
740144478Sharti	 */
741144478Sharti	while (!Lst_IsEmpty(&examine)) {
742144478Sharti		gn = Lst_DeQueue(&examine);
7438874Srgrimes
744144478Sharti		if (!gn->make) {
745144478Sharti			gn->make = TRUE;
746144478Sharti			numNodes++;
7478874Srgrimes
748144478Sharti			/*
749144478Sharti			 * Apply any .USE rules before looking for implicit
750144478Sharti			 * dependencies to make sure everything has commands
751144478Sharti			 * that should...
752144478Sharti			 */
753144478Sharti			LST_FOREACH(ln, &gn->children)
754144478Sharti				if (Make_HandleUse(Lst_Datum(ln), gn))
755144478Sharti					break;
756143694Sharti
757144478Sharti			Suff_FindDeps(gn);
7581590Srgrimes
759144478Sharti			if (gn->unmade != 0) {
760144478Sharti				LST_FOREACH(ln, &gn->children) {
761144478Sharti					cgn = Lst_Datum(ln);
762144478Sharti					if (!cgn->make && !(cgn->type & OP_USE))
763144478Sharti						Lst_EnQueue(&examine, cgn);
764144478Sharti				}
765144478Sharti			} else {
766144478Sharti				Lst_EnQueue(&toBeMade, gn);
767144478Sharti			}
768143694Sharti		}
7691590Srgrimes	}
7708874Srgrimes
771144478Sharti	if (queryFlag) {
772144478Sharti		/*
773144478Sharti		 * We wouldn't do any work unless we could start some jobs in
774144478Sharti		 * the next loop... (we won't actually start any, of course,
775144478Sharti		 * this is just to see if any of the targets was out of date)
776144478Sharti		 */
777144478Sharti		return (MakeStartJobs());
778144478Sharti
779144478Sharti	} else {
780144478Sharti		/*
781144478Sharti		 * Initialization. At the moment, no jobs are running and
782144478Sharti		 * until some get started, nothing will happen since the
783144478Sharti		 * remaining upward traversal of the graph is performed by the
784144478Sharti		 * routines in job.c upon the finishing of a job. So we fill
785144478Sharti		 * the Job table as much as we can before going into our loop.
786144478Sharti		 */
787144478Sharti		MakeStartJobs();
788144478Sharti	}
789144478Sharti
7901590Srgrimes	/*
791144478Sharti	 * Main Loop: The idea here is that the ending of jobs will take
792144478Sharti	 * care of the maintenance of data structures and the waiting for output
793144478Sharti	 * will cause us to be idle most of the time while our children run as
794144478Sharti	 * much as possible. Because the job table is kept as full as possible,
795144478Sharti	 * the only time when it will be empty is when all the jobs which need
796144478Sharti	 * running have been run, so that is the end condition of this loop.
797144478Sharti	 * Note that the Job module will exit if there were any errors unless
798144478Sharti	 * the keepgoing flag was given.
7991590Srgrimes	 */
800144478Sharti	while (!Job_Empty()) {
801144478Sharti		Job_CatchOutput(!Lst_IsEmpty(&toBeMade));
802144478Sharti		Job_CatchChildren(!usePipes);
803144478Sharti		MakeStartJobs();
804144478Sharti	}
805144478Sharti
806144478Sharti	errors = Job_Finish();
807144478Sharti
8081590Srgrimes	/*
809144478Sharti	 * Print the final status of each target. E.g. if it wasn't made
810144478Sharti	 * because some inferior reported an error.
8111590Srgrimes	 */
812144478Sharti	errors = ((errors == 0) && (numNodes != 0));
813144478Sharti	LST_FOREACH(ln, targs)
814144478Sharti		MakePrintStatus(Lst_Datum(ln), errors);
8151590Srgrimes
816144478Sharti	return (TRUE);
8171590Srgrimes}
818