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