Home | History | Annotate | Line # | Download | only in make
suff.c revision 1.298
      1 /*	$NetBSD: suff.c,v 1.298 2020/11/22 17:20:15 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.298 2020/11/22 17:20:15 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 	*inout_main = target;
    690 	Targ_SetMain(target);
    691 	/*
    692 	 * XXX: Why could it be a good idea to return TRUE here?
    693 	 * The main task of this function is to turn ordinary nodes into
    694 	 * transformations, no matter whether or not a new .MAIN node
    695 	 * has been found.
    696 	 */
    697 	/*
    698 	 * XXX: Even when changing this to FALSE, none of the existing unit
    699 	 * tests fails.
    700 	 */
    701 	return TRUE;
    702     }
    703 
    704     if (target->type == OP_TRANSFORM)
    705 	return FALSE;
    706 
    707     /*
    708      * XXX: What about a transformation ".cpp.c"?  If ".c" is added as a new
    709      * suffix, it seems wrong that this transformation would be skipped just
    710      * because ".c" happens to be a prefix of ".cpp".
    711      */
    712     ptr = strstr(target->name, suff->name);
    713     if (ptr == NULL)
    714 	return FALSE;
    715 
    716     /*
    717      * XXX: In suff-rebuild.mk, in the line '.SUFFIXES: .c .b .a', this
    718      * condition prevents the rule '.b.c' from being added again during
    719      * Suff_AddSuffix(".b").
    720      *
    721      * XXX: Removing this paragraph makes suff-add-later.mk use massive
    722      * amounts of memory.
    723      */
    724     if (ptr == target->name)
    725 	return FALSE;
    726 
    727     if (ParseTransform(target->name, &srcSuff, &targSuff)) {
    728 	if (*inout_main == target) {
    729 	    *inout_removedMain = TRUE;
    730 	    *inout_main = NULL;
    731 	    Targ_SetMain(NULL);
    732 	}
    733 	Lst_Free(target->children);
    734 	target->children = Lst_New();
    735 	target->type = OP_TRANSFORM;
    736 	/*
    737 	 * link the two together in the proper relationship and order
    738 	 */
    739 	SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
    740 		    srcSuff->name, targSuff->name);
    741 	Relate(srcSuff, targSuff);
    742     }
    743     return FALSE;
    744 }
    745 
    746 /* Look at all existing targets to see if adding this suffix will make one
    747  * of the current targets mutate into a suffix rule.
    748  *
    749  * This is ugly, but other makes treat all targets that start with a '.' as
    750  * suffix rules. */
    751 static void
    752 UpdateTargets(GNode **inout_main, Suffix *suff)
    753 {
    754     Boolean r = FALSE;
    755     GNodeListNode *ln;
    756     for (ln = Targ_List()->first; ln != NULL; ln = ln->next) {
    757 	GNode *gn = ln->datum;
    758 	if (UpdateTarget(gn, inout_main, suff, &r))
    759 	    break;
    760     }
    761 }
    762 
    763 /* Add the suffix to the end of the list of known suffixes.
    764  * Should we restructure the suffix graph? Make doesn't...
    765  *
    766  * A GNode is created for the suffix and a Suffix structure is created and
    767  * added to the suffixes list unless the suffix was already known.
    768  * The mainNode passed can be modified if a target mutated into a
    769  * transform and that target happened to be the main target.
    770  *
    771  * Input:
    772  *	name		the name of the suffix to add
    773  */
    774 void
    775 Suff_AddSuffix(const char *name, GNode **inout_main)
    776 {
    777     GNodeListNode *ln;
    778 
    779     Suffix *suff = FindSuffixByName(name);
    780     if (suff != NULL)
    781 	return;
    782 
    783     suff = Suffix_New(name);
    784     Lst_Append(sufflist, suff);
    785     DEBUG1(SUFF, "Adding suffix \"%s\"\n", suff->name);
    786 
    787     UpdateTargets(inout_main, suff);
    788 
    789     /*
    790      * Look for any existing transformations from or to this suffix.
    791      * XXX: Only do this after a Suff_ClearSuffixes?
    792      */
    793     for (ln = transforms->first; ln != NULL; ln = ln->next)
    794 	RebuildGraph(ln->datum, suff);
    795 }
    796 
    797 /* Return the search path for the given suffix, or NULL. */
    798 SearchPath *
    799 Suff_GetPath(const char *sname)
    800 {
    801     Suffix *suff = FindSuffixByName(sname);
    802     return suff != NULL ? suff->searchPath : NULL;
    803 }
    804 
    805 /*
    806  * Extend the search paths for all suffixes to include the default search
    807  * path (dirSearchPath).
    808  *
    809  * The default search path can be defined using the special target '.PATH'.
    810  * The search path of each suffix can be defined using the special target
    811  * '.PATH<suffix>'.
    812  *
    813  * If paths were specified for the ".h" suffix, the directories are stuffed
    814  * into a global variable called ".INCLUDES" with each directory preceded by
    815  * '-I'. The same is done for the ".a" suffix, except the variable is called
    816  * ".LIBS" and the flag is '-L'.
    817  */
    818 void
    819 Suff_DoPaths(void)
    820 {
    821     SuffixListNode *ln;
    822     char *ptr;
    823     SearchPath *inIncludes; /* Cumulative .INCLUDES path */
    824     SearchPath *inLibs;	    /* Cumulative .LIBS path */
    825 
    826     inIncludes = Lst_New();
    827     inLibs = Lst_New();
    828 
    829     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
    830 	Suffix *suff = ln->datum;
    831 	if (!Lst_IsEmpty(suff->searchPath)) {
    832 #ifdef INCLUDES
    833 	    if (suff->flags & SUFF_INCLUDE)
    834 		Dir_Concat(inIncludes, suff->searchPath);
    835 #endif
    836 #ifdef LIBRARIES
    837 	    if (suff->flags & SUFF_LIBRARY)
    838 		Dir_Concat(inLibs, suff->searchPath);
    839 #endif
    840 	    Dir_Concat(suff->searchPath, dirSearchPath);
    841 	} else {
    842 	    Lst_Destroy(suff->searchPath, Dir_Destroy);
    843 	    suff->searchPath = Dir_CopyDirSearchPath();
    844 	}
    845     }
    846 
    847     Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
    848     free(ptr);
    849     Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
    850     free(ptr);
    851 
    852     Lst_Destroy(inIncludes, Dir_Destroy);
    853     Lst_Destroy(inLibs, Dir_Destroy);
    854 }
    855 
    856 /*
    857  * Add the given suffix as a type of file which gets included.
    858  * Called when a '.INCLUDES: .h' line is parsed.
    859  * To have an effect, the suffix must already exist.
    860  * This affects the magic variable '.INCLUDES'.
    861  */
    862 void
    863 Suff_AddInclude(const char *suffName)
    864 {
    865     Suffix *suff = FindSuffixByName(suffName);
    866     if (suff != NULL)
    867 	suff->flags |= SUFF_INCLUDE;
    868 }
    869 
    870 /*
    871  * Add the given suffix as a type of file which is a library.
    872  * Called when a '.LIBS: .a' line is parsed.
    873  * To have an effect, the suffix must already exist.
    874  * This affects the magic variable '.LIBS'.
    875  */
    876 void
    877 Suff_AddLib(const char *suffName)
    878 {
    879     Suffix *suff = FindSuffixByName(suffName);
    880     if (suff != NULL)
    881 	suff->flags |= SUFF_LIBRARY;
    882 }
    883 
    884 	  /********** Implicit Source Search Functions *********/
    885 
    886 #ifdef DEBUG_SRC
    887 static void
    888 CandidateList_PrintAddrs(CandidateList *list)
    889 {
    890     CandidateListNode *ln;
    891 
    892     for (ln = list->first; ln != NULL; ln = ln->next)
    893 	debug_printf(" %p", ln->datum);
    894     debug_printf("\n");
    895 }
    896 #endif
    897 
    898 static Candidate *
    899 Candidate_New(char *name, char *prefix, Suffix *suff, Candidate *parent,
    900 	      GNode *gn)
    901 {
    902     Candidate *cand = bmake_malloc(sizeof *cand);
    903 
    904     cand->file = name;
    905     cand->prefix = prefix;
    906     cand->suff = Suffix_Ref(suff);
    907     cand->parent = parent;
    908     cand->node = gn;
    909     cand->numChildren = 0;
    910 #ifdef DEBUG_SRC
    911     cand->childrenList = Lst_New();
    912 #endif
    913 
    914     return cand;
    915 }
    916 
    917 /* Add a new candidate to the list. */
    918 static void
    919 CandidateList_Add(CandidateList *list, char *srcName, Candidate *targ,
    920 		  Suffix *suff, const char *debug_tag)
    921 {
    922     Candidate *cand = Candidate_New(srcName, targ->prefix, suff, targ, NULL);
    923     targ->numChildren++;
    924     Lst_Append(list, cand);
    925 
    926 #ifdef DEBUG_SRC
    927     Lst_Append(targ->childrenList, cand);
    928     debug_printf("%s add suff %p candidate %p to list %p:",
    929 		 debug_tag, targ, cand, list);
    930     CandidateList_PrintAddrs(list);
    931 #endif
    932 }
    933 
    934 /* Add all candidates to the list that can be formed by applying a suffix to
    935  * the candidate. */
    936 static void
    937 CandidateList_AddCandidatesFor(CandidateList *list, Candidate *cand)
    938 {
    939     SuffixListNode *ln;
    940     for (ln = cand->suff->children->first; ln != NULL; ln = ln->next) {
    941 	Suffix *suff = ln->datum;
    942 
    943 	if ((suff->flags & SUFF_NULL) && suff->name[0] != '\0') {
    944 	    /*
    945 	     * If the suffix has been marked as the NULL suffix, also
    946 	     * create a candidate for a file with no suffix attached.
    947 	     */
    948 	    CandidateList_Add(list, bmake_strdup(cand->prefix),
    949 		       cand, suff, "1");
    950 	}
    951 
    952 	CandidateList_Add(list, str_concat2(cand->prefix, suff->name),
    953 			  cand, suff, "2");
    954     }
    955 }
    956 
    957 /* Free the first candidate in the list that is not referenced anymore.
    958  * Return whether a candidate was removed. */
    959 static Boolean
    960 RemoveCandidate(CandidateList *srcs)
    961 {
    962     CandidateListNode *ln;
    963 
    964 #ifdef DEBUG_SRC
    965     debug_printf("cleaning list %p:", srcs);
    966     CandidateList_PrintAddrs(srcs);
    967 #endif
    968 
    969     for (ln = srcs->first; ln != NULL; ln = ln->next) {
    970 	Candidate *src = ln->datum;
    971 
    972 	if (src->numChildren == 0) {
    973 	    free(src->file);
    974 	    if (src->parent == NULL)
    975 		free(src->prefix);
    976 	    else {
    977 #ifdef DEBUG_SRC
    978 	        /* XXX: Lst_RemoveDatum */
    979 		CandidateListNode *ln2;
    980 		ln2 = Lst_FindDatum(src->parent->childrenList, src);
    981 		if (ln2 != NULL)
    982 		    Lst_Remove(src->parent->childrenList, ln2);
    983 #endif
    984 		src->parent->numChildren--;
    985 	    }
    986 #ifdef DEBUG_SRC
    987 	    debug_printf("free: list %p src %p children %d\n",
    988 			 srcs, src, src->numChildren);
    989 	    Lst_Free(src->childrenList);
    990 #endif
    991 	    Lst_Remove(srcs, ln);
    992 	    free(src);
    993 	    return TRUE;
    994 	}
    995 #ifdef DEBUG_SRC
    996 	else {
    997 	    debug_printf("keep: list %p src %p children %d:",
    998 			 srcs, src, src->numChildren);
    999 	    CandidateList_PrintAddrs(src->childrenList);
   1000 	}
   1001 #endif
   1002     }
   1003 
   1004     return FALSE;
   1005 }
   1006 
   1007 /* Find the first existing file/target in srcs. */
   1008 /* XXX: The parameter names are too similar. */
   1009 static Candidate *
   1010 FindThem(CandidateList *srcs, CandidateList *slst)
   1011 {
   1012     Candidate *retsrc = NULL;
   1013 
   1014     while (!Lst_IsEmpty(srcs)) {
   1015 	Candidate *src = Lst_Dequeue(srcs);
   1016 
   1017 	SUFF_DEBUG1("\ttrying %s...", src->file);
   1018 
   1019 	/*
   1020 	 * A file is considered to exist if either a node exists in the
   1021 	 * graph for it or the file actually exists.
   1022 	 */
   1023 	if (Targ_FindNode(src->file) != NULL) {
   1024 #ifdef DEBUG_SRC
   1025 	    debug_printf("remove from list %p src %p\n", srcs, src);
   1026 #endif
   1027 	    retsrc = src;
   1028 	    break;
   1029 	}
   1030 
   1031 	{
   1032 	    char *file = Dir_FindFile(src->file, src->suff->searchPath);
   1033 	    if (file != NULL) {
   1034 		retsrc = src;
   1035 #ifdef DEBUG_SRC
   1036 		debug_printf("remove from list %p src %p\n", srcs, src);
   1037 #endif
   1038 		free(file);
   1039 		break;
   1040 	    }
   1041 	}
   1042 
   1043 	SUFF_DEBUG0("not there\n");
   1044 
   1045 	CandidateList_AddCandidatesFor(srcs, src);
   1046 	Lst_Append(slst, src);
   1047     }
   1048 
   1049     if (retsrc) {
   1050 	SUFF_DEBUG0("got it\n");
   1051     }
   1052     return retsrc;
   1053 }
   1054 
   1055 /*
   1056  * See if any of the children of the candidate's GNode is one from which the
   1057  * target can be transformed. If there is one, a candidate is put together
   1058  * for it and returned.
   1059  */
   1060 static Candidate *
   1061 FindCmds(Candidate *targ, CandidateList *slst)
   1062 {
   1063     GNodeListNode *gln;
   1064     GNode *tgn;			/* Target GNode */
   1065     GNode *sgn;			/* Source GNode */
   1066     size_t prefLen;		/* The length of the defined prefix */
   1067     Suffix *suff;		/* Suffix on matching beastie */
   1068     Candidate *ret;		/* Return value */
   1069     char *cp;
   1070 
   1071     tgn = targ->node;
   1072     prefLen = strlen(targ->prefix);
   1073 
   1074     for (gln = tgn->children->first; gln != NULL; gln = gln->next) {
   1075 	sgn = gln->datum;
   1076 
   1077 	if (sgn->type & OP_OPTIONAL && Lst_IsEmpty(tgn->commands)) {
   1078 	    /*
   1079 	     * We haven't looked to see if .OPTIONAL files exist yet, so
   1080 	     * don't use one as the implicit source.
   1081 	     * This allows us to use .OPTIONAL in .depend files so make won't
   1082 	     * complain "don't know how to make xxx.h' when a dependent file
   1083 	     * has been moved/deleted.
   1084 	     */
   1085 	    continue;
   1086 	}
   1087 
   1088 	cp = strrchr(sgn->name, '/');
   1089 	if (cp == NULL) {
   1090 	    cp = sgn->name;
   1091 	} else {
   1092 	    cp++;
   1093 	}
   1094 	if (strncmp(cp, targ->prefix, prefLen) != 0)
   1095 	    continue;
   1096 	/* The node matches the prefix ok, see if it has a known suffix. */
   1097 	suff = FindSuffixByName(cp + prefLen);
   1098 	if (suff == NULL)
   1099 	    continue;
   1100 
   1101 	/*
   1102 	 * It even has a known suffix, see if there's a transformation
   1103 	 * defined between the node's suffix and the target's suffix.
   1104 	 *
   1105 	 * XXX: Handle multi-stage transformations here, too.
   1106 	 */
   1107 
   1108 	if (Lst_FindDatum(suff->parents, targ->suff) != NULL)
   1109 	    break;
   1110     }
   1111 
   1112     if (gln == NULL)
   1113 	return NULL;
   1114 
   1115     ret = Candidate_New(bmake_strdup(sgn->name), targ->prefix, suff, targ, sgn);
   1116     targ->numChildren++;
   1117 #ifdef DEBUG_SRC
   1118     debug_printf("3 add targ %p ret %p\n", targ, ret);
   1119     Lst_Append(targ->childrenList, ret);
   1120 #endif
   1121     Lst_Append(slst, ret);
   1122     SUFF_DEBUG1("\tusing existing source %s\n", sgn->name);
   1123     return ret;
   1124 }
   1125 
   1126 static void
   1127 ExpandWildcards(GNodeListNode *cln, GNode *pgn)
   1128 {
   1129     GNode *cgn = cln->datum;
   1130     StringList *expansions;
   1131 
   1132     if (!Dir_HasWildcards(cgn->name))
   1133 	return;
   1134 
   1135     /*
   1136      * Expand the word along the chosen path
   1137      */
   1138     expansions = Lst_New();
   1139     Dir_Expand(cgn->name, Suff_FindPath(cgn), expansions);
   1140 
   1141     while (!Lst_IsEmpty(expansions)) {
   1142 	GNode	*gn;
   1143 	/*
   1144 	 * Fetch next expansion off the list and find its GNode
   1145 	 */
   1146 	char *cp = Lst_Dequeue(expansions);
   1147 
   1148 	SUFF_DEBUG1("%s...", cp);
   1149 	gn = Targ_GetNode(cp);
   1150 
   1151 	/* Add gn to the parents child list before the original child */
   1152 	Lst_InsertBefore(pgn->children, cln, gn);
   1153 	Lst_Append(gn->parents, pgn);
   1154 	pgn->unmade++;
   1155     }
   1156 
   1157     Lst_Free(expansions);
   1158 
   1159     SUFF_DEBUG0("\n");
   1160 
   1161     /*
   1162      * Now the source is expanded, remove it from the list of children to
   1163      * keep it from being processed.
   1164      */
   1165     pgn->unmade--;
   1166     Lst_Remove(pgn->children, cln);
   1167     Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
   1168 }
   1169 
   1170 /* Expand the names of any children of a given node that contain variable
   1171  * expressions or file wildcards into actual targets.
   1172  *
   1173  * The expanded node is removed from the parent's list of children, and the
   1174  * parent's unmade counter is decremented, but other nodes may be added.
   1175  *
   1176  * Input:
   1177  *	cln		Child to examine
   1178  *	pgn		Parent node being processed
   1179  */
   1180 static void
   1181 ExpandChildren(GNodeListNode *cln, GNode *pgn)
   1182 {
   1183     GNode *cgn = cln->datum;
   1184     GNode *gn;			/* New source 8) */
   1185     char *cp;			/* Expanded value */
   1186 
   1187     if (!Lst_IsEmpty(cgn->order_pred) || !Lst_IsEmpty(cgn->order_succ))
   1188 	/* It is all too hard to process the result of .ORDER */
   1189 	return;
   1190 
   1191     if (cgn->type & OP_WAIT)
   1192 	/* Ignore these (& OP_PHONY ?) */
   1193 	return;
   1194 
   1195     /*
   1196      * First do variable expansion -- this takes precedence over
   1197      * wildcard expansion. If the result contains wildcards, they'll be gotten
   1198      * to later since the resulting words are tacked on to the end of
   1199      * the children list.
   1200      */
   1201     if (strchr(cgn->name, '$') == NULL) {
   1202 	ExpandWildcards(cln, pgn);
   1203 	return;
   1204     }
   1205 
   1206     SUFF_DEBUG1("Expanding \"%s\"...", cgn->name);
   1207     (void)Var_Subst(cgn->name, pgn, VARE_WANTRES | VARE_UNDEFERR, &cp);
   1208     /* TODO: handle errors */
   1209 
   1210     {
   1211 	GNodeList *members = Lst_New();
   1212 
   1213 	if (cgn->type & OP_ARCHV) {
   1214 	    /*
   1215 	     * Node was an archive(member) target, so we want to call
   1216 	     * on the Arch module to find the nodes for us, expanding
   1217 	     * variables in the parent's context.
   1218 	     */
   1219 	    char	*sacrifice = cp;
   1220 
   1221 	    (void)Arch_ParseArchive(&sacrifice, members, pgn);
   1222 	} else {
   1223 	    /*
   1224 	     * Break the result into a vector of strings whose nodes
   1225 	     * we can find, then add those nodes to the members list.
   1226 	     * Unfortunately, we can't use Str_Words because it
   1227 	     * doesn't understand about variable specifications with
   1228 	     * spaces in them...
   1229 	     */
   1230 	    char	    *start;
   1231 	    char	    *initcp = cp;   /* For freeing... */
   1232 
   1233 	    start = cp;
   1234 	    pp_skip_hspace(&start);
   1235 	    cp = start;
   1236 	    while (*cp != '\0') {
   1237 		if (*cp == ' ' || *cp == '\t') {
   1238 		    /*
   1239 		     * White-space -- terminate element, find the node,
   1240 		     * add it, skip any further spaces.
   1241 		     */
   1242 		    *cp++ = '\0';
   1243 		    gn = Targ_GetNode(start);
   1244 		    Lst_Append(members, gn);
   1245 		    pp_skip_hspace(&cp);
   1246 		    start = cp;		/* Continue at the next non-space. */
   1247 		} else if (*cp == '$') {
   1248 		    /* Skip over the variable expression. */
   1249 		    const char *nested_p = cp;
   1250 		    const char	*junk;
   1251 		    void	*freeIt;
   1252 
   1253 		    (void)Var_Parse(&nested_p, pgn, VARE_NONE, &junk, &freeIt);
   1254 		    /* TODO: handle errors */
   1255 		    if (junk == var_Error) {
   1256 			Parse_Error(PARSE_FATAL,
   1257 				    "Malformed variable expression at \"%s\"",
   1258 				    cp);
   1259 			cp++;
   1260 		    } else {
   1261 			cp += nested_p - cp;
   1262 		    }
   1263 
   1264 		    free(freeIt);
   1265 		} else if (cp[0] == '\\' && cp[1] != '\0') {
   1266 		    /*
   1267 		     * Escaped something -- skip over it
   1268 		     */
   1269 		    /* XXX: In other places, escaping at this syntactical
   1270 		     * position is done by a '$', not a '\'.  The '\' is only
   1271 		     * used in variable modifiers. */
   1272 		    cp += 2;
   1273 		} else {
   1274 		    cp++;
   1275 		}
   1276 	    }
   1277 
   1278 	    if (cp != start) {
   1279 		/*
   1280 		 * Stuff left over -- add it to the list too
   1281 		 */
   1282 		gn = Targ_GetNode(start);
   1283 		Lst_Append(members, gn);
   1284 	    }
   1285 	    /*
   1286 	     * Point cp back at the beginning again so the variable value
   1287 	     * can be freed.
   1288 	     */
   1289 	    cp = initcp;
   1290 	}
   1291 
   1292 	/*
   1293 	 * Add all elements of the members list to the parent node.
   1294 	 */
   1295 	while(!Lst_IsEmpty(members)) {
   1296 	    gn = Lst_Dequeue(members);
   1297 
   1298 	    SUFF_DEBUG1("%s...", gn->name);
   1299 	    /* Add gn to the parents child list before the original child */
   1300 	    Lst_InsertBefore(pgn->children, cln, gn);
   1301 	    Lst_Append(gn->parents, pgn);
   1302 	    pgn->unmade++;
   1303 	    /* Expand wildcards on new node */
   1304 	    ExpandWildcards(cln->prev, pgn);
   1305 	}
   1306 	Lst_Free(members);
   1307 
   1308 	/*
   1309 	 * Free the result
   1310 	 */
   1311 	free(cp);
   1312     }
   1313 
   1314     SUFF_DEBUG0("\n");
   1315 
   1316     /*
   1317      * Now the source is expanded, remove it from the list of children to
   1318      * keep it from being processed.
   1319      */
   1320     pgn->unmade--;
   1321     Lst_Remove(pgn->children, cln);
   1322     Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
   1323 }
   1324 
   1325 static void
   1326 ExpandAllChildren(GNode *gn)
   1327 {
   1328     GNodeListNode *ln, *nln;
   1329 
   1330     for (ln = gn->children->first; ln != NULL; ln = nln) {
   1331 	nln = ln->next;
   1332 	ExpandChildren(ln, gn);
   1333     }
   1334 }
   1335 
   1336 /* Find a path along which to expand the node.
   1337  *
   1338  * If the node has a known suffix, use that path.
   1339  * If it has no known suffix, use the default system search path.
   1340  *
   1341  * Input:
   1342  *	gn		Node being examined
   1343  *
   1344  * Results:
   1345  *	The appropriate path to search for the GNode.
   1346  */
   1347 SearchPath *
   1348 Suff_FindPath(GNode* gn)
   1349 {
   1350     Suffix *suff = gn->suffix;
   1351 
   1352     if (suff == NULL) {
   1353 	char *name = gn->name;
   1354 	size_t nameLen = strlen(gn->name);
   1355 	SuffixListNode *ln;
   1356 	for (ln = sufflist->first; ln != NULL; ln = ln->next)
   1357 	    if (Suffix_IsSuffix(ln->datum, nameLen, name + nameLen))
   1358 		break;
   1359 
   1360 	SUFF_DEBUG1("Wildcard expanding \"%s\"...", gn->name);
   1361 	if (ln != NULL)
   1362 	    suff = ln->datum;
   1363 	/* XXX: Here we can save the suffix so we don't have to do this again */
   1364     }
   1365 
   1366     if (suff != NULL) {
   1367 	SUFF_DEBUG1("suffix is \"%s\"...\n", suff->name);
   1368 	return suff->searchPath;
   1369     } else {
   1370 	SUFF_DEBUG0("\n");
   1371 	return dirSearchPath;	/* Use default search path */
   1372     }
   1373 }
   1374 
   1375 /* Apply a transformation rule, given the source and target nodes and
   1376  * suffixes.
   1377  *
   1378  * The source and target are linked and the commands from the transformation
   1379  * are added to the target node's commands list. The target also inherits all
   1380  * the sources for the transformation rule.
   1381  *
   1382  * Results:
   1383  *	TRUE if successful, FALSE if not.
   1384  */
   1385 static Boolean
   1386 ApplyTransform(GNode *tgn, GNode *sgn, Suffix *tsuff, Suffix *ssuff)
   1387 {
   1388     GNodeListNode *ln;
   1389     char *tname;		/* Name of transformation rule */
   1390     GNode *gn;			/* Node for same */
   1391 
   1392     /*
   1393      * Form the proper links between the target and source.
   1394      */
   1395     Lst_Append(tgn->children, sgn);
   1396     Lst_Append(sgn->parents, tgn);
   1397     tgn->unmade++;
   1398 
   1399     /*
   1400      * Locate the transformation rule itself
   1401      */
   1402     tname = str_concat2(ssuff->name, tsuff->name);
   1403     gn = FindTransformByName(tname);
   1404     free(tname);
   1405 
   1406     if (gn == NULL) {
   1407 	/* This can happen when linking an OP_MEMBER and OP_ARCHV node. */
   1408 	return FALSE;
   1409     }
   1410 
   1411     DEBUG3(SUFF,"\tapplying %s -> %s to \"%s\"\n",
   1412 		ssuff->name, tsuff->name, tgn->name);
   1413 
   1414     /* Record last child; Make_HandleUse may add child nodes. */
   1415     ln = tgn->children->last;
   1416 
   1417     /* Apply the rule. */
   1418     Make_HandleUse(gn, tgn);
   1419 
   1420     /* Deal with wildcards and variables in any acquired sources. */
   1421     ln = ln != NULL ? ln->next : NULL;
   1422     while (ln != NULL) {
   1423 	GNodeListNode *nln = ln->next;
   1424 	ExpandChildren(ln, tgn);
   1425 	ln = nln;
   1426     }
   1427 
   1428     /*
   1429      * Keep track of another parent to which this node is transformed so
   1430      * the .IMPSRC variable can be set correctly for the parent.
   1431      */
   1432     Lst_Append(sgn->implicitParents, tgn);
   1433 
   1434     return TRUE;
   1435 }
   1436 
   1437 /*
   1438  * Member has a known suffix, so look for a transformation rule from
   1439  * it to a possible suffix of the archive.
   1440  *
   1441  * Rather than searching through the entire list, we just look at
   1442  * suffixes to which the member's suffix may be transformed.
   1443  */
   1444 static void
   1445 ExpandMember(GNode *gn, const char *eoarch, GNode *mem, Suffix *memSuff)
   1446 {
   1447     GNodeListNode *ln;
   1448     size_t nameLen = (size_t)(eoarch - gn->name);
   1449 
   1450     /* Use first matching suffix... */
   1451     for (ln = memSuff->parents->first; ln != NULL; ln = ln->next)
   1452 	if (Suffix_IsSuffix(ln->datum, nameLen, eoarch))
   1453 	    break;
   1454 
   1455     if (ln != NULL) {
   1456 	/* Got one -- apply it */
   1457 	Suffix *suff = ln->datum;
   1458 	if (!ApplyTransform(gn, mem, suff, memSuff)) {
   1459 	    SUFF_DEBUG2("\tNo transformation from %s -> %s\n",
   1460 			memSuff->name, suff->name);
   1461 	}
   1462     }
   1463 }
   1464 
   1465 static void FindDeps(GNode *, CandidateList *);
   1466 
   1467 /* Locate dependencies for an OP_ARCHV node.
   1468  *
   1469  * Input:
   1470  *	gn		Node for which to locate dependencies
   1471  *
   1472  * Side Effects:
   1473  *	Same as Suff_FindDeps
   1474  */
   1475 static void
   1476 FindDepsArchive(GNode *gn, CandidateList *slst)
   1477 {
   1478     char *eoarch;		/* End of archive portion */
   1479     char *eoname;		/* End of member portion */
   1480     GNode *mem;			/* Node for member */
   1481     Suffix *memSuff;
   1482     const char *name;		/* Start of member's name */
   1483 
   1484     /*
   1485      * The node is an archive(member) pair. so we must find a
   1486      * suffix for both of them.
   1487      */
   1488     eoarch = strchr(gn->name, '(');
   1489     eoname = strchr(eoarch, ')');
   1490 
   1491     /*
   1492      * Caller guarantees the format `libname(member)', via
   1493      * Arch_ParseArchive.
   1494      */
   1495     assert(eoarch != NULL);
   1496     assert(eoname != NULL);
   1497 
   1498     *eoname = '\0';	  /* Nuke parentheses during suffix search */
   1499     *eoarch = '\0';	  /* So a suffix can be found */
   1500 
   1501     name = eoarch + 1;
   1502 
   1503     /*
   1504      * To simplify things, call Suff_FindDeps recursively on the member now,
   1505      * so we can simply compare the member's .PREFIX and .TARGET variables
   1506      * to locate its suffix. This allows us to figure out the suffix to
   1507      * use for the archive without having to do a quadratic search over the
   1508      * suffix list, backtracking for each one...
   1509      */
   1510     mem = Targ_GetNode(name);
   1511     FindDeps(mem, slst);
   1512 
   1513     /*
   1514      * Create the link between the two nodes right off
   1515      */
   1516     Lst_Append(gn->children, mem);
   1517     Lst_Append(mem->parents, gn);
   1518     gn->unmade++;
   1519 
   1520     /*
   1521      * Copy in the variables from the member node to this one.
   1522      */
   1523     Var_Set(PREFIX, GNode_VarPrefix(mem), gn);
   1524     Var_Set(TARGET, GNode_VarTarget(mem), gn);
   1525 
   1526     memSuff = mem->suffix;
   1527     if (memSuff == NULL) {	/* Didn't know what it was. */
   1528 	SUFF_DEBUG0("using null suffix\n");
   1529 	memSuff = nullSuff;
   1530     }
   1531 
   1532 
   1533     /*
   1534      * Set the other two local variables required for this target.
   1535      */
   1536     Var_Set(MEMBER, name, gn);
   1537     Var_Set(ARCHIVE, gn->name, gn);
   1538 
   1539     /*
   1540      * Set $@ for compatibility with other makes
   1541      */
   1542     Var_Set(TARGET, gn->name, gn);
   1543 
   1544     /*
   1545      * Now we've got the important local variables set, expand any sources
   1546      * that still contain variables or wildcards in their names.
   1547      */
   1548     ExpandAllChildren(gn);
   1549 
   1550     if (memSuff != NULL)
   1551 	ExpandMember(gn, eoarch, mem, memSuff);
   1552 
   1553     /*
   1554      * Replace the opening and closing parens now we've no need of the separate
   1555      * pieces.
   1556      */
   1557     *eoarch = '(';
   1558     *eoname = ')';
   1559 
   1560     /*
   1561      * Pretend gn appeared to the left of a dependency operator so
   1562      * the user needn't provide a transformation from the member to the
   1563      * archive.
   1564      */
   1565     if (!GNode_IsTarget(gn))
   1566 	gn->type |= OP_DEPENDS;
   1567 
   1568     /*
   1569      * Flag the member as such so we remember to look in the archive for
   1570      * its modification time. The OP_JOIN | OP_MADE is needed because this
   1571      * target should never get made.
   1572      */
   1573     mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
   1574 }
   1575 
   1576 /*
   1577  * If the node is a library, it is the arch module's job to find it
   1578  * and set the TARGET variable accordingly. We merely provide the
   1579  * search path, assuming all libraries end in ".a" (if the suffix
   1580  * hasn't been defined, there's nothing we can do for it, so we just
   1581  * set the TARGET variable to the node's name in order to give it a
   1582  * value).
   1583  */
   1584 static void
   1585 FindDepsLib(GNode *gn)
   1586 {
   1587     Suffix *suff = FindSuffixByName(LIBSUFF);
   1588     if (suff != NULL) {
   1589 	Suffix_Reassign(&gn->suffix, suff);
   1590 	Arch_FindLib(gn, suff->searchPath);
   1591     } else {
   1592 	Suffix_Unassign(&gn->suffix);
   1593 	Var_Set(TARGET, gn->name, gn);
   1594     }
   1595 
   1596     /*
   1597      * Because a library (-lfoo) target doesn't follow the standard
   1598      * filesystem conventions, we don't set the regular variables for
   1599      * the thing. .PREFIX is simply made empty.
   1600      */
   1601     Var_Set(PREFIX, "", gn);
   1602 }
   1603 
   1604 static void
   1605 FindDepsRegularKnown(const char *name, size_t nameLen, GNode *gn,
   1606 		     CandidateList *srcs, CandidateList *targs)
   1607 {
   1608     SuffixListNode *ln;
   1609     Candidate *targ;
   1610     char *pref;
   1611 
   1612     for (ln = sufflist->first; ln != NULL; ln = ln->next) {
   1613 	Suffix *suff = ln->datum;
   1614 	if (!Suffix_IsSuffix(suff, nameLen, name + nameLen))
   1615 	    continue;
   1616 
   1617 	pref = bmake_strldup(name, (size_t)(nameLen - suff->nameLen));
   1618 	targ = Candidate_New(bmake_strdup(gn->name), pref, suff, NULL, gn);
   1619 
   1620 	CandidateList_AddCandidatesFor(srcs, targ);
   1621 
   1622 	/*
   1623 	 * Record the target so we can nuke it
   1624 	 */
   1625 	Lst_Append(targs, targ);
   1626     }
   1627 }
   1628 
   1629 static void
   1630 FindDepsRegularUnknown(GNode *gn, const char *sopref,
   1631 		       CandidateList *srcs, CandidateList *targs)
   1632 {
   1633     Candidate *targ;
   1634 
   1635     if (!Lst_IsEmpty(targs) || nullSuff == NULL)
   1636 	return;
   1637 
   1638     SUFF_DEBUG1("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
   1639 
   1640     targ = Candidate_New(bmake_strdup(gn->name), bmake_strdup(sopref),
   1641 		  nullSuff, NULL, gn);
   1642 
   1643     /*
   1644      * Only use the default suffix rules if we don't have commands
   1645      * defined for this gnode; traditional make programs used to
   1646      * not define suffix rules if the gnode had children but we
   1647      * don't do this anymore.
   1648      */
   1649     if (Lst_IsEmpty(gn->commands))
   1650 	CandidateList_AddCandidatesFor(srcs, targ);
   1651     else {
   1652 	SUFF_DEBUG0("not ");
   1653     }
   1654 
   1655     SUFF_DEBUG0("adding suffix rules\n");
   1656 
   1657     Lst_Append(targs, targ);
   1658 }
   1659 
   1660 /*
   1661  * Deal with finding the thing on the default search path. We
   1662  * always do that, not only if the node is only a source (not
   1663  * on the lhs of a dependency operator or [XXX] it has neither
   1664  * children or commands) as the old pmake did.
   1665  */
   1666 static void
   1667 FindDepsRegularPath(GNode *gn, Candidate *targ)
   1668 {
   1669     if (gn->type & (OP_PHONY | OP_NOPATH))
   1670 	return;
   1671 
   1672     free(gn->path);
   1673     gn->path = Dir_FindFile(gn->name,
   1674 			    (targ == NULL ? dirSearchPath :
   1675 			     targ->suff->searchPath));
   1676     if (gn->path == NULL)
   1677 	return;
   1678 
   1679     Var_Set(TARGET, gn->path, gn);
   1680 
   1681     if (targ != NULL) {
   1682 	/*
   1683 	 * Suffix known for the thing -- trim the suffix off
   1684 	 * the path to form the proper .PREFIX variable.
   1685 	 */
   1686 	size_t savep = strlen(gn->path) - targ->suff->nameLen;
   1687 	char savec;
   1688 	char *ptr;
   1689 
   1690 	Suffix_Reassign(&gn->suffix, targ->suff);
   1691 
   1692 	savec = gn->path[savep];
   1693 	gn->path[savep] = '\0';
   1694 
   1695 	if ((ptr = strrchr(gn->path, '/')) != NULL)
   1696 	    ptr++;
   1697 	else
   1698 	    ptr = gn->path;
   1699 
   1700 	Var_Set(PREFIX, ptr, gn);
   1701 
   1702 	gn->path[savep] = savec;
   1703     } else {
   1704 	char *ptr;
   1705 
   1706 	/* The .PREFIX gets the full path if the target has no known suffix. */
   1707 	Suffix_Unassign(&gn->suffix);
   1708 
   1709 	if ((ptr = strrchr(gn->path, '/')) != NULL)
   1710 	    ptr++;
   1711 	else
   1712 	    ptr = gn->path;
   1713 
   1714 	Var_Set(PREFIX, ptr, gn);
   1715     }
   1716 }
   1717 
   1718 /* Locate implicit dependencies for regular targets.
   1719  *
   1720  * Input:
   1721  *	gn		Node for which to find sources
   1722  *
   1723  * Side Effects:
   1724  *	Same as Suff_FindDeps
   1725  */
   1726 static void
   1727 FindDepsRegular(GNode *gn, CandidateList *slst)
   1728 {
   1729     CandidateList *srcs;	/* List of sources at which to look */
   1730     CandidateList *targs;	/* List of targets to which things can be
   1731 				 * transformed. They all have the same file,
   1732 				 * but different suff and prefix fields */
   1733     Candidate *bottom;		/* Start of found transformation path */
   1734     Candidate *src;
   1735     Candidate *targ;
   1736 
   1737     const char *name = gn->name;
   1738     size_t nameLen = strlen(name);
   1739 
   1740     /*
   1741      * Begin at the beginning...
   1742      */
   1743     srcs = Lst_New();
   1744     targs = Lst_New();
   1745 
   1746     /*
   1747      * We're caught in a catch-22 here. On the one hand, we want to use any
   1748      * transformation implied by the target's sources, but we can't examine
   1749      * the sources until we've expanded any variables/wildcards they may hold,
   1750      * and we can't do that until we've set up the target's local variables
   1751      * and we can't do that until we know what the proper suffix for the
   1752      * target is (in case there are two suffixes one of which is a suffix of
   1753      * the other) and we can't know that until we've found its implied
   1754      * source, which we may not want to use if there's an existing source
   1755      * that implies a different transformation.
   1756      *
   1757      * In an attempt to get around this, which may not work all the time,
   1758      * but should work most of the time, we look for implied sources first,
   1759      * checking transformations to all possible suffixes of the target,
   1760      * use what we find to set the target's local variables, expand the
   1761      * children, then look for any overriding transformations they imply.
   1762      * Should we find one, we discard the one we found before.
   1763      */
   1764     bottom = NULL;
   1765     targ = NULL;
   1766 
   1767     if (!(gn->type & OP_PHONY)) {
   1768 
   1769 	FindDepsRegularKnown(name, nameLen, gn, srcs, targs);
   1770 
   1771 	/* Handle target of unknown suffix... */
   1772 	FindDepsRegularUnknown(gn, name, srcs, targs);
   1773 
   1774 	/*
   1775 	 * Using the list of possible sources built up from the target
   1776 	 * suffix(es), try and find an existing file/target that matches.
   1777 	 */
   1778 	bottom = FindThem(srcs, slst);
   1779 
   1780 	if (bottom == NULL) {
   1781 	    /*
   1782 	     * No known transformations -- use the first suffix found
   1783 	     * for setting the local variables.
   1784 	     */
   1785 	    if (targs->first != NULL)
   1786 		targ = targs->first->datum;
   1787 	    else
   1788 		targ = NULL;
   1789 	} else {
   1790 	    /*
   1791 	     * Work up the transformation path to find the suffix of the
   1792 	     * target to which the transformation was made.
   1793 	     */
   1794 	    for (targ = bottom; targ->parent != NULL; targ = targ->parent)
   1795 		continue;
   1796 	}
   1797     }
   1798 
   1799     Var_Set(TARGET, GNode_Path(gn), gn);
   1800     Var_Set(PREFIX, targ != NULL ? targ->prefix : gn->name, gn);
   1801 
   1802     /*
   1803      * Now we've got the important local variables set, expand any sources
   1804      * that still contain variables or wildcards in their names.
   1805      */
   1806     {
   1807 	GNodeListNode *ln, *nln;
   1808 	for (ln = gn->children->first; ln != NULL; ln = nln) {
   1809 	    nln = ln->next;
   1810 	    ExpandChildren(ln, gn);
   1811 	}
   1812     }
   1813 
   1814     if (targ == NULL) {
   1815 	SUFF_DEBUG1("\tNo valid suffix on %s\n", gn->name);
   1816 
   1817 sfnd_abort:
   1818 	FindDepsRegularPath(gn, targ);
   1819 	goto sfnd_return;
   1820     }
   1821 
   1822     /*
   1823      * If the suffix indicates that the target is a library, mark that in
   1824      * the node's type field.
   1825      */
   1826     if (targ->suff->flags & SUFF_LIBRARY)
   1827 	gn->type |= OP_LIB;
   1828 
   1829     /*
   1830      * Check for overriding transformation rule implied by sources
   1831      */
   1832     if (!Lst_IsEmpty(gn->children)) {
   1833 	src = FindCmds(targ, slst);
   1834 
   1835 	if (src != NULL) {
   1836 	    /*
   1837 	     * Free up all the candidates in the transformation path,
   1838 	     * up to but not including the parent node.
   1839 	     */
   1840 	    while (bottom != NULL && bottom->parent != NULL) {
   1841 		if (Lst_FindDatum(slst, bottom) == NULL)
   1842 		    Lst_Append(slst, bottom);
   1843 		bottom = bottom->parent;
   1844 	    }
   1845 	    bottom = src;
   1846 	}
   1847     }
   1848 
   1849     if (bottom == NULL) {
   1850 	/*
   1851 	 * No idea from where it can come -- return now.
   1852 	 */
   1853 	goto sfnd_abort;
   1854     }
   1855 
   1856     /*
   1857      * We now have a list of candidates headed by 'bottom' and linked via
   1858      * their 'parent' pointers. What we do next is create links between
   1859      * source and target nodes (which may or may not have been created)
   1860      * and set the necessary local variables in each target.
   1861      *
   1862      * The commands for each target are set from the commands of the
   1863      * transformation rule used to get from the src suffix to the targ
   1864      * suffix. Note that this causes the commands list of the original
   1865      * node, gn, to be replaced with the commands of the final
   1866      * transformation rule.
   1867      */
   1868     if (bottom->node == NULL)
   1869 	bottom->node = Targ_GetNode(bottom->file);
   1870 
   1871     for (src = bottom; src->parent != NULL; src = src->parent) {
   1872 	targ = src->parent;
   1873 
   1874 	Suffix_Reassign(&src->node->suffix, src->suff);
   1875 
   1876 	if (targ->node == NULL)
   1877 	    targ->node = Targ_GetNode(targ->file);
   1878 
   1879 	ApplyTransform(targ->node, src->node,
   1880 			   targ->suff, src->suff);
   1881 
   1882 	if (targ->node != gn) {
   1883 	    /*
   1884 	     * Finish off the dependency-search process for any nodes
   1885 	     * between bottom and gn (no point in questing around the
   1886 	     * filesystem for their implicit source when it's already
   1887 	     * known). Note that the node can't have any sources that
   1888 	     * need expanding, since SuffFindThem will stop on an existing
   1889 	     * node, so all we need to do is set the standard variables.
   1890 	     */
   1891 	    targ->node->type |= OP_DEPS_FOUND;
   1892 	    Var_Set(PREFIX, targ->prefix, targ->node);
   1893 	    Var_Set(TARGET, targ->node->name, targ->node);
   1894 	}
   1895     }
   1896 
   1897     Suffix_Reassign(&gn->suffix, src->suff);
   1898 
   1899     /*
   1900      * Nuke the transformation path and the candidates left over in the
   1901      * two lists.
   1902      */
   1903 sfnd_return:
   1904     if (bottom != NULL && Lst_FindDatum(slst, bottom) == NULL)
   1905 	Lst_Append(slst, bottom);
   1906 
   1907     while (RemoveCandidate(srcs) || RemoveCandidate(targs))
   1908 	continue;
   1909 
   1910     Lst_MoveAll(slst, srcs);
   1911     Lst_MoveAll(slst, targs);
   1912 }
   1913 
   1914 
   1915 /* Find implicit sources for the target.
   1916  *
   1917  * Nodes are added to the graph below the passed-in node. The nodes are
   1918  * marked to have their IMPSRC variable filled in. The PREFIX variable is set
   1919  * for the given node and all its implied children.
   1920  *
   1921  * The path found by this target is the shortest path in the transformation
   1922  * graph, which may pass through non-existent targets, to an existing target.
   1923  * The search continues on all paths from the root suffix until a file is
   1924  * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
   1925  * .l,v file exists but the .c and .l files don't, the search will branch out
   1926  * in all directions from .o and again from all the nodes on the next level
   1927  * until the .l,v node is encountered.
   1928  */
   1929 void
   1930 Suff_FindDeps(GNode *gn)
   1931 {
   1932     CandidateList *srcs = Lst_New();
   1933 
   1934     FindDeps(gn, srcs);
   1935 
   1936     while (RemoveCandidate(srcs))
   1937 	continue;
   1938 
   1939     assert(Lst_IsEmpty(srcs));
   1940     Lst_Free(srcs);
   1941 }
   1942 
   1943 static void
   1944 FindDeps(GNode *gn, CandidateList *slst)
   1945 {
   1946     if (gn->type & OP_DEPS_FOUND)
   1947 	return;
   1948     gn->type |= OP_DEPS_FOUND;
   1949 
   1950     /*
   1951      * Make sure we have these set, may get revised below.
   1952      */
   1953     Var_Set(TARGET, GNode_Path(gn), gn);
   1954     Var_Set(PREFIX, gn->name, gn);
   1955 
   1956     SUFF_DEBUG1("SuffFindDeps (%s)\n", gn->name);
   1957 
   1958     if (gn->type & OP_ARCHV)
   1959 	FindDepsArchive(gn, slst);
   1960     else if (gn->type & OP_LIB)
   1961 	FindDepsLib(gn);
   1962     else
   1963 	FindDepsRegular(gn, slst);
   1964 }
   1965 
   1966 /* Define which suffix is the null suffix.
   1967  *
   1968  * Need to handle the changing of the null suffix gracefully so the old
   1969  * transformation rules don't just go away.
   1970  *
   1971  * Input:
   1972  *	name		Name of null suffix
   1973  */
   1974 void
   1975 Suff_SetNull(const char *name)
   1976 {
   1977     Suffix *suff = FindSuffixByName(name);
   1978     if (suff == NULL) {
   1979 	Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
   1980 		    name);
   1981 	return;
   1982     }
   1983 
   1984     if (nullSuff != NULL)
   1985 	nullSuff->flags &= ~(unsigned)SUFF_NULL;
   1986     suff->flags |= SUFF_NULL;
   1987     /*
   1988      * XXX: Here's where the transformation mangling would take place
   1989      */
   1990     nullSuff = suff;
   1991 }
   1992 
   1993 /* Initialize the suffixes module. */
   1994 void
   1995 Suff_Init(void)
   1996 {
   1997 #ifdef CLEANUP
   1998     suffClean = Lst_New();
   1999     sufflist = Lst_New();
   2000 #endif
   2001     transforms = Lst_New();
   2002 
   2003     /*
   2004      * Create null suffix for single-suffix rules (POSIX). The thing doesn't
   2005      * actually go on the suffix list or everyone will think that's its
   2006      * suffix.
   2007      */
   2008     Suff_ClearSuffixes();
   2009 }
   2010 
   2011 
   2012 /* Clean up the suffixes module. */
   2013 void
   2014 Suff_End(void)
   2015 {
   2016 #ifdef CLEANUP
   2017     Lst_Destroy(sufflist, SuffFree);
   2018     Lst_Destroy(suffClean, SuffFree);
   2019     if (nullSuff != NULL)
   2020 	SuffFree(nullSuff);
   2021     Lst_Free(transforms);
   2022 #endif
   2023 }
   2024 
   2025 
   2026 static void
   2027 PrintSuffNames(const char *prefix, SuffixList *suffs)
   2028 {
   2029     SuffixListNode *ln;
   2030 
   2031     debug_printf("#\t%s: ", prefix);
   2032     for (ln = suffs->first; ln != NULL; ln = ln->next) {
   2033 	Suffix *suff = ln->datum;
   2034 	debug_printf("%s ", suff->name);
   2035     }
   2036     debug_printf("\n");
   2037 }
   2038 
   2039 static void
   2040 Suffix_Print(Suffix *suff)
   2041 {
   2042     debug_printf("# \"%s\" (num %d, ref %d)",
   2043 		 suff->name, suff->sNum, suff->refCount);
   2044     if (suff->flags != 0) {
   2045 	char flags_buf[SuffixFlags_ToStringSize];
   2046 
   2047 	debug_printf(" (%s)",
   2048 		     Enum_FlagsToString(flags_buf, sizeof flags_buf,
   2049 					suff->flags,
   2050 					SuffixFlags_ToStringSpecs));
   2051     }
   2052     debug_printf("\n");
   2053 
   2054     PrintSuffNames("To", suff->parents);
   2055     PrintSuffNames("From", suff->children);
   2056 
   2057     debug_printf("#\tSearch Path: ");
   2058     Dir_PrintPath(suff->searchPath);
   2059     debug_printf("\n");
   2060 }
   2061 
   2062 static void
   2063 PrintTransformation(GNode *t)
   2064 {
   2065     debug_printf("%-16s:", t->name);
   2066     Targ_PrintType(t->type);
   2067     debug_printf("\n");
   2068     Targ_PrintCmds(t);
   2069     debug_printf("\n");
   2070 }
   2071 
   2072 void
   2073 Suff_PrintAll(void)
   2074 {
   2075     debug_printf("#*** Suffixes:\n");
   2076     {
   2077 	SuffixListNode *ln;
   2078 	for (ln = sufflist->first; ln != NULL; ln = ln->next)
   2079 	    Suffix_Print(ln->datum);
   2080     }
   2081 
   2082     debug_printf("#*** Transformations:\n");
   2083     {
   2084 	GNodeListNode *ln;
   2085 	for (ln = transforms->first; ln != NULL; ln = ln->next)
   2086 	    PrintTransformation(ln->datum);
   2087     }
   2088 }
   2089