Home | History | Annotate | Line # | Download | only in make
make.c revision 1.90
      1  1.89  christos /*	$NetBSD: make.c,v 1.90 2014/09/07 20:55:34 joerg 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.89  christos static char rcsid[] = "$NetBSD: make.c,v 1.90 2014/09/07 20:55:34 joerg 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.89  christos __RCSID("$NetBSD: make.c,v 1.90 2014/09/07 20:55:34 joerg 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.83  christos  *	    	  	    	of the cmgn 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.83  christos  *	Make_TimeStamp	    	Function to set the parent's cmgn 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.68       dsl  *	Make_ExpandUse	    	Expand .USE nodes
    116   1.1       cgd  */
    117   1.1       cgd 
    118   1.1       cgd #include    "make.h"
    119   1.4       cgd #include    "hash.h"
    120   1.4       cgd #include    "dir.h"
    121   1.4       cgd #include    "job.h"
    122   1.1       cgd 
    123  1.70       dsl static unsigned int checked = 1;/* Sequence # to detect recursion */
    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 
    130  1.78       dsl static int MakeAddChild(void *, void *);
    131  1.78       dsl static int MakeFindChild(void *, void *);
    132  1.78       dsl static int MakeUnmark(void *, void *);
    133  1.78       dsl static int MakeAddAllSrc(void *, void *);
    134  1.78       dsl static int MakeTimeStamp(void *, void *);
    135  1.78       dsl static int MakeHandleUse(void *, void *);
    136  1.50       wiz static Boolean MakeStartJobs(void);
    137  1.78       dsl static int MakePrintStatus(void *, void *);
    138  1.78       dsl static int MakeCheckOrder(void *, void *);
    139  1.78       dsl static int MakeBuildChild(void *, void *);
    140  1.78       dsl static int MakeBuildParent(void *, void *);
    141  1.68       dsl 
    142  1.87     joerg MAKE_ATTR_DEAD static void
    143  1.68       dsl make_abort(GNode *gn, int line)
    144  1.68       dsl {
    145  1.74       dsl     static int two = 2;
    146  1.74       dsl 
    147  1.68       dsl     fprintf(debug_file, "make_abort from line %d\n", line);
    148  1.74       dsl     Targ_PrintNode(gn, &two);
    149  1.74       dsl     Lst_ForEach(toBeMade, Targ_PrintNode, &two);
    150  1.68       dsl     Targ_PrintGraph(3);
    151  1.68       dsl     abort();
    152  1.68       dsl }
    153  1.68       dsl 
    154   1.1       cgd /*-
    155   1.1       cgd  *-----------------------------------------------------------------------
    156   1.1       cgd  * Make_TimeStamp --
    157  1.83  christos  *	Set the cmgn field of a parent node based on the mtime stamp in its
    158  1.10  christos  *	child. Called from MakeOODate via Lst_ForEach.
    159   1.1       cgd  *
    160  1.50       wiz  * Input:
    161  1.50       wiz  *	pgn		the current parent
    162  1.50       wiz  *	cgn		the child we've just examined
    163  1.50       wiz  *
    164   1.1       cgd  * Results:
    165  1.10  christos  *	Always returns 0.
    166   1.1       cgd  *
    167   1.1       cgd  * Side Effects:
    168  1.83  christos  *	The cmgn of the parent node will be changed if the mtime
    169   1.1       cgd  *	field of the child is greater than it.
    170   1.1       cgd  *-----------------------------------------------------------------------
    171   1.1       cgd  */
    172   1.1       cgd int
    173  1.50       wiz Make_TimeStamp(GNode *pgn, GNode *cgn)
    174   1.1       cgd {
    175  1.83  christos     if (pgn->cmgn == NULL || cgn->mtime > pgn->cmgn->mtime) {
    176  1.83  christos 	pgn->cmgn = cgn;
    177   1.1       cgd     }
    178   1.1       cgd     return (0);
    179   1.1       cgd }
    180   1.5       jtc 
    181  1.50       wiz /*
    182  1.50       wiz  * Input:
    183  1.50       wiz  *	pgn		the current parent
    184  1.50       wiz  *	cgn		the child we've just examined
    185  1.50       wiz  *
    186  1.50       wiz  */
    187   1.5       jtc static int
    188  1.78       dsl MakeTimeStamp(void *pgn, void *cgn)
    189   1.5       jtc {
    190  1.58  christos     return Make_TimeStamp((GNode *)pgn, (GNode *)cgn);
    191   1.5       jtc }
    192   1.1       cgd 
    193   1.1       cgd /*-
    195   1.1       cgd  *-----------------------------------------------------------------------
    196   1.1       cgd  * Make_OODate --
    197   1.1       cgd  *	See if a given node is out of date with respect to its sources.
    198   1.1       cgd  *	Used by Make_Run when deciding which nodes to place on the
    199   1.1       cgd  *	toBeMade queue initially and by Make_Update to screen out USE and
    200   1.1       cgd  *	EXEC nodes. In the latter case, however, any other sort of node
    201   1.1       cgd  *	must be considered out-of-date since at least one of its children
    202   1.1       cgd  *	will have been recreated.
    203  1.50       wiz  *
    204  1.50       wiz  * Input:
    205  1.50       wiz  *	gn		the node to check
    206   1.1       cgd  *
    207  1.10  christos  * Results:
    208   1.1       cgd  *	TRUE if the node is out of date. FALSE otherwise.
    209   1.1       cgd  *
    210  1.83  christos  * Side Effects:
    211   1.1       cgd  *	The mtime field of the node and the cmgn field of its parents
    212   1.1       cgd  *	will/may be changed.
    213   1.1       cgd  *-----------------------------------------------------------------------
    214   1.1       cgd  */
    215  1.50       wiz Boolean
    216   1.1       cgd Make_OODate(GNode *gn)
    217   1.1       cgd {
    218   1.1       cgd     Boolean         oodate;
    219   1.1       cgd 
    220   1.1       cgd     /*
    221   1.1       cgd      * Certain types of targets needn't even be sought as their datedness
    222   1.1       cgd      * doesn't depend on their modification time...
    223  1.39  christos      */
    224  1.86  christos     if ((gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC)) == 0) {
    225   1.1       cgd 	(void)Dir_MTime(gn, 1);
    226   1.1       cgd 	if (DEBUG(MAKE)) {
    227  1.66       dsl 	    if (gn->mtime != 0) {
    228   1.1       cgd 		fprintf(debug_file, "modified %s...", Targ_FmtTime(gn->mtime));
    229  1.66       dsl 	    } else {
    230   1.1       cgd 		fprintf(debug_file, "non-existent...");
    231   1.1       cgd 	    }
    232   1.1       cgd 	}
    233   1.1       cgd     }
    234   1.1       cgd 
    235   1.1       cgd     /*
    236   1.1       cgd      * A target is remade in one of the following circumstances:
    237   1.1       cgd      *	its modification time is smaller than that of its youngest child
    238   1.1       cgd      *	    and it would actually be run (has commands or type OP_NOP)
    239   1.1       cgd      *	it's the object of a force operator
    240   1.1       cgd      *	it has no children, was on the lhs of an operator and doesn't exist
    241   1.1       cgd      *	    already.
    242   1.1       cgd      *
    243   1.1       cgd      * Libraries are only considered out-of-date if the archive module says
    244   1.1       cgd      * they are.
    245  1.37       wiz      *
    246   1.1       cgd      * These weird rules are brought to you by Backward-Compatibility and
    247   1.1       cgd      * the strange people who wrote 'Make'.
    248  1.39  christos      */
    249   1.1       cgd     if (gn->type & (OP_USE|OP_USEBEFORE)) {
    250   1.1       cgd 	/*
    251   1.1       cgd 	 * If the node is a USE node it is *never* out of date
    252   1.1       cgd 	 * no matter *what*.
    253   1.1       cgd 	 */
    254  1.66       dsl 	if (DEBUG(MAKE)) {
    255   1.1       cgd 	    fprintf(debug_file, ".USE node...");
    256   1.1       cgd 	}
    257  1.25       sjg 	oodate = FALSE;
    258  1.25       sjg     } else if ((gn->type & OP_LIB) &&
    259   1.1       cgd 	       ((gn->mtime==0) || Arch_IsLib(gn))) {
    260  1.66       dsl 	if (DEBUG(MAKE)) {
    261   1.1       cgd 	    fprintf(debug_file, "library...");
    262   1.6  christos 	}
    263   1.6  christos 
    264   1.6  christos 	/*
    265  1.26       sjg 	 * always out of date if no children and :: target
    266   1.6  christos 	 * or non-existent.
    267  1.56  christos 	 */
    268  1.83  christos 	oodate = (gn->mtime == 0 || Arch_LibOODate(gn) ||
    269   1.1       cgd 		  (gn->cmgn == NULL && (gn->type & OP_DOUBLEDEP)));
    270   1.1       cgd     } else if (gn->type & OP_JOIN) {
    271   1.1       cgd 	/*
    272   1.1       cgd 	 * A target with the .JOIN attribute is only considered
    273   1.1       cgd 	 * out-of-date if any of its children was out-of-date.
    274   1.1       cgd 	 */
    275  1.66       dsl 	if (DEBUG(MAKE)) {
    276   1.1       cgd 	    fprintf(debug_file, ".JOIN node...");
    277  1.22  christos 	}
    278  1.66       dsl 	if (DEBUG(MAKE)) {
    279  1.22  christos 	    fprintf(debug_file, "source %smade...", gn->flags & CHILDMADE ? "" : "not ");
    280  1.52       dsl 	}
    281   1.8  christos 	oodate = (gn->flags & CHILDMADE) ? TRUE : FALSE;
    282   1.1       cgd     } else if (gn->type & (OP_FORCE|OP_EXEC|OP_PHONY)) {
    283   1.1       cgd 	/*
    284   1.1       cgd 	 * A node which is the object of the force (!) operator or which has
    285   1.1       cgd 	 * the .EXEC attribute is always considered out-of-date.
    286   1.1       cgd 	 */
    287   1.1       cgd 	if (DEBUG(MAKE)) {
    288  1.66       dsl 	    if (gn->type & OP_FORCE) {
    289   1.8  christos 		fprintf(debug_file, "! operator...");
    290  1.66       dsl 	    } else if (gn->type & OP_PHONY) {
    291   1.1       cgd 		fprintf(debug_file, ".PHONY node...");
    292  1.66       dsl 	    } else {
    293   1.1       cgd 		fprintf(debug_file, ".EXEC node...");
    294   1.1       cgd 	    }
    295   1.1       cgd 	}
    296  1.83  christos 	oodate = TRUE;
    297  1.83  christos     } else if ((gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) ||
    298  1.52       dsl 	       (gn->cmgn == NULL &&
    299  1.52       dsl 		((gn->mtime == 0 && !(gn->type & OP_OPTIONAL))
    300   1.1       cgd 		  || gn->type & OP_DOUBLEDEP)))
    301   1.1       cgd     {
    302   1.1       cgd 	/*
    303  1.83  christos 	 * A node whose modification time is less than that of its
    304  1.52       dsl 	 * youngest child or that has no children (cmgn == NULL) and
    305  1.52       dsl 	 * either doesn't exist (mtime == 0) and it isn't optional
    306  1.52       dsl 	 * or was the object of a * :: operator is out-of-date.
    307   1.1       cgd 	 * Why? Because that's the way Make does it.
    308   1.1       cgd 	 */
    309  1.83  christos 	if (DEBUG(MAKE)) {
    310  1.83  christos 	    if (gn->cmgn != NULL && gn->mtime < gn->cmgn->mtime) {
    311  1.83  christos 		fprintf(debug_file, "modified before source %s...",
    312   1.1       cgd 		    gn->cmgn->path);
    313  1.66       dsl 	    } else if (gn->mtime == 0) {
    314   1.1       cgd 		fprintf(debug_file, "non-existent and no sources...");
    315  1.66       dsl 	    } else {
    316   1.1       cgd 		fprintf(debug_file, ":: operator and no sources...");
    317   1.1       cgd 	    }
    318   1.1       cgd 	}
    319   1.1       cgd 	oodate = TRUE;
    320  1.21  christos     } else {
    321  1.22  christos 	/*
    322  1.21  christos 	 * When a non-existing child with no sources
    323  1.21  christos 	 * (such as a typically used FORCE source) has been made and
    324  1.21  christos 	 * the target of the child (usually a directory) has the same
    325  1.21  christos 	 * timestamp as the timestamp just given to the non-existing child
    326  1.21  christos 	 * after it was considered made.
    327   1.1       cgd 	 */
    328  1.22  christos 	if (DEBUG(MAKE)) {
    329  1.66       dsl 	    if (gn->flags & FORCE)
    330   1.1       cgd 		fprintf(debug_file, "non existing child...");
    331  1.52       dsl 	}
    332   1.1       cgd 	oodate = (gn->flags & FORCE) ? TRUE : FALSE;
    333   1.1       cgd     }
    334  1.82       sjg 
    335  1.82       sjg #ifdef USE_META
    336  1.82       sjg     if (useMeta) {
    337  1.82       sjg 	oodate = meta_oodate(gn, oodate);
    338  1.82       sjg     }
    339  1.82       sjg #endif
    340   1.1       cgd 
    341   1.1       cgd     /*
    342   1.1       cgd      * If the target isn't out-of-date, the parents need to know its
    343   1.1       cgd      * modification time. Note that targets that appear to be out-of-date
    344   1.1       cgd      * but aren't, because they have no commands and aren't of type OP_NOP,
    345   1.1       cgd      * have their mtime stay below their children's mtime to keep parents from
    346   1.1       cgd      * thinking they're out-of-date.
    347   1.1       cgd      */
    348  1.67       dsl     if (!oodate) {
    349   1.1       cgd 	Lst_ForEach(gn->parents, MakeTimeStamp, gn);
    350   1.1       cgd     }
    351   1.1       cgd 
    352   1.1       cgd     return (oodate);
    353   1.1       cgd }
    354   1.1       cgd 
    355   1.1       cgd /*-
    357   1.1       cgd  *-----------------------------------------------------------------------
    358   1.1       cgd  * MakeAddChild  --
    359   1.1       cgd  *	Function used by Make_Run to add a child to the list l.
    360  1.50       wiz  *	It will only add the child if its make field is FALSE.
    361  1.50       wiz  *
    362  1.50       wiz  * Input:
    363  1.50       wiz  *	gnp		the node to add
    364   1.1       cgd  *	lp		the list to which to add it
    365   1.1       cgd  *
    366   1.1       cgd  * Results:
    367   1.1       cgd  *	Always returns 0
    368   1.1       cgd  *
    369   1.1       cgd  * Side Effects:
    370   1.1       cgd  *	The given list is extended
    371   1.1       cgd  *-----------------------------------------------------------------------
    372  1.78       dsl  */
    373   1.1       cgd static int
    374  1.58  christos MakeAddChild(void *gnp, void *lp)
    375   1.5       jtc {
    376  1.14  christos     GNode          *gn = (GNode *)gnp;
    377  1.39  christos     Lst            l = (Lst) lp;
    378  1.68       dsl 
    379  1.68       dsl     if ((gn->flags & REMAKE) == 0 && !(gn->type & (OP_USE|OP_USEBEFORE))) {
    380  1.68       dsl 	if (DEBUG(MAKE))
    381  1.67       dsl 	    fprintf(debug_file, "MakeAddChild: need to examine %s%s\n",
    382   1.1       cgd 		gn->name, gn->cohort_num);
    383   1.1       cgd 	(void)Lst_EnQueue(l, gn);
    384   1.1       cgd     }
    385  1.13  christos     return (0);
    386  1.13  christos }
    387  1.13  christos 
    388  1.13  christos /*-
    390  1.13  christos  *-----------------------------------------------------------------------
    391  1.13  christos  * MakeFindChild  --
    392  1.50       wiz  *	Function used by Make_Run to find the pathname of a child
    393  1.50       wiz  *	that was already made.
    394  1.50       wiz  *
    395  1.13  christos  * Input:
    396  1.13  christos  *	gnp		the node to find
    397  1.13  christos  *
    398  1.13  christos  * Results:
    399  1.83  christos  *	Always returns 0
    400  1.42        pk  *
    401  1.13  christos  * Side Effects:
    402  1.13  christos  *	The path and mtime of the node and the cmgn of the parent are
    403  1.13  christos  *	updated; the unmade children count of the parent is decremented.
    404  1.78       dsl  *-----------------------------------------------------------------------
    405  1.13  christos  */
    406  1.58  christos static int
    407  1.58  christos MakeFindChild(void *gnp, void *pgnp)
    408  1.14  christos {
    409  1.85  christos     GNode          *gn = (GNode *)gnp;
    410  1.22  christos     GNode          *pgn = (GNode *)pgnp;
    411  1.42        pk 
    412  1.14  christos     (void)Dir_MTime(gn, 0);
    413  1.13  christos     Make_TimeStamp(pgn, gn);
    414  1.13  christos     pgn->unmade--;
    415  1.13  christos 
    416   1.1       cgd     return (0);
    417   1.1       cgd }
    418   1.1       cgd 
    419   1.1       cgd /*-
    421  1.44        pk  *-----------------------------------------------------------------------
    422  1.44        pk  * Make_HandleUse --
    423   1.1       cgd  *	Function called by Make_Run and SuffApplyTransform on the downward
    424   1.1       cgd  *	pass to handle .USE and transformation nodes. It implements the
    425   1.1       cgd  *	.USE and transformation functionality by copying the node's commands,
    426   1.1       cgd  *	type flags and children to the parent node.
    427   1.1       cgd  *
    428  1.50       wiz  *	A .USE node is much like an explicit transformation rule, except
    429  1.50       wiz  *	its commands are always added to the target node, even if the
    430  1.50       wiz  *	target already has commands.
    431  1.50       wiz  *
    432   1.1       cgd  * Input:
    433  1.44        pk  *	cgn		The .USE node
    434   1.1       cgd  *	pgn		The target of the .USE node
    435   1.1       cgd  *
    436   1.1       cgd  * Results:
    437   1.1       cgd  *	none
    438   1.1       cgd  *
    439   1.1       cgd  * Side Effects:
    440   1.1       cgd  *	Children and commands may be added to the parent and the parent's
    441  1.43        pk  *	type may be changed.
    442  1.50       wiz  *
    443   1.1       cgd  *-----------------------------------------------------------------------
    444  1.44        pk  */
    445  1.44        pk void
    446  1.44        pk Make_HandleUse(GNode *cgn, GNode *pgn)
    447  1.44        pk {
    448  1.66       dsl     LstNode	ln; 	/* An element in the children list */
    449  1.44        pk 
    450  1.44        pk #ifdef DEBUG_SRC
    451  1.44        pk     if ((cgn->type & (OP_USE|OP_USEBEFORE|OP_TRANSFORM)) == 0) {
    452   1.1       cgd 	fprintf(debug_file, "Make_HandleUse: called for plain node %s\n", cgn->name);
    453  1.44        pk 	return;
    454  1.39  christos     }
    455  1.39  christos #endif
    456  1.39  christos 
    457  1.39  christos     if ((cgn->type & (OP_USE|OP_USEBEFORE)) || Lst_IsEmpty(pgn->commands)) {
    458  1.39  christos 	    if (cgn->type & OP_USEBEFORE) {
    459  1.39  christos 		/*
    460  1.77       dsl 		 * .USEBEFORE --
    461  1.57  christos 		 *	prepend the child's commands to the parent.
    462  1.77       dsl 		 */
    463  1.39  christos 		Lst cmds = pgn->commands;
    464  1.39  christos 		pgn->commands = Lst_Duplicate(cgn->commands, NULL);
    465  1.39  christos 		(void)Lst_Concat(pgn->commands, cmds, LST_CONCNEW);
    466  1.39  christos 		Lst_Destroy(cmds, NULL);
    467  1.39  christos 	    } else {
    468  1.57  christos 		/*
    469  1.39  christos 		 * .USE or target has no commands --
    470  1.44        pk 		 *	append the child's commands to the parent.
    471  1.10  christos 		 */
    472  1.56  christos 		(void)Lst_Concat(pgn->commands, cgn->commands, LST_CONCNEW);
    473  1.77       dsl 	    }
    474  1.90     joerg     }
    475  1.11  christos 
    476  1.44        pk     if (Lst_Open(cgn->children) == SUCCESS) {
    477  1.90     joerg 	while ((ln = Lst_Next(cgn->children)) != NULL) {
    478  1.90     joerg 	    GNode *tgn, *gn = (GNode *)Lst_Datum(ln);
    479  1.90     joerg 
    480  1.90     joerg 	    /*
    481  1.44        pk 	     * Expand variables in the .USE node's name
    482  1.90     joerg 	     * and save the unexpanded form.
    483  1.90     joerg 	     * We don't need to do this for commands.
    484  1.90     joerg 	     * They get expanded properly when we execute.
    485  1.90     joerg 	     */
    486  1.90     joerg 	    if (gn->uname == NULL) {
    487  1.90     joerg 		gn->uname = gn->name;
    488  1.90     joerg 	    } else {
    489  1.90     joerg 		if (gn->name)
    490  1.90     joerg 		    free(gn->name);
    491  1.90     joerg 	    }
    492  1.90     joerg 	    gn->name = Var_Subst(NULL, gn->uname, pgn, FALSE);
    493  1.90     joerg 	    if (gn->name && gn->uname && strcmp(gn->name, gn->uname) != 0) {
    494  1.44        pk 		/* See if we have a target for this node. */
    495   1.1       cgd 		tgn = Targ_FindNode(gn->name, TARG_NOCREATE);
    496  1.57  christos 		if (tgn != NULL)
    497  1.57  christos 		    gn = tgn;
    498  1.44        pk 	    }
    499   1.1       cgd 
    500  1.56  christos 	    (void)Lst_AtEnd(pgn->children, gn);
    501  1.44        pk 	    (void)Lst_AtEnd(gn->parents, pgn);
    502  1.10  christos 	    pgn->unmade += 1;
    503  1.90     joerg 	}
    504   1.1       cgd 	Lst_Close(cgn->children);
    505  1.31   mycroft     }
    506  1.44        pk 
    507  1.44        pk     pgn->type |= cgn->type & ~(OP_OPMASK|OP_USE|OP_USEBEFORE|OP_TRANSFORM);
    508  1.44        pk }
    509  1.44        pk 
    510  1.44        pk /*-
    511  1.44        pk  *-----------------------------------------------------------------------
    512  1.44        pk  * MakeHandleUse --
    513  1.44        pk  *	Callback function for Lst_ForEach, used by Make_Run on the downward
    514  1.44        pk  *	pass to handle .USE nodes. Should be called before the children
    515  1.50       wiz  *	are enqueued to be looked at by MakeAddChild.
    516  1.50       wiz  *	This function calls Make_HandleUse to copy the .USE node's commands,
    517  1.50       wiz  *	type flags and children to the parent node.
    518  1.50       wiz  *
    519  1.44        pk  * Input:
    520  1.44        pk  *	cgnp		the child we've just examined
    521  1.44        pk  *	pgnp		the current parent
    522  1.44        pk  *
    523  1.44        pk  * Results:
    524  1.44        pk  *	returns 0.
    525  1.44        pk  *
    526  1.44        pk  * Side Effects:
    527   1.5       jtc  *	After expansion, .USE child nodes are removed from the parent
    528  1.78       dsl  *
    529   1.5       jtc  *-----------------------------------------------------------------------
    530  1.58  christos  */
    531  1.58  christos static int
    532  1.43        pk MakeHandleUse(void *cgnp, void *pgnp)
    533  1.44        pk {
    534  1.44        pk     GNode	*cgn = (GNode *)cgnp;
    535  1.44        pk     GNode	*pgn = (GNode *)pgnp;
    536  1.44        pk     LstNode	ln; 	/* An element in the children list */
    537  1.31   mycroft     int		unmarked;
    538  1.44        pk 
    539  1.44        pk     unmarked = ((cgn->type & OP_MARK) == 0);
    540  1.44        pk     cgn->type |= OP_MARK;
    541  1.44        pk 
    542  1.43        pk     if ((cgn->type & (OP_USE|OP_USEBEFORE)) == 0)
    543  1.31   mycroft 	return (0);
    544  1.43        pk 
    545  1.43        pk     if (unmarked)
    546  1.43        pk 	Make_HandleUse(cgn, pgn);
    547  1.43        pk 
    548  1.43        pk     /*
    549  1.43        pk      * This child node is now "made", so we decrement the count of
    550  1.43        pk      * unmade children in the parent... We also remove the child
    551  1.77       dsl      * from the parent's list to accurately reflect the number of decent
    552  1.43        pk      * children the parent has. This is used by Make_Run to decide
    553  1.43        pk      * whether to queue the parent or examine its children...
    554  1.43        pk      */
    555  1.43        pk     if ((ln = Lst_Member(pgn->children, cgn)) != NULL) {
    556   1.5       jtc 	Lst_Remove(pgn->children, ln);
    557  1.22  christos 	pgn->unmade--;
    558  1.22  christos     }
    559  1.22  christos     return (0);
    560  1.22  christos }
    561  1.22  christos 
    562  1.22  christos 
    563  1.22  christos /*-
    564  1.22  christos  *-----------------------------------------------------------------------
    565  1.22  christos  * Make_Recheck --
    566  1.22  christos  *	Check the modification time of a gnode, and update it as described
    567  1.22  christos  *	in the comments below.
    568  1.22  christos  *
    569  1.22  christos  * Results:
    570  1.22  christos  *	returns 0 if the gnode does not exist, or it's filesystem
    571  1.22  christos  *	time if it does.
    572  1.22  christos  *
    573  1.22  christos  * Side Effects:
    574  1.22  christos  *	the gnode's modification time and path name are affected.
    575  1.50       wiz  *
    576  1.22  christos  *-----------------------------------------------------------------------
    577  1.85  christos  */
    578  1.22  christos time_t
    579  1.22  christos Make_Recheck(GNode *gn)
    580  1.22  christos {
    581  1.22  christos     time_t mtime = Dir_MTime(gn, 1);
    582  1.22  christos 
    583  1.22  christos #ifndef RECHECK
    584  1.22  christos     /*
    585  1.22  christos      * We can't re-stat the thing, but we can at least take care of rules
    586  1.22  christos      * where a target depends on a source that actually creates the
    587  1.22  christos      * target, but only if it has changed, e.g.
    588  1.22  christos      *
    589  1.22  christos      * parse.h : parse.o
    590  1.22  christos      *
    591  1.22  christos      * parse.o : parse.y
    592  1.22  christos      *  	yacc -d parse.y
    593  1.22  christos      *  	cc -c y.tab.c
    594  1.22  christos      *  	mv y.tab.o parse.o
    595  1.22  christos      *  	cmp -s y.tab.h parse.h || mv y.tab.h parse.h
    596  1.22  christos      *
    597  1.22  christos      * In this case, if the definitions produced by yacc haven't changed
    598  1.22  christos      * from before, parse.h won't have been updated and gn->mtime will
    599  1.22  christos      * reflect the current modification time for parse.h. This is
    600  1.22  christos      * something of a kludge, I admit, but it's a useful one..
    601  1.22  christos      * XXX: People like to use a rule like
    602  1.22  christos      *
    603  1.22  christos      * FRC:
    604  1.22  christos      *
    605  1.22  christos      * To force things that depend on FRC to be made, so we have to
    606  1.22  christos      * check for gn->children being empty as well...
    607  1.22  christos      */
    608  1.22  christos     if (!Lst_IsEmpty(gn->commands) || Lst_IsEmpty(gn->children)) {
    609  1.22  christos 	gn->mtime = now;
    610  1.22  christos     }
    611  1.22  christos #else
    612  1.22  christos     /*
    613  1.22  christos      * This is what Make does and it's actually a good thing, as it
    614  1.22  christos      * allows rules like
    615  1.22  christos      *
    616  1.22  christos      *	cmp -s y.tab.h parse.h || cp y.tab.h parse.h
    617  1.22  christos      *
    618  1.22  christos      * to function as intended. Unfortunately, thanks to the stateless
    619  1.22  christos      * nature of NFS (by which I mean the loose coupling of two clients
    620  1.22  christos      * using the same file from a common server), there are times
    621  1.22  christos      * when the modification time of a file created on a remote
    622  1.22  christos      * machine will not be modified before the local stat() implied by
    623  1.22  christos      * the Dir_MTime occurs, thus leading us to believe that the file
    624  1.22  christos      * is unchanged, wreaking havoc with files that depend on this one.
    625  1.22  christos      *
    626  1.22  christos      * I have decided it is better to make too much than to make too
    627  1.22  christos      * little, so this stuff is commented out unless you're sure it's ok.
    628  1.22  christos      * -- ardeb 1/12/88
    629  1.22  christos      */
    630  1.22  christos     /*
    631  1.73       dsl      * Christos, 4/9/92: If we are  saving commands pretend that
    632  1.73       dsl      * the target is made now. Otherwise archives with ... rules
    633  1.22  christos      * don't work!
    634  1.73       dsl      */
    635  1.42        pk     if (NoExecute(gn) || (gn->type & OP_SAVE_CMDS) ||
    636  1.22  christos 	    (mtime == 0 && !(gn->type & OP_WAIT))) {
    637  1.22  christos 	if (DEBUG(MAKE)) {
    638  1.22  christos 	    fprintf(debug_file, " recheck(%s): update time from %s to now\n",
    639  1.22  christos 		   gn->name, Targ_FmtTime(gn->mtime));
    640  1.22  christos 	}
    641  1.66       dsl 	gn->mtime = now;
    642  1.42        pk     }
    643  1.22  christos     else {
    644  1.22  christos 	if (DEBUG(MAKE)) {
    645  1.22  christos 	    fprintf(debug_file, " recheck(%s): current update time: %s\n",
    646  1.22  christos 		   gn->name, Targ_FmtTime(gn->mtime));
    647  1.22  christos 	}
    648  1.22  christos     }
    649   1.1       cgd #endif
    650   1.1       cgd     return mtime;
    651   1.1       cgd }
    652   1.1       cgd 
    653   1.1       cgd /*-
    654  1.10  christos  *-----------------------------------------------------------------------
    655   1.1       cgd  * Make_Update  --
    656  1.50       wiz  *	Perform update on the parents of a node. Used by JobFinish once
    657  1.50       wiz  *	a node has been dealt with and by MakeStartJobs if it finds an
    658  1.50       wiz  *	up-to-date node.
    659   1.1       cgd  *
    660   1.1       cgd  * Input:
    661   1.1       cgd  *	cgn		the child node
    662   1.1       cgd  *
    663   1.1       cgd  * Results:
    664   1.1       cgd  *	Always returns 0
    665   1.1       cgd  *
    666  1.22  christos  * Side Effects:
    667  1.83  christos  *	The unmade field of pgn is decremented and pgn may be placed on
    668  1.22  christos  *	the toBeMade queue if this field becomes 0.
    669  1.22  christos  *
    670  1.22  christos  * 	If the child was made, the parent's flag CHILDMADE field will be
    671   1.1       cgd  *	set true.
    672  1.83  christos  *
    673   1.1       cgd  *	If the child is not up-to-date and still does not exist,
    674   1.1       cgd  *	set the FORCE flag on the parents.
    675   1.1       cgd  *
    676   1.1       cgd  *	If the child wasn't made, the cmgn field of the parent will be
    677   1.1       cgd  *	altered if the child's mtime is big enough.
    678   1.1       cgd  *
    679   1.1       cgd  *	Finally, if the child is the implied source for the parent, the
    680   1.1       cgd  *	parent's IMPSRC variable is set appropriately.
    681  1.50       wiz  *
    682   1.1       cgd  *-----------------------------------------------------------------------
    683  1.44        pk  */
    684  1.44        pk void
    685  1.44        pk Make_Update(GNode *cgn)
    686  1.44        pk {
    687  1.44        pk     GNode 	*pgn;	/* the parent node */
    688  1.47        pk     char  	*cname;	/* the child's name */
    689  1.47        pk     LstNode	ln; 	/* Element in parents and iParents lists */
    690   1.1       cgd     time_t	mtime = -1;
    691  1.70       dsl     char	*p1;
    692  1.70       dsl     Lst		parents;
    693  1.70       dsl     GNode	*centurion;
    694  1.56  christos 
    695   1.5       jtc     /* It is save to re-examine any nodes again */
    696   1.5       jtc     checked++;
    697   1.1       cgd 
    698  1.68       dsl     cname = Var_Value(TARGET, cgn, &p1);
    699  1.68       dsl     if (p1)
    700  1.68       dsl 	free(p1);
    701   1.1       cgd 
    702   1.1       cgd     if (DEBUG(MAKE))
    703   1.1       cgd 	fprintf(debug_file, "Make_Update: %s%s\n", cgn->name, cgn->cohort_num);
    704   1.1       cgd 
    705   1.1       cgd     /*
    706   1.1       cgd      * If the child was actually made, see what its modification time is
    707  1.22  christos      * now -- some rules won't actually update the file. If the file still
    708   1.1       cgd      * doesn't exist, make its mtime now.
    709  1.10  christos      */
    710  1.47        pk     if (cgn->made != UPTODATE) {
    711  1.47        pk 	mtime = Make_Recheck(cgn);
    712  1.47        pk     }
    713  1.47        pk 
    714  1.47        pk     /*
    715  1.47        pk      * If this is a `::' node, we must consult its first instance
    716  1.68       dsl      * which is where all parents are linked.
    717  1.47        pk      */
    718  1.47        pk     if ((centurion = cgn->centurion) != NULL) {
    719  1.56  christos 	if (!Lst_IsEmpty(cgn->parents))
    720  1.47        pk 		Punt("%s%s: cohort has parents", cgn->name, cgn->cohort_num);
    721  1.47        pk 	centurion->unmade_cohorts -= 1;
    722  1.47        pk 	if (centurion->unmade_cohorts < 0)
    723  1.68       dsl 	    Error("Graph cycles through centurion %s", centurion->name);
    724  1.68       dsl     } else {
    725  1.68       dsl 	centurion = cgn;
    726  1.68       dsl     }
    727  1.68       dsl     parents = centurion->parents;
    728  1.68       dsl 
    729  1.56  christos     /* If this was a .ORDER node, schedule the RHS */
    730  1.77       dsl     Lst_ForEach(centurion->order_succ, MakeBuildParent, Lst_First(toBeMade));
    731  1.57  christos 
    732  1.68       dsl     /* Now mark all the parents as having one less unmade child */
    733  1.68       dsl     if (Lst_Open(parents) == SUCCESS) {
    734  1.68       dsl 	while ((ln = Lst_Next(parents)) != NULL) {
    735  1.68       dsl 	    pgn = (GNode *)Lst_Datum(ln);
    736  1.68       dsl 	    if (DEBUG(MAKE))
    737  1.68       dsl 		fprintf(debug_file, "inspect parent %s%s: flags %x, "
    738  1.68       dsl 			    "type %x, made %d, unmade %d ",
    739  1.68       dsl 			pgn->name, pgn->cohort_num, pgn->flags,
    740  1.68       dsl 			pgn->type, pgn->made, pgn->unmade-1);
    741  1.68       dsl 
    742  1.68       dsl 	    if (!(pgn->flags & REMAKE)) {
    743  1.68       dsl 		/* This parent isn't needed */
    744  1.72       dsl 		if (DEBUG(MAKE))
    745  1.22  christos 		    fprintf(debug_file, "- not needed\n");
    746  1.68       dsl 		continue;
    747  1.42        pk 	    }
    748  1.68       dsl 	    if (mtime == 0 && !(cgn->type & OP_WAIT))
    749  1.68       dsl 		pgn->flags |= FORCE;
    750  1.68       dsl 
    751  1.68       dsl 	    /*
    752  1.68       dsl 	     * If the parent has the .MADE attribute, its timestamp got
    753  1.68       dsl 	     * updated to that of its newest child, and its unmake
    754  1.42        pk 	     * child count got set to zero in Make_ExpandUse().
    755  1.68       dsl 	     * However other things might cause us to build one of its
    756  1.68       dsl 	     * children - and so we mustn't do any processing here when
    757  1.68       dsl 	     * the child build finishes.
    758  1.47        pk 	     */
    759  1.68       dsl 	    if (pgn->type & OP_MADE) {
    760  1.47        pk 		if (DEBUG(MAKE))
    761  1.47        pk 		    fprintf(debug_file, "- .MADE\n");
    762  1.47        pk 		continue;
    763  1.47        pk 	    }
    764  1.57  christos 
    765  1.47        pk 	    if ( ! (cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE))) {
    766   1.1       cgd 		if (cgn->made == MADE)
    767  1.47        pk 		    pgn->flags |= CHILDMADE;
    768  1.47        pk 		(void)Make_TimeStamp(pgn, cgn);
    769  1.47        pk 	    }
    770  1.47        pk 
    771  1.68       dsl 	    /*
    772  1.68       dsl 	     * A parent must wait for the completion of all instances
    773  1.68       dsl 	     * of a `::' dependency.
    774  1.68       dsl 	     */
    775  1.68       dsl 	    if (centurion->unmade_cohorts != 0 || centurion->made < MADE) {
    776  1.47        pk 		if (DEBUG(MAKE))
    777  1.68       dsl 		    fprintf(debug_file,
    778  1.47        pk 			    "- centurion made %d, %d unmade cohorts\n",
    779  1.47        pk 			    centurion->made, centurion->unmade_cohorts);
    780  1.47        pk 		continue;
    781  1.68       dsl 	    }
    782  1.68       dsl 
    783  1.68       dsl 	    /* One more child of this parent is now made */
    784  1.68       dsl 	    pgn->unmade -= 1;
    785  1.68       dsl 	    if (pgn->unmade < 0) {
    786  1.68       dsl 		if (DEBUG(MAKE)) {
    787  1.68       dsl 		    fprintf(debug_file, "Graph cycles through %s%s\n",
    788  1.68       dsl 			pgn->name, pgn->cohort_num);
    789  1.68       dsl 		    Targ_PrintGraph(2);
    790  1.68       dsl 		}
    791  1.68       dsl 		Error("Graph cycles through %s%s", pgn->name, pgn->cohort_num);
    792  1.68       dsl 	    }
    793  1.68       dsl 
    794  1.68       dsl 	    /* We must always rescan the parents of .WAIT and .ORDER nodes. */
    795  1.68       dsl 	    if (pgn->unmade != 0 && !(centurion->type & OP_WAIT)
    796  1.68       dsl 		    && !(centurion->flags & DONE_ORDER)) {
    797  1.68       dsl 		if (DEBUG(MAKE))
    798  1.47        pk 		    fprintf(debug_file, "- unmade children\n");
    799  1.68       dsl 		continue;
    800  1.68       dsl 	    }
    801  1.68       dsl 	    if (pgn->made != DEFERRED) {
    802  1.47        pk 		/*
    803  1.68       dsl 		 * Either this parent is on a different branch of the tree,
    804  1.68       dsl 		 * or it on the RHS of a .WAIT directive
    805  1.68       dsl 		 * or it is already on the toBeMade list.
    806  1.68       dsl 		 */
    807  1.68       dsl 		if (DEBUG(MAKE))
    808  1.68       dsl 		    fprintf(debug_file, "- not deferred\n");
    809  1.68       dsl 		continue;
    810  1.68       dsl 	    }
    811  1.68       dsl 	    if (pgn->order_pred
    812  1.68       dsl 		    && Lst_ForEach(pgn->order_pred, MakeCheckOrder, 0)) {
    813  1.74       dsl 		/* A .ORDER rule stops us building this */
    814  1.68       dsl 		continue;
    815  1.68       dsl 	    }
    816  1.68       dsl 	    if (DEBUG(MAKE)) {
    817  1.74       dsl 		static int two = 2;
    818   1.1       cgd 		fprintf(debug_file, "- %s%s made, schedule %s%s (made %d)\n",
    819  1.68       dsl 			cgn->name, cgn->cohort_num,
    820  1.68       dsl 			pgn->name, pgn->cohort_num, pgn->made);
    821  1.68       dsl 		Targ_PrintNode(pgn, &two);
    822   1.1       cgd 	    }
    823  1.56  christos 	    /* Ok, we can schedule the parent again */
    824   1.1       cgd 	    pgn->made = REQUESTED;
    825  1.10  christos 	    (void)Lst_EnQueue(toBeMade, pgn);
    826   1.1       cgd 	}
    827   1.1       cgd 	Lst_Close(parents);
    828   1.1       cgd     }
    829   1.1       cgd 
    830  1.56  christos     /*
    831   1.5       jtc      * Set the .PREFIX and .IMPSRC variables for all the implied parents
    832   1.1       cgd      * of this node.
    833  1.77       dsl      */
    834  1.57  christos     if (Lst_Open(cgn->iParents) == SUCCESS) {
    835  1.22  christos 	char	*cpref = Var_Value(PREFIX, cgn, &p1);
    836  1.56  christos 
    837  1.35  christos 	while ((ln = Lst_Next(cgn->iParents)) != NULL) {
    838  1.56  christos 	    pgn = (GNode *)Lst_Datum(ln);
    839   1.1       cgd 	    if (pgn->flags & REMAKE) {
    840   1.1       cgd 		Var_Set(IMPSRC, cname, pgn, 0);
    841   1.5       jtc 		if (cpref != NULL)
    842   1.5       jtc 		    Var_Set(PREFIX, cpref, pgn, 0);
    843  1.56  christos 	    }
    844   1.1       cgd 	}
    845   1.1       cgd 	if (p1)
    846   1.1       cgd 	    free(p1);
    847   1.1       cgd 	Lst_Close(cgn->iParents);
    848   1.1       cgd     }
    849   1.1       cgd }
    850   1.1       cgd 
    851   1.1       cgd /*-
    853   1.1       cgd  *-----------------------------------------------------------------------
    854  1.45        pk  * MakeAddAllSrc --
    855  1.45        pk  *	Add a child's name to the ALLSRC and OODATE variables of the given
    856   1.1       cgd  *	node. Called from Make_DoAllVar via Lst_ForEach. A child is added only
    857   1.1       cgd  *	if it has not been given the .EXEC, .USE or .INVISIBLE attributes.
    858   1.1       cgd  *	.EXEC and .USE children are very rarely going to be files, so...
    859   1.1       cgd  *	If the child is a .JOIN node, its ALLSRC is propagated to the parent.
    860   1.1       cgd  *
    861   1.1       cgd  *	A child is added to the OODATE variable if its modification time is
    862   1.1       cgd  *	later than that of its parent, as defined by Make, except if the
    863   1.1       cgd  *	parent is a .JOIN node. In that case, it is only added to the OODATE
    864   1.1       cgd  *	variable if it was actually made (since .JOIN nodes don't have
    865   1.1       cgd  *	modification times, the comparison is rather unfair...)..
    866   1.1       cgd  *
    867   1.1       cgd  * Results:
    868   1.1       cgd  *	Always returns 0
    869   1.1       cgd  *
    870  1.87     joerg  * Side Effects:
    871  1.31   mycroft  *	The ALLSRC variable for the given node is extended.
    872  1.58  christos  *-----------------------------------------------------------------------
    873  1.31   mycroft  */
    874  1.31   mycroft static int
    875  1.31   mycroft MakeUnmark(void *cgnp, void *pgnp MAKE_ATTR_UNUSED)
    876  1.31   mycroft {
    877  1.31   mycroft     GNode	*cgn = (GNode *)cgnp;
    878  1.50       wiz 
    879  1.50       wiz     cgn->type &= ~OP_MARK;
    880  1.50       wiz     return (0);
    881  1.50       wiz }
    882  1.50       wiz 
    883  1.50       wiz /*
    884  1.50       wiz  * Input:
    885  1.31   mycroft  *	cgnp		The child to add
    886  1.78       dsl  *	pgnp		The parent to whose ALLSRC variable it should
    887   1.1       cgd  *			be added
    888  1.58  christos  *
    889  1.58  christos  */
    890  1.31   mycroft static int
    891  1.31   mycroft MakeAddAllSrc(void *cgnp, void *pgnp)
    892  1.31   mycroft {
    893  1.31   mycroft     GNode	*cgn = (GNode *)cgnp;
    894  1.31   mycroft     GNode	*pgn = (GNode *)pgnp;
    895  1.39  christos 
    896  1.45        pk     if (cgn->type & OP_MARK)
    897  1.45        pk 	return (0);
    898   1.1       cgd     cgn->type |= OP_MARK;
    899  1.17  christos 
    900  1.56  christos     if ((cgn->type & (OP_EXEC|OP_USE|OP_USEBEFORE|OP_INVISIBLE)) == 0) {
    901  1.17  christos 	char *child, *allsrc;
    902  1.17  christos 	char *p1 = NULL, *p2 = NULL;
    903  1.45        pk 
    904  1.56  christos 	if (cgn->type & OP_ARCHV)
    905  1.45        pk 	    child = Var_Value(MEMBER, cgn, &p1);
    906  1.45        pk 	else
    907  1.45        pk 	    child = cgn->path ? cgn->path : cgn->name;
    908  1.46        pk 	if (cgn->type & OP_JOIN) {
    909  1.56  christos 	    allsrc = Var_Value(ALLSRC, cgn, &p2);
    910  1.45        pk 	} else {
    911  1.45        pk 	    allsrc = child;
    912   1.1       cgd 	}
    913   1.1       cgd 	if (allsrc != NULL)
    914   1.1       cgd 		Var_Append(ALLSRC, allsrc, pgn);
    915   1.1       cgd 	if (p2)
    916   1.1       cgd 	    free(p2);
    917   1.1       cgd 	if (pgn->type & OP_JOIN) {
    918   1.1       cgd 	    if (cgn->made == MADE) {
    919   1.1       cgd 		Var_Append(OODATE, child, pgn);
    920   1.1       cgd 	    }
    921   1.1       cgd 	} else if ((pgn->mtime < cgn->mtime) ||
    922   1.1       cgd 		   (cgn->mtime >= now && cgn->made == MADE))
    923   1.1       cgd 	{
    924   1.1       cgd 	    /*
    925   1.1       cgd 	     * It goes in the OODATE variable if the parent is younger than the
    926   1.1       cgd 	     * child or if the child has been modified more recently than
    927   1.1       cgd 	     * the start of the make. This is to keep pmake from getting
    928   1.1       cgd 	     * confused if something else updates the parent after the
    929   1.1       cgd 	     * make starts (shouldn't happen, I know, but sometimes it
    930   1.1       cgd 	     * does). In such a case, if we've updated the kid, the parent
    931   1.1       cgd 	     * is likely to have a modification time later than that of
    932   1.1       cgd 	     * the kid and anything that relies on the OODATE variable will
    933   1.1       cgd 	     * be hosed.
    934   1.1       cgd 	     *
    935   1.1       cgd 	     * XXX: This will cause all made children to go in the OODATE
    936   1.1       cgd 	     * variable, even if they're not touched, if RECHECK isn't defined,
    937   1.5       jtc 	     * since cgn->mtime is set to now in Make_Update. According to
    938   1.5       jtc 	     * some people, this is good...
    939   1.1       cgd 	     */
    940   1.1       cgd 	    Var_Append(OODATE, child, pgn);
    941   1.1       cgd 	}
    942   1.1       cgd 	if (p1)
    943   1.1       cgd 	    free(p1);
    944   1.1       cgd     }
    945   1.1       cgd     return (0);
    946   1.1       cgd }
    947   1.1       cgd 
    948   1.1       cgd /*-
    950   1.1       cgd  *-----------------------------------------------------------------------
    951   1.1       cgd  * Make_DoAllVar --
    952   1.1       cgd  *	Set up the ALLSRC and OODATE variables. Sad to say, it must be
    953   1.1       cgd  *	done separately, rather than while traversing the graph. This is
    954   1.1       cgd  *	because Make defined OODATE to contain all sources whose modification
    955   1.1       cgd  *	times were later than that of the target, *not* those sources that
    956   1.1       cgd  *	were out-of-date. Since in both compatibility and native modes,
    957   1.1       cgd  *	the modification time of the parent isn't found until the child
    958   1.1       cgd  *	has been dealt with, we have to wait until now to fill in the
    959   1.1       cgd  *	variable. As for ALLSRC, the ordering is important and not
    960   1.1       cgd  *	guaranteed when in native mode, so it must be set here, too.
    961   1.1       cgd  *
    962   1.1       cgd  * Results:
    963   1.1       cgd  *	None
    964   1.1       cgd  *
    965   1.1       cgd  * Side Effects:
    966  1.50       wiz  *	The ALLSRC and OODATE variables of the given node is filled in.
    967   1.1       cgd  *	If the node is a .JOIN node, its TARGET variable will be set to
    968  1.79       sjg  * 	match its ALLSRC variable.
    969  1.79       sjg  *-----------------------------------------------------------------------
    970  1.79       sjg  */
    971  1.67       dsl void
    972  1.67       dsl Make_DoAllVar(GNode *gn)
    973   1.1       cgd {
    974   1.1       cgd     if (gn->flags & DONE_ALLSRC)
    975  1.56  christos 	return;
    976   1.1       cgd 
    977   1.1       cgd     Lst_ForEach(gn->children, MakeUnmark, gn);
    978  1.56  christos     Lst_ForEach(gn->children, MakeAddAllSrc, gn);
    979   1.1       cgd 
    980   1.1       cgd     if (!Var_Exists (OODATE, gn)) {
    981   1.1       cgd 	Var_Set(OODATE, "", gn, 0);
    982   1.5       jtc     }
    983  1.56  christos     if (!Var_Exists (ALLSRC, gn)) {
    984   1.5       jtc 	Var_Set(ALLSRC, "", gn, 0);
    985   1.5       jtc     }
    986   1.1       cgd 
    987  1.79       sjg     if (gn->type & OP_JOIN) {
    988   1.1       cgd 	char *p1;
    989   1.1       cgd 	Var_Set(TARGET, Var_Value(ALLSRC, gn, &p1), gn, 0);
    990   1.1       cgd 	if (p1)
    991   1.1       cgd 	    free(p1);
    992   1.1       cgd     }
    993   1.1       cgd     gn->flags |= DONE_ALLSRC;
    994   1.1       cgd }
    995   1.1       cgd 
    996   1.1       cgd /*-
    998   1.1       cgd  *-----------------------------------------------------------------------
    999   1.1       cgd  * MakeStartJobs --
   1000   1.1       cgd  *	Start as many jobs as possible.
   1001   1.1       cgd  *
   1002   1.1       cgd  * Results:
   1003   1.1       cgd  *	If the query flag was given to pmake, no job will be started,
   1004   1.1       cgd  *	but as soon as an out-of-date target is found, this function
   1005   1.1       cgd  *	returns TRUE. At all other times, this function returns FALSE.
   1006  1.68       dsl  *
   1007  1.68       dsl  * Side Effects:
   1008  1.87     joerg  *	Nodes are removed from the toBeMade queue and job table slots
   1009  1.68       dsl  *	are filled.
   1010  1.68       dsl  *
   1011  1.68       dsl  *-----------------------------------------------------------------------
   1012  1.68       dsl  */
   1013  1.68       dsl 
   1014  1.68       dsl static int
   1015  1.68       dsl MakeCheckOrder(void *v_bn, void *ignore MAKE_ATTR_UNUSED)
   1016  1.68       dsl {
   1017  1.68       dsl     GNode *bn = v_bn;
   1018  1.68       dsl 
   1019  1.68       dsl     if (bn->made >= MADE || !(bn->flags & REMAKE))
   1020  1.68       dsl 	return 0;
   1021  1.78       dsl     if (DEBUG(MAKE))
   1022  1.68       dsl 	fprintf(debug_file, "MakeCheckOrder: Waiting for .ORDER node %s%s\n",
   1023  1.68       dsl 		bn->name, bn->cohort_num);
   1024  1.68       dsl     return 1;
   1025  1.68       dsl }
   1026  1.68       dsl 
   1027  1.68       dsl static int
   1028  1.68       dsl MakeBuildChild(void *v_cn, void *toBeMade_next)
   1029  1.68       dsl {
   1030  1.68       dsl     GNode *cn = v_cn;
   1031  1.68       dsl 
   1032  1.68       dsl     if (DEBUG(MAKE))
   1033  1.68       dsl 	fprintf(debug_file, "MakeBuildChild: inspect %s%s, made %d, type %x\n",
   1034  1.68       dsl 	    cn->name, cn->cohort_num, cn->made, cn->type);
   1035  1.88       sjg     if (cn->made > DEFERRED)
   1036  1.68       dsl 	return 0;
   1037  1.68       dsl 
   1038  1.68       dsl     /* If this node is on the RHS of a .ORDER, check LHSs. */
   1039  1.68       dsl     if (cn->order_pred && Lst_ForEach(cn->order_pred, MakeCheckOrder, 0)) {
   1040  1.68       dsl 	/* Can't build this (or anything else in this child list) yet */
   1041  1.68       dsl 	cn->made = DEFERRED;
   1042  1.68       dsl 	return 0;			/* but keep looking */
   1043  1.77       dsl     }
   1044  1.68       dsl 
   1045  1.68       dsl     if (DEBUG(MAKE))
   1046  1.68       dsl 	fprintf(debug_file, "MakeBuildChild: schedule %s%s\n",
   1047  1.68       dsl 		cn->name, cn->cohort_num);
   1048  1.68       dsl 
   1049  1.68       dsl     cn->made = REQUESTED;
   1050  1.68       dsl     if (toBeMade_next == NULL)
   1051  1.68       dsl 	Lst_AtEnd(toBeMade, cn);
   1052  1.68       dsl     else
   1053  1.68       dsl 	Lst_InsertBefore(toBeMade, toBeMade_next, cn);
   1054  1.68       dsl 
   1055  1.68       dsl     if (cn->unmade_cohorts != 0)
   1056  1.68       dsl 	Lst_ForEach(cn->cohorts, MakeBuildChild, toBeMade_next);
   1057  1.68       dsl 
   1058  1.88       sjg     /*
   1059  1.68       dsl      * If this node is a .WAIT node with unmade chlidren
   1060  1.78       dsl      * then don't add the next sibling.
   1061  1.68       dsl      */
   1062  1.68       dsl     return cn->type & OP_WAIT && cn->unmade > 0;
   1063  1.68       dsl }
   1064  1.68       dsl 
   1065  1.68       dsl /* When a .ORDER LHS node completes we do this on each RHS */
   1066  1.68       dsl static int
   1067  1.68       dsl MakeBuildParent(void *v_pn, void *toBeMade_next)
   1068  1.68       dsl {
   1069  1.68       dsl     GNode *pn = v_pn;
   1070  1.68       dsl 
   1071  1.68       dsl     if (pn->made != DEFERRED)
   1072  1.68       dsl 	return 0;
   1073  1.68       dsl 
   1074  1.68       dsl     if (MakeBuildChild(pn, toBeMade_next) == 0) {
   1075   1.1       cgd 	/* Mark so that when this node is built we reschedule its parents */
   1076  1.50       wiz 	pn->flags |= DONE_ORDER;
   1077   1.1       cgd     }
   1078  1.44        pk 
   1079  1.59       dsl     return 0;
   1080  1.10  christos }
   1081  1.32  sommerfe 
   1082  1.60       dsl static Boolean
   1083  1.60       dsl MakeStartJobs(void)
   1084  1.60       dsl {
   1085  1.60       dsl     GNode	*gn;
   1086  1.60       dsl     int		have_token = 0;
   1087  1.57  christos 
   1088  1.68       dsl     while (!Lst_IsEmpty (toBeMade)) {
   1089  1.68       dsl 	/* Get token now to avoid cycling job-list when we only have 1 token */
   1090  1.68       dsl 	if (!have_token && !Job_TokenWithdraw())
   1091  1.68       dsl 	    break;
   1092  1.68       dsl 	have_token = 1;
   1093  1.68       dsl 
   1094  1.68       dsl 	gn = (GNode *)Lst_DeQueue(toBeMade);
   1095  1.68       dsl 	if (DEBUG(MAKE))
   1096  1.68       dsl 	    fprintf(debug_file, "Examining %s%s...\n",
   1097   1.1       cgd 		    gn->name, gn->cohort_num);
   1098   1.1       cgd 
   1099  1.70       dsl 	if (gn->made != REQUESTED) {
   1100  1.70       dsl 	    if (DEBUG(MAKE))
   1101  1.70       dsl 		fprintf(debug_file, "state %d\n", gn->made);
   1102  1.71       dsl 
   1103  1.70       dsl 	    make_abort(gn, __LINE__);
   1104  1.71       dsl 	}
   1105  1.70       dsl 
   1106  1.70       dsl 	if (gn->checked == checked) {
   1107  1.70       dsl 	    /* We've already looked at this node since a job finished... */
   1108  1.70       dsl 	    if (DEBUG(MAKE))
   1109  1.68       dsl 		fprintf(debug_file, "already checked %s%s\n",
   1110   1.1       cgd 			gn->name, gn->cohort_num);
   1111  1.68       dsl 	    gn->made = DEFERRED;
   1112  1.68       dsl 	    continue;
   1113   1.1       cgd 	}
   1114  1.68       dsl 	gn->checked = checked;
   1115  1.68       dsl 
   1116  1.68       dsl 	if (gn->unmade != 0) {
   1117  1.68       dsl 	    /*
   1118  1.68       dsl 	     * We can't build this yet, add all unmade children to toBeMade,
   1119  1.68       dsl 	     * just before the current first element.
   1120   1.1       cgd 	     */
   1121  1.10  christos 	    gn->made = DEFERRED;
   1122  1.68       dsl 	    Lst_ForEach(gn->children, MakeBuildChild, Lst_First(toBeMade));
   1123  1.56  christos 	    /* and drop this node on the floor */
   1124   1.1       cgd 	    if (DEBUG(MAKE))
   1125  1.66       dsl 		fprintf(debug_file, "dropped %s%s\n", gn->name, gn->cohort_num);
   1126   1.1       cgd 	    continue;
   1127   1.1       cgd 	}
   1128   1.1       cgd 
   1129   1.1       cgd 	gn->made = BEINGMADE;
   1130  1.56  christos 	if (Make_OODate(gn)) {
   1131  1.56  christos 	    if (DEBUG(MAKE)) {
   1132  1.59       dsl 		fprintf(debug_file, "out-of-date\n");
   1133   1.1       cgd 	    }
   1134   1.1       cgd 	    if (queryFlag) {
   1135  1.66       dsl 		return (TRUE);
   1136   1.1       cgd 	    }
   1137   1.1       cgd 	    Make_DoAllVar(gn);
   1138   1.1       cgd 	    Job_Make(gn);
   1139   1.1       cgd 	    have_token = 0;
   1140   1.1       cgd 	} else {
   1141   1.1       cgd 	    if (DEBUG(MAKE)) {
   1142   1.1       cgd 		fprintf(debug_file, "up-to-date\n");
   1143   1.1       cgd 	    }
   1144   1.1       cgd 	    gn->made = UPTODATE;
   1145  1.56  christos 	    if (gn->type & OP_JOIN) {
   1146   1.1       cgd 		/*
   1147  1.56  christos 		 * Even for an up-to-date .JOIN node, we need it to have its
   1148   1.1       cgd 		 * context variables so references to it get the correct
   1149   1.1       cgd 		 * value for .TARGET when building up the context variables
   1150  1.59       dsl 		 * of its parent(s)...
   1151  1.59       dsl 		 */
   1152  1.59       dsl 		Make_DoAllVar(gn);
   1153  1.59       dsl 	    }
   1154   1.1       cgd 	    Make_Update(gn);
   1155   1.1       cgd 	}
   1156   1.1       cgd     }
   1157   1.1       cgd 
   1158   1.1       cgd     if (have_token)
   1159   1.1       cgd 	Job_TokenReturn();
   1160   1.1       cgd 
   1161   1.1       cgd     return (FALSE);
   1162   1.1       cgd }
   1163   1.1       cgd 
   1164  1.50       wiz /*-
   1166  1.50       wiz  *-----------------------------------------------------------------------
   1167  1.50       wiz  * MakePrintStatus --
   1168  1.50       wiz  *	Print the status of a top-level node, viz. it being up-to-date
   1169  1.50       wiz  *	already or not created due to an error in a lower level.
   1170   1.1       cgd  *	Callback function for Make_Run via Lst_ForEach.
   1171   1.1       cgd  *
   1172   1.1       cgd  * Input:
   1173   1.1       cgd  *	gnp		Node to examine
   1174   1.1       cgd  *	cyclep		True if gn->unmade being non-zero implies a
   1175   1.1       cgd  *			cycle in the graph, not an error in an
   1176   1.1       cgd  *			inferior.
   1177   1.1       cgd  *
   1178   1.1       cgd  * Results:
   1179  1.78       dsl  *	Always returns 0.
   1180  1.68       dsl  *
   1181  1.68       dsl  * Side Effects:
   1182  1.68       dsl  *	A message may be printed.
   1183  1.68       dsl  *
   1184  1.68       dsl  *-----------------------------------------------------------------------
   1185  1.68       dsl  */
   1186  1.68       dsl static int
   1187  1.68       dsl MakePrintStatusOrder(void *ognp, void *gnp)
   1188  1.68       dsl {
   1189  1.68       dsl     GNode *ogn = ognp;
   1190  1.68       dsl     GNode *gn = gnp;
   1191  1.68       dsl 
   1192  1.68       dsl     if (!(ogn->flags & REMAKE) || ogn->made > REQUESTED)
   1193  1.68       dsl 	/* not waiting for this one */
   1194  1.68       dsl 	return 0;
   1195  1.68       dsl 
   1196  1.68       dsl     printf("    `%s%s' has .ORDER dependency against %s%s "
   1197  1.68       dsl 		"(made %d, flags %x, type %x)\n",
   1198  1.68       dsl 	    gn->name, gn->cohort_num,
   1199  1.68       dsl 	    ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
   1200  1.68       dsl     if (DEBUG(MAKE) && debug_file != stdout)
   1201  1.78       dsl 	fprintf(debug_file, "    `%s%s' has .ORDER dependency against %s%s "
   1202   1.1       cgd 		    "(made %d, flags %x, type %x)\n",
   1203  1.58  christos 		gn->name, gn->cohort_num,
   1204  1.68       dsl 		ogn->name, ogn->cohort_num, ogn->made, ogn->flags, ogn->type);
   1205  1.68       dsl     return 0;
   1206  1.68       dsl }
   1207  1.68       dsl 
   1208  1.68       dsl static int
   1209  1.68       dsl MakePrintStatus(void *gnp, void *v_errors)
   1210  1.68       dsl {
   1211  1.68       dsl     GNode   	*gn = (GNode *)gnp;
   1212  1.68       dsl     int 	*errors = v_errors;
   1213  1.68       dsl 
   1214  1.68       dsl     if (gn->flags & DONECYCLE)
   1215  1.68       dsl 	/* We've completely processed this node before, don't do it again. */
   1216  1.68       dsl 	return 0;
   1217  1.68       dsl 
   1218  1.68       dsl     if (gn->unmade == 0) {
   1219  1.68       dsl 	gn->flags |= DONECYCLE;
   1220  1.68       dsl 	switch (gn->made) {
   1221  1.68       dsl 	case UPTODATE:
   1222  1.68       dsl 	    printf("`%s%s' is up to date.\n", gn->name, gn->cohort_num);
   1223  1.68       dsl 	    break;
   1224  1.68       dsl 	case MADE:
   1225  1.68       dsl 	    break;
   1226  1.68       dsl 	case UNMADE:
   1227  1.68       dsl 	case DEFERRED:
   1228  1.68       dsl 	case REQUESTED:
   1229  1.68       dsl 	case BEINGMADE:
   1230  1.68       dsl 	    (*errors)++;
   1231  1.68       dsl 	    printf("`%s%s' was not built (made %d, flags %x, type %x)!\n",
   1232  1.68       dsl 		    gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
   1233  1.68       dsl 	    if (DEBUG(MAKE) && debug_file != stdout)
   1234  1.68       dsl 		fprintf(debug_file,
   1235  1.68       dsl 			"`%s%s' was not built (made %d, flags %x, type %x)!\n",
   1236  1.68       dsl 			gn->name, gn->cohort_num, gn->made, gn->flags, gn->type);
   1237  1.68       dsl 	    /* Most likely problem is actually caused by .ORDER */
   1238  1.68       dsl 	    Lst_ForEach(gn->order_pred, MakePrintStatusOrder, gn);
   1239  1.68       dsl 	    break;
   1240   1.1       cgd 	default:
   1241  1.68       dsl 	    /* Errors - already counted */
   1242  1.68       dsl 	    printf("`%s%s' not remade because of errors.\n",
   1243  1.68       dsl 		    gn->name, gn->cohort_num);
   1244  1.68       dsl 	    if (DEBUG(MAKE) && debug_file != stdout)
   1245  1.68       dsl 		fprintf(debug_file, "`%s%s' not remade because of errors.\n",
   1246  1.68       dsl 			gn->name, gn->cohort_num);
   1247  1.68       dsl 	    break;
   1248  1.68       dsl 	}
   1249  1.70       dsl 	return 0;
   1250  1.68       dsl     }
   1251  1.70       dsl 
   1252  1.70       dsl     if (DEBUG(MAKE))
   1253  1.68       dsl 	fprintf(debug_file, "MakePrintStatus: %s%s has %d unmade children\n",
   1254  1.68       dsl 		gn->name, gn->cohort_num, gn->unmade);
   1255  1.70       dsl     /*
   1256  1.68       dsl      * If printing cycles and came to one that has unmade children,
   1257  1.70       dsl      * print out the cycle by recursing on its children.
   1258   1.1       cgd      */
   1259  1.68       dsl     if (!(gn->flags & CYCLE)) {
   1260  1.70       dsl 	/* Fist time we've seen this node, check all children */
   1261  1.70       dsl 	gn->flags |= CYCLE;
   1262  1.70       dsl 	Lst_ForEach(gn->children, MakePrintStatus, errors);
   1263  1.70       dsl 	/* Mark that this node needn't be processed again */
   1264  1.70       dsl 	gn->flags |= DONECYCLE;
   1265  1.70       dsl 	return 0;
   1266  1.70       dsl     }
   1267  1.70       dsl 
   1268  1.70       dsl     /* Only output the error once per node */
   1269  1.70       dsl     gn->flags |= DONECYCLE;
   1270   1.1       cgd     Error("Graph cycles through `%s%s'", gn->name, gn->cohort_num);
   1271   1.1       cgd     if ((*errors)++ > 100)
   1272  1.11  christos 	/* Abandon the whole error report */
   1273   1.1       cgd 	return 1;
   1274   1.1       cgd 
   1275  1.11  christos     /* Reporting for our children will give the rest of the loop */
   1276  1.11  christos     Lst_ForEach(gn->children, MakePrintStatus, errors);
   1277  1.50       wiz     return 0;
   1278  1.50       wiz }
   1279  1.50       wiz 
   1280  1.50       wiz 
   1282   1.1       cgd /*-
   1283   1.1       cgd  *-----------------------------------------------------------------------
   1284  1.68       dsl  * Make_ExpandUse --
   1285  1.50       wiz  *	Expand .USE nodes and create a new targets list
   1286   1.1       cgd  *
   1287  1.44        pk  * Input:
   1288  1.44        pk  *	targs		the initial list of targets
   1289   1.1       cgd  *
   1290  1.77       dsl  * Side Effects:
   1291  1.10  christos  *-----------------------------------------------------------------------
   1292   1.1       cgd  */
   1293   1.1       cgd void
   1294   1.1       cgd Make_ExpandUse(Lst targs)
   1295   1.1       cgd {
   1296   1.1       cgd     GNode  *gn;		/* a temporary pointer */
   1297   1.1       cgd     Lst    examine; 	/* List of targets to examine */
   1298  1.10  christos 
   1299   1.1       cgd     examine = Lst_Duplicate(targs, NULL);
   1300   1.1       cgd 
   1301  1.57  christos     /*
   1302  1.68       dsl      * Make an initial downward pass over the graph, marking nodes to be made
   1303  1.68       dsl      * as we go down. We call Suff_FindDeps to find where a node is and
   1304  1.68       dsl      * to get some children for it if it has none and also has no commands.
   1305  1.68       dsl      * If the node is a leaf, we stick it on the toBeMade queue to
   1306  1.68       dsl      * be looked at in a minute, otherwise we add its children to our queue
   1307  1.68       dsl      * and go on about our business.
   1308  1.68       dsl      */
   1309  1.68       dsl     while (!Lst_IsEmpty (examine)) {
   1310  1.10  christos 	gn = (GNode *)Lst_DeQueue(examine);
   1311  1.24   mycroft 
   1312  1.68       dsl 	if (gn->flags & REMAKE)
   1313  1.23   mycroft 	    /* We've looked at this one already */
   1314  1.77       dsl 	    continue;
   1315  1.56  christos 	gn->flags |= REMAKE;
   1316  1.23   mycroft 	if (DEBUG(MAKE))
   1317  1.23   mycroft 	    fprintf(debug_file, "Make_ExpandUse: examine %s%s\n",
   1318  1.10  christos 		    gn->name, gn->cohort_num);
   1319  1.68       dsl 
   1320  1.68       dsl 	if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts)) {
   1321  1.68       dsl 	    /* Append all the 'cohorts' to the list of things to examine */
   1322  1.68       dsl 	    Lst new;
   1323  1.68       dsl 	    new = Lst_Duplicate(gn->cohorts, NULL);
   1324  1.68       dsl 	    Lst_Concat(new, examine, LST_CONCLINK);
   1325  1.68       dsl 	    examine = new;
   1326  1.68       dsl 	}
   1327  1.68       dsl 
   1328  1.68       dsl 	/*
   1329  1.68       dsl 	 * Apply any .USE rules before looking for implicit dependencies
   1330  1.68       dsl 	 * to make sure everything has commands that should...
   1331  1.68       dsl 	 * Make sure that the TARGET is set, so that we can make
   1332  1.68       dsl 	 * expansions.
   1333  1.68       dsl 	 */
   1334  1.68       dsl 	if (gn->type & OP_ARCHV) {
   1335  1.68       dsl 	    char *eoa, *eon;
   1336  1.68       dsl 	    eoa = strchr(gn->name, '(');
   1337  1.68       dsl 	    eon = strchr(gn->name, ')');
   1338  1.68       dsl 	    if (eoa == NULL || eon == NULL)
   1339  1.85  christos 		continue;
   1340  1.68       dsl 	    *eoa = '\0';
   1341  1.68       dsl 	    *eon = '\0';
   1342  1.68       dsl 	    Var_Set(MEMBER, eoa + 1, gn, 0);
   1343  1.68       dsl 	    Var_Set(ARCHIVE, gn->name, gn, 0);
   1344  1.68       dsl 	    *eoa = '(';
   1345  1.68       dsl 	    *eon = ')';
   1346  1.68       dsl 	}
   1347  1.68       dsl 
   1348  1.68       dsl 	(void)Dir_MTime(gn, 0);
   1349  1.68       dsl 	Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
   1350  1.68       dsl 	Lst_ForEach(gn->children, MakeUnmark, gn);
   1351  1.68       dsl 	Lst_ForEach(gn->children, MakeHandleUse, gn);
   1352  1.68       dsl 
   1353  1.68       dsl 	if ((gn->type & OP_MADE) == 0)
   1354  1.68       dsl 	    Suff_FindDeps(gn);
   1355  1.68       dsl 	else {
   1356  1.68       dsl 	    /* Pretend we made all this node's children */
   1357  1.68       dsl 	    Lst_ForEach(gn->children, MakeFindChild, gn);
   1358  1.77       dsl 	    if (gn->unmade != 0)
   1359  1.68       dsl 		    printf("Warning: %s%s still has %d unmade children\n",
   1360  1.68       dsl 			    gn->name, gn->cohort_num, gn->unmade);
   1361  1.68       dsl 	}
   1362  1.68       dsl 
   1363  1.68       dsl 	if (gn->unmade != 0)
   1364  1.68       dsl 	    Lst_ForEach(gn->children, MakeAddChild, examine);
   1365  1.68       dsl     }
   1366  1.68       dsl 
   1367  1.68       dsl     Lst_Destroy(examine, NULL);
   1368  1.68       dsl }
   1369  1.68       dsl 
   1370  1.68       dsl /*-
   1371  1.68       dsl  *-----------------------------------------------------------------------
   1372  1.68       dsl  * Make_ProcessWait --
   1373  1.78       dsl  *	Convert .WAIT nodes into dependencies
   1374  1.68       dsl  *
   1375  1.68       dsl  * Input:
   1376  1.68       dsl  *	targs		the initial list of targets
   1377  1.68       dsl  *
   1378  1.68       dsl  *-----------------------------------------------------------------------
   1379  1.68       dsl  */
   1380  1.68       dsl 
   1381  1.68       dsl static int
   1382  1.68       dsl link_parent(void *cnp, void *pnp)
   1383  1.68       dsl {
   1384  1.68       dsl     GNode *cn = cnp;
   1385  1.68       dsl     GNode *pn = pnp;
   1386  1.68       dsl 
   1387  1.68       dsl     Lst_AtEnd(pn->children, cn);
   1388  1.68       dsl     Lst_AtEnd(cn->parents, pn);
   1389  1.68       dsl     pn->unmade++;
   1390  1.68       dsl     return 0;
   1391  1.68       dsl }
   1392  1.68       dsl 
   1393  1.68       dsl static int
   1394  1.68       dsl add_wait_dep(void *v_cn, void *v_wn)
   1395  1.68       dsl {
   1396  1.68       dsl     GNode *cn = v_cn;
   1397  1.68       dsl     GNode *wn = v_wn;
   1398  1.68       dsl 
   1399  1.68       dsl     if (cn == wn)
   1400  1.68       dsl 	return 1;
   1401  1.68       dsl 
   1402  1.68       dsl     if (cn == NULL || wn == NULL) {
   1403  1.68       dsl 	printf("bad wait dep %p %p\n", cn, wn);
   1404  1.68       dsl 	exit(4);
   1405  1.68       dsl     }
   1406  1.68       dsl     if (DEBUG(MAKE))
   1407  1.68       dsl 	 fprintf(debug_file, ".WAIT: add dependency %s%s -> %s\n",
   1408  1.68       dsl 		cn->name, cn->cohort_num, wn->name);
   1409  1.68       dsl 
   1410  1.68       dsl     Lst_AtEnd(wn->children, cn);
   1411  1.68       dsl     wn->unmade++;
   1412  1.68       dsl     Lst_AtEnd(cn->parents, wn);
   1413  1.68       dsl     return 0;
   1414  1.68       dsl }
   1415  1.68       dsl 
   1416  1.68       dsl static void
   1417  1.68       dsl Make_ProcessWait(Lst targs)
   1418  1.68       dsl {
   1419  1.68       dsl     GNode  *pgn;	/* 'parent' node we are examining */
   1420  1.68       dsl     GNode  *cgn;	/* Each child in turn */
   1421  1.68       dsl     LstNode owln;	/* Previous .WAIT node */
   1422  1.68       dsl     Lst    examine; 	/* List of targets to examine */
   1423  1.68       dsl     LstNode ln;
   1424  1.68       dsl 
   1425  1.68       dsl     /*
   1426  1.68       dsl      * We need all the nodes to have a common parent in order for the
   1427  1.68       dsl      * .WAIT and .ORDER scheduling to work.
   1428  1.68       dsl      * Perhaps this should be done earlier...
   1429  1.68       dsl      */
   1430  1.68       dsl 
   1431  1.77       dsl     pgn = Targ_NewGN(".MAIN");
   1432  1.68       dsl     pgn->flags = REMAKE;
   1433  1.68       dsl     pgn->type = OP_PHONY | OP_DEPENDS;
   1434  1.68       dsl     /* Get it displayed in the diag dumps */
   1435  1.68       dsl     Lst_AtFront(Targ_List(), pgn);
   1436  1.68       dsl 
   1437  1.68       dsl     Lst_ForEach(targs, link_parent, pgn);
   1438  1.68       dsl 
   1439  1.68       dsl     /* Start building with the 'dummy' .MAIN' node */
   1440  1.68       dsl     MakeBuildChild(pgn, NULL);
   1441  1.68       dsl 
   1442  1.68       dsl     examine = Lst_Init(FALSE);
   1443  1.68       dsl     Lst_AtEnd(examine, pgn);
   1444  1.68       dsl 
   1445  1.68       dsl     while (!Lst_IsEmpty (examine)) {
   1446  1.68       dsl 	pgn = Lst_DeQueue(examine);
   1447  1.68       dsl 
   1448  1.68       dsl 	/* We only want to process each child-list once */
   1449  1.77       dsl 	if (pgn->flags & DONE_WAIT)
   1450  1.68       dsl 	    continue;
   1451  1.68       dsl 	pgn->flags |= DONE_WAIT;
   1452  1.68       dsl 	if (DEBUG(MAKE))
   1453   1.1       cgd 	    fprintf(debug_file, "Make_ProcessWait: examine %s\n", pgn->name);
   1454  1.68       dsl 
   1455  1.68       dsl 	if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts)) {
   1456  1.77       dsl 	    /* Append all the 'cohorts' to the list of things to examine */
   1457  1.68       dsl 	    Lst new;
   1458  1.68       dsl 	    new = Lst_Duplicate(pgn->cohorts, NULL);
   1459  1.68       dsl 	    Lst_Concat(new, examine, LST_CONCLINK);
   1460  1.68       dsl 	    examine = new;
   1461  1.68       dsl 	}
   1462   1.1       cgd 
   1463  1.68       dsl 	owln = Lst_First(pgn->children);
   1464   1.1       cgd 	Lst_Open(pgn->children);
   1465   1.1       cgd 	for (; (ln = Lst_Next(pgn->children)) != NULL; ) {
   1466  1.68       dsl 	    cgn = Lst_Datum(ln);
   1467   1.1       cgd 	    if (cgn->type & OP_WAIT) {
   1468  1.10  christos 		/* Make the .WAIT node depend on the previous children */
   1469  1.77       dsl 		Lst_ForEachFrom(pgn->children, owln, add_wait_dep, cgn);
   1470  1.11  christos 		owln = ln;
   1471  1.11  christos 	    } else {
   1472  1.11  christos 		Lst_AtEnd(examine, cgn);
   1473  1.11  christos 	    }
   1474  1.11  christos 	}
   1475  1.11  christos 	Lst_Close(pgn->children);
   1476  1.11  christos     }
   1477  1.11  christos 
   1478  1.11  christos     Lst_Destroy(examine, NULL);
   1479  1.11  christos }
   1480  1.11  christos 
   1481  1.11  christos /*-
   1482  1.11  christos  *-----------------------------------------------------------------------
   1483  1.11  christos  * Make_Run --
   1484  1.50       wiz  *	Initialize the nodes to remake and the list of nodes which are
   1485  1.50       wiz  *	ready to be made by doing a breadth-first traversal of the graph
   1486  1.50       wiz  *	starting from the nodes in the given list. Once this traversal
   1487  1.11  christos  *	is finished, all the 'leaves' of the graph are in the toBeMade
   1488  1.11  christos  *	queue.
   1489  1.11  christos  *	Using this queue and the Job module, work back up the graph,
   1490  1.11  christos  *	calling on MakeStartJobs to keep the job table as full as
   1491  1.11  christos  *	possible.
   1492  1.11  christos  *
   1493  1.11  christos  * Input:
   1494  1.11  christos  *	targs		the initial list of targets
   1495  1.11  christos  *
   1496  1.11  christos  * Results:
   1497  1.50       wiz  *	TRUE if work was done. FALSE otherwise.
   1498  1.11  christos  *
   1499  1.11  christos  * Side Effects:
   1500  1.11  christos  *	The make field of all nodes involved in the creation of the given
   1501  1.68       dsl  *	targets is set to 1. The toBeMade list is set to contain all the
   1502  1.68       dsl  *	'leaves' of these subgraphs.
   1503  1.68       dsl  *-----------------------------------------------------------------------
   1504  1.68       dsl  */
   1505  1.68       dsl Boolean
   1506  1.68       dsl Make_Run(Lst targs)
   1507  1.60       dsl {
   1508  1.68       dsl     int	    	    errors; 	/* Number of errors the Job module reports */
   1509  1.68       dsl 
   1510  1.60       dsl     /* Start trying to make the current targets... */
   1511   1.1       cgd     toBeMade = Lst_Init(FALSE);
   1512   1.1       cgd 
   1513   1.1       cgd     Make_ExpandUse(targs);
   1514   1.1       cgd     Make_ProcessWait(targs);
   1515   1.1       cgd 
   1516   1.1       cgd     if (DEBUG(MAKE)) {
   1517   1.1       cgd 	 fprintf(debug_file, "#***# full graph\n");
   1518   1.1       cgd 	 Targ_PrintGraph(1);
   1519   1.1       cgd     }
   1520  1.68       dsl 
   1521  1.68       dsl     if (queryFlag) {
   1522  1.68       dsl 	/*
   1523  1.68       dsl 	 * We wouldn't do any work unless we could start some jobs in the
   1524  1.68       dsl 	 * next loop... (we won't actually start any, of course, this is just
   1525  1.68       dsl 	 * to see if any of the targets was out of date)
   1526  1.68       dsl 	 */
   1527  1.68       dsl 	return (MakeStartJobs());
   1528   1.1       cgd     }
   1529   1.1       cgd     /*
   1530   1.1       cgd      * Initialization. At the moment, no jobs are running and until some
   1531   1.1       cgd      * get started, nothing will happen since the remaining upward
   1532   1.1       cgd      * traversal of the graph is performed by the routines in job.c upon
   1533   1.1       cgd      * the finishing of a job. So we fill the Job table as much as we can
   1534   1.1       cgd      * before going into our loop.
   1535   1.1       cgd      */
   1536   1.1       cgd     (void)MakeStartJobs();
   1537   1.1       cgd 
   1538   1.1       cgd     /*
   1539  1.62       dsl      * Main Loop: The idea here is that the ending of jobs will take
   1540  1.56  christos      * care of the maintenance of data structures and the waiting for output
   1541   1.1       cgd      * will cause us to be idle most of the time while our children run as
   1542   1.1       cgd      * much as possible. Because the job table is kept as full as possible,
   1543   1.1       cgd      * the only time when it will be empty is when all the jobs which need
   1544  1.20  christos      * running have been run, so that is the end condition of this loop.
   1545   1.1       cgd      * Note that the Job module will exit if there were any errors unless the
   1546   1.1       cgd      * keepgoing flag was given.
   1547   1.1       cgd      */
   1548   1.1       cgd     while (!Lst_IsEmpty(toBeMade) || jobTokensRunning > 0) {
   1549   1.1       cgd 	Job_CatchOutput();
   1550  1.68       dsl 	(void)MakeStartJobs();
   1551  1.68       dsl     }
   1552  1.68       dsl 
   1553  1.68       dsl     errors = Job_Finish();
   1554  1.68       dsl 
   1555  1.68       dsl     /*
   1556  1.68       dsl      * Print the final status of each target. E.g. if it wasn't made
   1557  1.68       dsl      * because some inferior reported an error.
   1558  1.68       dsl      */
   1559  1.68       dsl     if (DEBUG(MAKE))
   1560  1.68       dsl 	 fprintf(debug_file, "done: errors %d\n", errors);
   1561   1.1       cgd     if (errors == 0) {
   1562                 	Lst_ForEach(targs, MakePrintStatus, &errors);
   1563                 	if (DEBUG(MAKE)) {
   1564                 	    fprintf(debug_file, "done: errors %d\n", errors);
   1565                 	    if (errors)
   1566                 		Targ_PrintGraph(4);
   1567                 	}
   1568                     }
   1569                     return errors != 0;
   1570                 }
   1571