Home | History | Annotate | Line # | Download | only in make
targ.c revision 1.43
      1  1.43       dsl /*	$NetBSD: targ.c,v 1.43 2006/10/15 08:38:22 dsl Exp $	*/
      2   1.5  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.30       agc  *
      7  1.30       agc  * This code is derived from software contributed to Berkeley by
      8  1.30       agc  * Adam de Boor.
      9  1.30       agc  *
     10  1.30       agc  * Redistribution and use in source and binary forms, with or without
     11  1.30       agc  * modification, are permitted provided that the following conditions
     12  1.30       agc  * are met:
     13  1.30       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.30       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.30       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.30       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.30       agc  *    documentation and/or other materials provided with the distribution.
     18  1.30       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.30       agc  *    may be used to endorse or promote products derived from this software
     20  1.30       agc  *    without specific prior written permission.
     21  1.30       agc  *
     22  1.30       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.30       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.30       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.30       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.30       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.30       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.30       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.30       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.30       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.30       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.30       agc  * SUCH DAMAGE.
     33  1.30       agc  */
     34  1.30       agc 
     35  1.30       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.31      ross #ifndef MAKE_NATIVE
     72  1.43       dsl static char rcsid[] = "$NetBSD: targ.c,v 1.43 2006/10/15 08:38:22 dsl Exp $";
     73  1.14     lukem #else
     74  1.13  christos #include <sys/cdefs.h>
     75   1.1       cgd #ifndef lint
     76   1.9   thorpej #if 0
     77  1.10  christos static char sccsid[] = "@(#)targ.c	8.2 (Berkeley) 3/19/94";
     78   1.9   thorpej #else
     79  1.43       dsl __RCSID("$NetBSD: targ.c,v 1.43 2006/10/15 08:38:22 dsl Exp $");
     80   1.9   thorpej #endif
     81   1.1       cgd #endif /* not lint */
     82  1.14     lukem #endif
     83   1.1       cgd 
     84   1.1       cgd /*-
     85   1.1       cgd  * targ.c --
     86   1.1       cgd  *	Functions for maintaining the Lst allTargets. Target nodes are
     87   1.1       cgd  * kept in two structures: a Lst, maintained by the list library, and a
     88   1.1       cgd  * hash table, maintained by the hash library.
     89   1.1       cgd  *
     90   1.1       cgd  * Interface:
     91   1.1       cgd  *	Targ_Init 	    	Initialization procedure.
     92   1.1       cgd  *
     93   1.4       jtc  *	Targ_End 	    	Cleanup the module
     94   1.4       jtc  *
     95  1.12  christos  *	Targ_List 	    	Return the list of all targets so far.
     96  1.12  christos  *
     97   1.1       cgd  *	Targ_NewGN	    	Create a new GNode for the passed target
     98   1.1       cgd  *	    	  	    	(string). The node is *not* placed in the
     99   1.1       cgd  *	    	  	    	hash table, though all its fields are
    100   1.1       cgd  *	    	  	    	initialized.
    101   1.1       cgd  *
    102   1.1       cgd  *	Targ_FindNode	    	Find the node for a given target, creating
    103   1.1       cgd  *	    	  	    	and storing it if it doesn't exist and the
    104   1.1       cgd  *	    	  	    	flags are right (TARG_CREATE)
    105   1.1       cgd  *
    106   1.1       cgd  *	Targ_FindList	    	Given a list of names, find nodes for all
    107   1.1       cgd  *	    	  	    	of them. If a name doesn't exist and the
    108   1.1       cgd  *	    	  	    	TARG_NOCREATE flag was given, an error message
    109   1.1       cgd  *	    	  	    	is printed. Else, if a name doesn't exist,
    110   1.1       cgd  *	    	  	    	its node is created.
    111   1.1       cgd  *
    112   1.1       cgd  *	Targ_Ignore	    	Return TRUE if errors should be ignored when
    113   1.1       cgd  *	    	  	    	creating the given target.
    114   1.1       cgd  *
    115   1.1       cgd  *	Targ_Silent	    	Return TRUE if we should be silent when
    116   1.1       cgd  *	    	  	    	creating the given target.
    117   1.1       cgd  *
    118   1.1       cgd  *	Targ_Precious	    	Return TRUE if the target is precious and
    119   1.1       cgd  *	    	  	    	should not be removed if we are interrupted.
    120   1.1       cgd  *
    121  1.42       apb  *	Targ_Propagate		Propagate information between related
    122  1.42       apb  *				nodes.	Should be called after the
    123  1.42       apb  *				makefiles are parsed but before any
    124  1.42       apb  *				action is taken.
    125  1.42       apb  *
    126   1.1       cgd  * Debugging:
    127   1.1       cgd  *	Targ_PrintGraph	    	Print out the entire graphm all variables
    128   1.1       cgd  *	    	  	    	and statistics for the directory cache. Should
    129   1.1       cgd  *	    	  	    	print something for suffixes, too, but...
    130   1.1       cgd  */
    131   1.1       cgd 
    132   1.1       cgd #include	  <stdio.h>
    133   1.1       cgd #include	  <time.h>
    134  1.28       wiz 
    135   1.1       cgd #include	  "make.h"
    136   1.1       cgd #include	  "hash.h"
    137   1.3       cgd #include	  "dir.h"
    138   1.1       cgd 
    139   1.1       cgd static Lst        allTargets;	/* the list of all targets found so far */
    140  1.18   mycroft #ifdef CLEANUP
    141   1.4       jtc static Lst	  allGNs;	/* List of all the GNodes */
    142  1.18   mycroft #endif
    143   1.1       cgd static Hash_Table targets;	/* a hash table of same */
    144   1.1       cgd 
    145   1.1       cgd #define HTSIZE	191		/* initial size of hash table */
    146   1.1       cgd 
    147  1.28       wiz static int TargPrintOnlySrc(ClientData, ClientData);
    148  1.28       wiz static int TargPrintName(ClientData, ClientData);
    149  1.18   mycroft #ifdef CLEANUP
    150  1.28       wiz static void TargFreeGN(ClientData);
    151  1.18   mycroft #endif
    152  1.28       wiz static int TargPropagateCohort(ClientData, ClientData);
    153  1.28       wiz static int TargPropagateNode(ClientData, ClientData);
    154   1.4       jtc 
    155   1.1       cgd /*-
    156   1.1       cgd  *-----------------------------------------------------------------------
    157   1.1       cgd  * Targ_Init --
    158   1.1       cgd  *	Initialize this module
    159   1.1       cgd  *
    160   1.1       cgd  * Results:
    161   1.1       cgd  *	None
    162   1.1       cgd  *
    163   1.1       cgd  * Side Effects:
    164   1.1       cgd  *	The allTargets list and the targets hash table are initialized
    165   1.1       cgd  *-----------------------------------------------------------------------
    166   1.1       cgd  */
    167   1.1       cgd void
    168  1.28       wiz Targ_Init(void)
    169   1.1       cgd {
    170  1.34  christos     allTargets = Lst_Init(FALSE);
    171  1.34  christos     Hash_InitTable(&targets, HTSIZE);
    172   1.1       cgd }
    173   1.1       cgd 
    174   1.1       cgd /*-
    175   1.1       cgd  *-----------------------------------------------------------------------
    176   1.4       jtc  * Targ_End --
    177   1.4       jtc  *	Finalize this module
    178   1.4       jtc  *
    179   1.4       jtc  * Results:
    180   1.4       jtc  *	None
    181   1.4       jtc  *
    182   1.4       jtc  * Side Effects:
    183   1.4       jtc  *	All lists and gnodes are cleared
    184   1.4       jtc  *-----------------------------------------------------------------------
    185   1.4       jtc  */
    186   1.4       jtc void
    187  1.28       wiz Targ_End(void)
    188   1.4       jtc {
    189  1.18   mycroft #ifdef CLEANUP
    190   1.4       jtc     Lst_Destroy(allTargets, NOFREE);
    191   1.4       jtc     if (allGNs)
    192   1.4       jtc 	Lst_Destroy(allGNs, TargFreeGN);
    193   1.4       jtc     Hash_DeleteTable(&targets);
    194  1.18   mycroft #endif
    195  1.12  christos }
    196  1.12  christos 
    197  1.12  christos /*-
    198  1.12  christos  *-----------------------------------------------------------------------
    199  1.12  christos  * Targ_List --
    200  1.12  christos  *	Return the list of all targets
    201  1.12  christos  *
    202  1.12  christos  * Results:
    203  1.12  christos  *	The list of all targets.
    204  1.12  christos  *
    205  1.12  christos  * Side Effects:
    206  1.12  christos  *	None
    207  1.12  christos  *-----------------------------------------------------------------------
    208  1.12  christos  */
    209  1.12  christos Lst
    210  1.28       wiz Targ_List(void)
    211  1.12  christos {
    212  1.12  christos     return allTargets;
    213   1.4       jtc }
    214   1.4       jtc 
    215   1.4       jtc /*-
    216   1.4       jtc  *-----------------------------------------------------------------------
    217   1.1       cgd  * Targ_NewGN  --
    218   1.1       cgd  *	Create and initialize a new graph node
    219   1.1       cgd  *
    220  1.28       wiz  * Input:
    221  1.28       wiz  *	name		the name to stick in the new node
    222  1.28       wiz  *
    223   1.1       cgd  * Results:
    224   1.1       cgd  *	An initialized graph node with the name field filled with a copy
    225   1.1       cgd  *	of the passed name
    226   1.1       cgd  *
    227   1.1       cgd  * Side Effects:
    228   1.4       jtc  *	The gnode is added to the list of all gnodes.
    229   1.1       cgd  *-----------------------------------------------------------------------
    230   1.1       cgd  */
    231   1.1       cgd GNode *
    232  1.29  christos Targ_NewGN(const char *name)
    233   1.1       cgd {
    234  1.28       wiz     GNode *gn;
    235   1.1       cgd 
    236  1.34  christos     gn = emalloc(sizeof(GNode));
    237  1.34  christos     gn->name = estrdup(name);
    238  1.11  christos     gn->uname = NULL;
    239  1.29  christos     gn->path = NULL;
    240   1.1       cgd     if (name[0] == '-' && name[1] == 'l') {
    241   1.1       cgd 	gn->type = OP_LIB;
    242   1.1       cgd     } else {
    243   1.1       cgd 	gn->type = 0;
    244   1.1       cgd     }
    245   1.1       cgd     gn->unmade =    	0;
    246  1.27        pk     gn->unmade_cohorts = 0;
    247  1.27        pk     gn->centurion =    	NULL;
    248   1.1       cgd     gn->made = 	    	UNMADE;
    249  1.16  christos     gn->flags = 	0;
    250   1.6  christos     gn->order =		0;
    251   1.1       cgd     gn->mtime = gn->cmtime = 0;
    252  1.34  christos     gn->iParents =  	Lst_Init(FALSE);
    253  1.34  christos     gn->cohorts =   	Lst_Init(FALSE);
    254  1.34  christos     gn->parents =   	Lst_Init(FALSE);
    255  1.42       apb     gn->ancestors =   	Lst_Init(FALSE);
    256  1.34  christos     gn->children =  	Lst_Init(FALSE);
    257  1.34  christos     gn->successors = 	Lst_Init(FALSE);
    258  1.34  christos     gn->preds =     	Lst_Init(FALSE);
    259  1.42       apb     gn->recpreds =	Lst_Init(FALSE);
    260  1.17   mycroft     Hash_InitTable(&gn->context, 0);
    261  1.34  christos     gn->commands =  	Lst_Init(FALSE);
    262   1.3       cgd     gn->suffix =	NULL;
    263  1.21  christos     gn->lineno =	0;
    264  1.21  christos     gn->fname = 	NULL;
    265   1.1       cgd 
    266  1.18   mycroft #ifdef CLEANUP
    267   1.4       jtc     if (allGNs == NULL)
    268   1.4       jtc 	allGNs = Lst_Init(FALSE);
    269   1.4       jtc     Lst_AtEnd(allGNs, (ClientData) gn);
    270  1.18   mycroft #endif
    271   1.4       jtc 
    272   1.1       cgd     return (gn);
    273   1.1       cgd }
    274   1.1       cgd 
    275  1.18   mycroft #ifdef CLEANUP
    276   1.1       cgd /*-
    277   1.1       cgd  *-----------------------------------------------------------------------
    278   1.4       jtc  * TargFreeGN  --
    279   1.4       jtc  *	Destroy a GNode
    280   1.4       jtc  *
    281   1.4       jtc  * Results:
    282   1.4       jtc  *	None.
    283   1.4       jtc  *
    284   1.4       jtc  * Side Effects:
    285   1.4       jtc  *	None.
    286   1.4       jtc  *-----------------------------------------------------------------------
    287   1.4       jtc  */
    288   1.4       jtc static void
    289  1.28       wiz TargFreeGN(ClientData gnp)
    290   1.4       jtc {
    291  1.37  christos     GNode *gn = (GNode *)gnp;
    292   1.4       jtc 
    293   1.4       jtc 
    294   1.4       jtc     free(gn->name);
    295  1.11  christos     if (gn->uname)
    296  1.11  christos 	free(gn->uname);
    297   1.4       jtc     if (gn->path)
    298   1.4       jtc 	free(gn->path);
    299  1.21  christos     if (gn->fname)
    300  1.21  christos 	free(gn->fname);
    301   1.4       jtc 
    302   1.4       jtc     Lst_Destroy(gn->iParents, NOFREE);
    303   1.4       jtc     Lst_Destroy(gn->cohorts, NOFREE);
    304   1.4       jtc     Lst_Destroy(gn->parents, NOFREE);
    305  1.42       apb     Lst_Destroy(gn->ancestors, NOFREE);
    306   1.4       jtc     Lst_Destroy(gn->children, NOFREE);
    307   1.4       jtc     Lst_Destroy(gn->successors, NOFREE);
    308   1.4       jtc     Lst_Destroy(gn->preds, NOFREE);
    309  1.42       apb     Lst_Destroy(gn->recpreds, NOFREE);
    310  1.17   mycroft     Hash_DeleteTable(&gn->context);
    311   1.4       jtc     Lst_Destroy(gn->commands, NOFREE);
    312  1.36  christos     free(gn);
    313   1.4       jtc }
    314  1.18   mycroft #endif
    315   1.4       jtc 
    316   1.4       jtc 
    317   1.4       jtc /*-
    318   1.4       jtc  *-----------------------------------------------------------------------
    319   1.1       cgd  * Targ_FindNode  --
    320   1.1       cgd  *	Find a node in the list using the given name for matching
    321   1.1       cgd  *
    322  1.28       wiz  * Input:
    323  1.28       wiz  *	name		the name to find
    324  1.28       wiz  *	flags		flags governing events when target not
    325  1.28       wiz  *			found
    326  1.28       wiz  *
    327   1.1       cgd  * Results:
    328   1.1       cgd  *	The node in the list if it was. If it wasn't, return NILGNODE of
    329   1.1       cgd  *	flags was TARG_NOCREATE or the newly created and initialized node
    330   1.1       cgd  *	if it was TARG_CREATE
    331   1.1       cgd  *
    332   1.1       cgd  * Side Effects:
    333   1.1       cgd  *	Sometimes a node is created and added to the list
    334   1.1       cgd  *-----------------------------------------------------------------------
    335   1.1       cgd  */
    336   1.1       cgd GNode *
    337  1.29  christos Targ_FindNode(const char *name, int flags)
    338   1.1       cgd {
    339   1.1       cgd     GNode         *gn;	      /* node in that element */
    340   1.1       cgd     Hash_Entry	  *he;	      /* New or used hash entry for node */
    341   1.1       cgd     Boolean	  isNew;      /* Set TRUE if Hash_CreateEntry had to create */
    342   1.1       cgd 			      /* an entry for the node */
    343   1.1       cgd 
    344   1.1       cgd 
    345   1.1       cgd     if (flags & TARG_CREATE) {
    346  1.34  christos 	he = Hash_CreateEntry(&targets, name, &isNew);
    347   1.1       cgd 	if (isNew) {
    348  1.34  christos 	    gn = Targ_NewGN(name);
    349  1.35  christos 	    Hash_SetValue(he, gn);
    350  1.24        tv 	    Var_Append(".ALLTARGETS", name, VAR_GLOBAL);
    351  1.35  christos 	    (void)Lst_AtEnd(allTargets, (ClientData)gn);
    352   1.1       cgd 	}
    353   1.1       cgd     } else {
    354  1.34  christos 	he = Hash_FindEntry(&targets, name);
    355   1.1       cgd     }
    356   1.1       cgd 
    357  1.37  christos     if (he == NULL) {
    358   1.1       cgd 	return (NILGNODE);
    359   1.1       cgd     } else {
    360  1.35  christos 	return ((GNode *)Hash_GetValue(he));
    361   1.1       cgd     }
    362   1.1       cgd }
    363   1.1       cgd 
    364   1.1       cgd /*-
    365   1.1       cgd  *-----------------------------------------------------------------------
    366   1.1       cgd  * Targ_FindList --
    367  1.10  christos  *	Make a complete list of GNodes from the given list of names
    368   1.1       cgd  *
    369  1.28       wiz  * Input:
    370  1.28       wiz  *	name		list of names to find
    371  1.28       wiz  *	flags		flags used if no node is found for a given name
    372  1.28       wiz  *
    373   1.1       cgd  * Results:
    374   1.1       cgd  *	A complete list of graph nodes corresponding to all instances of all
    375  1.10  christos  *	the names in names.
    376   1.1       cgd  *
    377   1.1       cgd  * Side Effects:
    378   1.1       cgd  *	If flags is TARG_CREATE, nodes will be created for all names in
    379   1.1       cgd  *	names which do not yet have graph nodes. If flags is TARG_NOCREATE,
    380   1.1       cgd  *	an error message will be printed for each name which can't be found.
    381   1.1       cgd  * -----------------------------------------------------------------------
    382   1.1       cgd  */
    383   1.1       cgd Lst
    384  1.28       wiz Targ_FindList(Lst names, int flags)
    385   1.1       cgd {
    386   1.1       cgd     Lst            nodes;	/* result list */
    387  1.28       wiz     LstNode	   ln;		/* name list element */
    388  1.28       wiz     GNode	   *gn;		/* node in tLn */
    389  1.28       wiz     char    	   *name;
    390   1.1       cgd 
    391  1.34  christos     nodes = Lst_Init(FALSE);
    392   1.1       cgd 
    393  1.34  christos     if (Lst_Open(names) == FAILURE) {
    394   1.1       cgd 	return (nodes);
    395   1.1       cgd     }
    396  1.34  christos     while ((ln = Lst_Next(names)) != NILLNODE) {
    397   1.1       cgd 	name = (char *)Lst_Datum(ln);
    398  1.34  christos 	gn = Targ_FindNode(name, flags);
    399   1.1       cgd 	if (gn != NILGNODE) {
    400   1.1       cgd 	    /*
    401   1.1       cgd 	     * Note: Lst_AtEnd must come before the Lst_Concat so the nodes
    402   1.1       cgd 	     * are added to the list in the order in which they were
    403   1.1       cgd 	     * encountered in the makefile.
    404   1.1       cgd 	     */
    405  1.35  christos 	    (void)Lst_AtEnd(nodes, (ClientData)gn);
    406   1.1       cgd 	} else if (flags == TARG_NOCREATE) {
    407  1.34  christos 	    Error("\"%s\" -- target unknown.", name);
    408   1.1       cgd 	}
    409   1.1       cgd     }
    410  1.34  christos     Lst_Close(names);
    411   1.1       cgd     return (nodes);
    412   1.1       cgd }
    413   1.1       cgd 
    414   1.1       cgd /*-
    415   1.1       cgd  *-----------------------------------------------------------------------
    416   1.1       cgd  * Targ_Ignore  --
    417   1.1       cgd  *	Return true if should ignore errors when creating gn
    418   1.1       cgd  *
    419  1.28       wiz  * Input:
    420  1.28       wiz  *	gn		node to check for
    421  1.28       wiz  *
    422   1.1       cgd  * Results:
    423   1.1       cgd  *	TRUE if should ignore errors
    424   1.1       cgd  *
    425   1.1       cgd  * Side Effects:
    426   1.1       cgd  *	None
    427   1.1       cgd  *-----------------------------------------------------------------------
    428   1.1       cgd  */
    429   1.1       cgd Boolean
    430  1.28       wiz Targ_Ignore(GNode *gn)
    431   1.1       cgd {
    432   1.1       cgd     if (ignoreErrors || gn->type & OP_IGNORE) {
    433   1.1       cgd 	return (TRUE);
    434   1.1       cgd     } else {
    435   1.1       cgd 	return (FALSE);
    436   1.1       cgd     }
    437   1.1       cgd }
    438   1.1       cgd 
    439   1.1       cgd /*-
    440   1.1       cgd  *-----------------------------------------------------------------------
    441   1.1       cgd  * Targ_Silent  --
    442   1.1       cgd  *	Return true if be silent when creating gn
    443   1.1       cgd  *
    444  1.28       wiz  * Input:
    445  1.28       wiz  *	gn		node to check for
    446  1.28       wiz  *
    447   1.1       cgd  * Results:
    448   1.1       cgd  *	TRUE if should be silent
    449   1.1       cgd  *
    450   1.1       cgd  * Side Effects:
    451   1.1       cgd  *	None
    452   1.1       cgd  *-----------------------------------------------------------------------
    453   1.1       cgd  */
    454   1.1       cgd Boolean
    455  1.28       wiz Targ_Silent(GNode *gn)
    456   1.1       cgd {
    457   1.1       cgd     if (beSilent || gn->type & OP_SILENT) {
    458   1.1       cgd 	return (TRUE);
    459   1.1       cgd     } else {
    460   1.1       cgd 	return (FALSE);
    461   1.1       cgd     }
    462   1.1       cgd }
    463   1.1       cgd 
    464   1.1       cgd /*-
    465   1.1       cgd  *-----------------------------------------------------------------------
    466   1.1       cgd  * Targ_Precious --
    467   1.1       cgd  *	See if the given target is precious
    468   1.1       cgd  *
    469  1.28       wiz  * Input:
    470  1.28       wiz  *	gn		the node to check
    471  1.28       wiz  *
    472   1.1       cgd  * Results:
    473   1.1       cgd  *	TRUE if it is precious. FALSE otherwise
    474   1.1       cgd  *
    475   1.1       cgd  * Side Effects:
    476   1.1       cgd  *	None
    477   1.1       cgd  *-----------------------------------------------------------------------
    478   1.1       cgd  */
    479   1.1       cgd Boolean
    480  1.28       wiz Targ_Precious(GNode *gn)
    481   1.1       cgd {
    482   1.1       cgd     if (allPrecious || (gn->type & (OP_PRECIOUS|OP_DOUBLEDEP))) {
    483   1.1       cgd 	return (TRUE);
    484   1.1       cgd     } else {
    485   1.1       cgd 	return (FALSE);
    486   1.1       cgd     }
    487   1.1       cgd }
    488   1.1       cgd 
    489   1.1       cgd /******************* DEBUG INFO PRINTING ****************/
    490   1.1       cgd 
    491   1.1       cgd static GNode	  *mainTarg;	/* the main target, as set by Targ_SetMain */
    492  1.10  christos /*-
    493   1.1       cgd  *-----------------------------------------------------------------------
    494   1.1       cgd  * Targ_SetMain --
    495   1.1       cgd  *	Set our idea of the main target we'll be creating. Used for
    496   1.1       cgd  *	debugging output.
    497   1.1       cgd  *
    498  1.28       wiz  * Input:
    499  1.28       wiz  *	gn		The main target we'll create
    500  1.28       wiz  *
    501   1.1       cgd  * Results:
    502   1.1       cgd  *	None.
    503   1.1       cgd  *
    504   1.1       cgd  * Side Effects:
    505   1.1       cgd  *	"mainTarg" is set to the main target's node.
    506   1.1       cgd  *-----------------------------------------------------------------------
    507   1.1       cgd  */
    508   1.1       cgd void
    509  1.28       wiz Targ_SetMain(GNode *gn)
    510   1.1       cgd {
    511   1.1       cgd     mainTarg = gn;
    512   1.1       cgd }
    513   1.1       cgd 
    514  1.38       dsl #define PrintWait (ClientData)1
    515  1.38       dsl #define PrintPath (ClientData)2
    516  1.38       dsl 
    517   1.1       cgd static int
    518  1.38       dsl TargPrintName(ClientData gnp, ClientData pflags)
    519   1.1       cgd {
    520  1.38       dsl     static int last_order;
    521  1.37  christos     GNode *gn = (GNode *)gnp;
    522  1.38       dsl 
    523  1.38       dsl     if (pflags == PrintWait && gn->order > last_order)
    524  1.43       dsl 	fprintf(debug_file, ".WAIT ");
    525  1.38       dsl     last_order = gn->order;
    526  1.38       dsl 
    527  1.43       dsl     fprintf(debug_file, "%s ", gn->name);
    528  1.38       dsl 
    529   1.1       cgd #ifdef notdef
    530  1.38       dsl     if (pflags == PrintPath) {
    531   1.1       cgd 	if (gn->path) {
    532  1.43       dsl 	    fprintf(debug_file, "[%s]  ", gn->path);
    533   1.1       cgd 	}
    534   1.1       cgd 	if (gn == mainTarg) {
    535  1.43       dsl 	    fprintf(debug_file, "(MAIN NAME)  ");
    536   1.1       cgd 	}
    537   1.1       cgd     }
    538   1.3       cgd #endif /* notdef */
    539  1.38       dsl 
    540  1.38       dsl     return 0;
    541   1.1       cgd }
    542   1.1       cgd 
    543   1.1       cgd 
    544   1.1       cgd int
    545  1.28       wiz Targ_PrintCmd(ClientData cmd, ClientData dummy)
    546   1.1       cgd {
    547  1.43       dsl     fprintf(debug_file, "\t%s\n", (char *)cmd);
    548   1.4       jtc     return (dummy ? 0 : 0);
    549   1.1       cgd }
    550   1.1       cgd 
    551   1.1       cgd /*-
    552   1.1       cgd  *-----------------------------------------------------------------------
    553   1.1       cgd  * Targ_FmtTime --
    554   1.1       cgd  *	Format a modification time in some reasonable way and return it.
    555   1.1       cgd  *
    556   1.1       cgd  * Results:
    557   1.1       cgd  *	The time reformatted.
    558   1.1       cgd  *
    559   1.1       cgd  * Side Effects:
    560   1.1       cgd  *	The time is placed in a static area, so it is overwritten
    561   1.1       cgd  *	with each call.
    562   1.1       cgd  *
    563   1.1       cgd  *-----------------------------------------------------------------------
    564   1.1       cgd  */
    565   1.1       cgd char *
    566  1.28       wiz Targ_FmtTime(time_t tm)
    567   1.1       cgd {
    568   1.1       cgd     struct tm	  	*parts;
    569  1.15  christos     static char	  	buf[128];
    570   1.1       cgd 
    571  1.25   reinoud     parts = localtime(&tm);
    572  1.15  christos     (void)strftime(buf, sizeof buf, "%k:%M:%S %b %d, %Y", parts);
    573   1.1       cgd     return(buf);
    574   1.1       cgd }
    575  1.10  christos 
    576   1.1       cgd /*-
    577   1.1       cgd  *-----------------------------------------------------------------------
    578   1.1       cgd  * Targ_PrintType --
    579   1.1       cgd  *	Print out a type field giving only those attributes the user can
    580   1.1       cgd  *	set.
    581   1.1       cgd  *
    582   1.1       cgd  * Results:
    583   1.1       cgd  *
    584   1.1       cgd  * Side Effects:
    585   1.1       cgd  *
    586   1.1       cgd  *-----------------------------------------------------------------------
    587   1.1       cgd  */
    588   1.1       cgd void
    589  1.28       wiz Targ_PrintType(int type)
    590   1.1       cgd {
    591  1.28       wiz     int    tbit;
    592  1.10  christos 
    593  1.43       dsl #define PRINTBIT(attr)	case CONCAT(OP_,attr): fprintf(debug_file, "." #attr " "); break
    594  1.43       dsl #define PRINTDBIT(attr) case CONCAT(OP_,attr): if (DEBUG(TARG))fprintf(debug_file, "." #attr " "); break
    595   1.1       cgd 
    596   1.1       cgd     type &= ~OP_OPMASK;
    597   1.1       cgd 
    598   1.1       cgd     while (type) {
    599   1.1       cgd 	tbit = 1 << (ffs(type) - 1);
    600   1.1       cgd 	type &= ~tbit;
    601   1.1       cgd 
    602   1.1       cgd 	switch(tbit) {
    603   1.1       cgd 	    PRINTBIT(OPTIONAL);
    604   1.1       cgd 	    PRINTBIT(USE);
    605   1.1       cgd 	    PRINTBIT(EXEC);
    606   1.1       cgd 	    PRINTBIT(IGNORE);
    607   1.1       cgd 	    PRINTBIT(PRECIOUS);
    608   1.1       cgd 	    PRINTBIT(SILENT);
    609   1.1       cgd 	    PRINTBIT(MAKE);
    610   1.1       cgd 	    PRINTBIT(JOIN);
    611   1.1       cgd 	    PRINTBIT(INVISIBLE);
    612   1.1       cgd 	    PRINTBIT(NOTMAIN);
    613   1.1       cgd 	    PRINTDBIT(LIB);
    614   1.1       cgd 	    /*XXX: MEMBER is defined, so CONCAT(OP_,MEMBER) gives OP_"%" */
    615  1.43       dsl 	    case OP_MEMBER: if (DEBUG(TARG))fprintf(debug_file, ".MEMBER "); break;
    616   1.1       cgd 	    PRINTDBIT(ARCHV);
    617  1.26        pk 	    PRINTDBIT(MADE);
    618  1.26        pk 	    PRINTDBIT(PHONY);
    619   1.1       cgd 	}
    620   1.1       cgd     }
    621   1.1       cgd }
    622   1.1       cgd 
    623   1.1       cgd /*-
    624   1.1       cgd  *-----------------------------------------------------------------------
    625   1.1       cgd  * TargPrintNode --
    626   1.1       cgd  *	print the contents of a node
    627   1.1       cgd  *-----------------------------------------------------------------------
    628   1.1       cgd  */
    629  1.40       dsl int
    630  1.40       dsl Targ_PrintNode(ClientData gnp, ClientData passp)
    631   1.1       cgd {
    632  1.37  christos     GNode         *gn = (GNode *)gnp;
    633  1.40       dsl     int	    	  pass = passp ? *(int *)passp : 0;
    634   1.1       cgd     if (!OP_NOP(gn->type)) {
    635  1.43       dsl 	fprintf(debug_file, "#\n");
    636   1.1       cgd 	if (gn == mainTarg) {
    637  1.43       dsl 	    fprintf(debug_file, "# *** MAIN TARGET ***\n");
    638   1.1       cgd 	}
    639   1.1       cgd 	if (pass == 2) {
    640   1.1       cgd 	    if (gn->unmade) {
    641  1.43       dsl 		fprintf(debug_file, "# %d unmade children\n", gn->unmade);
    642   1.1       cgd 	    } else {
    643  1.43       dsl 		fprintf(debug_file, "# No unmade children\n");
    644   1.1       cgd 	    }
    645  1.22  christos 	    if (! (gn->type & (OP_JOIN|OP_USE|OP_USEBEFORE|OP_EXEC))) {
    646   1.1       cgd 		if (gn->mtime != 0) {
    647  1.43       dsl 		    fprintf(debug_file, "# last modified %s: %s\n",
    648   1.1       cgd 			      Targ_FmtTime(gn->mtime),
    649   1.1       cgd 			      (gn->made == UNMADE ? "unmade" :
    650   1.1       cgd 			       (gn->made == MADE ? "made" :
    651   1.1       cgd 				(gn->made == UPTODATE ? "up-to-date" :
    652   1.1       cgd 				 "error when made"))));
    653   1.1       cgd 		} else if (gn->made != UNMADE) {
    654  1.43       dsl 		    fprintf(debug_file, "# non-existent (maybe): %s\n",
    655   1.1       cgd 			      (gn->made == MADE ? "made" :
    656   1.1       cgd 			       (gn->made == UPTODATE ? "up-to-date" :
    657   1.1       cgd 				(gn->made == ERROR ? "error when made" :
    658   1.1       cgd 				 "aborted"))));
    659   1.1       cgd 		} else {
    660  1.43       dsl 		    fprintf(debug_file, "# unmade\n");
    661   1.1       cgd 		}
    662   1.1       cgd 	    }
    663   1.1       cgd 	    if (!Lst_IsEmpty (gn->iParents)) {
    664  1.43       dsl 		fprintf(debug_file, "# implicit parents: ");
    665  1.34  christos 		Lst_ForEach(gn->iParents, TargPrintName, (ClientData)0);
    666  1.43       dsl 		fprintf(debug_file, "\n");
    667   1.1       cgd 	    }
    668  1.41       dsl 	} else {
    669  1.41       dsl 	    if (gn->unmade)
    670  1.43       dsl 		fprintf(debug_file, "# %d unmade children\n", gn->unmade);
    671   1.1       cgd 	}
    672   1.1       cgd 	if (!Lst_IsEmpty (gn->parents)) {
    673  1.43       dsl 	    fprintf(debug_file, "# parents: ");
    674  1.34  christos 	    Lst_ForEach(gn->parents, TargPrintName, (ClientData)0);
    675  1.43       dsl 	    fprintf(debug_file, "\n");
    676   1.1       cgd 	}
    677  1.42       apb 	if (!Lst_IsEmpty (gn->children)) {
    678  1.43       dsl 	    fprintf(debug_file, "# children: ");
    679  1.42       apb 	    Lst_ForEach(gn->children, TargPrintName, (ClientData)0);
    680  1.43       dsl 	    fprintf(debug_file, "\n");
    681  1.42       apb 	}
    682  1.39       dsl 	if (!Lst_IsEmpty (gn->preds)) {
    683  1.43       dsl 	    fprintf(debug_file, "# preds: ");
    684  1.39       dsl 	    Lst_ForEach(gn->preds, TargPrintName, (ClientData)0);
    685  1.43       dsl 	    fprintf(debug_file, "\n");
    686  1.39       dsl 	}
    687  1.42       apb 	if (!Lst_IsEmpty (gn->recpreds)) {
    688  1.43       dsl 	    fprintf(debug_file, "# recpreds: ");
    689  1.42       apb 	    Lst_ForEach(gn->recpreds, TargPrintName, (ClientData)0);
    690  1.43       dsl 	    fprintf(debug_file, "\n");
    691  1.42       apb 	}
    692  1.39       dsl 	if (!Lst_IsEmpty (gn->successors)) {
    693  1.43       dsl 	    fprintf(debug_file, "# successors: ");
    694  1.39       dsl 	    Lst_ForEach(gn->successors, TargPrintName, (ClientData)0);
    695  1.43       dsl 	    fprintf(debug_file, "\n");
    696  1.39       dsl 	}
    697  1.10  christos 
    698  1.43       dsl 	fprintf(debug_file, "%-16s", gn->name);
    699   1.1       cgd 	switch (gn->type & OP_OPMASK) {
    700   1.1       cgd 	    case OP_DEPENDS:
    701  1.43       dsl 		fprintf(debug_file, ": "); break;
    702   1.1       cgd 	    case OP_FORCE:
    703  1.43       dsl 		fprintf(debug_file, "! "); break;
    704   1.1       cgd 	    case OP_DOUBLEDEP:
    705  1.43       dsl 		fprintf(debug_file, ":: "); break;
    706   1.1       cgd 	}
    707  1.34  christos 	Targ_PrintType(gn->type);
    708  1.38       dsl 	Lst_ForEach(gn->children, TargPrintName, PrintWait);
    709  1.43       dsl 	fprintf(debug_file, "\n");
    710  1.34  christos 	Lst_ForEach(gn->commands, Targ_PrintCmd, (ClientData)0);
    711  1.43       dsl 	fprintf(debug_file, "\n\n");
    712  1.20   mycroft 	if (gn->type & OP_DOUBLEDEP) {
    713  1.40       dsl 	    Lst_ForEach(gn->cohorts, Targ_PrintNode, (ClientData)&pass);
    714   1.1       cgd 	}
    715   1.1       cgd     }
    716   1.1       cgd     return (0);
    717   1.1       cgd }
    718   1.1       cgd 
    719   1.1       cgd /*-
    720   1.1       cgd  *-----------------------------------------------------------------------
    721   1.1       cgd  * TargPrintOnlySrc --
    722   1.1       cgd  *	Print only those targets that are just a source.
    723   1.1       cgd  *
    724   1.1       cgd  * Results:
    725   1.1       cgd  *	0.
    726   1.1       cgd  *
    727   1.1       cgd  * Side Effects:
    728  1.23       wiz  *	The name of each file is printed preceded by #\t
    729   1.1       cgd  *
    730   1.1       cgd  *-----------------------------------------------------------------------
    731   1.1       cgd  */
    732   1.1       cgd static int
    733  1.28       wiz TargPrintOnlySrc(ClientData gnp, ClientData dummy)
    734   1.4       jtc {
    735  1.37  christos     GNode   	  *gn = (GNode *)gnp;
    736   1.4       jtc     if (OP_NOP(gn->type))
    737  1.43       dsl 	fprintf(debug_file, "#\t%s [%s]\n", gn->name, gn->path ? gn->path : gn->name);
    738   1.4       jtc 
    739   1.4       jtc     return (dummy ? 0 : 0);
    740   1.1       cgd }
    741   1.1       cgd 
    742   1.1       cgd /*-
    743   1.1       cgd  *-----------------------------------------------------------------------
    744   1.1       cgd  * Targ_PrintGraph --
    745   1.1       cgd  *	print the entire graph. heh heh
    746   1.1       cgd  *
    747  1.28       wiz  * Input:
    748  1.28       wiz  *	pass		Which pass this is. 1 => no processing
    749  1.28       wiz  *			2 => processing done
    750  1.28       wiz  *
    751   1.1       cgd  * Results:
    752   1.1       cgd  *	none
    753   1.1       cgd  *
    754   1.1       cgd  * Side Effects:
    755   1.1       cgd  *	lots o' output
    756   1.1       cgd  *-----------------------------------------------------------------------
    757   1.1       cgd  */
    758   1.3       cgd void
    759  1.28       wiz Targ_PrintGraph(int pass)
    760   1.1       cgd {
    761  1.43       dsl     fprintf(debug_file, "#*** Input graph:\n");
    762  1.40       dsl     Lst_ForEach(allTargets, Targ_PrintNode, (ClientData)&pass);
    763  1.43       dsl     fprintf(debug_file, "\n\n");
    764  1.43       dsl     fprintf(debug_file, "#\n#   Files that are only sources:\n");
    765  1.34  christos     Lst_ForEach(allTargets, TargPrintOnlySrc, (ClientData) 0);
    766  1.43       dsl     fprintf(debug_file, "#*** Global Variables:\n");
    767  1.34  christos     Var_Dump(VAR_GLOBAL);
    768  1.43       dsl     fprintf(debug_file, "#*** Command-line Variables:\n");
    769  1.34  christos     Var_Dump(VAR_CMD);
    770  1.43       dsl     fprintf(debug_file, "\n");
    771   1.1       cgd     Dir_PrintDirectories();
    772  1.43       dsl     fprintf(debug_file, "\n");
    773   1.1       cgd     Suff_PrintAll();
    774  1.19   mycroft }
    775  1.19   mycroft 
    776  1.42       apb /*-
    777  1.42       apb  *-----------------------------------------------------------------------
    778  1.42       apb  * TargAppendAncestor -
    779  1.42       apb  *	Appends a single ancestor to a node's list of ancestors,
    780  1.42       apb  *	ignoring duplicates.
    781  1.42       apb  *
    782  1.42       apb  * Input:
    783  1.42       apb  *	ancestorgnp	An ancestor to be added to a list.
    784  1.42       apb  *	thisgnp		The node whose ancestor list will be changed.
    785  1.42       apb  *
    786  1.42       apb  * Results:
    787  1.42       apb  *	Always returns 0, for the benefit of Lst_ForEach().
    788  1.42       apb  *
    789  1.42       apb  * Side Effects:
    790  1.42       apb  *	May modify the ancestors list of the node we are
    791  1.42       apb  *	examining.
    792  1.42       apb  *-----------------------------------------------------------------------
    793  1.42       apb  */
    794  1.42       apb static int
    795  1.42       apb TargAppendAncestor(ClientData ancestorgnp, ClientData thisgnp)
    796  1.42       apb {
    797  1.42       apb     GNode	  *ancestorgn = (GNode *)ancestorgnp;
    798  1.42       apb     GNode	  *thisgn = (GNode *)thisgnp;
    799  1.42       apb 
    800  1.42       apb     if (Lst_Member(thisgn->ancestors, ancestorgn) == NILLNODE) {
    801  1.42       apb 	(void)Lst_AtEnd(thisgn->ancestors, (ClientData)ancestorgn);
    802  1.42       apb     }
    803  1.42       apb     return (0);
    804  1.42       apb }
    805  1.42       apb 
    806  1.42       apb /*-
    807  1.42       apb  *-----------------------------------------------------------------------
    808  1.42       apb  * TargAppendParentAncestors -
    809  1.42       apb  *	Appends all ancestors of a parent node to the ancestor list of a
    810  1.42       apb  *	given node, ignoring duplicates.
    811  1.42       apb  *
    812  1.42       apb  * Input:
    813  1.42       apb  *	parentgnp	A parent node whose ancestor list will be
    814  1.42       apb  *			propagated to another node.
    815  1.42       apb  *	thisgnp		The node whose ancestor list will be changed.
    816  1.42       apb  *
    817  1.42       apb  * Results:
    818  1.42       apb  *	Always returns 0, for the benefit of Lst_ForEach().
    819  1.42       apb  *
    820  1.42       apb  * Side Effects:
    821  1.42       apb  *	May modify the ancestors list of the node we are
    822  1.42       apb  *	examining.
    823  1.42       apb  *-----------------------------------------------------------------------
    824  1.42       apb  */
    825  1.42       apb static int
    826  1.42       apb TargAppendParentAncestors(ClientData parentgnp, ClientData thisgnp)
    827  1.42       apb {
    828  1.42       apb     GNode	  *parentgn = (GNode *)parentgnp;
    829  1.42       apb     GNode	  *thisgn = (GNode *)thisgnp;
    830  1.42       apb 
    831  1.42       apb     Lst_ForEach(parentgn->ancestors, TargAppendAncestor, thisgn);
    832  1.42       apb     return (0);
    833  1.42       apb }
    834  1.42       apb 
    835  1.42       apb /*-
    836  1.42       apb  *-----------------------------------------------------------------------
    837  1.42       apb  * TargInitAncestors -
    838  1.42       apb  *	Initialises the ancestor list of a node and all the
    839  1.42       apb  *	node's ancestors.
    840  1.42       apb  *
    841  1.42       apb  * Input:
    842  1.42       apb  *	thisgnp		The node that we are examining.
    843  1.42       apb  *
    844  1.42       apb  * Results:
    845  1.42       apb  *	Always returns 0, for the benefit of Lst_ForEach().
    846  1.42       apb  *
    847  1.42       apb  * Side Effects:
    848  1.42       apb  *	May initialise the ancestors list of the node we are
    849  1.42       apb  *	examining and all its ancestors.  Does nothing if the
    850  1.42       apb  *	list has already been initialised.
    851  1.42       apb  *-----------------------------------------------------------------------
    852  1.42       apb  */
    853  1.19   mycroft static int
    854  1.42       apb TargInitAncestors(ClientData thisgnp, ClientData junk __unused)
    855  1.42       apb {
    856  1.42       apb     GNode	  *thisgn = (GNode *)thisgnp;
    857  1.42       apb 
    858  1.42       apb     if (Lst_IsEmpty (thisgn->ancestors)) {
    859  1.42       apb 	/*
    860  1.42       apb 	 * Add our parents to our ancestor list before recursing, to
    861  1.42       apb 	 * ensure that loops in the dependency graph will not result in
    862  1.42       apb 	 * infinite recursion.
    863  1.42       apb 	 */
    864  1.42       apb 	Lst_ForEach(thisgn->parents, TargAppendAncestor, thisgn);
    865  1.42       apb 	Lst_ForEach(thisgn->iParents, TargAppendAncestor, thisgn);
    866  1.42       apb 	/* Recursively initialise our parents' ancestor lists */
    867  1.42       apb 	Lst_ForEach(thisgn->parents, TargInitAncestors, (ClientData)0);
    868  1.42       apb 	Lst_ForEach(thisgn->iParents, TargInitAncestors, (ClientData)0);
    869  1.42       apb 	/* Our parents' ancestors are also our ancestors */
    870  1.42       apb 	Lst_ForEach(thisgn->parents, TargAppendParentAncestors, thisgn);
    871  1.42       apb 	Lst_ForEach(thisgn->iParents, TargAppendParentAncestors, thisgn);
    872  1.42       apb     }
    873  1.42       apb     return (0);
    874  1.42       apb }
    875  1.42       apb 
    876  1.42       apb /*-
    877  1.42       apb  *-----------------------------------------------------------------------
    878  1.42       apb  * TargHasAncestor -
    879  1.42       apb  *	Checks whether one node is an ancestor of another node.
    880  1.42       apb  *
    881  1.42       apb  *	If called with both arguments pointing to the
    882  1.42       apb  *	same node, checks whether the node is part of a cycle
    883  1.42       apb  *	in the graph.
    884  1.42       apb  *
    885  1.42       apb  * Input:
    886  1.42       apb  *	thisgnp		The node whose ancestor list we are examining.
    887  1.42       apb  *	seekgnp		The node that we are seeking in the
    888  1.42       apb  *			ancestor list.
    889  1.42       apb  *
    890  1.42       apb  * Results:
    891  1.42       apb  *	TRUE if seekgn is an ancestor of thisgn; FALSE if not.
    892  1.42       apb  *
    893  1.42       apb  * Side Effects:
    894  1.42       apb  *	Initialises the ancestors list in thisgnp and all its ancestors.
    895  1.42       apb  *-----------------------------------------------------------------------
    896  1.42       apb  */
    897  1.42       apb static Boolean
    898  1.42       apb TargHasAncestor(ClientData thisgnp, ClientData seekgnp)
    899  1.42       apb {
    900  1.42       apb     GNode	  *thisgn = (GNode *)thisgnp;
    901  1.42       apb     GNode	  *seekgn = (GNode *)seekgnp;
    902  1.42       apb 
    903  1.42       apb     TargInitAncestors(thisgn, (ClientData)0);
    904  1.42       apb     if (Lst_Member(thisgn->ancestors, seekgn) != NILLNODE) {
    905  1.42       apb 	return (TRUE);
    906  1.42       apb     }
    907  1.42       apb     return (FALSE);
    908  1.42       apb }
    909  1.42       apb 
    910  1.42       apb /*-
    911  1.42       apb  *-----------------------------------------------------------------------
    912  1.42       apb  * TargPropagateRecpredChild --
    913  1.42       apb  *	Create a new predecessor/successor relationship between a pair
    914  1.42       apb  *	of nodes, and recursively propagate the relationship to all
    915  1.42       apb  *	children of the successor node.
    916  1.42       apb  *
    917  1.42       apb  *	If there is already a predecessor/successor relationship
    918  1.42       apb  *	in the opposite direction, or if there is already an
    919  1.42       apb  *	ancestor/descendent relationship in the oposite direction, then
    920  1.42       apb  *	we avoid adding the new relationship, because that would cause a
    921  1.42       apb  *	cycle in the graph.
    922  1.42       apb  *
    923  1.42       apb  * Input:
    924  1.42       apb  *	succgnp		Successor node.
    925  1.42       apb  *	predgnp		Predecessor node.
    926  1.42       apb  *
    927  1.42       apb  * Results:
    928  1.42       apb  *	Always returns 0, for the benefit of Lst_ForEach().
    929  1.42       apb  *
    930  1.42       apb  * Side Effects:
    931  1.42       apb  *	preds/successors information is modified for predgnp, succgnp,
    932  1.42       apb  *	and recursively for all children of succgnp.
    933  1.42       apb  *-----------------------------------------------------------------------
    934  1.42       apb  */
    935  1.42       apb static int
    936  1.42       apb TargPropagateRecpredChild(ClientData succgnp, ClientData predgnp)
    937  1.42       apb {
    938  1.42       apb     GNode	  *succgn = (GNode *)succgnp;
    939  1.42       apb     GNode	  *predgn = (GNode *)predgnp;
    940  1.42       apb     Boolean	  debugmore = FALSE;	/* how much debugging? */
    941  1.42       apb 
    942  1.42       apb     /* Ignore if succgn == predgn */
    943  1.42       apb     if (succgn == predgn) {
    944  1.42       apb 	if (DEBUG(TARG) && debugmore) {
    945  1.43       dsl 	    fprintf(debug_file, "# TargPropagateRecpredChild: not propagating %s - %s (identical)\n",
    946  1.42       apb 		    predgn->name, succgn->name);
    947  1.42       apb 	}
    948  1.42       apb 	return (0);
    949  1.42       apb     }
    950  1.42       apb     /* Pre-existing pred/successor relationship
    951  1.42       apb      * in the opposite direction takes precedence. */
    952  1.42       apb     if (Lst_Member(succgn->successors, predgn) != NILLNODE) {
    953  1.42       apb 	if (DEBUG(TARG) && debugmore) {
    954  1.43       dsl 	    fprintf(debug_file, "# TargPropagateRecpredChild: not propagating %s - %s (opposite)\n",
    955  1.42       apb 		    predgn->name, succgn->name);
    956  1.42       apb 	}
    957  1.42       apb 	return (0);
    958  1.42       apb     }
    959  1.42       apb     /* Pre-existing descendent/ancestor relationship in the opposite
    960  1.42       apb      * direction takes precedence. */
    961  1.42       apb     if (TargHasAncestor(succgn, predgn)) {
    962  1.42       apb 	if (DEBUG(TARG) && debugmore) {
    963  1.43       dsl 	    fprintf(debug_file, "# TargPropagateRecpredChild: not propagating %s - %s (ancestor)\n",
    964  1.42       apb 		    predgn->name, succgn->name);
    965  1.42       apb 	}
    966  1.42       apb 	return (0);
    967  1.42       apb     }
    968  1.42       apb     /* Note the new pred/successor relationship. */
    969  1.42       apb     if (DEBUG(TARG)) {
    970  1.43       dsl 	fprintf(debug_file, "# TargPropagateRecpredChild: propagating %s - %s\n",
    971  1.42       apb 		predgn->name, succgn->name);
    972  1.42       apb     }
    973  1.42       apb     if (Lst_Member(succgn->preds, predgn) == NILLNODE) {
    974  1.42       apb 	(void)Lst_AtEnd(succgn->preds, (ClientData)predgn);
    975  1.42       apb 	(void)Lst_AtEnd(predgn->successors, (ClientData)succgn);
    976  1.42       apb     }
    977  1.42       apb     /* Recurse, provided there's not a cycle. */
    978  1.42       apb     if (! TargHasAncestor(succgn, succgn)) {
    979  1.42       apb 	Lst_ForEach(succgn->children, TargPropagateRecpredChild, predgn);
    980  1.42       apb     }
    981  1.42       apb     return (0);
    982  1.42       apb }
    983  1.42       apb 
    984  1.42       apb /*-
    985  1.42       apb  *-----------------------------------------------------------------------
    986  1.42       apb  * TargPropagateRecpred --
    987  1.42       apb  *	Recursively propagate information about a single predecessor
    988  1.42       apb  *	node, from a single successor node to all children of the
    989  1.42       apb  *	successor node.
    990  1.42       apb  *
    991  1.42       apb  * Input:
    992  1.42       apb  *	predgnp		Predecessor node.
    993  1.42       apb  *	succgnp		Successor node.
    994  1.42       apb  *
    995  1.42       apb  * Results:
    996  1.42       apb  *	Always returns 0, for the benefit of Lst_ForEach().
    997  1.42       apb  *
    998  1.42       apb  * Side Effects:
    999  1.42       apb  *	preds/successors information is modified for predgnp, succgnp,
   1000  1.42       apb  *	and recursively for all children of succgnp.
   1001  1.42       apb  *
   1002  1.42       apb  *	The real work is done by TargPropagateRecpredChild(), which
   1003  1.42       apb  *	will be called for each child of the successor node.
   1004  1.42       apb  *-----------------------------------------------------------------------
   1005  1.42       apb  */
   1006  1.42       apb static int
   1007  1.42       apb TargPropagateRecpred(ClientData predgnp, ClientData succgnp)
   1008  1.19   mycroft {
   1009  1.42       apb     GNode	  *predgn = (GNode *)predgnp;
   1010  1.42       apb     GNode	  *succgn = (GNode *)succgnp;
   1011  1.19   mycroft 
   1012  1.42       apb     Lst_ForEach(succgn->children, TargPropagateRecpredChild, predgn);
   1013  1.19   mycroft     return (0);
   1014  1.19   mycroft }
   1015  1.19   mycroft 
   1016  1.42       apb /*-
   1017  1.42       apb  *-----------------------------------------------------------------------
   1018  1.42       apb  * TargPropagateNode --
   1019  1.42       apb  *	Propagate information from a single node to related nodes if
   1020  1.42       apb  *	appropriate.
   1021  1.42       apb  *
   1022  1.42       apb  * Input:
   1023  1.42       apb  *	gnp		The node that we are processing.
   1024  1.42       apb  *
   1025  1.42       apb  * Results:
   1026  1.42       apb  *	Always returns 0, for the benefit of Lst_ForEach().
   1027  1.42       apb  *
   1028  1.42       apb  * Side Effects:
   1029  1.42       apb  *	Information is propagated from this node to cohort or child
   1030  1.42       apb  *	nodes.
   1031  1.42       apb  *
   1032  1.42       apb  *	If the node was defined with "::", then TargPropagateCohort()
   1033  1.42       apb  *	will be called for each cohort node.
   1034  1.42       apb  *
   1035  1.42       apb  *	If the node has recursive predecessors, then
   1036  1.42       apb  *	TargPropagateRecpred() will be called for each recursive
   1037  1.42       apb  *	predecessor.
   1038  1.42       apb  *-----------------------------------------------------------------------
   1039  1.42       apb  */
   1040  1.19   mycroft static int
   1041  1.33       jmc TargPropagateNode(ClientData gnp, ClientData junk __unused)
   1042  1.19   mycroft {
   1043  1.37  christos     GNode	  *gn = (GNode *)gnp;
   1044  1.20   mycroft     if (gn->type & OP_DOUBLEDEP)
   1045  1.34  christos 	Lst_ForEach(gn->cohorts, TargPropagateCohort, gnp);
   1046  1.42       apb     Lst_ForEach(gn->recpreds, TargPropagateRecpred, gnp);
   1047  1.19   mycroft     return (0);
   1048  1.19   mycroft }
   1049  1.19   mycroft 
   1050  1.42       apb /*-
   1051  1.42       apb  *-----------------------------------------------------------------------
   1052  1.42       apb  * TargPropagateCohort --
   1053  1.42       apb  *	Propagate some bits in the type mask from a node to
   1054  1.42       apb  *	a related cohort node.
   1055  1.42       apb  *
   1056  1.42       apb  * Input:
   1057  1.42       apb  *	cnp		The node that we are processing.
   1058  1.42       apb  *	gnp		Another node that has cnp as a cohort.
   1059  1.42       apb  *
   1060  1.42       apb  * Results:
   1061  1.42       apb  *	Always returns 0, for the benefit of Lst_ForEach().
   1062  1.42       apb  *
   1063  1.42       apb  * Side Effects:
   1064  1.42       apb  *	cnp's type bitmask is modified to incorporate some of the
   1065  1.42       apb  *	bits from gnp's type bitmask.  (XXX need a better explanation.)
   1066  1.42       apb  *-----------------------------------------------------------------------
   1067  1.42       apb  */
   1068  1.42       apb static int
   1069  1.42       apb TargPropagateCohort(ClientData cgnp, ClientData pgnp)
   1070  1.42       apb {
   1071  1.42       apb     GNode	  *cgn = (GNode *)cgnp;
   1072  1.42       apb     GNode	  *pgn = (GNode *)pgnp;
   1073  1.42       apb 
   1074  1.42       apb     cgn->type |= pgn->type & ~OP_OPMASK;
   1075  1.42       apb     return (0);
   1076  1.42       apb }
   1077  1.42       apb 
   1078  1.42       apb /*-
   1079  1.42       apb  *-----------------------------------------------------------------------
   1080  1.42       apb  * Targ_Propagate --
   1081  1.42       apb  *	Propagate information between related nodes.  Should be called
   1082  1.42       apb  *	after the makefiles are parsed but before any action is taken.
   1083  1.42       apb  *
   1084  1.42       apb  * Results:
   1085  1.42       apb  *	none
   1086  1.42       apb  *
   1087  1.42       apb  * Side Effects:
   1088  1.42       apb  *	Information is propagated between related nodes throughout the
   1089  1.42       apb  *	graph.
   1090  1.42       apb  *-----------------------------------------------------------------------
   1091  1.42       apb  */
   1092  1.19   mycroft void
   1093  1.28       wiz Targ_Propagate(void)
   1094  1.19   mycroft {
   1095  1.34  christos     Lst_ForEach(allTargets, TargPropagateNode, (ClientData)0);
   1096   1.1       cgd }
   1097