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