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.22 2004/12/17 08:13:30 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_End Cleanup the module
53 * Suff_DoPaths This function is used to make life easier
54 * when searching for a file according to its
55 * suffix. It takes the global search path,
56 * as defined using the .PATH: target, and appends
57 * its directories to the path of each of the
58 * defined suffixes, as specified using
59 * .PATH<suffix>: targets. In addition, all
60 * directories given for suffixes labeled as
61 * include files or libraries, using the .INCLUDES
62 * or .LIBS targets, are played with using
63 * Dir_MakeFlags to create the .INCLUDES and
64 * .LIBS global variables.
66 * Suff_ClearSuffixes Clear out all the suffixes and defined
69 * Suff_IsTransform Return TRUE if the passed string is the lhs
70 * of a transformation rule.
72 * Suff_AddSuffix Add the passed string as another known suffix.
74 * Suff_GetPath Return the search path for the given suffix.
76 * Suff_AddInclude Mark the given suffix as denoting an include
79 * Suff_AddLib Mark the given suffix as denoting a library.
81 * Suff_AddTransform Add another transformation to the suffix
82 * graph. Returns GNode suitable for framing, I
83 * mean, tacking commands, attributes, etc. on.
85 * Suff_SetNull Define the suffix to consider the suffix of
86 * any file that doesn't have a known one.
88 * Suff_FindDeps Find implicit sources for and the location of
89 * a target based on its suffix. Returns the
90 * bottom-most node added to the graph or NULL
91 * if the target had no implicit sources.
100 static Lst sufflist = Lst_Initializer(sufflist);
102 /* Lst of suffixes to be cleaned */
103 static Lst suffClean = Lst_Initializer(suffClean);
106 static Lst srclist = Lst_Initializer(srclist);
108 /* Lst of transformation rules */
109 static Lst transforms = Lst_Initializer(transforms);
111 static int sNum = 0; /* Counter for assigning suffix numbers */
114 * Structure describing an individual suffix.
116 typedef struct _Suff {
117 char *name; /* The suffix itself */
118 int nameLen; /* Length of the suffix */
119 short flags; /* Type of suffix */
120 #define SUFF_INCLUDE 0x01 /* One which is #include'd */
121 #define SUFF_LIBRARY 0x02 /* One which contains a library */
122 #define SUFF_NULL 0x04 /* The empty suffix */
123 Lst searchPath; /* The path along which files of this suffix
125 int sNum; /* The suffix number */
126 int refCount; /* Reference count of list membership */
127 Lst parents; /* Suffixes we have a transformation to */
128 Lst children; /* Suffixes we have a transformation from */
129 Lst ref; /* List of lists this suffix is referenced */
133 * Structure used in the search for implied sources.
135 typedef struct _Src {
136 char *file; /* The file to look for */
137 char *pref; /* Prefix from which file was formed */
138 Suff *suff; /* The suffix on the file */
139 struct _Src *parent; /* The Src for which this is a source */
140 GNode *node; /* The node describing the file */
141 int children; /* Count of existing children (so we don't free
142 * this thing too early or never nuke it) */
144 Lst cp; /* Debug; children list */
149 * A structure for passing more than one argument to the Lst-library-invoked
157 static Suff *suffNull; /* The NULL suffix for this run */
158 static Suff *emptySuff; /* The empty suffix required for POSIX
159 * single-suffix transformation rules */
162 static void SuffFree(void *);
163 static void SuffInsert(Lst *, Suff *);
164 static void SuffRemove(Lst *, Suff *);
165 static Boolean SuffParseTransform(char *, Suff **, Suff **);
166 static int SuffRebuildGraph(void *, void *);
167 static int SuffAddSrc(void *, void *);
168 static int SuffRemoveSrc(Lst *);
169 static void SuffAddLevel(Lst *, Src *);
170 static Src *SuffFindThem(Lst *, Lst *);
171 static Src *SuffFindCmds(Src *, Lst *);
172 static int SuffExpandChildren(void *, void *);
173 static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
174 static void SuffFindDeps(GNode *, Lst *);
175 static void SuffFindArchiveDeps(GNode *, Lst *);
176 static void SuffFindNormalDeps(GNode *, Lst *);
177 static int SuffPrintName(void *, void *);
178 static int SuffPrintSuff(void *, void *);
179 static int SuffPrintTrans(void *, void *);
181 /*************** Lst Predicates ****************/
183 *-----------------------------------------------------------------------
185 * See if pref is a prefix of str.
188 * NULL if it ain't, pointer to character in str after prefix if so
192 *-----------------------------------------------------------------------
195 SuffStrIsPrefix(const char *pref, char *str)
198 while (*str && *pref == *str) {
203 return (*pref ? NULL : str);
207 *-----------------------------------------------------------------------
208 * SuffSuffIsSuffix --
209 * See if suff is a suffix of str. Str should point to THE END of the
210 * string to check. (THE END == the null byte)
213 * NULL if it ain't, pointer to character in str before suffix if
218 *-----------------------------------------------------------------------
221 SuffSuffIsSuffix(const Suff *s, char *str)
223 const char *p1; /* Pointer into suffix name */
224 char *p2; /* Pointer into string being examined */
226 p1 = s->name + s->nameLen;
229 while (p1 >= s->name && *p1 == *p2) {
234 return (p1 == s->name - 1 ? p2 : NULL);
238 *-----------------------------------------------------------------------
239 * SuffSuffIsSuffixP --
240 * Predicate form of SuffSuffIsSuffix. Passed as the callback function
244 * 0 if the suffix is the one desired, non-zero if not.
249 * XXX use the function above once constification is complete.
250 *-----------------------------------------------------------------------
253 SuffSuffIsSuffixP(const void *is, const void *str)
256 const char *p1; /* Pointer into suffix name */
257 const char *p2 = str; /* Pointer into string being examined */
259 p1 = s->name + s->nameLen;
261 while (p1 >= s->name && *p1 == *p2) {
266 return (p1 != s->name - 1);
270 *-----------------------------------------------------------------------
271 * SuffSuffHasNameP --
272 * Callback procedure for finding a suffix based on its name. Used by
276 * 0 if the suffix is of the given name. non-zero otherwise.
280 *-----------------------------------------------------------------------
283 SuffSuffHasNameP(const void *s, const void *sname)
286 return (strcmp(sname, ((const Suff *)s)->name));
290 *-----------------------------------------------------------------------
291 * SuffSuffIsPrefix --
292 * See if the suffix described by s is a prefix of the string. Care
293 * must be taken when using this to search for transformations and
294 * what-not, since there could well be two suffixes, one of which
295 * is a prefix of the other...
298 * 0 if s is a prefix of str. non-zero otherwise
303 * XXX use the function above once constification is complete.
304 *-----------------------------------------------------------------------
307 SuffSuffIsPrefix(const void *s, const void *istr)
309 const char *pref = ((const Suff *)s)->name;
310 const char *str = istr;
312 while (*str != '\0' && *pref == *str) {
317 return (*pref != '\0');
321 *-----------------------------------------------------------------------
323 * See if the graph node has the desired name
326 * 0 if it does. non-zero if it doesn't
330 *-----------------------------------------------------------------------
333 SuffGNHasNameP(const void *gn, const void *name)
336 return (strcmp(name, ((const GNode *)gn)->name));
339 /*********** Maintenance Functions ************/
342 *-----------------------------------------------------------------------
344 * Free up all memory associated with the given suffix structure.
350 * the suffix entry is detroyed
351 *-----------------------------------------------------------------------
364 Lst_Destroy(&s->ref, NOFREE);
365 Lst_Destroy(&s->children, NOFREE);
366 Lst_Destroy(&s->parents, NOFREE);
367 Lst_Destroy(&s->searchPath, Dir_Destroy);
374 *-----------------------------------------------------------------------
376 * Remove the suffix into the list
382 * The reference count for the suffix is decremented
383 *-----------------------------------------------------------------------
386 SuffRemove(Lst *l, Suff *s)
388 LstNode *ln = Lst_Member(l, s);
397 *-----------------------------------------------------------------------
399 * Insert the suffix into the list keeping the list ordered by suffix
406 * The reference count of the suffix is incremented
407 *-----------------------------------------------------------------------
410 SuffInsert(Lst *l, Suff *s)
412 LstNode *ln; /* current element in l we're examining */
413 Suff *s2; /* the suffix descriptor in this element */
416 for (ln = Lst_First(l); ln != NULL; ln = Lst_Succ(ln)) {
418 if (s2->sNum >= s->sNum)
422 DEBUGF(SUFF, ("inserting an empty list?..."));
425 DEBUGF(SUFF, ("inserting %s(%d)...", s->name, s->sNum));
427 DEBUGF(SUFF, ("at end of list\n"));
430 Lst_AtEnd(&s->ref, l);
431 } else if (s2->sNum != s->sNum) {
432 DEBUGF(SUFF, ("before %s(%d)\n", s2->name, s2->sNum));
433 Lst_Insert(l, ln, s);
435 Lst_AtEnd(&s->ref, l);
437 DEBUGF(SUFF, ("already there\n"));
442 *-----------------------------------------------------------------------
443 * Suff_ClearSuffixes --
444 * This is gross. Nuke the list of suffixes but keep all transformation
445 * rules around. The transformation graph is destroyed in this process,
446 * but we leave the list of rules so when a new graph is formed the rules
448 * This function is called from the parse module when a
449 * .SUFFIXES:\n line is encountered.
455 * the sufflist and its graph nodes are destroyed
456 *-----------------------------------------------------------------------
459 Suff_ClearSuffixes(void)
462 Lst_Concat(&suffClean, &sufflist, LST_CONCLINK);
465 suffNull = emptySuff;
467 * Clear suffNull's children list (the other suffixes are built new, but
468 * suffNull is used as is).
469 * NOFREE is used because all suffixes are are on the suffClean list.
470 * suffNull should not have parents.
472 Lst_Destroy(&suffNull->children, NOFREE);
476 *-----------------------------------------------------------------------
477 * SuffParseTransform --
478 * Parse a transformation string to find its two component suffixes.
481 * TRUE if the string is a valid transformation and FALSE otherwise.
484 * The passed pointers are overwritten.
486 *-----------------------------------------------------------------------
489 SuffParseTransform(char *str, Suff **srcPtr, Suff **targPtr)
491 LstNode *srcLn; /* element in suffix list of trans source*/
492 Suff *src; /* Source of transformation */
493 LstNode *targLn; /* element in suffix list of trans target*/
494 char *str2; /* Extra pointer (maybe target suffix) */
495 LstNode *singleLn; /* element in suffix list of any suffix
496 * that exactly matches str */
497 Suff *single = NULL;/* Source of possible transformation to
504 * Loop looking first for a suffix that matches the start of the
505 * string and then for one that exactly matches the rest of it. If
506 * we can find two that meet these criteria, we've successfully
511 srcLn = Lst_Find(&sufflist, str, SuffSuffIsPrefix);
513 srcLn = Lst_FindFrom(&sufflist, Lst_Succ(srcLn), str,
518 * Ran out of source suffixes -- no such rule
520 if (singleLn != NULL) {
522 * Not so fast Mr. Smith! There was a suffix that encompassed
523 * the entire string, so we assume it was a transformation
524 * to the null suffix (thank you POSIX). We still prefer to
525 * find a double rule over a singleton, hence we leave this
526 * check until the end.
528 * XXX: Use emptySuff over suffNull?
536 src = Lst_Datum (srcLn);
537 str2 = str + src->nameLen;
542 targLn = Lst_Find(&sufflist, str2, SuffSuffHasNameP);
543 if (targLn != NULL) {
545 *targPtr = Lst_Datum(targLn);
553 *-----------------------------------------------------------------------
554 * Suff_IsTransform --
555 * Return TRUE if the given string is a transformation rule
559 * TRUE if the string is a concatenation of two known suffixes.
564 *-----------------------------------------------------------------------
567 Suff_IsTransform(char *str)
571 return (SuffParseTransform(str, &src, &targ));
575 *-----------------------------------------------------------------------
576 * Suff_AddTransform --
577 * Add the transformation rule described by the line to the
578 * list of rules and place the transformation itself in the graph
581 * The node created for the transformation in the transforms list
584 * The node is placed on the end of the transforms Lst and links are
585 * made between the two suffixes mentioned in the target name
586 *-----------------------------------------------------------------------
589 Suff_AddTransform(char *line)
591 GNode *gn; /* GNode of transformation rule */
592 Suff *s, /* source suffix */
593 *t; /* target suffix */
594 LstNode *ln; /* Node for existing transformation */
596 ln = Lst_Find(&transforms, line, SuffGNHasNameP);
599 * Make a new graph node for the transformation. It will be filled in
600 * by the Parse module.
602 gn = Targ_NewGN(line);
603 Lst_AtEnd(&transforms, gn);
606 * New specification for transformation rule. Just nuke the old list
607 * of commands so they can be filled in again... We don't actually
608 * free the commands themselves, because a given command can be
609 * attached to several different transformations.
612 Lst_Destroy(&gn->commands, NOFREE);
613 Lst_Destroy(&gn->children, NOFREE);
616 gn->type = OP_TRANSFORM;
618 SuffParseTransform(line, &s, &t);
621 * link the two together in the proper relationship and order
623 DEBUGF(SUFF, ("defining transformation from `%s' to `%s'\n",
625 SuffInsert(&t->children, s);
626 SuffInsert(&s->parents, t);
632 *-----------------------------------------------------------------------
633 * Suff_EndTransform --
634 * Handle the finish of a transformation definition, removing the
635 * transformation from the graph if it has neither commands nor
636 * sources. This is a callback procedure for the Parse module via
643 * If the node has no commands or children, the children and parents
644 * lists of the affected suffices are altered.
646 *-----------------------------------------------------------------------
649 Suff_EndTransform(void *gnp, void *dummy __unused)
651 GNode *gn = (GNode *)gnp;
653 if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(&gn->commands) &&
654 Lst_IsEmpty(&gn->children))
659 * SuffParseTransform() may fail for special rules which are not
660 * actual transformation rules (e.g., .DEFAULT).
662 if (!SuffParseTransform(gn->name, &s, &t))
665 DEBUGF(SUFF, ("deleting transformation from `%s' to `%s'\n",
669 * Remove the source from the target's children list. We check for a
670 * NULL return to handle a beanhead saying something like
673 * We'll be called twice when the next target is seen, but .c and .o
674 * are only linked once...
676 SuffRemove(&t->children, s);
679 * Remove the target from the source's parents list
681 SuffRemove(&s->parents, t);
682 } else if (gn->type & OP_TRANSFORM) {
683 DEBUGF(SUFF, ("transformation %s complete\n", gn->name));
690 *-----------------------------------------------------------------------
691 * SuffRebuildGraph --
692 * Called from Suff_AddSuffix via Lst_ForEach to search through the
693 * list of existing transformation rules and rebuild the transformation
694 * graph when it has been destroyed by Suff_ClearSuffixes. If the
695 * given rule is a transformation involving this suffix and another,
696 * existing suffix, the proper relationship is established between
703 * The appropriate links will be made between this suffix and
704 * others if transformation rules exist for it.
706 *-----------------------------------------------------------------------
709 SuffRebuildGraph(void *transformp, void *sp)
711 GNode *transform = transformp;
718 * First see if it is a transformation from this suffix.
720 cp = SuffStrIsPrefix(s->name, transform->name);
721 if (cp != (char *)NULL) {
722 if (cp[0] == '\0') /* null rule */
725 ln = Lst_Find(&sufflist, cp, SuffSuffHasNameP);
731 * Found target. Link in and return, since it can't be anything
734 SuffInsert(&s2->children, s);
735 SuffInsert(&s->parents, s2);
741 * Not from, maybe to?
743 cp = SuffSuffIsSuffix(s, transform->name + strlen(transform->name));
746 * Null-terminate the source suffix in order to find it.
749 ln = Lst_Find(&sufflist, transform->name, SuffSuffHasNameP);
751 * Replace the start of the target suffix
756 * Found it -- establish the proper relationship
759 SuffInsert(&s->children, s2);
760 SuffInsert(&s2->parents, s);
767 *-----------------------------------------------------------------------
769 * Add the suffix in string to the end of the list of known suffixes.
770 * Should we restructure the suffix graph? Make doesn't...
776 * A GNode is created for the suffix and a Suff structure is created and
777 * added to the suffixes list unless the suffix was already known.
778 *-----------------------------------------------------------------------
781 Suff_AddSuffix(char *str)
783 Suff *s; /* new suffix descriptor */
786 ln = Lst_Find(&sufflist, str, SuffSuffHasNameP);
788 s = emalloc(sizeof(Suff));
790 s->name = estrdup(str);
791 s->nameLen = strlen (s->name);
792 Lst_Init(&s->searchPath);
793 Lst_Init(&s->children);
794 Lst_Init(&s->parents);
800 Lst_AtEnd(&sufflist, s);
803 * Look for any existing transformations from or to this suffix.
804 * XXX: Only do this after a Suff_ClearSuffixes?
806 Lst_ForEach(&transforms, SuffRebuildGraph, s);
811 *-----------------------------------------------------------------------
813 * Return the search path for the given suffix, if it's defined.
816 * The searchPath for the desired suffix or NULL if the suffix isn't
821 *-----------------------------------------------------------------------
824 Suff_GetPath(char *sname)
829 ln = Lst_Find(&sufflist, sname, SuffSuffHasNameP);
834 return (&s->searchPath);
839 *-----------------------------------------------------------------------
841 * Extend the search paths for all suffixes to include the default
848 * The searchPath field of all the suffixes is extended by the
849 * directories in dirSearchPath. If paths were specified for the
850 * ".h" suffix, the directories are stuffed into a global variable
851 * called ".INCLUDES" with each directory preceded by a -I. The same
852 * is done for the ".a" suffix, except the variable is called
853 * ".LIBS" and the flag is -L.
854 *-----------------------------------------------------------------------
862 Lst inIncludes; /* Cumulative .INCLUDES path */
863 Lst inLibs; /* Cumulative .LIBS path */
865 Lst_Init(&inIncludes);
868 for (ln = Lst_First(&sufflist); ln != NULL; ln = Lst_Succ(ln)) {
870 if (!Lst_IsEmpty(&s->searchPath)) {
872 if (s->flags & SUFF_INCLUDE) {
873 Dir_Concat(&inIncludes, &s->searchPath);
875 #endif /* INCLUDES */
877 if (s->flags & SUFF_LIBRARY) {
878 Dir_Concat(&inLibs, &s->searchPath);
880 #endif /* LIBRARIES */
881 Dir_Concat(&s->searchPath, &dirSearchPath);
883 Lst_Destroy(&s->searchPath, Dir_Destroy);
884 Lst_Duplicate(&s->searchPath, &dirSearchPath, Dir_CopyDir);
888 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", &inIncludes), VAR_GLOBAL);
890 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", &inLibs), VAR_GLOBAL);
893 Lst_Destroy(&inIncludes, Dir_Destroy);
894 Lst_Destroy(&inLibs, Dir_Destroy);
898 *-----------------------------------------------------------------------
900 * Add the given suffix as a type of file which gets included.
901 * Called from the parse module when a .INCLUDES line is parsed.
902 * The suffix must have already been defined.
908 * The SUFF_INCLUDE bit is set in the suffix's flags field
910 *-----------------------------------------------------------------------
913 Suff_AddInclude(char *sname)
918 ln = Lst_Find(&sufflist, sname, SuffSuffHasNameP);
921 s->flags |= SUFF_INCLUDE;
926 *-----------------------------------------------------------------------
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. The
930 * suffix must have been defined via .SUFFIXES before this is
937 * The SUFF_LIBRARY bit is set in the suffix's flags field
939 *-----------------------------------------------------------------------
942 Suff_AddLib(char *sname)
947 ln = Lst_Find(&sufflist, sname, SuffSuffHasNameP);
950 s->flags |= SUFF_LIBRARY;
954 /********** Implicit Source Search Functions *********/
957 *-----------------------------------------------------------------------
959 * Add a suffix as a Src structure to the given list with its parent
960 * being the given Src structure. If the suffix is the null suffix,
961 * the prefix is used unaltered as the file name in the Src structure.
967 * A Src structure is created and tacked onto the end of the list
968 *-----------------------------------------------------------------------
971 SuffAddSrc(void *sp, void *lsp)
975 Src *s2; /* new Src structure */
976 Src *targ; /* Target structure */
980 if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
982 * If the suffix has been marked as the NULL suffix, also create a Src
983 * structure for a file with no suffix attached. Two birds, and all
986 s2 = emalloc(sizeof(Src));
987 s2->file = estrdup(targ->pref);
988 s2->pref = targ->pref;
995 Lst_AtEnd(ls->l, s2);
998 Lst_AtEnd(&targ->cp, s2);
999 printf("1 add %p %p to %p:", targ, s2, ls->l);
1000 Lst_ForEach(ls->l, PrintAddr, (void *)NULL);
1004 s2 = emalloc(sizeof(Src));
1005 s2->file = str_concat(targ->pref, s->name, 0);
1006 s2->pref = targ->pref;
1012 targ->children += 1;
1013 Lst_AtEnd(ls->l, s2);
1016 Lst_AtEnd(&targ->cp, s2);
1017 printf("2 add %p %p to %p:", targ, s2, ls->l);
1018 Lst_ForEach(ls->l, PrintAddr, (void *)NULL);
1026 *-----------------------------------------------------------------------
1028 * Add all the children of targ as Src structures to the given list
1034 * Lots of structures are created and added to the list
1035 *-----------------------------------------------------------------------
1038 SuffAddLevel(Lst *l, Src *targ)
1045 Lst_ForEach(&targ->suff->children, SuffAddSrc, &ls);
1049 *----------------------------------------------------------------------
1051 * Free all src structures in list that don't have a reference count
1052 * XXX this actually frees only the first of these.
1055 * True if a src was removed
1058 * The memory is free'd.
1059 *----------------------------------------------------------------------
1062 SuffRemoveSrc(Lst *l)
1069 printf("cleaning %lx: ", (unsigned long) l);
1070 Lst_ForEach(l, PrintAddr, (void *)NULL);
1074 for (ln = Lst_First(l); ln != NULL; ln = ln1) {
1077 s = (Src *)Lst_Datum(ln);
1078 if (s->children == 0) {
1084 LstNode *ln = Lst_Member(&s->parent->cp, s);
1086 Lst_Remove(&s->parent->cp, ln);
1088 --s->parent->children;
1091 printf("free: [l=%p] p=%p %d\n", l, s, s->children);
1092 Lst_Destroy(&s->cp, NOFREE);
1101 printf("keep: [l=%p] p=%p %d: ", l, s, s->children);
1102 Lst_ForEach(&s->cp, PrintAddr, (void *)NULL);
1112 *-----------------------------------------------------------------------
1114 * Find the first existing file/target in the list srcs
1117 * The lowest structure in the chain of transformations
1121 *-----------------------------------------------------------------------
1124 SuffFindThem(Lst *srcs, Lst *slst)
1126 Src *s; /* current Src */
1127 Src *rs; /* returned Src */
1132 while (!Lst_IsEmpty (srcs)) {
1133 s = Lst_DeQueue(srcs);
1135 DEBUGF(SUFF, ("\ttrying %s...", s->file));
1138 * A file is considered to exist if either a node exists in the
1139 * graph for it or the file actually exists.
1141 if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
1143 printf("remove %p from %p\n", s, srcs);
1149 if ((ptr = Dir_FindFile(s->file, &s->suff->searchPath)) != NULL) {
1152 printf("remove %p from %p\n", s, srcs);
1158 DEBUGF(SUFF, ("not there\n"));
1160 SuffAddLevel(srcs, s);
1165 DEBUGF(SUFF, ("got it\n"));
1171 *-----------------------------------------------------------------------
1173 * See if any of the children of the target in the Src structure is
1174 * one from which the target can be transformed. If there is one,
1175 * a Src structure is put together for it and returned.
1178 * The Src structure of the "winning" child, or NULL if no such beast.
1181 * A Src structure may be allocated.
1183 *-----------------------------------------------------------------------
1186 SuffFindCmds (Src *targ, Lst *slst)
1188 LstNode *ln; /* General-purpose list node */
1189 GNode *t, /* Target GNode */
1190 *s; /* Source GNode */
1191 int prefLen;/* The length of the defined prefix */
1192 Suff *suff; /* Suffix on matching beastie */
1193 Src *ret; /* Return value */
1197 prefLen = strlen(targ->pref);
1199 for (ln = Lst_First(&t->children); ln != NULL; ln = Lst_Succ(ln)) {
1202 cp = strrchr(s->name, '/');
1208 if (strncmp(cp, targ->pref, prefLen) == 0) {
1210 * The node matches the prefix ok, see if it has a known
1213 ln = Lst_Find(&sufflist, &cp[prefLen], SuffSuffHasNameP);
1216 * It even has a known suffix, see if there's a transformation
1217 * defined between the node's suffix and the target's suffix.
1219 * XXX: Handle multi-stage transformations here, too.
1221 suff = Lst_Datum(ln);
1223 if (Lst_Member(&suff->parents, targ->suff) != NULL) {
1225 * Hot Damn! Create a new Src structure to describe
1226 * this transformation (making sure to duplicate the
1227 * source node's name so Suff_FindDeps can free it
1228 * again (ick)), and return the new structure.
1230 ret = emalloc(sizeof(Src));
1231 ret->file = estrdup(s->name);
1232 ret->pref = targ->pref;
1238 targ->children += 1;
1241 printf("3 add %p %p\n", &targ, ret);
1242 Lst_AtEnd(&targ->cp, ret);
1244 Lst_AtEnd(slst, ret);
1245 DEBUGF(SUFF, ("\tusing existing source %s\n", s->name));
1255 *-----------------------------------------------------------------------
1256 * SuffExpandChildren --
1257 * Expand the names of any children of a given node that contain
1258 * variable invocations or file wildcards into actual targets.
1264 * The expanded node is removed from the parent's list of children,
1265 * and the parent's unmade counter is decremented, but other nodes
1268 *-----------------------------------------------------------------------
1271 SuffExpandChildren(void *cgnp, void *pgnp)
1275 GNode *gn; /* New source 8) */
1276 LstNode *prevLN; /* Node after which new source should be put */
1277 LstNode *ln; /* List element for old source */
1278 char *cp; /* Expanded value */
1281 * New nodes effectively take the place of the child, so place them
1284 prevLN = Lst_Member(&pgn->children, cgn);
1287 * First do variable expansion -- this takes precedence over
1288 * wildcard expansion. If the result contains wildcards, they'll be gotten
1289 * to later since the resulting words are tacked on to the end of
1290 * the children list.
1292 if (strchr(cgn->name, '$') != NULL) {
1293 DEBUGF(SUFF, ("Expanding \"%s\"...", cgn->name));
1294 cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
1297 Lst members = Lst_Initializer(members);
1299 if (cgn->type & OP_ARCHV) {
1301 * Node was an archive(member) target, so we want to call
1302 * on the Arch module to find the nodes for us, expanding
1303 * variables in the parent's context.
1305 char *sacrifice = cp;
1307 Arch_ParseArchive(&sacrifice, &members, pgn);
1310 * Break the result into a vector of strings whose nodes
1311 * we can find, then add those nodes to the members list.
1312 * Unfortunately, we can't use brk_string b/c it
1313 * doesn't understand about variable specifications with
1317 char *initcp = cp; /* For freeing... */
1319 for (start = cp; *start == ' ' || *start == '\t'; start++)
1321 for (cp = start; *cp != '\0'; cp++) {
1322 if (*cp == ' ' || *cp == '\t') {
1324 * White-space -- terminate element, find the node,
1325 * add it, skip any further spaces.
1328 gn = Targ_FindNode(start, TARG_CREATE);
1329 Lst_AtEnd(&members, gn);
1330 while (*cp == ' ' || *cp == '\t') {
1334 * Adjust cp for increment at start of loop, but
1335 * set start to first non-space.
1338 } else if (*cp == '$') {
1340 * Start of a variable spec -- contact variable module
1341 * to find the end so we can skip over it.
1347 junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
1348 if (junk != var_Error) {
1355 } else if (*cp == '\\' && *cp != '\0') {
1357 * Escaped something -- skip over it
1365 * Stuff left over -- add it to the list too
1367 gn = Targ_FindNode(start, TARG_CREATE);
1368 Lst_AtEnd(&members, gn);
1371 * Point cp back at the beginning again so the variable value
1377 * Add all elements of the members list to the parent node.
1379 while(!Lst_IsEmpty(&members)) {
1380 gn = Lst_DeQueue(&members);
1382 DEBUGF(SUFF, ("%s...", gn->name));
1383 if (Lst_Member(&pgn->children, gn) == NULL) {
1384 Lst_Append(&pgn->children, prevLN, gn);
1385 prevLN = Lst_Succ(prevLN);
1386 Lst_AtEnd(&gn->parents, pgn);
1397 * Now the source is expanded, remove it from the list of children to
1398 * keep it from being processed.
1400 ln = Lst_Member(&pgn->children, cgn);
1402 Lst_Remove(&pgn->children, ln);
1403 DEBUGF(SUFF, ("\n"));
1404 } else if (Dir_HasWildcards(cgn->name)) {
1405 Lst exp; /* List of expansions */
1406 Lst *path; /* Search path along which to expand */
1409 * Find a path along which to expand the word.
1411 * If the word has a known suffix, use that path.
1412 * If it has no known suffix and we're allowed to use the null
1413 * suffix, use its path.
1414 * Else use the default system search path.
1416 cp = cgn->name + strlen(cgn->name);
1417 ln = Lst_Find(&sufflist, cp, SuffSuffIsSuffixP);
1419 DEBUGF(SUFF, ("Wildcard expanding \"%s\"...", cgn->name));
1422 Suff *s = Lst_Datum(ln);
1424 DEBUGF(SUFF, ("suffix is \"%s\"...", s->name));
1425 path = &s->searchPath;
1428 * Use default search path
1430 path = &dirSearchPath;
1434 * Expand the word along the chosen path
1437 Dir_Expand(cgn->name, path, &exp);
1439 while (!Lst_IsEmpty(&exp)) {
1441 * Fetch next expansion off the list and find its GNode
1443 cp = Lst_DeQueue(&exp);
1445 DEBUGF(SUFF, ("%s...", cp));
1446 gn = Targ_FindNode(cp, TARG_CREATE);
1449 * If gn isn't already a child of the parent, make it so and
1450 * up the parent's count of unmade children.
1452 if (Lst_Member(&pgn->children, gn) == NULL) {
1453 Lst_Append(&pgn->children, prevLN, gn);
1454 prevLN = Lst_Succ(prevLN);
1455 Lst_AtEnd(&gn->parents, pgn);
1461 * Now the source is expanded, remove it from the list of children to
1462 * keep it from being processed.
1464 ln = Lst_Member(&pgn->children, cgn);
1466 Lst_Remove(&pgn->children, ln);
1467 DEBUGF(SUFF, ("\n"));
1474 *-----------------------------------------------------------------------
1475 * SuffApplyTransform --
1476 * Apply a transformation rule, given the source and target nodes
1480 * TRUE if successful, FALSE if not.
1483 * The source and target are linked and the commands from the
1484 * transformation are added to the target node's commands list.
1485 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1486 * to the target. The target also inherits all the sources for
1487 * the transformation rule.
1489 *-----------------------------------------------------------------------
1492 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
1494 LstNode *ln; /* General node */
1495 char *tname; /* Name of transformation rule */
1496 GNode *gn; /* Node for same */
1498 if (Lst_Member(&tGn->children, sGn) == NULL) {
1500 * Not already linked, so form the proper links between the
1501 * target and source.
1503 Lst_AtEnd(&tGn->children, sGn);
1504 Lst_AtEnd(&sGn->parents, tGn);
1508 if ((sGn->type & OP_OPMASK) == OP_DOUBLEDEP) {
1510 * When a :: node is used as the implied source of a node, we have
1511 * to link all its cohorts in as sources as well. Only the initial
1512 * sGn gets the target in its iParents list, however, as that
1513 * will be sufficient to get the .IMPSRC variable set for tGn
1515 for (ln = Lst_First(&sGn->cohorts); ln != NULL; ln = Lst_Succ(ln)) {
1518 if (Lst_Member(&tGn->children, gn) == NULL) {
1520 * Not already linked, so form the proper links between the
1521 * target and source.
1523 Lst_AtEnd(&tGn->children, gn);
1524 Lst_AtEnd(&gn->parents, tGn);
1530 * Locate the transformation rule itself
1532 tname = str_concat(s->name, t->name, 0);
1533 ln = Lst_Find(&transforms, tname, SuffGNHasNameP);
1538 * Not really such a transformation rule (can happen when we're
1539 * called to link an OP_MEMBER and OP_ARCHV node), so return
1547 DEBUGF(SUFF, ("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name));
1550 * Record last child for expansion purposes
1552 ln = Lst_Last(&tGn->children);
1555 * Pass the buck to Make_HandleUse to apply the rule
1557 Make_HandleUse(gn, tGn);
1560 * Deal with wildcards and variables in any acquired sources
1564 Lst_ForEachFrom(&tGn->children, ln, SuffExpandChildren, tGn);
1568 * Keep track of another parent to which this beast is transformed so
1569 * the .IMPSRC variable can be set correctly for the parent.
1571 Lst_AtEnd(&sGn->iParents, tGn);
1578 *-----------------------------------------------------------------------
1579 * SuffFindArchiveDeps --
1580 * Locate dependencies for an OP_ARCHV node.
1586 * Same as Suff_FindDeps
1588 *-----------------------------------------------------------------------
1591 SuffFindArchiveDeps(GNode *gn, Lst *slst)
1593 char *eoarch; /* End of archive portion */
1594 char *eoname; /* End of member portion */
1595 GNode *mem; /* Node for member */
1596 static char *copy[] = { /* Variables to be copied from the member node */
1597 TARGET, /* Must be first */
1598 PREFIX, /* Must be second */
1600 int i; /* Index into copy and vals */
1601 Suff *ms; /* Suffix descriptor for member */
1602 char *name; /* Start of member's name */
1605 * The node is an archive(member) pair. so we must find a
1606 * suffix for both of them.
1608 eoarch = strchr(gn->name, '(');
1609 eoname = strchr(eoarch, ')');
1611 *eoname = '\0'; /* Nuke parentheses during suffix search */
1612 *eoarch = '\0'; /* So a suffix can be found */
1617 * To simplify things, call Suff_FindDeps recursively on the member now,
1618 * so we can simply compare the member's .PREFIX and .TARGET variables
1619 * to locate its suffix. This allows us to figure out the suffix to
1620 * use for the archive without having to do a quadratic search over the
1621 * suffix list, backtracking for each one...
1623 mem = Targ_FindNode(name, TARG_CREATE);
1624 SuffFindDeps(mem, slst);
1627 * Create the link between the two nodes right off
1629 if (Lst_Member(&gn->children, mem) == NULL) {
1630 Lst_AtEnd(&gn->children, mem);
1631 Lst_AtEnd(&mem->parents, gn);
1636 * Copy in the variables from the member node to this one.
1638 for (i = (sizeof(copy) / sizeof(copy[0]))-1; i >= 0; i--) {
1640 Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn);
1648 * Didn't know what it was -- use .NULL suffix if not in make mode
1650 DEBUGF(SUFF, ("using null suffix\n"));
1656 * Set the other two local variables required for this target.
1658 Var_Set(MEMBER, name, gn);
1659 Var_Set(ARCHIVE, gn->name, gn);
1663 * Member has a known suffix, so look for a transformation rule from
1664 * it to a possible suffix of the archive. Rather than searching
1665 * through the entire list, we just look at suffixes to which the
1666 * member's suffix may be transformed...
1671 * Use first matching suffix...
1673 ln = Lst_Find(&ms->parents, eoarch, SuffSuffIsSuffixP);
1677 * Got one -- apply it
1679 if (!SuffApplyTransform(gn, mem, Lst_Datum(ln), ms)) {
1680 DEBUGF(SUFF, ("\tNo transformation from %s -> %s\n",
1681 ms->name, ((Suff *)Lst_Datum(ln))->name));
1687 * Replace the opening and closing parens now we've no need of the separate
1690 *eoarch = '('; *eoname = ')';
1693 * Pretend gn appeared to the left of a dependency operator so
1694 * the user needn't provide a transformation from the member to the
1697 if (OP_NOP(gn->type)) {
1698 gn->type |= OP_DEPENDS;
1702 * Flag the member as such so we remember to look in the archive for
1703 * its modification time.
1705 mem->type |= OP_MEMBER;
1709 *-----------------------------------------------------------------------
1710 * SuffFindNormalDeps --
1711 * Locate implicit dependencies for regular targets.
1717 * Same as Suff_FindDeps...
1719 *-----------------------------------------------------------------------
1722 SuffFindNormalDeps(GNode *gn, Lst *slst)
1724 char *eoname; /* End of name */
1725 char *sopref; /* Start of prefix */
1726 LstNode *ln; /* Next suffix node to check */
1727 Lst srcs; /* List of sources at which to look */
1728 Lst targs; /* List of targets to which things can be
1729 * transformed. They all have the same file,
1730 * but different suff and pref fields */
1731 Src *bottom; /* Start of found transformation path */
1732 Src *src; /* General Src pointer */
1733 char *pref; /* Prefix to use */
1734 Src *targ; /* General Src target pointer */
1737 eoname = gn->name + strlen(gn->name);
1742 * Begin at the beginning...
1744 ln = Lst_First(&sufflist);
1749 * We're caught in a catch-22 here. On the one hand, we want to use any
1750 * transformation implied by the target's sources, but we can't examine
1751 * the sources until we've expanded any variables/wildcards they may hold,
1752 * and we can't do that until we've set up the target's local variables
1753 * and we can't do that until we know what the proper suffix for the
1754 * target is (in case there are two suffixes one of which is a suffix of
1755 * the other) and we can't know that until we've found its implied
1756 * source, which we may not want to use if there's an existing source
1757 * that implies a different transformation.
1759 * In an attempt to get around this, which may not work all the time,
1760 * but should work most of the time, we look for implied sources first,
1761 * checking transformations to all possible suffixes of the target,
1762 * use what we find to set the target's local variables, expand the
1763 * children, then look for any overriding transformations they imply.
1764 * Should we find one, we discard the one we found before.
1767 while (ln != NULL) {
1769 * Look for next possible suffix...
1771 ln = Lst_FindFrom(&sufflist, ln, eoname, SuffSuffIsSuffixP);
1774 int prefLen; /* Length of the prefix */
1778 * Allocate a Src structure to which things can be transformed
1780 target = emalloc(sizeof(Src));
1781 target->file = estrdup(gn->name);
1782 target->suff = Lst_Datum(ln);
1783 target->suff->refCount++;
1785 target->parent = NULL;
1786 target->children = 0;
1788 Lst_Init(&target->cp);
1792 * Allocate room for the prefix, whose end is found by subtracting
1793 * the length of the suffix from the end of the name.
1795 prefLen = (eoname - target->suff->nameLen) - sopref;
1796 target->pref = emalloc(prefLen + 1);
1797 memcpy(target->pref, sopref, prefLen);
1798 target->pref[prefLen] = '\0';
1801 * Add nodes from which the target can be made
1803 SuffAddLevel(&srcs, target);
1806 * Record the target so we can nuke it
1808 Lst_AtEnd(&targs, target);
1811 * Search from this suffix's successor...
1818 * Handle target of unknown suffix...
1820 if (Lst_IsEmpty(&targs) && suffNull != NULL) {
1821 DEBUGF(SUFF, ("\tNo known suffix on %s. Using .NULL suffix\n", gn->name));
1823 targ = emalloc(sizeof(Src));
1824 targ->file = estrdup(gn->name);
1825 targ->suff = suffNull;
1826 targ->suff->refCount++;
1828 targ->parent = NULL;
1830 targ->pref = estrdup(sopref);
1832 Lst_Init(&targ->cp);
1836 * Only use the default suffix rules if we don't have commands
1837 * or dependencies defined for this gnode
1839 if (Lst_IsEmpty(&gn->commands) && Lst_IsEmpty(&gn->children))
1840 SuffAddLevel(&srcs, targ);
1842 DEBUGF(SUFF, ("not "));
1845 DEBUGF(SUFF, ("adding suffix rules\n"));
1847 Lst_AtEnd(&targs, targ);
1851 * Using the list of possible sources built up from the target suffix(es),
1852 * try and find an existing file/target that matches.
1854 bottom = SuffFindThem(&srcs, slst);
1856 if (bottom == NULL) {
1858 * No known transformations -- use the first suffix found for setting
1859 * the local variables.
1861 if (!Lst_IsEmpty(&targs)) {
1862 targ = Lst_Datum(Lst_First(&targs));
1868 * Work up the transformation path to find the suffix of the
1869 * target to which the transformation was made.
1871 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
1876 * The .TARGET variable we always set to be the name at this point,
1877 * since it's only set to the path if the thing is only a source and
1878 * if it's only a source, it doesn't matter what we put here as far
1879 * as expanding sources is concerned, since it has none...
1881 Var_Set(TARGET, gn->name, gn);
1883 pref = (targ != NULL) ? targ->pref : gn->name;
1884 Var_Set(PREFIX, pref, gn);
1887 * Now we've got the important local variables set, expand any sources
1888 * that still contain variables or wildcards in their names.
1890 Lst_ForEach(&gn->children, SuffExpandChildren, (void *)gn);
1893 DEBUGF(SUFF, ("\tNo valid suffix on %s\n", gn->name));
1897 * Deal with finding the thing on the default search path if the
1898 * node is only a source (not on the lhs of a dependency operator
1899 * or [XXX] it has neither children or commands).
1901 if (OP_NOP(gn->type) ||
1902 (Lst_IsEmpty(&gn->children) && Lst_IsEmpty(&gn->commands)))
1904 gn->path = Dir_FindFile(gn->name,
1905 (targ == NULL ? &dirSearchPath :
1906 &targ->suff->searchPath));
1907 if (gn->path != NULL) {
1909 Var_Set(TARGET, gn->path, gn);
1913 * Suffix known for the thing -- trim the suffix off
1914 * the path to form the proper .PREFIX variable.
1916 int savep = strlen(gn->path) - targ->suff->nameLen;
1920 gn->suffix->refCount--;
1921 gn->suffix = targ->suff;
1922 gn->suffix->refCount++;
1924 savec = gn->path[savep];
1925 gn->path[savep] = '\0';
1927 if ((ptr = strrchr(gn->path, '/')) != NULL)
1932 Var_Set(PREFIX, ptr, gn);
1934 gn->path[savep] = savec;
1937 * The .PREFIX gets the full path if the target has
1941 gn->suffix->refCount--;
1944 if ((ptr = strrchr(gn->path, '/')) != NULL)
1949 Var_Set(PREFIX, ptr, gn);
1954 * Not appropriate to search for the thing -- set the
1955 * path to be the name so Dir_MTime won't go grovelling for
1959 gn->suffix->refCount--;
1960 gn->suffix = (targ == NULL) ? NULL : targ->suff;
1962 gn->suffix->refCount++;
1964 gn->path = estrdup(gn->name);
1971 * If the suffix indicates that the target is a library, mark that in
1972 * the node's type field.
1974 if (targ->suff->flags & SUFF_LIBRARY) {
1979 * Check for overriding transformation rule implied by sources
1981 if (!Lst_IsEmpty(&gn->children)) {
1982 src = SuffFindCmds(targ, slst);
1986 * Free up all the Src structures in the transformation path
1987 * up to, but not including, the parent node.
1989 while (bottom && bottom->parent != NULL) {
1990 if (Lst_Member(slst, bottom) == NULL) {
1991 Lst_AtEnd(slst, bottom);
1993 bottom = bottom->parent;
1999 if (bottom == NULL) {
2001 * No idea from where it can come -- return now.
2007 * We now have a list of Src structures headed by 'bottom' and linked via
2008 * their 'parent' pointers. What we do next is create links between
2009 * source and target nodes (which may or may not have been created)
2010 * and set the necessary local variables in each target. The
2011 * commands for each target are set from the commands of the
2012 * transformation rule used to get from the src suffix to the targ
2013 * suffix. Note that this causes the commands list of the original
2014 * node, gn, to be replaced by the commands of the final
2015 * transformation rule. Also, the unmade field of gn is incremented.
2018 if (bottom->node == NULL) {
2019 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2022 for (src = bottom; src->parent != NULL; src = src->parent) {
2025 if (src->node->suffix)
2026 src->node->suffix->refCount--;
2027 src->node->suffix = src->suff;
2028 src->node->suffix->refCount++;
2030 if (targ->node == NULL) {
2031 targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2034 SuffApplyTransform(targ->node, src->node,
2035 targ->suff, src->suff);
2037 if (targ->node != gn) {
2039 * Finish off the dependency-search process for any nodes
2040 * between bottom and gn (no point in questing around the
2041 * filesystem for their implicit source when it's already
2042 * known). Note that the node can't have any sources that
2043 * need expanding, since SuffFindThem will stop on an existing
2044 * node, so all we need to do is set the standard and System V
2047 targ->node->type |= OP_DEPS_FOUND;
2049 Var_Set(PREFIX, targ->pref, targ->node);
2051 Var_Set(TARGET, targ->node->name, targ->node);
2056 gn->suffix->refCount--;
2057 gn->suffix = src->suff;
2058 gn->suffix->refCount++;
2061 * So Dir_MTime doesn't go questing for it...
2064 gn->path = estrdup(gn->name);
2067 * Nuke the transformation path and the Src structures left over in the
2072 if (Lst_Member(slst, bottom) == NULL)
2073 Lst_AtEnd(slst, bottom);
2075 while (SuffRemoveSrc(&srcs) || SuffRemoveSrc(&targs))
2078 Lst_Concat(slst, &srcs, LST_CONCLINK);
2079 Lst_Concat(slst, &targs, LST_CONCLINK);
2083 *-----------------------------------------------------------------------
2085 * Find implicit sources for the target described by the graph node
2092 * Nodes are added to the graph below the passed-in node. The nodes
2093 * are marked to have their IMPSRC variable filled in. The
2094 * PREFIX variable is set for the given node and all its
2098 * The path found by this target is the shortest path in the
2099 * transformation graph, which may pass through non-existent targets,
2100 * to an existing target. The search continues on all paths from the
2101 * root suffix until a file is found. I.e. if there's a path
2102 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2103 * the .c and .l files don't, the search will branch out in
2104 * all directions from .o and again from all the nodes on the
2105 * next level until the .l,v node is encountered.
2107 *-----------------------------------------------------------------------
2110 Suff_FindDeps(GNode *gn)
2113 SuffFindDeps(gn, &srclist);
2114 while (SuffRemoveSrc(&srclist))
2120 SuffFindDeps(GNode *gn, Lst *slst)
2123 if (gn->type & OP_DEPS_FOUND) {
2125 * If dependencies already found, no need to do it again...
2129 gn->type |= OP_DEPS_FOUND;
2132 DEBUGF(SUFF, ("SuffFindDeps (%s)\n", gn->name));
2134 if (gn->type & OP_ARCHV) {
2135 SuffFindArchiveDeps(gn, slst);
2136 } else if (gn->type & OP_LIB) {
2138 * If the node is a library, it is the arch module's job to find it
2139 * and set the TARGET variable accordingly. We merely provide the
2140 * search path, assuming all libraries end in ".a" (if the suffix
2141 * hasn't been defined, there's nothing we can do for it, so we just
2142 * set the TARGET variable to the node's name in order to give it a
2148 ln = Lst_Find(&sufflist, LIBSUFF, SuffSuffHasNameP);
2150 gn->suffix->refCount--;
2152 gn->suffix = s = Lst_Datum (ln);
2153 gn->suffix->refCount++;
2154 Arch_FindLib(gn, &s->searchPath);
2157 Var_Set(TARGET, gn->name, gn);
2160 * Because a library (-lfoo) target doesn't follow the standard
2161 * filesystem conventions, we don't set the regular variables for
2162 * the thing. .PREFIX is simply made empty...
2164 Var_Set(PREFIX, "", gn);
2166 SuffFindNormalDeps(gn, slst);
2171 *-----------------------------------------------------------------------
2173 * Define which suffix is the null suffix.
2179 * 'suffNull' is altered.
2182 * Need to handle the changing of the null suffix gracefully so the
2183 * old transformation rules don't just go away.
2185 *-----------------------------------------------------------------------
2188 Suff_SetNull(char *name)
2193 ln = Lst_Find(&sufflist, name, SuffSuffHasNameP);
2196 if (suffNull != NULL) {
2197 suffNull->flags &= ~SUFF_NULL;
2199 s->flags |= SUFF_NULL;
2201 * XXX: Here's where the transformation mangling would take place
2205 Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
2211 *-----------------------------------------------------------------------
2213 * Initialize suffixes module
2220 *-----------------------------------------------------------------------
2228 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2229 * actually go on the suffix list or everyone will think that's its
2232 emptySuff = suffNull = emalloc(sizeof(Suff));
2234 suffNull->name = estrdup("");
2235 suffNull->nameLen = 0;
2236 Lst_Init(&suffNull->searchPath);
2237 Dir_Concat(&suffNull->searchPath, &dirSearchPath);
2238 Lst_Init(&suffNull->children);
2239 Lst_Init(&suffNull->parents);
2240 Lst_Init(&suffNull->ref);
2241 suffNull->sNum = sNum++;
2242 suffNull->flags = SUFF_NULL;
2243 suffNull->refCount = 1;
2247 *----------------------------------------------------------------------
2249 * Cleanup the this module
2255 * The memory is free'd.
2256 *----------------------------------------------------------------------
2263 Lst_Destroy(&sufflist, SuffFree);
2264 Lst_Destroy(&suffClean, SuffFree);
2267 Lst_Destroy(&srclist, NOFREE);
2268 Lst_Destroy(&transforms, NOFREE);
2271 /********************* DEBUGGING FUNCTIONS **********************/
2274 SuffPrintName(void *s, void *dummy __unused)
2277 printf("`%s' ", ((Suff *)s)->name);
2282 SuffPrintSuff(void *sp, void *dummy __unused)
2288 printf("# `%s' [%d] ", s->name, s->refCount);
2292 fputs(" (", stdout);
2294 flag = 1 << (ffs(flags) - 1);
2309 fputc(flags ? '|' : ')', stdout);
2312 fputc('\n', stdout);
2314 Lst_ForEach(&s->parents, SuffPrintName, (void *)NULL);
2315 fputc('\n', stdout);
2316 printf("#\tFrom: ");
2317 Lst_ForEach(&s->children, SuffPrintName, (void *)NULL);
2318 fputc('\n', stdout);
2319 printf("#\tSearch Path: ");
2320 Dir_PrintPath(&s->searchPath);
2321 fputc('\n', stdout);
2326 SuffPrintTrans(void *tp, void *dummy __unused)
2330 printf("%-16s: ", t->name);
2331 Targ_PrintType(t->type);
2332 fputc('\n', stdout);
2333 Lst_ForEach(&t->commands, Targ_PrintCmd, (void *)NULL);
2334 fputc('\n', stdout);
2342 printf("#*** Suffixes:\n");
2343 Lst_ForEach(&sufflist, SuffPrintSuff, (void *)NULL);
2345 printf("#*** Transformations:\n");
2346 Lst_ForEach(&transforms, SuffPrintTrans, (void *)NULL);