1 /* $NetBSD: suff.c,v 1.142 2020/08/31 16:44:25 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid[] = "$NetBSD: suff.c,v 1.142 2020/08/31 16:44:25 rillig Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94";
79 __RCSID("$NetBSD: suff.c,v 1.142 2020/08/31 16:44:25 rillig Exp $");
86 * Functions to maintain suffix lists and find implicit dependents
87 * using suffix transformation rules
90 * Suff_Init Initialize all things to do with suffixes.
92 * Suff_End Cleanup the module
94 * Suff_DoPaths This function is used to make life easier
95 * when searching for a file according to its
96 * suffix. It takes the global search path,
97 * as defined using the .PATH: target, and appends
98 * its directories to the path of each of the
99 * defined suffixes, as specified using
100 * .PATH<suffix>: targets. In addition, all
101 * directories given for suffixes labeled as
102 * include files or libraries, using the .INCLUDES
103 * or .LIBS targets, are played with using
104 * Dir_MakeFlags to create the .INCLUDES and
105 * .LIBS global variables.
107 * Suff_ClearSuffixes Clear out all the suffixes and defined
110 * Suff_IsTransform Return TRUE if the passed string is the lhs
111 * of a transformation rule.
113 * Suff_AddSuffix Add the passed string as another known suffix.
115 * Suff_GetPath Return the search path for the given suffix.
117 * Suff_AddInclude Mark the given suffix as denoting an include
120 * Suff_AddLib Mark the given suffix as denoting a library.
122 * Suff_AddTransform Add another transformation to the suffix
123 * graph. Returns GNode suitable for framing, I
124 * mean, tacking commands, attributes, etc. on.
126 * Suff_SetNull Define the suffix to consider the suffix of
127 * any file that doesn't have a known one.
129 * Suff_FindDeps Find implicit sources for and the location of
130 * a target based on its suffix. Returns the
131 * bottom-most node added to the graph or NULL
132 * if the target had no implicit sources.
134 * Suff_FindPath Return the appropriate path to search in
135 * order to find the node.
141 #define SUFF_DEBUG0(fmt) \
142 if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt)
144 #define SUFF_DEBUG1(fmt, arg1) \
145 if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt, arg1)
147 #define SUFF_DEBUG2(fmt, arg1, arg2) \
148 if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt, arg1, arg2)
150 #define SUFF_DEBUG3(fmt, arg1, arg2, arg3) \
151 if (!DEBUG(SUFF)) (void) 0; else fprintf(debug_file, fmt, arg1, arg2, arg3)
153 static Lst sufflist; /* Lst of suffixes */
155 static Lst suffClean; /* Lst of suffixes to be cleaned */
157 static Lst srclist; /* Lst of sources */
158 static Lst transforms; /* Lst of transformation rules */
160 static int sNum = 0; /* Counter for assigning suffix numbers */
163 SUFF_INCLUDE = 0x01, /* One which is #include'd */
164 SUFF_LIBRARY = 0x02, /* One which contains a library */
165 SUFF_NULL = 0x04 /* The empty suffix */
166 /* XXX: Why is SUFF_NULL needed? Wouldn't nameLen == 0 mean the same? */
169 ENUM_FLAGS_RTTI_3(SuffFlags,
170 SUFF_INCLUDE, SUFF_LIBRARY, SUFF_NULL);
173 * Structure describing an individual suffix.
175 typedef struct Suff {
176 char *name; /* The suffix itself, such as ".c" */
177 int nameLen; /* Length of the name, to avoid strlen calls */
178 SuffFlags flags; /* Type of suffix */
179 Lst searchPath; /* The path along which files of this suffix
181 int sNum; /* The suffix number */
182 int refCount; /* Reference count of list membership */
183 Lst parents; /* Suffixes we have a transformation to */
184 Lst children; /* Suffixes we have a transformation from */
185 Lst ref; /* List of lists this suffix is referenced */
189 * Structure used in the search for implied sources.
191 typedef struct _Src {
192 char *file; /* The file to look for */
193 char *pref; /* Prefix from which file was formed */
194 Suff *suff; /* The suffix on the file */
195 struct _Src *parent; /* The Src for which this is a source */
196 GNode *node; /* The node describing the file */
197 int children; /* Count of existing children (so we don't free
198 * this thing too early or never nuke it) */
200 Lst cp; /* Debug; children list */
205 * A structure for passing more than one argument to the Lst-library-invoked
219 static Suff *suffNull; /* The NULL suffix for this run */
220 static Suff *emptySuff; /* The empty suffix required for POSIX
221 * single-suffix transformation rules */
224 static void SuffUnRef(void *, void *);
225 static void SuffFree(void *);
226 static void SuffInsert(Lst, Suff *);
227 static void SuffRemove(Lst, Suff *);
228 static Boolean SuffParseTransform(char *, Suff **, Suff **);
229 static int SuffRebuildGraph(void *, void *);
230 static int SuffScanTargets(void *, void *);
231 static int SuffAddSrc(void *, void *);
232 static void SuffAddLevel(Lst, Src *);
233 static void SuffExpandChildren(LstNode, GNode *);
234 static void SuffExpandWildcards(LstNode, GNode *);
235 static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
236 static void SuffFindDeps(GNode *, Lst);
237 static void SuffFindArchiveDeps(GNode *, Lst);
238 static void SuffFindNormalDeps(GNode *, Lst);
239 static int SuffPrintName(void *, void *);
240 static int SuffPrintSuff(void *, void *);
241 static int SuffPrintTrans(void *, void *);
243 /*************** Lst Predicates ****************/
245 *-----------------------------------------------------------------------
247 * See if pref is a prefix of str.
250 * pref possible prefix
251 * str string to check
254 * NULL if it ain't, pointer to character in str after prefix if so
258 *-----------------------------------------------------------------------
261 SuffStrIsPrefix(const char *pref, const char *str)
263 while (*str && *pref == *str) {
268 return *pref ? NULL : str;
272 char *ename; /* The end of the name */
273 int len; /* Length of the name */
274 } SuffSuffGetSuffixArgs;
276 /* See if suff is a suffix of str. str->ename should point to THE END
277 * of the string to check. (THE END == the null byte)
281 * str string to examine
284 * NULL if it ain't, pointer to character in str before suffix if
288 SuffSuffGetSuffix(const Suff *s, const SuffSuffGetSuffixArgs *str)
290 char *p1; /* Pointer into suffix name */
291 char *p2; /* Pointer into string being examined */
293 if (str->len < s->nameLen)
294 return NULL; /* this string is shorter than the suffix */
296 p1 = s->name + s->nameLen;
299 while (p1 >= s->name && *p1 == *p2) {
304 return p1 == s->name - 1 ? p2 : NULL;
307 /* Predicate form of SuffSuffGetSuffix, for Lst_Find. */
309 SuffSuffIsSuffix(const void *s, const void *sd)
311 return SuffSuffGetSuffix(s, sd) != NULL;
314 /* See if the suffix has the desired name. */
316 SuffSuffHasName(const void *s, const void *desiredName)
318 return strcmp(((const Suff *)s)->name, desiredName) == 0;
321 /* See if the suffix name is a prefix of the string. Care must be taken when
322 * using this to search for transformations and what-not, since there could
323 * well be two suffixes, one of which is a prefix of the other... */
325 SuffSuffIsPrefix(const void *s, const void *str)
327 return SuffStrIsPrefix(((const Suff *)s)->name, str) != NULL;
330 /* See if the graph node has the desired name. */
332 SuffGNHasName(const void *gn, const void *desiredName)
334 return strcmp(((const GNode *)gn)->name, desiredName) == 0;
337 /*********** Maintenance Functions ************/
340 SuffUnRef(void *lp, void *sp)
344 LstNode ln = Lst_FindDatum(l, sp);
347 ((Suff *)sp)->refCount--;
351 /* Free up all memory associated with the given suffix structure. */
355 Suff *s = (Suff *)sp;
364 /* We don't delete suffixes in order, so we cannot use this */
366 Punt("Internal error deleting suffix `%s' with refcount = %d", s->name,
371 Lst_Free(s->children);
372 Lst_Free(s->parents);
373 Lst_Destroy(s->searchPath, Dir_Destroy);
379 /* Remove the suffix from the list, and free if it is otherwise unused. */
381 SuffRemove(Lst l, Suff *s)
384 if (s->refCount == 0) {
385 SuffUnRef(sufflist, s);
390 /* Insert the suffix into the list keeping the list ordered by suffix numbers.
393 * l the list where in s should be inserted
394 * s the suffix to insert
397 SuffInsert(Lst l, Suff *s)
399 LstNode ln; /* current element in l we're examining */
400 Suff *s2 = NULL; /* the suffix descriptor in this element */
403 while ((ln = Lst_Next(l)) != NULL) {
404 s2 = LstNode_Datum(ln);
405 if (s2->sNum >= s->sNum) {
411 SUFF_DEBUG2("inserting %s(%d)...", s->name, s->sNum);
414 SUFF_DEBUG0("at end of list\n");
417 Lst_Append(s->ref, l);
418 } else if (s2->sNum != s->sNum) {
419 SUFF_DEBUG2("before %s(%d)\n", s2->name, s2->sNum);
420 Lst_InsertBefore(l, ln, s);
422 Lst_Append(s->ref, l);
424 SUFF_DEBUG0("already there\n");
429 SuffNew(const char *name)
431 Suff *s = bmake_malloc(sizeof(Suff));
433 s->name = bmake_strdup(name);
434 s->nameLen = strlen(s->name);
435 s->searchPath = Lst_Init();
436 s->children = Lst_Init();
437 s->parents = Lst_Init();
446 /* This is gross. Nuke the list of suffixes but keep all transformation
447 * rules around. The transformation graph is destroyed in this process, but
448 * we leave the list of rules so when a new graph is formed the rules will
449 * remain. This function is called from the parse module when a .SUFFIXES:\n
450 * line is encountered. */
452 Suff_ClearSuffixes(void)
455 Lst_MoveAll(suffClean, sufflist);
457 sufflist = Lst_Init();
461 emptySuff = suffNull = SuffNew("");
463 Dir_Concat(suffNull->searchPath, dirSearchPath);
464 suffNull->flags = SUFF_NULL;
467 /* Parse a transformation string to find its two component suffixes.
470 * str String being parsed
471 * out_src Place to store source of trans.
472 * out_targ Place to store target of trans.
475 * TRUE if the string is a valid transformation, FALSE otherwise.
478 SuffParseTransform(char *str, Suff **out_src, Suff **out_targ)
480 LstNode srcLn; /* element in suffix list of trans source*/
481 Suff *src; /* Source of transformation */
482 LstNode targLn; /* element in suffix list of trans target*/
483 char *str2; /* Extra pointer (maybe target suffix) */
484 LstNode singleLn; /* element in suffix list of any suffix
485 * that exactly matches str */
486 Suff *single = NULL;/* Source of possible transformation to
493 * Loop looking first for a suffix that matches the start of the
494 * string and then for one that exactly matches the rest of it. If
495 * we can find two that meet these criteria, we've successfully
500 srcLn = Lst_Find(sufflist, SuffSuffIsPrefix, str);
502 srcLn = Lst_FindFrom(sufflist, LstNode_Next(srcLn),
503 SuffSuffIsPrefix, str);
507 * Ran out of source suffixes -- no such rule
509 if (singleLn != NULL) {
511 * Not so fast Mr. Smith! There was a suffix that encompassed
512 * the entire string, so we assume it was a transformation
513 * to the null suffix (thank you POSIX). We still prefer to
514 * find a double rule over a singleton, hence we leave this
515 * check until the end.
517 * XXX: Use emptySuff over suffNull?
520 *out_targ = suffNull;
525 src = LstNode_Datum(srcLn);
526 str2 = str + src->nameLen;
531 targLn = Lst_Find(sufflist, SuffSuffHasName, str2);
532 if (targLn != NULL) {
534 *out_targ = LstNode_Datum(targLn);
541 /* Return TRUE if the given string is a transformation rule, that is, a
542 * concatenation of two known suffixes. */
544 Suff_IsTransform(char *str)
548 return SuffParseTransform(str, &src, &targ);
551 /* Add the transformation rule described by the line to the list of rules
552 * and place the transformation itself in the graph.
554 * The node is placed on the end of the transforms Lst and links are made
555 * between the two suffixes mentioned in the target name.
558 * line name of transformation to add
561 * The node created for the transformation in the transforms list
564 Suff_AddTransform(char *line)
566 GNode *gn; /* GNode of transformation rule */
567 Suff *s, /* source suffix */
568 *t; /* target suffix */
569 LstNode ln; /* Node for existing transformation */
572 ln = Lst_Find(transforms, SuffGNHasName, line);
575 * Make a new graph node for the transformation. It will be filled in
576 * by the Parse module.
578 gn = Targ_NewGN(line);
579 Lst_Append(transforms, gn);
582 * New specification for transformation rule. Just nuke the old list
583 * of commands so they can be filled in again... We don't actually
584 * free the commands themselves, because a given command can be
585 * attached to several different transformations.
587 gn = LstNode_Datum(ln);
588 Lst_Free(gn->commands);
589 Lst_Free(gn->children);
590 gn->commands = Lst_Init();
591 gn->children = Lst_Init();
594 gn->type = OP_TRANSFORM;
596 ok = SuffParseTransform(line, &s, &t);
601 * link the two together in the proper relationship and order
603 SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
605 SuffInsert(t->children, s);
606 SuffInsert(s->parents, t);
611 /* Handle the finish of a transformation definition, removing the
612 * transformation from the graph if it has neither commands nor sources.
613 * This is a callback procedure for the Parse module via Lst_ForEach.
615 * If the node has no commands or children, the children and parents lists
616 * of the affected suffixes are altered.
619 * gnp Node for transformation
622 * 0, so that Lst_ForEach continues
625 Suff_EndTransform(void *gnp, void *dummy MAKE_ATTR_UNUSED)
627 GNode *gn = (GNode *)gnp;
629 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(gn->cohorts))
630 gn = LstNode_Datum(Lst_Last(gn->cohorts));
631 if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
632 Lst_IsEmpty(gn->children))
637 * SuffParseTransform() may fail for special rules which are not
638 * actual transformation rules. (e.g. .DEFAULT)
640 if (SuffParseTransform(gn->name, &s, &t)) {
643 SUFF_DEBUG2("deleting transformation from `%s' to `%s'\n",
647 * Store s->parents because s could be deleted in SuffRemove
652 * Remove the source from the target's children list. We check for a
653 * nil return to handle a beanhead saying something like
656 * We'll be called twice when the next target is seen, but .c and .o
657 * are only linked once...
659 SuffRemove(t->children, s);
662 * Remove the target from the source's parents list
666 } else if (gn->type & OP_TRANSFORM) {
667 SUFF_DEBUG1("transformation %s complete\n", gn->name);
673 /* Called from Suff_AddSuffix via Lst_ForEach to search through the list of
674 * existing transformation rules and rebuild the transformation graph when
675 * it has been destroyed by Suff_ClearSuffixes. If the given rule is a
676 * transformation involving this suffix and another, existing suffix, the
677 * proper relationship is established between the two.
679 * The appropriate links will be made between this suffix and others if
680 * transformation rules exist for it.
683 * transformp Transformation to test
684 * sp Suffix to rebuild
687 * 0, so that Lst_ForEach continues
690 SuffRebuildGraph(void *transformp, void *sp)
692 GNode *transform = (GNode *)transformp;
693 Suff *s = (Suff *)sp;
697 SuffSuffGetSuffixArgs sd;
700 * First see if it is a transformation from this suffix.
702 cp = UNCONST(SuffStrIsPrefix(s->name, transform->name));
704 ln = Lst_Find(sufflist, SuffSuffHasName, cp);
707 * Found target. Link in and return, since it can't be anything
710 s2 = LstNode_Datum(ln);
711 SuffInsert(s2->children, s);
712 SuffInsert(s->parents, s2);
718 * Not from, maybe to?
720 sd.len = strlen(transform->name);
721 sd.ename = transform->name + sd.len;
722 cp = SuffSuffGetSuffix(s, &sd);
725 * Null-terminate the source suffix in order to find it.
728 ln = Lst_Find(sufflist, SuffSuffHasName, transform->name);
730 * Replace the start of the target suffix
735 * Found it -- establish the proper relationship
737 s2 = LstNode_Datum(ln);
738 SuffInsert(s->children, s2);
739 SuffInsert(s2->parents, s);
745 /* Called from Suff_AddSuffix via Lst_ForEach to search through the list of
746 * existing targets and find if any of the existing targets can be turned
747 * into a transformation rule.
749 * If such a target is found and the target is the current main target, the
750 * main target is set to NULL and the next target examined (if that exists)
751 * becomes the main target.
754 * 1 if a new main target has been selected, 0 otherwise.
757 SuffScanTargets(void *targetp, void *gsp)
759 GNode *target = (GNode *)targetp;
760 GNodeSuff *gs = (GNodeSuff *)gsp;
764 if (*gs->gn == NULL && gs->r && (target->type & OP_NOTARGET) == 0) {
766 Targ_SetMain(target);
770 if (target->type == OP_TRANSFORM)
773 if ((ptr = strstr(target->name, gs->s->name)) == NULL ||
777 if (SuffParseTransform(target->name, &s, &t)) {
778 if (*gs->gn == target) {
783 Lst_Free(target->children);
784 target->children = Lst_Init();
785 target->type = OP_TRANSFORM;
787 * link the two together in the proper relationship and order
789 SUFF_DEBUG2("defining transformation from `%s' to `%s'\n",
791 SuffInsert(t->children, s);
792 SuffInsert(s->parents, t);
797 /* Add the suffix to the end of the list of known suffixes.
798 * Should we restructure the suffix graph? Make doesn't...
800 * A GNode is created for the suffix and a Suff structure is created and
801 * added to the suffixes list unless the suffix was already known.
802 * The mainNode passed can be modified if a target mutated into a
803 * transform and that target happened to be the main target.
806 * name the name of the suffix to add
809 Suff_AddSuffix(const char *name, GNode **gn)
811 Suff *s; /* new suffix descriptor */
815 ln = Lst_Find(sufflist, SuffSuffHasName, name);
819 Lst_Append(sufflist, s);
821 * We also look at our existing targets list to see if adding
822 * this suffix will make one of our current targets mutate into
823 * a suffix rule. This is ugly, but other makes treat all targets
824 * that start with a . as suffix rules.
829 Lst_ForEach(Targ_List(), SuffScanTargets, &gs);
831 * Look for any existing transformations from or to this suffix.
832 * XXX: Only do this after a Suff_ClearSuffixes?
834 Lst_ForEach(transforms, SuffRebuildGraph, s);
838 /* Return the search path for the given suffix, or NULL. */
840 Suff_GetPath(char *sname)
845 ln = Lst_Find(sufflist, SuffSuffHasName, sname);
849 s = LstNode_Datum(ln);
850 return s->searchPath;
854 /* Extend the search paths for all suffixes to include the default search
857 * The searchPath field of all the suffixes is extended by the directories
858 * in dirSearchPath. If paths were specified for the ".h" suffix, the
859 * directories are stuffed into a global variable called ".INCLUDES" with
860 * each directory preceded by a -I. The same is done for the ".a" suffix,
861 * except the variable is called ".LIBS" and the flag is -L.
869 Lst inIncludes; /* Cumulative .INCLUDES path */
870 Lst inLibs; /* Cumulative .LIBS path */
873 inIncludes = Lst_Init();
877 while ((ln = Lst_Next(sufflist)) != NULL) {
878 s = LstNode_Datum(ln);
879 if (!Lst_IsEmpty(s->searchPath)) {
881 if (s->flags & SUFF_INCLUDE) {
882 Dir_Concat(inIncludes, s->searchPath);
884 #endif /* INCLUDES */
886 if (s->flags & SUFF_LIBRARY) {
887 Dir_Concat(inLibs, s->searchPath);
889 #endif /* LIBRARIES */
890 Dir_Concat(s->searchPath, dirSearchPath);
892 Lst_Destroy(s->searchPath, Dir_Destroy);
893 s->searchPath = Lst_Copy(dirSearchPath, Dir_CopyDir);
898 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
900 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
903 Lst_Destroy(inIncludes, Dir_Destroy);
904 Lst_Destroy(inLibs, Dir_Destroy);
907 /* Add the given suffix as a type of file which gets included.
908 * Called from the parse module when a .INCLUDES line is parsed.
909 * The suffix must have already been defined.
910 * The SUFF_INCLUDE bit is set in the suffix's flags field.
913 * sname Name of the suffix to mark
916 Suff_AddInclude(char *sname)
921 ln = Lst_Find(sufflist, SuffSuffHasName, sname);
923 s = LstNode_Datum(ln);
924 s->flags |= SUFF_INCLUDE;
928 /* Add the given suffix as a type of file which is a library.
929 * Called from the parse module when parsing a .LIBS line.
930 * The suffix must have been defined via .SUFFIXES before this is called.
931 * The SUFF_LIBRARY bit is set in the suffix's flags field.
934 * sname Name of the suffix to mark
937 Suff_AddLib(const char *sname)
942 ln = Lst_Find(sufflist, SuffSuffHasName, sname);
944 s = LstNode_Datum(ln);
945 s->flags |= SUFF_LIBRARY;
949 /********** Implicit Source Search Functions *********/
951 /* Add a suffix as a Src structure to the given list with its parent
952 * being the given Src structure. If the suffix is the null suffix,
953 * the prefix is used unaltered as the file name in the Src structure.
956 * sp suffix for which to create a Src structure
957 * lsp list and parent for the new Src
960 * 0, so that Lst_ForEach continues
963 SuffAddSrc(void *sp, void *lsp)
965 Suff *s = (Suff *)sp;
966 LstSrc *ls = (LstSrc *)lsp;
967 Src *s2; /* new Src structure */
968 Src *targ; /* Target structure */
972 if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
974 * If the suffix has been marked as the NULL suffix, also create a Src
975 * structure for a file with no suffix attached. Two birds, and all
978 s2 = bmake_malloc(sizeof(Src));
979 s2->file = bmake_strdup(targ->pref);
980 s2->pref = targ->pref;
987 Lst_Append(ls->l, s2);
990 Lst_Append(targ->cp, s2);
991 fprintf(debug_file, "1 add %p %p to %p:", targ, s2, ls->l);
992 Lst_ForEach(ls->l, PrintAddr, NULL);
993 fprintf(debug_file, "\n");
996 s2 = bmake_malloc(sizeof(Src));
997 s2->file = str_concat2(targ->pref, s->name);
998 s2->pref = targ->pref;
1004 targ->children += 1;
1005 Lst_Append(ls->l, s2);
1007 s2->cp = Lst_Init();
1008 Lst_Append(targ->cp, s2);
1009 fprintf(debug_file, "2 add %p %p to %p:", targ, s2, ls->l);
1010 Lst_ForEach(ls->l, PrintAddr, NULL);
1011 fprintf(debug_file, "\n");
1017 /* Add all the children of targ as Src structures to the given list.
1020 * l list to which to add the new level
1021 * targ Src structure to use as the parent
1024 SuffAddLevel(Lst l, Src *targ)
1031 Lst_ForEach(targ->suff->children, SuffAddSrc, &ls);
1034 /* Free the first Src in the list that doesn't have a reference count.
1035 * Return whether a Src was removed. */
1037 SuffRemoveSrc(Lst l)
1045 fprintf(debug_file, "cleaning %lx: ", (unsigned long) l);
1046 Lst_ForEach(l, PrintAddr, NULL);
1047 fprintf(debug_file, "\n");
1050 while ((ln = Lst_Next(l)) != NULL) {
1051 s = LstNode_Datum(ln);
1052 if (s->children == 0) {
1058 LstNode ln2 = Lst_FindDatum(s->parent->cp, s);
1060 Lst_Remove(s->parent->cp, ln2);
1062 --s->parent->children;
1065 fprintf(debug_file, "free: [l=%p] p=%p %d\n", l, s, s->children);
1075 fprintf(debug_file, "keep: [l=%p] p=%p %d: ", l, s, s->children);
1076 Lst_ForEach(s->cp, PrintAddr, NULL);
1077 fprintf(debug_file, "\n");
1087 /* Find the first existing file/target in the list srcs.
1090 * srcs list of Src structures to search through
1093 * The lowest structure in the chain of transformations, or NULL.
1096 SuffFindThem(Lst srcs, Lst slst)
1098 Src *s; /* current Src */
1099 Src *rs; /* returned Src */
1104 while (!Lst_IsEmpty(srcs)) {
1105 s = Lst_Dequeue(srcs);
1107 SUFF_DEBUG1("\ttrying %s...", s->file);
1110 * A file is considered to exist if either a node exists in the
1111 * graph for it or the file actually exists.
1113 if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
1115 fprintf(debug_file, "remove %p from %p\n", s, srcs);
1121 if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) {
1124 fprintf(debug_file, "remove %p from %p\n", s, srcs);
1130 SUFF_DEBUG0("not there\n");
1132 SuffAddLevel(srcs, s);
1133 Lst_Append(slst, s);
1137 SUFF_DEBUG0("got it\n");
1142 /* See if any of the children of the target in the Src structure is one from
1143 * which the target can be transformed. If there is one, a Src structure is
1144 * put together for it and returned.
1147 * targ Src to play with
1150 * The Src of the "winning" child, or NULL.
1153 SuffFindCmds(Src *targ, Lst slst)
1155 LstNode ln; /* General-purpose list node */
1156 GNode *t, /* Target GNode */
1157 *s; /* Source GNode */
1158 int prefLen;/* The length of the defined prefix */
1159 Suff *suff; /* Suffix on matching beastie */
1160 Src *ret; /* Return value */
1164 Lst_Open(t->children);
1165 prefLen = strlen(targ->pref);
1168 ln = Lst_Next(t->children);
1170 Lst_Close(t->children);
1173 s = LstNode_Datum(ln);
1175 if (s->type & OP_OPTIONAL && Lst_IsEmpty(t->commands)) {
1177 * We haven't looked to see if .OPTIONAL files exist yet, so
1178 * don't use one as the implicit source.
1179 * This allows us to use .OPTIONAL in .depend files so make won't
1180 * complain "don't know how to make xxx.h' when a dependent file
1181 * has been moved/deleted.
1186 cp = strrchr(s->name, '/');
1192 if (strncmp(cp, targ->pref, prefLen) != 0)
1195 * The node matches the prefix ok, see if it has a known
1198 ln = Lst_Find(sufflist, SuffSuffHasName, &cp[prefLen]);
1202 * It even has a known suffix, see if there's a transformation
1203 * defined between the node's suffix and the target's suffix.
1205 * XXX: Handle multi-stage transformations here, too.
1207 suff = LstNode_Datum(ln);
1209 /* XXX: Can targ->suff be NULL here? */
1210 if (targ->suff != NULL &&
1211 Lst_FindDatum(suff->parents, targ->suff) != NULL)
1216 * Hot Damn! Create a new Src structure to describe
1217 * this transformation (making sure to duplicate the
1218 * source node's name so Suff_FindDeps can free it
1219 * again (ick)), and return the new structure.
1221 ret = bmake_malloc(sizeof(Src));
1222 ret->file = bmake_strdup(s->name);
1223 ret->pref = targ->pref;
1229 targ->children += 1;
1231 ret->cp = Lst_Init();
1232 fprintf(debug_file, "3 add %p %p\n", targ, ret);
1233 Lst_Append(targ->cp, ret);
1235 Lst_Append(slst, ret);
1236 SUFF_DEBUG1("\tusing existing source %s\n", s->name);
1237 Lst_Close(t->children);
1241 /* Expand the names of any children of a given node that contain variable
1242 * invocations or file wildcards into actual targets.
1244 * The expanded node is removed from the parent's list of children, and the
1245 * parent's unmade counter is decremented, but other nodes may be added.
1248 * cln Child to examine
1249 * pgn Parent node being processed
1252 SuffExpandChildren(LstNode cln, GNode *pgn)
1254 GNode *cgn = LstNode_Datum(cln);
1255 GNode *gn; /* New source 8) */
1256 char *cp; /* Expanded value */
1258 if (!Lst_IsEmpty(cgn->order_pred) || !Lst_IsEmpty(cgn->order_succ))
1259 /* It is all too hard to process the result of .ORDER */
1262 if (cgn->type & OP_WAIT)
1263 /* Ignore these (& OP_PHONY ?) */
1267 * First do variable expansion -- this takes precedence over
1268 * wildcard expansion. If the result contains wildcards, they'll be gotten
1269 * to later since the resulting words are tacked on to the end of
1270 * the children list.
1272 if (strchr(cgn->name, '$') == NULL) {
1273 SuffExpandWildcards(cln, pgn);
1277 SUFF_DEBUG1("Expanding \"%s\"...", cgn->name);
1278 cp = Var_Subst(cgn->name, pgn, VARE_UNDEFERR|VARE_WANTRES);
1281 Lst members = Lst_Init();
1283 if (cgn->type & OP_ARCHV) {
1285 * Node was an archive(member) target, so we want to call
1286 * on the Arch module to find the nodes for us, expanding
1287 * variables in the parent's context.
1289 char *sacrifice = cp;
1291 (void)Arch_ParseArchive(&sacrifice, members, pgn);
1294 * Break the result into a vector of strings whose nodes
1295 * we can find, then add those nodes to the members list.
1296 * Unfortunately, we can't use brk_string b/c it
1297 * doesn't understand about variable specifications with
1301 char *initcp = cp; /* For freeing... */
1303 for (start = cp; *start == ' ' || *start == '\t'; start++)
1305 for (cp = start; *cp != '\0'; cp++) {
1306 if (*cp == ' ' || *cp == '\t') {
1308 * White-space -- terminate element, find the node,
1309 * add it, skip any further spaces.
1312 gn = Targ_FindNode(start, TARG_CREATE);
1313 Lst_Append(members, gn);
1314 while (*cp == ' ' || *cp == '\t') {
1318 * Adjust cp for increment at start of loop, but
1319 * set start to first non-space.
1322 } else if (*cp == '$') {
1324 * Start of a variable spec -- contact variable module
1325 * to find the end so we can skip over it.
1331 junk = Var_Parse(cp, pgn, VARE_UNDEFERR|VARE_WANTRES,
1333 if (junk != var_Error) {
1338 } else if (*cp == '\\' && cp[1] != '\0') {
1340 * Escaped something -- skip over it
1348 * Stuff left over -- add it to the list too
1350 gn = Targ_FindNode(start, TARG_CREATE);
1351 Lst_Append(members, gn);
1354 * Point cp back at the beginning again so the variable value
1361 * Add all elements of the members list to the parent node.
1363 while(!Lst_IsEmpty(members)) {
1364 gn = Lst_Dequeue(members);
1366 SUFF_DEBUG1("%s...", gn->name);
1367 /* Add gn to the parents child list before the original child */
1368 Lst_InsertBefore(pgn->children, cln, gn);
1369 Lst_Append(gn->parents, pgn);
1371 /* Expand wildcards on new node */
1372 SuffExpandWildcards(LstNode_Prev(cln), pgn);
1385 * Now the source is expanded, remove it from the list of children to
1386 * keep it from being processed.
1389 Lst_Remove(pgn->children, cln);
1390 Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
1394 SuffExpandWildcards(LstNode cln, GNode *pgn)
1396 GNode *cgn = LstNode_Datum(cln);
1397 GNode *gn; /* New source 8) */
1398 char *cp; /* Expanded value */
1399 Lst explist; /* List of expansions */
1401 if (!Dir_HasWildcards(cgn->name))
1405 * Expand the word along the chosen path
1407 explist = Lst_Init();
1408 Dir_Expand(cgn->name, Suff_FindPath(cgn), explist);
1410 while (!Lst_IsEmpty(explist)) {
1412 * Fetch next expansion off the list and find its GNode
1414 cp = Lst_Dequeue(explist);
1416 SUFF_DEBUG1("%s...", cp);
1417 gn = Targ_FindNode(cp, TARG_CREATE);
1419 /* Add gn to the parents child list before the original child */
1420 Lst_InsertBefore(pgn->children, cln, gn);
1421 Lst_Append(gn->parents, pgn);
1430 * Now the source is expanded, remove it from the list of children to
1431 * keep it from being processed.
1434 Lst_Remove(pgn->children, cln);
1435 Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
1438 /* Find a path along which to expand the node.
1440 * If the word has a known suffix, use that path.
1441 * If it has no known suffix, use the default system search path.
1444 * gn Node being examined
1447 * The appropriate path to search for the GNode.
1450 Suff_FindPath(GNode* gn)
1452 Suff *suff = gn->suffix;
1455 SuffSuffGetSuffixArgs sd; /* Search string data */
1457 sd.len = strlen(gn->name);
1458 sd.ename = gn->name + sd.len;
1459 ln = Lst_Find(sufflist, SuffSuffIsSuffix, &sd);
1461 SUFF_DEBUG1("Wildcard expanding \"%s\"...", gn->name);
1463 suff = LstNode_Datum(ln);
1464 /* XXX: Here we can save the suffix so we don't have to do this again */
1468 SUFF_DEBUG1("suffix is \"%s\"...", suff->name);
1469 return suff->searchPath;
1472 * Use default search path
1474 return dirSearchPath;
1478 /* Apply a transformation rule, given the source and target nodes and
1488 * TRUE if successful, FALSE if not.
1491 * The source and target are linked and the commands from the
1492 * transformation are added to the target node's commands list.
1493 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1494 * to the target. The target also inherits all the sources for
1495 * the transformation rule.
1498 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
1500 LstNode ln, nln; /* General node */
1501 char *tname; /* Name of transformation rule */
1502 GNode *gn; /* Node for same */
1505 * Form the proper links between the target and source.
1507 Lst_Append(tGn->children, sGn);
1508 Lst_Append(sGn->parents, tGn);
1512 * Locate the transformation rule itself
1514 tname = str_concat2(s->name, t->name);
1515 ln = Lst_Find(transforms, SuffGNHasName, tname);
1520 * Not really such a transformation rule (can happen when we're
1521 * called to link an OP_MEMBER and OP_ARCHV node), so return
1527 gn = LstNode_Datum(ln);
1529 SUFF_DEBUG3("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
1532 * Record last child for expansion purposes
1534 ln = Lst_Last(tGn->children);
1537 * Pass the buck to Make_HandleUse to apply the rule
1539 (void)Make_HandleUse(gn, tGn);
1542 * Deal with wildcards and variables in any acquired sources
1544 for (ln = ln != NULL ? LstNode_Next(ln) : NULL; ln != NULL; ln = nln) {
1545 nln = LstNode_Next(ln);
1546 SuffExpandChildren(ln, tGn);
1550 * Keep track of another parent to which this beast is transformed so
1551 * the .IMPSRC variable can be set correctly for the parent.
1553 Lst_Append(sGn->implicitParents, tGn);
1559 /* Locate dependencies for an OP_ARCHV node.
1562 * gn Node for which to locate dependencies
1565 * Same as Suff_FindDeps
1568 SuffFindArchiveDeps(GNode *gn, Lst slst)
1570 char *eoarch; /* End of archive portion */
1571 char *eoname; /* End of member portion */
1572 GNode *mem; /* Node for member */
1573 LstNode ln, nln; /* Next suffix node to check */
1574 Suff *ms; /* Suffix descriptor for member */
1575 char *name; /* Start of member's name */
1578 * The node is an archive(member) pair. so we must find a
1579 * suffix for both of them.
1581 eoarch = strchr(gn->name, '(');
1582 eoname = strchr(eoarch, ')');
1585 * Caller guarantees the format `libname(member)', via
1586 * Arch_ParseArchive.
1588 assert(eoarch != NULL);
1589 assert(eoname != NULL);
1591 *eoname = '\0'; /* Nuke parentheses during suffix search */
1592 *eoarch = '\0'; /* So a suffix can be found */
1597 * To simplify things, call Suff_FindDeps recursively on the member now,
1598 * so we can simply compare the member's .PREFIX and .TARGET variables
1599 * to locate its suffix. This allows us to figure out the suffix to
1600 * use for the archive without having to do a quadratic search over the
1601 * suffix list, backtracking for each one...
1603 mem = Targ_FindNode(name, TARG_CREATE);
1604 SuffFindDeps(mem, slst);
1607 * Create the link between the two nodes right off
1609 Lst_Append(gn->children, mem);
1610 Lst_Append(mem->parents, gn);
1614 * Copy in the variables from the member node to this one.
1618 Var_Set(PREFIX, Var_Value(PREFIX, mem, &freeIt), gn);
1620 Var_Set(TARGET, Var_Value(TARGET, mem, &freeIt), gn);
1627 * Didn't know what it was -- use .NULL suffix if not in make mode
1629 SUFF_DEBUG0("using null suffix\n");
1635 * Set the other two local variables required for this target.
1637 Var_Set(MEMBER, name, gn);
1638 Var_Set(ARCHIVE, gn->name, gn);
1641 * Set $@ for compatibility with other makes
1643 Var_Set(TARGET, gn->name, gn);
1646 * Now we've got the important local variables set, expand any sources
1647 * that still contain variables or wildcards in their names.
1649 for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
1650 nln = LstNode_Next(ln);
1651 SuffExpandChildren(ln, gn);
1656 * Member has a known suffix, so look for a transformation rule from
1657 * it to a possible suffix of the archive. Rather than searching
1658 * through the entire list, we just look at suffixes to which the
1659 * member's suffix may be transformed...
1661 SuffSuffGetSuffixArgs sd; /* Search string data */
1664 * Use first matching suffix...
1666 sd.len = eoarch - gn->name;
1668 ln = Lst_Find(ms->parents, SuffSuffIsSuffix, &sd);
1672 * Got one -- apply it
1674 Suff *suff = LstNode_Datum(ln);
1675 if (!SuffApplyTransform(gn, mem, suff, ms)) {
1676 SUFF_DEBUG2("\tNo transformation from %s -> %s\n",
1677 ms->name, suff->name);
1683 * Replace the opening and closing parens now we've no need of the separate
1686 *eoarch = '('; *eoname = ')';
1689 * Pretend gn appeared to the left of a dependency operator so
1690 * the user needn't provide a transformation from the member to the
1693 if (OP_NOP(gn->type)) {
1694 gn->type |= OP_DEPENDS;
1698 * Flag the member as such so we remember to look in the archive for
1699 * its modification time. The OP_JOIN | OP_MADE is needed because this
1700 * target should never get made.
1702 mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
1705 /* Locate implicit dependencies for regular targets.
1708 * gn Node for which to find sources
1711 * Same as Suff_FindDeps
1714 SuffFindNormalDeps(GNode *gn, Lst slst)
1716 char *eoname; /* End of name */
1717 char *sopref; /* Start of prefix */
1718 LstNode ln, nln; /* Next suffix node to check */
1719 Lst srcs; /* List of sources at which to look */
1720 Lst targs; /* List of targets to which things can be
1721 * transformed. They all have the same file,
1722 * but different suff and pref fields */
1723 Src *bottom; /* Start of found transformation path */
1724 Src *src; /* General Src pointer */
1725 char *pref; /* Prefix to use */
1726 Src *targ; /* General Src target pointer */
1727 SuffSuffGetSuffixArgs sd; /* Search string data */
1730 sd.len = strlen(gn->name);
1731 sd.ename = eoname = gn->name + sd.len;
1736 * Begin at the beginning...
1738 ln = Lst_First(sufflist);
1743 * We're caught in a catch-22 here. On the one hand, we want to use any
1744 * transformation implied by the target's sources, but we can't examine
1745 * the sources until we've expanded any variables/wildcards they may hold,
1746 * and we can't do that until we've set up the target's local variables
1747 * and we can't do that until we know what the proper suffix for the
1748 * target is (in case there are two suffixes one of which is a suffix of
1749 * the other) and we can't know that until we've found its implied
1750 * source, which we may not want to use if there's an existing source
1751 * that implies a different transformation.
1753 * In an attempt to get around this, which may not work all the time,
1754 * but should work most of the time, we look for implied sources first,
1755 * checking transformations to all possible suffixes of the target,
1756 * use what we find to set the target's local variables, expand the
1757 * children, then look for any overriding transformations they imply.
1758 * Should we find one, we discard the one we found before.
1763 if (!(gn->type & OP_PHONY)) {
1765 while (ln != NULL) {
1767 * Look for next possible suffix...
1769 ln = Lst_FindFrom(sufflist, ln, SuffSuffIsSuffix, &sd);
1775 * Allocate a Src structure to which things can be transformed
1777 targ = bmake_malloc(sizeof(Src));
1778 targ->file = bmake_strdup(gn->name);
1779 targ->suff = LstNode_Datum(ln);
1780 targ->suff->refCount++;
1782 targ->parent = NULL;
1785 targ->cp = Lst_Init();
1788 eopref = eoname - targ->suff->nameLen;
1789 targ->pref = bmake_strsedup(sopref, eopref);
1792 * Add nodes from which the target can be made
1794 SuffAddLevel(srcs, targ);
1797 * Record the target so we can nuke it
1799 Lst_Append(targs, targ);
1802 * Search from this suffix's successor...
1804 ln = LstNode_Next(ln);
1809 * Handle target of unknown suffix...
1811 if (Lst_IsEmpty(targs) && suffNull != NULL) {
1812 SUFF_DEBUG1("\tNo known suffix on %s. Using .NULL suffix\n",
1815 targ = bmake_malloc(sizeof(Src));
1816 targ->file = bmake_strdup(gn->name);
1817 targ->suff = suffNull;
1818 targ->suff->refCount++;
1820 targ->parent = NULL;
1822 targ->pref = bmake_strdup(sopref);
1824 targ->cp = Lst_Init();
1828 * Only use the default suffix rules if we don't have commands
1829 * defined for this gnode; traditional make programs used to
1830 * not define suffix rules if the gnode had children but we
1831 * don't do this anymore.
1833 if (Lst_IsEmpty(gn->commands))
1834 SuffAddLevel(srcs, targ);
1836 SUFF_DEBUG0("not ");
1839 SUFF_DEBUG0("adding suffix rules\n");
1841 Lst_Append(targs, targ);
1845 * Using the list of possible sources built up from the target
1846 * suffix(es), try and find an existing file/target that matches.
1848 bottom = SuffFindThem(srcs, slst);
1850 if (bottom == NULL) {
1852 * No known transformations -- use the first suffix found
1853 * for setting the local variables.
1855 if (!Lst_IsEmpty(targs)) {
1856 targ = LstNode_Datum(Lst_First(targs));
1862 * Work up the transformation path to find the suffix of the
1863 * target to which the transformation was made.
1865 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
1870 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
1872 pref = (targ != NULL) ? targ->pref : gn->name;
1873 Var_Set(PREFIX, pref, gn);
1876 * Now we've got the important local variables set, expand any sources
1877 * that still contain variables or wildcards in their names.
1879 for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
1880 nln = LstNode_Next(ln);
1881 SuffExpandChildren(ln, gn);
1885 SUFF_DEBUG1("\tNo valid suffix on %s\n", gn->name);
1889 * Deal with finding the thing on the default search path. We
1890 * always do that, not only if the node is only a source (not
1891 * on the lhs of a dependency operator or [XXX] it has neither
1892 * children or commands) as the old pmake did.
1894 if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
1896 gn->path = Dir_FindFile(gn->name,
1897 (targ == NULL ? dirSearchPath :
1898 targ->suff->searchPath));
1899 if (gn->path != NULL) {
1901 Var_Set(TARGET, gn->path, gn);
1905 * Suffix known for the thing -- trim the suffix off
1906 * the path to form the proper .PREFIX variable.
1908 int savep = strlen(gn->path) - targ->suff->nameLen;
1912 gn->suffix->refCount--;
1913 gn->suffix = targ->suff;
1914 gn->suffix->refCount++;
1916 savec = gn->path[savep];
1917 gn->path[savep] = '\0';
1919 if ((ptr = strrchr(gn->path, '/')) != NULL)
1924 Var_Set(PREFIX, ptr, gn);
1926 gn->path[savep] = savec;
1929 * The .PREFIX gets the full path if the target has
1933 gn->suffix->refCount--;
1936 if ((ptr = strrchr(gn->path, '/')) != NULL)
1941 Var_Set(PREFIX, ptr, gn);
1950 * If the suffix indicates that the target is a library, mark that in
1951 * the node's type field.
1953 if (targ->suff->flags & SUFF_LIBRARY) {
1958 * Check for overriding transformation rule implied by sources
1960 if (!Lst_IsEmpty(gn->children)) {
1961 src = SuffFindCmds(targ, slst);
1965 * Free up all the Src structures in the transformation path
1966 * up to, but not including, the parent node.
1968 while (bottom && bottom->parent != NULL) {
1969 if (Lst_FindDatum(slst, bottom) == NULL) {
1970 Lst_Append(slst, bottom);
1972 bottom = bottom->parent;
1978 if (bottom == NULL) {
1980 * No idea from where it can come -- return now.
1986 * We now have a list of Src structures headed by 'bottom' and linked via
1987 * their 'parent' pointers. What we do next is create links between
1988 * source and target nodes (which may or may not have been created)
1989 * and set the necessary local variables in each target. The
1990 * commands for each target are set from the commands of the
1991 * transformation rule used to get from the src suffix to the targ
1992 * suffix. Note that this causes the commands list of the original
1993 * node, gn, to be replaced by the commands of the final
1994 * transformation rule. Also, the unmade field of gn is incremented.
1997 if (bottom->node == NULL) {
1998 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2001 for (src = bottom; src->parent != NULL; src = src->parent) {
2004 if (src->node->suffix)
2005 src->node->suffix->refCount--;
2006 src->node->suffix = src->suff;
2007 src->node->suffix->refCount++;
2009 if (targ->node == NULL) {
2010 targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2013 SuffApplyTransform(targ->node, src->node,
2014 targ->suff, src->suff);
2016 if (targ->node != gn) {
2018 * Finish off the dependency-search process for any nodes
2019 * between bottom and gn (no point in questing around the
2020 * filesystem for their implicit source when it's already
2021 * known). Note that the node can't have any sources that
2022 * need expanding, since SuffFindThem will stop on an existing
2023 * node, so all we need to do is set the standard and System V
2026 targ->node->type |= OP_DEPS_FOUND;
2028 Var_Set(PREFIX, targ->pref, targ->node);
2030 Var_Set(TARGET, targ->node->name, targ->node);
2035 gn->suffix->refCount--;
2036 gn->suffix = src->suff;
2037 gn->suffix->refCount++;
2040 * Nuke the transformation path and the Src structures left over in the
2045 if (Lst_FindDatum(slst, bottom) == NULL)
2046 Lst_Append(slst, bottom);
2048 while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
2051 Lst_MoveAll(slst, srcs);
2052 Lst_MoveAll(slst, targs);
2056 /* Find implicit sources for the target described by the graph node.
2058 * Nodes are added to the graph below the passed-in node. The nodes are
2059 * marked to have their IMPSRC variable filled in. The PREFIX variable is set
2060 * for the given node and all its implied children.
2062 * The path found by this target is the shortest path in the transformation
2063 * graph, which may pass through non-existent targets, to an existing target.
2064 * The search continues on all paths from the root suffix until a file is
2065 * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
2066 * .l,v file exists but the .c and .l files don't, the search will branch out
2067 * in all directions from .o and again from all the nodes on the next level
2068 * until the .l,v node is encountered.
2071 Suff_FindDeps(GNode *gn)
2074 SuffFindDeps(gn, srclist);
2075 while (SuffRemoveSrc(srclist))
2080 SuffFindDeps(GNode *gn, Lst slst)
2082 if (gn->type & OP_DEPS_FOUND)
2084 gn->type |= OP_DEPS_FOUND;
2087 * Make sure we have these set, may get revised below.
2089 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
2090 Var_Set(PREFIX, gn->name, gn);
2092 SUFF_DEBUG1("SuffFindDeps (%s)\n", gn->name);
2094 if (gn->type & OP_ARCHV) {
2095 SuffFindArchiveDeps(gn, slst);
2096 } else if (gn->type & OP_LIB) {
2098 * If the node is a library, it is the arch module's job to find it
2099 * and set the TARGET variable accordingly. We merely provide the
2100 * search path, assuming all libraries end in ".a" (if the suffix
2101 * hasn't been defined, there's nothing we can do for it, so we just
2102 * set the TARGET variable to the node's name in order to give it a
2108 ln = Lst_Find(sufflist, SuffSuffHasName, LIBSUFF);
2110 gn->suffix->refCount--;
2112 gn->suffix = s = LstNode_Datum(ln);
2113 gn->suffix->refCount++;
2114 Arch_FindLib(gn, s->searchPath);
2117 Var_Set(TARGET, gn->name, gn);
2120 * Because a library (-lfoo) target doesn't follow the standard
2121 * filesystem conventions, we don't set the regular variables for
2122 * the thing. .PREFIX is simply made empty...
2124 Var_Set(PREFIX, "", gn);
2126 SuffFindNormalDeps(gn, slst);
2130 /* Define which suffix is the null suffix.
2132 * Need to handle the changing of the null suffix gracefully so the old
2133 * transformation rules don't just go away.
2136 * name Name of null suffix
2139 Suff_SetNull(char *name)
2144 ln = Lst_Find(sufflist, SuffSuffHasName, name);
2146 s = LstNode_Datum(ln);
2147 if (suffNull != NULL) {
2148 suffNull->flags &= ~SUFF_NULL;
2150 s->flags |= SUFF_NULL;
2152 * XXX: Here's where the transformation mangling would take place
2156 Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
2161 /* Initialize the suffixes module. */
2166 suffClean = Lst_Init();
2167 sufflist = Lst_Init();
2169 srclist = Lst_Init();
2170 transforms = Lst_Init();
2173 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2174 * actually go on the suffix list or everyone will think that's its
2177 Suff_ClearSuffixes();
2181 /* Clean up the suffixes module. */
2186 Lst_Destroy(sufflist, SuffFree);
2187 Lst_Destroy(suffClean, SuffFree);
2191 Lst_Free(transforms);
2196 /********************* DEBUGGING FUNCTIONS **********************/
2198 static int SuffPrintName(void *s, void *dummy MAKE_ATTR_UNUSED)
2201 fprintf(debug_file, "%s ", ((Suff *)s)->name);
2206 SuffPrintSuff(void *sp, void *dummy MAKE_ATTR_UNUSED)
2208 Suff *s = (Suff *)sp;
2210 fprintf(debug_file, "# `%s' [%d] ", s->name, s->refCount);
2212 if (s->flags != 0) {
2213 char flags_buf[SuffFlags_ToStringSize];
2215 fprintf(debug_file, " (%s)",
2216 Enum_FlagsToString(flags_buf, sizeof flags_buf,
2217 s->flags, SuffFlags_ToStringSpecs));
2219 fputc('\n', debug_file);
2220 fprintf(debug_file, "#\tTo: ");
2221 Lst_ForEach(s->parents, SuffPrintName, NULL);
2222 fputc('\n', debug_file);
2223 fprintf(debug_file, "#\tFrom: ");
2224 Lst_ForEach(s->children, SuffPrintName, NULL);
2225 fputc('\n', debug_file);
2226 fprintf(debug_file, "#\tSearch Path: ");
2227 Dir_PrintPath(s->searchPath);
2228 fputc('\n', debug_file);
2233 SuffPrintTrans(void *tp, void *dummy MAKE_ATTR_UNUSED)
2235 GNode *t = (GNode *)tp;
2237 fprintf(debug_file, "%-16s: ", t->name);
2238 Targ_PrintType(t->type);
2239 fputc('\n', debug_file);
2240 Lst_ForEach(t->commands, Targ_PrintCmd, NULL);
2241 fputc('\n', debug_file);
2248 fprintf(debug_file, "#*** Suffixes:\n");
2249 Lst_ForEach(sufflist, SuffPrintSuff, NULL);
2251 fprintf(debug_file, "#*** Transformations:\n");
2252 Lst_ForEach(transforms, SuffPrintTrans, NULL);