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.14 2004/12/10 19:22:24 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.
99 static Lst sufflist; /* Lst of suffixes */
100 static Lst suffClean; /* Lst of suffixes to be cleaned */
101 static Lst srclist; /* Lst of sources */
102 static Lst transforms; /* Lst of transformation rules */
104 static int sNum = 0; /* Counter for assigning suffix numbers */
107 * Structure describing an individual suffix.
109 typedef struct _Suff {
110 char *name; /* The suffix itself */
111 int nameLen; /* Length of the suffix */
112 short flags; /* Type of suffix */
113 #define SUFF_INCLUDE 0x01 /* One which is #include'd */
114 #define SUFF_LIBRARY 0x02 /* One which contains a library */
115 #define SUFF_NULL 0x04 /* The empty suffix */
116 Lst searchPath; /* The path along which files of this suffix
118 int sNum; /* The suffix number */
119 int refCount; /* Reference count of list membership */
120 Lst parents; /* Suffixes we have a transformation to */
121 Lst children; /* Suffixes we have a transformation from */
122 Lst ref; /* List of lists this suffix is referenced */
126 * Structure used in the search for implied sources.
128 typedef struct _Src {
129 char *file; /* The file to look for */
130 char *pref; /* Prefix from which file was formed */
131 Suff *suff; /* The suffix on the file */
132 struct _Src *parent; /* The Src for which this is a source */
133 GNode *node; /* The node describing the file */
134 int children; /* Count of existing children (so we don't free
135 * this thing too early or never nuke it) */
137 Lst cp; /* Debug; children list */
142 * A structure for passing more than one argument to the Lst-library-invoked
150 static Suff *suffNull; /* The NULL suffix for this run */
151 static Suff *emptySuff; /* The empty suffix required for POSIX
152 * single-suffix transformation rules */
155 static char *SuffStrIsPrefix(char *, char *);
156 static char *SuffSuffIsSuffix(Suff *, char *);
157 static int SuffSuffIsSuffixP(void *, void *);
158 static int SuffSuffHasNameP(void *, void *);
159 static int SuffSuffIsPrefix(void *, void *);
160 static int SuffGNHasNameP(void *, void *);
161 static void SuffFree(void *);
162 static void SuffInsert(Lst, Suff *);
163 static void SuffRemove(Lst, Suff *);
164 static Boolean SuffParseTransform(char *, Suff **, Suff **);
165 static int SuffRebuildGraph(void *, void *);
166 static int SuffAddSrc(void *, void *);
167 static int SuffRemoveSrc(Lst);
168 static void SuffAddLevel(Lst, Src *);
169 static Src *SuffFindThem(Lst, Lst);
170 static Src *SuffFindCmds(Src *, Lst);
171 static int SuffExpandChildren(void *, void *);
172 static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
173 static void SuffFindDeps(GNode *, Lst);
174 static void SuffFindArchiveDeps(GNode *, Lst);
175 static void SuffFindNormalDeps(GNode *, Lst);
176 static int SuffPrintName(void *, void *);
177 static int SuffPrintSuff(void *, void *);
178 static int SuffPrintTrans(void *, void *);
180 /*************** Lst Predicates ****************/
182 *-----------------------------------------------------------------------
184 * See if pref is a prefix of str.
187 * NULL if it ain't, pointer to character in str after prefix if so
191 *-----------------------------------------------------------------------
194 SuffStrIsPrefix(char *pref, char *str)
197 while (*str && *pref == *str) {
202 return (*pref ? NULL : str);
206 *-----------------------------------------------------------------------
207 * SuffSuffIsSuffix --
208 * See if suff is a suffix of str. Str should point to THE END of the
209 * string to check. (THE END == the null byte)
212 * NULL if it ain't, pointer to character in str before suffix if
217 *-----------------------------------------------------------------------
220 SuffSuffIsSuffix(Suff *s, char *str)
222 char *p1; /* Pointer into suffix name */
223 char *p2; /* Pointer into string being examined */
225 p1 = s->name + s->nameLen;
228 while (p1 >= s->name && *p1 == *p2) {
233 return (p1 == s->name - 1 ? p2 : NULL);
237 *-----------------------------------------------------------------------
238 * SuffSuffIsSuffixP --
239 * Predicate form of SuffSuffIsSuffix. Passed as the callback function
243 * 0 if the suffix is the one desired, non-zero if not.
248 *-----------------------------------------------------------------------
251 SuffSuffIsSuffixP(void *s, void *str)
254 return (!SuffSuffIsSuffix((Suff *)s, (char *)str));
258 *-----------------------------------------------------------------------
259 * SuffSuffHasNameP --
260 * Callback procedure for finding a suffix based on its name. Used by
264 * 0 if the suffix is of the given name. non-zero otherwise.
268 *-----------------------------------------------------------------------
271 SuffSuffHasNameP(void *s, void *sname)
274 return (strcmp((char *)sname, ((Suff *)s)->name));
278 *-----------------------------------------------------------------------
279 * SuffSuffIsPrefix --
280 * See if the suffix described by s is a prefix of the string. Care
281 * must be taken when using this to search for transformations and
282 * what-not, since there could well be two suffixes, one of which
283 * is a prefix of the other...
286 * 0 if s is a prefix of str. non-zero otherwise
290 *-----------------------------------------------------------------------
293 SuffSuffIsPrefix(void *s, void *str)
296 return (SuffStrIsPrefix(((Suff *)s)->name, (char *)str) == NULL ? 1 : 0);
300 *-----------------------------------------------------------------------
302 * See if the graph node has the desired name
305 * 0 if it does. non-zero if it doesn't
309 *-----------------------------------------------------------------------
312 SuffGNHasNameP(void *gn, void *name)
315 return (strcmp((char *)name, ((GNode *)gn)->name));
318 /*********** Maintenance Functions ************/
321 *-----------------------------------------------------------------------
323 * Free up all memory associated with the given suffix structure.
329 * the suffix entry is detroyed
330 *-----------------------------------------------------------------------
335 Suff *s = (Suff *) sp;
343 Lst_Destroy(s->ref, NOFREE);
344 Lst_Destroy(s->children, NOFREE);
345 Lst_Destroy(s->parents, NOFREE);
346 Lst_Destroy(s->searchPath, Dir_Destroy);
353 *-----------------------------------------------------------------------
355 * Remove the suffix into the list
361 * The reference count for the suffix is decremented
362 *-----------------------------------------------------------------------
365 SuffRemove(Lst l, Suff *s)
367 LstNode ln = Lst_Member(l, (void *)s);
375 *-----------------------------------------------------------------------
377 * Insert the suffix into the list keeping the list ordered by suffix
384 * The reference count of the suffix is incremented
385 *-----------------------------------------------------------------------
388 SuffInsert(Lst l, Suff *s)
390 LstNode ln; /* current element in l we're examining */
391 Suff *s2 = NULL; /* the suffix descriptor in this element */
393 if (Lst_Open(l) == FAILURE) {
396 while ((ln = Lst_Next(l)) != NULL) {
397 s2 = (Suff *)Lst_Datum(ln);
398 if (s2->sNum >= s->sNum) {
403 DEBUGF(SUFF, ("inserting an empty list?..."));
407 DEBUGF(SUFF, ("inserting %s(%d)...", s->name, s->sNum));
409 DEBUGF(SUFF, ("at end of list\n"));
410 Lst_AtEnd (l, (void *)s);
412 Lst_AtEnd(s->ref, (void *)l);
413 } else if (s2->sNum != s->sNum) {
414 DEBUGF(SUFF, ("before %s(%d)\n", s2->name, s2->sNum));
415 Lst_Insert(l, ln, (void *)s);
417 Lst_AtEnd(s->ref, (void *)l);
419 DEBUGF(SUFF, ("already there\n"));
424 *-----------------------------------------------------------------------
425 * Suff_ClearSuffixes --
426 * This is gross. Nuke the list of suffixes but keep all transformation
427 * rules around. The transformation graph is destroyed in this process,
428 * but we leave the list of rules so when a new graph is formed the rules
430 * This function is called from the parse module when a
431 * .SUFFIXES:\n line is encountered.
437 * the sufflist and its graph nodes are destroyed
438 *-----------------------------------------------------------------------
441 Suff_ClearSuffixes(void)
444 Lst_Concat(suffClean, sufflist, LST_CONCLINK);
445 sufflist = Lst_Init(FALSE);
447 suffNull = emptySuff;
449 * Clear suffNull's children list (the other suffixes are built new, but
450 * suffNull is used as is).
451 * NOFREE is used because all suffixes are are on the suffClean list.
452 * suffNull should not have parents.
454 Lst_Destroy(suffNull->children, NOFREE);
455 suffNull->children = Lst_Init(FALSE);
459 *-----------------------------------------------------------------------
460 * SuffParseTransform --
461 * Parse a transformation string to find its two component suffixes.
464 * TRUE if the string is a valid transformation and FALSE otherwise.
467 * The passed pointers are overwritten.
469 *-----------------------------------------------------------------------
472 SuffParseTransform(char *str, Suff **srcPtr, Suff **targPtr)
474 LstNode srcLn; /* element in suffix list of trans source*/
475 Suff *src; /* Source of transformation */
476 LstNode targLn; /* element in suffix list of trans target*/
477 char *str2; /* Extra pointer (maybe target suffix) */
478 LstNode singleLn; /* element in suffix list of any suffix
479 * that exactly matches str */
480 Suff *single = NULL;/* Source of possible transformation to
487 * Loop looking first for a suffix that matches the start of the
488 * string and then for one that exactly matches the rest of it. If
489 * we can find two that meet these criteria, we've successfully
494 srcLn = Lst_Find(sufflist, (void *)str, SuffSuffIsPrefix);
496 srcLn = Lst_FindFrom(sufflist, Lst_Succ(srcLn), (void *)str,
501 * Ran out of source suffixes -- no such rule
503 if (singleLn != NULL) {
505 * Not so fast Mr. Smith! There was a suffix that encompassed
506 * the entire string, so we assume it was a transformation
507 * to the null suffix (thank you POSIX). We still prefer to
508 * find a double rule over a singleton, hence we leave this
509 * check until the end.
511 * XXX: Use emptySuff over suffNull?
519 src = (Suff *) Lst_Datum (srcLn);
520 str2 = str + src->nameLen;
525 targLn = Lst_Find(sufflist, (void *)str2, SuffSuffHasNameP);
526 if (targLn != NULL) {
528 *targPtr = (Suff *)Lst_Datum(targLn);
536 *-----------------------------------------------------------------------
537 * Suff_IsTransform --
538 * Return TRUE if the given string is a transformation rule
542 * TRUE if the string is a concatenation of two known suffixes.
547 *-----------------------------------------------------------------------
550 Suff_IsTransform(char *str)
554 return (SuffParseTransform(str, &src, &targ));
558 *-----------------------------------------------------------------------
559 * Suff_AddTransform --
560 * Add the transformation rule described by the line to the
561 * list of rules and place the transformation itself in the graph
564 * The node created for the transformation in the transforms list
567 * The node is placed on the end of the transforms Lst and links are
568 * made between the two suffixes mentioned in the target name
569 *-----------------------------------------------------------------------
572 Suff_AddTransform(char *line)
574 GNode *gn; /* GNode of transformation rule */
575 Suff *s, /* source suffix */
576 *t; /* target suffix */
577 LstNode ln; /* Node for existing transformation */
579 ln = Lst_Find(transforms, (void *)line, SuffGNHasNameP);
582 * Make a new graph node for the transformation. It will be filled in
583 * by the Parse module.
585 gn = Targ_NewGN(line);
586 Lst_AtEnd (transforms, (void *)gn);
589 * New specification for transformation rule. Just nuke the old list
590 * of commands so they can be filled in again... We don't actually
591 * free the commands themselves, because a given command can be
592 * attached to several different transformations.
594 gn = (GNode *)Lst_Datum(ln);
595 Lst_Destroy(gn->commands, NOFREE);
596 Lst_Destroy(gn->children, NOFREE);
597 gn->commands = Lst_Init(FALSE);
598 gn->children = Lst_Init(FALSE);
601 gn->type = OP_TRANSFORM;
603 SuffParseTransform(line, &s, &t);
606 * link the two together in the proper relationship and order
608 DEBUGF(SUFF, ("defining transformation from `%s' to `%s'\n",
610 SuffInsert(t->children, s);
611 SuffInsert(s->parents, t);
617 *-----------------------------------------------------------------------
618 * Suff_EndTransform --
619 * Handle the finish of a transformation definition, removing the
620 * transformation from the graph if it has neither commands nor
621 * sources. This is a callback procedure for the Parse module via
628 * If the node has no commands or children, the children and parents
629 * lists of the affected suffices are altered.
631 *-----------------------------------------------------------------------
634 Suff_EndTransform(void *gnp, void *dummy __unused)
636 GNode *gn = (GNode *)gnp;
638 if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
639 Lst_IsEmpty(gn->children))
644 * SuffParseTransform() may fail for special rules which are not
645 * actual transformation rules (e.g., .DEFAULT).
647 if (!SuffParseTransform(gn->name, &s, &t))
650 DEBUGF(SUFF, ("deleting transformation from `%s' to `%s'\n",
654 * Remove the source from the target's children list. We check for a
655 * NULL return to handle a beanhead saying something like
658 * We'll be called twice when the next target is seen, but .c and .o
659 * are only linked once...
661 SuffRemove(t->children, s);
664 * Remove the target from the source's parents list
666 SuffRemove(s->parents, t);
667 } else if (gn->type & OP_TRANSFORM) {
668 DEBUGF(SUFF, ("transformation %s complete\n", gn->name));
675 *-----------------------------------------------------------------------
676 * SuffRebuildGraph --
677 * Called from Suff_AddSuffix via Lst_ForEach to search through the
678 * list of existing transformation rules and rebuild the transformation
679 * graph when it has been destroyed by Suff_ClearSuffixes. If the
680 * given rule is a transformation involving this suffix and another,
681 * existing suffix, the proper relationship is established between
688 * The appropriate links will be made between this suffix and
689 * others if transformation rules exist for it.
691 *-----------------------------------------------------------------------
694 SuffRebuildGraph(void *transformp, void *sp)
696 GNode *transform = (GNode *)transformp;
697 Suff *s = (Suff *)sp;
703 * First see if it is a transformation from this suffix.
705 cp = SuffStrIsPrefix(s->name, transform->name);
706 if (cp != (char *)NULL) {
707 if (cp[0] == '\0') /* null rule */
710 ln = Lst_Find(sufflist, (void *)cp, SuffSuffHasNameP);
712 s2 = (Suff *)Lst_Datum(ln);
716 * Found target. Link in and return, since it can't be anything
719 SuffInsert(s2->children, s);
720 SuffInsert(s->parents, s2);
726 * Not from, maybe to?
728 cp = SuffSuffIsSuffix(s, transform->name + strlen(transform->name));
729 if (cp != (char *)NULL) {
731 * Null-terminate the source suffix in order to find it.
734 ln = Lst_Find(sufflist, (void *)transform->name, SuffSuffHasNameP);
736 * Replace the start of the target suffix
741 * Found it -- establish the proper relationship
743 s2 = (Suff *)Lst_Datum(ln);
744 SuffInsert(s->children, s2);
745 SuffInsert(s2->parents, s);
752 *-----------------------------------------------------------------------
754 * Add the suffix in string to the end of the list of known suffixes.
755 * Should we restructure the suffix graph? Make doesn't...
761 * A GNode is created for the suffix and a Suff structure is created and
762 * added to the suffixes list unless the suffix was already known.
763 *-----------------------------------------------------------------------
766 Suff_AddSuffix(char *str)
768 Suff *s; /* new suffix descriptor */
771 ln = Lst_Find(sufflist, (void *)str, SuffSuffHasNameP);
773 s = (Suff *)emalloc(sizeof(Suff));
775 s->name = estrdup(str);
776 s->nameLen = strlen (s->name);
777 s->searchPath = Lst_Init(FALSE);
778 s->children = Lst_Init(FALSE);
779 s->parents = Lst_Init(FALSE);
780 s->ref = Lst_Init(FALSE);
785 Lst_AtEnd(sufflist, (void *)s);
787 * Look for any existing transformations from or to this suffix.
788 * XXX: Only do this after a Suff_ClearSuffixes?
790 Lst_ForEach(transforms, SuffRebuildGraph, (void *)s);
795 *-----------------------------------------------------------------------
797 * Return the search path for the given suffix, if it's defined.
800 * The searchPath for the desired suffix or NULL if the suffix isn't
805 *-----------------------------------------------------------------------
808 Suff_GetPath(char *sname)
813 ln = Lst_Find(sufflist, (void *)sname, SuffSuffHasNameP);
817 s = (Suff *)Lst_Datum(ln);
818 return (s->searchPath);
823 *-----------------------------------------------------------------------
825 * Extend the search paths for all suffixes to include the default
832 * The searchPath field of all the suffixes is extended by the
833 * directories in dirSearchPath. If paths were specified for the
834 * ".h" suffix, the directories are stuffed into a global variable
835 * called ".INCLUDES" with each directory preceded by a -I. The same
836 * is done for the ".a" suffix, except the variable is called
837 * ".LIBS" and the flag is -L.
838 *-----------------------------------------------------------------------
846 Lst inIncludes; /* Cumulative .INCLUDES path */
847 Lst inLibs; /* Cumulative .LIBS path */
849 if (Lst_Open(sufflist) == FAILURE) {
853 inIncludes = Lst_Init(FALSE);
854 inLibs = Lst_Init(FALSE);
856 while ((ln = Lst_Next(sufflist)) != NULL) {
857 s = (Suff *)Lst_Datum(ln);
858 if (!Lst_IsEmpty(s->searchPath)) {
860 if (s->flags & SUFF_INCLUDE) {
861 Dir_Concat(inIncludes, s->searchPath);
863 #endif /* INCLUDES */
865 if (s->flags & SUFF_LIBRARY) {
866 Dir_Concat(inLibs, s->searchPath);
868 #endif /* LIBRARIES */
869 Dir_Concat(s->searchPath, dirSearchPath);
871 Lst_Destroy(s->searchPath, Dir_Destroy);
872 s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir);
876 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
878 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
881 Lst_Destroy(inIncludes, Dir_Destroy);
882 Lst_Destroy(inLibs, Dir_Destroy);
888 *-----------------------------------------------------------------------
890 * Add the given suffix as a type of file which gets included.
891 * Called from the parse module when a .INCLUDES line is parsed.
892 * The suffix must have already been defined.
898 * The SUFF_INCLUDE bit is set in the suffix's flags field
900 *-----------------------------------------------------------------------
903 Suff_AddInclude(char *sname)
908 ln = Lst_Find(sufflist, (void *)sname, SuffSuffHasNameP);
910 s = (Suff *)Lst_Datum(ln);
911 s->flags |= SUFF_INCLUDE;
916 *-----------------------------------------------------------------------
918 * Add the given suffix as a type of file which is a library.
919 * Called from the parse module when parsing a .LIBS line. The
920 * suffix must have been defined via .SUFFIXES before this is
927 * The SUFF_LIBRARY bit is set in the suffix's flags field
929 *-----------------------------------------------------------------------
932 Suff_AddLib(char *sname)
937 ln = Lst_Find(sufflist, (void *)sname, SuffSuffHasNameP);
939 s = (Suff *)Lst_Datum(ln);
940 s->flags |= SUFF_LIBRARY;
944 /********** Implicit Source Search Functions *********/
947 *-----------------------------------------------------------------------
949 * Add a suffix as a Src structure to the given list with its parent
950 * being the given Src structure. If the suffix is the null suffix,
951 * the prefix is used unaltered as the file name in the Src structure.
957 * A Src structure is created and tacked onto the end of the list
958 *-----------------------------------------------------------------------
961 SuffAddSrc(void *sp, void *lsp)
963 Suff *s = (Suff *) sp;
964 LstSrc *ls = (LstSrc *) lsp;
965 Src *s2; /* new Src structure */
966 Src *targ; /* Target structure */
970 if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
972 * If the suffix has been marked as the NULL suffix, also create a Src
973 * structure for a file with no suffix attached. Two birds, and all
976 s2 = (Src *)emalloc(sizeof(Src));
977 s2->file = estrdup(targ->pref);
978 s2->pref = targ->pref;
985 Lst_AtEnd(ls->l, (void *)s2);
987 s2->cp = Lst_Init(FALSE);
988 Lst_AtEnd(targ->cp, (void *) s2);
989 printf("1 add %x %x to %x:", targ, s2, ls->l);
990 Lst_ForEach(ls->l, PrintAddr, (void *) 0);
994 s2 = (Src *)emalloc(sizeof(Src));
995 s2->file = str_concat(targ->pref, s->name, 0);
996 s2->pref = targ->pref;
1002 targ->children += 1;
1003 Lst_AtEnd(ls->l, (void *)s2);
1005 s2->cp = Lst_Init(FALSE);
1006 Lst_AtEnd(targ->cp, (void *) s2);
1007 printf("2 add %x %x to %x:", targ, s2, ls->l);
1008 Lst_ForEach(ls->l, PrintAddr, (void *) 0);
1016 *-----------------------------------------------------------------------
1018 * Add all the children of targ as Src structures to the given list
1024 * Lots of structures are created and added to the list
1025 *-----------------------------------------------------------------------
1028 SuffAddLevel(Lst l, Src *targ)
1035 Lst_ForEach(targ->suff->children, SuffAddSrc, (void *)&ls);
1039 *----------------------------------------------------------------------
1041 * Free all src structures in list that don't have a reference count
1044 * True if a src was removed
1047 * The memory is free'd.
1048 *----------------------------------------------------------------------
1051 SuffRemoveSrc(Lst l)
1057 if (Lst_Open(l) == FAILURE) {
1061 printf("cleaning %lx: ", (unsigned long) l);
1062 Lst_ForEach(l, PrintAddr, (void *) 0);
1067 while ((ln = Lst_Next(l)) != NULL) {
1068 s = (Src *)Lst_Datum(ln);
1069 if (s->children == 0) {
1075 LstNode ln = Lst_Member(s->parent->cp, (void *)s);
1077 Lst_Remove(s->parent->cp, ln);
1079 --s->parent->children;
1082 printf("free: [l=%x] p=%x %d\n", l, s, s->children);
1083 Lst_Destroy(s->cp, NOFREE);
1093 printf("keep: [l=%x] p=%x %d: ", l, s, s->children);
1094 Lst_ForEach(s->cp, PrintAddr, (void *)0);
1106 *-----------------------------------------------------------------------
1108 * Find the first existing file/target in the list srcs
1111 * The lowest structure in the chain of transformations
1115 *-----------------------------------------------------------------------
1118 SuffFindThem (Lst srcs, Lst slst)
1120 Src *s; /* current Src */
1121 Src *rs; /* returned Src */
1126 while (!Lst_IsEmpty (srcs)) {
1127 s = (Src *)Lst_DeQueue(srcs);
1129 DEBUGF(SUFF, ("\ttrying %s...", s->file));
1132 * A file is considered to exist if either a node exists in the
1133 * graph for it or the file actually exists.
1135 if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
1137 printf("remove %x from %x\n", s, srcs);
1143 if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) {
1146 printf("remove %x from %x\n", s, srcs);
1152 DEBUGF(SUFF, ("not there\n"));
1154 SuffAddLevel(srcs, s);
1155 Lst_AtEnd(slst, (void *)s);
1159 DEBUGF(SUFF, ("got it\n"));
1165 *-----------------------------------------------------------------------
1167 * See if any of the children of the target in the Src structure is
1168 * one from which the target can be transformed. If there is one,
1169 * a Src structure is put together for it and returned.
1172 * The Src structure of the "winning" child, or NULL if no such beast.
1175 * A Src structure may be allocated.
1177 *-----------------------------------------------------------------------
1180 SuffFindCmds (Src *targ, Lst slst)
1182 LstNode ln; /* General-purpose list node */
1183 GNode *t, /* Target GNode */
1184 *s; /* Source GNode */
1185 int prefLen;/* The length of the defined prefix */
1186 Suff *suff; /* Suffix on matching beastie */
1187 Src *ret; /* Return value */
1191 Lst_Open(t->children);
1192 prefLen = strlen(targ->pref);
1194 while ((ln = Lst_Next(t->children)) != NULL) {
1195 s = (GNode *)Lst_Datum(ln);
1197 cp = strrchr(s->name, '/');
1198 if (cp == (char *)NULL) {
1203 if (strncmp(cp, targ->pref, prefLen) == 0) {
1205 * The node matches the prefix ok, see if it has a known
1208 ln = Lst_Find(sufflist, (void *)&cp[prefLen], SuffSuffHasNameP);
1211 * It even has a known suffix, see if there's a transformation
1212 * defined between the node's suffix and the target's suffix.
1214 * XXX: Handle multi-stage transformations here, too.
1216 suff = (Suff *)Lst_Datum(ln);
1218 if (Lst_Member(suff->parents, (void *)targ->suff) != NULL)
1221 * Hot Damn! Create a new Src structure to describe
1222 * this transformation (making sure to duplicate the
1223 * source node's name so Suff_FindDeps can free it
1224 * again (ick)), and return the new structure.
1226 ret = (Src *)emalloc (sizeof(Src));
1227 ret->file = estrdup(s->name);
1228 ret->pref = targ->pref;
1234 targ->children += 1;
1236 ret->cp = Lst_Init(FALSE);
1237 printf("3 add %x %x\n", targ, ret);
1238 Lst_AtEnd(targ->cp, (void *)ret);
1240 Lst_AtEnd(slst, (void *)ret);
1241 DEBUGF(SUFF, ("\tusing existing source %s\n", s->name));
1247 Lst_Close(t->children);
1248 return ((Src *)NULL);
1252 *-----------------------------------------------------------------------
1253 * SuffExpandChildren --
1254 * Expand the names of any children of a given node that contain
1255 * variable invocations or file wildcards into actual targets.
1261 * The expanded node is removed from the parent's list of children,
1262 * and the parent's unmade counter is decremented, but other nodes
1265 *-----------------------------------------------------------------------
1268 SuffExpandChildren(void *cgnp, void *pgnp)
1270 GNode *cgn = (GNode *)cgnp;
1271 GNode *pgn = (GNode *)pgnp;
1272 GNode *gn; /* New source 8) */
1273 LstNode prevLN; /* Node after which new source should be put */
1274 LstNode ln; /* List element for old source */
1275 char *cp; /* Expanded value */
1278 * New nodes effectively take the place of the child, so place them
1281 prevLN = Lst_Member(pgn->children, (void *)cgn);
1284 * First do variable expansion -- this takes precedence over
1285 * wildcard expansion. If the result contains wildcards, they'll be gotten
1286 * to later since the resulting words are tacked on to the end of
1287 * the children list.
1289 if (strchr(cgn->name, '$') != (char *)NULL) {
1290 DEBUGF(SUFF, ("Expanding \"%s\"...", cgn->name));
1291 cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
1293 if (cp != (char *)NULL) {
1294 Lst members = Lst_Init(FALSE);
1296 if (cgn->type & OP_ARCHV) {
1298 * Node was an archive(member) target, so we want to call
1299 * on the Arch module to find the nodes for us, expanding
1300 * variables in the parent's context.
1302 char *sacrifice = cp;
1304 Arch_ParseArchive(&sacrifice, members, pgn);
1307 * Break the result into a vector of strings whose nodes
1308 * we can find, then add those nodes to the members list.
1309 * Unfortunately, we can't use brk_string b/c it
1310 * doesn't understand about variable specifications with
1314 char *initcp = cp; /* For freeing... */
1316 for (start = cp; *start == ' ' || *start == '\t'; start++)
1318 for (cp = start; *cp != '\0'; cp++) {
1319 if (*cp == ' ' || *cp == '\t') {
1321 * White-space -- terminate element, find the node,
1322 * add it, skip any further spaces.
1325 gn = Targ_FindNode(start, TARG_CREATE);
1326 Lst_AtEnd(members, (void *)gn);
1327 while (*cp == ' ' || *cp == '\t') {
1331 * Adjust cp for increment at start of loop, but
1332 * set start to first non-space.
1335 } else if (*cp == '$') {
1337 * Start of a variable spec -- contact variable module
1338 * to find the end so we can skip over it.
1344 junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
1345 if (junk != var_Error) {
1352 } else if (*cp == '\\' && *cp != '\0') {
1354 * Escaped something -- skip over it
1362 * Stuff left over -- add it to the list too
1364 gn = Targ_FindNode(start, TARG_CREATE);
1365 Lst_AtEnd(members, (void *)gn);
1368 * Point cp back at the beginning again so the variable value
1374 * Add all elements of the members list to the parent node.
1376 while(!Lst_IsEmpty(members)) {
1377 gn = (GNode *)Lst_DeQueue(members);
1379 DEBUGF(SUFF, ("%s...", gn->name));
1380 if (Lst_Member(pgn->children, (void *)gn) == NULL) {
1381 Lst_Append(pgn->children, prevLN, (void *)gn);
1382 prevLN = Lst_Succ(prevLN);
1383 Lst_AtEnd(gn->parents, (void *)pgn);
1387 Lst_Destroy(members, NOFREE);
1394 * Now the source is expanded, remove it from the list of children to
1395 * keep it from being processed.
1397 ln = Lst_Member(pgn->children, (void *)cgn);
1399 Lst_Remove(pgn->children, ln);
1400 DEBUGF(SUFF, ("\n"));
1401 } else if (Dir_HasWildcards(cgn->name)) {
1402 Lst exp; /* List of expansions */
1403 Lst path; /* Search path along which to expand */
1406 * Find a path along which to expand the word.
1408 * If the word has a known suffix, use that path.
1409 * If it has no known suffix and we're allowed to use the null
1410 * suffix, use its path.
1411 * Else use the default system search path.
1413 cp = cgn->name + strlen(cgn->name);
1414 ln = Lst_Find(sufflist, (void *)cp, SuffSuffIsSuffixP);
1416 DEBUGF(SUFF, ("Wildcard expanding \"%s\"...", cgn->name));
1419 Suff *s = (Suff *)Lst_Datum(ln);
1421 DEBUGF(SUFF, ("suffix is \"%s\"...", s->name));
1422 path = s->searchPath;
1425 * Use default search path
1427 path = dirSearchPath;
1431 * Expand the word along the chosen path
1433 exp = Lst_Init(FALSE);
1434 Dir_Expand(cgn->name, path, exp);
1436 while (!Lst_IsEmpty(exp)) {
1438 * Fetch next expansion off the list and find its GNode
1440 cp = (char *)Lst_DeQueue(exp);
1442 DEBUGF(SUFF, ("%s...", cp));
1443 gn = Targ_FindNode(cp, TARG_CREATE);
1446 * If gn isn't already a child of the parent, make it so and
1447 * up the parent's count of unmade children.
1449 if (Lst_Member(pgn->children, (void *)gn) == NULL) {
1450 Lst_Append(pgn->children, prevLN, (void *)gn);
1451 prevLN = Lst_Succ(prevLN);
1452 Lst_AtEnd(gn->parents, (void *)pgn);
1458 * Nuke what's left of the list
1460 Lst_Destroy(exp, NOFREE);
1463 * Now the source is expanded, remove it from the list of children to
1464 * keep it from being processed.
1466 ln = Lst_Member(pgn->children, (void *)cgn);
1468 Lst_Remove(pgn->children, ln);
1469 DEBUGF(SUFF, ("\n"));
1476 *-----------------------------------------------------------------------
1477 * SuffApplyTransform --
1478 * Apply a transformation rule, given the source and target nodes
1482 * TRUE if successful, FALSE if not.
1485 * The source and target are linked and the commands from the
1486 * transformation are added to the target node's commands list.
1487 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1488 * to the target. The target also inherits all the sources for
1489 * the transformation rule.
1491 *-----------------------------------------------------------------------
1494 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
1496 LstNode ln; /* General node */
1497 char *tname; /* Name of transformation rule */
1498 GNode *gn; /* Node for same */
1500 if (Lst_Member(tGn->children, (void *)sGn) == NULL) {
1502 * Not already linked, so form the proper links between the
1503 * target and source.
1505 Lst_AtEnd(tGn->children, (void *)sGn);
1506 Lst_AtEnd(sGn->parents, (void *)tGn);
1510 if ((sGn->type & OP_OPMASK) == OP_DOUBLEDEP) {
1512 * When a :: node is used as the implied source of a node, we have
1513 * to link all its cohorts in as sources as well. Only the initial
1514 * sGn gets the target in its iParents list, however, as that
1515 * will be sufficient to get the .IMPSRC variable set for tGn
1517 for (ln=Lst_First(sGn->cohorts); ln != NULL; ln=Lst_Succ(ln)) {
1518 gn = (GNode *)Lst_Datum(ln);
1520 if (Lst_Member(tGn->children, (void *)gn) == NULL) {
1522 * Not already linked, so form the proper links between the
1523 * target and source.
1525 Lst_AtEnd(tGn->children, (void *)gn);
1526 Lst_AtEnd(gn->parents, (void *)tGn);
1532 * Locate the transformation rule itself
1534 tname = str_concat(s->name, t->name, 0);
1535 ln = Lst_Find(transforms, (void *)tname, SuffGNHasNameP);
1540 * Not really such a transformation rule (can happen when we're
1541 * called to link an OP_MEMBER and OP_ARCHV node), so return
1547 gn = (GNode *)Lst_Datum(ln);
1549 DEBUGF(SUFF, ("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name));
1552 * Record last child for expansion purposes
1554 ln = Lst_Last(tGn->children);
1557 * Pass the buck to Make_HandleUse to apply the rule
1559 Make_HandleUse(gn, tGn);
1562 * Deal with wildcards and variables in any acquired sources
1566 Lst_ForEachFrom(tGn->children, ln, SuffExpandChildren, (void *)tGn);
1570 * Keep track of another parent to which this beast is transformed so
1571 * the .IMPSRC variable can be set correctly for the parent.
1573 Lst_AtEnd(sGn->iParents, (void *)tGn);
1580 *-----------------------------------------------------------------------
1581 * SuffFindArchiveDeps --
1582 * Locate dependencies for an OP_ARCHV node.
1588 * Same as Suff_FindDeps
1590 *-----------------------------------------------------------------------
1593 SuffFindArchiveDeps(GNode *gn, Lst slst)
1595 char *eoarch; /* End of archive portion */
1596 char *eoname; /* End of member portion */
1597 GNode *mem; /* Node for member */
1598 static char *copy[] = { /* Variables to be copied from the member node */
1599 TARGET, /* Must be first */
1600 PREFIX, /* Must be second */
1602 int i; /* Index into copy and vals */
1603 Suff *ms; /* Suffix descriptor for member */
1604 char *name; /* Start of member's name */
1607 * The node is an archive(member) pair. so we must find a
1608 * suffix for both of them.
1610 eoarch = strchr(gn->name, '(');
1611 eoname = strchr(eoarch, ')');
1613 *eoname = '\0'; /* Nuke parentheses during suffix search */
1614 *eoarch = '\0'; /* So a suffix can be found */
1619 * To simplify things, call Suff_FindDeps recursively on the member now,
1620 * so we can simply compare the member's .PREFIX and .TARGET variables
1621 * to locate its suffix. This allows us to figure out the suffix to
1622 * use for the archive without having to do a quadratic search over the
1623 * suffix list, backtracking for each one...
1625 mem = Targ_FindNode(name, TARG_CREATE);
1626 SuffFindDeps(mem, slst);
1629 * Create the link between the two nodes right off
1631 if (Lst_Member(gn->children, (void *)mem) == NULL) {
1632 Lst_AtEnd(gn->children, (void *)mem);
1633 Lst_AtEnd(mem->parents, (void *)gn);
1638 * Copy in the variables from the member node to this one.
1640 for (i = (sizeof(copy) / sizeof(copy[0]))-1; i >= 0; i--) {
1642 Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn);
1650 * Didn't know what it was -- use .NULL suffix if not in make mode
1652 DEBUGF(SUFF, ("using null suffix\n"));
1658 * Set the other two local variables required for this target.
1660 Var_Set(MEMBER, name, gn);
1661 Var_Set(ARCHIVE, gn->name, gn);
1665 * Member has a known suffix, so look for a transformation rule from
1666 * it to a possible suffix of the archive. Rather than searching
1667 * through the entire list, we just look at suffixes to which the
1668 * member's suffix may be transformed...
1673 * Use first matching suffix...
1675 ln = Lst_Find(ms->parents, eoarch, SuffSuffIsSuffixP);
1679 * Got one -- apply it
1681 if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms)) {
1682 DEBUGF(SUFF, ("\tNo transformation from %s -> %s\n",
1683 ms->name, ((Suff *)Lst_Datum(ln))->name));
1689 * Replace the opening and closing parens now we've no need of the separate
1692 *eoarch = '('; *eoname = ')';
1695 * Pretend gn appeared to the left of a dependency operator so
1696 * the user needn't provide a transformation from the member to the
1699 if (OP_NOP(gn->type)) {
1700 gn->type |= OP_DEPENDS;
1704 * Flag the member as such so we remember to look in the archive for
1705 * its modification time.
1707 mem->type |= OP_MEMBER;
1711 *-----------------------------------------------------------------------
1712 * SuffFindNormalDeps --
1713 * Locate implicit dependencies for regular targets.
1719 * Same as Suff_FindDeps...
1721 *-----------------------------------------------------------------------
1724 SuffFindNormalDeps(GNode *gn, Lst slst)
1726 char *eoname; /* End of name */
1727 char *sopref; /* Start of prefix */
1728 LstNode ln; /* Next suffix node to check */
1729 Lst srcs; /* List of sources at which to look */
1730 Lst targs; /* List of targets to which things can be
1731 * transformed. They all have the same file,
1732 * but different suff and pref fields */
1733 Src *bottom; /* Start of found transformation path */
1734 Src *src; /* General Src pointer */
1735 char *pref; /* Prefix to use */
1736 Src *targ; /* General Src target pointer */
1739 eoname = gn->name + strlen(gn->name);
1744 * Begin at the beginning...
1746 ln = Lst_First(sufflist);
1747 srcs = Lst_Init(FALSE);
1748 targs = Lst_Init(FALSE);
1751 * We're caught in a catch-22 here. On the one hand, we want to use any
1752 * transformation implied by the target's sources, but we can't examine
1753 * the sources until we've expanded any variables/wildcards they may hold,
1754 * and we can't do that until we've set up the target's local variables
1755 * and we can't do that until we know what the proper suffix for the
1756 * target is (in case there are two suffixes one of which is a suffix of
1757 * the other) and we can't know that until we've found its implied
1758 * source, which we may not want to use if there's an existing source
1759 * that implies a different transformation.
1761 * In an attempt to get around this, which may not work all the time,
1762 * but should work most of the time, we look for implied sources first,
1763 * checking transformations to all possible suffixes of the target,
1764 * use what we find to set the target's local variables, expand the
1765 * children, then look for any overriding transformations they imply.
1766 * Should we find one, we discard the one we found before.
1769 while (ln != NULL) {
1771 * Look for next possible suffix...
1773 ln = Lst_FindFrom(sufflist, ln, eoname, SuffSuffIsSuffixP);
1776 int prefLen; /* Length of the prefix */
1780 * Allocate a Src structure to which things can be transformed
1782 target = (Src *)emalloc(sizeof(Src));
1783 target->file = estrdup(gn->name);
1784 target->suff = (Suff *)Lst_Datum(ln);
1785 target->suff->refCount++;
1787 target->parent = (Src *)NULL;
1788 target->children = 0;
1790 target->cp = Lst_Init(FALSE);
1794 * Allocate room for the prefix, whose end is found by subtracting
1795 * the length of the suffix from the end of the name.
1797 prefLen = (eoname - target->suff->nameLen) - sopref;
1798 target->pref = emalloc(prefLen + 1);
1799 memcpy(target->pref, sopref, prefLen);
1800 target->pref[prefLen] = '\0';
1803 * Add nodes from which the target can be made
1805 SuffAddLevel(srcs, target);
1808 * Record the target so we can nuke it
1810 Lst_AtEnd(targs, (void *)target);
1813 * Search from this suffix's successor...
1820 * Handle target of unknown suffix...
1822 if (Lst_IsEmpty(targs) && suffNull != NULL) {
1823 DEBUGF(SUFF, ("\tNo known suffix on %s. Using .NULL suffix\n", gn->name));
1825 targ = (Src *)emalloc(sizeof(Src));
1826 targ->file = estrdup(gn->name);
1827 targ->suff = suffNull;
1828 targ->suff->refCount++;
1830 targ->parent = (Src *)NULL;
1832 targ->pref = estrdup(sopref);
1834 targ->cp = Lst_Init(FALSE);
1838 * Only use the default suffix rules if we don't have commands
1839 * or dependencies defined for this gnode
1841 if (Lst_IsEmpty(gn->commands) && Lst_IsEmpty(gn->children))
1842 SuffAddLevel(srcs, targ);
1844 DEBUGF(SUFF, ("not "));
1847 DEBUGF(SUFF, ("adding suffix rules\n"));
1849 Lst_AtEnd(targs, (void *)targ);
1853 * Using the list of possible sources built up from the target suffix(es),
1854 * try and find an existing file/target that matches.
1856 bottom = SuffFindThem(srcs, slst);
1858 if (bottom == (Src *)NULL) {
1860 * No known transformations -- use the first suffix found for setting
1861 * the local variables.
1863 if (!Lst_IsEmpty(targs)) {
1864 targ = (Src *)Lst_Datum(Lst_First(targs));
1870 * Work up the transformation path to find the suffix of the
1871 * target to which the transformation was made.
1873 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
1878 * The .TARGET variable we always set to be the name at this point,
1879 * since it's only set to the path if the thing is only a source and
1880 * if it's only a source, it doesn't matter what we put here as far
1881 * as expanding sources is concerned, since it has none...
1883 Var_Set(TARGET, gn->name, gn);
1885 pref = (targ != NULL) ? targ->pref : gn->name;
1886 Var_Set(PREFIX, pref, gn);
1889 * Now we've got the important local variables set, expand any sources
1890 * that still contain variables or wildcards in their names.
1892 Lst_ForEach(gn->children, SuffExpandChildren, (void *)gn);
1895 DEBUGF(SUFF, ("\tNo valid suffix on %s\n", gn->name));
1899 * Deal with finding the thing on the default search path if the
1900 * node is only a source (not on the lhs of a dependency operator
1901 * or [XXX] it has neither children or commands).
1903 if (OP_NOP(gn->type) ||
1904 (Lst_IsEmpty(gn->children) && Lst_IsEmpty(gn->commands)))
1906 gn->path = Dir_FindFile(gn->name,
1907 (targ == NULL ? dirSearchPath :
1908 targ->suff->searchPath));
1909 if (gn->path != NULL) {
1911 Var_Set(TARGET, gn->path, gn);
1915 * Suffix known for the thing -- trim the suffix off
1916 * the path to form the proper .PREFIX variable.
1918 int savep = strlen(gn->path) - targ->suff->nameLen;
1922 gn->suffix->refCount--;
1923 gn->suffix = targ->suff;
1924 gn->suffix->refCount++;
1926 savec = gn->path[savep];
1927 gn->path[savep] = '\0';
1929 if ((ptr = strrchr(gn->path, '/')) != NULL)
1934 Var_Set(PREFIX, ptr, gn);
1936 gn->path[savep] = savec;
1939 * The .PREFIX gets the full path if the target has
1943 gn->suffix->refCount--;
1946 if ((ptr = strrchr(gn->path, '/')) != NULL)
1951 Var_Set(PREFIX, ptr, gn);
1956 * Not appropriate to search for the thing -- set the
1957 * path to be the name so Dir_MTime won't go grovelling for
1961 gn->suffix->refCount--;
1962 gn->suffix = (targ == NULL) ? NULL : targ->suff;
1964 gn->suffix->refCount++;
1966 gn->path = estrdup(gn->name);
1973 * If the suffix indicates that the target is a library, mark that in
1974 * the node's type field.
1976 if (targ->suff->flags & SUFF_LIBRARY) {
1981 * Check for overriding transformation rule implied by sources
1983 if (!Lst_IsEmpty(gn->children)) {
1984 src = SuffFindCmds(targ, slst);
1986 if (src != (Src *)NULL) {
1988 * Free up all the Src structures in the transformation path
1989 * up to, but not including, the parent node.
1991 while (bottom && bottom->parent != NULL) {
1992 if (Lst_Member(slst, (void *)bottom) == NULL) {
1993 Lst_AtEnd(slst, (void *)bottom);
1995 bottom = bottom->parent;
2001 if (bottom == NULL) {
2003 * No idea from where it can come -- return now.
2009 * We now have a list of Src structures headed by 'bottom' and linked via
2010 * their 'parent' pointers. What we do next is create links between
2011 * source and target nodes (which may or may not have been created)
2012 * and set the necessary local variables in each target. The
2013 * commands for each target are set from the commands of the
2014 * transformation rule used to get from the src suffix to the targ
2015 * suffix. Note that this causes the commands list of the original
2016 * node, gn, to be replaced by the commands of the final
2017 * transformation rule. Also, the unmade field of gn is incremented.
2020 if (bottom->node == NULL) {
2021 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2024 for (src = bottom; src->parent != (Src *)NULL; src = src->parent) {
2027 if (src->node->suffix)
2028 src->node->suffix->refCount--;
2029 src->node->suffix = src->suff;
2030 src->node->suffix->refCount++;
2032 if (targ->node == NULL) {
2033 targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2036 SuffApplyTransform(targ->node, src->node,
2037 targ->suff, src->suff);
2039 if (targ->node != gn) {
2041 * Finish off the dependency-search process for any nodes
2042 * between bottom and gn (no point in questing around the
2043 * filesystem for their implicit source when it's already
2044 * known). Note that the node can't have any sources that
2045 * need expanding, since SuffFindThem will stop on an existing
2046 * node, so all we need to do is set the standard and System V
2049 targ->node->type |= OP_DEPS_FOUND;
2051 Var_Set(PREFIX, targ->pref, targ->node);
2053 Var_Set(TARGET, targ->node->name, targ->node);
2058 gn->suffix->refCount--;
2059 gn->suffix = src->suff;
2060 gn->suffix->refCount++;
2063 * So Dir_MTime doesn't go questing for it...
2066 gn->path = estrdup(gn->name);
2069 * Nuke the transformation path and the Src structures left over in the
2074 if (Lst_Member(slst, (void *)bottom) == NULL)
2075 Lst_AtEnd(slst, (void *)bottom);
2077 while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
2080 Lst_Concat(slst, srcs, LST_CONCLINK);
2081 Lst_Concat(slst, targs, LST_CONCLINK);
2085 *-----------------------------------------------------------------------
2087 * Find implicit sources for the target described by the graph node
2094 * Nodes are added to the graph below the passed-in node. The nodes
2095 * are marked to have their IMPSRC variable filled in. The
2096 * PREFIX variable is set for the given node and all its
2100 * The path found by this target is the shortest path in the
2101 * transformation graph, which may pass through non-existent targets,
2102 * to an existing target. The search continues on all paths from the
2103 * root suffix until a file is found. I.e. if there's a path
2104 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2105 * the .c and .l files don't, the search will branch out in
2106 * all directions from .o and again from all the nodes on the
2107 * next level until the .l,v node is encountered.
2109 *-----------------------------------------------------------------------
2112 Suff_FindDeps(GNode *gn)
2115 SuffFindDeps(gn, srclist);
2116 while (SuffRemoveSrc(srclist))
2122 SuffFindDeps(GNode *gn, Lst slst)
2125 if (gn->type & OP_DEPS_FOUND) {
2127 * If dependencies already found, no need to do it again...
2131 gn->type |= OP_DEPS_FOUND;
2134 DEBUGF(SUFF, ("SuffFindDeps (%s)\n", gn->name));
2136 if (gn->type & OP_ARCHV) {
2137 SuffFindArchiveDeps(gn, slst);
2138 } else if (gn->type & OP_LIB) {
2140 * If the node is a library, it is the arch module's job to find it
2141 * and set the TARGET variable accordingly. We merely provide the
2142 * search path, assuming all libraries end in ".a" (if the suffix
2143 * hasn't been defined, there's nothing we can do for it, so we just
2144 * set the TARGET variable to the node's name in order to give it a
2150 ln = Lst_Find(sufflist, (void *)LIBSUFF, SuffSuffHasNameP);
2152 gn->suffix->refCount--;
2154 gn->suffix = s = (Suff *)Lst_Datum (ln);
2155 gn->suffix->refCount++;
2156 Arch_FindLib(gn, s->searchPath);
2159 Var_Set(TARGET, gn->name, gn);
2162 * Because a library (-lfoo) target doesn't follow the standard
2163 * filesystem conventions, we don't set the regular variables for
2164 * the thing. .PREFIX is simply made empty...
2166 Var_Set(PREFIX, "", gn);
2168 SuffFindNormalDeps(gn, slst);
2173 *-----------------------------------------------------------------------
2175 * Define which suffix is the null suffix.
2181 * 'suffNull' is altered.
2184 * Need to handle the changing of the null suffix gracefully so the
2185 * old transformation rules don't just go away.
2187 *-----------------------------------------------------------------------
2190 Suff_SetNull(char *name)
2195 ln = Lst_Find(sufflist, (void *)name, SuffSuffHasNameP);
2197 s = (Suff *)Lst_Datum(ln);
2198 if (suffNull != (Suff *)NULL) {
2199 suffNull->flags &= ~SUFF_NULL;
2201 s->flags |= SUFF_NULL;
2203 * XXX: Here's where the transformation mangling would take place
2207 Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
2213 *-----------------------------------------------------------------------
2215 * Initialize suffixes module
2222 *-----------------------------------------------------------------------
2228 sufflist = Lst_Init(FALSE);
2229 suffClean = Lst_Init(FALSE);
2230 srclist = Lst_Init(FALSE);
2231 transforms = Lst_Init(FALSE);
2235 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2236 * actually go on the suffix list or everyone will think that's its
2239 emptySuff = suffNull = (Suff *)emalloc(sizeof(Suff));
2241 suffNull->name = estrdup("");
2242 suffNull->nameLen = 0;
2243 suffNull->searchPath = Lst_Init(FALSE);
2244 Dir_Concat(suffNull->searchPath, dirSearchPath);
2245 suffNull->children = Lst_Init(FALSE);
2246 suffNull->parents = Lst_Init(FALSE);
2247 suffNull->ref = Lst_Init(FALSE);
2248 suffNull->sNum = sNum++;
2249 suffNull->flags = SUFF_NULL;
2250 suffNull->refCount = 1;
2254 *----------------------------------------------------------------------
2256 * Cleanup the this module
2262 * The memory is free'd.
2263 *----------------------------------------------------------------------
2270 Lst_Destroy(sufflist, SuffFree);
2271 Lst_Destroy(suffClean, SuffFree);
2274 Lst_Destroy(srclist, NOFREE);
2275 Lst_Destroy(transforms, NOFREE);
2278 /********************* DEBUGGING FUNCTIONS **********************/
2281 SuffPrintName(void *s, void *dummy __unused)
2284 printf("`%s' ", ((Suff *)s)->name);
2289 SuffPrintSuff(void *sp, void *dummy __unused)
2291 Suff *s = (Suff *)sp;
2295 printf("# `%s' [%d] ", s->name, s->refCount);
2299 fputs(" (", stdout);
2301 flag = 1 << (ffs(flags) - 1);
2316 fputc(flags ? '|' : ')', stdout);
2319 fputc('\n', stdout);
2321 Lst_ForEach (s->parents, SuffPrintName, (void *)0);
2322 fputc('\n', stdout);
2323 printf("#\tFrom: ");
2324 Lst_ForEach (s->children, SuffPrintName, (void *)0);
2325 fputc('\n', stdout);
2326 printf("#\tSearch Path: ");
2327 Dir_PrintPath(s->searchPath);
2328 fputc('\n', stdout);
2333 SuffPrintTrans(void *tp, void *dummy __unused)
2335 GNode *t = (GNode *)tp;
2337 printf("%-16s: ", t->name);
2338 Targ_PrintType(t->type);
2339 fputc('\n', stdout);
2340 Lst_ForEach(t->commands, Targ_PrintCmd, (void *)0);
2341 fputc('\n', stdout);
2349 printf("#*** Suffixes:\n");
2350 Lst_ForEach(sufflist, SuffPrintSuff, (void *)0);
2352 printf("#*** Transformations:\n");
2353 Lst_ForEach(transforms, SuffPrintTrans, (void *)0);