1 /* $NetBSD: suff.c,v 1.67 2009/01/23 21:58:28 dsl Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1989 by Berkeley Softworks
37 * All rights reserved.
39 * This code is derived from software contributed to Berkeley by
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 static char rcsid
[] = "$NetBSD: suff.c,v 1.67 2009/01/23 21:58:28 dsl Exp $";
74 #include <sys/cdefs.h>
77 static char sccsid
[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94";
79 __RCSID("$NetBSD: suff.c,v 1.67 2009/01/23 21:58:28 dsl Exp $");
86 * Functions to maintain suffix lists and find implicit dependents
87 * using suffix transformation rules
90 * Suff_Init Initialize all things to do with suffixes.
92 * Suff_End Cleanup the module
94 * Suff_DoPaths This function is used to make life easier
95 * when searching for a file according to its
96 * suffix. It takes the global search path,
97 * as defined using the .PATH: target, and appends
98 * its directories to the path of each of the
99 * defined suffixes, as specified using
100 * .PATH<suffix>: targets. In addition, all
101 * directories given for suffixes labeled as
102 * include files or libraries, using the .INCLUDES
103 * or .LIBS targets, are played with using
104 * Dir_MakeFlags to create the .INCLUDES and
105 * .LIBS global variables.
107 * Suff_ClearSuffixes Clear out all the suffixes and defined
110 * Suff_IsTransform Return TRUE if the passed string is the lhs
111 * of a transformation rule.
113 * Suff_AddSuffix Add the passed string as another known suffix.
115 * Suff_GetPath Return the search path for the given suffix.
117 * Suff_AddInclude Mark the given suffix as denoting an include
120 * Suff_AddLib Mark the given suffix as denoting a library.
122 * Suff_AddTransform Add another transformation to the suffix
123 * graph. Returns GNode suitable for framing, I
124 * mean, tacking commands, attributes, etc. on.
126 * Suff_SetNull Define the suffix to consider the suffix of
127 * any file that doesn't have a known one.
129 * Suff_FindDeps Find implicit sources for and the location of
130 * a target based on its suffix. Returns the
131 * bottom-most node added to the graph or NULL
132 * if the target had no implicit sources.
134 * Suff_FindPath Return the appropriate path to search in
135 * order to find the node.
144 static Lst sufflist
; /* Lst of suffixes */
146 static Lst suffClean
; /* Lst of suffixes to be cleaned */
148 static Lst srclist
; /* Lst of sources */
149 static Lst transforms
; /* Lst of transformation rules */
151 static int sNum
= 0; /* Counter for assigning suffix numbers */
154 * Structure describing an individual suffix.
156 typedef struct _Suff
{
157 char *name
; /* The suffix itself */
158 int nameLen
; /* Length of the suffix */
159 short flags
; /* Type of suffix */
160 #define SUFF_INCLUDE 0x01 /* One which is #include'd */
161 #define SUFF_LIBRARY 0x02 /* One which contains a library */
162 #define SUFF_NULL 0x04 /* The empty suffix */
163 Lst searchPath
; /* The path along which files of this suffix
165 int sNum
; /* The suffix number */
166 int refCount
; /* Reference count of list membership */
167 Lst parents
; /* Suffixes we have a transformation to */
168 Lst children
; /* Suffixes we have a transformation from */
169 Lst ref
; /* List of lists this suffix is referenced */
173 * for SuffSuffIsSuffix
176 char *ename
; /* The end of the name */
177 int len
; /* Length of the name */
181 * Structure used in the search for implied sources.
183 typedef struct _Src
{
184 char *file
; /* The file to look for */
185 char *pref
; /* Prefix from which file was formed */
186 Suff
*suff
; /* The suffix on the file */
187 struct _Src
*parent
; /* The Src for which this is a source */
188 GNode
*node
; /* The node describing the file */
189 int children
; /* Count of existing children (so we don't free
190 * this thing too early or never nuke it) */
192 Lst cp
; /* Debug; children list */
197 * A structure for passing more than one argument to the Lst-library-invoked
211 static Suff
*suffNull
; /* The NULL suffix for this run */
212 static Suff
*emptySuff
; /* The empty suffix required for POSIX
213 * single-suffix transformation rules */
216 static const char *SuffStrIsPrefix(const char *, const char *);
217 static char *SuffSuffIsSuffix(const Suff
*, const SuffixCmpData
*);
218 static int SuffSuffIsSuffixP(const void *, const void *);
219 static int SuffSuffHasNameP(const void *, const void *);
220 static int SuffSuffIsPrefix(const void *, const void *);
221 static int SuffGNHasNameP(const void *, const void *);
222 static void SuffUnRef(void *, void *);
223 static void SuffFree(void *);
224 static void SuffInsert(Lst
, Suff
*);
225 static void SuffRemove(Lst
, Suff
*);
226 static Boolean
SuffParseTransform(char *, Suff
**, Suff
**);
227 static int SuffRebuildGraph(void *, void *);
228 static int SuffScanTargets(void *, void *);
229 static int SuffAddSrc(void *, void *);
230 static int SuffRemoveSrc(Lst
);
231 static void SuffAddLevel(Lst
, Src
*);
232 static Src
*SuffFindThem(Lst
, Lst
);
233 static Src
*SuffFindCmds(Src
*, Lst
);
234 static void SuffExpandChildren(LstNode
, GNode
*);
235 static void SuffExpandWildcards(LstNode
, GNode
*);
236 static Boolean
SuffApplyTransform(GNode
*, GNode
*, Suff
*, Suff
*);
237 static void SuffFindDeps(GNode
*, Lst
);
238 static void SuffFindArchiveDeps(GNode
*, Lst
);
239 static void SuffFindNormalDeps(GNode
*, Lst
);
240 static int SuffPrintName(void *, void *);
241 static int SuffPrintSuff(void *, void *);
242 static int SuffPrintTrans(void *, void *);
244 /*************** Lst Predicates ****************/
246 *-----------------------------------------------------------------------
248 * See if pref is a prefix of str.
251 * pref possible prefix
252 * str string to check
255 * NULL if it ain't, pointer to character in str after prefix if so
259 *-----------------------------------------------------------------------
262 SuffStrIsPrefix(const char *pref
, const char *str
)
264 while (*str
&& *pref
== *str
) {
269 return (*pref
? NULL
: str
);
273 *-----------------------------------------------------------------------
274 * SuffSuffIsSuffix --
275 * See if suff is a suffix of str. sd->ename should point to THE END
276 * of the string to check. (THE END == the null byte)
280 * sd string to examine
283 * NULL if it ain't, pointer to character in str before suffix if
288 *-----------------------------------------------------------------------
291 SuffSuffIsSuffix(const Suff
*s
, const SuffixCmpData
*sd
)
293 char *p1
; /* Pointer into suffix name */
294 char *p2
; /* Pointer into string being examined */
296 if (sd
->len
< s
->nameLen
)
297 return NULL
; /* this string is shorter than the suffix */
299 p1
= s
->name
+ s
->nameLen
;
302 while (p1
>= s
->name
&& *p1
== *p2
) {
307 return (p1
== s
->name
- 1 ? p2
: NULL
);
311 *-----------------------------------------------------------------------
312 * SuffSuffIsSuffixP --
313 * Predicate form of SuffSuffIsSuffix. Passed as the callback function
317 * 0 if the suffix is the one desired, non-zero if not.
322 *-----------------------------------------------------------------------
325 SuffSuffIsSuffixP(const void *s
, const void *sd
)
327 return(!SuffSuffIsSuffix(s
, sd
));
331 *-----------------------------------------------------------------------
332 * SuffSuffHasNameP --
333 * Callback procedure for finding a suffix based on its name. Used by
341 * 0 if the suffix is of the given name. non-zero otherwise.
345 *-----------------------------------------------------------------------
348 SuffSuffHasNameP(const void *s
, const void *sname
)
350 return (strcmp(sname
, ((const Suff
*)s
)->name
));
354 *-----------------------------------------------------------------------
355 * SuffSuffIsPrefix --
356 * See if the suffix described by s is a prefix of the string. Care
357 * must be taken when using this to search for transformations and
358 * what-not, since there could well be two suffixes, one of which
359 * is a prefix of the other...
362 * s suffix to compare
363 * str string to examine
366 * 0 if s is a prefix of str. non-zero otherwise
370 *-----------------------------------------------------------------------
373 SuffSuffIsPrefix(const void *s
, const void *str
)
375 return SuffStrIsPrefix(((const Suff
*)s
)->name
, str
) == NULL
;
379 *-----------------------------------------------------------------------
381 * See if the graph node has the desired name
384 * gn current node we're looking at
385 * name name we're looking for
388 * 0 if it does. non-zero if it doesn't
392 *-----------------------------------------------------------------------
395 SuffGNHasNameP(const void *gn
, const void *name
)
397 return (strcmp(name
, ((const GNode
*)gn
)->name
));
400 /*********** Maintenance Functions ************/
403 SuffUnRef(void *lp
, void *sp
)
407 LstNode ln
= Lst_Member(l
, sp
);
410 ((Suff
*)sp
)->refCount
--;
415 *-----------------------------------------------------------------------
417 * Free up all memory associated with the given suffix structure.
423 * the suffix entry is detroyed
424 *-----------------------------------------------------------------------
429 Suff
*s
= (Suff
*)sp
;
438 /* We don't delete suffixes in order, so we cannot use this */
440 Punt("Internal error deleting suffix `%s' with refcount = %d", s
->name
,
444 Lst_Destroy(s
->ref
, NULL
);
445 Lst_Destroy(s
->children
, NULL
);
446 Lst_Destroy(s
->parents
, NULL
);
447 Lst_Destroy(s
->searchPath
, Dir_Destroy
);
454 *-----------------------------------------------------------------------
456 * Remove the suffix into the list
462 * The reference count for the suffix is decremented and the
463 * suffix is possibly freed
464 *-----------------------------------------------------------------------
467 SuffRemove(Lst l
, Suff
*s
)
470 if (s
->refCount
== 0) {
471 SuffUnRef(sufflist
, s
);
477 *-----------------------------------------------------------------------
479 * Insert the suffix into the list keeping the list ordered by suffix
483 * l the list where in s should be inserted
484 * s the suffix to insert
490 * The reference count of the suffix is incremented
491 *-----------------------------------------------------------------------
494 SuffInsert(Lst l
, Suff
*s
)
496 LstNode ln
; /* current element in l we're examining */
497 Suff
*s2
= NULL
; /* the suffix descriptor in this element */
499 if (Lst_Open(l
) == FAILURE
) {
502 while ((ln
= Lst_Next(l
)) != NULL
) {
503 s2
= (Suff
*)Lst_Datum(ln
);
504 if (s2
->sNum
>= s
->sNum
) {
511 fprintf(debug_file
, "inserting %s(%d)...", s
->name
, s
->sNum
);
515 fprintf(debug_file
, "at end of list\n");
517 (void)Lst_AtEnd(l
, s
);
519 (void)Lst_AtEnd(s
->ref
, l
);
520 } else if (s2
->sNum
!= s
->sNum
) {
522 fprintf(debug_file
, "before %s(%d)\n", s2
->name
, s2
->sNum
);
524 (void)Lst_InsertBefore(l
, ln
, s
);
526 (void)Lst_AtEnd(s
->ref
, l
);
527 } else if (DEBUG(SUFF
)) {
528 fprintf(debug_file
, "already there\n");
533 *-----------------------------------------------------------------------
534 * Suff_ClearSuffixes --
535 * This is gross. Nuke the list of suffixes but keep all transformation
536 * rules around. The transformation graph is destroyed in this process,
537 * but we leave the list of rules so when a new graph is formed the rules
539 * This function is called from the parse module when a
540 * .SUFFIXES:\n line is encountered.
546 * the sufflist and its graph nodes are destroyed
547 *-----------------------------------------------------------------------
550 Suff_ClearSuffixes(void)
553 Lst_Concat(suffClean
, sufflist
, LST_CONCLINK
);
555 sufflist
= Lst_Init(FALSE
);
557 suffNull
= emptySuff
;
561 *-----------------------------------------------------------------------
562 * SuffParseTransform --
563 * Parse a transformation string to find its two component suffixes.
566 * str String being parsed
567 * srcPtr Place to store source of trans.
568 * targPtr Place to store target of trans.
571 * TRUE if the string is a valid transformation and FALSE otherwise.
574 * The passed pointers are overwritten.
576 *-----------------------------------------------------------------------
579 SuffParseTransform(char *str
, Suff
**srcPtr
, Suff
**targPtr
)
581 LstNode srcLn
; /* element in suffix list of trans source*/
582 Suff
*src
; /* Source of transformation */
583 LstNode targLn
; /* element in suffix list of trans target*/
584 char *str2
; /* Extra pointer (maybe target suffix) */
585 LstNode singleLn
; /* element in suffix list of any suffix
586 * that exactly matches str */
587 Suff
*single
= NULL
;/* Source of possible transformation to
594 * Loop looking first for a suffix that matches the start of the
595 * string and then for one that exactly matches the rest of it. If
596 * we can find two that meet these criteria, we've successfully
601 srcLn
= Lst_Find(sufflist
, str
, SuffSuffIsPrefix
);
603 srcLn
= Lst_FindFrom(sufflist
, Lst_Succ(srcLn
), str
,
608 * Ran out of source suffixes -- no such rule
610 if (singleLn
!= NULL
) {
612 * Not so fast Mr. Smith! There was a suffix that encompassed
613 * the entire string, so we assume it was a transformation
614 * to the null suffix (thank you POSIX). We still prefer to
615 * find a double rule over a singleton, hence we leave this
616 * check until the end.
618 * XXX: Use emptySuff over suffNull?
626 src
= (Suff
*)Lst_Datum(srcLn
);
627 str2
= str
+ src
->nameLen
;
632 targLn
= Lst_Find(sufflist
, str2
, SuffSuffHasNameP
);
633 if (targLn
!= NULL
) {
635 *targPtr
= (Suff
*)Lst_Datum(targLn
);
643 *-----------------------------------------------------------------------
644 * Suff_IsTransform --
645 * Return TRUE if the given string is a transformation rule
649 * str string to check
652 * TRUE if the string is a concatenation of two known suffixes.
657 *-----------------------------------------------------------------------
660 Suff_IsTransform(char *str
)
664 return (SuffParseTransform(str
, &src
, &targ
));
668 *-----------------------------------------------------------------------
669 * Suff_AddTransform --
670 * Add the transformation rule described by the line to the
671 * list of rules and place the transformation itself in the graph
674 * line name of transformation to add
677 * The node created for the transformation in the transforms list
680 * The node is placed on the end of the transforms Lst and links are
681 * made between the two suffixes mentioned in the target name
682 *-----------------------------------------------------------------------
685 Suff_AddTransform(char *line
)
687 GNode
*gn
; /* GNode of transformation rule */
688 Suff
*s
, /* source suffix */
689 *t
; /* target suffix */
690 LstNode ln
; /* Node for existing transformation */
692 ln
= Lst_Find(transforms
, line
, SuffGNHasNameP
);
695 * Make a new graph node for the transformation. It will be filled in
696 * by the Parse module.
698 gn
= Targ_NewGN(line
);
699 (void)Lst_AtEnd(transforms
, gn
);
702 * New specification for transformation rule. Just nuke the old list
703 * of commands so they can be filled in again... We don't actually
704 * free the commands themselves, because a given command can be
705 * attached to several different transformations.
707 gn
= (GNode
*)Lst_Datum(ln
);
708 Lst_Destroy(gn
->commands
, NULL
);
709 Lst_Destroy(gn
->children
, NULL
);
710 gn
->commands
= Lst_Init(FALSE
);
711 gn
->children
= Lst_Init(FALSE
);
714 gn
->type
= OP_TRANSFORM
;
716 (void)SuffParseTransform(line
, &s
, &t
);
719 * link the two together in the proper relationship and order
722 fprintf(debug_file
, "defining transformation from `%s' to `%s'\n",
725 SuffInsert(t
->children
, s
);
726 SuffInsert(s
->parents
, t
);
732 *-----------------------------------------------------------------------
733 * Suff_EndTransform --
734 * Handle the finish of a transformation definition, removing the
735 * transformation from the graph if it has neither commands nor
736 * sources. This is a callback procedure for the Parse module via
740 * gnp Node for transformation
741 * dummy Node for transformation
747 * If the node has no commands or children, the children and parents
748 * lists of the affected suffixes are altered.
750 *-----------------------------------------------------------------------
753 Suff_EndTransform(void *gnp
, void *dummy
)
755 GNode
*gn
= (GNode
*)gnp
;
757 if ((gn
->type
& OP_DOUBLEDEP
) && !Lst_IsEmpty (gn
->cohorts
))
758 gn
= (GNode
*)Lst_Datum(Lst_Last(gn
->cohorts
));
759 if ((gn
->type
& OP_TRANSFORM
) && Lst_IsEmpty(gn
->commands
) &&
760 Lst_IsEmpty(gn
->children
))
765 * SuffParseTransform() may fail for special rules which are not
766 * actual transformation rules. (e.g. .DEFAULT)
768 if (SuffParseTransform(gn
->name
, &s
, &t
)) {
772 fprintf(debug_file
, "deleting transformation from `%s' to `%s'\n",
777 * Store s->parents because s could be deleted in SuffRemove
782 * Remove the source from the target's children list. We check for a
783 * nil return to handle a beanhead saying something like
786 * We'll be called twice when the next target is seen, but .c and .o
787 * are only linked once...
789 SuffRemove(t
->children
, s
);
792 * Remove the target from the source's parents list
796 } else if ((gn
->type
& OP_TRANSFORM
) && DEBUG(SUFF
)) {
797 fprintf(debug_file
, "transformation %s complete\n", gn
->name
);
800 return(dummy
? 0 : 0);
804 *-----------------------------------------------------------------------
805 * SuffRebuildGraph --
806 * Called from Suff_AddSuffix via Lst_ForEach to search through the
807 * list of existing transformation rules and rebuild the transformation
808 * graph when it has been destroyed by Suff_ClearSuffixes. If the
809 * given rule is a transformation involving this suffix and another,
810 * existing suffix, the proper relationship is established between
814 * transformp Transformation to test
815 * sp Suffix to rebuild
821 * The appropriate links will be made between this suffix and
822 * others if transformation rules exist for it.
824 *-----------------------------------------------------------------------
827 SuffRebuildGraph(void *transformp
, void *sp
)
829 GNode
*transform
= (GNode
*)transformp
;
830 Suff
*s
= (Suff
*)sp
;
837 * First see if it is a transformation from this suffix.
839 cp
= UNCONST(SuffStrIsPrefix(s
->name
, transform
->name
));
841 ln
= Lst_Find(sufflist
, cp
, SuffSuffHasNameP
);
844 * Found target. Link in and return, since it can't be anything
847 s2
= (Suff
*)Lst_Datum(ln
);
848 SuffInsert(s2
->children
, s
);
849 SuffInsert(s
->parents
, s2
);
855 * Not from, maybe to?
857 sd
.len
= strlen(transform
->name
);
858 sd
.ename
= transform
->name
+ sd
.len
;
859 cp
= SuffSuffIsSuffix(s
, &sd
);
862 * Null-terminate the source suffix in order to find it.
865 ln
= Lst_Find(sufflist
, transform
->name
, SuffSuffHasNameP
);
867 * Replace the start of the target suffix
872 * Found it -- establish the proper relationship
874 s2
= (Suff
*)Lst_Datum(ln
);
875 SuffInsert(s
->children
, s2
);
876 SuffInsert(s2
->parents
, s
);
883 *-----------------------------------------------------------------------
885 * Called from Suff_AddSuffix via Lst_ForEach to search through the
886 * list of existing targets and find if any of the existing targets
887 * can be turned into a transformation rule.
890 * 1 if a new main target has been selected, 0 otherwise.
893 * If such a target is found and the target is the current main
894 * target, the main target is set to NULL and the next target
895 * examined (if that exists) becomes the main target.
897 *-----------------------------------------------------------------------
900 SuffScanTargets(void *targetp
, void *gsp
)
902 GNode
*target
= (GNode
*)targetp
;
903 GNodeSuff
*gs
= (GNodeSuff
*)gsp
;
907 if (*gs
->gn
== NULL
&& gs
->r
&& (target
->type
& OP_NOTARGET
) == 0) {
909 Targ_SetMain(target
);
913 if ((unsigned int)target
->type
== OP_TRANSFORM
)
916 if ((ptr
= strstr(target
->name
, gs
->s
->name
)) == NULL
||
920 if (SuffParseTransform(target
->name
, &s
, &t
)) {
921 if (*gs
->gn
== target
) {
926 Lst_Destroy(target
->children
, NULL
);
927 target
->children
= Lst_Init(FALSE
);
928 target
->type
= OP_TRANSFORM
;
930 * link the two together in the proper relationship and order
933 fprintf(debug_file
, "defining transformation from `%s' to `%s'\n",
936 SuffInsert(t
->children
, s
);
937 SuffInsert(s
->parents
, t
);
943 *-----------------------------------------------------------------------
945 * Add the suffix in string to the end of the list of known suffixes.
946 * Should we restructure the suffix graph? Make doesn't...
949 * str the name of the suffix to add
955 * A GNode is created for the suffix and a Suff structure is created and
956 * added to the suffixes list unless the suffix was already known.
957 * The mainNode passed can be modified if a target mutated into a
958 * transform and that target happened to be the main target.
959 *-----------------------------------------------------------------------
962 Suff_AddSuffix(char *str
, GNode
**gn
)
964 Suff
*s
; /* new suffix descriptor */
968 ln
= Lst_Find(sufflist
, str
, SuffSuffHasNameP
);
970 s
= bmake_malloc(sizeof(Suff
));
972 s
->name
= bmake_strdup(str
);
973 s
->nameLen
= strlen(s
->name
);
974 s
->searchPath
= Lst_Init(FALSE
);
975 s
->children
= Lst_Init(FALSE
);
976 s
->parents
= Lst_Init(FALSE
);
977 s
->ref
= Lst_Init(FALSE
);
982 (void)Lst_AtEnd(sufflist
, s
);
984 * We also look at our existing targets list to see if adding
985 * this suffix will make one of our current targets mutate into
986 * a suffix rule. This is ugly, but other makes treat all targets
987 * that start with a . as suffix rules.
992 Lst_ForEach(Targ_List(), SuffScanTargets
, &gs
);
994 * Look for any existing transformations from or to this suffix.
995 * XXX: Only do this after a Suff_ClearSuffixes?
997 Lst_ForEach(transforms
, SuffRebuildGraph
, s
);
1002 *-----------------------------------------------------------------------
1004 * Return the search path for the given suffix, if it's defined.
1007 * The searchPath for the desired suffix or NULL if the suffix isn't
1012 *-----------------------------------------------------------------------
1015 Suff_GetPath(char *sname
)
1020 ln
= Lst_Find(sufflist
, sname
, SuffSuffHasNameP
);
1024 s
= (Suff
*)Lst_Datum(ln
);
1025 return (s
->searchPath
);
1030 *-----------------------------------------------------------------------
1032 * Extend the search paths for all suffixes to include the default
1039 * The searchPath field of all the suffixes is extended by the
1040 * directories in dirSearchPath. If paths were specified for the
1041 * ".h" suffix, the directories are stuffed into a global variable
1042 * called ".INCLUDES" with each directory preceded by a -I. The same
1043 * is done for the ".a" suffix, except the variable is called
1044 * ".LIBS" and the flag is -L.
1045 *-----------------------------------------------------------------------
1053 Lst inIncludes
; /* Cumulative .INCLUDES path */
1054 Lst inLibs
; /* Cumulative .LIBS path */
1056 if (Lst_Open(sufflist
) == FAILURE
) {
1060 inIncludes
= Lst_Init(FALSE
);
1061 inLibs
= Lst_Init(FALSE
);
1063 while ((ln
= Lst_Next(sufflist
)) != NULL
) {
1064 s
= (Suff
*)Lst_Datum(ln
);
1065 if (!Lst_IsEmpty (s
->searchPath
)) {
1067 if (s
->flags
& SUFF_INCLUDE
) {
1068 Dir_Concat(inIncludes
, s
->searchPath
);
1070 #endif /* INCLUDES */
1072 if (s
->flags
& SUFF_LIBRARY
) {
1073 Dir_Concat(inLibs
, s
->searchPath
);
1075 #endif /* LIBRARIES */
1076 Dir_Concat(s
->searchPath
, dirSearchPath
);
1078 Lst_Destroy(s
->searchPath
, Dir_Destroy
);
1079 s
->searchPath
= Lst_Duplicate(dirSearchPath
, Dir_CopyDir
);
1083 Var_Set(".INCLUDES", ptr
= Dir_MakeFlags("-I", inIncludes
), VAR_GLOBAL
, 0);
1085 Var_Set(".LIBS", ptr
= Dir_MakeFlags("-L", inLibs
), VAR_GLOBAL
, 0);
1088 Lst_Destroy(inIncludes
, Dir_Destroy
);
1089 Lst_Destroy(inLibs
, Dir_Destroy
);
1091 Lst_Close(sufflist
);
1095 *-----------------------------------------------------------------------
1096 * Suff_AddInclude --
1097 * Add the given suffix as a type of file which gets included.
1098 * Called from the parse module when a .INCLUDES line is parsed.
1099 * The suffix must have already been defined.
1102 * sname Name of the suffix to mark
1108 * The SUFF_INCLUDE bit is set in the suffix's flags field
1110 *-----------------------------------------------------------------------
1113 Suff_AddInclude(char *sname
)
1118 ln
= Lst_Find(sufflist
, sname
, SuffSuffHasNameP
);
1120 s
= (Suff
*)Lst_Datum(ln
);
1121 s
->flags
|= SUFF_INCLUDE
;
1126 *-----------------------------------------------------------------------
1128 * Add the given suffix as a type of file which is a library.
1129 * Called from the parse module when parsing a .LIBS line. The
1130 * suffix must have been defined via .SUFFIXES before this is
1134 * sname Name of the suffix to mark
1140 * The SUFF_LIBRARY bit is set in the suffix's flags field
1142 *-----------------------------------------------------------------------
1145 Suff_AddLib(char *sname
)
1150 ln
= Lst_Find(sufflist
, sname
, SuffSuffHasNameP
);
1152 s
= (Suff
*)Lst_Datum(ln
);
1153 s
->flags
|= SUFF_LIBRARY
;
1157 /********** Implicit Source Search Functions *********/
1160 *-----------------------------------------------------------------------
1162 * Add a suffix as a Src structure to the given list with its parent
1163 * being the given Src structure. If the suffix is the null suffix,
1164 * the prefix is used unaltered as the file name in the Src structure.
1167 * sp suffix for which to create a Src structure
1168 * lsp list and parent for the new Src
1174 * A Src structure is created and tacked onto the end of the list
1175 *-----------------------------------------------------------------------
1178 SuffAddSrc(void *sp
, void *lsp
)
1180 Suff
*s
= (Suff
*)sp
;
1181 LstSrc
*ls
= (LstSrc
*)lsp
;
1182 Src
*s2
; /* new Src structure */
1183 Src
*targ
; /* Target structure */
1187 if ((s
->flags
& SUFF_NULL
) && (*s
->name
!= '\0')) {
1189 * If the suffix has been marked as the NULL suffix, also create a Src
1190 * structure for a file with no suffix attached. Two birds, and all
1193 s2
= bmake_malloc(sizeof(Src
));
1194 s2
->file
= bmake_strdup(targ
->pref
);
1195 s2
->pref
= targ
->pref
;
1201 targ
->children
+= 1;
1202 (void)Lst_AtEnd(ls
->l
, s2
);
1204 s2
->cp
= Lst_Init(FALSE
);
1205 Lst_AtEnd(targ
->cp
, s2
);
1206 fprintf(debug_file
, "1 add %x %x to %x:", targ
, s2
, ls
->l
);
1207 Lst_ForEach(ls
->l
, PrintAddr
, NULL
);
1208 fprintf(debug_file
, "\n");
1211 s2
= bmake_malloc(sizeof(Src
));
1212 s2
->file
= str_concat(targ
->pref
, s
->name
, 0);
1213 s2
->pref
= targ
->pref
;
1219 targ
->children
+= 1;
1220 (void)Lst_AtEnd(ls
->l
, s2
);
1222 s2
->cp
= Lst_Init(FALSE
);
1223 Lst_AtEnd(targ
->cp
, s2
);
1224 fprintf(debug_file
, "2 add %x %x to %x:", targ
, s2
, ls
->l
);
1225 Lst_ForEach(ls
->l
, PrintAddr
, NULL
);
1226 fprintf(debug_file
, "\n");
1233 *-----------------------------------------------------------------------
1235 * Add all the children of targ as Src structures to the given list
1238 * l list to which to add the new level
1239 * targ Src structure to use as the parent
1245 * Lots of structures are created and added to the list
1246 *-----------------------------------------------------------------------
1249 SuffAddLevel(Lst l
, Src
*targ
)
1256 Lst_ForEach(targ
->suff
->children
, SuffAddSrc
, &ls
);
1260 *----------------------------------------------------------------------
1262 * Free all src structures in list that don't have a reference count
1265 * Ture if an src was removed
1268 * The memory is free'd.
1269 *----------------------------------------------------------------------
1272 SuffRemoveSrc(Lst l
)
1278 if (Lst_Open(l
) == FAILURE
) {
1282 fprintf(debug_file
, "cleaning %lx: ", (unsigned long) l
);
1283 Lst_ForEach(l
, PrintAddr
, NULL
);
1284 fprintf(debug_file
, "\n");
1288 while ((ln
= Lst_Next(l
)) != NULL
) {
1289 s
= (Src
*)Lst_Datum(ln
);
1290 if (s
->children
== 0) {
1296 LstNode ln
= Lst_Member(s
->parent
->cp
, s
);
1298 Lst_Remove(s
->parent
->cp
, ln
);
1300 --s
->parent
->children
;
1303 fprintf(debug_file
, "free: [l=%x] p=%x %d\n", l
, s
, s
->children
);
1304 Lst_Destroy(s
->cp
, NULL
);
1314 fprintf(debug_file
, "keep: [l=%x] p=%x %d: ", l
, s
, s
->children
);
1315 Lst_ForEach(s
->cp
, PrintAddr
, NULL
);
1316 fprintf(debug_file
, "\n");
1327 *-----------------------------------------------------------------------
1329 * Find the first existing file/target in the list srcs
1332 * srcs list of Src structures to search through
1335 * The lowest structure in the chain of transformations
1339 *-----------------------------------------------------------------------
1342 SuffFindThem(Lst srcs
, Lst slst
)
1344 Src
*s
; /* current Src */
1345 Src
*rs
; /* returned Src */
1350 while (!Lst_IsEmpty (srcs
)) {
1351 s
= (Src
*)Lst_DeQueue(srcs
);
1354 fprintf(debug_file
, "\ttrying %s...", s
->file
);
1358 * A file is considered to exist if either a node exists in the
1359 * graph for it or the file actually exists.
1361 if (Targ_FindNode(s
->file
, TARG_NOCREATE
) != NULL
) {
1363 fprintf(debug_file
, "remove %x from %x\n", s
, srcs
);
1369 if ((ptr
= Dir_FindFile(s
->file
, s
->suff
->searchPath
)) != NULL
) {
1372 fprintf(debug_file
, "remove %x from %x\n", s
, srcs
);
1379 fprintf(debug_file
, "not there\n");
1382 SuffAddLevel(srcs
, s
);
1386 if (DEBUG(SUFF
) && rs
) {
1387 fprintf(debug_file
, "got it\n");
1393 *-----------------------------------------------------------------------
1395 * See if any of the children of the target in the Src structure is
1396 * one from which the target can be transformed. If there is one,
1397 * a Src structure is put together for it and returned.
1400 * targ Src structure to play with
1403 * The Src structure of the "winning" child, or NULL if no such beast.
1406 * A Src structure may be allocated.
1408 *-----------------------------------------------------------------------
1411 SuffFindCmds(Src
*targ
, Lst slst
)
1413 LstNode ln
; /* General-purpose list node */
1414 GNode
*t
, /* Target GNode */
1415 *s
; /* Source GNode */
1416 int prefLen
;/* The length of the defined prefix */
1417 Suff
*suff
; /* Suffix on matching beastie */
1418 Src
*ret
; /* Return value */
1422 (void)Lst_Open(t
->children
);
1423 prefLen
= strlen(targ
->pref
);
1426 ln
= Lst_Next(t
->children
);
1428 Lst_Close(t
->children
);
1431 s
= (GNode
*)Lst_Datum(ln
);
1433 if (s
->type
& OP_OPTIONAL
&& Lst_IsEmpty(t
->commands
)) {
1435 * We haven't looked to see if .OPTIONAL files exist yet, so
1436 * don't use one as the implicit source.
1437 * This allows us to use .OPTIONAL in .depend files so make won't
1438 * complain "don't know how to make xxx.h' when a dependant file
1439 * has been moved/deleted.
1444 cp
= strrchr(s
->name
, '/');
1450 if (strncmp(cp
, targ
->pref
, prefLen
) != 0)
1453 * The node matches the prefix ok, see if it has a known
1456 ln
= Lst_Find(sufflist
, &cp
[prefLen
], SuffSuffHasNameP
);
1460 * It even has a known suffix, see if there's a transformation
1461 * defined between the node's suffix and the target's suffix.
1463 * XXX: Handle multi-stage transformations here, too.
1465 suff
= (Suff
*)Lst_Datum(ln
);
1467 if (Lst_Member(suff
->parents
, targ
->suff
) != NULL
)
1472 * Hot Damn! Create a new Src structure to describe
1473 * this transformation (making sure to duplicate the
1474 * source node's name so Suff_FindDeps can free it
1475 * again (ick)), and return the new structure.
1477 ret
= bmake_malloc(sizeof(Src
));
1478 ret
->file
= bmake_strdup(s
->name
);
1479 ret
->pref
= targ
->pref
;
1485 targ
->children
+= 1;
1487 ret
->cp
= Lst_Init(FALSE
);
1488 fprintf(debug_file
, "3 add %x %x\n", targ
, ret
);
1489 Lst_AtEnd(targ
->cp
, ret
);
1491 Lst_AtEnd(slst
, ret
);
1493 fprintf(debug_file
, "\tusing existing source %s\n", s
->name
);
1499 *-----------------------------------------------------------------------
1500 * SuffExpandChildren --
1501 * Expand the names of any children of a given node that contain
1502 * variable invocations or file wildcards into actual targets.
1505 * cln Child to examine
1506 * pgn Parent node being processed
1512 * The expanded node is removed from the parent's list of children,
1513 * and the parent's unmade counter is decremented, but other nodes
1516 *-----------------------------------------------------------------------
1519 SuffExpandChildren(LstNode cln
, GNode
*pgn
)
1521 GNode
*cgn
= (GNode
*)Lst_Datum(cln
);
1522 GNode
*gn
; /* New source 8) */
1523 char *cp
; /* Expanded value */
1525 if (!Lst_IsEmpty(cgn
->order_pred
) || !Lst_IsEmpty(cgn
->order_succ
))
1526 /* It is all too hard to process the result of .ORDER */
1529 if (cgn
->type
& OP_WAIT
)
1530 /* Ignore these (& OP_PHONY ?) */
1534 * First do variable expansion -- this takes precedence over
1535 * wildcard expansion. If the result contains wildcards, they'll be gotten
1536 * to later since the resulting words are tacked on to the end of
1537 * the children list.
1539 if (strchr(cgn
->name
, '$') == NULL
) {
1540 SuffExpandWildcards(cln
, pgn
);
1545 fprintf(debug_file
, "Expanding \"%s\"...", cgn
->name
);
1547 cp
= Var_Subst(NULL
, cgn
->name
, pgn
, TRUE
);
1550 Lst members
= Lst_Init(FALSE
);
1552 if (cgn
->type
& OP_ARCHV
) {
1554 * Node was an archive(member) target, so we want to call
1555 * on the Arch module to find the nodes for us, expanding
1556 * variables in the parent's context.
1558 char *sacrifice
= cp
;
1560 (void)Arch_ParseArchive(&sacrifice
, members
, pgn
);
1563 * Break the result into a vector of strings whose nodes
1564 * we can find, then add those nodes to the members list.
1565 * Unfortunately, we can't use brk_string b/c it
1566 * doesn't understand about variable specifications with
1570 char *initcp
= cp
; /* For freeing... */
1572 for (start
= cp
; *start
== ' ' || *start
== '\t'; start
++)
1574 for (cp
= start
; *cp
!= '\0'; cp
++) {
1575 if (*cp
== ' ' || *cp
== '\t') {
1577 * White-space -- terminate element, find the node,
1578 * add it, skip any further spaces.
1581 gn
= Targ_FindNode(start
, TARG_CREATE
);
1582 (void)Lst_AtEnd(members
, gn
);
1583 while (*cp
== ' ' || *cp
== '\t') {
1587 * Adjust cp for increment at start of loop, but
1588 * set start to first non-space.
1591 } else if (*cp
== '$') {
1593 * Start of a variable spec -- contact variable module
1594 * to find the end so we can skip over it.
1600 junk
= Var_Parse(cp
, pgn
, TRUE
, &len
, &freeIt
);
1601 if (junk
!= var_Error
) {
1607 } else if (*cp
== '\\' && *cp
!= '\0') {
1609 * Escaped something -- skip over it
1617 * Stuff left over -- add it to the list too
1619 gn
= Targ_FindNode(start
, TARG_CREATE
);
1620 (void)Lst_AtEnd(members
, gn
);
1623 * Point cp back at the beginning again so the variable value
1630 * Add all elements of the members list to the parent node.
1632 while(!Lst_IsEmpty(members
)) {
1633 gn
= (GNode
*)Lst_DeQueue(members
);
1636 fprintf(debug_file
, "%s...", gn
->name
);
1638 /* Add gn to the parents child list before the original child */
1639 (void)Lst_InsertBefore(pgn
->children
, cln
, gn
);
1640 (void)Lst_AtEnd(gn
->parents
, pgn
);
1642 /* Expand wildcards on new node */
1643 SuffExpandWildcards(Lst_Prev(cln
), pgn
);
1645 Lst_Destroy(members
, NULL
);
1653 fprintf(debug_file
, "\n");
1657 * Now the source is expanded, remove it from the list of children to
1658 * keep it from being processed.
1661 Lst_Remove(pgn
->children
, cln
);
1662 Lst_Remove(cgn
->parents
, Lst_Member(cgn
->parents
, pgn
));
1666 SuffExpandWildcards(LstNode cln
, GNode
*pgn
)
1668 GNode
*cgn
= (GNode
*)Lst_Datum(cln
);
1669 GNode
*gn
; /* New source 8) */
1670 char *cp
; /* Expanded value */
1671 Lst explist
; /* List of expansions */
1673 if (!Dir_HasWildcards(cgn
->name
))
1677 * Expand the word along the chosen path
1679 explist
= Lst_Init(FALSE
);
1680 Dir_Expand(cgn
->name
, Suff_FindPath(cgn
), explist
);
1682 while (!Lst_IsEmpty(explist
)) {
1684 * Fetch next expansion off the list and find its GNode
1686 cp
= (char *)Lst_DeQueue(explist
);
1689 fprintf(debug_file
, "%s...", cp
);
1691 gn
= Targ_FindNode(cp
, TARG_CREATE
);
1693 /* Add gn to the parents child list before the original child */
1694 (void)Lst_InsertBefore(pgn
->children
, cln
, gn
);
1695 (void)Lst_AtEnd(gn
->parents
, pgn
);
1700 * Nuke what's left of the list
1702 Lst_Destroy(explist
, NULL
);
1705 fprintf(debug_file
, "\n");
1709 * Now the source is expanded, remove it from the list of children to
1710 * keep it from being processed.
1713 Lst_Remove(pgn
->children
, cln
);
1714 Lst_Remove(cgn
->parents
, Lst_Member(cgn
->parents
, pgn
));
1718 *-----------------------------------------------------------------------
1720 * Find a path along which to expand the node.
1722 * If the word has a known suffix, use that path.
1723 * If it has no known suffix, use the default system search path.
1726 * gn Node being examined
1729 * The appropriate path to search for the GNode.
1732 * XXX: We could set the suffix here so that we don't have to scan
1735 *-----------------------------------------------------------------------
1738 Suff_FindPath(GNode
* gn
)
1740 Suff
*suff
= gn
->suffix
;
1743 SuffixCmpData sd
; /* Search string data */
1745 sd
.len
= strlen(gn
->name
);
1746 sd
.ename
= gn
->name
+ sd
.len
;
1747 ln
= Lst_Find(sufflist
, &sd
, SuffSuffIsSuffixP
);
1750 fprintf(debug_file
, "Wildcard expanding \"%s\"...", gn
->name
);
1753 suff
= (Suff
*)Lst_Datum(ln
);
1754 /* XXX: Here we can save the suffix so we don't have to do this again */
1759 fprintf(debug_file
, "suffix is \"%s\"...", suff
->name
);
1761 return suff
->searchPath
;
1764 * Use default search path
1766 return dirSearchPath
;
1771 *-----------------------------------------------------------------------
1772 * SuffApplyTransform --
1773 * Apply a transformation rule, given the source and target nodes
1783 * TRUE if successful, FALSE if not.
1786 * The source and target are linked and the commands from the
1787 * transformation are added to the target node's commands list.
1788 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1789 * to the target. The target also inherits all the sources for
1790 * the transformation rule.
1792 *-----------------------------------------------------------------------
1795 SuffApplyTransform(GNode
*tGn
, GNode
*sGn
, Suff
*t
, Suff
*s
)
1797 LstNode ln
, nln
; /* General node */
1798 char *tname
; /* Name of transformation rule */
1799 GNode
*gn
; /* Node for same */
1802 * Form the proper links between the target and source.
1804 (void)Lst_AtEnd(tGn
->children
, sGn
);
1805 (void)Lst_AtEnd(sGn
->parents
, tGn
);
1809 * Locate the transformation rule itself
1811 tname
= str_concat(s
->name
, t
->name
, 0);
1812 ln
= Lst_Find(transforms
, tname
, SuffGNHasNameP
);
1817 * Not really such a transformation rule (can happen when we're
1818 * called to link an OP_MEMBER and OP_ARCHV node), so return
1824 gn
= (GNode
*)Lst_Datum(ln
);
1827 fprintf(debug_file
, "\tapplying %s -> %s to \"%s\"\n", s
->name
, t
->name
, tGn
->name
);
1831 * Record last child for expansion purposes
1833 ln
= Lst_Last(tGn
->children
);
1836 * Pass the buck to Make_HandleUse to apply the rule
1838 (void)Make_HandleUse(gn
, tGn
);
1841 * Deal with wildcards and variables in any acquired sources
1843 for (ln
= Lst_Succ(ln
); ln
!= NULL
; ln
= nln
) {
1845 SuffExpandChildren(ln
, tGn
);
1849 * Keep track of another parent to which this beast is transformed so
1850 * the .IMPSRC variable can be set correctly for the parent.
1852 (void)Lst_AtEnd(sGn
->iParents
, tGn
);
1859 *-----------------------------------------------------------------------
1860 * SuffFindArchiveDeps --
1861 * Locate dependencies for an OP_ARCHV node.
1864 * gn Node for which to locate dependencies
1870 * Same as Suff_FindDeps
1872 *-----------------------------------------------------------------------
1875 SuffFindArchiveDeps(GNode
*gn
, Lst slst
)
1877 char *eoarch
; /* End of archive portion */
1878 char *eoname
; /* End of member portion */
1879 GNode
*mem
; /* Node for member */
1880 static const char *copy
[] = {
1881 /* Variables to be copied from the member node */
1882 TARGET
, /* Must be first */
1883 PREFIX
, /* Must be second */
1885 int i
; /* Index into copy and vals */
1886 Suff
*ms
; /* Suffix descriptor for member */
1887 char *name
; /* Start of member's name */
1890 * The node is an archive(member) pair. so we must find a
1891 * suffix for both of them.
1893 eoarch
= strchr(gn
->name
, '(');
1894 eoname
= strchr(eoarch
, ')');
1896 *eoname
= '\0'; /* Nuke parentheses during suffix search */
1897 *eoarch
= '\0'; /* So a suffix can be found */
1902 * To simplify things, call Suff_FindDeps recursively on the member now,
1903 * so we can simply compare the member's .PREFIX and .TARGET variables
1904 * to locate its suffix. This allows us to figure out the suffix to
1905 * use for the archive without having to do a quadratic search over the
1906 * suffix list, backtracking for each one...
1908 mem
= Targ_FindNode(name
, TARG_CREATE
);
1909 SuffFindDeps(mem
, slst
);
1912 * Create the link between the two nodes right off
1914 (void)Lst_AtEnd(gn
->children
, mem
);
1915 (void)Lst_AtEnd(mem
->parents
, gn
);
1919 * Copy in the variables from the member node to this one.
1921 for (i
= (sizeof(copy
)/sizeof(copy
[0]))-1; i
>= 0; i
--) {
1923 Var_Set(copy
[i
], Var_Value(copy
[i
], mem
, &p1
), gn
, 0);
1932 * Didn't know what it was -- use .NULL suffix if not in make mode
1935 fprintf(debug_file
, "using null suffix\n");
1942 * Set the other two local variables required for this target.
1944 Var_Set(MEMBER
, name
, gn
, 0);
1945 Var_Set(ARCHIVE
, gn
->name
, gn
, 0);
1949 * Member has a known suffix, so look for a transformation rule from
1950 * it to a possible suffix of the archive. Rather than searching
1951 * through the entire list, we just look at suffixes to which the
1952 * member's suffix may be transformed...
1955 SuffixCmpData sd
; /* Search string data */
1958 * Use first matching suffix...
1960 sd
.len
= eoarch
- gn
->name
;
1962 ln
= Lst_Find(ms
->parents
, &sd
, SuffSuffIsSuffixP
);
1966 * Got one -- apply it
1968 if (!SuffApplyTransform(gn
, mem
, (Suff
*)Lst_Datum(ln
), ms
) &&
1971 fprintf(debug_file
, "\tNo transformation from %s -> %s\n",
1972 ms
->name
, ((Suff
*)Lst_Datum(ln
))->name
);
1978 * Replace the opening and closing parens now we've no need of the separate
1981 *eoarch
= '('; *eoname
= ')';
1984 * Pretend gn appeared to the left of a dependency operator so
1985 * the user needn't provide a transformation from the member to the
1988 if (OP_NOP(gn
->type
)) {
1989 gn
->type
|= OP_DEPENDS
;
1993 * Flag the member as such so we remember to look in the archive for
1994 * its modification time.
1996 mem
->type
|= OP_MEMBER
;
2000 *-----------------------------------------------------------------------
2001 * SuffFindNormalDeps --
2002 * Locate implicit dependencies for regular targets.
2005 * gn Node for which to find sources
2011 * Same as Suff_FindDeps...
2013 *-----------------------------------------------------------------------
2016 SuffFindNormalDeps(GNode
*gn
, Lst slst
)
2018 char *eoname
; /* End of name */
2019 char *sopref
; /* Start of prefix */
2020 LstNode ln
, nln
; /* Next suffix node to check */
2021 Lst srcs
; /* List of sources at which to look */
2022 Lst targs
; /* List of targets to which things can be
2023 * transformed. They all have the same file,
2024 * but different suff and pref fields */
2025 Src
*bottom
; /* Start of found transformation path */
2026 Src
*src
; /* General Src pointer */
2027 char *pref
; /* Prefix to use */
2028 Src
*targ
; /* General Src target pointer */
2029 SuffixCmpData sd
; /* Search string data */
2032 sd
.len
= strlen(gn
->name
);
2033 sd
.ename
= eoname
= gn
->name
+ sd
.len
;
2038 * Begin at the beginning...
2040 ln
= Lst_First(sufflist
);
2041 srcs
= Lst_Init(FALSE
);
2042 targs
= Lst_Init(FALSE
);
2045 * We're caught in a catch-22 here. On the one hand, we want to use any
2046 * transformation implied by the target's sources, but we can't examine
2047 * the sources until we've expanded any variables/wildcards they may hold,
2048 * and we can't do that until we've set up the target's local variables
2049 * and we can't do that until we know what the proper suffix for the
2050 * target is (in case there are two suffixes one of which is a suffix of
2051 * the other) and we can't know that until we've found its implied
2052 * source, which we may not want to use if there's an existing source
2053 * that implies a different transformation.
2055 * In an attempt to get around this, which may not work all the time,
2056 * but should work most of the time, we look for implied sources first,
2057 * checking transformations to all possible suffixes of the target,
2058 * use what we find to set the target's local variables, expand the
2059 * children, then look for any overriding transformations they imply.
2060 * Should we find one, we discard the one we found before.
2063 while (ln
!= NULL
) {
2065 * Look for next possible suffix...
2067 ln
= Lst_FindFrom(sufflist
, ln
, &sd
, SuffSuffIsSuffixP
);
2070 int prefLen
; /* Length of the prefix */
2073 * Allocate a Src structure to which things can be transformed
2075 targ
= bmake_malloc(sizeof(Src
));
2076 targ
->file
= bmake_strdup(gn
->name
);
2077 targ
->suff
= (Suff
*)Lst_Datum(ln
);
2078 targ
->suff
->refCount
++;
2080 targ
->parent
= NULL
;
2083 targ
->cp
= Lst_Init(FALSE
);
2087 * Allocate room for the prefix, whose end is found by subtracting
2088 * the length of the suffix from the end of the name.
2090 prefLen
= (eoname
- targ
->suff
->nameLen
) - sopref
;
2091 targ
->pref
= bmake_malloc(prefLen
+ 1);
2092 memcpy(targ
->pref
, sopref
, prefLen
);
2093 targ
->pref
[prefLen
] = '\0';
2096 * Add nodes from which the target can be made
2098 SuffAddLevel(srcs
, targ
);
2101 * Record the target so we can nuke it
2103 (void)Lst_AtEnd(targs
, targ
);
2106 * Search from this suffix's successor...
2113 * Handle target of unknown suffix...
2115 if (Lst_IsEmpty(targs
) && suffNull
!= NULL
) {
2117 fprintf(debug_file
, "\tNo known suffix on %s. Using .NULL suffix\n", gn
->name
);
2120 targ
= bmake_malloc(sizeof(Src
));
2121 targ
->file
= bmake_strdup(gn
->name
);
2122 targ
->suff
= suffNull
;
2123 targ
->suff
->refCount
++;
2125 targ
->parent
= NULL
;
2127 targ
->pref
= bmake_strdup(sopref
);
2129 targ
->cp
= Lst_Init(FALSE
);
2133 * Only use the default suffix rules if we don't have commands
2134 * defined for this gnode; traditional make programs used to
2135 * not define suffix rules if the gnode had children but we
2136 * don't do this anymore.
2138 if (Lst_IsEmpty(gn
->commands
))
2139 SuffAddLevel(srcs
, targ
);
2142 fprintf(debug_file
, "not ");
2146 fprintf(debug_file
, "adding suffix rules\n");
2148 (void)Lst_AtEnd(targs
, targ
);
2152 * Using the list of possible sources built up from the target suffix(es),
2153 * try and find an existing file/target that matches.
2155 bottom
= SuffFindThem(srcs
, slst
);
2157 if (bottom
== NULL
) {
2159 * No known transformations -- use the first suffix found for setting
2160 * the local variables.
2162 if (!Lst_IsEmpty(targs
)) {
2163 targ
= (Src
*)Lst_Datum(Lst_First(targs
));
2169 * Work up the transformation path to find the suffix of the
2170 * target to which the transformation was made.
2172 for (targ
= bottom
; targ
->parent
!= NULL
; targ
= targ
->parent
)
2176 Var_Set(TARGET
, gn
->path
? gn
->path
: gn
->name
, gn
, 0);
2178 pref
= (targ
!= NULL
) ? targ
->pref
: gn
->name
;
2179 Var_Set(PREFIX
, pref
, gn
, 0);
2182 * Now we've got the important local variables set, expand any sources
2183 * that still contain variables or wildcards in their names.
2185 for (ln
= Lst_First(gn
->children
); ln
!= NULL
; ln
= nln
) {
2187 SuffExpandChildren(ln
, gn
);
2192 fprintf(debug_file
, "\tNo valid suffix on %s\n", gn
->name
);
2197 * Deal with finding the thing on the default search path. We
2198 * always do that, not only if the node is only a source (not
2199 * on the lhs of a dependency operator or [XXX] it has neither
2200 * children or commands) as the old pmake did.
2202 if ((gn
->type
& (OP_PHONY
|OP_NOPATH
)) == 0) {
2204 gn
->path
= Dir_FindFile(gn
->name
,
2205 (targ
== NULL
? dirSearchPath
:
2206 targ
->suff
->searchPath
));
2207 if (gn
->path
!= NULL
) {
2209 Var_Set(TARGET
, gn
->path
, gn
, 0);
2213 * Suffix known for the thing -- trim the suffix off
2214 * the path to form the proper .PREFIX variable.
2216 int savep
= strlen(gn
->path
) - targ
->suff
->nameLen
;
2220 gn
->suffix
->refCount
--;
2221 gn
->suffix
= targ
->suff
;
2222 gn
->suffix
->refCount
++;
2224 savec
= gn
->path
[savep
];
2225 gn
->path
[savep
] = '\0';
2227 if ((ptr
= strrchr(gn
->path
, '/')) != NULL
)
2232 Var_Set(PREFIX
, ptr
, gn
, 0);
2234 gn
->path
[savep
] = savec
;
2237 * The .PREFIX gets the full path if the target has
2241 gn
->suffix
->refCount
--;
2244 if ((ptr
= strrchr(gn
->path
, '/')) != NULL
)
2249 Var_Set(PREFIX
, ptr
, gn
, 0);
2258 * If the suffix indicates that the target is a library, mark that in
2259 * the node's type field.
2261 if (targ
->suff
->flags
& SUFF_LIBRARY
) {
2266 * Check for overriding transformation rule implied by sources
2268 if (!Lst_IsEmpty(gn
->children
)) {
2269 src
= SuffFindCmds(targ
, slst
);
2273 * Free up all the Src structures in the transformation path
2274 * up to, but not including, the parent node.
2276 while (bottom
&& bottom
->parent
!= NULL
) {
2277 if (Lst_Member(slst
, bottom
) == NULL
) {
2278 Lst_AtEnd(slst
, bottom
);
2280 bottom
= bottom
->parent
;
2286 if (bottom
== NULL
) {
2288 * No idea from where it can come -- return now.
2294 * We now have a list of Src structures headed by 'bottom' and linked via
2295 * their 'parent' pointers. What we do next is create links between
2296 * source and target nodes (which may or may not have been created)
2297 * and set the necessary local variables in each target. The
2298 * commands for each target are set from the commands of the
2299 * transformation rule used to get from the src suffix to the targ
2300 * suffix. Note that this causes the commands list of the original
2301 * node, gn, to be replaced by the commands of the final
2302 * transformation rule. Also, the unmade field of gn is incremented.
2305 if (bottom
->node
== NULL
) {
2306 bottom
->node
= Targ_FindNode(bottom
->file
, TARG_CREATE
);
2309 for (src
= bottom
; src
->parent
!= NULL
; src
= src
->parent
) {
2312 if (src
->node
->suffix
)
2313 src
->node
->suffix
->refCount
--;
2314 src
->node
->suffix
= src
->suff
;
2315 src
->node
->suffix
->refCount
++;
2317 if (targ
->node
== NULL
) {
2318 targ
->node
= Targ_FindNode(targ
->file
, TARG_CREATE
);
2321 SuffApplyTransform(targ
->node
, src
->node
,
2322 targ
->suff
, src
->suff
);
2324 if (targ
->node
!= gn
) {
2326 * Finish off the dependency-search process for any nodes
2327 * between bottom and gn (no point in questing around the
2328 * filesystem for their implicit source when it's already
2329 * known). Note that the node can't have any sources that
2330 * need expanding, since SuffFindThem will stop on an existing
2331 * node, so all we need to do is set the standard and System V
2334 targ
->node
->type
|= OP_DEPS_FOUND
;
2336 Var_Set(PREFIX
, targ
->pref
, targ
->node
, 0);
2338 Var_Set(TARGET
, targ
->node
->name
, targ
->node
, 0);
2343 gn
->suffix
->refCount
--;
2344 gn
->suffix
= src
->suff
;
2345 gn
->suffix
->refCount
++;
2348 * Nuke the transformation path and the Src structures left over in the
2353 if (Lst_Member(slst
, bottom
) == NULL
)
2354 Lst_AtEnd(slst
, bottom
);
2356 while (SuffRemoveSrc(srcs
) || SuffRemoveSrc(targs
))
2359 Lst_Concat(slst
, srcs
, LST_CONCLINK
);
2360 Lst_Concat(slst
, targs
, LST_CONCLINK
);
2365 *-----------------------------------------------------------------------
2367 * Find implicit sources for the target described by the graph node
2374 * Nodes are added to the graph below the passed-in node. The nodes
2375 * are marked to have their IMPSRC variable filled in. The
2376 * PREFIX variable is set for the given node and all its
2380 * The path found by this target is the shortest path in the
2381 * transformation graph, which may pass through non-existent targets,
2382 * to an existing target. The search continues on all paths from the
2383 * root suffix until a file is found. I.e. if there's a path
2384 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2385 * the .c and .l files don't, the search will branch out in
2386 * all directions from .o and again from all the nodes on the
2387 * next level until the .l,v node is encountered.
2389 *-----------------------------------------------------------------------
2393 Suff_FindDeps(GNode
*gn
)
2396 SuffFindDeps(gn
, srclist
);
2397 while (SuffRemoveSrc(srclist
))
2404 * gn node we're dealing with
2408 SuffFindDeps(GNode
*gn
, Lst slst
)
2410 if (gn
->type
& (OP_DEPS_FOUND
|OP_PHONY
)) {
2412 * If dependencies already found, no need to do it again...
2413 * If this is a .PHONY target, we do not apply suffix rules.
2417 gn
->type
|= OP_DEPS_FOUND
;
2421 fprintf(debug_file
, "SuffFindDeps (%s)\n", gn
->name
);
2424 if (gn
->type
& OP_ARCHV
) {
2425 SuffFindArchiveDeps(gn
, slst
);
2426 } else if (gn
->type
& OP_LIB
) {
2428 * If the node is a library, it is the arch module's job to find it
2429 * and set the TARGET variable accordingly. We merely provide the
2430 * search path, assuming all libraries end in ".a" (if the suffix
2431 * hasn't been defined, there's nothing we can do for it, so we just
2432 * set the TARGET variable to the node's name in order to give it a
2438 ln
= Lst_Find(sufflist
, LIBSUFF
, SuffSuffHasNameP
);
2440 gn
->suffix
->refCount
--;
2442 gn
->suffix
= s
= (Suff
*)Lst_Datum(ln
);
2443 gn
->suffix
->refCount
++;
2444 Arch_FindLib(gn
, s
->searchPath
);
2447 Var_Set(TARGET
, gn
->name
, gn
, 0);
2450 * Because a library (-lfoo) target doesn't follow the standard
2451 * filesystem conventions, we don't set the regular variables for
2452 * the thing. .PREFIX is simply made empty...
2454 Var_Set(PREFIX
, "", gn
, 0);
2456 SuffFindNormalDeps(gn
, slst
);
2461 *-----------------------------------------------------------------------
2463 * Define which suffix is the null suffix.
2466 * name Name of null suffix
2472 * 'suffNull' is altered.
2475 * Need to handle the changing of the null suffix gracefully so the
2476 * old transformation rules don't just go away.
2478 *-----------------------------------------------------------------------
2481 Suff_SetNull(char *name
)
2486 ln
= Lst_Find(sufflist
, name
, SuffSuffHasNameP
);
2488 s
= (Suff
*)Lst_Datum(ln
);
2489 if (suffNull
!= NULL
) {
2490 suffNull
->flags
&= ~SUFF_NULL
;
2492 s
->flags
|= SUFF_NULL
;
2494 * XXX: Here's where the transformation mangling would take place
2498 Parse_Error(PARSE_WARNING
, "Desired null suffix %s not defined.",
2504 *-----------------------------------------------------------------------
2506 * Initialize suffixes module
2513 *-----------------------------------------------------------------------
2518 sufflist
= Lst_Init(FALSE
);
2520 suffClean
= Lst_Init(FALSE
);
2522 srclist
= Lst_Init(FALSE
);
2523 transforms
= Lst_Init(FALSE
);
2527 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2528 * actually go on the suffix list or everyone will think that's its
2531 emptySuff
= suffNull
= bmake_malloc(sizeof(Suff
));
2533 suffNull
->name
= bmake_strdup("");
2534 suffNull
->nameLen
= 0;
2535 suffNull
->searchPath
= Lst_Init(FALSE
);
2536 Dir_Concat(suffNull
->searchPath
, dirSearchPath
);
2537 suffNull
->children
= Lst_Init(FALSE
);
2538 suffNull
->parents
= Lst_Init(FALSE
);
2539 suffNull
->ref
= Lst_Init(FALSE
);
2540 suffNull
->sNum
= sNum
++;
2541 suffNull
->flags
= SUFF_NULL
;
2542 suffNull
->refCount
= 1;
2548 *----------------------------------------------------------------------
2550 * Cleanup the this module
2556 * The memory is free'd.
2557 *----------------------------------------------------------------------
2564 Lst_Destroy(sufflist
, SuffFree
);
2565 Lst_Destroy(suffClean
, SuffFree
);
2568 Lst_Destroy(srclist
, NULL
);
2569 Lst_Destroy(transforms
, NULL
);
2574 /********************* DEBUGGING FUNCTIONS **********************/
2576 static int SuffPrintName(void *s
, void *dummy
)
2578 fprintf(debug_file
, "%s ", ((Suff
*)s
)->name
);
2579 return (dummy
? 0 : 0);
2583 SuffPrintSuff(void *sp
, void *dummy
)
2585 Suff
*s
= (Suff
*)sp
;
2589 fprintf(debug_file
, "# `%s' [%d] ", s
->name
, s
->refCount
);
2593 fputs(" (", debug_file
);
2595 flag
= 1 << (ffs(flags
) - 1);
2599 fprintf(debug_file
, "NULL");
2602 fprintf(debug_file
, "INCLUDE");
2605 fprintf(debug_file
, "LIBRARY");
2608 fputc(flags
? '|' : ')', debug_file
);
2611 fputc('\n', debug_file
);
2612 fprintf(debug_file
, "#\tTo: ");
2613 Lst_ForEach(s
->parents
, SuffPrintName
, NULL
);
2614 fputc('\n', debug_file
);
2615 fprintf(debug_file
, "#\tFrom: ");
2616 Lst_ForEach(s
->children
, SuffPrintName
, NULL
);
2617 fputc('\n', debug_file
);
2618 fprintf(debug_file
, "#\tSearch Path: ");
2619 Dir_PrintPath(s
->searchPath
);
2620 fputc('\n', debug_file
);
2621 return (dummy
? 0 : 0);
2625 SuffPrintTrans(void *tp
, void *dummy
)
2627 GNode
*t
= (GNode
*)tp
;
2629 fprintf(debug_file
, "%-16s: ", t
->name
);
2630 Targ_PrintType(t
->type
);
2631 fputc('\n', debug_file
);
2632 Lst_ForEach(t
->commands
, Targ_PrintCmd
, NULL
);
2633 fputc('\n', debug_file
);
2634 return(dummy
? 0 : 0);
2640 fprintf(debug_file
, "#*** Suffixes:\n");
2641 Lst_ForEach(sufflist
, SuffPrintSuff
, NULL
);
2643 fprintf(debug_file
, "#*** Transformations:\n");
2644 Lst_ForEach(transforms
, SuffPrintTrans
, NULL
);