Home | History | Annotate | Line # | Download | only in make
make.c revision 1.61
      1  1.61       dsl /*	$NetBSD: make.c,v 1.61 2006/02/11 20:59:49 dsl Exp $	*/
      2   1.7  christos 
      3   1.1       cgd /*
      4  1.10  christos  * Copyright (c) 1988, 1989, 1990, 1993
      5  1.10  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.51       agc  *
      7  1.51       agc  * This code is derived from software contributed to Berkeley by
      8  1.51       agc  * Adam de Boor.
      9  1.51       agc  *
     10  1.51       agc  * Redistribution and use in source and binary forms, with or without
     11  1.51       agc  * modification, are permitted provided that the following conditions
     12  1.51       agc  * are met:
     13  1.51       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.51       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.51       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.51       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.51       agc  *    documentation and/or other materials provided with the distribution.
     18  1.51       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.51       agc  *    may be used to endorse or promote products derived from this software
     20  1.51       agc  *    without specific prior written permission.
     21  1.51       agc  *
     22  1.51       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.51       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.51       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.51       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.51       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.51       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.51       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.51       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.51       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.51       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.51       agc  * SUCH DAMAGE.
     33  1.51       agc  */
     34  1.51       agc 
     35  1.51       agc /*
     36   1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     37   1.1       cgd  * All rights reserved.
     38   1.1       cgd  *
     39   1.1       cgd  * This code is derived from software contributed to Berkeley by
     40   1.1       cgd  * Adam de Boor.
     41   1.1       cgd  *
     42   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     43   1.1       cgd  * modification, are permitted provided that the following conditions
     44   1.1       cgd  * are met:
     45   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     46   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     47   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     48   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     49   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     50   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     51   1.1       cgd  *    must display the following acknowledgement:
     52   1.1       cgd  *	This product includes software developed by the University of
     53   1.1       cgd  *	California, Berkeley and its contributors.
     54   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     55   1.1       cgd  *    may be used to endorse or promote products derived from this software
     56   1.1       cgd  *    without specific prior written permission.
     57   1.1       cgd  *
     58   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68   1.1       cgd  * SUCH DAMAGE.
     69   1.1       cgd  */
     70   1.1       cgd 
     71  1.53      ross #ifndef MAKE_NATIVE
     72  1.61       dsl static char rcsid[] = "$NetBSD: make.c,v 1.61 2006/02/11 20:59:49 dsl Exp $";
     73  1.19     lukem #else
     74  1.18  christos #include <sys/cdefs.h>
     75   1.1       cgd #ifndef lint
     76   1.7  christos #if 0
     77  1.10  christos static char sccsid[] = "@(#)make.c	8.1 (Berkeley) 6/6/93";
     78   1.7  christos #else
     79  1.61       dsl __RCSID("$NetBSD: make.c,v 1.61 2006/02/11 20:59:49 dsl Exp $");
     80   1.7  christos #endif
     81   1.1       cgd #endif /* not lint */
     82  1.19     lukem #endif
     83   1.1       cgd 
     84   1.1       cgd /*-
     85   1.1       cgd  * make.c --
     86   1.1       cgd  *	The functions which perform the examination of targets and
     87   1.1       cgd  *	their suitability for creation
     88   1.1       cgd  *
     89   1.1       cgd  * Interface:
     90   1.1       cgd  *	Make_Run 	    	Initialize things for the module and recreate
     91   1.1       cgd  *	    	  	    	whatever needs recreating. Returns TRUE if
     92   1.1       cgd  *	    	    	    	work was (or would have been) done and FALSE
     93   1.1       cgd  *	    	  	    	otherwise.
     94   1.1       cgd  *
     95   1.1       cgd  *	Make_Update	    	Update all parents of a given child. Performs
     96   1.1       cgd  *	    	  	    	various bookkeeping chores like the updating
     97   1.1       cgd  *	    	  	    	of the cmtime field of the parent, filling
     98   1.1       cgd  *	    	  	    	of the IMPSRC context variable, etc. It will
     99   1.1       cgd  *	    	  	    	place the parent on the toBeMade queue if it
    100   1.1       cgd  *	    	  	    	should be.
    101   1.1       cgd  *
    102   1.1       cgd  *	Make_TimeStamp	    	Function to set the parent's cmtime field
    103   1.1       cgd  *	    	  	    	based on a child's modification time.
    104   1.1       cgd  *
    105   1.1       cgd  *	Make_DoAllVar	    	Set up the various local variables for a
    106   1.1       cgd  *	    	  	    	target, including the .ALLSRC variable, making
    107   1.1       cgd  *	    	  	    	sure that any variable that needs to exist
    108   1.1       cgd  *	    	  	    	at the very least has the empty value.
    109   1.1       cgd  *
    110   1.1       cgd  *	Make_OODate 	    	Determine if a target is out-of-date.
    111   1.1       cgd  *
    112   1.1       cgd  *	Make_HandleUse	    	See if a child is a .USE node for a parent
    113   1.1       cgd  *				and perform the .USE actions if so.
    114  1.11  christos  *
    115  1.11  christos  *	Make_ExpandUse	    	Expand .USE nodes and return the new list of
    116  1.11  christos  *				targets.
    117   1.1       cgd  */
    118   1.1       cgd 
    119   1.1       cgd #include    "make.h"
    120   1.4       cgd #include    "hash.h"
    121   1.4       cgd #include    "dir.h"
    122   1.4       cgd #include    "job.h"
    123   1.1       cgd 
    124   1.1       cgd static Lst     	toBeMade;	/* The current fringe of the graph. These
    125   1.1       cgd 				 * are nodes which await examination by
    126   1.1       cgd 				 * MakeOODate. It is added to by
    127   1.1       cgd 				 * Make_Update and subtracted from by
    128   1.1       cgd 				 * MakeStartJobs */
    129   1.1       cgd static int  	numNodes;   	/* Number of nodes to be processed. If this
    130   1.1       cgd 				 * is non-zero when Job_Empty() returns
    131   1.1       cgd 				 * TRUE, there's a cycle in the graph */
    132   1.1       cgd 
    133  1.50       wiz static int MakeAddChild(ClientData, ClientData);
    134  1.50       wiz static int MakeFindChild(ClientData, ClientData);
    135  1.50       wiz static int MakeUnmark(ClientData, ClientData);
    136  1.50       wiz static int MakeAddAllSrc(ClientData, ClientData);
    137  1.50       wiz static int MakeTimeStamp(ClientData, ClientData);
    138  1.50       wiz static int MakeHandleUse(ClientData, ClientData);
    139  1.50       wiz static Boolean MakeStartJobs(void);
    140  1.50       wiz static int MakePrintStatus(ClientData, ClientData);
    141   1.1       cgd /*-
    142   1.1       cgd  *-----------------------------------------------------------------------
    143   1.1       cgd  * Make_TimeStamp --
    144   1.1       cgd  *	Set the cmtime field of a parent node based on the mtime stamp in its
    145  1.10  christos  *	child. Called from MakeOODate via Lst_ForEach.
    146   1.1       cgd  *
    147  1.50       wiz  * Input:
    148  1.50       wiz  *	pgn		the current parent
    149  1.50       wiz  *	cgn		the child we've just examined
    150  1.50       wiz  *
    151   1.1       cgd  * Results:
    152  1.10  christos  *	Always returns 0.
    153   1.1       cgd  *
    154   1.1       cgd  * Side Effects:
    155   1.1       cgd  *	The cmtime of the parent node will be changed if the mtime
    156   1.1       cgd  *	field of the child is greater than it.
    157   1.1       cgd  *-----------------------------------------------------------------------
    158   1.1       cgd  */
    159   1.1       cgd int
    160  1.50       wiz Make_TimeStamp(GNode *pgn, GNode *cgn)
    161   1.1       cgd {
    162   1.1       cgd     if (cgn->mtime > pgn->cmtime) {
    163   1.1       cgd 	pgn->cmtime = cgn->mtime;
    164   1.1       cgd     }
    165   1.1       cgd     return (0);
    166   1.1       cgd }
    167   1.5       jtc 
    168  1.50       wiz /*
    169  1.50       wiz  * Input:
    170  1.50       wiz  *	pgn		the current parent
    171  1.50       wiz  *	cgn		the child we've just examined
    172  1.50       wiz  *
    173  1.50       wiz  */
    174   1.5       jtc static int
    175  1.50       wiz MakeTimeStamp(ClientData pgn, ClientData cgn)
    176   1.5       jtc {
    177  1.58  christos     return Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
    178   1.5       jtc }
    179   1.1       cgd 
    180   1.1       cgd /*-
    182   1.1       cgd  *-----------------------------------------------------------------------
    183   1.1       cgd  * Make_OODate --
    184   1.1       cgd  *	See if a given node is out of date with respect to its sources.
    185   1.1       cgd  *	Used by Make_Run when deciding which nodes to place on the
    186   1.1       cgd  *	toBeMade queue initially and by Make_Update to screen out USE and
    187   1.1       cgd  *	EXEC nodes. In the latter case, however, any other sort of node
    188   1.1       cgd  *	must be considered out-of-date since at least one of its children
    189   1.1       cgd  *	will have been recreated.
    190  1.50       wiz  *
    191  1.50       wiz  * Input:
    192  1.50       wiz  *	gn		the node to check
    193   1.1       cgd  *
    194  1.10  christos  * Results:
    195   1.1       cgd  *	TRUE if the node is out of date. FALSE otherwise.
    196   1.1       cgd  *
    197   1.1       cgd  * Side Effects:
    198   1.1       cgd  *	The mtime field of the node and the cmtime field of its parents
    199   1.1       cgd  *	will/may be changed.
    200   1.1       cgd  *-----------------------------------------------------------------------
    201   1.1       cgd  */
    202  1.50       wiz Boolean
    203   1.1       cgd Make_OODate(GNode *gn)
    204   1.1       cgd {
    205   1.1       cgd     Boolean         oodate;
    206   1.1       cgd 
    207   1.1       cgd     /*
    208   1.1       cgd      * Certain types of targets needn't even be sought as their datedness
    209   1.1       cgd      * doesn't depend on their modification time...
    210  1.39  christos      */
    211  1.57  christos     if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
    212   1.1       cgd 	(void)Dir_MTime(gn);
    213   1.1       cgd 	if (DEBUG(MAKE)) {
    214  1.57  christos 	    if (gn->mtime != 0) {
    215   1.1       cgd 		printf("modified %s...", Targ_FmtTime(gn->mtime));
    216  1.57  christos 	    } else {
    217   1.1       cgd 		printf("non-existent...");
    218   1.1       cgd 	    }
    219   1.1       cgd 	}
    220   1.1       cgd     }
    221   1.1       cgd 
    222   1.1       cgd     /*
    223   1.1       cgd      * A target is remade in one of the following circumstances:
    224   1.1       cgd      *	its modification time is smaller than that of its youngest child
    225   1.1       cgd      *	    and it would actually be run (has commands or type OP_NOP)
    226   1.1       cgd      *	it's the object of a force operator
    227   1.1       cgd      *	it has no children, was on the lhs of an operator and doesn't exist
    228   1.1       cgd      *	    already.
    229   1.1       cgd      *
    230   1.1       cgd      * Libraries are only considered out-of-date if the archive module says
    231   1.1       cgd      * they are.
    232  1.37       wiz      *
    233   1.1       cgd      * These weird rules are brought to you by Backward-Compatibility and
    234   1.1       cgd      * the strange people who wrote 'Make'.
    235  1.39  christos      */
    236   1.1       cgd     if (gn->type & (OP_USE|OP_USEBEFORE)) {
    237   1.1       cgd 	/*
    238   1.1       cgd 	 * If the node is a USE node it is *never* out of date
    239   1.1       cgd 	 * no matter *what*.
    240   1.1       cgd 	 */
    241   1.1       cgd 	if (DEBUG(MAKE)) {
    242   1.1       cgd 	    printf(".USE node...");
    243   1.1       cgd 	}
    244  1.25       sjg 	oodate = FALSE;
    245  1.25       sjg     } else if ((gn->type & OP_LIB) &&
    246   1.1       cgd 	       ((gn->mtime==0) || Arch_IsLib(gn))) {
    247   1.1       cgd 	if (DEBUG(MAKE)) {
    248   1.1       cgd 	    printf("library...");
    249   1.6  christos 	}
    250   1.6  christos 
    251   1.6  christos 	/*
    252  1.26       sjg 	 * always out of date if no children and :: target
    253   1.6  christos 	 * or non-existent.
    254  1.56  christos 	 */
    255  1.27       sjg 	oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
    256   1.1       cgd 		  (gn->cmtime == 0 && (gn->type & OP_DOUBLEDEP)));
    257   1.1       cgd     } else if (gn->type & OP_JOIN) {
    258   1.1       cgd 	/*
    259   1.1       cgd 	 * A target with the .JOIN attribute is only considered
    260   1.1       cgd 	 * out-of-date if any of its children was out-of-date.
    261   1.1       cgd 	 */
    262   1.1       cgd 	if (DEBUG(MAKE)) {
    263   1.1       cgd 	    printf(".JOIN node...");
    264  1.22  christos 	}
    265  1.22  christos 	if (DEBUG(MAKE)) {
    266  1.22  christos 	    printf("source %smade...", gn->flags & CHILDMADE ? "" : "not ");
    267  1.52       dsl 	}
    268   1.8  christos 	oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
    269   1.1       cgd     } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
    270   1.1       cgd 	/*
    271   1.1       cgd 	 * A node which is the object of the force (!) operator or which has
    272   1.1       cgd 	 * the .EXEC attribute is always considered out-of-date.
    273   1.1       cgd 	 */
    274   1.1       cgd 	if (DEBUG(MAKE)) {
    275   1.1       cgd 	    if (gn->type & OP_FORCE) {
    276   1.8  christos 		printf("! operator...");
    277   1.8  christos 	    } else if (gn->type & OP_PHONY) {
    278   1.1       cgd 		printf(".PHONY node...");
    279   1.1       cgd 	    } else {
    280   1.1       cgd 		printf(".EXEC node...");
    281   1.1       cgd 	    }
    282   1.1       cgd 	}
    283  1.52       dsl 	oodate = TRUE;
    284  1.52       dsl     } else if (gn->mtime < gn->cmtime ||
    285  1.52       dsl 	       (gn->cmtime == 0 &&
    286  1.52       dsl 		((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
    287   1.1       cgd 		  || gn->type & OP_DOUBLEDEP)))
    288   1.1       cgd     {
    289   1.1       cgd 	/*
    290   1.1       cgd 	 * A node whose modification time is less than that of its
    291  1.52       dsl 	 * youngest child or that has no children (cmtime == 0) and
    292  1.52       dsl 	 * either doesn't exist (mtime == 0) and it isn't optional
    293  1.52       dsl 	 * or was the object of a * :: operator is out-of-date.
    294   1.1       cgd 	 * Why? Because that's the way Make does it.
    295   1.1       cgd 	 */
    296   1.1       cgd 	if (DEBUG(MAKE)) {
    297   1.1       cgd 	    if (gn->mtime < gn->cmtime) {
    298   1.1       cgd 		printf("modified before source...");
    299   1.1       cgd 	    } else if (gn->mtime == 0) {
    300   1.1       cgd 		printf("non-existent and no sources...");
    301   1.1       cgd 	    } else {
    302   1.1       cgd 		printf(":: operator and no sources...");
    303   1.1       cgd 	    }
    304   1.1       cgd 	}
    305   1.1       cgd 	oodate = TRUE;
    306  1.21  christos     } else {
    307  1.22  christos 	/*
    308  1.21  christos 	 * When a non-existing child with no sources
    309  1.21  christos 	 * (such as a typically used FORCE source) has been made and
    310  1.21  christos 	 * the target of the child (usually a directory) has the same
    311  1.21  christos 	 * timestamp as the timestamp just given to the non-existing child
    312  1.21  christos 	 * after it was considered made.
    313   1.1       cgd 	 */
    314  1.22  christos 	if (DEBUG(MAKE)) {
    315  1.22  christos 	    if (gn->flags & FORCE)
    316   1.1       cgd 		printf("non existing child...");
    317  1.52       dsl 	}
    318   1.1       cgd 	oodate = (gn->flags & FORCE) ? TRUE : FALSE;
    319   1.1       cgd     }
    320   1.1       cgd 
    321   1.1       cgd     /*
    322   1.1       cgd      * If the target isn't out-of-date, the parents need to know its
    323   1.1       cgd      * modification time. Note that targets that appear to be out-of-date
    324   1.1       cgd      * but aren't, because they have no commands and aren't of type OP_NOP,
    325   1.1       cgd      * have their mtime stay below their children's mtime to keep parents from
    326   1.1       cgd      * thinking they're out-of-date.
    327   1.1       cgd      */
    328  1.56  christos     if (!oodate) {
    329   1.1       cgd 	Lst_ForEach(gn->parents, MakeTimeStamp, (ClientData)gn);
    330   1.1       cgd     }
    331   1.1       cgd 
    332   1.1       cgd     return (oodate);
    333   1.1       cgd }
    334   1.1       cgd 
    335   1.1       cgd /*-
    337   1.1       cgd  *-----------------------------------------------------------------------
    338   1.1       cgd  * MakeAddChild  --
    339   1.1       cgd  *	Function used by Make_Run to add a child to the list l.
    340  1.50       wiz  *	It will only add the child if its make field is FALSE.
    341  1.50       wiz  *
    342  1.50       wiz  * Input:
    343  1.50       wiz  *	gnp		the node to add
    344   1.1       cgd  *	lp		the list to which to add it
    345   1.1       cgd  *
    346   1.1       cgd  * Results:
    347   1.1       cgd  *	Always returns 0
    348   1.1       cgd  *
    349   1.1       cgd  * Side Effects:
    350   1.1       cgd  *	The given list is extended
    351   1.1       cgd  *-----------------------------------------------------------------------
    352  1.50       wiz  */
    353   1.1       cgd static int
    354  1.58  christos MakeAddChild(ClientData gnp, ClientData lp)
    355   1.5       jtc {
    356  1.14  christos     GNode          *gn = (GNode *)gnp;
    357  1.39  christos     Lst            l = (Lst) lp;
    358  1.57  christos 
    359   1.1       cgd     if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
    360   1.1       cgd 	(void)Lst_EnQueue(l, (ClientData)gn);
    361   1.1       cgd     }
    362  1.13  christos     return (0);
    363  1.13  christos }
    364  1.13  christos 
    365  1.13  christos /*-
    367  1.13  christos  *-----------------------------------------------------------------------
    368  1.13  christos  * MakeFindChild  --
    369  1.50       wiz  *	Function used by Make_Run to find the pathname of a child
    370  1.50       wiz  *	that was already made.
    371  1.50       wiz  *
    372  1.13  christos  * Input:
    373  1.13  christos  *	gnp		the node to find
    374  1.13  christos  *
    375  1.13  christos  * Results:
    376  1.14  christos  *	Always returns 0
    377  1.42        pk  *
    378  1.13  christos  * Side Effects:
    379  1.13  christos  *	The path and mtime of the node and the cmtime of the parent are
    380  1.13  christos  *	updated; the unmade children count of the parent is decremented.
    381  1.50       wiz  *-----------------------------------------------------------------------
    382  1.13  christos  */
    383  1.58  christos static int
    384  1.58  christos MakeFindChild(ClientData gnp, ClientData pgnp)
    385  1.14  christos {
    386  1.57  christos     GNode          *gn = (GNode *)gnp;
    387  1.22  christos     GNode          *pgn = (GNode *)pgnp;
    388  1.42        pk 
    389  1.14  christos     (void)Dir_MTime(gn);
    390  1.13  christos     Make_TimeStamp(pgn, gn);
    391  1.13  christos     pgn->unmade--;
    392  1.13  christos 
    393   1.1       cgd     return (0);
    394   1.1       cgd }
    395   1.1       cgd 
    396   1.1       cgd /*-
    398  1.44        pk  *-----------------------------------------------------------------------
    399  1.44        pk  * Make_HandleUse --
    400   1.1       cgd  *	Function called by Make_Run and SuffApplyTransform on the downward
    401   1.1       cgd  *	pass to handle .USE and transformation nodes. It implements the
    402   1.1       cgd  *	.USE and transformation functionality by copying the node's commands,
    403   1.1       cgd  *	type flags and children to the parent node.
    404   1.1       cgd  *
    405  1.50       wiz  *	A .USE node is much like an explicit transformation rule, except
    406  1.50       wiz  *	its commands are always added to the target node, even if the
    407  1.50       wiz  *	target already has commands.
    408  1.50       wiz  *
    409   1.1       cgd  * Input:
    410  1.44        pk  *	cgn		The .USE node
    411   1.1       cgd  *	pgn		The target of the .USE node
    412   1.1       cgd  *
    413   1.1       cgd  * Results:
    414   1.1       cgd  *	none
    415   1.1       cgd  *
    416   1.1       cgd  * Side Effects:
    417   1.1       cgd  *	Children and commands may be added to the parent and the parent's
    418  1.43        pk  *	type may be changed.
    419  1.50       wiz  *
    420   1.1       cgd  *-----------------------------------------------------------------------
    421  1.44        pk  */
    422  1.44        pk void
    423  1.44        pk Make_HandleUse(GNode *cgn, GNode *pgn)
    424  1.44        pk {
    425  1.44        pk     LstNode	ln; 	/* An element in the children list */
    426  1.44        pk 
    427  1.44        pk #ifdef DEBUG_SRC
    428  1.44        pk     if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
    429   1.1       cgd 	printf("Make_HandleUse: called for plain node %s\n", cgn->name);
    430  1.44        pk 	return;
    431  1.39  christos     }
    432  1.39  christos #endif
    433  1.39  christos 
    434  1.39  christos     if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
    435  1.39  christos 	    if (cgn->type & OP_USEBEFORE) {
    436  1.39  christos 		/*
    437  1.56  christos 		 * .USEBEFORE --
    438  1.57  christos 		 *	prepend the child's commands to the parent.
    439  1.56  christos 		 */
    440  1.39  christos 		Lst cmds = pgn->commands;
    441  1.39  christos 		pgn->commands = Lst_Duplicate(cgn->commands, NOCOPY);
    442  1.39  christos 		(void)Lst_Concat(pgn->commands, cmds, LST_CONCNEW);
    443  1.39  christos 		Lst_Destroy(cmds, NOFREE);
    444  1.39  christos 	    } else {
    445  1.57  christos 		/*
    446  1.39  christos 		 * .USE or target has no commands --
    447  1.44        pk 		 *	append the child's commands to the parent.
    448  1.10  christos 		 */
    449  1.56  christos 		(void)Lst_Concat(pgn->commands, cgn->commands, LST_CONCNEW);
    450  1.56  christos 	    }
    451  1.57  christos     }
    452  1.11  christos 
    453  1.44        pk     if (Lst_Open(cgn->children) == SUCCESS) {
    454  1.44        pk 	while ((ln = Lst_Next(cgn->children)) != NILLNODE) {
    455  1.44        pk 	    GNode *tgn, *gn = (GNode *)Lst_Datum(ln);
    456  1.44        pk 
    457  1.44        pk 	    /*
    458  1.44        pk 	     * Expand variables in the .USE node's name
    459  1.44        pk 	     * and save the unexpanded form.
    460  1.44        pk 	     * We don't need to do this for commands.
    461  1.44        pk 	     * They get expanded properly when we execute.
    462  1.44        pk 	     */
    463  1.44        pk 	    if (gn->uname == NULL) {
    464  1.44        pk 		gn->uname = gn->name;
    465  1.44        pk 	    } else {
    466  1.44        pk 		if (gn->name)
    467  1.44        pk 		    free(gn->name);
    468  1.44        pk 	    }
    469  1.44        pk 	    gn->name = Var_Subst(NULL, gn->uname, pgn, FALSE);
    470  1.44        pk 	    if (gn->name && gn->uname && strcmp(gn->name, gn->uname) != 0) {
    471  1.44        pk 		/* See if we have a target for this node. */
    472   1.1       cgd 		tgn = Targ_FindNode(gn->name, TARG_NOCREATE);
    473  1.57  christos 		if (tgn != NILGNODE)
    474  1.57  christos 		    gn = tgn;
    475  1.44        pk 	    }
    476   1.1       cgd 
    477  1.56  christos 	    (void)Lst_AtEnd(pgn->children, gn);
    478  1.44        pk 	    (void)Lst_AtEnd(gn->parents, pgn);
    479  1.10  christos 	    pgn->unmade += 1;
    480  1.44        pk 	}
    481   1.1       cgd 	Lst_Close(cgn->children);
    482  1.31   mycroft     }
    483  1.44        pk 
    484  1.44        pk     pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
    485  1.44        pk }
    486  1.44        pk 
    487  1.44        pk /*-
    488  1.44        pk  *-----------------------------------------------------------------------
    489  1.44        pk  * MakeHandleUse --
    490  1.44        pk  *	Callback function for Lst_ForEach, used by Make_Run on the downward
    491  1.44        pk  *	pass to handle .USE nodes. Should be called before the children
    492  1.50       wiz  *	are enqueued to be looked at by MakeAddChild.
    493  1.50       wiz  *	This function calls Make_HandleUse to copy the .USE node's commands,
    494  1.50       wiz  *	type flags and children to the parent node.
    495  1.50       wiz  *
    496  1.44        pk  * Input:
    497  1.44        pk  *	cgnp		the child we've just examined
    498  1.44        pk  *	pgnp		the current parent
    499  1.44        pk  *
    500  1.44        pk  * Results:
    501  1.44        pk  *	returns 0.
    502  1.44        pk  *
    503  1.44        pk  * Side Effects:
    504   1.5       jtc  *	After expansion, .USE child nodes are removed from the parent
    505  1.50       wiz  *
    506   1.5       jtc  *-----------------------------------------------------------------------
    507  1.58  christos  */
    508  1.58  christos static int
    509  1.43        pk MakeHandleUse(ClientData cgnp, ClientData pgnp)
    510  1.44        pk {
    511  1.44        pk     GNode	*cgn = (GNode *)cgnp;
    512  1.44        pk     GNode	*pgn = (GNode *)pgnp;
    513  1.44        pk     LstNode	ln; 	/* An element in the children list */
    514  1.31   mycroft     int		unmarked;
    515  1.44        pk 
    516  1.44        pk     unmarked = ((cgn->type & OP_MARK) == 0);
    517  1.44        pk     cgn->type |= OP_MARK;
    518  1.44        pk 
    519  1.43        pk     if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
    520  1.31   mycroft 	return (0);
    521  1.43        pk 
    522  1.43        pk     if (unmarked)
    523  1.43        pk 	Make_HandleUse(cgn, pgn);
    524  1.43        pk 
    525  1.43        pk     /*
    526  1.43        pk      * This child node is now "made", so we decrement the count of
    527  1.43        pk      * unmade children in the parent... We also remove the child
    528  1.56  christos      * from the parent's list to accurately reflect the number of decent
    529  1.43        pk      * children the parent has. This is used by Make_Run to decide
    530  1.43        pk      * whether to queue the parent or examine its children...
    531  1.43        pk      */
    532  1.43        pk     if ((ln = Lst_Member(pgn->children, (ClientData) cgn)) != NILLNODE) {
    533   1.5       jtc 	Lst_Remove(pgn->children, ln);
    534  1.22  christos 	pgn->unmade--;
    535  1.22  christos     }
    536  1.22  christos     return (0);
    537  1.22  christos }
    538  1.22  christos 
    539  1.22  christos 
    540  1.22  christos /*-
    541  1.22  christos  *-----------------------------------------------------------------------
    542  1.22  christos  * Make_Recheck --
    543  1.22  christos  *	Check the modification time of a gnode, and update it as described
    544  1.22  christos  *	in the comments below.
    545  1.22  christos  *
    546  1.22  christos  * Results:
    547  1.22  christos  *	returns 0 if the gnode does not exist, or it's filesystem
    548  1.22  christos  *	time if it does.
    549  1.22  christos  *
    550  1.22  christos  * Side Effects:
    551  1.22  christos  *	the gnode's modification time and path name are affected.
    552  1.50       wiz  *
    553  1.22  christos  *-----------------------------------------------------------------------
    554  1.22  christos  */
    555  1.22  christos time_t
    556  1.22  christos Make_Recheck(GNode *gn)
    557  1.22  christos {
    558  1.22  christos     time_t mtime = Dir_MTime(gn);
    559  1.22  christos 
    560  1.22  christos #ifndef RECHECK
    561  1.22  christos     /*
    562  1.22  christos      * We can't re-stat the thing, but we can at least take care of rules
    563  1.22  christos      * where a target depends on a source that actually creates the
    564  1.22  christos      * target, but only if it has changed, e.g.
    565  1.22  christos      *
    566  1.22  christos      * parse.h : parse.o
    567  1.22  christos      *
    568  1.22  christos      * parse.o : parse.y
    569  1.22  christos      *  	yacc -d parse.y
    570  1.22  christos      *  	cc -c y.tab.c
    571  1.22  christos      *  	mv y.tab.o parse.o
    572  1.22  christos      *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
    573  1.22  christos      *
    574  1.22  christos      * In this case, if the definitions produced by yacc haven't changed
    575  1.22  christos      * from before, parse.h won't have been updated and gn->mtime will
    576  1.22  christos      * reflect the current modification time for parse.h. This is
    577  1.22  christos      * something of a kludge, I admit, but it's a useful one..
    578  1.22  christos      * XXX: People like to use a rule like
    579  1.22  christos      *
    580  1.22  christos      * FRC:
    581  1.22  christos      *
    582  1.22  christos      * To force things that depend on FRC to be made, so we have to
    583  1.22  christos      * check for gn->children being empty as well...
    584  1.22  christos      */
    585  1.22  christos     if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
    586  1.22  christos 	gn->mtime = now;
    587  1.22  christos     }
    588  1.22  christos #else
    589  1.22  christos     /*
    590  1.22  christos      * This is what Make does and it's actually a good thing, as it
    591  1.22  christos      * allows rules like
    592  1.22  christos      *
    593  1.22  christos      *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
    594  1.22  christos      *
    595  1.22  christos      * to function as intended. Unfortunately, thanks to the stateless
    596  1.22  christos      * nature of NFS (by which I mean the loose coupling of two clients
    597  1.22  christos      * using the same file from a common server), there are times
    598  1.22  christos      * when the modification time of a file created on a remote
    599  1.22  christos      * machine will not be modified before the local stat() implied by
    600  1.22  christos      * the Dir_MTime occurs, thus leading us to believe that the file
    601  1.22  christos      * is unchanged, wreaking havoc with files that depend on this one.
    602  1.22  christos      *
    603  1.22  christos      * I have decided it is better to make too much than to make too
    604  1.22  christos      * little, so this stuff is commented out unless you're sure it's ok.
    605  1.22  christos      * -- ardeb 1/12/88
    606  1.22  christos      */
    607  1.22  christos     /*
    608  1.34  sommerfe      * Christos, 4/9/92: If we are  saving commands pretend that
    609  1.22  christos      * the target is made now. Otherwise archives with ... rules
    610  1.22  christos      * don't work!
    611  1.42        pk      */
    612  1.42        pk     if (NoExecute(gn) ||
    613  1.22  christos 	(gn->type & OP_SAVE_CMDS) || mtime == 0) {
    614  1.22  christos 	if (DEBUG(MAKE)) {
    615  1.22  christos 	    printf(" recheck(%s): update time to now: %s\n",
    616  1.22  christos 		   gn->name, Targ_FmtTime(gn->mtime));
    617  1.22  christos 	}
    618  1.42        pk 	gn->mtime = now;
    619  1.42        pk     }
    620  1.22  christos     else {
    621  1.22  christos 	if (DEBUG(MAKE)) {
    622  1.22  christos 	    printf(" recheck(%s): current update time: %s\n",
    623  1.22  christos 		   gn->name, Targ_FmtTime(gn->mtime));
    624  1.22  christos 	}
    625  1.22  christos     }
    626   1.1       cgd #endif
    627   1.1       cgd     return mtime;
    628   1.1       cgd }
    629   1.1       cgd 
    630   1.1       cgd /*-
    631  1.10  christos  *-----------------------------------------------------------------------
    632   1.1       cgd  * Make_Update  --
    633  1.50       wiz  *	Perform update on the parents of a node. Used by JobFinish once
    634  1.50       wiz  *	a node has been dealt with and by MakeStartJobs if it finds an
    635  1.50       wiz  *	up-to-date node.
    636   1.1       cgd  *
    637   1.1       cgd  * Input:
    638   1.1       cgd  *	cgn		the child node
    639   1.1       cgd  *
    640   1.1       cgd  * Results:
    641   1.1       cgd  *	Always returns 0
    642   1.1       cgd  *
    643  1.22  christos  * Side Effects:
    644  1.22  christos  *	The unmade field of pgn is decremented and pgn may be placed on
    645  1.22  christos  *	the toBeMade queue if this field becomes 0.
    646  1.22  christos  *
    647  1.22  christos  * 	If the child was made, the parent's flag CHILDMADE field will be
    648   1.1       cgd  *	set true and its cmtime set to now.
    649   1.1       cgd  *
    650   1.1       cgd  *	If the child is not up-to-date and still does not exist,
    651   1.1       cgd  *	set the FORCE flag on the parents.
    652   1.1       cgd  *
    653   1.1       cgd  *	If the child wasn't made, the cmtime field of the parent will be
    654   1.1       cgd  *	altered if the child's mtime is big enough.
    655   1.1       cgd  *
    656   1.1       cgd  *	Finally, if the child is the implied source for the parent, the
    657   1.1       cgd  *	parent's IMPSRC variable is set appropriately.
    658  1.50       wiz  *
    659   1.1       cgd  *-----------------------------------------------------------------------
    660  1.44        pk  */
    661  1.44        pk void
    662  1.44        pk Make_Update(GNode *cgn)
    663  1.44        pk {
    664  1.44        pk     GNode 	*pgn;	/* the parent node */
    665  1.47        pk     char  	*cname;	/* the child's name */
    666  1.47        pk     LstNode	ln; 	/* Element in parents and iParents lists */
    667   1.1       cgd     time_t	mtime = -1;
    668  1.56  christos     char	*p1;
    669   1.5       jtc     Lst		parents;
    670   1.5       jtc     GNode	*centurion;
    671   1.1       cgd 
    672   1.1       cgd     cname = Var_Value(TARGET, cgn, &p1);
    673   1.1       cgd     if (p1)
    674   1.1       cgd 	free(p1);
    675   1.1       cgd 
    676   1.1       cgd     /*
    677   1.1       cgd      * If the child was actually made, see what its modification time is
    678  1.22  christos      * now -- some rules won't actually update the file. If the file still
    679   1.1       cgd      * doesn't exist, make its mtime now.
    680  1.10  christos      */
    681  1.47        pk     if (cgn->made != UPTODATE) {
    682  1.47        pk 	mtime = Make_Recheck(cgn);
    683  1.47        pk     }
    684  1.47        pk 
    685  1.47        pk     /*
    686  1.47        pk      * If this is a `::' node, we must consult its first instance
    687  1.47        pk      * which is where all parents are linked.
    688  1.47        pk      */
    689  1.47        pk     if ((centurion = cgn->centurion) != NULL) {
    690  1.56  christos 	if (!Lst_IsEmpty(cgn->parents))
    691  1.47        pk 		Punt("%s: cohort has parents", cgn->name);
    692  1.47        pk 	centurion->unmade_cohorts -= 1;
    693  1.47        pk 	if (centurion->unmade_cohorts < 0)
    694  1.47        pk 	    Error("Graph cycles through centurion %s", centurion->name);
    695  1.47        pk 	parents = centurion->parents;
    696  1.56  christos     } else {
    697  1.56  christos 	centurion = cgn;
    698  1.57  christos 	parents = cgn->parents;
    699  1.22  christos     }
    700  1.22  christos     if (Lst_Open(parents) == SUCCESS) {
    701  1.42        pk 	while ((ln = Lst_Next(parents)) != NILLNODE) {
    702  1.42        pk 	    pgn = (GNode *)Lst_Datum(ln);
    703  1.42        pk 	    if (mtime == 0)
    704  1.42        pk 		pgn->flags |= FORCE;
    705  1.42        pk 	    /*
    706  1.47        pk 	     * If the parent has the .MADE attribute, it has already
    707  1.47        pk 	     * been queued on the `toBeMade' list in Make_ExpandUse()
    708  1.47        pk 	     * and its unmade children counter is zero.
    709  1.47        pk 	     */
    710  1.47        pk 	    if ((pgn->flags & REMAKE) == 0 || (pgn->type & OP_MADE) != 0)
    711  1.47        pk 		continue;
    712  1.57  christos 
    713  1.47        pk 	    if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
    714   1.1       cgd 		if (cgn->made == MADE)
    715  1.47        pk 		    pgn->flags |= CHILDMADE;
    716  1.47        pk 		(void)Make_TimeStamp(pgn, cgn);
    717  1.47        pk 	    }
    718  1.47        pk 
    719  1.47        pk 	    /*
    720  1.47        pk 	     * A parent must wait for the completion of all instances
    721  1.47        pk 	     * of a `::' dependency.
    722  1.47        pk 	     */
    723  1.47        pk 	    if (centurion->unmade_cohorts != 0 || centurion->made == UNMADE)
    724  1.47        pk 		continue;
    725  1.47        pk 
    726  1.47        pk 	    /* One more child of this parent is now made */
    727  1.47        pk 	    pgn->unmade -= 1;
    728  1.47        pk 	    if (pgn->unmade == 0) {
    729  1.61       dsl 		/*
    730  1.61       dsl 		 * Queue the node up -- any unmade predecessors will
    731  1.61       dsl 		 * be dealt with in MakeStartJobs.
    732  1.61       dsl 		 */
    733  1.57  christos 		if (DEBUG(MAKE)) {
    734  1.47        pk 		    printf("# %s made, schedule %s\n", cgn->name, pgn->name);
    735  1.56  christos 		    Targ_PrintNode(pgn, 0);
    736   1.1       cgd 		}
    737   1.1       cgd 		(void)Lst_EnQueue(toBeMade, (ClientData)pgn);
    738  1.56  christos 	    } else if (pgn->unmade < 0) {
    739   1.1       cgd 		Error("Graph cycles through %s", pgn->name);
    740   1.1       cgd 	    }
    741   1.1       cgd 	}
    742   1.1       cgd 	Lst_Close(parents);
    743   1.1       cgd     }
    744   1.1       cgd     /*
    745   1.1       cgd      * Deal with successor nodes. If any is marked for making and has an unmade
    746  1.48        pk      * count of 0, has not been made and isn't in the examination queue,
    747  1.48        pk      * it means we need to place it in the queue as it restrained itself
    748   1.1       cgd      * before.
    749   1.1       cgd      */
    750  1.48        pk     for (ln = Lst_First(centurion->successors); ln != NILLNODE;
    751  1.48        pk 	 ln = Lst_Succ(ln)) {
    752   1.1       cgd 	GNode	*succ = (GNode *)Lst_Datum(ln);
    753   1.1       cgd 
    754   1.1       cgd 	if ((succ->flags & REMAKE) != 0 && succ->unmade == 0 &&
    755   1.1       cgd 	    succ->made == UNMADE &&
    756   1.1       cgd 	    Lst_Member(toBeMade, (ClientData)succ) == NILLNODE)
    757  1.10  christos 	{
    758   1.1       cgd 	    (void)Lst_EnQueue(toBeMade, (ClientData)succ);
    759   1.1       cgd 	}
    760   1.1       cgd     }
    761   1.1       cgd 
    762  1.56  christos     /*
    763   1.5       jtc      * Set the .PREFIX and .IMPSRC variables for all the implied parents
    764   1.1       cgd      * of this node.
    765  1.56  christos      */
    766  1.57  christos     if (Lst_Open(cgn->iParents) == SUCCESS) {
    767  1.22  christos 	char	*cpref = Var_Value(PREFIX, cgn, &p1);
    768  1.56  christos 
    769  1.35  christos 	while ((ln = Lst_Next(cgn->iParents)) != NILLNODE) {
    770  1.56  christos 	    pgn = (GNode *)Lst_Datum(ln);
    771   1.1       cgd 	    if (pgn->flags & REMAKE) {
    772   1.1       cgd 		Var_Set(IMPSRC, cname, pgn, 0);
    773   1.5       jtc 		if (cpref != NULL)
    774   1.5       jtc 		    Var_Set(PREFIX, cpref, pgn, 0);
    775  1.56  christos 	    }
    776   1.1       cgd 	}
    777   1.1       cgd 	if (p1)
    778   1.1       cgd 	    free(p1);
    779   1.1       cgd 	Lst_Close(cgn->iParents);
    780   1.1       cgd     }
    781   1.1       cgd }
    782   1.1       cgd 
    783   1.1       cgd /*-
    785   1.1       cgd  *-----------------------------------------------------------------------
    786  1.45        pk  * MakeAddAllSrc --
    787  1.45        pk  *	Add a child's name to the ALLSRC and OODATE variables of the given
    788   1.1       cgd  *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
    789   1.1       cgd  *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
    790   1.1       cgd  *	.EXEC and .USE children are very rarely going to be files, so...
    791   1.1       cgd  *	If the child is a .JOIN node, its ALLSRC is propagated to the parent.
    792   1.1       cgd  *
    793   1.1       cgd  *	A child is added to the OODATE variable if its modification time is
    794   1.1       cgd  *	later than that of its parent, as defined by Make, except if the
    795   1.1       cgd  *	parent is a .JOIN node. In that case, it is only added to the OODATE
    796   1.1       cgd  *	variable if it was actually made (since .JOIN nodes don't have
    797   1.1       cgd  *	modification times, the comparison is rather unfair...)..
    798   1.1       cgd  *
    799   1.1       cgd  * Results:
    800   1.1       cgd  *	Always returns 0
    801   1.1       cgd  *
    802  1.55       jmc  * Side Effects:
    803  1.31   mycroft  *	The ALLSRC variable for the given node is extended.
    804  1.58  christos  *-----------------------------------------------------------------------
    805  1.31   mycroft  */
    806  1.31   mycroft static int
    807  1.31   mycroft MakeUnmark(ClientData cgnp, ClientData pgnp __unused)
    808  1.31   mycroft {
    809  1.31   mycroft     GNode	*cgn = (GNode *)cgnp;
    810  1.50       wiz 
    811  1.50       wiz     cgn->type &= ~OP_MARK;
    812  1.50       wiz     return (0);
    813  1.50       wiz }
    814  1.50       wiz 
    815  1.50       wiz /*
    816  1.50       wiz  * Input:
    817  1.31   mycroft  *	cgnp		The child to add
    818  1.50       wiz  *	pgnp		The parent to whose ALLSRC variable it should
    819   1.1       cgd  *			be added
    820  1.58  christos  *
    821  1.58  christos  */
    822  1.31   mycroft static int
    823  1.31   mycroft MakeAddAllSrc(ClientData cgnp, ClientData pgnp)
    824  1.31   mycroft {
    825  1.31   mycroft     GNode	*cgn = (GNode *)cgnp;
    826  1.31   mycroft     GNode	*pgn = (GNode *)pgnp;
    827  1.39  christos 
    828  1.45        pk     if (cgn->type & OP_MARK)
    829  1.45        pk 	return (0);
    830   1.1       cgd     cgn->type |= OP_MARK;
    831  1.17  christos 
    832  1.56  christos     if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
    833  1.17  christos 	char *child, *allsrc;
    834  1.17  christos 	char *p1 = NULL, *p2 = NULL;
    835  1.45        pk 
    836  1.56  christos 	if (cgn->type & OP_ARCHV)
    837  1.45        pk 	    child = Var_Value(MEMBER, cgn, &p1);
    838  1.45        pk 	else
    839  1.45        pk 	    child = cgn->path ? cgn->path : cgn->name;
    840  1.46        pk 	if (cgn->type & OP_JOIN) {
    841  1.56  christos 	    allsrc = Var_Value(ALLSRC, cgn, &p2);
    842  1.45        pk 	} else {
    843  1.45        pk 	    allsrc = child;
    844   1.1       cgd 	}
    845   1.1       cgd 	if (allsrc != NULL)
    846   1.1       cgd 		Var_Append(ALLSRC, allsrc, pgn);
    847   1.1       cgd 	if (p2)
    848   1.1       cgd 	    free(p2);
    849   1.1       cgd 	if (pgn->type & OP_JOIN) {
    850   1.1       cgd 	    if (cgn->made == MADE) {
    851   1.1       cgd 		Var_Append(OODATE, child, pgn);
    852   1.1       cgd 	    }
    853   1.1       cgd 	} else if ((pgn->mtime < cgn->mtime) ||
    854   1.1       cgd 		   (cgn->mtime >= now && cgn->made == MADE))
    855   1.1       cgd 	{
    856   1.1       cgd 	    /*
    857   1.1       cgd 	     * It goes in the OODATE variable if the parent is younger than the
    858   1.1       cgd 	     * child or if the child has been modified more recently than
    859   1.1       cgd 	     * the start of the make. This is to keep pmake from getting
    860   1.1       cgd 	     * confused if something else updates the parent after the
    861   1.1       cgd 	     * make starts (shouldn't happen, I know, but sometimes it
    862   1.1       cgd 	     * does). In such a case, if we've updated the kid, the parent
    863   1.1       cgd 	     * is likely to have a modification time later than that of
    864   1.1       cgd 	     * the kid and anything that relies on the OODATE variable will
    865   1.1       cgd 	     * be hosed.
    866   1.1       cgd 	     *
    867   1.1       cgd 	     * XXX: This will cause all made children to go in the OODATE
    868   1.1       cgd 	     * variable, even if they're not touched, if RECHECK isn't defined,
    869   1.5       jtc 	     * since cgn->mtime is set to now in Make_Update. According to
    870   1.5       jtc 	     * some people, this is good...
    871   1.1       cgd 	     */
    872   1.1       cgd 	    Var_Append(OODATE, child, pgn);
    873   1.1       cgd 	}
    874   1.1       cgd 	if (p1)
    875   1.1       cgd 	    free(p1);
    876   1.1       cgd     }
    877   1.1       cgd     return (0);
    878   1.1       cgd }
    879   1.1       cgd 
    880   1.1       cgd /*-
    882   1.1       cgd  *-----------------------------------------------------------------------
    883   1.1       cgd  * Make_DoAllVar --
    884   1.1       cgd  *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
    885   1.1       cgd  *	done separately, rather than while traversing the graph. This is
    886   1.1       cgd  *	because Make defined OODATE to contain all sources whose modification
    887   1.1       cgd  *	times were later than that of the target, *not* those sources that
    888   1.1       cgd  *	were out-of-date. Since in both compatibility and native modes,
    889   1.1       cgd  *	the modification time of the parent isn't found until the child
    890   1.1       cgd  *	has been dealt with, we have to wait until now to fill in the
    891   1.1       cgd  *	variable. As for ALLSRC, the ordering is important and not
    892   1.1       cgd  *	guaranteed when in native mode, so it must be set here, too.
    893   1.1       cgd  *
    894   1.1       cgd  * Results:
    895   1.1       cgd  *	None
    896   1.1       cgd  *
    897   1.1       cgd  * Side Effects:
    898  1.50       wiz  *	The ALLSRC and OODATE variables of the given node is filled in.
    899   1.1       cgd  *	If the node is a .JOIN node, its TARGET variable will be set to
    900  1.56  christos  * 	match its ALLSRC variable.
    901  1.56  christos  *-----------------------------------------------------------------------
    902   1.1       cgd  */
    903   1.1       cgd void
    904  1.56  christos Make_DoAllVar(GNode *gn)
    905   1.1       cgd {
    906   1.1       cgd     Lst_ForEach(gn->children, MakeUnmark, (ClientData) gn);
    907  1.56  christos     Lst_ForEach(gn->children, MakeAddAllSrc, (ClientData) gn);
    908   1.1       cgd 
    909   1.1       cgd     if (!Var_Exists (OODATE, gn)) {
    910   1.1       cgd 	Var_Set(OODATE, "", gn, 0);
    911   1.5       jtc     }
    912  1.56  christos     if (!Var_Exists (ALLSRC, gn)) {
    913   1.5       jtc 	Var_Set(ALLSRC, "", gn, 0);
    914   1.5       jtc     }
    915   1.1       cgd 
    916   1.1       cgd     if (gn->type & OP_JOIN) {
    917   1.1       cgd 	char *p1;
    918   1.1       cgd 	Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn, 0);
    919   1.1       cgd 	if (p1)
    920   1.1       cgd 	    free(p1);
    921   1.1       cgd     }
    922   1.1       cgd }
    923   1.1       cgd 
    924   1.1       cgd /*-
    926   1.1       cgd  *-----------------------------------------------------------------------
    927   1.1       cgd  * MakeStartJobs --
    928   1.1       cgd  *	Start as many jobs as possible.
    929   1.1       cgd  *
    930   1.1       cgd  * Results:
    931   1.1       cgd  *	If the query flag was given to pmake, no job will be started,
    932   1.1       cgd  *	but as soon as an out-of-date target is found, this function
    933   1.1       cgd  *	returns TRUE. At all other times, this function returns FALSE.
    934   1.1       cgd  *
    935  1.50       wiz  * Side Effects:
    936   1.1       cgd  *	Nodes are removed from the toBeMade queue and job table slots
    937  1.44        pk  *	are filled.
    938  1.59       dsl  *
    939  1.10  christos  *-----------------------------------------------------------------------
    940  1.32  sommerfe  */
    941  1.60       dsl static Boolean
    942  1.60       dsl MakeStartJobs(void)
    943  1.60       dsl {
    944  1.60       dsl     GNode	*gn;
    945  1.60       dsl     int		have_token = 0;
    946  1.57  christos 
    947   1.1       cgd     while (!Lst_IsEmpty (toBeMade)) {
    948  1.57  christos 	/* Get token now to avoid cycling job-list when we only have 1 token */
    949   1.1       cgd 	if (!have_token && !Job_TokenWithdraw())
    950   1.1       cgd 	    break;
    951   1.1       cgd 	have_token = 1;
    952   1.1       cgd 
    953   1.1       cgd 	gn = (GNode *)Lst_DeQueue(toBeMade);
    954   1.1       cgd 	if (DEBUG(MAKE)) {
    955   1.1       cgd 	    printf("Examining %s...", gn->name);
    956   1.1       cgd 	}
    957   1.1       cgd 	/*
    958   1.1       cgd 	 * Make sure any and all predecessors that are going to be made,
    959   1.1       cgd 	 * have been.
    960  1.48        pk 	 */
    961  1.48        pk 	if (!Lst_IsEmpty(gn->preds)) {
    962   1.1       cgd 	    LstNode ln;
    963   1.1       cgd 
    964   1.1       cgd 	    for (ln = Lst_First(gn->preds); ln != NILLNODE; ln = Lst_Succ(ln)){
    965   1.1       cgd 		GNode	*pgn = (GNode *)Lst_Datum(ln);
    966   1.1       cgd 
    967   1.1       cgd 		if ((pgn->flags & REMAKE) &&
    968   1.1       cgd 		    (pgn->made == UNMADE || pgn->unmade_cohorts != 0)) {
    969   1.1       cgd 		    if (DEBUG(MAKE)) {
    970   1.1       cgd 			printf("predecessor %s not made yet.\n", pgn->name);
    971   1.1       cgd 		    }
    972   1.1       cgd 		    break;
    973   1.1       cgd 		}
    974   1.1       cgd 	    }
    975   1.1       cgd 	    /*
    976   1.1       cgd 	     * If ln isn't nil, there's a predecessor as yet unmade, so we
    977   1.1       cgd 	     * just drop this node on the floor. When the node in question
    978  1.10  christos 	     * has been made, it will notice this node as being ready to
    979   1.1       cgd 	     * make but as yet unmade and will place the node on the queue.
    980  1.56  christos 	     */
    981   1.1       cgd 	    if (ln != NILLNODE) {
    982  1.57  christos 		continue;
    983   1.1       cgd 	    }
    984   1.1       cgd 	}
    985   1.1       cgd 
    986   1.1       cgd 	numNodes--;
    987  1.56  christos 	if (Make_OODate(gn)) {
    988  1.56  christos 	    if (DEBUG(MAKE)) {
    989  1.59       dsl 		printf("out-of-date\n");
    990   1.1       cgd 	    }
    991   1.1       cgd 	    if (queryFlag) {
    992  1.57  christos 		return (TRUE);
    993   1.1       cgd 	    }
    994   1.1       cgd 	    Make_DoAllVar(gn);
    995   1.1       cgd 	    Job_Make(gn);
    996   1.1       cgd 	    have_token = 0;
    997   1.1       cgd 	} else {
    998   1.1       cgd 	    if (DEBUG(MAKE)) {
    999   1.1       cgd 		printf("up-to-date\n");
   1000   1.1       cgd 	    }
   1001   1.1       cgd 	    gn->made = UPTODATE;
   1002  1.56  christos 	    if (gn->type & OP_JOIN) {
   1003   1.1       cgd 		/*
   1004  1.56  christos 		 * Even for an up-to-date .JOIN node, we need it to have its
   1005   1.1       cgd 		 * context variables so references to it get the correct
   1006   1.1       cgd 		 * value for .TARGET when building up the context variables
   1007  1.59       dsl 		 * of its parent(s)...
   1008  1.59       dsl 		 */
   1009  1.59       dsl 		Make_DoAllVar(gn);
   1010  1.59       dsl 	    }
   1011   1.1       cgd 	    Make_Update(gn);
   1012   1.1       cgd 	}
   1013   1.1       cgd     }
   1014   1.1       cgd 
   1015   1.1       cgd     if (have_token)
   1016   1.1       cgd 	Job_TokenReturn();
   1017   1.1       cgd 
   1018   1.1       cgd     return (FALSE);
   1019   1.1       cgd }
   1020   1.1       cgd 
   1021  1.50       wiz /*-
   1023  1.50       wiz  *-----------------------------------------------------------------------
   1024  1.50       wiz  * MakePrintStatus --
   1025  1.50       wiz  *	Print the status of a top-level node, viz. it being up-to-date
   1026  1.50       wiz  *	already or not created due to an error in a lower level.
   1027   1.1       cgd  *	Callback function for Make_Run via Lst_ForEach.
   1028   1.1       cgd  *
   1029   1.1       cgd  * Input:
   1030   1.1       cgd  *	gnp		Node to examine
   1031   1.1       cgd  *	cyclep		True if gn->unmade being non-zero implies a
   1032   1.1       cgd  *			cycle in the graph, not an error in an
   1033   1.1       cgd  *			inferior.
   1034   1.1       cgd  *
   1035   1.1       cgd  * Results:
   1036  1.50       wiz  *	Always returns 0.
   1037   1.1       cgd  *
   1038  1.58  christos  * Side Effects:
   1039  1.58  christos  *	A message may be printed.
   1040   1.1       cgd  *
   1041  1.57  christos  *-----------------------------------------------------------------------
   1042   1.1       cgd  */
   1043   1.1       cgd static int
   1044   1.5       jtc MakePrintStatus(ClientData gnp, ClientData cyclep)
   1045   1.1       cgd {
   1046   1.1       cgd     GNode   	*gn = (GNode *)gnp;
   1047   1.1       cgd     Boolean 	cycle = *(Boolean *)cyclep;
   1048   1.1       cgd     if (gn->made == UPTODATE) {
   1049   1.1       cgd 	printf("`%s' is up to date.\n", gn->name);
   1050   1.1       cgd     } else if (gn->unmade != 0) {
   1051   1.1       cgd 	if (cycle) {
   1052   1.1       cgd 	    Boolean t = TRUE;
   1053   1.1       cgd 	    /*
   1054   1.1       cgd 	     * If printing cycles and came to one that has unmade children,
   1055   1.1       cgd 	     * print out the cycle by recursing on its children. Note a
   1056   1.1       cgd 	     * cycle like:
   1057   1.1       cgd 	     *	a : b
   1058   1.5       jtc 	     *	b : c
   1059   1.1       cgd 	     *	c : b
   1060   1.1       cgd 	     * will cause this to erroneously complain about a being in
   1061   1.1       cgd 	     * the cycle, but this is a good approximation.
   1062   1.5       jtc 	     */
   1063   1.1       cgd 	    if (gn->made == CYCLE) {
   1064   1.1       cgd 		Error("Graph cycles through `%s'", gn->name);
   1065  1.57  christos 		gn->made = ENDCYCLE;
   1066   1.1       cgd 		Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
   1067   1.1       cgd 		gn->made = UNMADE;
   1068   1.1       cgd 	    } else if (gn->made != ENDCYCLE) {
   1069   1.1       cgd 		gn->made = CYCLE;
   1070   1.1       cgd 		Lst_ForEach(gn->children, MakePrintStatus, (ClientData) &t);
   1071  1.11  christos 	    }
   1072   1.1       cgd 	} else {
   1073   1.1       cgd 	    printf("`%s' not remade because of errors.\n", gn->name);
   1074  1.11  christos 	}
   1075  1.11  christos     }
   1076  1.50       wiz     return (0);
   1077  1.50       wiz }
   1078  1.50       wiz 
   1079  1.50       wiz 
   1081  1.11  christos /*-
   1082   1.1       cgd  *-----------------------------------------------------------------------
   1083   1.1       cgd  * Make_ExpandUse --
   1084  1.11  christos  *	Expand .USE nodes and create a new targets list
   1085   1.1       cgd  *
   1086   1.1       cgd  * Input:
   1087  1.11  christos  *	targs		the initial list of targets
   1088  1.50       wiz  *
   1089   1.1       cgd  * Results:
   1090  1.44        pk  *	The new list of targets.
   1091  1.44        pk  *
   1092  1.44        pk  * Side Effects:
   1093   1.1       cgd  *	numNodes is set to the number of elements in the list of targets.
   1094  1.56  christos  *-----------------------------------------------------------------------
   1095   1.1       cgd  */
   1096   1.1       cgd Lst
   1097   1.1       cgd Make_ExpandUse(Lst targs)
   1098  1.10  christos {
   1099   1.1       cgd     GNode  *gn;		/* a temporary pointer */
   1100   1.1       cgd     Lst    examine; 	/* List of targets to examine */
   1101   1.1       cgd     Lst    ntargs;	/* List of new targets to be made */
   1102   1.1       cgd 
   1103   1.1       cgd     ntargs = Lst_Init(FALSE);
   1104   1.1       cgd 
   1105  1.10  christos     examine = Lst_Duplicate(targs, NOCOPY);
   1106   1.1       cgd     numNodes = 0;
   1107   1.1       cgd 
   1108  1.57  christos     /*
   1109  1.10  christos      * Make an initial downward pass over the graph, marking nodes to be made
   1110  1.24   mycroft      * as we go down. We call Suff_FindDeps to find where a node is and
   1111  1.23   mycroft      * to get some children for it if it has none and also has no commands.
   1112  1.56  christos      * If the node is a leaf, we stick it on the toBeMade queue to
   1113  1.56  christos      * be looked at in a minute, otherwise we add its children to our queue
   1114  1.23   mycroft      * and go on about our business.
   1115  1.23   mycroft      */
   1116  1.23   mycroft     while (!Lst_IsEmpty (examine)) {
   1117  1.22  christos 	gn = (GNode *)Lst_DeQueue(examine);
   1118  1.22  christos 
   1119   1.1       cgd 	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) {
   1120  1.10  christos 	    Lst new;
   1121   1.1       cgd 	    new = Lst_Duplicate(gn->cohorts, NOCOPY);
   1122   1.1       cgd 	    Lst_Concat(new, examine, LST_CONCLINK);
   1123   1.1       cgd 	    examine = new;
   1124  1.11  christos 	}
   1125  1.11  christos 
   1126   1.1       cgd 	if ((gn->flags & REMAKE) == 0) {
   1127  1.17  christos 	    gn->flags |= REMAKE;
   1128  1.17  christos 	    numNodes++;
   1129  1.17  christos 
   1130  1.17  christos 	    /*
   1131  1.17  christos 	     * Apply any .USE rules before looking for implicit dependencies
   1132  1.17  christos 	     * to make sure everything has commands that should...
   1133  1.17  christos 	     * Make sure that the TARGET is set, so that we can make
   1134  1.17  christos 	     * expansions.
   1135  1.56  christos 	     */
   1136  1.56  christos 	    if (gn->type & OP_ARCHV) {
   1137  1.17  christos 		char *eoa, *eon;
   1138  1.17  christos 		eoa = strchr(gn->name, '(');
   1139  1.17  christos 		eon = strchr(gn->name, ')');
   1140  1.17  christos 		if (eoa == NULL || eon == NULL)
   1141  1.22  christos 		    continue;
   1142  1.56  christos 		*eoa = '\0';
   1143  1.56  christos 		*eon = '\0';
   1144  1.56  christos 		Var_Set(MEMBER, eoa + 1, gn, 0);
   1145  1.42        pk 		Var_Set(ARCHIVE, gn->name, gn, 0);
   1146  1.41        pk 		*eoa = '(';
   1147  1.56  christos 		*eon = ')';
   1148  1.42        pk 	    }
   1149  1.42        pk 
   1150  1.56  christos 	    (void)Dir_MTime(gn);
   1151  1.42        pk 	    Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
   1152  1.42        pk 	    Lst_ForEach(gn->children, MakeUnmark, (ClientData)gn);
   1153  1.42        pk 	    Lst_ForEach(gn->children, MakeHandleUse, (ClientData)gn);
   1154  1.42        pk 
   1155   1.1       cgd 	    if ((gn->type & OP_MADE) == 0)
   1156  1.42        pk 		Suff_FindDeps(gn);
   1157  1.56  christos 	    else {
   1158   1.1       cgd 		/* Pretend we made all this node's children */
   1159  1.57  christos 		Lst_ForEach(gn->children, MakeFindChild, (ClientData)gn);
   1160   1.1       cgd 		if (gn->unmade != 0)
   1161   1.1       cgd 			printf("Warning: %s still has %d unmade children\n",
   1162   1.1       cgd 				gn->name, gn->unmade);
   1163  1.10  christos 	    }
   1164  1.56  christos 
   1165  1.11  christos 	    if (gn->unmade != 0) {
   1166  1.11  christos 		Lst_ForEach(gn->children, MakeAddChild, (ClientData)examine);
   1167  1.11  christos 	    } else {
   1168  1.11  christos 		(void)Lst_EnQueue(ntargs, (ClientData)gn);
   1169  1.11  christos 	    }
   1170  1.11  christos 	}
   1171  1.11  christos     }
   1172  1.11  christos 
   1173  1.11  christos     Lst_Destroy(examine, NOFREE);
   1174  1.11  christos     return ntargs;
   1175  1.11  christos }
   1176  1.11  christos 
   1177  1.11  christos /*-
   1178  1.11  christos  *-----------------------------------------------------------------------
   1179  1.11  christos  * Make_Run --
   1180  1.50       wiz  *	Initialize the nodes to remake and the list of nodes which are
   1181  1.50       wiz  *	ready to be made by doing a breadth-first traversal of the graph
   1182  1.50       wiz  *	starting from the nodes in the given list. Once this traversal
   1183  1.11  christos  *	is finished, all the 'leaves' of the graph are in the toBeMade
   1184  1.11  christos  *	queue.
   1185  1.11  christos  *	Using this queue and the Job module, work back up the graph,
   1186  1.11  christos  *	calling on MakeStartJobs to keep the job table as full as
   1187  1.11  christos  *	possible.
   1188  1.11  christos  *
   1189  1.11  christos  * Input:
   1190  1.11  christos  *	targs		the initial list of targets
   1191  1.11  christos  *
   1192  1.11  christos  * Results:
   1193  1.50       wiz  *	TRUE if work was done. FALSE otherwise.
   1194  1.11  christos  *
   1195  1.11  christos  * Side Effects:
   1196  1.11  christos  *	The make field of all nodes involved in the creation of the given
   1197  1.56  christos  *	targets is set to 1. The toBeMade list is set to contain all the
   1198  1.60       dsl  *	'leaves' of these subgraphs.
   1199  1.60       dsl  *-----------------------------------------------------------------------
   1200  1.60       dsl  */
   1201  1.60       dsl Boolean
   1202   1.1       cgd Make_Run(Lst targs)
   1203   1.1       cgd {
   1204   1.1       cgd     int	    	    errors; 	/* Number of errors the Job module reports */
   1205   1.1       cgd 
   1206   1.1       cgd     toBeMade = Make_ExpandUse(targs);
   1207   1.1       cgd     if (DEBUG(MAKE)) {
   1208   1.1       cgd 	 printf("#***# toBeMade\n");
   1209   1.1       cgd 	 Lst_ForEach(toBeMade, Targ_PrintNode, 0);
   1210   1.1       cgd     }
   1211   1.1       cgd 
   1212   1.1       cgd     if (queryFlag) {
   1213   1.1       cgd 	/*
   1214   1.1       cgd 	 * We wouldn't do any work unless we could start some jobs in the
   1215   1.1       cgd 	 * next loop... (we won't actually start any, of course, this is just
   1216  1.10  christos 	 * to see if any of the targets was out of date)
   1217   1.1       cgd 	 */
   1218  1.57  christos 	return (MakeStartJobs());
   1219   1.1       cgd     } else {
   1220   1.1       cgd 	/*
   1221   1.1       cgd 	 * Initialization. At the moment, no jobs are running and until some
   1222   1.1       cgd 	 * get started, nothing will happen since the remaining upward
   1223   1.1       cgd 	 * traversal of the graph is performed by the routines in job.c upon
   1224   1.1       cgd 	 * the finishing of a job. So we fill the Job table as much as we can
   1225   1.1       cgd 	 * before going into our loop.
   1226   1.1       cgd 	 */
   1227   1.1       cgd 	(void)MakeStartJobs();
   1228   1.1       cgd     }
   1229   1.1       cgd 
   1230   1.1       cgd     /*
   1231  1.32  sommerfe      * Main Loop: The idea here is that the ending of jobs will take
   1232  1.56  christos      * care of the maintenance of data structures and the waiting for output
   1233  1.56  christos      * will cause us to be idle most of the time while our children run as
   1234   1.1       cgd      * much as possible. Because the job table is kept as full as possible,
   1235   1.1       cgd      * the only time when it will be empty is when all the jobs which need
   1236   1.1       cgd      * running have been run, so that is the end condition of this loop.
   1237  1.20  christos      * Note that the Job module will exit if there were any errors unless the
   1238   1.1       cgd      * keepgoing flag was given.
   1239   1.1       cgd      */
   1240   1.1       cgd     while (!Lst_IsEmpty(toBeMade) || !Job_Empty ()) {
   1241   1.1       cgd 	Job_CatchOutput();
   1242   1.1       cgd 	Job_CatchChildren(!usePipes);
   1243   1.5       jtc 	(void)MakeStartJobs();
   1244   1.5       jtc     }
   1245  1.10  christos 
   1246   1.1       cgd     errors = Job_Finish();
   1247   1.1       cgd 
   1248                     /*
   1249                      * Print the final status of each target. E.g. if it wasn't made
   1250                      * because some inferior reported an error.
   1251                      */
   1252                     errors = ((errors == 0) && (numNodes != 0));
   1253                     Lst_ForEach(targs, MakePrintStatus, (ClientData) &errors);
   1254                 
   1255                     return (TRUE);
   1256                 }
   1257