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