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