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