make.c revision 138232
11590Srgrimes/*
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
3528454Scharnier * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361590Srgrimes * SUCH DAMAGE.
371590Srgrimes *
381590Srgrimes * @(#)make.c	8.1 (Berkeley) 6/6/93
391590Srgrimes */
401590Srgrimes
4128454Scharnier#include <sys/cdefs.h>
421590Srgrimes__FBSDID("$FreeBSD: head/usr.bin/make/make.c 138232 2004-11-30 17:46:29Z harti $");
4328454Scharnier
4428454Scharnier/*-
4550477Speter * make.c --
461590Srgrimes *	The functions which perform the examination of targets and
471590Srgrimes *	their suitability for creation
4828454Scharnier *
491590Srgrimes * Interface:
5028454Scharnier *	Make_Run 	    	Initialize things for the module and recreate
5128454Scharnier *	    	  	    	whatever needs recreating. Returns TRUE if
5228454Scharnier *	    	    	    	work was (or would have been) done and FALSE
5328454Scharnier *	    	  	    	otherwise.
541590Srgrimes *
551590Srgrimes *	Make_Update	    	Update all parents of a given child. Performs
561590Srgrimes *	    	  	    	various bookkeeping chores like the updating
571590Srgrimes *	    	  	    	of the cmtime field of the parent, filling
581590Srgrimes *	    	  	    	of the IMPSRC context variable, etc. It will
591590Srgrimes *	    	  	    	place the parent on the toBeMade queue if it
601590Srgrimes *	    	  	    	should be.
611590Srgrimes *
621590Srgrimes *	Make_TimeStamp	    	Function to set the parent's cmtime field
631590Srgrimes *	    	  	    	based on a child's modification time.
641590Srgrimes *
651590Srgrimes *	Make_DoAllVar	    	Set up the various local variables for a
661590Srgrimes *	    	  	    	target, including the .ALLSRC variable, making
671590Srgrimes *	    	  	    	sure that any variable that needs to exist
681590Srgrimes *	    	  	    	at the very least has the empty value.
691590Srgrimes *
701590Srgrimes *	Make_OODate 	    	Determine if a target is out-of-date.
711590Srgrimes *
721590Srgrimes *	Make_HandleUse		See if a child is a .USE node for a parent
731590Srgrimes *				and perform the .USE actions if so.
741590Srgrimes */
751590Srgrimes
761590Srgrimes#include    "make.h"
771590Srgrimes#include    "hash.h"
781590Srgrimes#include    "dir.h"
791590Srgrimes#include    "job.h"
801590Srgrimes
811590Srgrimesstatic Lst     	toBeMade;	/* The current fringe of the graph. These
821590Srgrimes				 * are nodes which await examination by
831590Srgrimes				 * MakeOODate. It is added to by
841590Srgrimes				 * Make_Update and subtracted from by
851590Srgrimes				 * MakeStartJobs */
861590Srgrimesstatic int  	numNodes;   	/* Number of nodes to be processed. If this
8728454Scharnier				 * is non-zero when Job_Empty() returns
8828454Scharnier				 * TRUE, there's a cycle in the graph */
8928454Scharnier
9028454Scharnierstatic int MakeAddChild(void *, void *);
9128454Scharnierstatic int MakeAddAllSrc(void *, void *);
9228454Scharnierstatic int MakeTimeStamp(void *, void *);
9328454Scharnierstatic int MakeHandleUse(void *, void *);
9428454Scharnierstatic Boolean MakeStartJobs(void);
9528454Scharnierstatic int MakePrintStatus(void *, void *);
9628454Scharnier
9728454Scharnier/*-
9828454Scharnier *-----------------------------------------------------------------------
9928454Scharnier * Make_TimeStamp --
1001590Srgrimes *	Set the cmtime field of a parent node based on the mtime stamp in its
1011590Srgrimes *	child. Called from MakeOODate via Lst_ForEach.
10228789Scharnier *
1031590Srgrimes * Results:
1041590Srgrimes *	Always returns 0.
1051590Srgrimes *
1061590Srgrimes * Side Effects:
1071590Srgrimes *	The cmtime of the parent node will be changed if the mtime
1081590Srgrimes *	field of the child is greater than it.
1091590Srgrimes *-----------------------------------------------------------------------
1101590Srgrimes */
1111590Srgrimesint
1121590SrgrimesMake_TimeStamp(GNode *pgn, GNode *cgn)
1131590Srgrimes{
1141590Srgrimes
11524360Simp    if (cgn->mtime > pgn->cmtime) {
1161590Srgrimes	pgn->cmtime = cgn->mtime;
1171590Srgrimes    }
1181590Srgrimes    return (0);
1191590Srgrimes}
1201590Srgrimes
1211590Srgrimesstatic int
1221590SrgrimesMakeTimeStamp(void *pgn, void *cgn)
1231590Srgrimes{
1241590Srgrimes
1251590Srgrimes    return (Make_TimeStamp((GNode *)pgn, (GNode *)cgn));
12628454Scharnier}
1271590Srgrimes
1281590Srgrimes/*-
1291590Srgrimes *-----------------------------------------------------------------------
1301590Srgrimes * Make_OODate --
1311590Srgrimes *	See if a given node is out of date with respect to its sources.
1321590Srgrimes *	Used by Make_Run when deciding which nodes to place on the
1331590Srgrimes *	toBeMade queue initially and by Make_Update to screen out USE and
1341590Srgrimes *	EXEC nodes. In the latter case, however, any other sort of node
13528454Scharnier *	must be considered out-of-date since at least one of its children
1361590Srgrimes *	will have been recreated.
1371590Srgrimes *
1381590Srgrimes * Results:
1391590Srgrimes *	TRUE if the node is out of date. FALSE otherwise.
1401590Srgrimes *
1411590Srgrimes * Side Effects:
1421590Srgrimes *	The mtime field of the node and the cmtime field of its parents
1431590Srgrimes *	will/may be changed.
1441590Srgrimes *-----------------------------------------------------------------------
1451590Srgrimes */
1461590SrgrimesBoolean
1471590SrgrimesMake_OODate(GNode *gn)
1481590Srgrimes{
1491590Srgrimes    Boolean         oodate;
1501590Srgrimes
1511590Srgrimes    /*
15228454Scharnier     * Certain types of targets needn't even be sought as their datedness
15328454Scharnier     * doesn't depend on their modification time...
15428454Scharnier     */
1551590Srgrimes    if ((gn->type & (OP_JOIN | OP_USE | OP_EXEC)) == 0) {
1561590Srgrimes	 Dir_MTime(gn);
1571590Srgrimes	if (gn->mtime != 0) {
1581590Srgrimes	    DEBUGF(MAKE, ("modified %s...", Targ_FmtTime(gn->mtime)));
1591590Srgrimes	} else {
16028454Scharnier	    DEBUGF(MAKE, ("non-existent..."));
16128454Scharnier	}
16228454Scharnier    }
16328454Scharnier
16428454Scharnier    /*
16528454Scharnier     * A target is remade in one of the following circumstances:
16628454Scharnier     *	its modification time is smaller than that of its youngest child
16728454Scharnier     *	    and it would actually be run (has commands or type OP_NOP)
1681590Srgrimes     *	it's the object of a force operator
1691590Srgrimes     *	it has no children, was on the lhs of an operator and doesn't exist
1701590Srgrimes     *	    already.
1711590Srgrimes     *
1721590Srgrimes     * Libraries are only considered out-of-date if the archive module says
1731590Srgrimes     * they are.
1741590Srgrimes     *
1751590Srgrimes     * These weird rules are brought to you by Backward-Compatability and
1761590Srgrimes     * the strange people who wrote 'Make'.
1771590Srgrimes     */
1781590Srgrimes    if (gn->type & OP_USE) {
1791590Srgrimes	/*
1801590Srgrimes	 * If the node is a USE node it is *never* out of date
1811590Srgrimes	 * no matter *what*.
1821590Srgrimes	 */
1831590Srgrimes	DEBUGF(MAKE, (".USE node..."));
1841590Srgrimes	oodate = FALSE;
1851590Srgrimes    } else if (gn->type & OP_LIB) {
1861590Srgrimes	DEBUGF(MAKE, ("library..."));
1871590Srgrimes
1881590Srgrimes	/*
1891590Srgrimes	 * always out of date if no children and :: target
1901590Srgrimes	 */
1911590Srgrimes
1921590Srgrimes	oodate = Arch_LibOODate(gn) ||
1931590Srgrimes	    ((gn->cmtime == 0) && (gn->type & OP_DOUBLEDEP));
1941590Srgrimes    } else if (gn->type & OP_JOIN) {
1951590Srgrimes	/*
1961590Srgrimes	 * A target with the .JOIN attribute is only considered
1971590Srgrimes	 * out-of-date if any of its children was out-of-date.
1981590Srgrimes	 */
1991590Srgrimes	DEBUGF(MAKE, (".JOIN node..."));
2001590Srgrimes	oodate = gn->childMade;
2011590Srgrimes    } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
2021590Srgrimes	/*
2031590Srgrimes	 * A node which is the object of the force (!) operator or which has
2041590Srgrimes	 * the .EXEC attribute is always considered out-of-date.
2051590Srgrimes	 */
2061590Srgrimes	if (gn->type & OP_FORCE) {
2071590Srgrimes	    DEBUGF(MAKE, ("! operator..."));
2081590Srgrimes	} else if (gn->type & OP_PHONY) {
2091590Srgrimes	    DEBUGF(MAKE, (".PHONY node..."));
2101590Srgrimes	} else {
2111590Srgrimes	    DEBUGF(MAKE, (".EXEC node..."));
2121590Srgrimes	}
2131590Srgrimes	oodate = TRUE;
2141590Srgrimes    } else if ((gn->mtime < gn->cmtime) ||
2151590Srgrimes	       ((gn->cmtime == 0) &&
2161590Srgrimes		((gn->mtime==0) || (gn->type & OP_DOUBLEDEP))))
2171590Srgrimes    {
2181590Srgrimes	/*
2191590Srgrimes	 * A node whose modification time is less than that of its
2201590Srgrimes	 * youngest child or that has no children (cmtime == 0) and
2211590Srgrimes	 * either doesn't exist (mtime == 0) or was the object of a
2221590Srgrimes	 * :: operator is out-of-date. Why? Because that's the way Make does
2231590Srgrimes	 * it.
2241590Srgrimes	 */
2251590Srgrimes	if (gn->mtime < gn->cmtime) {
2261590Srgrimes	    DEBUGF(MAKE, ("modified before source..."));
2271590Srgrimes	} else if (gn->mtime == 0) {
2281590Srgrimes	    DEBUGF(MAKE, ("non-existent and no sources..."));
2291590Srgrimes	} else {
2301590Srgrimes	    DEBUGF(MAKE, (":: operator and no sources..."));
2311590Srgrimes	}
23228454Scharnier	oodate = TRUE;
2331590Srgrimes    } else
2341590Srgrimes	oodate = FALSE;
2351590Srgrimes
2361590Srgrimes    /*
2371590Srgrimes     * If the target isn't out-of-date, the parents need to know its
2381590Srgrimes     * modification time. Note that targets that appear to be out-of-date
2391590Srgrimes     * but aren't, because they have no commands and aren't of type OP_NOP,
2401590Srgrimes     * have their mtime stay below their children's mtime to keep parents from
2411590Srgrimes     * thinking they're out-of-date.
2421590Srgrimes     */
2431590Srgrimes    if (!oodate) {
2441590Srgrimes	Lst_ForEach(gn->parents, MakeTimeStamp, (void *)gn);
2451590Srgrimes    }
2461590Srgrimes
2471590Srgrimes    return (oodate);
2481590Srgrimes}
2491590Srgrimes
2501590Srgrimes/*-
2511590Srgrimes *-----------------------------------------------------------------------
2521590Srgrimes * MakeAddChild  --
2531590Srgrimes *	Function used by Make_Run to add a child to the list l.
2541590Srgrimes *	It will only add the child if its make field is FALSE.
2551590Srgrimes *
2561590Srgrimes * Results:
2571590Srgrimes *	Always returns 0
2581590Srgrimes *
2591590Srgrimes * Side Effects:
2601590Srgrimes *	The given list is extended
2611590Srgrimes *-----------------------------------------------------------------------
2621590Srgrimes */
2631590Srgrimesstatic int
2641590SrgrimesMakeAddChild(void *gnp, void *lp)
2651590Srgrimes{
2661590Srgrimes    GNode          *gn = (GNode *)gnp;
2671590Srgrimes    Lst            l = (Lst)lp;
2681590Srgrimes
2691590Srgrimes    if (!gn->make && !(gn->type & OP_USE)) {
2701590Srgrimes	Lst_EnQueue(l, (void *)gn);
2711590Srgrimes    }
2721590Srgrimes    return (0);
2731590Srgrimes}
2741590Srgrimes
2751590Srgrimes/*-
2761590Srgrimes *-----------------------------------------------------------------------
2771590Srgrimes * Make_HandleUse --
27828454Scharnier *	Function called by Make_Run and SuffApplyTransform on the downward
2791590Srgrimes *	pass to handle .USE and transformation nodes. A callback function
2801590Srgrimes *	for Lst_ForEach, it implements the .USE and transformation
2811590Srgrimes *	functionality by copying the node's commands, type flags
2821590Srgrimes *	and children to the parent node. Should be called before the
2831590Srgrimes *	children are enqueued to be looked at by MakeAddChild.
2841590Srgrimes *
2851590Srgrimes *	A .USE node is much like an explicit transformation rule, except
2861590Srgrimes *	its commands are always added to the target node, even if the
2871590Srgrimes *	target already has commands.
2881590Srgrimes *
28928454Scharnier * Results:
2901590Srgrimes *	returns 0.
2911590Srgrimes *
2921590Srgrimes * Side Effects:
2931590Srgrimes *	Children and commands may be added to the parent and the parent's
2941590Srgrimes *	type may be changed.
2951590Srgrimes *
2961590Srgrimes *-----------------------------------------------------------------------
2971590Srgrimes */
2981590Srgrimesint
2991590SrgrimesMake_HandleUse(GNode *cgn, GNode *pgn)
3001590Srgrimes{
30128454Scharnier    GNode	*gn;	 	/* A child of the .USE node */
3021590Srgrimes    LstNode	ln;	 	/* An element in the children list */
3031590Srgrimes
3041590Srgrimes    if (cgn->type & (OP_USE|OP_TRANSFORM)) {
3051590Srgrimes	if ((cgn->type & OP_USE) || Lst_IsEmpty(pgn->commands)) {
3061590Srgrimes	    /*
3071590Srgrimes	     * .USE or transformation and target has no commands -- append
3081590Srgrimes	     * the child's commands to the parent.
3091590Srgrimes	     */
3101590Srgrimes	     Lst_Concat(pgn->commands, cgn->commands, LST_CONCNEW);
3111590Srgrimes	}
3121590Srgrimes
3131590Srgrimes	if (Lst_Open(cgn->children) == SUCCESS) {
3141590Srgrimes	    while ((ln = Lst_Next(cgn->children)) != NULL) {
3151590Srgrimes		gn = (GNode *)Lst_Datum(ln);
3161590Srgrimes
3171590Srgrimes		if (Lst_Member(pgn->children, gn) == NULL) {
31828454Scharnier		    Lst_AtEnd(pgn->children, gn);
3191590Srgrimes		    Lst_AtEnd(gn->parents, pgn);
3201590Srgrimes		    pgn->unmade += 1;
3211590Srgrimes		}
3221590Srgrimes	    }
3231590Srgrimes	    Lst_Close(cgn->children);
3241590Srgrimes	}
3251590Srgrimes
3261590Srgrimes	pgn->type |= cgn->type & ~(OP_OPMASK | OP_USE | OP_TRANSFORM);
3271590Srgrimes
3281590Srgrimes	/*
3291590Srgrimes	 * This child node is now "made", so we decrement the count of
3301590Srgrimes	 * unmade children in the parent... We also remove the child
3311590Srgrimes	 * from the parent's list to accurately reflect the number of decent
3321590Srgrimes	 * children the parent has. This is used by Make_Run to decide
3331590Srgrimes	 * whether to queue the parent or examine its children...
3341590Srgrimes	 */
3351590Srgrimes	if (cgn->type & OP_USE) {
3361590Srgrimes	    pgn->unmade--;
3371590Srgrimes	}
3381590Srgrimes    }
3391590Srgrimes    return (0);
3401590Srgrimes}
3411590Srgrimes
3421590Srgrimesstatic int
3431590SrgrimesMakeHandleUse(void *pgn, void *cgn)
3441590Srgrimes{
3451590Srgrimes
3461590Srgrimes    return (Make_HandleUse((GNode *)pgn, (GNode *)cgn));
3471590Srgrimes}
3481590Srgrimes
3491590Srgrimes/*-
3501590Srgrimes *-----------------------------------------------------------------------
3511590Srgrimes * Make_Update  --
3521590Srgrimes *	Perform update on the parents of a node. Used by JobFinish once
3531590Srgrimes *	a node has been dealt with and by MakeStartJobs if it finds an
3541590Srgrimes *	up-to-date node.
3551590Srgrimes *
35628454Scharnier * Results:
3571590Srgrimes *	Always returns 0
3581590Srgrimes *
3591590Srgrimes * Side Effects:
3601590Srgrimes *	The unmade field of pgn is decremented and pgn may be placed on
3611590Srgrimes *	the toBeMade queue if this field becomes 0.
3621590Srgrimes *
3631590Srgrimes * 	If the child was made, the parent's childMade field will be set true
3641590Srgrimes *	and its cmtime set to now.
3651590Srgrimes *
3661590Srgrimes *	If the child wasn't made, the cmtime field of the parent will be
3671590Srgrimes *	altered if the child's mtime is big enough.
3681590Srgrimes *
3691590Srgrimes *	Finally, if the child is the implied source for the parent, the
3701590Srgrimes *	parent's IMPSRC variable is set appropriately.
3711590Srgrimes *
3721590Srgrimes *-----------------------------------------------------------------------
3731590Srgrimes */
3741590Srgrimesvoid
3751590SrgrimesMake_Update(GNode *cgn)
3761590Srgrimes{
3771590Srgrimes    GNode 	*pgn;		/* the parent node */
3781590Srgrimes    char  	*cname;		/* the child's name */
3791590Srgrimes    LstNode	ln;	 	/* Element in parents and iParents lists */
38028454Scharnier    char	*p1;
3811590Srgrimes
3821590Srgrimes    cname = Var_Value(TARGET, cgn, &p1);
3831590Srgrimes    free(p1);
3841590Srgrimes
3851590Srgrimes    /*
3861590Srgrimes     * If the child was actually made, see what its modification time is
3871590Srgrimes     * now -- some rules won't actually update the file. If the file still
3881590Srgrimes     * doesn't exist, make its mtime now.
3891590Srgrimes     */
39028454Scharnier    if (cgn->made != UPTODATE) {
3911590Srgrimes#ifndef RECHECK
3921590Srgrimes	/*
3931590Srgrimes	 * We can't re-stat the thing, but we can at least take care of rules
3941590Srgrimes	 * where a target depends on a source that actually creates the
3951590Srgrimes	 * target, but only if it has changed, e.g.
3961590Srgrimes	 *
3971590Srgrimes	 * parse.h : parse.o
3981590Srgrimes	 *
3991590Srgrimes	 * parse.o : parse.y
4001590Srgrimes	 *  	yacc -d parse.y
4011590Srgrimes	 *  	cc -c y.tab.c
40228454Scharnier	 *  	mv y.tab.o parse.o
4031590Srgrimes	 *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
4041590Srgrimes	 *
4051590Srgrimes	 * In this case, if the definitions produced by yacc haven't changed
4061590Srgrimes	 * from before, parse.h won't have been updated and cgn->mtime will
4071590Srgrimes	 * reflect the current modification time for parse.h. This is
4081590Srgrimes	 * something of a kludge, I admit, but it's a useful one..
4091590Srgrimes	 * XXX: People like to use a rule like
4101590Srgrimes	 *
4111590Srgrimes	 * FRC:
41228454Scharnier	 *
4131590Srgrimes	 * To force things that depend on FRC to be made, so we have to
4141590Srgrimes	 * check for gn->children being empty as well...
4151590Srgrimes	 */
4161590Srgrimes	if (!Lst_IsEmpty(cgn->commands) || Lst_IsEmpty(cgn->children)) {
4171590Srgrimes	    cgn->mtime = now;
4181590Srgrimes	}
4191590Srgrimes#else
4201590Srgrimes	/*
4211590Srgrimes	 * This is what Make does and it's actually a good thing, as it
4221590Srgrimes	 * allows rules like
4231590Srgrimes	 *
4241590Srgrimes	 *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
4251590Srgrimes	 *
4261590Srgrimes	 * to function as intended. Unfortunately, thanks to the stateless
4271590Srgrimes	 * nature of NFS (by which I mean the loose coupling of two clients
4281590Srgrimes	 * using the same file from a common server), there are times
4291590Srgrimes	 * when the modification time of a file created on a remote
4301590Srgrimes	 * machine will not be modified before the local stat() implied by
4311590Srgrimes	 * the Dir_MTime occurs, thus leading us to believe that the file
4321590Srgrimes	 * is unchanged, wreaking havoc with files that depend on this one.
4331590Srgrimes	 *
4341590Srgrimes	 * I have decided it is better to make too much than to make too
4351590Srgrimes	 * little, so this stuff is commented out unless you're sure it's ok.
4361590Srgrimes	 * -- ardeb 1/12/88
4371590Srgrimes	 */
4381590Srgrimes	/*
4391590Srgrimes	 * Christos, 4/9/92: If we are  saving commands pretend that
4401590Srgrimes	 * the target is made now. Otherwise archives with ... rules
4411590Srgrimes	 * don't work!
4421590Srgrimes	 */
4431590Srgrimes	if (noExecute || (cgn->type & OP_SAVE_CMDS) || Dir_MTime(cgn) == 0) {
4441590Srgrimes	    cgn->mtime = now;
4451590Srgrimes	}
4461590Srgrimes	DEBUGF(MAKE, ("update time: %s\n", Targ_FmtTime(cgn->mtime)));
4471590Srgrimes#endif
4481590Srgrimes    }
4491590Srgrimes
4501590Srgrimes    if (Lst_Open(cgn->parents) == SUCCESS) {
4511590Srgrimes	while ((ln = Lst_Next(cgn->parents)) != NULL) {
4528874Srgrimes	    pgn = (GNode *)Lst_Datum(ln);
4531590Srgrimes	    if (pgn->make) {
4541590Srgrimes		pgn->unmade -= 1;
4551590Srgrimes
4561590Srgrimes		if (!(cgn->type & (OP_EXEC | OP_USE))) {
4571590Srgrimes		    if (cgn->made == MADE) {
4581590Srgrimes			pgn->childMade = TRUE;
4591590Srgrimes			if (pgn->cmtime < cgn->mtime) {
4601590Srgrimes			    pgn->cmtime = cgn->mtime;
4611590Srgrimes			}
4621590Srgrimes		    } else {
4631590Srgrimes			Make_TimeStamp(pgn, cgn);
4641590Srgrimes		    }
46528454Scharnier		}
4661590Srgrimes		if (pgn->unmade == 0) {
4671590Srgrimes		    /*
4681590Srgrimes		     * Queue the node up -- any unmade predecessors will
46928454Scharnier		     * be dealt with in MakeStartJobs.
4701590Srgrimes		     */
4711590Srgrimes		    Lst_EnQueue(toBeMade, (void *)pgn);
4721590Srgrimes		} else if (pgn->unmade < 0) {
4731590Srgrimes		    Error("Graph cycles through %s", pgn->name);
47428454Scharnier		}
4751590Srgrimes	    }
4761590Srgrimes	}
4771590Srgrimes	Lst_Close(cgn->parents);
4781590Srgrimes    }
4791590Srgrimes    /*
4801590Srgrimes     * Deal with successor nodes. If any is marked for making and has an unmade
4811590Srgrimes     * count of 0, has not been made and isn't in the examination queue,
4821590Srgrimes     * it means we need to place it in the queue as it restrained itself
4831590Srgrimes     * before.
4841590Srgrimes     */
48528454Scharnier    for (ln = Lst_First(cgn->successors); ln != NULL; ln = Lst_Succ(ln)) {
48628454Scharnier	GNode	*succ = (GNode *)Lst_Datum(ln);
4871590Srgrimes
4881590Srgrimes	if (succ->make && succ->unmade == 0 && succ->made == UNMADE &&
4891590Srgrimes	    Lst_Member(toBeMade, (void *)succ) == NULL)
4901590Srgrimes	{
49128454Scharnier	    Lst_EnQueue(toBeMade, (void *)succ);
4921590Srgrimes	}
4931590Srgrimes    }
4941590Srgrimes
4951590Srgrimes    /*
4961590Srgrimes     * Set the .PREFIX and .IMPSRC variables for all the implied parents
4971590Srgrimes     * of this node.
4981590Srgrimes     */
4991590Srgrimes    if (Lst_Open(cgn->iParents) == SUCCESS) {
5001590Srgrimes	char    *ptr;
5011590Srgrimes	char	*cpref = Var_Value(PREFIX, cgn, &ptr);
5021590Srgrimes
5031590Srgrimes	while ((ln = Lst_Next(cgn->iParents)) != NULL) {
5041590Srgrimes	    pgn = (GNode *)Lst_Datum (ln);
5051590Srgrimes	    if (pgn->make) {
5061590Srgrimes		Var_Set(IMPSRC, cname, pgn);
5071590Srgrimes		Var_Set(PREFIX, cpref, pgn);
5081590Srgrimes	    }
5091590Srgrimes	}
5101590Srgrimes	free(ptr);
5111590Srgrimes	Lst_Close(cgn->iParents);
5121590Srgrimes    }
5131590Srgrimes}
5141590Srgrimes
5151590Srgrimes/*-
5161590Srgrimes *-----------------------------------------------------------------------
5171590Srgrimes * MakeAddAllSrc --
5181590Srgrimes *	Add a child's name to the ALLSRC and OODATE variables of the given
5191590Srgrimes *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
5201590Srgrimes *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
5211590Srgrimes *	.EXEC and .USE children are very rarely going to be files, so...
5221590Srgrimes *	A child is added to the OODATE variable if its modification time is
5231590Srgrimes *	later than that of its parent, as defined by Make, except if the
5241590Srgrimes *	parent is a .JOIN node. In that case, it is only added to the OODATE
5251590Srgrimes *	variable if it was actually made (since .JOIN nodes don't have
5261590Srgrimes *	modification times, the comparison is rather unfair...)..
5271590Srgrimes *
5281590Srgrimes * Results:
5291590Srgrimes *	Always returns 0
5301590Srgrimes *
5311590Srgrimes * Side Effects:
5321590Srgrimes *	The ALLSRC variable for the given node is extended.
5331590Srgrimes *-----------------------------------------------------------------------
5341590Srgrimes */
5351590Srgrimesstatic int
5361590SrgrimesMakeAddAllSrc(void *cgnp, void *pgnp)
537{
538    GNode	*cgn = (GNode *) cgnp;
539    GNode	*pgn = (GNode *) pgnp;
540
541    if ((cgn->type & (OP_EXEC | OP_USE | OP_INVISIBLE)) == 0) {
542	char *child;
543	char *p1 = NULL;
544
545	if (OP_NOP(cgn->type)) {
546	    /*
547	     * this node is only source; use the specific pathname for it
548	     */
549	    child = cgn->path ? cgn->path : cgn->name;
550	}
551	else
552	    child = Var_Value(TARGET, cgn, &p1);
553	Var_Append(ALLSRC, child, pgn);
554	if (pgn->type & OP_JOIN) {
555	    if (cgn->made == MADE) {
556		Var_Append(OODATE, child, pgn);
557	    }
558	} else if ((pgn->mtime < cgn->mtime) ||
559		   (cgn->mtime >= now && cgn->made == MADE))
560	{
561	    /*
562	     * It goes in the OODATE variable if the parent is younger than the
563	     * child or if the child has been modified more recently than
564	     * the start of the make. This is to keep pmake from getting
565	     * confused if something else updates the parent after the
566	     * make starts (shouldn't happen, I know, but sometimes it
567	     * does). In such a case, if we've updated the kid, the parent
568	     * is likely to have a modification time later than that of
569	     * the kid and anything that relies on the OODATE variable will
570	     * be hosed.
571	     *
572	     * XXX: This will cause all made children to go in the OODATE
573	     * variable, even if they're not touched, if RECHECK isn't defined,
574	     * since cgn->mtime is set to now in Make_Update. According to
575	     * some people, this is good...
576	     */
577	    Var_Append(OODATE, child, pgn);
578	}
579	free(p1);
580    }
581    return (0);
582}
583
584/*-
585 *-----------------------------------------------------------------------
586 * Make_DoAllVar --
587 *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
588 *	done separately, rather than while traversing the graph. This is
589 *	because Make defined OODATE to contain all sources whose modification
590 *	times were later than that of the target, *not* those sources that
591 *	were out-of-date. Since in both compatibility and native modes,
592 *	the modification time of the parent isn't found until the child
593 *	has been dealt with, we have to wait until now to fill in the
594 *	variable. As for ALLSRC, the ordering is important and not
595 *	guaranteed when in native mode, so it must be set here, too.
596 *
597 * Results:
598 *	None
599 *
600 * Side Effects:
601 *	The ALLSRC and OODATE variables of the given node is filled in.
602 *	If the node is a .JOIN node, its TARGET variable will be set to
603 * 	match its ALLSRC variable.
604 *-----------------------------------------------------------------------
605 */
606void
607Make_DoAllVar(GNode *gn)
608{
609
610    Lst_ForEach(gn->children, MakeAddAllSrc, (void *)gn);
611
612    if (!Var_Exists (OODATE, gn)) {
613	Var_Set(OODATE, "", gn);
614    }
615    if (!Var_Exists (ALLSRC, gn)) {
616	Var_Set(ALLSRC, "", gn);
617    }
618
619    if (gn->type & OP_JOIN) {
620	char *p1;
621
622	Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn);
623	free(p1);
624    }
625}
626
627/*-
628 *-----------------------------------------------------------------------
629 * MakeStartJobs --
630 *	Start as many jobs as possible.
631 *
632 * Results:
633 *	If the query flag was given to pmake, no job will be started,
634 *	but as soon as an out-of-date target is found, this function
635 *	returns TRUE. At all other times, this function returns FALSE.
636 *
637 * Side Effects:
638 *	Nodes are removed from the toBeMade queue and job table slots
639 *	are filled.
640 *
641 *-----------------------------------------------------------------------
642 */
643static Boolean
644MakeStartJobs(void)
645{
646    GNode	*gn;
647
648    while (!Lst_IsEmpty (toBeMade) && !Job_Full()) {
649	gn = (GNode *)Lst_DeQueue(toBeMade);
650	DEBUGF(MAKE, ("Examining %s...", gn->name));
651	/*
652	 * Make sure any and all predecessors that are going to be made,
653	 * have been.
654	 */
655	if (!Lst_IsEmpty(gn->preds)) {
656	    LstNode ln;
657
658	    for (ln = Lst_First(gn->preds); ln != NULL; ln = Lst_Succ(ln)){
659		GNode	*pgn = (GNode *)Lst_Datum(ln);
660
661		if (pgn->make && pgn->made == UNMADE) {
662		    DEBUGF(MAKE, ("predecessor %s not made yet.\n", pgn->name));
663		    break;
664		}
665	    }
666	    /*
667	     * If ln isn't NULL, there's a predecessor as yet unmade, so we
668	     * just drop this node on the floor. When the node in question
669	     * has been made, it will notice this node as being ready to
670	     * make but as yet unmade and will place the node on the queue.
671	     */
672	    if (ln != NULL) {
673		continue;
674	    }
675	}
676
677	numNodes--;
678	if (Make_OODate(gn)) {
679	    DEBUGF(MAKE, ("out-of-date\n"));
680	    if (queryFlag) {
681		return (TRUE);
682	    }
683	    Make_DoAllVar(gn);
684	    Job_Make(gn);
685	} else {
686	    DEBUGF(MAKE, ("up-to-date\n"));
687	    gn->made = UPTODATE;
688	    if (gn->type & OP_JOIN) {
689		/*
690		 * Even for an up-to-date .JOIN node, we need it to have its
691		 * context variables so references to it get the correct
692		 * value for .TARGET when building up the context variables
693		 * of its parent(s)...
694		 */
695		Make_DoAllVar(gn);
696	    }
697
698	    Make_Update(gn);
699	}
700    }
701    return (FALSE);
702}
703
704/*-
705 *-----------------------------------------------------------------------
706 * MakePrintStatus --
707 *	Print the status of a top-level node, viz. it being up-to-date
708 *	already or not created due to an error in a lower level.
709 *	Callback function for Make_Run via Lst_ForEach.  If gn->unmade is
710 *	nonzero and that is meant to imply a cycle in the graph, then
711 *	cycle is TRUE.
712 *
713 * Results:
714 *	Always returns 0.
715 *
716 * Side Effects:
717 *	A message may be printed.
718 *
719 *-----------------------------------------------------------------------
720 */
721static int
722MakePrintStatus(void *gnp, void *cyclep)
723{
724    GNode   	*gn = (GNode *)gnp;
725    Boolean 	cycle = *(Boolean *)cyclep;
726
727    if (gn->made == UPTODATE) {
728	printf("`%s' is up to date.\n", gn->name);
729    } else if (gn->unmade != 0) {
730	if (cycle) {
731	    Boolean t = TRUE;
732	    /*
733	     * If printing cycles and came to one that has unmade children,
734	     * print out the cycle by recursing on its children. Note a
735	     * cycle like:
736	     *	a : b
737	     *	b : c
738	     *	c : b
739	     * will cause this to erroneously complain about a being in
740	     * the cycle, but this is a good approximation.
741	     */
742	    if (gn->made == CYCLE) {
743		Error("Graph cycles through `%s'", gn->name);
744		gn->made = ENDCYCLE;
745		Lst_ForEach(gn->children, MakePrintStatus, (void *)&t);
746		gn->made = UNMADE;
747	    } else if (gn->made != ENDCYCLE) {
748		gn->made = CYCLE;
749		Lst_ForEach(gn->children, MakePrintStatus, (void *)&t);
750	    }
751	} else {
752	    printf("`%s' not remade because of errors.\n", gn->name);
753	}
754    }
755    return (0);
756}
757
758/*-
759 *-----------------------------------------------------------------------
760 * Make_Run --
761 *	Initialize the nodes to remake and the list of nodes which are
762 *	ready to be made by doing a breadth-first traversal of the graph
763 *	starting from the nodes in the given list. Once this traversal
764 *	is finished, all the 'leaves' of the graph are in the toBeMade
765 *	queue.
766 *	Using this queue and the Job module, work back up the graph,
767 *	calling on MakeStartJobs to keep the job table as full as
768 *	possible.
769 *
770 * Results:
771 *	TRUE if work was done. FALSE otherwise.
772 *
773 * Side Effects:
774 *	The make field of all nodes involved in the creation of the given
775 *	targets is set to 1. The toBeMade list is set to contain all the
776 *	'leaves' of these subgraphs.
777 *-----------------------------------------------------------------------
778 */
779Boolean
780Make_Run(Lst targs)
781{
782    GNode	    *gn;	/* a temporary pointer */
783    Lst		    examine; 	/* List of targets to examine */
784    int	    	    errors; 	/* Number of errors the Job module reports */
785
786    toBeMade = Lst_Init(FALSE);
787
788    examine = Lst_Duplicate(targs, NOCOPY);
789    numNodes = 0;
790
791    /*
792     * Make an initial downward pass over the graph, marking nodes to be made
793     * as we go down. We call Suff_FindDeps to find where a node is and
794     * to get some children for it if it has none and also has no commands.
795     * If the node is a leaf, we stick it on the toBeMade queue to
796     * be looked at in a minute, otherwise we add its children to our queue
797     * and go on about our business.
798     */
799    while (!Lst_IsEmpty(examine)) {
800	gn = (GNode *)Lst_DeQueue(examine);
801
802	if (!gn->make) {
803	    gn->make = TRUE;
804	    numNodes++;
805
806	    /*
807	     * Apply any .USE rules before looking for implicit dependencies
808	     * to make sure everything has commands that should...
809	     */
810	    Lst_ForEach(gn->children, MakeHandleUse, (void *)gn);
811	    Suff_FindDeps(gn);
812
813	    if (gn->unmade != 0) {
814		Lst_ForEach(gn->children, MakeAddChild, (void *)examine);
815	    } else {
816		Lst_EnQueue(toBeMade, (void *)gn);
817	    }
818	}
819    }
820
821    Lst_Destroy(examine, NOFREE);
822
823    if (queryFlag) {
824	/*
825	 * We wouldn't do any work unless we could start some jobs in the
826	 * next loop... (we won't actually start any, of course, this is just
827	 * to see if any of the targets was out of date)
828	 */
829	return (MakeStartJobs());
830    } else {
831	/*
832	 * Initialization. At the moment, no jobs are running and until some
833	 * get started, nothing will happen since the remaining upward
834	 * traversal of the graph is performed by the routines in job.c upon
835	 * the finishing of a job. So we fill the Job table as much as we can
836	 * before going into our loop.
837	 */
838	 MakeStartJobs();
839    }
840
841    /*
842     * Main Loop: The idea here is that the ending of jobs will take
843     * care of the maintenance of data structures and the waiting for output
844     * will cause us to be idle most of the time while our children run as
845     * much as possible. Because the job table is kept as full as possible,
846     * the only time when it will be empty is when all the jobs which need
847     * running have been run, so that is the end condition of this loop.
848     * Note that the Job module will exit if there were any errors unless the
849     * keepgoing flag was given.
850     */
851    while (!Job_Empty ()) {
852	Job_CatchOutput(!Lst_IsEmpty (toBeMade));
853	Job_CatchChildren(!usePipes);
854	MakeStartJobs();
855    }
856
857    errors = Job_Finish();
858
859    /*
860     * Print the final status of each target. E.g. if it wasn't made
861     * because some inferior reported an error.
862     */
863    errors = ((errors == 0) && (numNodes != 0));
864    Lst_ForEach(targs, MakePrintStatus, (void *)&errors);
865
866    return (TRUE);
867}
868