2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1989 by Berkeley Softworks
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)suff.c 8.4 (Berkeley) 3/21/94
39 * $FreeBSD: src/usr.bin/make/suff.c,v 1.12.2.2 2004/06/10 13:07:53 ru Exp $
40 * $DragonFly: src/usr.bin/make/suff.c,v 1.29 2005/01/22 11:34:41 okumoto Exp $
45 * Functions to maintain suffix lists and find implicit dependents
46 * using suffix transformation rules
49 * Suff_Init Initialize all things to do with suffixes.
51 * Suff_DoPaths This function is used to make life easier
52 * when searching for a file according to its
53 * suffix. It takes the global search path,
54 * as defined using the .PATH: target, and appends
55 * its directories to the path of each of the
56 * defined suffixes, as specified using
57 * .PATH<suffix>: targets. In addition, all
58 * directories given for suffixes labeled as
59 * include files or libraries, using the .INCLUDES
60 * or .LIBS targets, are played with using
61 * Dir_MakeFlags to create the .INCLUDES and
62 * .LIBS global variables.
64 * Suff_ClearSuffixes Clear out all the suffixes and defined
67 * Suff_IsTransform Return TRUE if the passed string is the lhs
68 * of a transformation rule.
70 * Suff_AddSuffix Add the passed string as another known suffix.
72 * Suff_GetPath Return the search path for the given suffix.
74 * Suff_AddInclude Mark the given suffix as denoting an include
77 * Suff_AddLib Mark the given suffix as denoting a library.
79 * Suff_AddTransform Add another transformation to the suffix
80 * graph. Returns GNode suitable for framing, I
81 * mean, tacking commands, attributes, etc. on.
83 * Suff_SetNull Define the suffix to consider the suffix of
84 * any file that doesn't have a known one.
86 * Suff_FindDeps Find implicit sources for and the location of
87 * a target based on its suffix. Returns the
88 * bottom-most node added to the graph or NULL
89 * if the target had no implicit sources.
109 /* Lst of suffixes */
110 static Lst sufflist = Lst_Initializer(sufflist);
112 /* Lst of suffixes to be cleaned */
113 static Lst suffClean = Lst_Initializer(suffClean);
116 static Lst srclist = Lst_Initializer(srclist);
118 /* Lst of transformation rules */
119 static Lst transforms = Lst_Initializer(transforms);
121 static int sNum = 0; /* Counter for assigning suffix numbers */
124 * Structure describing an individual suffix.
126 typedef struct _Suff {
127 char *name; /* The suffix itself */
128 int nameLen; /* Length of the suffix */
129 short flags; /* Type of suffix */
130 #define SUFF_INCLUDE 0x01 /* One which is #include'd */
131 #define SUFF_LIBRARY 0x02 /* One which contains a library */
132 #define SUFF_NULL 0x04 /* The empty suffix */
133 Lst searchPath; /* The path along which files of this suffix
135 int sNum; /* The suffix number */
136 int refCount; /* Reference count of list membership */
137 Lst parents; /* Suffixes we have a transformation to */
138 Lst children; /* Suffixes we have a transformation from */
139 Lst ref; /* List of lists this suffix is referenced */
143 * Structure used in the search for implied sources.
145 typedef struct _Src {
146 char *file; /* The file to look for */
147 char *pref; /* Prefix from which file was formed */
148 Suff *suff; /* The suffix on the file */
149 struct _Src *parent; /* The Src for which this is a source */
150 GNode *node; /* The node describing the file */
151 int children; /* Count of existing children (so we don't free
152 * this thing too early or never nuke it) */
154 Lst cp; /* Debug; children list */
159 * A structure for passing more than one argument to the Lst-library-invoked
167 static Suff *suffNull; /* The NULL suffix for this run */
168 static Suff *emptySuff; /* The empty suffix required for POSIX
169 * single-suffix transformation rules */
172 static void SuffInsert(Lst *, Suff *);
173 static void SuffRemove(Lst *, Suff *);
174 static Boolean SuffParseTransform(char *, Suff **, Suff **);
175 static int SuffRebuildGraph(void *, void *);
176 static int SuffAddSrc(void *, void *);
177 static int SuffRemoveSrc(Lst *);
178 static void SuffAddLevel(Lst *, Src *);
179 static Src *SuffFindThem(Lst *, Lst *);
180 static Src *SuffFindCmds(Src *, Lst *);
181 static int SuffExpandChildren(void *, void *);
182 static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
183 static void SuffFindDeps(GNode *, Lst *);
184 static void SuffFindArchiveDeps(GNode *, Lst *);
185 static void SuffFindNormalDeps(GNode *, Lst *);
186 static int SuffPrintName(void *, void *);
187 static int SuffPrintSuff(void *, void *);
188 static int SuffPrintTrans(void *, void *);
190 static int PrintAddr(void *, void *);
191 #endif /* DEBUG_SRC */
194 /*************** Lst Predicates ****************/
196 *-----------------------------------------------------------------------
198 * See if pref is a prefix of str.
201 * NULL if it ain't, pointer to character in str after prefix if so
205 *-----------------------------------------------------------------------
208 SuffStrIsPrefix(const char *pref, char *str)
211 while (*str && *pref == *str) {
216 return (*pref ? NULL : str);
220 *-----------------------------------------------------------------------
221 * SuffSuffIsSuffix --
222 * See if suff is a suffix of str. Str should point to THE END of the
223 * string to check. (THE END == the null byte)
226 * NULL if it ain't, pointer to character in str before suffix if
231 *-----------------------------------------------------------------------
234 SuffSuffIsSuffix(const Suff *s, char *str)
236 const char *p1; /* Pointer into suffix name */
237 char *p2; /* Pointer into string being examined */
239 p1 = s->name + s->nameLen;
242 while (p1 >= s->name && *p1 == *p2) {
247 return (p1 == s->name - 1 ? p2 : NULL);
251 *-----------------------------------------------------------------------
252 * SuffSuffIsSuffixP --
253 * Predicate form of SuffSuffIsSuffix. Passed as the callback function
257 * 0 if the suffix is the one desired, non-zero if not.
262 * XXX use the function above once constification is complete.
263 *-----------------------------------------------------------------------
266 SuffSuffIsSuffixP(const void *is, const void *str)
269 const char *p1; /* Pointer into suffix name */
270 const char *p2 = str; /* Pointer into string being examined */
272 p1 = s->name + s->nameLen;
274 while (p1 >= s->name && *p1 == *p2) {
279 return (p1 != s->name - 1);
283 *-----------------------------------------------------------------------
284 * SuffSuffHasNameP --
285 * Callback procedure for finding a suffix based on its name. Used by
289 * 0 if the suffix is of the given name. non-zero otherwise.
293 *-----------------------------------------------------------------------
296 SuffSuffHasNameP(const void *s, const void *sname)
299 return (strcmp(sname, ((const Suff *)s)->name));
303 *-----------------------------------------------------------------------
304 * SuffSuffIsPrefix --
305 * See if the suffix described by s is a prefix of the string. Care
306 * must be taken when using this to search for transformations and
307 * what-not, since there could well be two suffixes, one of which
308 * is a prefix of the other...
311 * 0 if s is a prefix of str. non-zero otherwise
316 * XXX use the function above once constification is complete.
317 *-----------------------------------------------------------------------
320 SuffSuffIsPrefix(const void *s, const void *istr)
322 const char *pref = ((const Suff *)s)->name;
323 const char *str = istr;
325 while (*str != '\0' && *pref == *str) {
330 return (*pref != '\0');
334 *-----------------------------------------------------------------------
336 * See if the graph node has the desired name
339 * 0 if it does. non-zero if it doesn't
343 *-----------------------------------------------------------------------
346 SuffGNHasNameP(const void *gn, const void *name)
349 return (strcmp(name, ((const GNode *)gn)->name));
352 /*********** Maintenance Functions ************/
356 * Keep this function for now until it is clear why a .SUFFIXES: doesn't
357 * actually delete the suffixes but just puts them on the suffClean list.
360 *-----------------------------------------------------------------------
362 * Free up all memory associated with the given suffix structure.
368 * the suffix entry is detroyed
369 *-----------------------------------------------------------------------
382 Lst_Destroy(&s->ref, NOFREE);
383 Lst_Destroy(&s->children, NOFREE);
384 Lst_Destroy(&s->parents, NOFREE);
385 Lst_Destroy(&s->searchPath, Dir_Destroy);
393 *-----------------------------------------------------------------------
395 * Remove the suffix into the list
401 * The reference count for the suffix is decremented
402 *-----------------------------------------------------------------------
405 SuffRemove(Lst *l, Suff *s)
407 LstNode *ln = Lst_Member(l, s);
416 *-----------------------------------------------------------------------
418 * Insert the suffix into the list keeping the list ordered by suffix
425 * The reference count of the suffix is incremented
426 *-----------------------------------------------------------------------
429 SuffInsert(Lst *l, Suff *s)
431 LstNode *ln; /* current element in l we're examining */
432 Suff *s2; /* the suffix descriptor in this element */
435 for (ln = Lst_First(l); ln != NULL; ln = Lst_Succ(ln)) {
437 if (s2->sNum >= s->sNum)
441 DEBUGF(SUFF, ("inserting an empty list?..."));
444 DEBUGF(SUFF, ("inserting %s(%d)...", s->name, s->sNum));
446 DEBUGF(SUFF, ("at end of list\n"));
449 Lst_AtEnd(&s->ref, l);
450 } else if (s2->sNum != s->sNum) {
451 DEBUGF(SUFF, ("before %s(%d)\n", s2->name, s2->sNum));
452 Lst_Insert(l, ln, s);
454 Lst_AtEnd(&s->ref, l);
456 DEBUGF(SUFF, ("already there\n"));
461 *-----------------------------------------------------------------------
462 * Suff_ClearSuffixes --
463 * This is gross. Nuke the list of suffixes but keep all transformation
464 * rules around. The transformation graph is destroyed in this process,
465 * but we leave the list of rules so when a new graph is formed the rules
467 * This function is called from the parse module when a
468 * .SUFFIXES:\n line is encountered.
474 * the sufflist and its graph nodes are destroyed
475 *-----------------------------------------------------------------------
478 Suff_ClearSuffixes(void)
481 Lst_Concat(&suffClean, &sufflist, LST_CONCLINK);
484 suffNull = emptySuff;
486 * Clear suffNull's children list (the other suffixes are built new, but
487 * suffNull is used as is).
488 * NOFREE is used because all suffixes are are on the suffClean list.
489 * suffNull should not have parents.
491 Lst_Destroy(&suffNull->children, NOFREE);
495 *-----------------------------------------------------------------------
496 * SuffParseTransform --
497 * Parse a transformation string to find its two component suffixes.
500 * TRUE if the string is a valid transformation and FALSE otherwise.
503 * The passed pointers are overwritten.
505 *-----------------------------------------------------------------------
508 SuffParseTransform(char *str, Suff **srcPtr, Suff **targPtr)
510 LstNode *srcLn; /* element in suffix list of trans source*/
511 Suff *src; /* Source of transformation */
512 LstNode *targLn; /* element in suffix list of trans target*/
513 char *str2; /* Extra pointer (maybe target suffix) */
514 LstNode *singleLn; /* element in suffix list of any suffix
515 * that exactly matches str */
516 Suff *single = NULL;/* Source of possible transformation to
523 * Loop looking first for a suffix that matches the start of the
524 * string and then for one that exactly matches the rest of it. If
525 * we can find two that meet these criteria, we've successfully
530 srcLn = Lst_Find(&sufflist, str, SuffSuffIsPrefix);
532 srcLn = Lst_FindFrom(&sufflist, Lst_Succ(srcLn), str,
537 * Ran out of source suffixes -- no such rule
539 if (singleLn != NULL) {
541 * Not so fast Mr. Smith! There was a suffix that encompassed
542 * the entire string, so we assume it was a transformation
543 * to the null suffix (thank you POSIX). We still prefer to
544 * find a double rule over a singleton, hence we leave this
545 * check until the end.
547 * XXX: Use emptySuff over suffNull?
555 src = Lst_Datum(srcLn);
556 str2 = str + src->nameLen;
561 targLn = Lst_Find(&sufflist, str2, SuffSuffHasNameP);
562 if (targLn != NULL) {
564 *targPtr = Lst_Datum(targLn);
572 *-----------------------------------------------------------------------
573 * Suff_IsTransform --
574 * Return TRUE if the given string is a transformation rule
578 * TRUE if the string is a concatenation of two known suffixes.
583 *-----------------------------------------------------------------------
586 Suff_IsTransform(char *str)
590 return (SuffParseTransform(str, &src, &targ));
594 *-----------------------------------------------------------------------
595 * Suff_AddTransform --
596 * Add the transformation rule described by the line to the
597 * list of rules and place the transformation itself in the graph
600 * The node created for the transformation in the transforms list
603 * The node is placed on the end of the transforms Lst and links are
604 * made between the two suffixes mentioned in the target name
605 *-----------------------------------------------------------------------
608 Suff_AddTransform(char *line)
610 GNode *gn; /* GNode of transformation rule */
611 Suff *s, /* source suffix */
612 *t; /* target suffix */
613 LstNode *ln; /* Node for existing transformation */
615 ln = Lst_Find(&transforms, line, SuffGNHasNameP);
618 * Make a new graph node for the transformation. It will be filled in
619 * by the Parse module.
621 gn = Targ_NewGN(line);
622 Lst_AtEnd(&transforms, gn);
625 * New specification for transformation rule. Just nuke the old list
626 * of commands so they can be filled in again... We don't actually
627 * free the commands themselves, because a given command can be
628 * attached to several different transformations.
631 Lst_Destroy(&gn->commands, NOFREE);
632 Lst_Destroy(&gn->children, NOFREE);
635 gn->type = OP_TRANSFORM;
637 SuffParseTransform(line, &s, &t);
640 * link the two together in the proper relationship and order
642 DEBUGF(SUFF, ("defining transformation from `%s' to `%s'\n",
644 SuffInsert(&t->children, s);
645 SuffInsert(&s->parents, t);
651 *-----------------------------------------------------------------------
652 * Suff_EndTransform --
653 * Handle the finish of a transformation definition, removing the
654 * transformation from the graph if it has neither commands nor
655 * sources. This is a callback procedure for the Parse module via
662 * If the node has no commands or children, the children and parents
663 * lists of the affected suffices are altered.
665 *-----------------------------------------------------------------------
668 Suff_EndTransform(void *gnp, void *dummy __unused)
670 GNode *gn = (GNode *)gnp;
672 if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(&gn->commands) &&
673 Lst_IsEmpty(&gn->children))
678 * SuffParseTransform() may fail for special rules which are not
679 * actual transformation rules (e.g., .DEFAULT).
681 if (!SuffParseTransform(gn->name, &s, &t))
684 DEBUGF(SUFF, ("deleting transformation from `%s' to `%s'\n",
688 * Remove the source from the target's children list. We check for a
689 * NULL return to handle a beanhead saying something like
692 * We'll be called twice when the next target is seen, but .c and .o
693 * are only linked once...
695 SuffRemove(&t->children, s);
698 * Remove the target from the source's parents list
700 SuffRemove(&s->parents, t);
701 } else if (gn->type & OP_TRANSFORM) {
702 DEBUGF(SUFF, ("transformation %s complete\n", gn->name));
709 *-----------------------------------------------------------------------
710 * SuffRebuildGraph --
711 * Called from Suff_AddSuffix via Lst_ForEach to search through the
712 * list of existing transformation rules and rebuild the transformation
713 * graph when it has been destroyed by Suff_ClearSuffixes. If the
714 * given rule is a transformation involving this suffix and another,
715 * existing suffix, the proper relationship is established between
722 * The appropriate links will be made between this suffix and
723 * others if transformation rules exist for it.
725 *-----------------------------------------------------------------------
728 SuffRebuildGraph(void *transformp, void *sp)
730 GNode *transform = transformp;
737 * First see if it is a transformation from this suffix.
739 cp = SuffStrIsPrefix(s->name, transform->name);
740 if (cp != (char *)NULL) {
741 if (cp[0] == '\0') /* null rule */
744 ln = Lst_Find(&sufflist, cp, SuffSuffHasNameP);
750 * Found target. Link in and return, since it can't be anything
753 SuffInsert(&s2->children, s);
754 SuffInsert(&s->parents, s2);
760 * Not from, maybe to?
762 cp = SuffSuffIsSuffix(s, transform->name + strlen(transform->name));
765 * Null-terminate the source suffix in order to find it.
768 ln = Lst_Find(&sufflist, transform->name, SuffSuffHasNameP);
770 * Replace the start of the target suffix
775 * Found it -- establish the proper relationship
778 SuffInsert(&s->children, s2);
779 SuffInsert(&s2->parents, s);
786 *-----------------------------------------------------------------------
788 * Add the suffix in string to the end of the list of known suffixes.
789 * Should we restructure the suffix graph? Make doesn't...
795 * A GNode is created for the suffix and a Suff structure is created and
796 * added to the suffixes list unless the suffix was already known.
797 *-----------------------------------------------------------------------
800 Suff_AddSuffix(char *str)
802 Suff *s; /* new suffix descriptor */
805 ln = Lst_Find(&sufflist, str, SuffSuffHasNameP);
807 s = emalloc(sizeof(Suff));
809 s->name = estrdup(str);
810 s->nameLen = strlen(s->name);
811 Lst_Init(&s->searchPath);
812 Lst_Init(&s->children);
813 Lst_Init(&s->parents);
819 Lst_AtEnd(&sufflist, s);
822 * Look for any existing transformations from or to this suffix.
823 * XXX: Only do this after a Suff_ClearSuffixes?
825 Lst_ForEach(&transforms, SuffRebuildGraph, s);
830 *-----------------------------------------------------------------------
832 * Return the search path for the given suffix, if it's defined.
835 * The searchPath for the desired suffix or NULL if the suffix isn't
840 *-----------------------------------------------------------------------
843 Suff_GetPath(char *sname)
848 ln = Lst_Find(&sufflist, sname, SuffSuffHasNameP);
853 return (&s->searchPath);
858 *-----------------------------------------------------------------------
860 * Extend the search paths for all suffixes to include the default
867 * The searchPath field of all the suffixes is extended by the
868 * directories in dirSearchPath. If paths were specified for the
869 * ".h" suffix, the directories are stuffed into a global variable
870 * called ".INCLUDES" with each directory preceded by a -I. The same
871 * is done for the ".a" suffix, except the variable is called
872 * ".LIBS" and the flag is -L.
873 *-----------------------------------------------------------------------
881 Lst inIncludes; /* Cumulative .INCLUDES path */
882 Lst inLibs; /* Cumulative .LIBS path */
884 Lst_Init(&inIncludes);
887 for (ln = Lst_First(&sufflist); ln != NULL; ln = Lst_Succ(ln)) {
889 if (!Lst_IsEmpty(&s->searchPath)) {
891 if (s->flags & SUFF_INCLUDE) {
892 Dir_Concat(&inIncludes, &s->searchPath);
894 #endif /* INCLUDES */
896 if (s->flags & SUFF_LIBRARY) {
897 Dir_Concat(&inLibs, &s->searchPath);
899 #endif /* LIBRARIES */
900 Dir_Concat(&s->searchPath, &dirSearchPath);
902 Lst_Destroy(&s->searchPath, Dir_Destroy);
903 Lst_Duplicate(&s->searchPath, &dirSearchPath, Dir_CopyDir);
907 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", &inIncludes), VAR_GLOBAL);
909 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", &inLibs), VAR_GLOBAL);
912 Lst_Destroy(&inIncludes, Dir_Destroy);
913 Lst_Destroy(&inLibs, Dir_Destroy);
917 *-----------------------------------------------------------------------
919 * Add the given suffix as a type of file which gets included.
920 * Called from the parse module when a .INCLUDES line is parsed.
921 * The suffix must have already been defined.
927 * The SUFF_INCLUDE bit is set in the suffix's flags field
929 *-----------------------------------------------------------------------
932 Suff_AddInclude(char *sname)
937 ln = Lst_Find(&sufflist, sname, SuffSuffHasNameP);
940 s->flags |= SUFF_INCLUDE;
945 *-----------------------------------------------------------------------
947 * Add the given suffix as a type of file which is a library.
948 * Called from the parse module when parsing a .LIBS line. The
949 * suffix must have been defined via .SUFFIXES before this is
956 * The SUFF_LIBRARY bit is set in the suffix's flags field
958 *-----------------------------------------------------------------------
961 Suff_AddLib(char *sname)
966 ln = Lst_Find(&sufflist, sname, SuffSuffHasNameP);
969 s->flags |= SUFF_LIBRARY;
973 /********** Implicit Source Search Functions *********/
976 *-----------------------------------------------------------------------
978 * Add a suffix as a Src structure to the given list with its parent
979 * being the given Src structure. If the suffix is the null suffix,
980 * the prefix is used unaltered as the file name in the Src structure.
986 * A Src structure is created and tacked onto the end of the list
987 *-----------------------------------------------------------------------
990 SuffAddSrc(void *sp, void *lsp)
994 Src *s2; /* new Src structure */
995 Src *targ; /* Target structure */
999 if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
1001 * If the suffix has been marked as the NULL suffix, also create a Src
1002 * structure for a file with no suffix attached. Two birds, and all
1005 s2 = emalloc(sizeof(Src));
1006 s2->file = estrdup(targ->pref);
1007 s2->pref = targ->pref;
1013 targ->children += 1;
1014 Lst_AtEnd(ls->l, s2);
1017 Lst_AtEnd(&targ->cp, s2);
1018 printf("1 add %p %p to %p:", targ, s2, ls->l);
1019 Lst_ForEach(ls->l, PrintAddr, (void *)NULL);
1023 s2 = emalloc(sizeof(Src));
1024 s2->file = str_concat(targ->pref, s->name, 0);
1025 s2->pref = targ->pref;
1031 targ->children += 1;
1032 Lst_AtEnd(ls->l, s2);
1035 Lst_AtEnd(&targ->cp, s2);
1036 printf("2 add %p %p to %p:", targ, s2, ls->l);
1037 Lst_ForEach(ls->l, PrintAddr, (void *)NULL);
1045 *-----------------------------------------------------------------------
1047 * Add all the children of targ as Src structures to the given list
1053 * Lots of structures are created and added to the list
1054 *-----------------------------------------------------------------------
1057 SuffAddLevel(Lst *l, Src *targ)
1064 Lst_ForEach(&targ->suff->children, SuffAddSrc, &ls);
1068 *----------------------------------------------------------------------
1070 * Free all src structures in list that don't have a reference count
1071 * XXX this actually frees only the first of these.
1074 * True if a src was removed
1077 * The memory is free'd.
1078 *----------------------------------------------------------------------
1081 SuffRemoveSrc(Lst *l)
1088 printf("cleaning %lx: ", (unsigned long) l);
1089 Lst_ForEach(l, PrintAddr, (void *)NULL);
1093 for (ln = Lst_First(l); ln != NULL; ln = ln1) {
1096 s = (Src *)Lst_Datum(ln);
1097 if (s->children == 0) {
1103 LstNode *ln = Lst_Member(&s->parent->cp, s);
1105 Lst_Remove(&s->parent->cp, ln);
1107 --s->parent->children;
1110 printf("free: [l=%p] p=%p %d\n", l, s, s->children);
1111 Lst_Destroy(&s->cp, NOFREE);
1120 printf("keep: [l=%p] p=%p %d: ", l, s, s->children);
1121 Lst_ForEach(&s->cp, PrintAddr, (void *)NULL);
1131 *-----------------------------------------------------------------------
1133 * Find the first existing file/target in the list srcs
1136 * The lowest structure in the chain of transformations
1140 *-----------------------------------------------------------------------
1143 SuffFindThem(Lst *srcs, Lst *slst)
1145 Src *s; /* current Src */
1146 Src *rs; /* returned Src */
1151 while (!Lst_IsEmpty (srcs)) {
1152 s = Lst_DeQueue(srcs);
1154 DEBUGF(SUFF, ("\ttrying %s...", s->file));
1157 * A file is considered to exist if either a node exists in the
1158 * graph for it or the file actually exists.
1160 if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
1162 printf("remove %p from %p\n", s, srcs);
1168 if ((ptr = Dir_FindFile(s->file, &s->suff->searchPath)) != NULL) {
1171 printf("remove %p from %p\n", s, srcs);
1177 DEBUGF(SUFF, ("not there\n"));
1179 SuffAddLevel(srcs, s);
1184 DEBUGF(SUFF, ("got it\n"));
1190 *-----------------------------------------------------------------------
1192 * See if any of the children of the target in the Src structure is
1193 * one from which the target can be transformed. If there is one,
1194 * a Src structure is put together for it and returned.
1197 * The Src structure of the "winning" child, or NULL if no such beast.
1200 * A Src structure may be allocated.
1202 *-----------------------------------------------------------------------
1205 SuffFindCmds(Src *targ, Lst *slst)
1207 LstNode *ln; /* General-purpose list node */
1208 GNode *t, /* Target GNode */
1209 *s; /* Source GNode */
1210 int prefLen;/* The length of the defined prefix */
1211 Suff *suff; /* Suffix on matching beastie */
1212 Src *ret; /* Return value */
1216 prefLen = strlen(targ->pref);
1218 for (ln = Lst_First(&t->children); ln != NULL; ln = Lst_Succ(ln)) {
1221 cp = strrchr(s->name, '/');
1227 if (strncmp(cp, targ->pref, prefLen) == 0) {
1229 * The node matches the prefix ok, see if it has a known
1232 ln = Lst_Find(&sufflist, &cp[prefLen], SuffSuffHasNameP);
1235 * It even has a known suffix, see if there's a transformation
1236 * defined between the node's suffix and the target's suffix.
1238 * XXX: Handle multi-stage transformations here, too.
1240 suff = Lst_Datum(ln);
1242 if (Lst_Member(&suff->parents, targ->suff) != NULL) {
1244 * Hot Damn! Create a new Src structure to describe
1245 * this transformation (making sure to duplicate the
1246 * source node's name so Suff_FindDeps can free it
1247 * again (ick)), and return the new structure.
1249 ret = emalloc(sizeof(Src));
1250 ret->file = estrdup(s->name);
1251 ret->pref = targ->pref;
1257 targ->children += 1;
1260 printf("3 add %p %p\n", &targ, ret);
1261 Lst_AtEnd(&targ->cp, ret);
1263 Lst_AtEnd(slst, ret);
1264 DEBUGF(SUFF, ("\tusing existing source %s\n", s->name));
1274 *-----------------------------------------------------------------------
1275 * SuffExpandChildren --
1276 * Expand the names of any children of a given node that contain
1277 * variable invocations or file wildcards into actual targets.
1283 * The expanded node is removed from the parent's list of children,
1284 * and the parent's unmade counter is decremented, but other nodes
1287 *-----------------------------------------------------------------------
1290 SuffExpandChildren(void *cgnp, void *pgnp)
1294 GNode *gn; /* New source 8) */
1295 LstNode *prevLN; /* Node after which new source should be put */
1296 LstNode *ln; /* List element for old source */
1297 char *cp; /* Expanded value */
1300 * New nodes effectively take the place of the child, so place them
1303 prevLN = Lst_Member(&pgn->children, cgn);
1306 * First do variable expansion -- this takes precedence over
1307 * wildcard expansion. If the result contains wildcards, they'll be gotten
1308 * to later since the resulting words are tacked on to the end of
1309 * the children list.
1311 if (strchr(cgn->name, '$') != NULL) {
1312 DEBUGF(SUFF, ("Expanding \"%s\"...", cgn->name));
1313 cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
1316 Lst members = Lst_Initializer(members);
1318 if (cgn->type & OP_ARCHV) {
1320 * Node was an archive(member) target, so we want to call
1321 * on the Arch module to find the nodes for us, expanding
1322 * variables in the parent's context.
1324 char *sacrifice = cp;
1326 Arch_ParseArchive(&sacrifice, &members, pgn);
1329 * Break the result into a vector of strings whose nodes
1330 * we can find, then add those nodes to the members list.
1331 * Unfortunately, we can't use brk_string b/c it
1332 * doesn't understand about variable specifications with
1336 char *initcp = cp; /* For freeing... */
1338 for (start = cp; *start == ' ' || *start == '\t'; start++)
1340 for (cp = start; *cp != '\0'; cp++) {
1341 if (*cp == ' ' || *cp == '\t') {
1343 * White-space -- terminate element, find the node,
1344 * add it, skip any further spaces.
1347 gn = Targ_FindNode(start, TARG_CREATE);
1348 Lst_AtEnd(&members, gn);
1349 while (*cp == ' ' || *cp == '\t') {
1353 * Adjust cp for increment at start of loop, but
1354 * set start to first non-space.
1357 } else if (*cp == '$') {
1359 * Start of a variable spec -- contact variable module
1360 * to find the end so we can skip over it.
1366 junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
1367 if (junk != var_Error) {
1374 } else if (*cp == '\\' && *cp != '\0') {
1376 * Escaped something -- skip over it
1384 * Stuff left over -- add it to the list too
1386 gn = Targ_FindNode(start, TARG_CREATE);
1387 Lst_AtEnd(&members, gn);
1390 * Point cp back at the beginning again so the variable value
1396 * Add all elements of the members list to the parent node.
1398 while(!Lst_IsEmpty(&members)) {
1399 gn = Lst_DeQueue(&members);
1401 DEBUGF(SUFF, ("%s...", gn->name));
1402 if (Lst_Member(&pgn->children, gn) == NULL) {
1403 Lst_Append(&pgn->children, prevLN, gn);
1404 prevLN = Lst_Succ(prevLN);
1405 Lst_AtEnd(&gn->parents, pgn);
1416 * Now the source is expanded, remove it from the list of children to
1417 * keep it from being processed.
1419 ln = Lst_Member(&pgn->children, cgn);
1421 Lst_Remove(&pgn->children, ln);
1422 DEBUGF(SUFF, ("\n"));
1423 } else if (Dir_HasWildcards(cgn->name)) {
1424 Lst exp; /* List of expansions */
1425 Lst *path; /* Search path along which to expand */
1428 * Find a path along which to expand the word.
1430 * If the word has a known suffix, use that path.
1431 * If it has no known suffix and we're allowed to use the null
1432 * suffix, use its path.
1433 * Else use the default system search path.
1435 cp = cgn->name + strlen(cgn->name);
1436 ln = Lst_Find(&sufflist, cp, SuffSuffIsSuffixP);
1438 DEBUGF(SUFF, ("Wildcard expanding \"%s\"...", cgn->name));
1441 Suff *s = Lst_Datum(ln);
1443 DEBUGF(SUFF, ("suffix is \"%s\"...", s->name));
1444 path = &s->searchPath;
1447 * Use default search path
1449 path = &dirSearchPath;
1453 * Expand the word along the chosen path
1456 Dir_Expand(cgn->name, path, &exp);
1458 while (!Lst_IsEmpty(&exp)) {
1460 * Fetch next expansion off the list and find its GNode
1462 cp = Lst_DeQueue(&exp);
1464 DEBUGF(SUFF, ("%s...", cp));
1465 gn = Targ_FindNode(cp, TARG_CREATE);
1468 * If gn isn't already a child of the parent, make it so and
1469 * up the parent's count of unmade children.
1471 if (Lst_Member(&pgn->children, gn) == NULL) {
1472 Lst_Append(&pgn->children, prevLN, gn);
1473 prevLN = Lst_Succ(prevLN);
1474 Lst_AtEnd(&gn->parents, pgn);
1480 * Now the source is expanded, remove it from the list of children to
1481 * keep it from being processed.
1483 ln = Lst_Member(&pgn->children, cgn);
1485 Lst_Remove(&pgn->children, ln);
1486 DEBUGF(SUFF, ("\n"));
1493 *-----------------------------------------------------------------------
1494 * SuffApplyTransform --
1495 * Apply a transformation rule, given the source and target nodes
1499 * TRUE if successful, FALSE if not.
1502 * The source and target are linked and the commands from the
1503 * transformation are added to the target node's commands list.
1504 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1505 * to the target. The target also inherits all the sources for
1506 * the transformation rule.
1508 *-----------------------------------------------------------------------
1511 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
1513 LstNode *ln; /* General node */
1514 char *tname; /* Name of transformation rule */
1515 GNode *gn; /* Node for same */
1517 if (Lst_Member(&tGn->children, sGn) == NULL) {
1519 * Not already linked, so form the proper links between the
1520 * target and source.
1522 Lst_AtEnd(&tGn->children, sGn);
1523 Lst_AtEnd(&sGn->parents, tGn);
1527 if ((sGn->type & OP_OPMASK) == OP_DOUBLEDEP) {
1529 * When a :: node is used as the implied source of a node, we have
1530 * to link all its cohorts in as sources as well. Only the initial
1531 * sGn gets the target in its iParents list, however, as that
1532 * will be sufficient to get the .IMPSRC variable set for tGn
1534 for (ln = Lst_First(&sGn->cohorts); ln != NULL; ln = Lst_Succ(ln)) {
1537 if (Lst_Member(&tGn->children, gn) == NULL) {
1539 * Not already linked, so form the proper links between the
1540 * target and source.
1542 Lst_AtEnd(&tGn->children, gn);
1543 Lst_AtEnd(&gn->parents, tGn);
1549 * Locate the transformation rule itself
1551 tname = str_concat(s->name, t->name, 0);
1552 ln = Lst_Find(&transforms, tname, SuffGNHasNameP);
1557 * Not really such a transformation rule (can happen when we're
1558 * called to link an OP_MEMBER and OP_ARCHV node), so return
1566 DEBUGF(SUFF, ("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name));
1569 * Record last child for expansion purposes
1571 ln = Lst_Last(&tGn->children);
1574 * Pass the buck to Make_HandleUse to apply the rule
1576 Make_HandleUse(gn, tGn);
1579 * Deal with wildcards and variables in any acquired sources
1583 Lst_ForEachFrom(&tGn->children, ln, SuffExpandChildren, tGn);
1587 * Keep track of another parent to which this beast is transformed so
1588 * the .IMPSRC variable can be set correctly for the parent.
1590 Lst_AtEnd(&sGn->iParents, tGn);
1597 *-----------------------------------------------------------------------
1598 * SuffFindArchiveDeps --
1599 * Locate dependencies for an OP_ARCHV node.
1605 * Same as Suff_FindDeps
1607 *-----------------------------------------------------------------------
1610 SuffFindArchiveDeps(GNode *gn, Lst *slst)
1612 char *eoarch; /* End of archive portion */
1613 char *eoname; /* End of member portion */
1614 GNode *mem; /* Node for member */
1615 static const char *copy[] = { /* Variables to be copied from the member node */
1616 TARGET, /* Must be first */
1617 PREFIX, /* Must be second */
1619 int i; /* Index into copy and vals */
1620 Suff *ms; /* Suffix descriptor for member */
1621 char *name; /* Start of member's name */
1624 * The node is an archive(member) pair. so we must find a
1625 * suffix for both of them.
1627 eoarch = strchr(gn->name, '(');
1628 eoname = strchr(eoarch, ')');
1630 *eoname = '\0'; /* Nuke parentheses during suffix search */
1631 *eoarch = '\0'; /* So a suffix can be found */
1636 * To simplify things, call Suff_FindDeps recursively on the member now,
1637 * so we can simply compare the member's .PREFIX and .TARGET variables
1638 * to locate its suffix. This allows us to figure out the suffix to
1639 * use for the archive without having to do a quadratic search over the
1640 * suffix list, backtracking for each one...
1642 mem = Targ_FindNode(name, TARG_CREATE);
1643 SuffFindDeps(mem, slst);
1646 * Create the link between the two nodes right off
1648 if (Lst_Member(&gn->children, mem) == NULL) {
1649 Lst_AtEnd(&gn->children, mem);
1650 Lst_AtEnd(&mem->parents, gn);
1655 * Copy in the variables from the member node to this one.
1657 for (i = (sizeof(copy) / sizeof(copy[0]))-1; i >= 0; i--) {
1659 Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn);
1667 * Didn't know what it was -- use .NULL suffix if not in make mode
1669 DEBUGF(SUFF, ("using null suffix\n"));
1675 * Set the other two local variables required for this target.
1677 Var_Set(MEMBER, name, gn);
1678 Var_Set(ARCHIVE, gn->name, gn);
1682 * Member has a known suffix, so look for a transformation rule from
1683 * it to a possible suffix of the archive. Rather than searching
1684 * through the entire list, we just look at suffixes to which the
1685 * member's suffix may be transformed...
1690 * Use first matching suffix...
1692 ln = Lst_Find(&ms->parents, eoarch, SuffSuffIsSuffixP);
1696 * Got one -- apply it
1698 if (!SuffApplyTransform(gn, mem, Lst_Datum(ln), ms)) {
1699 DEBUGF(SUFF, ("\tNo transformation from %s -> %s\n",
1700 ms->name, ((Suff *)Lst_Datum(ln))->name));
1706 * Replace the opening and closing parens now we've no need of the separate
1709 *eoarch = '('; *eoname = ')';
1712 * Pretend gn appeared to the left of a dependency operator so
1713 * the user needn't provide a transformation from the member to the
1716 if (OP_NOP(gn->type)) {
1717 gn->type |= OP_DEPENDS;
1721 * Flag the member as such so we remember to look in the archive for
1722 * its modification time.
1724 mem->type |= OP_MEMBER;
1728 *-----------------------------------------------------------------------
1729 * SuffFindNormalDeps --
1730 * Locate implicit dependencies for regular targets.
1736 * Same as Suff_FindDeps...
1738 *-----------------------------------------------------------------------
1741 SuffFindNormalDeps(GNode *gn, Lst *slst)
1743 char *eoname; /* End of name */
1744 char *sopref; /* Start of prefix */
1745 LstNode *ln; /* Next suffix node to check */
1746 Lst srcs; /* List of sources at which to look */
1747 Lst targs; /* List of targets to which things can be
1748 * transformed. They all have the same file,
1749 * but different suff and pref fields */
1750 Src *bottom; /* Start of found transformation path */
1751 Src *src; /* General Src pointer */
1752 char *pref; /* Prefix to use */
1753 Src *targ; /* General Src target pointer */
1756 eoname = gn->name + strlen(gn->name);
1761 * Begin at the beginning...
1763 ln = Lst_First(&sufflist);
1768 * We're caught in a catch-22 here. On the one hand, we want to use any
1769 * transformation implied by the target's sources, but we can't examine
1770 * the sources until we've expanded any variables/wildcards they may hold,
1771 * and we can't do that until we've set up the target's local variables
1772 * and we can't do that until we know what the proper suffix for the
1773 * target is (in case there are two suffixes one of which is a suffix of
1774 * the other) and we can't know that until we've found its implied
1775 * source, which we may not want to use if there's an existing source
1776 * that implies a different transformation.
1778 * In an attempt to get around this, which may not work all the time,
1779 * but should work most of the time, we look for implied sources first,
1780 * checking transformations to all possible suffixes of the target,
1781 * use what we find to set the target's local variables, expand the
1782 * children, then look for any overriding transformations they imply.
1783 * Should we find one, we discard the one we found before.
1786 while (ln != NULL) {
1788 * Look for next possible suffix...
1790 ln = Lst_FindFrom(&sufflist, ln, eoname, SuffSuffIsSuffixP);
1793 int prefLen; /* Length of the prefix */
1797 * Allocate a Src structure to which things can be transformed
1799 target = emalloc(sizeof(Src));
1800 target->file = estrdup(gn->name);
1801 target->suff = Lst_Datum(ln);
1802 target->suff->refCount++;
1804 target->parent = NULL;
1805 target->children = 0;
1807 Lst_Init(&target->cp);
1811 * Allocate room for the prefix, whose end is found by subtracting
1812 * the length of the suffix from the end of the name.
1814 prefLen = (eoname - target->suff->nameLen) - sopref;
1815 target->pref = emalloc(prefLen + 1);
1816 memcpy(target->pref, sopref, prefLen);
1817 target->pref[prefLen] = '\0';
1820 * Add nodes from which the target can be made
1822 SuffAddLevel(&srcs, target);
1825 * Record the target so we can nuke it
1827 Lst_AtEnd(&targs, target);
1830 * Search from this suffix's successor...
1837 * Handle target of unknown suffix...
1839 if (Lst_IsEmpty(&targs) && suffNull != NULL) {
1840 DEBUGF(SUFF, ("\tNo known suffix on %s. Using .NULL suffix\n", gn->name));
1842 targ = emalloc(sizeof(Src));
1843 targ->file = estrdup(gn->name);
1844 targ->suff = suffNull;
1845 targ->suff->refCount++;
1847 targ->parent = NULL;
1849 targ->pref = estrdup(sopref);
1851 Lst_Init(&targ->cp);
1855 * Only use the default suffix rules if we don't have commands
1856 * or dependencies defined for this gnode
1858 if (Lst_IsEmpty(&gn->commands) && Lst_IsEmpty(&gn->children))
1859 SuffAddLevel(&srcs, targ);
1861 DEBUGF(SUFF, ("not "));
1864 DEBUGF(SUFF, ("adding suffix rules\n"));
1866 Lst_AtEnd(&targs, targ);
1870 * Using the list of possible sources built up from the target suffix(es),
1871 * try and find an existing file/target that matches.
1873 bottom = SuffFindThem(&srcs, slst);
1875 if (bottom == NULL) {
1877 * No known transformations -- use the first suffix found for setting
1878 * the local variables.
1880 if (!Lst_IsEmpty(&targs)) {
1881 targ = Lst_Datum(Lst_First(&targs));
1887 * Work up the transformation path to find the suffix of the
1888 * target to which the transformation was made.
1890 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
1895 * The .TARGET variable we always set to be the name at this point,
1896 * since it's only set to the path if the thing is only a source and
1897 * if it's only a source, it doesn't matter what we put here as far
1898 * as expanding sources is concerned, since it has none...
1900 Var_Set(TARGET, gn->name, gn);
1902 pref = (targ != NULL) ? targ->pref : gn->name;
1903 Var_Set(PREFIX, pref, gn);
1906 * Now we've got the important local variables set, expand any sources
1907 * that still contain variables or wildcards in their names.
1909 Lst_ForEach(&gn->children, SuffExpandChildren, (void *)gn);
1912 DEBUGF(SUFF, ("\tNo valid suffix on %s\n", gn->name));
1916 * Deal with finding the thing on the default search path if the
1917 * node is only a source (not on the lhs of a dependency operator
1918 * or [XXX] it has neither children or commands).
1920 if (OP_NOP(gn->type) ||
1921 (Lst_IsEmpty(&gn->children) && Lst_IsEmpty(&gn->commands)))
1923 gn->path = Dir_FindFile(gn->name,
1924 (targ == NULL ? &dirSearchPath :
1925 &targ->suff->searchPath));
1926 if (gn->path != NULL) {
1928 Var_Set(TARGET, gn->path, gn);
1932 * Suffix known for the thing -- trim the suffix off
1933 * the path to form the proper .PREFIX variable.
1935 int savep = strlen(gn->path) - targ->suff->nameLen;
1939 gn->suffix->refCount--;
1940 gn->suffix = targ->suff;
1941 gn->suffix->refCount++;
1943 savec = gn->path[savep];
1944 gn->path[savep] = '\0';
1946 if ((ptr = strrchr(gn->path, '/')) != NULL)
1951 Var_Set(PREFIX, ptr, gn);
1953 gn->path[savep] = savec;
1956 * The .PREFIX gets the full path if the target has
1960 gn->suffix->refCount--;
1963 if ((ptr = strrchr(gn->path, '/')) != NULL)
1968 Var_Set(PREFIX, ptr, gn);
1973 * Not appropriate to search for the thing -- set the
1974 * path to be the name so Dir_MTime won't go grovelling for
1978 gn->suffix->refCount--;
1979 gn->suffix = (targ == NULL) ? NULL : targ->suff;
1981 gn->suffix->refCount++;
1983 gn->path = estrdup(gn->name);
1990 * If the suffix indicates that the target is a library, mark that in
1991 * the node's type field.
1993 if (targ->suff->flags & SUFF_LIBRARY) {
1998 * Check for overriding transformation rule implied by sources
2000 if (!Lst_IsEmpty(&gn->children)) {
2001 src = SuffFindCmds(targ, slst);
2005 * Free up all the Src structures in the transformation path
2006 * up to, but not including, the parent node.
2008 while (bottom && bottom->parent != NULL) {
2009 if (Lst_Member(slst, bottom) == NULL) {
2010 Lst_AtEnd(slst, bottom);
2012 bottom = bottom->parent;
2018 if (bottom == NULL) {
2020 * No idea from where it can come -- return now.
2026 * We now have a list of Src structures headed by 'bottom' and linked via
2027 * their 'parent' pointers. What we do next is create links between
2028 * source and target nodes (which may or may not have been created)
2029 * and set the necessary local variables in each target. The
2030 * commands for each target are set from the commands of the
2031 * transformation rule used to get from the src suffix to the targ
2032 * suffix. Note that this causes the commands list of the original
2033 * node, gn, to be replaced by the commands of the final
2034 * transformation rule. Also, the unmade field of gn is incremented.
2037 if (bottom->node == NULL) {
2038 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2041 for (src = bottom; src->parent != NULL; src = src->parent) {
2044 if (src->node->suffix)
2045 src->node->suffix->refCount--;
2046 src->node->suffix = src->suff;
2047 src->node->suffix->refCount++;
2049 if (targ->node == NULL) {
2050 targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2053 SuffApplyTransform(targ->node, src->node,
2054 targ->suff, src->suff);
2056 if (targ->node != gn) {
2058 * Finish off the dependency-search process for any nodes
2059 * between bottom and gn (no point in questing around the
2060 * filesystem for their implicit source when it's already
2061 * known). Note that the node can't have any sources that
2062 * need expanding, since SuffFindThem will stop on an existing
2063 * node, so all we need to do is set the standard and System V
2066 targ->node->type |= OP_DEPS_FOUND;
2068 Var_Set(PREFIX, targ->pref, targ->node);
2070 Var_Set(TARGET, targ->node->name, targ->node);
2075 gn->suffix->refCount--;
2076 gn->suffix = src->suff;
2077 gn->suffix->refCount++;
2080 * So Dir_MTime doesn't go questing for it...
2083 gn->path = estrdup(gn->name);
2086 * Nuke the transformation path and the Src structures left over in the
2091 if (Lst_Member(slst, bottom) == NULL)
2092 Lst_AtEnd(slst, bottom);
2094 while (SuffRemoveSrc(&srcs) || SuffRemoveSrc(&targs))
2097 Lst_Concat(slst, &srcs, LST_CONCLINK);
2098 Lst_Concat(slst, &targs, LST_CONCLINK);
2102 *-----------------------------------------------------------------------
2104 * Find implicit sources for the target described by the graph node
2111 * Nodes are added to the graph below the passed-in node. The nodes
2112 * are marked to have their IMPSRC variable filled in. The
2113 * PREFIX variable is set for the given node and all its
2117 * The path found by this target is the shortest path in the
2118 * transformation graph, which may pass through non-existent targets,
2119 * to an existing target. The search continues on all paths from the
2120 * root suffix until a file is found. I.e. if there's a path
2121 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2122 * the .c and .l files don't, the search will branch out in
2123 * all directions from .o and again from all the nodes on the
2124 * next level until the .l,v node is encountered.
2126 *-----------------------------------------------------------------------
2129 Suff_FindDeps(GNode *gn)
2132 SuffFindDeps(gn, &srclist);
2133 while (SuffRemoveSrc(&srclist))
2139 SuffFindDeps(GNode *gn, Lst *slst)
2142 if (gn->type & OP_DEPS_FOUND) {
2144 * If dependencies already found, no need to do it again...
2148 gn->type |= OP_DEPS_FOUND;
2151 DEBUGF(SUFF, ("SuffFindDeps (%s)\n", gn->name));
2153 if (gn->type & OP_ARCHV) {
2154 SuffFindArchiveDeps(gn, slst);
2155 } else if (gn->type & OP_LIB) {
2157 * If the node is a library, it is the arch module's job to find it
2158 * and set the TARGET variable accordingly. We merely provide the
2159 * search path, assuming all libraries end in ".a" (if the suffix
2160 * hasn't been defined, there's nothing we can do for it, so we just
2161 * set the TARGET variable to the node's name in order to give it a
2167 ln = Lst_Find(&sufflist, LIBSUFF, SuffSuffHasNameP);
2169 gn->suffix->refCount--;
2171 gn->suffix = s = Lst_Datum(ln);
2172 gn->suffix->refCount++;
2173 Arch_FindLib(gn, &s->searchPath);
2176 Var_Set(TARGET, gn->name, gn);
2179 * Because a library (-lfoo) target doesn't follow the standard
2180 * filesystem conventions, we don't set the regular variables for
2181 * the thing. .PREFIX is simply made empty...
2183 Var_Set(PREFIX, "", gn);
2185 SuffFindNormalDeps(gn, slst);
2190 *-----------------------------------------------------------------------
2192 * Define which suffix is the null suffix.
2198 * 'suffNull' is altered.
2201 * Need to handle the changing of the null suffix gracefully so the
2202 * old transformation rules don't just go away.
2204 *-----------------------------------------------------------------------
2207 Suff_SetNull(char *name)
2212 ln = Lst_Find(&sufflist, name, SuffSuffHasNameP);
2215 if (suffNull != NULL) {
2216 suffNull->flags &= ~SUFF_NULL;
2218 s->flags |= SUFF_NULL;
2220 * XXX: Here's where the transformation mangling would take place
2224 Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
2230 *-----------------------------------------------------------------------
2232 * Initialize suffixes module
2239 *-----------------------------------------------------------------------
2247 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2248 * actually go on the suffix list or everyone will think that's its
2251 emptySuff = suffNull = emalloc(sizeof(Suff));
2253 suffNull->name = estrdup("");
2254 suffNull->nameLen = 0;
2255 Lst_Init(&suffNull->searchPath);
2256 Dir_Concat(&suffNull->searchPath, &dirSearchPath);
2257 Lst_Init(&suffNull->children);
2258 Lst_Init(&suffNull->parents);
2259 Lst_Init(&suffNull->ref);
2260 suffNull->sNum = sNum++;
2261 suffNull->flags = SUFF_NULL;
2262 suffNull->refCount = 1;
2265 /********************* DEBUGGING FUNCTIONS **********************/
2268 SuffPrintName(void *s, void *dummy __unused)
2271 printf("`%s' ", ((Suff *)s)->name);
2276 SuffPrintSuff(void *sp, void *dummy __unused)
2282 printf("# `%s' [%d] ", s->name, s->refCount);
2286 fputs(" (", stdout);
2288 flag = 1 << (ffs(flags) - 1);
2303 fputc(flags ? '|' : ')', stdout);
2306 fputc('\n', stdout);
2308 Lst_ForEach(&s->parents, SuffPrintName, (void *)NULL);
2309 fputc('\n', stdout);
2310 printf("#\tFrom: ");
2311 Lst_ForEach(&s->children, SuffPrintName, (void *)NULL);
2312 fputc('\n', stdout);
2313 printf("#\tSearch Path: ");
2314 Dir_PrintPath(&s->searchPath);
2315 fputc('\n', stdout);
2320 SuffPrintTrans(void *tp, void *dummy __unused)
2324 printf("%-16s: ", t->name);
2325 Targ_PrintType(t->type);
2326 fputc('\n', stdout);
2327 Lst_ForEach(&t->commands, Targ_PrintCmd, (void *)NULL);
2328 fputc('\n', stdout);
2336 printf("#*** Suffixes:\n");
2337 Lst_ForEach(&sufflist, SuffPrintSuff, (void *)NULL);
2339 printf("#*** Transformations:\n");
2340 Lst_ForEach(&transforms, SuffPrintTrans, (void *)NULL);
2346 * Print the address of a node, used as an interative function.
2349 PrintAddr(void *a, void *b __unused)
2354 #endif /* DEBUG_SRC */