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