1 /* $NetBSD: suff.c,v 1.69 2011/09/29 23:38:04 sjg 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.69 2011/09/29 23:38:04 sjg 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.69 2011/09/29 23:38:04 sjg 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.
143 static Lst sufflist
; /* Lst of suffixes */
145 static Lst suffClean
; /* Lst of suffixes to be cleaned */
147 static Lst srclist
; /* Lst of sources */
148 static Lst transforms
; /* Lst of transformation rules */
150 static int sNum
= 0; /* Counter for assigning suffix numbers */
153 * Structure describing an individual suffix.
155 typedef struct _Suff
{
156 char *name
; /* The suffix itself */
157 int nameLen
; /* Length of the suffix */
158 short flags
; /* Type of suffix */
159 #define SUFF_INCLUDE 0x01 /* One which is #include'd */
160 #define SUFF_LIBRARY 0x02 /* One which contains a library */
161 #define SUFF_NULL 0x04 /* The empty suffix */
162 Lst searchPath
; /* The path along which files of this suffix
164 int sNum
; /* The suffix number */
165 int refCount
; /* Reference count of list membership */
166 Lst parents
; /* Suffixes we have a transformation to */
167 Lst children
; /* Suffixes we have a transformation from */
168 Lst ref
; /* List of lists this suffix is referenced */
172 * for SuffSuffIsSuffix
175 char *ename
; /* The end of the name */
176 int len
; /* Length of the name */
180 * Structure used in the search for implied sources.
182 typedef struct _Src
{
183 char *file
; /* The file to look for */
184 char *pref
; /* Prefix from which file was formed */
185 Suff
*suff
; /* The suffix on the file */
186 struct _Src
*parent
; /* The Src for which this is a source */
187 GNode
*node
; /* The node describing the file */
188 int children
; /* Count of existing children (so we don't free
189 * this thing too early or never nuke it) */
191 Lst cp
; /* Debug; children list */
196 * A structure for passing more than one argument to the Lst-library-invoked
210 static Suff
*suffNull
; /* The NULL suffix for this run */
211 static Suff
*emptySuff
; /* The empty suffix required for POSIX
212 * single-suffix transformation rules */
215 static const char *SuffStrIsPrefix(const char *, const char *);
216 static char *SuffSuffIsSuffix(const Suff
*, const SuffixCmpData
*);
217 static int SuffSuffIsSuffixP(const void *, const void *);
218 static int SuffSuffHasNameP(const void *, const void *);
219 static int SuffSuffIsPrefix(const void *, const void *);
220 static int SuffGNHasNameP(const void *, const void *);
221 static void SuffUnRef(void *, void *);
222 static void SuffFree(void *);
223 static void SuffInsert(Lst
, Suff
*);
224 static void SuffRemove(Lst
, Suff
*);
225 static Boolean
SuffParseTransform(char *, Suff
**, Suff
**);
226 static int SuffRebuildGraph(void *, void *);
227 static int SuffScanTargets(void *, void *);
228 static int SuffAddSrc(void *, void *);
229 static int SuffRemoveSrc(Lst
);
230 static void SuffAddLevel(Lst
, Src
*);
231 static Src
*SuffFindThem(Lst
, Lst
);
232 static Src
*SuffFindCmds(Src
*, Lst
);
233 static void SuffExpandChildren(LstNode
, GNode
*);
234 static void SuffExpandWildcards(LstNode
, GNode
*);
235 static Boolean
SuffApplyTransform(GNode
*, GNode
*, Suff
*, Suff
*);
236 static void SuffFindDeps(GNode
*, Lst
);
237 static void SuffFindArchiveDeps(GNode
*, Lst
);
238 static void SuffFindNormalDeps(GNode
*, Lst
);
239 static int SuffPrintName(void *, void *);
240 static int SuffPrintSuff(void *, void *);
241 static int SuffPrintTrans(void *, void *);
243 /*************** Lst Predicates ****************/
245 *-----------------------------------------------------------------------
247 * See if pref is a prefix of str.
250 * pref possible prefix
251 * str string to check
254 * NULL if it ain't, pointer to character in str after prefix if so
258 *-----------------------------------------------------------------------
261 SuffStrIsPrefix(const char *pref
, const char *str
)
263 while (*str
&& *pref
== *str
) {
268 return (*pref
? NULL
: str
);
272 *-----------------------------------------------------------------------
273 * SuffSuffIsSuffix --
274 * See if suff is a suffix of str. sd->ename should point to THE END
275 * of the string to check. (THE END == the null byte)
279 * sd string to examine
282 * NULL if it ain't, pointer to character in str before suffix if
287 *-----------------------------------------------------------------------
290 SuffSuffIsSuffix(const Suff
*s
, const SuffixCmpData
*sd
)
292 char *p1
; /* Pointer into suffix name */
293 char *p2
; /* Pointer into string being examined */
295 if (sd
->len
< s
->nameLen
)
296 return NULL
; /* this string is shorter than the suffix */
298 p1
= s
->name
+ s
->nameLen
;
301 while (p1
>= s
->name
&& *p1
== *p2
) {
306 return (p1
== s
->name
- 1 ? p2
: NULL
);
310 *-----------------------------------------------------------------------
311 * SuffSuffIsSuffixP --
312 * Predicate form of SuffSuffIsSuffix. Passed as the callback function
316 * 0 if the suffix is the one desired, non-zero if not.
321 *-----------------------------------------------------------------------
324 SuffSuffIsSuffixP(const void *s
, const void *sd
)
326 return(!SuffSuffIsSuffix(s
, sd
));
330 *-----------------------------------------------------------------------
331 * SuffSuffHasNameP --
332 * Callback procedure for finding a suffix based on its name. Used by
340 * 0 if the suffix is of the given name. non-zero otherwise.
344 *-----------------------------------------------------------------------
347 SuffSuffHasNameP(const void *s
, const void *sname
)
349 return (strcmp(sname
, ((const Suff
*)s
)->name
));
353 *-----------------------------------------------------------------------
354 * SuffSuffIsPrefix --
355 * See if the suffix described by s is a prefix of the string. Care
356 * must be taken when using this to search for transformations and
357 * what-not, since there could well be two suffixes, one of which
358 * is a prefix of the other...
361 * s suffix to compare
362 * str string to examine
365 * 0 if s is a prefix of str. non-zero otherwise
369 *-----------------------------------------------------------------------
372 SuffSuffIsPrefix(const void *s
, const void *str
)
374 return SuffStrIsPrefix(((const Suff
*)s
)->name
, str
) == NULL
;
378 *-----------------------------------------------------------------------
380 * See if the graph node has the desired name
383 * gn current node we're looking at
384 * name name we're looking for
387 * 0 if it does. non-zero if it doesn't
391 *-----------------------------------------------------------------------
394 SuffGNHasNameP(const void *gn
, const void *name
)
396 return (strcmp(name
, ((const GNode
*)gn
)->name
));
399 /*********** Maintenance Functions ************/
402 SuffUnRef(void *lp
, void *sp
)
406 LstNode ln
= Lst_Member(l
, sp
);
409 ((Suff
*)sp
)->refCount
--;
414 *-----------------------------------------------------------------------
416 * Free up all memory associated with the given suffix structure.
422 * the suffix entry is detroyed
423 *-----------------------------------------------------------------------
428 Suff
*s
= (Suff
*)sp
;
437 /* We don't delete suffixes in order, so we cannot use this */
439 Punt("Internal error deleting suffix `%s' with refcount = %d", s
->name
,
443 Lst_Destroy(s
->ref
, NULL
);
444 Lst_Destroy(s
->children
, NULL
);
445 Lst_Destroy(s
->parents
, NULL
);
446 Lst_Destroy(s
->searchPath
, Dir_Destroy
);
453 *-----------------------------------------------------------------------
455 * Remove the suffix into the list
461 * The reference count for the suffix is decremented and the
462 * suffix is possibly freed
463 *-----------------------------------------------------------------------
466 SuffRemove(Lst l
, Suff
*s
)
469 if (s
->refCount
== 0) {
470 SuffUnRef(sufflist
, s
);
476 *-----------------------------------------------------------------------
478 * Insert the suffix into the list keeping the list ordered by suffix
482 * l the list where in s should be inserted
483 * s the suffix to insert
489 * The reference count of the suffix is incremented
490 *-----------------------------------------------------------------------
493 SuffInsert(Lst l
, Suff
*s
)
495 LstNode ln
; /* current element in l we're examining */
496 Suff
*s2
= NULL
; /* the suffix descriptor in this element */
498 if (Lst_Open(l
) == FAILURE
) {
501 while ((ln
= Lst_Next(l
)) != NULL
) {
502 s2
= (Suff
*)Lst_Datum(ln
);
503 if (s2
->sNum
>= s
->sNum
) {
510 fprintf(debug_file
, "inserting %s(%d)...", s
->name
, s
->sNum
);
514 fprintf(debug_file
, "at end of list\n");
516 (void)Lst_AtEnd(l
, s
);
518 (void)Lst_AtEnd(s
->ref
, l
);
519 } else if (s2
->sNum
!= s
->sNum
) {
521 fprintf(debug_file
, "before %s(%d)\n", s2
->name
, s2
->sNum
);
523 (void)Lst_InsertBefore(l
, ln
, s
);
525 (void)Lst_AtEnd(s
->ref
, l
);
526 } else if (DEBUG(SUFF
)) {
527 fprintf(debug_file
, "already there\n");
532 *-----------------------------------------------------------------------
533 * Suff_ClearSuffixes --
534 * This is gross. Nuke the list of suffixes but keep all transformation
535 * rules around. The transformation graph is destroyed in this process,
536 * but we leave the list of rules so when a new graph is formed the rules
538 * This function is called from the parse module when a
539 * .SUFFIXES:\n line is encountered.
545 * the sufflist and its graph nodes are destroyed
546 *-----------------------------------------------------------------------
549 Suff_ClearSuffixes(void)
552 Lst_Concat(suffClean
, sufflist
, LST_CONCLINK
);
554 sufflist
= Lst_Init(FALSE
);
556 suffNull
= emptySuff
;
560 *-----------------------------------------------------------------------
561 * SuffParseTransform --
562 * Parse a transformation string to find its two component suffixes.
565 * str String being parsed
566 * srcPtr Place to store source of trans.
567 * targPtr Place to store target of trans.
570 * TRUE if the string is a valid transformation and FALSE otherwise.
573 * The passed pointers are overwritten.
575 *-----------------------------------------------------------------------
578 SuffParseTransform(char *str
, Suff
**srcPtr
, Suff
**targPtr
)
580 LstNode srcLn
; /* element in suffix list of trans source*/
581 Suff
*src
; /* Source of transformation */
582 LstNode targLn
; /* element in suffix list of trans target*/
583 char *str2
; /* Extra pointer (maybe target suffix) */
584 LstNode singleLn
; /* element in suffix list of any suffix
585 * that exactly matches str */
586 Suff
*single
= NULL
;/* Source of possible transformation to
593 * Loop looking first for a suffix that matches the start of the
594 * string and then for one that exactly matches the rest of it. If
595 * we can find two that meet these criteria, we've successfully
600 srcLn
= Lst_Find(sufflist
, str
, SuffSuffIsPrefix
);
602 srcLn
= Lst_FindFrom(sufflist
, Lst_Succ(srcLn
), str
,
607 * Ran out of source suffixes -- no such rule
609 if (singleLn
!= NULL
) {
611 * Not so fast Mr. Smith! There was a suffix that encompassed
612 * the entire string, so we assume it was a transformation
613 * to the null suffix (thank you POSIX). We still prefer to
614 * find a double rule over a singleton, hence we leave this
615 * check until the end.
617 * XXX: Use emptySuff over suffNull?
625 src
= (Suff
*)Lst_Datum(srcLn
);
626 str2
= str
+ src
->nameLen
;
631 targLn
= Lst_Find(sufflist
, str2
, SuffSuffHasNameP
);
632 if (targLn
!= NULL
) {
634 *targPtr
= (Suff
*)Lst_Datum(targLn
);
642 *-----------------------------------------------------------------------
643 * Suff_IsTransform --
644 * Return TRUE if the given string is a transformation rule
648 * str string to check
651 * TRUE if the string is a concatenation of two known suffixes.
656 *-----------------------------------------------------------------------
659 Suff_IsTransform(char *str
)
663 return (SuffParseTransform(str
, &src
, &targ
));
667 *-----------------------------------------------------------------------
668 * Suff_AddTransform --
669 * Add the transformation rule described by the line to the
670 * list of rules and place the transformation itself in the graph
673 * line name of transformation to add
676 * The node created for the transformation in the transforms list
679 * The node is placed on the end of the transforms Lst and links are
680 * made between the two suffixes mentioned in the target name
681 *-----------------------------------------------------------------------
684 Suff_AddTransform(char *line
)
686 GNode
*gn
; /* GNode of transformation rule */
687 Suff
*s
, /* source suffix */
688 *t
; /* target suffix */
689 LstNode ln
; /* Node for existing transformation */
691 ln
= Lst_Find(transforms
, line
, SuffGNHasNameP
);
694 * Make a new graph node for the transformation. It will be filled in
695 * by the Parse module.
697 gn
= Targ_NewGN(line
);
698 (void)Lst_AtEnd(transforms
, gn
);
701 * New specification for transformation rule. Just nuke the old list
702 * of commands so they can be filled in again... We don't actually
703 * free the commands themselves, because a given command can be
704 * attached to several different transformations.
706 gn
= (GNode
*)Lst_Datum(ln
);
707 Lst_Destroy(gn
->commands
, NULL
);
708 Lst_Destroy(gn
->children
, NULL
);
709 gn
->commands
= Lst_Init(FALSE
);
710 gn
->children
= Lst_Init(FALSE
);
713 gn
->type
= OP_TRANSFORM
;
715 (void)SuffParseTransform(line
, &s
, &t
);
718 * link the two together in the proper relationship and order
721 fprintf(debug_file
, "defining transformation from `%s' to `%s'\n",
724 SuffInsert(t
->children
, s
);
725 SuffInsert(s
->parents
, t
);
731 *-----------------------------------------------------------------------
732 * Suff_EndTransform --
733 * Handle the finish of a transformation definition, removing the
734 * transformation from the graph if it has neither commands nor
735 * sources. This is a callback procedure for the Parse module via
739 * gnp Node for transformation
740 * dummy Node for transformation
746 * If the node has no commands or children, the children and parents
747 * lists of the affected suffixes are altered.
749 *-----------------------------------------------------------------------
752 Suff_EndTransform(void *gnp
, void *dummy
)
754 GNode
*gn
= (GNode
*)gnp
;
756 if ((gn
->type
& OP_DOUBLEDEP
) && !Lst_IsEmpty (gn
->cohorts
))
757 gn
= (GNode
*)Lst_Datum(Lst_Last(gn
->cohorts
));
758 if ((gn
->type
& OP_TRANSFORM
) && Lst_IsEmpty(gn
->commands
) &&
759 Lst_IsEmpty(gn
->children
))
764 * SuffParseTransform() may fail for special rules which are not
765 * actual transformation rules. (e.g. .DEFAULT)
767 if (SuffParseTransform(gn
->name
, &s
, &t
)) {
771 fprintf(debug_file
, "deleting transformation from `%s' to `%s'\n",
776 * Store s->parents because s could be deleted in SuffRemove
781 * Remove the source from the target's children list. We check for a
782 * nil return to handle a beanhead saying something like
785 * We'll be called twice when the next target is seen, but .c and .o
786 * are only linked once...
788 SuffRemove(t
->children
, s
);
791 * Remove the target from the source's parents list
795 } else if ((gn
->type
& OP_TRANSFORM
) && DEBUG(SUFF
)) {
796 fprintf(debug_file
, "transformation %s complete\n", gn
->name
);
799 return(dummy
? 0 : 0);
803 *-----------------------------------------------------------------------
804 * SuffRebuildGraph --
805 * Called from Suff_AddSuffix via Lst_ForEach to search through the
806 * list of existing transformation rules and rebuild the transformation
807 * graph when it has been destroyed by Suff_ClearSuffixes. If the
808 * given rule is a transformation involving this suffix and another,
809 * existing suffix, the proper relationship is established between
813 * transformp Transformation to test
814 * sp Suffix to rebuild
820 * The appropriate links will be made between this suffix and
821 * others if transformation rules exist for it.
823 *-----------------------------------------------------------------------
826 SuffRebuildGraph(void *transformp
, void *sp
)
828 GNode
*transform
= (GNode
*)transformp
;
829 Suff
*s
= (Suff
*)sp
;
836 * First see if it is a transformation from this suffix.
838 cp
= UNCONST(SuffStrIsPrefix(s
->name
, transform
->name
));
840 ln
= Lst_Find(sufflist
, cp
, SuffSuffHasNameP
);
843 * Found target. Link in and return, since it can't be anything
846 s2
= (Suff
*)Lst_Datum(ln
);
847 SuffInsert(s2
->children
, s
);
848 SuffInsert(s
->parents
, s2
);
854 * Not from, maybe to?
856 sd
.len
= strlen(transform
->name
);
857 sd
.ename
= transform
->name
+ sd
.len
;
858 cp
= SuffSuffIsSuffix(s
, &sd
);
861 * Null-terminate the source suffix in order to find it.
864 ln
= Lst_Find(sufflist
, transform
->name
, SuffSuffHasNameP
);
866 * Replace the start of the target suffix
871 * Found it -- establish the proper relationship
873 s2
= (Suff
*)Lst_Datum(ln
);
874 SuffInsert(s
->children
, s2
);
875 SuffInsert(s2
->parents
, s
);
882 *-----------------------------------------------------------------------
884 * Called from Suff_AddSuffix via Lst_ForEach to search through the
885 * list of existing targets and find if any of the existing targets
886 * can be turned into a transformation rule.
889 * 1 if a new main target has been selected, 0 otherwise.
892 * If such a target is found and the target is the current main
893 * target, the main target is set to NULL and the next target
894 * examined (if that exists) becomes the main target.
896 *-----------------------------------------------------------------------
899 SuffScanTargets(void *targetp
, void *gsp
)
901 GNode
*target
= (GNode
*)targetp
;
902 GNodeSuff
*gs
= (GNodeSuff
*)gsp
;
906 if (*gs
->gn
== NULL
&& gs
->r
&& (target
->type
& OP_NOTARGET
) == 0) {
908 Targ_SetMain(target
);
912 if ((unsigned int)target
->type
== OP_TRANSFORM
)
915 if ((ptr
= strstr(target
->name
, gs
->s
->name
)) == NULL
||
919 if (SuffParseTransform(target
->name
, &s
, &t
)) {
920 if (*gs
->gn
== target
) {
925 Lst_Destroy(target
->children
, NULL
);
926 target
->children
= Lst_Init(FALSE
);
927 target
->type
= OP_TRANSFORM
;
929 * link the two together in the proper relationship and order
932 fprintf(debug_file
, "defining transformation from `%s' to `%s'\n",
935 SuffInsert(t
->children
, s
);
936 SuffInsert(s
->parents
, t
);
942 *-----------------------------------------------------------------------
944 * Add the suffix in string to the end of the list of known suffixes.
945 * Should we restructure the suffix graph? Make doesn't...
948 * str the name of the suffix to add
954 * A GNode is created for the suffix and a Suff structure is created and
955 * added to the suffixes list unless the suffix was already known.
956 * The mainNode passed can be modified if a target mutated into a
957 * transform and that target happened to be the main target.
958 *-----------------------------------------------------------------------
961 Suff_AddSuffix(char *str
, GNode
**gn
)
963 Suff
*s
; /* new suffix descriptor */
967 ln
= Lst_Find(sufflist
, str
, SuffSuffHasNameP
);
969 s
= bmake_malloc(sizeof(Suff
));
971 s
->name
= bmake_strdup(str
);
972 s
->nameLen
= strlen(s
->name
);
973 s
->searchPath
= Lst_Init(FALSE
);
974 s
->children
= Lst_Init(FALSE
);
975 s
->parents
= Lst_Init(FALSE
);
976 s
->ref
= Lst_Init(FALSE
);
981 (void)Lst_AtEnd(sufflist
, s
);
983 * We also look at our existing targets list to see if adding
984 * this suffix will make one of our current targets mutate into
985 * a suffix rule. This is ugly, but other makes treat all targets
986 * that start with a . as suffix rules.
991 Lst_ForEach(Targ_List(), SuffScanTargets
, &gs
);
993 * Look for any existing transformations from or to this suffix.
994 * XXX: Only do this after a Suff_ClearSuffixes?
996 Lst_ForEach(transforms
, SuffRebuildGraph
, s
);
1001 *-----------------------------------------------------------------------
1003 * Return the search path for the given suffix, if it's defined.
1006 * The searchPath for the desired suffix or NULL if the suffix isn't
1011 *-----------------------------------------------------------------------
1014 Suff_GetPath(char *sname
)
1019 ln
= Lst_Find(sufflist
, sname
, SuffSuffHasNameP
);
1023 s
= (Suff
*)Lst_Datum(ln
);
1024 return (s
->searchPath
);
1029 *-----------------------------------------------------------------------
1031 * Extend the search paths for all suffixes to include the default
1038 * The searchPath field of all the suffixes is extended by the
1039 * directories in dirSearchPath. If paths were specified for the
1040 * ".h" suffix, the directories are stuffed into a global variable
1041 * called ".INCLUDES" with each directory preceded by a -I. The same
1042 * is done for the ".a" suffix, except the variable is called
1043 * ".LIBS" and the flag is -L.
1044 *-----------------------------------------------------------------------
1052 Lst inIncludes
; /* Cumulative .INCLUDES path */
1053 Lst inLibs
; /* Cumulative .LIBS path */
1055 if (Lst_Open(sufflist
) == FAILURE
) {
1059 inIncludes
= Lst_Init(FALSE
);
1060 inLibs
= Lst_Init(FALSE
);
1062 while ((ln
= Lst_Next(sufflist
)) != NULL
) {
1063 s
= (Suff
*)Lst_Datum(ln
);
1064 if (!Lst_IsEmpty (s
->searchPath
)) {
1066 if (s
->flags
& SUFF_INCLUDE
) {
1067 Dir_Concat(inIncludes
, s
->searchPath
);
1069 #endif /* INCLUDES */
1071 if (s
->flags
& SUFF_LIBRARY
) {
1072 Dir_Concat(inLibs
, s
->searchPath
);
1074 #endif /* LIBRARIES */
1075 Dir_Concat(s
->searchPath
, dirSearchPath
);
1077 Lst_Destroy(s
->searchPath
, Dir_Destroy
);
1078 s
->searchPath
= Lst_Duplicate(dirSearchPath
, Dir_CopyDir
);
1082 Var_Set(".INCLUDES", ptr
= Dir_MakeFlags("-I", inIncludes
), VAR_GLOBAL
, 0);
1084 Var_Set(".LIBS", ptr
= Dir_MakeFlags("-L", inLibs
), VAR_GLOBAL
, 0);
1087 Lst_Destroy(inIncludes
, Dir_Destroy
);
1088 Lst_Destroy(inLibs
, Dir_Destroy
);
1090 Lst_Close(sufflist
);
1094 *-----------------------------------------------------------------------
1095 * Suff_AddInclude --
1096 * Add the given suffix as a type of file which gets included.
1097 * Called from the parse module when a .INCLUDES line is parsed.
1098 * The suffix must have already been defined.
1101 * sname Name of the suffix to mark
1107 * The SUFF_INCLUDE bit is set in the suffix's flags field
1109 *-----------------------------------------------------------------------
1112 Suff_AddInclude(char *sname
)
1117 ln
= Lst_Find(sufflist
, sname
, SuffSuffHasNameP
);
1119 s
= (Suff
*)Lst_Datum(ln
);
1120 s
->flags
|= SUFF_INCLUDE
;
1125 *-----------------------------------------------------------------------
1127 * Add the given suffix as a type of file which is a library.
1128 * Called from the parse module when parsing a .LIBS line. The
1129 * suffix must have been defined via .SUFFIXES before this is
1133 * sname Name of the suffix to mark
1139 * The SUFF_LIBRARY bit is set in the suffix's flags field
1141 *-----------------------------------------------------------------------
1144 Suff_AddLib(char *sname
)
1149 ln
= Lst_Find(sufflist
, sname
, SuffSuffHasNameP
);
1151 s
= (Suff
*)Lst_Datum(ln
);
1152 s
->flags
|= SUFF_LIBRARY
;
1156 /********** Implicit Source Search Functions *********/
1159 *-----------------------------------------------------------------------
1161 * Add a suffix as a Src structure to the given list with its parent
1162 * being the given Src structure. If the suffix is the null suffix,
1163 * the prefix is used unaltered as the file name in the Src structure.
1166 * sp suffix for which to create a Src structure
1167 * lsp list and parent for the new Src
1173 * A Src structure is created and tacked onto the end of the list
1174 *-----------------------------------------------------------------------
1177 SuffAddSrc(void *sp
, void *lsp
)
1179 Suff
*s
= (Suff
*)sp
;
1180 LstSrc
*ls
= (LstSrc
*)lsp
;
1181 Src
*s2
; /* new Src structure */
1182 Src
*targ
; /* Target structure */
1186 if ((s
->flags
& SUFF_NULL
) && (*s
->name
!= '\0')) {
1188 * If the suffix has been marked as the NULL suffix, also create a Src
1189 * structure for a file with no suffix attached. Two birds, and all
1192 s2
= bmake_malloc(sizeof(Src
));
1193 s2
->file
= bmake_strdup(targ
->pref
);
1194 s2
->pref
= targ
->pref
;
1200 targ
->children
+= 1;
1201 (void)Lst_AtEnd(ls
->l
, s2
);
1203 s2
->cp
= Lst_Init(FALSE
);
1204 Lst_AtEnd(targ
->cp
, s2
);
1205 fprintf(debug_file
, "1 add %x %x to %x:", targ
, s2
, ls
->l
);
1206 Lst_ForEach(ls
->l
, PrintAddr
, NULL
);
1207 fprintf(debug_file
, "\n");
1210 s2
= bmake_malloc(sizeof(Src
));
1211 s2
->file
= str_concat(targ
->pref
, s
->name
, 0);
1212 s2
->pref
= targ
->pref
;
1218 targ
->children
+= 1;
1219 (void)Lst_AtEnd(ls
->l
, s2
);
1221 s2
->cp
= Lst_Init(FALSE
);
1222 Lst_AtEnd(targ
->cp
, s2
);
1223 fprintf(debug_file
, "2 add %x %x to %x:", targ
, s2
, ls
->l
);
1224 Lst_ForEach(ls
->l
, PrintAddr
, NULL
);
1225 fprintf(debug_file
, "\n");
1232 *-----------------------------------------------------------------------
1234 * Add all the children of targ as Src structures to the given list
1237 * l list to which to add the new level
1238 * targ Src structure to use as the parent
1244 * Lots of structures are created and added to the list
1245 *-----------------------------------------------------------------------
1248 SuffAddLevel(Lst l
, Src
*targ
)
1255 Lst_ForEach(targ
->suff
->children
, SuffAddSrc
, &ls
);
1259 *----------------------------------------------------------------------
1261 * Free all src structures in list that don't have a reference count
1264 * Ture if an src was removed
1267 * The memory is free'd.
1268 *----------------------------------------------------------------------
1271 SuffRemoveSrc(Lst l
)
1277 if (Lst_Open(l
) == FAILURE
) {
1281 fprintf(debug_file
, "cleaning %lx: ", (unsigned long) l
);
1282 Lst_ForEach(l
, PrintAddr
, NULL
);
1283 fprintf(debug_file
, "\n");
1287 while ((ln
= Lst_Next(l
)) != NULL
) {
1288 s
= (Src
*)Lst_Datum(ln
);
1289 if (s
->children
== 0) {
1295 LstNode ln
= Lst_Member(s
->parent
->cp
, s
);
1297 Lst_Remove(s
->parent
->cp
, ln
);
1299 --s
->parent
->children
;
1302 fprintf(debug_file
, "free: [l=%x] p=%x %d\n", l
, s
, s
->children
);
1303 Lst_Destroy(s
->cp
, NULL
);
1313 fprintf(debug_file
, "keep: [l=%x] p=%x %d: ", l
, s
, s
->children
);
1314 Lst_ForEach(s
->cp
, PrintAddr
, NULL
);
1315 fprintf(debug_file
, "\n");
1326 *-----------------------------------------------------------------------
1328 * Find the first existing file/target in the list srcs
1331 * srcs list of Src structures to search through
1334 * The lowest structure in the chain of transformations
1338 *-----------------------------------------------------------------------
1341 SuffFindThem(Lst srcs
, Lst slst
)
1343 Src
*s
; /* current Src */
1344 Src
*rs
; /* returned Src */
1349 while (!Lst_IsEmpty (srcs
)) {
1350 s
= (Src
*)Lst_DeQueue(srcs
);
1353 fprintf(debug_file
, "\ttrying %s...", s
->file
);
1357 * A file is considered to exist if either a node exists in the
1358 * graph for it or the file actually exists.
1360 if (Targ_FindNode(s
->file
, TARG_NOCREATE
) != NULL
) {
1362 fprintf(debug_file
, "remove %x from %x\n", s
, srcs
);
1368 if ((ptr
= Dir_FindFile(s
->file
, s
->suff
->searchPath
)) != NULL
) {
1371 fprintf(debug_file
, "remove %x from %x\n", s
, srcs
);
1378 fprintf(debug_file
, "not there\n");
1381 SuffAddLevel(srcs
, s
);
1385 if (DEBUG(SUFF
) && rs
) {
1386 fprintf(debug_file
, "got it\n");
1392 *-----------------------------------------------------------------------
1394 * See if any of the children of the target in the Src structure is
1395 * one from which the target can be transformed. If there is one,
1396 * a Src structure is put together for it and returned.
1399 * targ Src structure to play with
1402 * The Src structure of the "winning" child, or NULL if no such beast.
1405 * A Src structure may be allocated.
1407 *-----------------------------------------------------------------------
1410 SuffFindCmds(Src
*targ
, Lst slst
)
1412 LstNode ln
; /* General-purpose list node */
1413 GNode
*t
, /* Target GNode */
1414 *s
; /* Source GNode */
1415 int prefLen
;/* The length of the defined prefix */
1416 Suff
*suff
; /* Suffix on matching beastie */
1417 Src
*ret
; /* Return value */
1421 (void)Lst_Open(t
->children
);
1422 prefLen
= strlen(targ
->pref
);
1425 ln
= Lst_Next(t
->children
);
1427 Lst_Close(t
->children
);
1430 s
= (GNode
*)Lst_Datum(ln
);
1432 if (s
->type
& OP_OPTIONAL
&& Lst_IsEmpty(t
->commands
)) {
1434 * We haven't looked to see if .OPTIONAL files exist yet, so
1435 * don't use one as the implicit source.
1436 * This allows us to use .OPTIONAL in .depend files so make won't
1437 * complain "don't know how to make xxx.h' when a dependent file
1438 * has been moved/deleted.
1443 cp
= strrchr(s
->name
, '/');
1449 if (strncmp(cp
, targ
->pref
, prefLen
) != 0)
1452 * The node matches the prefix ok, see if it has a known
1455 ln
= Lst_Find(sufflist
, &cp
[prefLen
], SuffSuffHasNameP
);
1459 * It even has a known suffix, see if there's a transformation
1460 * defined between the node's suffix and the target's suffix.
1462 * XXX: Handle multi-stage transformations here, too.
1464 suff
= (Suff
*)Lst_Datum(ln
);
1466 if (Lst_Member(suff
->parents
, targ
->suff
) != NULL
)
1471 * Hot Damn! Create a new Src structure to describe
1472 * this transformation (making sure to duplicate the
1473 * source node's name so Suff_FindDeps can free it
1474 * again (ick)), and return the new structure.
1476 ret
= bmake_malloc(sizeof(Src
));
1477 ret
->file
= bmake_strdup(s
->name
);
1478 ret
->pref
= targ
->pref
;
1484 targ
->children
+= 1;
1486 ret
->cp
= Lst_Init(FALSE
);
1487 fprintf(debug_file
, "3 add %x %x\n", targ
, ret
);
1488 Lst_AtEnd(targ
->cp
, ret
);
1490 Lst_AtEnd(slst
, ret
);
1492 fprintf(debug_file
, "\tusing existing source %s\n", s
->name
);
1498 *-----------------------------------------------------------------------
1499 * SuffExpandChildren --
1500 * Expand the names of any children of a given node that contain
1501 * variable invocations or file wildcards into actual targets.
1504 * cln Child to examine
1505 * pgn Parent node being processed
1511 * The expanded node is removed from the parent's list of children,
1512 * and the parent's unmade counter is decremented, but other nodes
1515 *-----------------------------------------------------------------------
1518 SuffExpandChildren(LstNode cln
, GNode
*pgn
)
1520 GNode
*cgn
= (GNode
*)Lst_Datum(cln
);
1521 GNode
*gn
; /* New source 8) */
1522 char *cp
; /* Expanded value */
1524 if (!Lst_IsEmpty(cgn
->order_pred
) || !Lst_IsEmpty(cgn
->order_succ
))
1525 /* It is all too hard to process the result of .ORDER */
1528 if (cgn
->type
& OP_WAIT
)
1529 /* Ignore these (& OP_PHONY ?) */
1533 * First do variable expansion -- this takes precedence over
1534 * wildcard expansion. If the result contains wildcards, they'll be gotten
1535 * to later since the resulting words are tacked on to the end of
1536 * the children list.
1538 if (strchr(cgn
->name
, '$') == NULL
) {
1539 SuffExpandWildcards(cln
, pgn
);
1544 fprintf(debug_file
, "Expanding \"%s\"...", cgn
->name
);
1546 cp
= Var_Subst(NULL
, cgn
->name
, pgn
, TRUE
);
1549 Lst members
= Lst_Init(FALSE
);
1551 if (cgn
->type
& OP_ARCHV
) {
1553 * Node was an archive(member) target, so we want to call
1554 * on the Arch module to find the nodes for us, expanding
1555 * variables in the parent's context.
1557 char *sacrifice
= cp
;
1559 (void)Arch_ParseArchive(&sacrifice
, members
, pgn
);
1562 * Break the result into a vector of strings whose nodes
1563 * we can find, then add those nodes to the members list.
1564 * Unfortunately, we can't use brk_string b/c it
1565 * doesn't understand about variable specifications with
1569 char *initcp
= cp
; /* For freeing... */
1571 for (start
= cp
; *start
== ' ' || *start
== '\t'; start
++)
1573 for (cp
= start
; *cp
!= '\0'; cp
++) {
1574 if (*cp
== ' ' || *cp
== '\t') {
1576 * White-space -- terminate element, find the node,
1577 * add it, skip any further spaces.
1580 gn
= Targ_FindNode(start
, TARG_CREATE
);
1581 (void)Lst_AtEnd(members
, gn
);
1582 while (*cp
== ' ' || *cp
== '\t') {
1586 * Adjust cp for increment at start of loop, but
1587 * set start to first non-space.
1590 } else if (*cp
== '$') {
1592 * Start of a variable spec -- contact variable module
1593 * to find the end so we can skip over it.
1599 junk
= Var_Parse(cp
, pgn
, TRUE
, &len
, &freeIt
);
1600 if (junk
!= var_Error
) {
1606 } else if (*cp
== '\\' && *cp
!= '\0') {
1608 * Escaped something -- skip over it
1616 * Stuff left over -- add it to the list too
1618 gn
= Targ_FindNode(start
, TARG_CREATE
);
1619 (void)Lst_AtEnd(members
, gn
);
1622 * Point cp back at the beginning again so the variable value
1629 * Add all elements of the members list to the parent node.
1631 while(!Lst_IsEmpty(members
)) {
1632 gn
= (GNode
*)Lst_DeQueue(members
);
1635 fprintf(debug_file
, "%s...", gn
->name
);
1637 /* Add gn to the parents child list before the original child */
1638 (void)Lst_InsertBefore(pgn
->children
, cln
, gn
);
1639 (void)Lst_AtEnd(gn
->parents
, pgn
);
1641 /* Expand wildcards on new node */
1642 SuffExpandWildcards(Lst_Prev(cln
), pgn
);
1644 Lst_Destroy(members
, NULL
);
1652 fprintf(debug_file
, "\n");
1656 * Now the source is expanded, remove it from the list of children to
1657 * keep it from being processed.
1660 Lst_Remove(pgn
->children
, cln
);
1661 Lst_Remove(cgn
->parents
, Lst_Member(cgn
->parents
, pgn
));
1665 SuffExpandWildcards(LstNode cln
, GNode
*pgn
)
1667 GNode
*cgn
= (GNode
*)Lst_Datum(cln
);
1668 GNode
*gn
; /* New source 8) */
1669 char *cp
; /* Expanded value */
1670 Lst explist
; /* List of expansions */
1672 if (!Dir_HasWildcards(cgn
->name
))
1676 * Expand the word along the chosen path
1678 explist
= Lst_Init(FALSE
);
1679 Dir_Expand(cgn
->name
, Suff_FindPath(cgn
), explist
);
1681 while (!Lst_IsEmpty(explist
)) {
1683 * Fetch next expansion off the list and find its GNode
1685 cp
= (char *)Lst_DeQueue(explist
);
1688 fprintf(debug_file
, "%s...", cp
);
1690 gn
= Targ_FindNode(cp
, TARG_CREATE
);
1692 /* Add gn to the parents child list before the original child */
1693 (void)Lst_InsertBefore(pgn
->children
, cln
, gn
);
1694 (void)Lst_AtEnd(gn
->parents
, pgn
);
1699 * Nuke what's left of the list
1701 Lst_Destroy(explist
, NULL
);
1704 fprintf(debug_file
, "\n");
1708 * Now the source is expanded, remove it from the list of children to
1709 * keep it from being processed.
1712 Lst_Remove(pgn
->children
, cln
);
1713 Lst_Remove(cgn
->parents
, Lst_Member(cgn
->parents
, pgn
));
1717 *-----------------------------------------------------------------------
1719 * Find a path along which to expand the node.
1721 * If the word has a known suffix, use that path.
1722 * If it has no known suffix, use the default system search path.
1725 * gn Node being examined
1728 * The appropriate path to search for the GNode.
1731 * XXX: We could set the suffix here so that we don't have to scan
1734 *-----------------------------------------------------------------------
1737 Suff_FindPath(GNode
* gn
)
1739 Suff
*suff
= gn
->suffix
;
1742 SuffixCmpData sd
; /* Search string data */
1744 sd
.len
= strlen(gn
->name
);
1745 sd
.ename
= gn
->name
+ sd
.len
;
1746 ln
= Lst_Find(sufflist
, &sd
, SuffSuffIsSuffixP
);
1749 fprintf(debug_file
, "Wildcard expanding \"%s\"...", gn
->name
);
1752 suff
= (Suff
*)Lst_Datum(ln
);
1753 /* XXX: Here we can save the suffix so we don't have to do this again */
1758 fprintf(debug_file
, "suffix is \"%s\"...", suff
->name
);
1760 return suff
->searchPath
;
1763 * Use default search path
1765 return dirSearchPath
;
1770 *-----------------------------------------------------------------------
1771 * SuffApplyTransform --
1772 * Apply a transformation rule, given the source and target nodes
1782 * TRUE if successful, FALSE if not.
1785 * The source and target are linked and the commands from the
1786 * transformation are added to the target node's commands list.
1787 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1788 * to the target. The target also inherits all the sources for
1789 * the transformation rule.
1791 *-----------------------------------------------------------------------
1794 SuffApplyTransform(GNode
*tGn
, GNode
*sGn
, Suff
*t
, Suff
*s
)
1796 LstNode ln
, nln
; /* General node */
1797 char *tname
; /* Name of transformation rule */
1798 GNode
*gn
; /* Node for same */
1801 * Form the proper links between the target and source.
1803 (void)Lst_AtEnd(tGn
->children
, sGn
);
1804 (void)Lst_AtEnd(sGn
->parents
, tGn
);
1808 * Locate the transformation rule itself
1810 tname
= str_concat(s
->name
, t
->name
, 0);
1811 ln
= Lst_Find(transforms
, tname
, SuffGNHasNameP
);
1816 * Not really such a transformation rule (can happen when we're
1817 * called to link an OP_MEMBER and OP_ARCHV node), so return
1823 gn
= (GNode
*)Lst_Datum(ln
);
1826 fprintf(debug_file
, "\tapplying %s -> %s to \"%s\"\n", s
->name
, t
->name
, tGn
->name
);
1830 * Record last child for expansion purposes
1832 ln
= Lst_Last(tGn
->children
);
1835 * Pass the buck to Make_HandleUse to apply the rule
1837 (void)Make_HandleUse(gn
, tGn
);
1840 * Deal with wildcards and variables in any acquired sources
1842 for (ln
= Lst_Succ(ln
); ln
!= NULL
; ln
= nln
) {
1844 SuffExpandChildren(ln
, tGn
);
1848 * Keep track of another parent to which this beast is transformed so
1849 * the .IMPSRC variable can be set correctly for the parent.
1851 (void)Lst_AtEnd(sGn
->iParents
, tGn
);
1858 *-----------------------------------------------------------------------
1859 * SuffFindArchiveDeps --
1860 * Locate dependencies for an OP_ARCHV node.
1863 * gn Node for which to locate dependencies
1869 * Same as Suff_FindDeps
1871 *-----------------------------------------------------------------------
1874 SuffFindArchiveDeps(GNode
*gn
, Lst slst
)
1876 char *eoarch
; /* End of archive portion */
1877 char *eoname
; /* End of member portion */
1878 GNode
*mem
; /* Node for member */
1879 static const char *copy
[] = {
1880 /* Variables to be copied from the member node */
1881 TARGET
, /* Must be first */
1882 PREFIX
, /* Must be second */
1884 int i
; /* Index into copy and vals */
1885 Suff
*ms
; /* Suffix descriptor for member */
1886 char *name
; /* Start of member's name */
1889 * The node is an archive(member) pair. so we must find a
1890 * suffix for both of them.
1892 eoarch
= strchr(gn
->name
, '(');
1893 eoname
= strchr(eoarch
, ')');
1895 *eoname
= '\0'; /* Nuke parentheses during suffix search */
1896 *eoarch
= '\0'; /* So a suffix can be found */
1901 * To simplify things, call Suff_FindDeps recursively on the member now,
1902 * so we can simply compare the member's .PREFIX and .TARGET variables
1903 * to locate its suffix. This allows us to figure out the suffix to
1904 * use for the archive without having to do a quadratic search over the
1905 * suffix list, backtracking for each one...
1907 mem
= Targ_FindNode(name
, TARG_CREATE
);
1908 SuffFindDeps(mem
, slst
);
1911 * Create the link between the two nodes right off
1913 (void)Lst_AtEnd(gn
->children
, mem
);
1914 (void)Lst_AtEnd(mem
->parents
, gn
);
1918 * Copy in the variables from the member node to this one.
1920 for (i
= (sizeof(copy
)/sizeof(copy
[0]))-1; i
>= 0; i
--) {
1922 Var_Set(copy
[i
], Var_Value(copy
[i
], mem
, &p1
), gn
, 0);
1931 * Didn't know what it was -- use .NULL suffix if not in make mode
1934 fprintf(debug_file
, "using null suffix\n");
1941 * Set the other two local variables required for this target.
1943 Var_Set(MEMBER
, name
, gn
, 0);
1944 Var_Set(ARCHIVE
, gn
->name
, gn
, 0);
1948 * Member has a known suffix, so look for a transformation rule from
1949 * it to a possible suffix of the archive. Rather than searching
1950 * through the entire list, we just look at suffixes to which the
1951 * member's suffix may be transformed...
1954 SuffixCmpData sd
; /* Search string data */
1957 * Use first matching suffix...
1959 sd
.len
= eoarch
- gn
->name
;
1961 ln
= Lst_Find(ms
->parents
, &sd
, SuffSuffIsSuffixP
);
1965 * Got one -- apply it
1967 if (!SuffApplyTransform(gn
, mem
, (Suff
*)Lst_Datum(ln
), ms
) &&
1970 fprintf(debug_file
, "\tNo transformation from %s -> %s\n",
1971 ms
->name
, ((Suff
*)Lst_Datum(ln
))->name
);
1977 * Replace the opening and closing parens now we've no need of the separate
1980 *eoarch
= '('; *eoname
= ')';
1983 * Pretend gn appeared to the left of a dependency operator so
1984 * the user needn't provide a transformation from the member to the
1987 if (OP_NOP(gn
->type
)) {
1988 gn
->type
|= OP_DEPENDS
;
1992 * Flag the member as such so we remember to look in the archive for
1993 * its modification time.
1995 mem
->type
|= OP_MEMBER
;
1999 *-----------------------------------------------------------------------
2000 * SuffFindNormalDeps --
2001 * Locate implicit dependencies for regular targets.
2004 * gn Node for which to find sources
2010 * Same as Suff_FindDeps...
2012 *-----------------------------------------------------------------------
2015 SuffFindNormalDeps(GNode
*gn
, Lst slst
)
2017 char *eoname
; /* End of name */
2018 char *sopref
; /* Start of prefix */
2019 LstNode ln
, nln
; /* Next suffix node to check */
2020 Lst srcs
; /* List of sources at which to look */
2021 Lst targs
; /* List of targets to which things can be
2022 * transformed. They all have the same file,
2023 * but different suff and pref fields */
2024 Src
*bottom
; /* Start of found transformation path */
2025 Src
*src
; /* General Src pointer */
2026 char *pref
; /* Prefix to use */
2027 Src
*targ
; /* General Src target pointer */
2028 SuffixCmpData sd
; /* Search string data */
2031 sd
.len
= strlen(gn
->name
);
2032 sd
.ename
= eoname
= gn
->name
+ sd
.len
;
2037 * Begin at the beginning...
2039 ln
= Lst_First(sufflist
);
2040 srcs
= Lst_Init(FALSE
);
2041 targs
= Lst_Init(FALSE
);
2044 * We're caught in a catch-22 here. On the one hand, we want to use any
2045 * transformation implied by the target's sources, but we can't examine
2046 * the sources until we've expanded any variables/wildcards they may hold,
2047 * and we can't do that until we've set up the target's local variables
2048 * and we can't do that until we know what the proper suffix for the
2049 * target is (in case there are two suffixes one of which is a suffix of
2050 * the other) and we can't know that until we've found its implied
2051 * source, which we may not want to use if there's an existing source
2052 * that implies a different transformation.
2054 * In an attempt to get around this, which may not work all the time,
2055 * but should work most of the time, we look for implied sources first,
2056 * checking transformations to all possible suffixes of the target,
2057 * use what we find to set the target's local variables, expand the
2058 * children, then look for any overriding transformations they imply.
2059 * Should we find one, we discard the one we found before.
2064 if (!(gn
->type
& OP_PHONY
)) {
2066 while (ln
!= NULL
) {
2068 * Look for next possible suffix...
2070 ln
= Lst_FindFrom(sufflist
, ln
, &sd
, SuffSuffIsSuffixP
);
2073 int prefLen
; /* Length of the prefix */
2076 * Allocate a Src structure to which things can be transformed
2078 targ
= bmake_malloc(sizeof(Src
));
2079 targ
->file
= bmake_strdup(gn
->name
);
2080 targ
->suff
= (Suff
*)Lst_Datum(ln
);
2081 targ
->suff
->refCount
++;
2083 targ
->parent
= NULL
;
2086 targ
->cp
= Lst_Init(FALSE
);
2090 * Allocate room for the prefix, whose end is found by subtracting
2091 * the length of the suffix from the end of the name.
2093 prefLen
= (eoname
- targ
->suff
->nameLen
) - sopref
;
2094 targ
->pref
= bmake_malloc(prefLen
+ 1);
2095 memcpy(targ
->pref
, sopref
, prefLen
);
2096 targ
->pref
[prefLen
] = '\0';
2099 * Add nodes from which the target can be made
2101 SuffAddLevel(srcs
, targ
);
2104 * Record the target so we can nuke it
2106 (void)Lst_AtEnd(targs
, targ
);
2109 * Search from this suffix's successor...
2116 * Handle target of unknown suffix...
2118 if (Lst_IsEmpty(targs
) && suffNull
!= NULL
) {
2120 fprintf(debug_file
, "\tNo known suffix on %s. Using .NULL suffix\n", gn
->name
);
2123 targ
= bmake_malloc(sizeof(Src
));
2124 targ
->file
= bmake_strdup(gn
->name
);
2125 targ
->suff
= suffNull
;
2126 targ
->suff
->refCount
++;
2128 targ
->parent
= NULL
;
2130 targ
->pref
= bmake_strdup(sopref
);
2132 targ
->cp
= Lst_Init(FALSE
);
2136 * Only use the default suffix rules if we don't have commands
2137 * defined for this gnode; traditional make programs used to
2138 * not define suffix rules if the gnode had children but we
2139 * don't do this anymore.
2141 if (Lst_IsEmpty(gn
->commands
))
2142 SuffAddLevel(srcs
, targ
);
2145 fprintf(debug_file
, "not ");
2149 fprintf(debug_file
, "adding suffix rules\n");
2151 (void)Lst_AtEnd(targs
, targ
);
2155 * Using the list of possible sources built up from the target suffix(es),
2156 * try and find an existing file/target that matches.
2158 bottom
= SuffFindThem(srcs
, slst
);
2160 if (bottom
== NULL
) {
2162 * No known transformations -- use the first suffix found for setting
2163 * the local variables.
2165 if (!Lst_IsEmpty(targs
)) {
2166 targ
= (Src
*)Lst_Datum(Lst_First(targs
));
2172 * Work up the transformation path to find the suffix of the
2173 * target to which the transformation was made.
2175 for (targ
= bottom
; targ
->parent
!= NULL
; targ
= targ
->parent
)
2180 Var_Set(TARGET
, gn
->path
? gn
->path
: gn
->name
, gn
, 0);
2182 pref
= (targ
!= NULL
) ? targ
->pref
: gn
->name
;
2183 Var_Set(PREFIX
, pref
, gn
, 0);
2186 * Now we've got the important local variables set, expand any sources
2187 * that still contain variables or wildcards in their names.
2189 for (ln
= Lst_First(gn
->children
); ln
!= NULL
; ln
= nln
) {
2191 SuffExpandChildren(ln
, gn
);
2196 fprintf(debug_file
, "\tNo valid suffix on %s\n", gn
->name
);
2201 * Deal with finding the thing on the default search path. We
2202 * always do that, not only if the node is only a source (not
2203 * on the lhs of a dependency operator or [XXX] it has neither
2204 * children or commands) as the old pmake did.
2206 if ((gn
->type
& (OP_PHONY
|OP_NOPATH
)) == 0) {
2208 gn
->path
= Dir_FindFile(gn
->name
,
2209 (targ
== NULL
? dirSearchPath
:
2210 targ
->suff
->searchPath
));
2211 if (gn
->path
!= NULL
) {
2213 Var_Set(TARGET
, gn
->path
, gn
, 0);
2217 * Suffix known for the thing -- trim the suffix off
2218 * the path to form the proper .PREFIX variable.
2220 int savep
= strlen(gn
->path
) - targ
->suff
->nameLen
;
2224 gn
->suffix
->refCount
--;
2225 gn
->suffix
= targ
->suff
;
2226 gn
->suffix
->refCount
++;
2228 savec
= gn
->path
[savep
];
2229 gn
->path
[savep
] = '\0';
2231 if ((ptr
= strrchr(gn
->path
, '/')) != NULL
)
2236 Var_Set(PREFIX
, ptr
, gn
, 0);
2238 gn
->path
[savep
] = savec
;
2241 * The .PREFIX gets the full path if the target has
2245 gn
->suffix
->refCount
--;
2248 if ((ptr
= strrchr(gn
->path
, '/')) != NULL
)
2253 Var_Set(PREFIX
, ptr
, gn
, 0);
2262 * If the suffix indicates that the target is a library, mark that in
2263 * the node's type field.
2265 if (targ
->suff
->flags
& SUFF_LIBRARY
) {
2270 * Check for overriding transformation rule implied by sources
2272 if (!Lst_IsEmpty(gn
->children
)) {
2273 src
= SuffFindCmds(targ
, slst
);
2277 * Free up all the Src structures in the transformation path
2278 * up to, but not including, the parent node.
2280 while (bottom
&& bottom
->parent
!= NULL
) {
2281 if (Lst_Member(slst
, bottom
) == NULL
) {
2282 Lst_AtEnd(slst
, bottom
);
2284 bottom
= bottom
->parent
;
2290 if (bottom
== NULL
) {
2292 * No idea from where it can come -- return now.
2298 * We now have a list of Src structures headed by 'bottom' and linked via
2299 * their 'parent' pointers. What we do next is create links between
2300 * source and target nodes (which may or may not have been created)
2301 * and set the necessary local variables in each target. The
2302 * commands for each target are set from the commands of the
2303 * transformation rule used to get from the src suffix to the targ
2304 * suffix. Note that this causes the commands list of the original
2305 * node, gn, to be replaced by the commands of the final
2306 * transformation rule. Also, the unmade field of gn is incremented.
2309 if (bottom
->node
== NULL
) {
2310 bottom
->node
= Targ_FindNode(bottom
->file
, TARG_CREATE
);
2313 for (src
= bottom
; src
->parent
!= NULL
; src
= src
->parent
) {
2316 if (src
->node
->suffix
)
2317 src
->node
->suffix
->refCount
--;
2318 src
->node
->suffix
= src
->suff
;
2319 src
->node
->suffix
->refCount
++;
2321 if (targ
->node
== NULL
) {
2322 targ
->node
= Targ_FindNode(targ
->file
, TARG_CREATE
);
2325 SuffApplyTransform(targ
->node
, src
->node
,
2326 targ
->suff
, src
->suff
);
2328 if (targ
->node
!= gn
) {
2330 * Finish off the dependency-search process for any nodes
2331 * between bottom and gn (no point in questing around the
2332 * filesystem for their implicit source when it's already
2333 * known). Note that the node can't have any sources that
2334 * need expanding, since SuffFindThem will stop on an existing
2335 * node, so all we need to do is set the standard and System V
2338 targ
->node
->type
|= OP_DEPS_FOUND
;
2340 Var_Set(PREFIX
, targ
->pref
, targ
->node
, 0);
2342 Var_Set(TARGET
, targ
->node
->name
, targ
->node
, 0);
2347 gn
->suffix
->refCount
--;
2348 gn
->suffix
= src
->suff
;
2349 gn
->suffix
->refCount
++;
2352 * Nuke the transformation path and the Src structures left over in the
2357 if (Lst_Member(slst
, bottom
) == NULL
)
2358 Lst_AtEnd(slst
, bottom
);
2360 while (SuffRemoveSrc(srcs
) || SuffRemoveSrc(targs
))
2363 Lst_Concat(slst
, srcs
, LST_CONCLINK
);
2364 Lst_Concat(slst
, targs
, LST_CONCLINK
);
2369 *-----------------------------------------------------------------------
2371 * Find implicit sources for the target described by the graph node
2378 * Nodes are added to the graph below the passed-in node. The nodes
2379 * are marked to have their IMPSRC variable filled in. The
2380 * PREFIX variable is set for the given node and all its
2384 * The path found by this target is the shortest path in the
2385 * transformation graph, which may pass through non-existent targets,
2386 * to an existing target. The search continues on all paths from the
2387 * root suffix until a file is found. I.e. if there's a path
2388 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2389 * the .c and .l files don't, the search will branch out in
2390 * all directions from .o and again from all the nodes on the
2391 * next level until the .l,v node is encountered.
2393 *-----------------------------------------------------------------------
2397 Suff_FindDeps(GNode
*gn
)
2400 SuffFindDeps(gn
, srclist
);
2401 while (SuffRemoveSrc(srclist
))
2408 * gn node we're dealing with
2412 SuffFindDeps(GNode
*gn
, Lst slst
)
2414 if (gn
->type
& OP_DEPS_FOUND
) {
2416 * If dependencies already found, no need to do it again...
2420 gn
->type
|= OP_DEPS_FOUND
;
2423 * Make sure we have these set, may get revised below.
2425 Var_Set(TARGET
, gn
->path
? gn
->path
: gn
->name
, gn
, 0);
2426 Var_Set(PREFIX
, gn
->name
, gn
, 0);
2429 fprintf(debug_file
, "SuffFindDeps (%s)\n", gn
->name
);
2432 if (gn
->type
& OP_ARCHV
) {
2433 SuffFindArchiveDeps(gn
, slst
);
2434 } else if (gn
->type
& OP_LIB
) {
2436 * If the node is a library, it is the arch module's job to find it
2437 * and set the TARGET variable accordingly. We merely provide the
2438 * search path, assuming all libraries end in ".a" (if the suffix
2439 * hasn't been defined, there's nothing we can do for it, so we just
2440 * set the TARGET variable to the node's name in order to give it a
2446 ln
= Lst_Find(sufflist
, LIBSUFF
, SuffSuffHasNameP
);
2448 gn
->suffix
->refCount
--;
2450 gn
->suffix
= s
= (Suff
*)Lst_Datum(ln
);
2451 gn
->suffix
->refCount
++;
2452 Arch_FindLib(gn
, s
->searchPath
);
2455 Var_Set(TARGET
, gn
->name
, gn
, 0);
2458 * Because a library (-lfoo) target doesn't follow the standard
2459 * filesystem conventions, we don't set the regular variables for
2460 * the thing. .PREFIX is simply made empty...
2462 Var_Set(PREFIX
, "", gn
, 0);
2464 SuffFindNormalDeps(gn
, slst
);
2469 *-----------------------------------------------------------------------
2471 * Define which suffix is the null suffix.
2474 * name Name of null suffix
2480 * 'suffNull' is altered.
2483 * Need to handle the changing of the null suffix gracefully so the
2484 * old transformation rules don't just go away.
2486 *-----------------------------------------------------------------------
2489 Suff_SetNull(char *name
)
2494 ln
= Lst_Find(sufflist
, name
, SuffSuffHasNameP
);
2496 s
= (Suff
*)Lst_Datum(ln
);
2497 if (suffNull
!= NULL
) {
2498 suffNull
->flags
&= ~SUFF_NULL
;
2500 s
->flags
|= SUFF_NULL
;
2502 * XXX: Here's where the transformation mangling would take place
2506 Parse_Error(PARSE_WARNING
, "Desired null suffix %s not defined.",
2512 *-----------------------------------------------------------------------
2514 * Initialize suffixes module
2521 *-----------------------------------------------------------------------
2526 sufflist
= Lst_Init(FALSE
);
2528 suffClean
= Lst_Init(FALSE
);
2530 srclist
= Lst_Init(FALSE
);
2531 transforms
= Lst_Init(FALSE
);
2535 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2536 * actually go on the suffix list or everyone will think that's its
2539 emptySuff
= suffNull
= bmake_malloc(sizeof(Suff
));
2541 suffNull
->name
= bmake_strdup("");
2542 suffNull
->nameLen
= 0;
2543 suffNull
->searchPath
= Lst_Init(FALSE
);
2544 Dir_Concat(suffNull
->searchPath
, dirSearchPath
);
2545 suffNull
->children
= Lst_Init(FALSE
);
2546 suffNull
->parents
= Lst_Init(FALSE
);
2547 suffNull
->ref
= Lst_Init(FALSE
);
2548 suffNull
->sNum
= sNum
++;
2549 suffNull
->flags
= SUFF_NULL
;
2550 suffNull
->refCount
= 1;
2556 *----------------------------------------------------------------------
2558 * Cleanup the this module
2564 * The memory is free'd.
2565 *----------------------------------------------------------------------
2572 Lst_Destroy(sufflist
, SuffFree
);
2573 Lst_Destroy(suffClean
, SuffFree
);
2576 Lst_Destroy(srclist
, NULL
);
2577 Lst_Destroy(transforms
, NULL
);
2582 /********************* DEBUGGING FUNCTIONS **********************/
2584 static int SuffPrintName(void *s
, void *dummy
)
2586 fprintf(debug_file
, "%s ", ((Suff
*)s
)->name
);
2587 return (dummy
? 0 : 0);
2591 SuffPrintSuff(void *sp
, void *dummy
)
2593 Suff
*s
= (Suff
*)sp
;
2597 fprintf(debug_file
, "# `%s' [%d] ", s
->name
, s
->refCount
);
2601 fputs(" (", debug_file
);
2603 flag
= 1 << (ffs(flags
) - 1);
2607 fprintf(debug_file
, "NULL");
2610 fprintf(debug_file
, "INCLUDE");
2613 fprintf(debug_file
, "LIBRARY");
2616 fputc(flags
? '|' : ')', debug_file
);
2619 fputc('\n', debug_file
);
2620 fprintf(debug_file
, "#\tTo: ");
2621 Lst_ForEach(s
->parents
, SuffPrintName
, NULL
);
2622 fputc('\n', debug_file
);
2623 fprintf(debug_file
, "#\tFrom: ");
2624 Lst_ForEach(s
->children
, SuffPrintName
, NULL
);
2625 fputc('\n', debug_file
);
2626 fprintf(debug_file
, "#\tSearch Path: ");
2627 Dir_PrintPath(s
->searchPath
);
2628 fputc('\n', debug_file
);
2629 return (dummy
? 0 : 0);
2633 SuffPrintTrans(void *tp
, void *dummy
)
2635 GNode
*t
= (GNode
*)tp
;
2637 fprintf(debug_file
, "%-16s: ", t
->name
);
2638 Targ_PrintType(t
->type
);
2639 fputc('\n', debug_file
);
2640 Lst_ForEach(t
->commands
, Targ_PrintCmd
, NULL
);
2641 fputc('\n', debug_file
);
2642 return(dummy
? 0 : 0);
2648 fprintf(debug_file
, "#*** Suffixes:\n");
2649 Lst_ForEach(sufflist
, SuffPrintSuff
, NULL
);
2651 fprintf(debug_file
, "#*** Transformations:\n");
2652 Lst_ForEach(transforms
, SuffPrintTrans
, NULL
);