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