Home | History | Annotate | Line # | Download | only in make
suff.c revision 1.194
      1 /*	$NetBSD: suff.c,v 1.194 2020/10/19 21:38:10 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1989 by Berkeley Softworks
     37  * All rights reserved.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * Adam de Boor.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  */
     70 
     71 /*-
     72  * suff.c --
     73  *	Functions to maintain suffix lists and find implicit dependents
     74  *	using suffix transformation rules
     75  *
     76  * Interface:
     77  *	Suff_Init	Initialize all things to do with suffixes.
     78  *
     79  *	Suff_End	Clean up the module
     80  *
     81  *	Suff_DoPaths	This function is used to make life easier
     82  *			when searching for a file according to its
     83  *			suffix. It takes the global search path,
     84  *			as defined using the .PATH: target, and appends
     85  *			its directories to the path of each of the
     86  *			defined suffixes, as specified using
     87  *			.PATH<suffix>: targets. In addition, all
     88  *			directories given for suffixes labeled as
     89  *			include files or libraries, using the .INCLUDES
     90  *			or .LIBS targets, are played with using
     91  *			Dir_MakeFlags to create the .INCLUDES and
     92  *			.LIBS global variables.
     93  *
     94  *	Suff_ClearSuffixes
     95  *			Clear out all the suffixes and defined
     96  *			transformations.
     97  *
     98  *	Suff_IsTransform
     99  *			Return TRUE if the passed string is the lhs
    100  *			of a transformation rule.
    101  *
    102  *	Suff_AddSuffix	Add the passed string as another known suffix.
    103  *
    104  *	Suff_GetPath	Return the search path for the given suffix.
    105  *
    106  *	Suff_AddInclude
    107  *			Mark the given suffix as denoting an include file.
    108  *
    109  *	Suff_AddLib	Mark the given suffix as denoting a library.
    110  *
    111  *	Suff_AddTransform
    112  *			Add another transformation to the suffix
    113  *			graph. Returns  GNode suitable for framing, I
    114  *			mean, tacking commands, attributes, etc. on.
    115  *
    116  *	Suff_SetNull	Define the suffix to consider the suffix of
    117  *			any file that doesn't have a known one.
    118  *
    119  *	Suff_FindDeps	Find implicit sources for and the location of
    120  *			a target based on its suffix. Returns the
    121  *			bottom-most node added to the graph or NULL
    122  *			if the target had no implicit sources.
    123  *
    124  *	Suff_FindPath	Return the appropriate path to search in order to
    125  *			find the node.
    126  */
    127 
    128 #include "make.h"
    129 #include "dir.h"
    130 
    131 /*	"@(#)suff.c	8.4 (Berkeley) 3/21/94"	*/
    132 MAKE_RCSID("$NetBSD: suff.c,v 1.194 2020/10/19 21:38:10 rillig Exp $");
    133 
    134 #define SUFF_DEBUG0(text) DEBUG0(SUFF, text)
    135 #define SUFF_DEBUG1(fmt, arg1) DEBUG1(SUFF, fmt, arg1)
    136 #define SUFF_DEBUG2(fmt, arg1, arg2) DEBUG2(SUFF, fmt, arg1, arg2)
    137 #define SUFF_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(SUFF, fmt, arg1, arg2, arg3)
    138 
    139 typedef List SuffList;
    140 typedef ListNode SuffListNode;
    141 
    142 typedef List SrcList;
    143 typedef ListNode SrcListNode;
    144 
    145 static SuffList *sufflist;	/* List of suffixes */
    146 #ifdef CLEANUP
    147 static SuffList *suffClean;	/* List of suffixes to be cleaned */
    148 #endif
    149 static SrcList *srclist;	/* List of sources */
    150 static GNodeList *transforms;	/* List of transformation rules */
    151 
    152 static int        sNum = 0;	/* Counter for assigning suffix numbers */
    153 
    154 typedef enum SuffFlags {
    155     SUFF_INCLUDE	= 0x01,	/* One which is #include'd */
    156     SUFF_LIBRARY	= 0x02,	/* One which contains a library */
    157     SUFF_NULL		= 0x04	/* The empty suffix */
    158     /* XXX: Why is SUFF_NULL needed? Wouldn't nameLen == 0 mean the same? */
    159 } SuffFlags;
    160 
    161 ENUM_FLAGS_RTTI_3(SuffFlags,
    162 		  SUFF_INCLUDE, SUFF_LIBRARY, SUFF_NULL);
    163 
    164 typedef List SuffListList;
    165 
    166 /*
    167  * Structure describing an individual suffix.
    168  */
    169 typedef struct Suff {
    170     char         *name;		/* The suffix itself, such as ".c" */
    171     size_t	 nameLen;	/* Length of the name, to avoid strlen calls */
    172     SuffFlags	 flags;		/* Type of suffix */
    173     SearchPath	 *searchPath;	/* The path along which files of this suffix
    174 				 * may be found */
    175     int          sNum;		/* The suffix number */
    176     int		 refCount;	/* Reference count of list membership */
    177     SuffList	 *parents;	/* Suffixes we have a transformation to */
    178     SuffList	 *children;	/* Suffixes we have a transformation from */
    179     SuffListList *ref;		/* Lists in which this suffix is referenced */
    180 } Suff;
    181 
    182 /*
    183  * Structure used in the search for implied sources.
    184  */
    185 typedef struct Src {
    186     char *file;			/* The file to look for */
    187     char *pref;			/* Prefix from which file was formed */
    188     Suff *suff;			/* The suffix on the file */
    189     struct Src *parent;		/* The Src for which this is a source */
    190     GNode *node;		/* The node describing the file */
    191     int children;		/* Count of existing children (so we don't free
    192 				 * this thing too early or never nuke it) */
    193 #ifdef DEBUG_SRC
    194     SrcList *cp;		/* Debug; children list */
    195 #endif
    196 } Src;
    197 
    198 static Suff *suffNull;		/* The NULL suffix for this run */
    199 static Suff *emptySuff;		/* The empty suffix required for POSIX
    200 				 * single-suffix transformation rules */
    201 
    202 
    203 static void SuffFindDeps(GNode *, SrcList *);
    204 static void SuffExpandWildcards(GNodeListNode *, GNode *);
    205 
    206 	/*************** Lst Predicates ****************/
    207 /*-
    208  *-----------------------------------------------------------------------
    209  * SuffStrIsPrefix  --
    210  *	See if pref is a prefix of str.
    211  *
    212  * Input:
    213  *	pref		possible prefix
    214  *	str		string to check
    215  *
    216  * Results:
    217  *	NULL if it ain't, pointer to character in str after prefix if so
    218  *
    219  * Side Effects:
    220  *	None
    221  *-----------------------------------------------------------------------
    222  */
    223 static const char *
    224 SuffStrIsPrefix(const char *pref, const char *str)
    225 {
    226     while (*str && *pref == *str) {
    227 	pref++;
    228 	str++;
    229     }
    230 
    231     return *pref ? NULL : str;
    232 }
    233 
    234 struct SuffSuffGetSuffixArgs {
    235     size_t name_len;
    236     char *name_end;
    237 };
    238 
    239 /* See if suff is a suffix of str. str->ename should point to THE END
    240  * of the string to check. (THE END == the null byte)
    241  *
    242  * Input:
    243  *	s		possible suffix
    244  *	str		string to examine
    245  *
    246  * Results:
    247  *	NULL if it ain't, pointer to character in str before suffix if
    248  *	it is.
    249  */
    250 static char *
    251 SuffSuffGetSuffix(const Suff *s, const struct SuffSuffGetSuffixArgs *str)
    252 {
    253     char *p1;			/* Pointer into suffix name */
    254     char *p2;			/* Pointer into string being examined */
    255 
    256     if (str->name_len < s->nameLen)
    257 	return NULL;		/* this string is shorter than the suffix */
    258 
    259     p1 = s->name + s->nameLen;
    260     p2 = str->name_end;
    261 
    262     while (p1 >= s->name && *p1 == *p2) {
    263 	p1--;
    264 	p2--;
    265     }
    266 
    267     return p1 == s->name - 1 ? p2 : NULL;
    268 }
    269 
    270 /* Predicate form of SuffSuffGetSuffix, for Lst_Find. */
    271 static Boolean
    272 SuffSuffIsSuffix(const void *s, const void *sd)
    273 {
    274     return SuffSuffGetSuffix(s, sd) != NULL;
    275 }
    276 
    277 static Suff *
    278 FindSuffByName(const char *name)
    279 {
    280     SuffListNode *ln;
    281 
    282     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
    283 	Suff *suff = ln->datum;
    284 	if (strcmp(suff->name, name) == 0)
    285 	    return suff;
    286     }
    287     return NULL;
    288 }
    289 
    290 /* See if the suffix name is a prefix of the string. Care must be taken when
    291  * using this to search for transformations and what-not, since there could
    292  * well be two suffixes, one of which is a prefix of the other... */
    293 static Boolean
    294 SuffSuffIsPrefix(const void *s, const void *str)
    295 {
    296     return SuffStrIsPrefix(((const Suff *)s)->name, str) != NULL;
    297 }
    298 
    299 /* See if the graph node has the desired name. */
    300 static Boolean
    301 SuffGNHasName(const void *gn, const void *desiredName)
    302 {
    303     return strcmp(((const GNode *)gn)->name, desiredName) == 0;
    304 }
    305 
    306 	    /*********** Maintenance Functions ************/
    307 
    308 static void
    309 SuffUnRef(void *lp, void *sp)
    310 {
    311     SuffList *l = lp;
    312 
    313     SuffListNode *ln = Lst_FindDatum(l, sp);
    314     if (ln != NULL) {
    315 	Lst_Remove(l, ln);
    316 	((Suff *)sp)->refCount--;
    317     }
    318 }
    319 
    320 /* Free up all memory associated with the given suffix structure. */
    321 static void
    322 SuffFree(void *sp)
    323 {
    324     Suff *s = (Suff *)sp;
    325 
    326     if (s == suffNull)
    327 	suffNull = NULL;
    328 
    329     if (s == emptySuff)
    330 	emptySuff = NULL;
    331 
    332 #ifdef notdef
    333     /* We don't delete suffixes in order, so we cannot use this */
    334     if (s->refCount)
    335 	Punt("Internal error deleting suffix `%s' with refcount = %d", s->name,
    336 	    s->refCount);
    337 #endif
    338 
    339     Lst_Free(s->ref);
    340     Lst_Free(s->children);
    341     Lst_Free(s->parents);
    342     Lst_Destroy(s->searchPath, Dir_Destroy);
    343 
    344     free(s->name);
    345     free(s);
    346 }
    347 
    348 /* Remove the suffix from the list, and free if it is otherwise unused. */
    349 static void
    350 SuffRemove(SuffList *l, Suff *s)
    351 {
    352     SuffUnRef(l, s);
    353     if (s->refCount == 0) {
    354 	SuffUnRef(sufflist, s);
    355 	SuffFree(s);
    356     }
    357 }
    358 
    359 /* Insert the suffix into the list keeping the list ordered by suffix numbers.
    360  *
    361  * Input:
    362  *	l		the list where in s should be inserted
    363  *	s		the suffix to insert
    364  */
    365 static void
    366 SuffInsert(SuffList *l, Suff *s)
    367 {
    368     SuffListNode *ln;		/* current element in l we're examining */
    369     Suff          *s2 = NULL;	/* the suffix descriptor in this element */
    370 
    371     for (ln = l->first; ln != NULL; ln = ln->next) {
    372 	s2 = LstNode_Datum(ln);
    373 	if (s2->sNum >= s->sNum) {
    374 	    break;
    375 	}
    376     }
    377 
    378     SUFF_DEBUG2("inserting %s(%d)...", s->name, s->sNum);
    379 
    380     if (ln == NULL) {
    381 	SUFF_DEBUG0("at end of list\n");
    382 	Lst_Append(l, s);
    383 	s->refCount++;
    384 	Lst_Append(s->ref, l);
    385     } else if (s2->sNum != s->sNum) {
    386 	SUFF_DEBUG2("before %s(%d)\n", s2->name, s2->sNum);
    387 	Lst_InsertBefore(l, ln, s);
    388 	s->refCount++;
    389 	Lst_Append(s->ref, l);
    390     } else {
    391 	SUFF_DEBUG0("already there\n");
    392     }
    393 }
    394 
    395 static Suff *
    396 SuffNew(const char *name)
    397 {
    398     Suff *s = bmake_malloc(sizeof(Suff));
    399 
    400     s->name = bmake_strdup(name);
    401     s->nameLen = strlen(s->name);
    402     s->searchPath = Lst_New();
    403     s->children = Lst_New();
    404     s->parents = Lst_New();
    405     s->ref = Lst_New();
    406     s->sNum = sNum++;
    407     s->flags = 0;
    408     s->refCount = 1;
    409 
    410     return s;
    411 }
    412 
    413 /* This is gross. Nuke the list of suffixes but keep all transformation
    414  * rules around. The transformation graph is destroyed in this process, but
    415  * we leave the list of rules so when a new graph is formed the rules will
    416  * remain. This function is called from the parse module when a .SUFFIXES:\n
    417  * line is encountered. */
    418 void
    419 Suff_ClearSuffixes(void)
    420 {
    421 #ifdef CLEANUP
    422     Lst_MoveAll(suffClean, sufflist);
    423 #endif
    424     sufflist = Lst_New();
    425     sNum = 0;
    426     if (suffNull)
    427 	SuffFree(suffNull);
    428     emptySuff = suffNull = SuffNew("");
    429 
    430     Dir_Concat(suffNull->searchPath, dirSearchPath);
    431     suffNull->flags = SUFF_NULL;
    432 }
    433 
    434 /* Parse a transformation string to find its two component suffixes.
    435  *
    436  * Input:
    437  *	str		String being parsed
    438  *	out_src		Place to store source of trans.
    439  *	out_targ	Place to store target of trans.
    440  *
    441  * Results:
    442  *	TRUE if the string is a valid transformation, FALSE otherwise.
    443  */
    444 static Boolean
    445 SuffParseTransform(const char *str, Suff **out_src, Suff **out_targ)
    446 {
    447     SuffListNode *srcLn;	/* element in suffix list of trans source*/
    448     Suff *src;			/* Source of transformation */
    449     const char *str2;		/* Extra pointer (maybe target suffix) */
    450     SuffListNode *singleLn;	/* element in suffix list of any suffix
    451 				 * that exactly matches str */
    452     Suff *single = NULL;	/* Source of possible transformation to
    453 				 * null suffix */
    454 
    455     srcLn = NULL;
    456     singleLn = NULL;
    457 
    458     /*
    459      * Loop looking first for a suffix that matches the start of the
    460      * string and then for one that exactly matches the rest of it. If
    461      * we can find two that meet these criteria, we've successfully
    462      * parsed the string.
    463      */
    464     for (;;) {
    465 	if (srcLn == NULL) {
    466 	    srcLn = Lst_Find(sufflist, SuffSuffIsPrefix, str);
    467 	} else {
    468 	    srcLn = Lst_FindFrom(sufflist, srcLn->next, SuffSuffIsPrefix, str);
    469 	}
    470 	if (srcLn == NULL) {
    471 	    /*
    472 	     * Ran out of source suffixes -- no such rule
    473 	     */
    474 	    if (singleLn != NULL) {
    475 		/*
    476 		 * Not so fast Mr. Smith! There was a suffix that encompassed
    477 		 * the entire string, so we assume it was a transformation
    478 		 * to the null suffix (thank you POSIX). We still prefer to
    479 		 * find a double rule over a singleton, hence we leave this
    480 		 * check until the end.
    481 		 *
    482 		 * XXX: Use emptySuff over suffNull?
    483 		 */
    484 		*out_src = single;
    485 		*out_targ = suffNull;
    486 		return TRUE;
    487 	    }
    488 	    return FALSE;
    489 	}
    490 	src = LstNode_Datum(srcLn);
    491 	str2 = str + src->nameLen;
    492 	if (*str2 == '\0') {
    493 	    single = src;
    494 	    singleLn = srcLn;
    495 	} else {
    496 	    Suff *targ = FindSuffByName(str2);
    497 	    if (targ != NULL) {
    498 		*out_src = src;
    499 		*out_targ = targ;
    500 		return TRUE;
    501 	    }
    502 	}
    503     }
    504 }
    505 
    506 /* Return TRUE if the given string is a transformation rule, that is, a
    507  * concatenation of two known suffixes. */
    508 Boolean
    509 Suff_IsTransform(const char *str)
    510 {
    511     Suff *src, *targ;
    512 
    513     return SuffParseTransform(str, &src, &targ);
    514 }
    515 
    516 /* Add the transformation rule described by the line to the list of rules
    517  * and place the transformation itself in the graph.
    518  *
    519  * The node is placed on the end of the transforms Lst and links are made
    520  * between the two suffixes mentioned in the target name.
    521 
    522  * Input:
    523  *	line		name of transformation to add
    524  *
    525  * Results:
    526  *	The node created for the transformation in the transforms list
    527  */
    528 GNode *
    529 Suff_AddTransform(const char *line)
    530 {
    531     GNode         *gn;		/* GNode of transformation rule */
    532     Suff          *s,		/* source suffix */
    533 		  *t;		/* target suffix */
    534     GNodeListNode *ln;		/* Node for existing transformation */
    535     Boolean ok;
    536 
    537     ln = Lst_Find(transforms, SuffGNHasName, line);
    538     if (ln == NULL) {
    539 	/*
    540 	 * Make a new graph node for the transformation. It will be filled in
    541 	 * by the Parse module.
    542 	 */
    543 	gn = Targ_NewGN(line);
    544 	Lst_Append(transforms, gn);
    545     } else {
    546 	/*
    547 	 * New specification for transformation rule. Just nuke the old list
    548 	 * of commands so they can be filled in again... We don't actually
    549 	 * free the commands themselves, because a given command can be
    550 	 * attached to several different transformations.
    551 	 */
    552 	gn = LstNode_Datum(ln);
    553 	Lst_Free(gn->commands);
    554 	Lst_Free(gn->children);
    555 	gn->commands = Lst_New();
    556 	gn->children = Lst_New();
    557     }
    558 
    559     gn->type = OP_TRANSFORM;
    560 
    561     ok = SuffParseTransform(line, &s, &t);
    562     assert(ok);
    563     (void)ok;
    564 
    565     /*
    566      * link the two together in the proper relationship and order
    567      */
    568     SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
    569 		s->name, t->name);
    570     SuffInsert(t->children, s);
    571     SuffInsert(s->parents, t);
    572 
    573     return gn;
    574 }
    575 
    576 /* Handle the finish of a transformation definition, removing the
    577  * transformation from the graph if it has neither commands nor sources.
    578  *
    579  * If the node has no commands or children, the children and parents lists
    580  * of the affected suffixes are altered.
    581  *
    582  * Input:
    583  *	gn		Node for transformation
    584  */
    585 void
    586 Suff_EndTransform(GNode *gn)
    587 {
    588     if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(gn->cohorts))
    589 	gn = LstNode_Datum(Lst_Last(gn->cohorts));
    590     if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
    591 	Lst_IsEmpty(gn->children))
    592     {
    593 	Suff	*s, *t;
    594 
    595 	/*
    596 	 * SuffParseTransform() may fail for special rules which are not
    597 	 * actual transformation rules. (e.g. .DEFAULT)
    598 	 */
    599 	if (SuffParseTransform(gn->name, &s, &t)) {
    600 	    SuffList *p;
    601 
    602 	    SUFF_DEBUG2("deleting transformation from `%s' to `%s'\n",
    603 			s->name, t->name);
    604 
    605 	    /*
    606 	     * Store s->parents because s could be deleted in SuffRemove
    607 	     */
    608 	    p = s->parents;
    609 
    610 	    /*
    611 	     * Remove the source from the target's children list. We check for a
    612 	     * nil return to handle a beanhead saying something like
    613 	     *  .c.o .c.o:
    614 	     *
    615 	     * We'll be called twice when the next target is seen, but .c and .o
    616 	     * are only linked once...
    617 	     */
    618 	    SuffRemove(t->children, s);
    619 
    620 	    /*
    621 	     * Remove the target from the source's parents list
    622 	     */
    623 	    SuffRemove(p, t);
    624 	}
    625     } else if (gn->type & OP_TRANSFORM) {
    626 	SUFF_DEBUG1("transformation %s complete\n", gn->name);
    627     }
    628 }
    629 
    630 /* Called from Suff_AddSuffix via Lst_ForEachUntil to search through the list of
    631  * existing transformation rules and rebuild the transformation graph when
    632  * it has been destroyed by Suff_ClearSuffixes. If the given rule is a
    633  * transformation involving this suffix and another, existing suffix, the
    634  * proper relationship is established between the two.
    635  *
    636  * The appropriate links will be made between this suffix and others if
    637  * transformation rules exist for it.
    638  *
    639  * Input:
    640  *	transform	Transformation to test
    641  *	s		Suffix to rebuild
    642  */
    643 static void
    644 SuffRebuildGraph(GNode *transform, Suff *s)
    645 {
    646     char *cp;
    647     struct SuffSuffGetSuffixArgs sd;
    648 
    649     /*
    650      * First see if it is a transformation from this suffix.
    651      */
    652     cp = UNCONST(SuffStrIsPrefix(s->name, transform->name));
    653     if (cp != NULL) {
    654 	Suff *s2 = FindSuffByName(cp);
    655 	if (s2 != NULL) {
    656 	    /* Link in and return, since it can't be anything else. */
    657 	    SuffInsert(s2->children, s);
    658 	    SuffInsert(s->parents, s2);
    659 	    return;
    660 	}
    661     }
    662 
    663     /*
    664      * Not from, maybe to?
    665      */
    666     sd.name_len = strlen(transform->name);
    667     sd.name_end = transform->name + sd.name_len;
    668     cp = SuffSuffGetSuffix(s, &sd);
    669     if (cp != NULL) {
    670 	Suff *s2;
    671 
    672 	/* Null-terminate the source suffix in order to find it. */
    673 	/* XXX: don't modify strings, not even temporarily */
    674 	cp[1] = '\0';
    675 	s2 = FindSuffByName(transform->name);
    676 	cp[1] = s->name[0];		/* restore */
    677 
    678 	if (s2 != NULL) {
    679 	    /* establish the proper relationship */
    680 	    SuffInsert(s->children, s2);
    681 	    SuffInsert(s2->parents, s);
    682 	}
    683     }
    684 }
    685 
    686 /* During Suff_AddSuffix, search through the list of existing targets and find
    687  * if any of the existing targets can be turned into a transformation rule.
    688  *
    689  * If such a target is found and the target is the current main target, the
    690  * main target is set to NULL and the next target examined (if that exists)
    691  * becomes the main target.
    692  *
    693  * Results:
    694  *	TRUE iff a new main target has been selected.
    695  */
    696 static Boolean
    697 SuffScanTargets(GNode *target, GNode **inout_main, Suff *gs_s, Boolean *gs_r)
    698 {
    699     Suff *s, *t;
    700     char *ptr;
    701 
    702     if (*inout_main == NULL && *gs_r && !(target->type & OP_NOTARGET)) {
    703 	*inout_main = target;
    704 	Targ_SetMain(target);
    705 	return TRUE;
    706     }
    707 
    708     if (target->type == OP_TRANSFORM)
    709 	return FALSE;
    710 
    711     if ((ptr = strstr(target->name, gs_s->name)) == NULL ||
    712 	ptr == target->name)
    713 	return FALSE;
    714 
    715     if (SuffParseTransform(target->name, &s, &t)) {
    716 	if (*inout_main == target) {
    717 	    *gs_r = TRUE;
    718 	    *inout_main = NULL;
    719 	    Targ_SetMain(NULL);
    720 	}
    721 	Lst_Free(target->children);
    722 	target->children = Lst_New();
    723 	target->type = OP_TRANSFORM;
    724 	/*
    725 	 * link the two together in the proper relationship and order
    726 	 */
    727 	SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
    728 		    s->name, t->name);
    729 	SuffInsert(t->children, s);
    730 	SuffInsert(s->parents, t);
    731     }
    732     return FALSE;
    733 }
    734 
    735 /* Look at all existing targets to see if adding this suffix will make one
    736  * of the current targets mutate into a suffix rule.
    737  *
    738  * This is ugly, but other makes treat all targets that start with a '.' as
    739  * suffix rules. */
    740 static void
    741 UpdateTargets(GNode **inout_main, Suff *s)
    742 {
    743     Boolean r = FALSE;
    744     GNodeListNode *ln;
    745     for (ln = Targ_List()->first; ln != NULL; ln = ln->next) {
    746 	GNode *gn = ln->datum;
    747 	if (SuffScanTargets(gn, inout_main, s, &r))
    748 	    break;
    749     }
    750 }
    751 
    752 /* Add the suffix to the end of the list of known suffixes.
    753  * Should we restructure the suffix graph? Make doesn't...
    754  *
    755  * A GNode is created for the suffix and a Suff structure is created and
    756  * added to the suffixes list unless the suffix was already known.
    757  * The mainNode passed can be modified if a target mutated into a
    758  * transform and that target happened to be the main target.
    759  *
    760  * Input:
    761  *	name		the name of the suffix to add
    762  */
    763 void
    764 Suff_AddSuffix(const char *name, GNode **inout_main)
    765 {
    766     GNodeListNode *ln;
    767 
    768     Suff *s = FindSuffByName(name);
    769     if (s != NULL)
    770 	return;
    771 
    772     s = SuffNew(name);
    773     Lst_Append(sufflist, s);
    774 
    775     UpdateTargets(inout_main, s);
    776 
    777     /*
    778      * Look for any existing transformations from or to this suffix.
    779      * XXX: Only do this after a Suff_ClearSuffixes?
    780      */
    781     for (ln = transforms->first; ln != NULL; ln = ln->next)
    782 	SuffRebuildGraph(ln->datum, s);
    783 }
    784 
    785 /* Return the search path for the given suffix, or NULL. */
    786 SearchPath *
    787 Suff_GetPath(const char *sname)
    788 {
    789     Suff *s = FindSuffByName(sname);
    790     return s != NULL ? s->searchPath : NULL;
    791 }
    792 
    793 /* Extend the search paths for all suffixes to include the default search
    794  * path.
    795  *
    796  * The searchPath field of all the suffixes is extended by the directories
    797  * in dirSearchPath. If paths were specified for the ".h" suffix, the
    798  * directories are stuffed into a global variable called ".INCLUDES" with
    799  * each directory preceded by a -I. The same is done for the ".a" suffix,
    800  * except the variable is called ".LIBS" and the flag is -L.
    801  */
    802 void
    803 Suff_DoPaths(void)
    804 {
    805     SuffListNode *ln;
    806     char *ptr;
    807     SearchPath *inIncludes; /* Cumulative .INCLUDES path */
    808     SearchPath *inLibs;	    /* Cumulative .LIBS path */
    809 
    810     inIncludes = Lst_New();
    811     inLibs = Lst_New();
    812 
    813     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
    814 	Suff *s = LstNode_Datum(ln);
    815 	if (!Lst_IsEmpty(s->searchPath)) {
    816 #ifdef INCLUDES
    817 	    if (s->flags & SUFF_INCLUDE) {
    818 		Dir_Concat(inIncludes, s->searchPath);
    819 	    }
    820 #endif
    821 #ifdef LIBRARIES
    822 	    if (s->flags & SUFF_LIBRARY) {
    823 		Dir_Concat(inLibs, s->searchPath);
    824 	    }
    825 #endif
    826 	    Dir_Concat(s->searchPath, dirSearchPath);
    827 	} else {
    828 	    Lst_Destroy(s->searchPath, Dir_Destroy);
    829 	    s->searchPath = Lst_Copy(dirSearchPath, Dir_CopyDir);
    830 	}
    831     }
    832 
    833     Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
    834     free(ptr);
    835     Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
    836     free(ptr);
    837 
    838     Lst_Destroy(inIncludes, Dir_Destroy);
    839     Lst_Destroy(inLibs, Dir_Destroy);
    840 }
    841 
    842 /* Add the given suffix as a type of file which gets included.
    843  * Called from the parse module when a .INCLUDES line is parsed.
    844  * The suffix must have already been defined.
    845  * The SUFF_INCLUDE bit is set in the suffix's flags field.
    846  *
    847  * Input:
    848  *	sname		Name of the suffix to mark
    849  */
    850 void
    851 Suff_AddInclude(const char *sname)
    852 {
    853     Suff *suff = FindSuffByName(sname);
    854     if (suff != NULL)
    855 	suff->flags |= SUFF_INCLUDE;
    856 }
    857 
    858 /* Add the given suffix as a type of file which is a library.
    859  * Called from the parse module when parsing a .LIBS line.
    860  * The suffix must have been defined via .SUFFIXES before this is called.
    861  * The SUFF_LIBRARY bit is set in the suffix's flags field.
    862  *
    863  * Input:
    864  *	sname		Name of the suffix to mark
    865  */
    866 void
    867 Suff_AddLib(const char *sname)
    868 {
    869     Suff *suff = FindSuffByName(sname);
    870     if (suff != NULL)
    871 	suff->flags |= SUFF_LIBRARY;
    872 }
    873 
    874 	  /********** Implicit Source Search Functions *********/
    875 
    876 #ifdef DEBUG_SRC
    877 static void
    878 SrcList_PrintAddrs(SrcList *srcList)
    879 {
    880     SrcListNode *ln;
    881     for (ln = srcList->first; ln != NULL; ln = ln->next)
    882 	debug_printf(" %p", ln->datum);
    883 }
    884 #endif
    885 
    886 static void
    887 SuffAddSrc(Suff *suff, SrcList *srcList, Src *targ, char *srcName,
    888 	   const char *debug_tag)
    889 {
    890     Src *s2 = bmake_malloc(sizeof(Src));
    891     s2->file = srcName;
    892     s2->pref = targ->pref;
    893     s2->parent = targ;
    894     s2->node = NULL;
    895     s2->suff = suff;
    896     suff->refCount++;
    897     s2->children = 0;
    898     targ->children++;
    899     Lst_Append(srcList, s2);
    900 #ifdef DEBUG_SRC
    901     s2->cp = Lst_New();
    902     Lst_Append(targ->cp, s2);
    903     debug_printf("%s add %p %p to %p:", debug_tag, targ, s2, srcList);
    904     SrcList_PrintAddrs(srcList);
    905     debug_printf("\n");
    906 #endif
    907 }
    908 
    909 /* Add a suffix as a Src structure to the given list with its parent
    910  * being the given Src structure. If the suffix is the null suffix,
    911  * the prefix is used unaltered as the file name in the Src structure.
    912  *
    913  * Input:
    914  *	suff		suffix for which to create a Src structure
    915  *	srcList		list for the new Src
    916  *	targ		parent for the new Src
    917  */
    918 static void
    919 SuffAddSources(Suff *suff, SrcList *srcList, Src *targ)
    920 {
    921     if ((suff->flags & SUFF_NULL) && suff->name[0] != '\0') {
    922 	/*
    923 	 * If the suffix has been marked as the NULL suffix, also create a Src
    924 	 * structure for a file with no suffix attached. Two birds, and all
    925 	 * that...
    926 	 */
    927 	SuffAddSrc(suff, srcList, targ, bmake_strdup(targ->pref), "1");
    928     }
    929     SuffAddSrc(suff, srcList, targ, str_concat2(targ->pref, suff->name), "2");
    930 }
    931 
    932 /* Add all the children of targ as Src structures to the given list.
    933  *
    934  * Input:
    935  *	l		list to which to add the new level
    936  *	targ		Src structure to use as the parent
    937  */
    938 static void
    939 SuffAddLevel(SrcList *l, Src *targ)
    940 {
    941     SrcListNode *ln;
    942     for (ln = targ->suff->children->first; ln != NULL; ln = ln->next) {
    943 	Suff *childSuff = ln->datum;
    944 	SuffAddSources(childSuff, l, targ);
    945     }
    946 }
    947 
    948 /* Free the first Src in the list that doesn't have a reference count.
    949  * Return whether a Src was removed. */
    950 static Boolean
    951 SuffRemoveSrc(SrcList *l)
    952 {
    953     SrcListNode *ln;
    954 
    955     Lst_Open(l);
    956 
    957 #ifdef DEBUG_SRC
    958     debug_printf("cleaning %p:", l);
    959     SrcList_PrintAddrs(l);
    960     debug_printf("\n");
    961 #endif
    962 
    963     while ((ln = Lst_Next(l)) != NULL) {
    964 	Src *s = LstNode_Datum(ln);
    965 	if (s->children == 0) {
    966 	    free(s->file);
    967 	    if (!s->parent)
    968 		free(s->pref);
    969 	    else {
    970 #ifdef DEBUG_SRC
    971 		SrcListNode *ln2 = Lst_FindDatum(s->parent->cp, s);
    972 		if (ln2 != NULL)
    973 		    Lst_Remove(s->parent->cp, ln2);
    974 #endif
    975 		--s->parent->children;
    976 	    }
    977 #ifdef DEBUG_SRC
    978 	    debug_printf("free: [l=%p] p=%p %d\n", l, s, s->children);
    979 	    Lst_Free(s->cp);
    980 #endif
    981 	    Lst_Remove(l, ln);
    982 	    free(s);
    983 	    Lst_Close(l);
    984 	    return TRUE;
    985 	}
    986 #ifdef DEBUG_SRC
    987 	else {
    988 	    debug_printf("keep: [l=%p] p=%p %d:", l, s, s->children);
    989 	    SrcList_PrintAddrs(s->cp);
    990 	    debug_printf("\n");
    991 	}
    992 #endif
    993     }
    994 
    995     Lst_Close(l);
    996 
    997     return FALSE;
    998 }
    999 
   1000 /* Find the first existing file/target in the list srcs.
   1001  *
   1002  * Input:
   1003  *	srcs		list of Src structures to search through
   1004  *
   1005  * Results:
   1006  *	The lowest structure in the chain of transformations, or NULL.
   1007  */
   1008 static Src *
   1009 SuffFindThem(SrcList *srcs, SrcList *slst)
   1010 {
   1011     Src            *s;		/* current Src */
   1012     Src		   *rs;		/* returned Src */
   1013     char	   *ptr;
   1014 
   1015     rs = NULL;
   1016 
   1017     while (!Lst_IsEmpty(srcs)) {
   1018 	s = Lst_Dequeue(srcs);
   1019 
   1020 	SUFF_DEBUG1("\ttrying %s...", s->file);
   1021 
   1022 	/*
   1023 	 * A file is considered to exist if either a node exists in the
   1024 	 * graph for it or the file actually exists.
   1025 	 */
   1026 	if (Targ_FindNode(s->file) != NULL) {
   1027 #ifdef DEBUG_SRC
   1028 	    debug_printf("remove %p from %p\n", s, srcs);
   1029 #endif
   1030 	    rs = s;
   1031 	    break;
   1032 	}
   1033 
   1034 	if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) {
   1035 	    rs = s;
   1036 #ifdef DEBUG_SRC
   1037 	    debug_printf("remove %p from %p\n", s, srcs);
   1038 #endif
   1039 	    free(ptr);
   1040 	    break;
   1041 	}
   1042 
   1043 	SUFF_DEBUG0("not there\n");
   1044 
   1045 	SuffAddLevel(srcs, s);
   1046 	Lst_Append(slst, s);
   1047     }
   1048 
   1049     if (rs) {
   1050 	SUFF_DEBUG0("got it\n");
   1051     }
   1052     return rs;
   1053 }
   1054 
   1055 /* See if any of the children of the target in the Src structure is one from
   1056  * which the target can be transformed. If there is one, a Src structure is
   1057  * put together for it and returned.
   1058  *
   1059  * Input:
   1060  *	targ		Src to play with
   1061  *
   1062  * Results:
   1063  *	The Src of the "winning" child, or NULL.
   1064  */
   1065 static Src *
   1066 SuffFindCmds(Src *targ, SrcList *slst)
   1067 {
   1068     GNodeListNode *gln;
   1069     GNode *t;			/* Target GNode */
   1070     GNode *s;			/* Source GNode */
   1071     size_t prefLen;		/* The length of the defined prefix */
   1072     Suff *suff;			/* Suffix on matching beastie */
   1073     Src *ret;			/* Return value */
   1074     char *cp;
   1075 
   1076     t = targ->node;
   1077     Lst_Open(t->children);
   1078     prefLen = strlen(targ->pref);
   1079 
   1080     for (;;) {
   1081 	gln = Lst_Next(t->children);
   1082 	if (gln == NULL) {
   1083 	    Lst_Close(t->children);
   1084 	    return NULL;
   1085 	}
   1086 	s = LstNode_Datum(gln);
   1087 
   1088 	if (s->type & OP_OPTIONAL && Lst_IsEmpty(t->commands)) {
   1089 	    /*
   1090 	     * We haven't looked to see if .OPTIONAL files exist yet, so
   1091 	     * don't use one as the implicit source.
   1092 	     * This allows us to use .OPTIONAL in .depend files so make won't
   1093 	     * complain "don't know how to make xxx.h' when a dependent file
   1094 	     * has been moved/deleted.
   1095 	     */
   1096 	    continue;
   1097 	}
   1098 
   1099 	cp = strrchr(s->name, '/');
   1100 	if (cp == NULL) {
   1101 	    cp = s->name;
   1102 	} else {
   1103 	    cp++;
   1104 	}
   1105 	if (strncmp(cp, targ->pref, prefLen) != 0)
   1106 	    continue;
   1107 	/*
   1108 	 * The node matches the prefix ok, see if it has a known
   1109 	 * suffix.
   1110 	 */
   1111 	suff = FindSuffByName(cp + prefLen);
   1112 	if (suff == NULL)
   1113 	    continue;
   1114 
   1115 	/*
   1116 	 * It even has a known suffix, see if there's a transformation
   1117 	 * defined between the node's suffix and the target's suffix.
   1118 	 *
   1119 	 * XXX: Handle multi-stage transformations here, too.
   1120 	 */
   1121 
   1122 	/* XXX: Can targ->suff be NULL here? */
   1123 	if (targ->suff != NULL &&
   1124 	    Lst_FindDatum(suff->parents, targ->suff) != NULL)
   1125 	    break;
   1126     }
   1127 
   1128     /*
   1129      * Hot Damn! Create a new Src structure to describe
   1130      * this transformation (making sure to duplicate the
   1131      * source node's name so Suff_FindDeps can free it
   1132      * again (ick)), and return the new structure.
   1133      */
   1134     ret = bmake_malloc(sizeof(Src));
   1135     ret->file = bmake_strdup(s->name);
   1136     ret->pref = targ->pref;
   1137     ret->suff = suff;
   1138     suff->refCount++;
   1139     ret->parent = targ;
   1140     ret->node = s;
   1141     ret->children = 0;
   1142     targ->children++;
   1143 #ifdef DEBUG_SRC
   1144     ret->cp = Lst_New();
   1145     debug_printf("3 add %p %p\n", targ, ret);
   1146     Lst_Append(targ->cp, ret);
   1147 #endif
   1148     Lst_Append(slst, ret);
   1149     SUFF_DEBUG1("\tusing existing source %s\n", s->name);
   1150     Lst_Close(t->children);
   1151     return ret;
   1152 }
   1153 
   1154 /* Expand the names of any children of a given node that contain variable
   1155  * expressions or file wildcards into actual targets.
   1156  *
   1157  * The expanded node is removed from the parent's list of children, and the
   1158  * parent's unmade counter is decremented, but other nodes may be added.
   1159  *
   1160  * Input:
   1161  *	cln		Child to examine
   1162  *	pgn		Parent node being processed
   1163  */
   1164 static void
   1165 SuffExpandChildren(GNodeListNode *cln, GNode *pgn)
   1166 {
   1167     GNode *cgn = LstNode_Datum(cln);
   1168     GNode *gn;			/* New source 8) */
   1169     char *cp;			/* Expanded value */
   1170 
   1171     if (!Lst_IsEmpty(cgn->order_pred) || !Lst_IsEmpty(cgn->order_succ))
   1172 	/* It is all too hard to process the result of .ORDER */
   1173 	return;
   1174 
   1175     if (cgn->type & OP_WAIT)
   1176 	/* Ignore these (& OP_PHONY ?) */
   1177 	return;
   1178 
   1179     /*
   1180      * First do variable expansion -- this takes precedence over
   1181      * wildcard expansion. If the result contains wildcards, they'll be gotten
   1182      * to later since the resulting words are tacked on to the end of
   1183      * the children list.
   1184      */
   1185     if (strchr(cgn->name, '$') == NULL) {
   1186 	SuffExpandWildcards(cln, pgn);
   1187 	return;
   1188     }
   1189 
   1190     SUFF_DEBUG1("Expanding \"%s\"...", cgn->name);
   1191     (void)Var_Subst(cgn->name, pgn, VARE_UNDEFERR|VARE_WANTRES, &cp);
   1192     /* TODO: handle errors */
   1193 
   1194     {
   1195 	GNodeList *members = Lst_New();
   1196 
   1197 	if (cgn->type & OP_ARCHV) {
   1198 	    /*
   1199 	     * Node was an archive(member) target, so we want to call
   1200 	     * on the Arch module to find the nodes for us, expanding
   1201 	     * variables in the parent's context.
   1202 	     */
   1203 	    char	*sacrifice = cp;
   1204 
   1205 	    (void)Arch_ParseArchive(&sacrifice, members, pgn);
   1206 	} else {
   1207 	    /*
   1208 	     * Break the result into a vector of strings whose nodes
   1209 	     * we can find, then add those nodes to the members list.
   1210 	     * Unfortunately, we can't use brk_string b/c it
   1211 	     * doesn't understand about variable specifications with
   1212 	     * spaces in them...
   1213 	     */
   1214 	    char	    *start;
   1215 	    char	    *initcp = cp;   /* For freeing... */
   1216 
   1217 	    for (start = cp; *start == ' ' || *start == '\t'; start++)
   1218 		continue;
   1219 	    cp = start;
   1220 	    while (*cp != '\0') {
   1221 		if (*cp == ' ' || *cp == '\t') {
   1222 		    /*
   1223 		     * White-space -- terminate element, find the node,
   1224 		     * add it, skip any further spaces.
   1225 		     */
   1226 		    *cp++ = '\0';
   1227 		    gn = Targ_GetNode(start);
   1228 		    Lst_Append(members, gn);
   1229 		    while (*cp == ' ' || *cp == '\t') {
   1230 			cp++;
   1231 		    }
   1232 		    start = cp;		/* Continue at the next non-space. */
   1233 		} else if (*cp == '$') {
   1234 		    /*
   1235 		     * Start of a variable spec -- contact variable module
   1236 		     * to find the end so we can skip over it.
   1237 		     */
   1238 		    const char *nested_p = cp;
   1239 		    const char	*junk;
   1240 		    void	*freeIt;
   1241 
   1242 		    /* XXX: Why VARE_WANTRES when the result is not used? */
   1243 		    (void)Var_Parse(&nested_p, pgn,
   1244 				    VARE_UNDEFERR|VARE_WANTRES,
   1245 				    &junk, &freeIt);
   1246 		    /* TODO: handle errors */
   1247 		    if (junk == var_Error) {
   1248 			Parse_Error(PARSE_FATAL,
   1249 				    "Malformed variable expression at \"%s\"",
   1250 				    cp);
   1251 			cp++;
   1252 		    } else {
   1253 			cp += nested_p - cp;
   1254 		    }
   1255 
   1256 		    free(freeIt);
   1257 		} else if (*cp == '\\' && cp[1] != '\0') {
   1258 		    /*
   1259 		     * Escaped something -- skip over it
   1260 		     */
   1261 		    /* XXX: In other places, escaping at this syntactical
   1262 		     * position is done by a '$', not a '\'.  The '\' is only
   1263 		     * used in variable modifiers. */
   1264 		    cp += 2;
   1265 		} else {
   1266 		    cp++;
   1267 		}
   1268 	    }
   1269 
   1270 	    if (cp != start) {
   1271 		/*
   1272 		 * Stuff left over -- add it to the list too
   1273 		 */
   1274 		gn = Targ_GetNode(start);
   1275 		Lst_Append(members, gn);
   1276 	    }
   1277 	    /*
   1278 	     * Point cp back at the beginning again so the variable value
   1279 	     * can be freed.
   1280 	     */
   1281 	    cp = initcp;
   1282 	}
   1283 
   1284 	/*
   1285 	 * Add all elements of the members list to the parent node.
   1286 	 */
   1287 	while(!Lst_IsEmpty(members)) {
   1288 	    gn = Lst_Dequeue(members);
   1289 
   1290 	    SUFF_DEBUG1("%s...", gn->name);
   1291 	    /* Add gn to the parents child list before the original child */
   1292 	    Lst_InsertBefore(pgn->children, cln, gn);
   1293 	    Lst_Append(gn->parents, pgn);
   1294 	    pgn->unmade++;
   1295 	    /* Expand wildcards on new node */
   1296 	    SuffExpandWildcards(cln->prev, pgn);
   1297 	}
   1298 	Lst_Free(members);
   1299 
   1300 	/*
   1301 	 * Free the result
   1302 	 */
   1303 	free(cp);
   1304     }
   1305 
   1306     SUFF_DEBUG0("\n");
   1307 
   1308     /*
   1309      * Now the source is expanded, remove it from the list of children to
   1310      * keep it from being processed.
   1311      */
   1312     pgn->unmade--;
   1313     Lst_Remove(pgn->children, cln);
   1314     Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
   1315 }
   1316 
   1317 static void
   1318 SuffExpandWildcards(GNodeListNode *cln, GNode *pgn)
   1319 {
   1320     GNode *cgn = LstNode_Datum(cln);
   1321     StringList *explist;
   1322 
   1323     if (!Dir_HasWildcards(cgn->name))
   1324 	return;
   1325 
   1326     /*
   1327      * Expand the word along the chosen path
   1328      */
   1329     explist = Lst_New();
   1330     Dir_Expand(cgn->name, Suff_FindPath(cgn), explist);
   1331 
   1332     while (!Lst_IsEmpty(explist)) {
   1333 	GNode	*gn;
   1334 	/*
   1335 	 * Fetch next expansion off the list and find its GNode
   1336 	 */
   1337 	char *cp = Lst_Dequeue(explist);
   1338 
   1339 	SUFF_DEBUG1("%s...", cp);
   1340 	gn = Targ_GetNode(cp);
   1341 
   1342 	/* Add gn to the parents child list before the original child */
   1343 	Lst_InsertBefore(pgn->children, cln, gn);
   1344 	Lst_Append(gn->parents, pgn);
   1345 	pgn->unmade++;
   1346     }
   1347 
   1348     Lst_Free(explist);
   1349 
   1350     SUFF_DEBUG0("\n");
   1351 
   1352     /*
   1353      * Now the source is expanded, remove it from the list of children to
   1354      * keep it from being processed.
   1355      */
   1356     pgn->unmade--;
   1357     Lst_Remove(pgn->children, cln);
   1358     Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
   1359 }
   1360 
   1361 /* Find a path along which to expand the node.
   1362  *
   1363  * If the word has a known suffix, use that path.
   1364  * If it has no known suffix, use the default system search path.
   1365  *
   1366  * Input:
   1367  *	gn		Node being examined
   1368  *
   1369  * Results:
   1370  *	The appropriate path to search for the GNode.
   1371  */
   1372 SearchPath *
   1373 Suff_FindPath(GNode* gn)
   1374 {
   1375     Suff *suff = gn->suffix;
   1376 
   1377     if (suff == NULL) {
   1378 	struct SuffSuffGetSuffixArgs sd;   /* Search string data */
   1379 	SuffListNode *ln;
   1380 	sd.name_len = strlen(gn->name);
   1381 	sd.name_end = gn->name + sd.name_len;
   1382 	ln = Lst_Find(sufflist, SuffSuffIsSuffix, &sd);
   1383 
   1384 	SUFF_DEBUG1("Wildcard expanding \"%s\"...", gn->name);
   1385 	if (ln != NULL)
   1386 	    suff = LstNode_Datum(ln);
   1387 	/* XXX: Here we can save the suffix so we don't have to do this again */
   1388     }
   1389 
   1390     if (suff != NULL) {
   1391 	SUFF_DEBUG1("suffix is \"%s\"...", suff->name);
   1392 	return suff->searchPath;
   1393     } else {
   1394 	/*
   1395 	 * Use default search path
   1396 	 */
   1397 	return dirSearchPath;
   1398     }
   1399 }
   1400 
   1401 /* Apply a transformation rule, given the source and target nodes and
   1402  * suffixes.
   1403  *
   1404  * Input:
   1405  *	tGn		Target node
   1406  *	sGn		Source node
   1407  *	t		Target suffix
   1408  *	s		Source suffix
   1409  *
   1410  * Results:
   1411  *	TRUE if successful, FALSE if not.
   1412  *
   1413  * Side Effects:
   1414  *	The source and target are linked and the commands from the
   1415  *	transformation are added to the target node's commands list.
   1416  *	All attributes but OP_DEPMASK and OP_TRANSFORM are applied
   1417  *	to the target. The target also inherits all the sources for
   1418  *	the transformation rule.
   1419  */
   1420 static Boolean
   1421 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
   1422 {
   1423     GNodeListNode *ln, *nln;    /* General node */
   1424     char *tname;		/* Name of transformation rule */
   1425     GNode *gn;			/* Node for same */
   1426 
   1427     /*
   1428      * Form the proper links between the target and source.
   1429      */
   1430     Lst_Append(tGn->children, sGn);
   1431     Lst_Append(sGn->parents, tGn);
   1432     tGn->unmade++;
   1433 
   1434     /*
   1435      * Locate the transformation rule itself
   1436      */
   1437     tname = str_concat2(s->name, t->name);
   1438     ln = Lst_Find(transforms, SuffGNHasName, tname);
   1439     free(tname);
   1440 
   1441     if (ln == NULL) {
   1442 	/*
   1443 	 * Not really such a transformation rule (can happen when we're
   1444 	 * called to link an OP_MEMBER and OP_ARCHV node), so return
   1445 	 * FALSE.
   1446 	 */
   1447 	return FALSE;
   1448     }
   1449 
   1450     gn = LstNode_Datum(ln);
   1451 
   1452     SUFF_DEBUG3("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
   1453 
   1454     /*
   1455      * Record last child for expansion purposes
   1456      */
   1457     ln = Lst_Last(tGn->children);
   1458 
   1459     /*
   1460      * Pass the buck to Make_HandleUse to apply the rule
   1461      */
   1462     (void)Make_HandleUse(gn, tGn);
   1463 
   1464     /*
   1465      * Deal with wildcards and variables in any acquired sources
   1466      */
   1467     for (ln = ln != NULL ? ln->next : NULL; ln != NULL; ln = nln) {
   1468 	nln = ln->next;
   1469 	SuffExpandChildren(ln, tGn);
   1470     }
   1471 
   1472     /*
   1473      * Keep track of another parent to which this beast is transformed so
   1474      * the .IMPSRC variable can be set correctly for the parent.
   1475      */
   1476     Lst_Append(sGn->implicitParents, tGn);
   1477 
   1478     return TRUE;
   1479 }
   1480 
   1481 
   1482 /* Locate dependencies for an OP_ARCHV node.
   1483  *
   1484  * Input:
   1485  *	gn		Node for which to locate dependencies
   1486  *
   1487  * Side Effects:
   1488  *	Same as Suff_FindDeps
   1489  */
   1490 static void
   1491 SuffFindArchiveDeps(GNode *gn, SrcList *slst)
   1492 {
   1493     char *eoarch;		/* End of archive portion */
   1494     char *eoname;		/* End of member portion */
   1495     GNode *mem;			/* Node for member */
   1496     SuffListNode *ln, *nln;	/* Next suffix node to check */
   1497     Suff *ms;			/* Suffix descriptor for member */
   1498     char *name;			/* Start of member's name */
   1499 
   1500     /*
   1501      * The node is an archive(member) pair. so we must find a
   1502      * suffix for both of them.
   1503      */
   1504     eoarch = strchr(gn->name, '(');
   1505     eoname = strchr(eoarch, ')');
   1506 
   1507     /*
   1508      * Caller guarantees the format `libname(member)', via
   1509      * Arch_ParseArchive.
   1510      */
   1511     assert(eoarch != NULL);
   1512     assert(eoname != NULL);
   1513 
   1514     *eoname = '\0';	  /* Nuke parentheses during suffix search */
   1515     *eoarch = '\0';	  /* So a suffix can be found */
   1516 
   1517     name = eoarch + 1;
   1518 
   1519     /*
   1520      * To simplify things, call Suff_FindDeps recursively on the member now,
   1521      * so we can simply compare the member's .PREFIX and .TARGET variables
   1522      * to locate its suffix. This allows us to figure out the suffix to
   1523      * use for the archive without having to do a quadratic search over the
   1524      * suffix list, backtracking for each one...
   1525      */
   1526     mem = Targ_GetNode(name);
   1527     SuffFindDeps(mem, slst);
   1528 
   1529     /*
   1530      * Create the link between the two nodes right off
   1531      */
   1532     Lst_Append(gn->children, mem);
   1533     Lst_Append(mem->parents, gn);
   1534     gn->unmade++;
   1535 
   1536     /*
   1537      * Copy in the variables from the member node to this one.
   1538      */
   1539     {
   1540 	char *freeIt;
   1541 	Var_Set(PREFIX, Var_Value(PREFIX, mem, &freeIt), gn);
   1542 	bmake_free(freeIt);
   1543 	Var_Set(TARGET, Var_Value(TARGET, mem, &freeIt), gn);
   1544 	bmake_free(freeIt);
   1545     }
   1546 
   1547     ms = mem->suffix;
   1548     if (ms == NULL) {
   1549 	/*
   1550 	 * Didn't know what it was -- use .NULL suffix if not in make mode
   1551 	 */
   1552 	SUFF_DEBUG0("using null suffix\n");
   1553 	ms = suffNull;
   1554     }
   1555 
   1556 
   1557     /*
   1558      * Set the other two local variables required for this target.
   1559      */
   1560     Var_Set(MEMBER, name, gn);
   1561     Var_Set(ARCHIVE, gn->name, gn);
   1562 
   1563     /*
   1564      * Set $@ for compatibility with other makes
   1565      */
   1566     Var_Set(TARGET, gn->name, gn);
   1567 
   1568     /*
   1569      * Now we've got the important local variables set, expand any sources
   1570      * that still contain variables or wildcards in their names.
   1571      */
   1572     for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
   1573 	nln = ln->next;
   1574 	SuffExpandChildren(ln, gn);
   1575     }
   1576 
   1577     if (ms != NULL) {
   1578 	/*
   1579 	 * Member has a known suffix, so look for a transformation rule from
   1580 	 * it to a possible suffix of the archive. Rather than searching
   1581 	 * through the entire list, we just look at suffixes to which the
   1582 	 * member's suffix may be transformed...
   1583 	 */
   1584 	struct SuffSuffGetSuffixArgs sd;	/* Search string data */
   1585 
   1586 	/*
   1587 	 * Use first matching suffix...
   1588 	 */
   1589 	sd.name_len = (size_t)(eoarch - gn->name);
   1590 	sd.name_end = eoarch;
   1591 	ln = Lst_Find(ms->parents, SuffSuffIsSuffix, &sd);
   1592 
   1593 	if (ln != NULL) {
   1594 	    /*
   1595 	     * Got one -- apply it
   1596 	     */
   1597 	    Suff *suff = LstNode_Datum(ln);
   1598 	    if (!SuffApplyTransform(gn, mem, suff, ms)) {
   1599 		SUFF_DEBUG2("\tNo transformation from %s -> %s\n",
   1600 			    ms->name, suff->name);
   1601 	    }
   1602 	}
   1603     }
   1604 
   1605     /*
   1606      * Replace the opening and closing parens now we've no need of the separate
   1607      * pieces.
   1608      */
   1609     *eoarch = '('; *eoname = ')';
   1610 
   1611     /*
   1612      * Pretend gn appeared to the left of a dependency operator so
   1613      * the user needn't provide a transformation from the member to the
   1614      * archive.
   1615      */
   1616     if (OP_NOP(gn->type)) {
   1617 	gn->type |= OP_DEPENDS;
   1618     }
   1619 
   1620     /*
   1621      * Flag the member as such so we remember to look in the archive for
   1622      * its modification time. The OP_JOIN | OP_MADE is needed because this
   1623      * target should never get made.
   1624      */
   1625     mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
   1626 }
   1627 
   1628 /* Locate implicit dependencies for regular targets.
   1629  *
   1630  * Input:
   1631  *	gn		Node for which to find sources
   1632  *
   1633  * Side Effects:
   1634  *	Same as Suff_FindDeps
   1635  */
   1636 static void
   1637 SuffFindNormalDeps(GNode *gn, SrcList *slst)
   1638 {
   1639     char *eoname;		/* End of name */
   1640     char *sopref;		/* Start of prefix */
   1641     SuffListNode *ln, *nln;
   1642     SrcList *srcs;		/* List of sources at which to look */
   1643     SrcList *targs;		/* List of targets to which things can be
   1644 				 * transformed. They all have the same file,
   1645 				 * but different suff and pref fields */
   1646     Src *bottom;		/* Start of found transformation path */
   1647     Src *src;			/* General Src pointer */
   1648     char *pref;			/* Prefix to use */
   1649     Src *targ;			/* General Src target pointer */
   1650     struct SuffSuffGetSuffixArgs sd; /* Search string data */
   1651 
   1652 
   1653     sd.name_len = strlen(gn->name);
   1654     sd.name_end = eoname = gn->name + sd.name_len;
   1655 
   1656     sopref = gn->name;
   1657 
   1658     /*
   1659      * Begin at the beginning...
   1660      */
   1661     ln = Lst_First(sufflist);
   1662     srcs = Lst_New();
   1663     targs = Lst_New();
   1664 
   1665     /*
   1666      * We're caught in a catch-22 here. On the one hand, we want to use any
   1667      * transformation implied by the target's sources, but we can't examine
   1668      * the sources until we've expanded any variables/wildcards they may hold,
   1669      * and we can't do that until we've set up the target's local variables
   1670      * and we can't do that until we know what the proper suffix for the
   1671      * target is (in case there are two suffixes one of which is a suffix of
   1672      * the other) and we can't know that until we've found its implied
   1673      * source, which we may not want to use if there's an existing source
   1674      * that implies a different transformation.
   1675      *
   1676      * In an attempt to get around this, which may not work all the time,
   1677      * but should work most of the time, we look for implied sources first,
   1678      * checking transformations to all possible suffixes of the target,
   1679      * use what we find to set the target's local variables, expand the
   1680      * children, then look for any overriding transformations they imply.
   1681      * Should we find one, we discard the one we found before.
   1682      */
   1683     bottom = NULL;
   1684     targ = NULL;
   1685 
   1686     if (!(gn->type & OP_PHONY)) {
   1687 
   1688 	while (ln != NULL) {
   1689 	    /*
   1690 	     * Look for next possible suffix...
   1691 	     */
   1692 	    ln = Lst_FindFrom(sufflist, ln, SuffSuffIsSuffix, &sd);
   1693 
   1694 	    if (ln != NULL) {
   1695 		const char *eopref;
   1696 
   1697 		/*
   1698 		 * Allocate a Src structure to which things can be transformed
   1699 		 */
   1700 		targ = bmake_malloc(sizeof(Src));
   1701 		targ->file = bmake_strdup(gn->name);
   1702 		targ->suff = LstNode_Datum(ln);
   1703 		targ->suff->refCount++;
   1704 		targ->node = gn;
   1705 		targ->parent = NULL;
   1706 		targ->children = 0;
   1707 #ifdef DEBUG_SRC
   1708 		targ->cp = Lst_New();
   1709 #endif
   1710 
   1711 		eopref = eoname - targ->suff->nameLen;
   1712 		targ->pref = bmake_strsedup(sopref, eopref);
   1713 
   1714 		/*
   1715 		 * Add nodes from which the target can be made
   1716 		 */
   1717 		SuffAddLevel(srcs, targ);
   1718 
   1719 		/*
   1720 		 * Record the target so we can nuke it
   1721 		 */
   1722 		Lst_Append(targs, targ);
   1723 
   1724 		/*
   1725 		 * Search from this suffix's successor...
   1726 		 */
   1727 		ln = ln->next;
   1728 	    }
   1729 	}
   1730 
   1731 	/*
   1732 	 * Handle target of unknown suffix...
   1733 	 */
   1734 	if (Lst_IsEmpty(targs) && suffNull != NULL) {
   1735 	    SUFF_DEBUG1("\tNo known suffix on %s. Using .NULL suffix\n",
   1736 			gn->name);
   1737 
   1738 	    targ = bmake_malloc(sizeof(Src));
   1739 	    targ->file = bmake_strdup(gn->name);
   1740 	    targ->suff = suffNull;
   1741 	    targ->suff->refCount++;
   1742 	    targ->node = gn;
   1743 	    targ->parent = NULL;
   1744 	    targ->children = 0;
   1745 	    targ->pref = bmake_strdup(sopref);
   1746 #ifdef DEBUG_SRC
   1747 	    targ->cp = Lst_New();
   1748 #endif
   1749 
   1750 	    /*
   1751 	     * Only use the default suffix rules if we don't have commands
   1752 	     * defined for this gnode; traditional make programs used to
   1753 	     * not define suffix rules if the gnode had children but we
   1754 	     * don't do this anymore.
   1755 	     */
   1756 	    if (Lst_IsEmpty(gn->commands))
   1757 		SuffAddLevel(srcs, targ);
   1758 	    else {
   1759 		SUFF_DEBUG0("not ");
   1760 	    }
   1761 
   1762 	    SUFF_DEBUG0("adding suffix rules\n");
   1763 
   1764 	    Lst_Append(targs, targ);
   1765 	}
   1766 
   1767 	/*
   1768 	 * Using the list of possible sources built up from the target
   1769 	 * suffix(es), try and find an existing file/target that matches.
   1770 	 */
   1771 	bottom = SuffFindThem(srcs, slst);
   1772 
   1773 	if (bottom == NULL) {
   1774 	    /*
   1775 	     * No known transformations -- use the first suffix found
   1776 	     * for setting the local variables.
   1777 	     */
   1778 	    if (!Lst_IsEmpty(targs)) {
   1779 		targ = LstNode_Datum(Lst_First(targs));
   1780 	    } else {
   1781 		targ = NULL;
   1782 	    }
   1783 	} else {
   1784 	    /*
   1785 	     * Work up the transformation path to find the suffix of the
   1786 	     * target to which the transformation was made.
   1787 	     */
   1788 	    for (targ = bottom; targ->parent != NULL; targ = targ->parent)
   1789 		continue;
   1790 	}
   1791     }
   1792 
   1793     Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
   1794 
   1795     pref = (targ != NULL) ? targ->pref : gn->name;
   1796     Var_Set(PREFIX, pref, gn);
   1797 
   1798     /*
   1799      * Now we've got the important local variables set, expand any sources
   1800      * that still contain variables or wildcards in their names.
   1801      */
   1802     for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
   1803 	nln = ln->next;
   1804 	SuffExpandChildren(ln, gn);
   1805     }
   1806 
   1807     if (targ == NULL) {
   1808 	SUFF_DEBUG1("\tNo valid suffix on %s\n", gn->name);
   1809 
   1810 sfnd_abort:
   1811 	/*
   1812 	 * Deal with finding the thing on the default search path. We
   1813 	 * always do that, not only if the node is only a source (not
   1814 	 * on the lhs of a dependency operator or [XXX] it has neither
   1815 	 * children or commands) as the old pmake did.
   1816 	 */
   1817 	if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
   1818 	    free(gn->path);
   1819 	    gn->path = Dir_FindFile(gn->name,
   1820 				    (targ == NULL ? dirSearchPath :
   1821 				     targ->suff->searchPath));
   1822 	    if (gn->path != NULL) {
   1823 		char *ptr;
   1824 		Var_Set(TARGET, gn->path, gn);
   1825 
   1826 		if (targ != NULL) {
   1827 		    /*
   1828 		     * Suffix known for the thing -- trim the suffix off
   1829 		     * the path to form the proper .PREFIX variable.
   1830 		     */
   1831 		    size_t savep = strlen(gn->path) - targ->suff->nameLen;
   1832 		    char    savec;
   1833 
   1834 		    if (gn->suffix)
   1835 			gn->suffix->refCount--;
   1836 		    gn->suffix = targ->suff;
   1837 		    gn->suffix->refCount++;
   1838 
   1839 		    savec = gn->path[savep];
   1840 		    gn->path[savep] = '\0';
   1841 
   1842 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
   1843 			ptr++;
   1844 		    else
   1845 			ptr = gn->path;
   1846 
   1847 		    Var_Set(PREFIX, ptr, gn);
   1848 
   1849 		    gn->path[savep] = savec;
   1850 		} else {
   1851 		    /*
   1852 		     * The .PREFIX gets the full path if the target has
   1853 		     * no known suffix.
   1854 		     */
   1855 		    if (gn->suffix)
   1856 			gn->suffix->refCount--;
   1857 		    gn->suffix = NULL;
   1858 
   1859 		    if ((ptr = strrchr(gn->path, '/')) != NULL)
   1860 			ptr++;
   1861 		    else
   1862 			ptr = gn->path;
   1863 
   1864 		    Var_Set(PREFIX, ptr, gn);
   1865 		}
   1866 	    }
   1867 	}
   1868 
   1869 	goto sfnd_return;
   1870     }
   1871 
   1872     /*
   1873      * If the suffix indicates that the target is a library, mark that in
   1874      * the node's type field.
   1875      */
   1876     if (targ->suff->flags & SUFF_LIBRARY) {
   1877 	gn->type |= OP_LIB;
   1878     }
   1879 
   1880     /*
   1881      * Check for overriding transformation rule implied by sources
   1882      */
   1883     if (!Lst_IsEmpty(gn->children)) {
   1884 	src = SuffFindCmds(targ, slst);
   1885 
   1886 	if (src != NULL) {
   1887 	    /*
   1888 	     * Free up all the Src structures in the transformation path
   1889 	     * up to, but not including, the parent node.
   1890 	     */
   1891 	    while (bottom && bottom->parent != NULL) {
   1892 		if (Lst_FindDatum(slst, bottom) == NULL) {
   1893 		    Lst_Append(slst, bottom);
   1894 		}
   1895 		bottom = bottom->parent;
   1896 	    }
   1897 	    bottom = src;
   1898 	}
   1899     }
   1900 
   1901     if (bottom == NULL) {
   1902 	/*
   1903 	 * No idea from where it can come -- return now.
   1904 	 */
   1905 	goto sfnd_abort;
   1906     }
   1907 
   1908     /*
   1909      * We now have a list of Src structures headed by 'bottom' and linked via
   1910      * their 'parent' pointers. What we do next is create links between
   1911      * source and target nodes (which may or may not have been created)
   1912      * and set the necessary local variables in each target. The
   1913      * commands for each target are set from the commands of the
   1914      * transformation rule used to get from the src suffix to the targ
   1915      * suffix. Note that this causes the commands list of the original
   1916      * node, gn, to be replaced by the commands of the final
   1917      * transformation rule. Also, the unmade field of gn is incremented.
   1918      * Etc.
   1919      */
   1920     if (bottom->node == NULL) {
   1921 	bottom->node = Targ_GetNode(bottom->file);
   1922     }
   1923 
   1924     for (src = bottom; src->parent != NULL; src = src->parent) {
   1925 	targ = src->parent;
   1926 
   1927 	if (src->node->suffix)
   1928 	    src->node->suffix->refCount--;
   1929 	src->node->suffix = src->suff;
   1930 	src->node->suffix->refCount++;
   1931 
   1932 	if (targ->node == NULL) {
   1933 	    targ->node = Targ_GetNode(targ->file);
   1934 	}
   1935 
   1936 	SuffApplyTransform(targ->node, src->node,
   1937 			   targ->suff, src->suff);
   1938 
   1939 	if (targ->node != gn) {
   1940 	    /*
   1941 	     * Finish off the dependency-search process for any nodes
   1942 	     * between bottom and gn (no point in questing around the
   1943 	     * filesystem for their implicit source when it's already
   1944 	     * known). Note that the node can't have any sources that
   1945 	     * need expanding, since SuffFindThem will stop on an existing
   1946 	     * node, so all we need to do is set the standard and System V
   1947 	     * variables.
   1948 	     */
   1949 	    targ->node->type |= OP_DEPS_FOUND;
   1950 
   1951 	    Var_Set(PREFIX, targ->pref, targ->node);
   1952 
   1953 	    Var_Set(TARGET, targ->node->name, targ->node);
   1954 	}
   1955     }
   1956 
   1957     if (gn->suffix)
   1958 	gn->suffix->refCount--;
   1959     gn->suffix = src->suff;
   1960     gn->suffix->refCount++;
   1961 
   1962     /*
   1963      * Nuke the transformation path and the Src structures left over in the
   1964      * two lists.
   1965      */
   1966 sfnd_return:
   1967     if (bottom)
   1968 	if (Lst_FindDatum(slst, bottom) == NULL)
   1969 	    Lst_Append(slst, bottom);
   1970 
   1971     while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
   1972 	continue;
   1973 
   1974     Lst_MoveAll(slst, srcs);
   1975     Lst_MoveAll(slst, targs);
   1976 }
   1977 
   1978 
   1979 /* Find implicit sources for the target described by the graph node.
   1980  *
   1981  * Nodes are added to the graph below the passed-in node. The nodes are
   1982  * marked to have their IMPSRC variable filled in. The PREFIX variable is set
   1983  * for the given node and all its implied children.
   1984  *
   1985  * The path found by this target is the shortest path in the transformation
   1986  * graph, which may pass through non-existent targets, to an existing target.
   1987  * The search continues on all paths from the root suffix until a file is
   1988  * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
   1989  * .l,v file exists but the .c and .l files don't, the search will branch out
   1990  * in all directions from .o and again from all the nodes on the next level
   1991  * until the .l,v node is encountered.
   1992  */
   1993 void
   1994 Suff_FindDeps(GNode *gn)
   1995 {
   1996 
   1997     SuffFindDeps(gn, srclist);
   1998     while (SuffRemoveSrc(srclist))
   1999 	continue;
   2000 }
   2001 
   2002 static void
   2003 SuffFindDeps(GNode *gn, SrcList *slst)
   2004 {
   2005     if (gn->type & OP_DEPS_FOUND)
   2006 	return;
   2007     gn->type |= OP_DEPS_FOUND;
   2008 
   2009     /*
   2010      * Make sure we have these set, may get revised below.
   2011      */
   2012     Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
   2013     Var_Set(PREFIX, gn->name, gn);
   2014 
   2015     SUFF_DEBUG1("SuffFindDeps (%s)\n", gn->name);
   2016 
   2017     if (gn->type & OP_ARCHV) {
   2018 	SuffFindArchiveDeps(gn, slst);
   2019     } else if (gn->type & OP_LIB) {
   2020 	/*
   2021 	 * If the node is a library, it is the arch module's job to find it
   2022 	 * and set the TARGET variable accordingly. We merely provide the
   2023 	 * search path, assuming all libraries end in ".a" (if the suffix
   2024 	 * hasn't been defined, there's nothing we can do for it, so we just
   2025 	 * set the TARGET variable to the node's name in order to give it a
   2026 	 * value).
   2027 	 */
   2028 	Suff *s = FindSuffByName(LIBSUFF);
   2029 	if (gn->suffix)
   2030 	    gn->suffix->refCount--;
   2031 	if (s != NULL) {
   2032 	    gn->suffix = s;
   2033 	    gn->suffix->refCount++;
   2034 	    Arch_FindLib(gn, s->searchPath);
   2035 	} else {
   2036 	    gn->suffix = NULL;
   2037 	    Var_Set(TARGET, gn->name, gn);
   2038 	}
   2039 	/*
   2040 	 * Because a library (-lfoo) target doesn't follow the standard
   2041 	 * filesystem conventions, we don't set the regular variables for
   2042 	 * the thing. .PREFIX is simply made empty...
   2043 	 */
   2044 	Var_Set(PREFIX, "", gn);
   2045     } else {
   2046 	SuffFindNormalDeps(gn, slst);
   2047     }
   2048 }
   2049 
   2050 /* Define which suffix is the null suffix.
   2051  *
   2052  * Need to handle the changing of the null suffix gracefully so the old
   2053  * transformation rules don't just go away.
   2054  *
   2055  * Input:
   2056  *	name		Name of null suffix
   2057  */
   2058 void
   2059 Suff_SetNull(const char *name)
   2060 {
   2061     Suff *s = FindSuffByName(name);
   2062     if (s == NULL) {
   2063 	Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
   2064 		    name);
   2065 	return;
   2066     }
   2067 
   2068     if (suffNull != NULL) {
   2069 	suffNull->flags &= ~(unsigned)SUFF_NULL;
   2070     }
   2071     s->flags |= SUFF_NULL;
   2072     /*
   2073      * XXX: Here's where the transformation mangling would take place
   2074      */
   2075     suffNull = s;
   2076 }
   2077 
   2078 /* Initialize the suffixes module. */
   2079 void
   2080 Suff_Init(void)
   2081 {
   2082 #ifdef CLEANUP
   2083     suffClean = Lst_New();
   2084     sufflist = Lst_New();
   2085 #endif
   2086     srclist = Lst_New();
   2087     transforms = Lst_New();
   2088 
   2089     /*
   2090      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
   2091      * actually go on the suffix list or everyone will think that's its
   2092      * suffix.
   2093      */
   2094     Suff_ClearSuffixes();
   2095 }
   2096 
   2097 
   2098 /* Clean up the suffixes module. */
   2099 void
   2100 Suff_End(void)
   2101 {
   2102 #ifdef CLEANUP
   2103     Lst_Destroy(sufflist, SuffFree);
   2104     Lst_Destroy(suffClean, SuffFree);
   2105     if (suffNull)
   2106 	SuffFree(suffNull);
   2107     Lst_Free(srclist);
   2108     Lst_Free(transforms);
   2109 #endif
   2110 }
   2111 
   2112 
   2113 /********************* DEBUGGING FUNCTIONS **********************/
   2114 
   2115 static void
   2116 PrintSuffNames(const char *prefix, SuffList *suffs)
   2117 {
   2118     SuffListNode *ln;
   2119 
   2120     debug_printf("#\t%s: ", prefix);
   2121     for (ln = suffs->first; ln != NULL; ln = ln->next) {
   2122 	Suff *suff = ln->datum;
   2123 	debug_printf("%s ", suff->name);
   2124     }
   2125     debug_printf("\n");
   2126 }
   2127 
   2128 static void
   2129 PrintSuff(Suff *s)
   2130 {
   2131     debug_printf("# `%s' [%d] ", s->name, s->refCount);
   2132     if (s->flags != 0) {
   2133 	char flags_buf[SuffFlags_ToStringSize];
   2134 
   2135 	debug_printf(" (%s)",
   2136 		     Enum_FlagsToString(flags_buf, sizeof flags_buf,
   2137 					s->flags, SuffFlags_ToStringSpecs));
   2138     }
   2139     debug_printf("\n");
   2140 
   2141     PrintSuffNames("To", s->parents);
   2142     PrintSuffNames("From", s->children);
   2143 
   2144     debug_printf("#\tSearch Path: ");
   2145     Dir_PrintPath(s->searchPath);
   2146     debug_printf("\n");
   2147 }
   2148 
   2149 static void
   2150 PrintTransformation(GNode *t)
   2151 {
   2152     debug_printf("%-16s:", t->name);
   2153     Targ_PrintType(t->type);
   2154     debug_printf("\n");
   2155     Targ_PrintCmds(t);
   2156     debug_printf("\n");
   2157 }
   2158 
   2159 void
   2160 Suff_PrintAll(void)
   2161 {
   2162     debug_printf("#*** Suffixes:\n");
   2163     {
   2164 	SuffListNode *ln;
   2165 	for (ln = sufflist->first; ln != NULL; ln = ln->next)
   2166 	    PrintSuff(ln->datum);
   2167     }
   2168 
   2169     debug_printf("#*** Transformations:\n");
   2170     {
   2171 	GNodeListNode *ln;
   2172 	for (ln = transforms->first; ln != NULL; ln = ln->next)
   2173 	    PrintTransformation(ln->datum);
   2174     }
   2175 }
   2176