Annotation of src/usr.bin/make/suff.c, Revision 1.28
1.28 ! espie 1: /* $OpenBSD: suff.c,v 1.27 2000/06/10 01:32:23 espie Exp $ */
1.5 millert 2: /* $NetBSD: suff.c,v 1.13 1996/11/06 17:59:25 christos Exp $ */
1.1 deraadt 3:
4: /*
1.5 millert 5: * Copyright (c) 1988, 1989, 1990, 1993
6: * The Regents of the University of California. All rights reserved.
1.1 deraadt 7: * Copyright (c) 1989 by Berkeley Softworks
8: * All rights reserved.
9: *
10: * This code is derived from software contributed to Berkeley by
11: * Adam de Boor.
12: *
13: * Redistribution and use in source and binary forms, with or without
14: * modification, are permitted provided that the following conditions
15: * are met:
16: * 1. Redistributions of source code must retain the above copyright
17: * notice, this list of conditions and the following disclaimer.
18: * 2. Redistributions in binary form must reproduce the above copyright
19: * notice, this list of conditions and the following disclaimer in the
20: * documentation and/or other materials provided with the distribution.
21: * 3. All advertising materials mentioning features or use of this software
22: * must display the following acknowledgement:
23: * This product includes software developed by the University of
24: * California, Berkeley and its contributors.
25: * 4. Neither the name of the University nor the names of its contributors
26: * may be used to endorse or promote products derived from this software
27: * without specific prior written permission.
28: *
29: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39: * SUCH DAMAGE.
40: */
41:
42: #ifndef lint
43: #if 0
1.5 millert 44: static char sccsid[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94";
1.1 deraadt 45: #else
1.28 ! espie 46: static char rcsid[] = "$OpenBSD: suff.c,v 1.27 2000/06/10 01:32:23 espie Exp $";
1.1 deraadt 47: #endif
48: #endif /* not lint */
49:
50: /*-
51: * suff.c --
52: * Functions to maintain suffix lists and find implicit dependents
53: * using suffix transformation rules
54: *
55: * Interface:
56: * Suff_Init Initialize all things to do with suffixes.
57: *
58: * Suff_End Cleanup the module
59: *
60: * Suff_DoPaths This function is used to make life easier
61: * when searching for a file according to its
62: * suffix. It takes the global search path,
63: * as defined using the .PATH: target, and appends
64: * its directories to the path of each of the
65: * defined suffixes, as specified using
66: * .PATH<suffix>: targets. In addition, all
67: * directories given for suffixes labeled as
68: * include files or libraries, using the .INCLUDES
69: * or .LIBS targets, are played with using
70: * Dir_MakeFlags to create the .INCLUDES and
71: * .LIBS global variables.
72: *
73: * Suff_ClearSuffixes Clear out all the suffixes and defined
74: * transformations.
75: *
76: * Suff_IsTransform Return TRUE if the passed string is the lhs
77: * of a transformation rule.
78: *
79: * Suff_AddSuffix Add the passed string as another known suffix.
80: *
81: * Suff_GetPath Return the search path for the given suffix.
82: *
83: * Suff_AddInclude Mark the given suffix as denoting an include
84: * file.
85: *
86: * Suff_AddLib Mark the given suffix as denoting a library.
87: *
88: * Suff_AddTransform Add another transformation to the suffix
89: * graph. Returns GNode suitable for framing, I
90: * mean, tacking commands, attributes, etc. on.
91: *
92: * Suff_SetNull Define the suffix to consider the suffix of
93: * any file that doesn't have a known one.
94: *
95: * Suff_FindDeps Find implicit sources for and the location of
96: * a target based on its suffix. Returns the
1.18 espie 97: * bottom-most node added to the graph or NULL
1.1 deraadt 98: * if the target had no implicit sources.
99: */
100:
101: #include <stdio.h>
102: #include "make.h"
103: #include "hash.h"
104: #include "dir.h"
105:
106: static Lst sufflist; /* Lst of suffixes */
1.14 espie 107: #ifdef CLEANUP
1.1 deraadt 108: static Lst suffClean; /* Lst of suffixes to be cleaned */
1.14 espie 109: #endif
1.1 deraadt 110: static Lst srclist; /* Lst of sources */
111: static Lst transforms; /* Lst of transformation rules */
112:
113: static int sNum = 0; /* Counter for assigning suffix numbers */
114:
115: /*
116: * Structure describing an individual suffix.
117: */
118: typedef struct _Suff {
119: char *name; /* The suffix itself */
120: int nameLen; /* Length of the suffix */
121: short flags; /* Type of suffix */
122: #define SUFF_INCLUDE 0x01 /* One which is #include'd */
123: #define SUFF_LIBRARY 0x02 /* One which contains a library */
124: #define SUFF_NULL 0x04 /* The empty suffix */
125: Lst searchPath; /* The path along which files of this suffix
126: * may be found */
127: int sNum; /* The suffix number */
128: Lst parents; /* Suffixes we have a transformation to */
129: Lst children; /* Suffixes we have a transformation from */
130: Lst ref; /* List of lists this suffix is referenced */
131: } Suff;
132:
133: /*
134: * Structure used in the search for implied sources.
135: */
136: typedef struct _Src {
137: char *file; /* The file to look for */
138: char *pref; /* Prefix from which file was formed */
139: Suff *suff; /* The suffix on the file */
140: struct _Src *parent; /* The Src for which this is a source */
141: GNode *node; /* The node describing the file */
142: int children; /* Count of existing children (so we don't free
143: * this thing too early or never nuke it) */
144: #ifdef DEBUG_SRC
145: Lst cp; /* Debug; children list */
146: #endif
147: } Src;
148:
149: /*
150: * A structure for passing more than one argument to the Lst-library-invoked
151: * function...
152: */
153: typedef struct {
154: Lst l;
155: Src *s;
156: } LstSrc;
157:
158: static Suff *suffNull; /* The NULL suffix for this run */
159: static Suff *emptySuff; /* The empty suffix required for POSIX
160: * single-suffix transformation rules */
161:
162:
163: static char *SuffStrIsPrefix __P((char *, char *));
164: static char *SuffSuffIsSuffix __P((Suff *, char *));
1.28 ! espie 165: static int SuffSuffIsSuffixP __P((void *, void *));
! 166: static int SuffSuffHasNameP __P((void *, void *));
! 167: static int SuffSuffIsPrefix __P((void *, void *));
! 168: static int SuffGNHasNameP __P((void *, void *));
! 169: static void SuffUnRef __P((void *, void *));
! 170: static void SuffFree __P((void *));
1.1 deraadt 171: static void SuffInsert __P((Lst, Suff *));
1.24 espie 172: static void SuffRemove __P((Lst, Suff *));
1.1 deraadt 173: static Boolean SuffParseTransform __P((char *, Suff **, Suff **));
1.28 ! espie 174: static void SuffRebuildGraph __P((void *, void *));
! 175: static void SuffAddSrc __P((void *, void *));
1.1 deraadt 176: static int SuffRemoveSrc __P((Lst));
177: static void SuffAddLevel __P((Lst, Src *));
178: static Src *SuffFindThem __P((Lst, Lst));
179: static Src *SuffFindCmds __P((Src *, Lst));
1.28 ! espie 180: static void SuffExpandChildren __P((void *, void *));
1.1 deraadt 181: static Boolean SuffApplyTransform __P((GNode *, GNode *, Suff *, Suff *));
182: static void SuffFindDeps __P((GNode *, Lst));
183: static void SuffFindArchiveDeps __P((GNode *, Lst));
184: static void SuffFindNormalDeps __P((GNode *, Lst));
1.28 ! espie 185: static void SuffPrintName __P((void *));
! 186: static void SuffPrintSuff __P((void *));
! 187: static void SuffPrintTrans __P((void *));
1.1 deraadt 188:
189: /*************** Lst Predicates ****************/
190: /*-
191: *-----------------------------------------------------------------------
192: * SuffStrIsPrefix --
193: * See if pref is a prefix of str.
194: *
195: * Results:
196: * NULL if it ain't, pointer to character in str after prefix if so
197: *
198: * Side Effects:
199: * None
200: *-----------------------------------------------------------------------
201: */
202: static char *
203: SuffStrIsPrefix (pref, str)
204: register char *pref; /* possible prefix */
205: register char *str; /* string to check */
206: {
207: while (*str && *pref == *str) {
208: pref++;
209: str++;
210: }
211:
212: return (*pref ? NULL : str);
213: }
214:
215: /*-
216: *-----------------------------------------------------------------------
217: * SuffSuffIsSuffix --
218: * See if suff is a suffix of str. Str should point to THE END of the
219: * string to check. (THE END == the null byte)
220: *
221: * Results:
222: * NULL if it ain't, pointer to character in str before suffix if
223: * it is.
224: *
225: * Side Effects:
226: * None
227: *-----------------------------------------------------------------------
228: */
229: static char *
230: SuffSuffIsSuffix (s, str)
231: register Suff *s; /* possible suffix */
232: char *str; /* string to examine */
233: {
234: register char *p1; /* Pointer into suffix name */
235: register char *p2; /* Pointer into string being examined */
236:
237: p1 = s->name + s->nameLen;
238: p2 = str;
239:
240: while (p1 >= s->name && *p1 == *p2) {
241: p1--;
242: p2--;
243: }
244:
245: return (p1 == s->name - 1 ? p2 : NULL);
246: }
247:
248: /*-
249: *-----------------------------------------------------------------------
250: * SuffSuffIsSuffixP --
251: * Predicate form of SuffSuffIsSuffix. Passed as the callback function
252: * to Lst_Find.
253: *
254: * Results:
255: * 0 if the suffix is the one desired, non-zero if not.
256: *
257: * Side Effects:
258: * None.
259: *
260: *-----------------------------------------------------------------------
261: */
262: static int
263: SuffSuffIsSuffixP(s, str)
1.28 ! espie 264: void *s;
! 265: void *str;
1.1 deraadt 266: {
1.28 ! espie 267: return !SuffSuffIsSuffix((Suff *)s, (char *)str);
1.1 deraadt 268: }
269:
270: /*-
271: *-----------------------------------------------------------------------
272: * SuffSuffHasNameP --
273: * Callback procedure for finding a suffix based on its name. Used by
274: * Suff_GetPath.
275: *
276: * Results:
277: * 0 if the suffix is of the given name. non-zero otherwise.
278: *
279: * Side Effects:
280: * None
281: *-----------------------------------------------------------------------
282: */
283: static int
284: SuffSuffHasNameP (s, sname)
1.28 ! espie 285: void *s; /* Suffix to check */
! 286: void *sname; /* Desired name */
1.1 deraadt 287: {
288: return (strcmp ((char *) sname, ((Suff *) s)->name));
289: }
290:
291: /*-
292: *-----------------------------------------------------------------------
293: * SuffSuffIsPrefix --
294: * See if the suffix described by s is a prefix of the string. Care
295: * must be taken when using this to search for transformations and
296: * what-not, since there could well be two suffixes, one of which
297: * is a prefix of the other...
298: *
299: * Results:
300: * 0 if s is a prefix of str. non-zero otherwise
301: *
302: * Side Effects:
303: * None
304: *-----------------------------------------------------------------------
305: */
306: static int
1.28 ! espie 307: SuffSuffIsPrefix(s, str)
! 308: void *s; /* suffix to compare */
! 309: void *str; /* string to examine */
1.1 deraadt 310: {
1.28 ! espie 311: return SuffStrIsPrefix (((Suff *)s)->name, (char *)str) == NULL ? 1 : 0;
1.1 deraadt 312: }
313:
314: /*-
315: *-----------------------------------------------------------------------
316: * SuffGNHasNameP --
317: * See if the graph node has the desired name
318: *
319: * Results:
320: * 0 if it does. non-zero if it doesn't
321: *
322: * Side Effects:
323: * None
324: *-----------------------------------------------------------------------
325: */
326: static int
1.28 ! espie 327: SuffGNHasNameP(gn, name)
! 328: void *gn; /* current node we're looking at */
! 329: void *name; /* name we're looking for */
1.1 deraadt 330: {
1.28 ! espie 331: return strcmp((char *)name, ((GNode *)gn)->name);
1.1 deraadt 332: }
333:
334: /*********** Maintenance Functions ************/
335:
336: static void
337: SuffUnRef(lp, sp)
1.28 ! espie 338: void *lp;
! 339: void *sp;
1.1 deraadt 340: {
341: Lst l = (Lst) lp;
342:
343: LstNode ln = Lst_Member(l, sp);
1.24 espie 344: if (ln != NULL)
1.1 deraadt 345: Lst_Remove(l, ln);
346: }
347:
348: /*-
349: *-----------------------------------------------------------------------
350: * SuffFree --
351: * Free up all memory associated with the given suffix structure.
352: *
353: * Results:
354: * none
355: *
356: * Side Effects:
357: * the suffix entry is detroyed
358: *-----------------------------------------------------------------------
359: */
360: static void
1.28 ! espie 361: SuffFree(sp)
! 362: void *sp;
1.1 deraadt 363: {
364: Suff *s = (Suff *) sp;
365:
366: if (s == suffNull)
367: suffNull = NULL;
368:
369: if (s == emptySuff)
370: emptySuff = NULL;
371:
1.27 espie 372: Lst_Destroy(s->ref, NOFREE);
373: Lst_Destroy(s->children, NOFREE);
374: Lst_Destroy(s->parents, NOFREE);
375: Lst_Destroy(s->searchPath, Dir_Destroy);
1.1 deraadt 376:
1.28 ! espie 377: free(s->name);
! 378: free(s);
1.1 deraadt 379: }
380:
381: /*-
382: *-----------------------------------------------------------------------
383: * SuffRemove --
1.24 espie 384: * Remove the suffix from the list
1.1 deraadt 385: *-----------------------------------------------------------------------
386: */
1.24 espie 387: static void
1.1 deraadt 388: SuffRemove(l, s)
389: Lst l;
390: Suff *s;
391: {
1.25 espie 392: SuffUnRef(l, s);
1.1 deraadt 393: }
394:
395: /*-
396: *-----------------------------------------------------------------------
397: * SuffInsert --
398: * Insert the suffix into the list keeping the list ordered by suffix
399: * numbers.
400: *
401: * Results:
402: * None
403: *
404: * Side Effects:
405: * The reference count of the suffix is incremented
406: *-----------------------------------------------------------------------
407: */
408: static void
409: SuffInsert (l, s)
410: Lst l; /* the list where in s should be inserted */
411: Suff *s; /* the suffix to insert */
412: {
413: LstNode ln; /* current element in l we're examining */
414: Suff *s2 = NULL; /* the suffix descriptor in this element */
415:
416: if (Lst_Open (l) == FAILURE) {
417: return;
418: }
1.18 espie 419: while ((ln = Lst_Next (l)) != NULL) {
1.1 deraadt 420: s2 = (Suff *) Lst_Datum (ln);
421: if (s2->sNum >= s->sNum) {
422: break;
423: }
424: }
425:
426: Lst_Close (l);
427: if (DEBUG(SUFF)) {
428: printf("inserting %s(%d)...", s->name, s->sNum);
429: }
1.18 espie 430: if (ln == NULL) {
1.1 deraadt 431: if (DEBUG(SUFF)) {
432: printf("at end of list\n");
433: }
1.25 espie 434: Lst_AtEnd(l, s);
435: Lst_AtEnd(s->ref, l);
1.1 deraadt 436: } else if (s2->sNum != s->sNum) {
437: if (DEBUG(SUFF)) {
438: printf("before %s(%d)\n", s2->name, s2->sNum);
439: }
1.25 espie 440: Lst_Insert(l, ln, s);
441: Lst_AtEnd(s->ref, l);
1.1 deraadt 442: } else if (DEBUG(SUFF)) {
443: printf("already there\n");
444: }
445: }
446:
447: /*-
448: *-----------------------------------------------------------------------
449: * Suff_ClearSuffixes --
450: * This is gross. Nuke the list of suffixes but keep all transformation
451: * rules around. The transformation graph is destroyed in this process,
452: * but we leave the list of rules so when a new graph is formed the rules
453: * will remain.
454: * This function is called from the parse module when a
455: * .SUFFIXES:\n line is encountered.
456: *
457: * Results:
458: * none
459: *
460: * Side Effects:
461: * the sufflist and its graph nodes are destroyed
462: *-----------------------------------------------------------------------
463: */
464: void
465: Suff_ClearSuffixes ()
466: {
1.14 espie 467: #ifdef CLEANUP
1.20 espie 468: Lst_Concat(suffClean, sufflist, LST_CONCLINK);
1.14 espie 469: #endif
1.17 espie 470: sufflist = Lst_Init();
1.1 deraadt 471: sNum = 0;
472: suffNull = emptySuff;
473: }
474:
475: /*-
476: *-----------------------------------------------------------------------
477: * SuffParseTransform --
478: * Parse a transformation string to find its two component suffixes.
479: *
480: * Results:
481: * TRUE if the string is a valid transformation and FALSE otherwise.
482: *
483: * Side Effects:
484: * The passed pointers are overwritten.
485: *
486: *-----------------------------------------------------------------------
487: */
488: static Boolean
489: SuffParseTransform(str, srcPtr, targPtr)
490: char *str; /* String being parsed */
491: Suff **srcPtr; /* Place to store source of trans. */
492: Suff **targPtr; /* Place to store target of trans. */
493: {
494: register LstNode srcLn; /* element in suffix list of trans source*/
495: register Suff *src; /* Source of transformation */
496: register LstNode targLn; /* element in suffix list of trans target*/
497: register char *str2; /* Extra pointer (maybe target suffix) */
498: LstNode singleLn; /* element in suffix list of any suffix
499: * that exactly matches str */
500: Suff *single = NULL;/* Source of possible transformation to
501: * null suffix */
502:
1.18 espie 503: srcLn = NULL;
504: singleLn = NULL;
1.5 millert 505:
1.1 deraadt 506: /*
507: * Loop looking first for a suffix that matches the start of the
508: * string and then for one that exactly matches the rest of it. If
509: * we can find two that meet these criteria, we've successfully
510: * parsed the string.
511: */
512: for (;;) {
1.18 espie 513: if (srcLn == NULL) {
1.25 espie 514: srcLn = Lst_Find(sufflist, SuffSuffIsPrefix, str);
1.1 deraadt 515: } else {
1.21 espie 516: srcLn = Lst_FindFrom(Lst_Succ(srcLn),
1.25 espie 517: SuffSuffIsPrefix, str);
1.1 deraadt 518: }
1.18 espie 519: if (srcLn == NULL) {
1.1 deraadt 520: /*
521: * Ran out of source suffixes -- no such rule
522: */
1.18 espie 523: if (singleLn != NULL) {
1.1 deraadt 524: /*
525: * Not so fast Mr. Smith! There was a suffix that encompassed
526: * the entire string, so we assume it was a transformation
527: * to the null suffix (thank you POSIX). We still prefer to
528: * find a double rule over a singleton, hence we leave this
529: * check until the end.
530: *
531: * XXX: Use emptySuff over suffNull?
532: */
533: *srcPtr = single;
534: *targPtr = suffNull;
535: return(TRUE);
536: }
537: return (FALSE);
538: }
539: src = (Suff *) Lst_Datum (srcLn);
540: str2 = str + src->nameLen;
541: if (*str2 == '\0') {
542: single = src;
543: singleLn = srcLn;
544: } else {
1.25 espie 545: targLn = Lst_Find(sufflist, SuffSuffHasNameP, str2);
1.18 espie 546: if (targLn != NULL) {
1.1 deraadt 547: *srcPtr = src;
548: *targPtr = (Suff *)Lst_Datum(targLn);
549: return (TRUE);
550: }
551: }
552: }
553: }
554:
555: /*-
556: *-----------------------------------------------------------------------
557: * Suff_IsTransform --
558: * Return TRUE if the given string is a transformation rule
559: *
560: *
561: * Results:
562: * TRUE if the string is a concatenation of two known suffixes.
563: * FALSE otherwise
564: *
565: * Side Effects:
566: * None
567: *-----------------------------------------------------------------------
568: */
569: Boolean
570: Suff_IsTransform (str)
571: char *str; /* string to check */
572: {
573: Suff *src, *targ;
574:
575: return (SuffParseTransform(str, &src, &targ));
576: }
577:
578: /*-
579: *-----------------------------------------------------------------------
580: * Suff_AddTransform --
581: * Add the transformation rule described by the line to the
582: * list of rules and place the transformation itself in the graph
583: *
584: * Results:
585: * The node created for the transformation in the transforms list
586: *
587: * Side Effects:
588: * The node is placed on the end of the transforms Lst and links are
589: * made between the two suffixes mentioned in the target name
590: *-----------------------------------------------------------------------
591: */
592: GNode *
593: Suff_AddTransform (line)
594: char *line; /* name of transformation to add */
595: {
596: GNode *gn; /* GNode of transformation rule */
597: Suff *s, /* source suffix */
598: *t; /* target suffix */
599: LstNode ln; /* Node for existing transformation */
600:
1.25 espie 601: ln = Lst_Find(transforms, SuffGNHasNameP, line);
1.18 espie 602: if (ln == NULL) {
1.1 deraadt 603: /*
604: * Make a new graph node for the transformation. It will be filled in
1.5 millert 605: * by the Parse module.
1.1 deraadt 606: */
607: gn = Targ_NewGN (line);
1.25 espie 608: Lst_AtEnd(transforms, gn);
1.1 deraadt 609: } else {
610: /*
611: * New specification for transformation rule. Just nuke the old list
612: * of commands so they can be filled in again... We don't actually
613: * free the commands themselves, because a given command can be
614: * attached to several different transformations.
615: */
616: gn = (GNode *) Lst_Datum (ln);
1.27 espie 617: Lst_Destroy(gn->commands, NOFREE);
618: Lst_Destroy(gn->children, NOFREE);
1.17 espie 619: gn->commands = Lst_Init();
620: gn->children = Lst_Init();
1.1 deraadt 621: }
622:
623: gn->type = OP_TRANSFORM;
624:
625: (void)SuffParseTransform(line, &s, &t);
626:
627: /*
1.5 millert 628: * link the two together in the proper relationship and order
1.1 deraadt 629: */
630: if (DEBUG(SUFF)) {
631: printf("defining transformation from `%s' to `%s'\n",
632: s->name, t->name);
633: }
634: SuffInsert (t->children, s);
635: SuffInsert (s->parents, t);
636:
637: return (gn);
638: }
639:
640: /*-
641: *-----------------------------------------------------------------------
642: * Suff_EndTransform --
643: * Handle the finish of a transformation definition, removing the
644: * transformation from the graph if it has neither commands nor
645: * sources. This is a callback procedure for the Parse module via
646: * Lst_ForEach
647: *
648: * Side Effects:
649: * If the node has no commands or children, the children and parents
650: * lists of the affected suffices are altered.
651: *
652: *-----------------------------------------------------------------------
653: */
1.27 espie 654: void
655: Suff_EndTransform(gnp)
1.28 ! espie 656: void *gnp; /* Node for transformation */
1.1 deraadt 657: {
1.27 espie 658: GNode *gn = (GNode *)gnp;
1.5 millert 659:
1.1 deraadt 660: if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
661: Lst_IsEmpty(gn->children))
662: {
663: Suff *s, *t;
664:
665: (void)SuffParseTransform(gn->name, &s, &t);
666:
667: if (DEBUG(SUFF)) {
1.8 millert 668: printf("deleting transformation from `%s' to `%s'\n",
1.1 deraadt 669: s->name, t->name);
670: }
671:
672: /*
1.24 espie 673: * Remove the source from the target's children list.
1.1 deraadt 674: *
675: * We'll be called twice when the next target is seen, but .c and .o
676: * are only linked once...
677: */
1.24 espie 678: SuffRemove(t->children, s);
1.1 deraadt 679:
680: /*
681: * Remove the target from the source's parents list
682: */
1.24 espie 683: SuffRemove(s->parents, t);
1.1 deraadt 684: } else if ((gn->type & OP_TRANSFORM) && DEBUG(SUFF)) {
685: printf("transformation %s complete\n", gn->name);
686: }
687: }
688:
689: /*-
690: *-----------------------------------------------------------------------
691: * SuffRebuildGraph --
692: * Called from Suff_AddSuffix via Lst_ForEach to search through the
693: * list of existing transformation rules and rebuild the transformation
694: * graph when it has been destroyed by Suff_ClearSuffixes. If the
695: * given rule is a transformation involving this suffix and another,
696: * existing suffix, the proper relationship is established between
697: * the two.
698: *
699: * Side Effects:
700: * The appropriate links will be made between this suffix and
701: * others if transformation rules exist for it.
702: *
703: *-----------------------------------------------------------------------
704: */
1.27 espie 705: static void
1.1 deraadt 706: SuffRebuildGraph(transformp, sp)
1.28 ! espie 707: void *transformp; /* Transformation to test */
! 708: void *sp; /* Suffix to rebuild */
1.1 deraadt 709: {
1.27 espie 710: GNode *transform = (GNode *)transformp;
711: Suff *s = (Suff *)sp;
1.1 deraadt 712: char *cp;
713: LstNode ln;
714: Suff *s2;
715:
716: /*
717: * First see if it is a transformation from this suffix.
718: */
719: cp = SuffStrIsPrefix(s->name, transform->name);
1.27 espie 720: if (cp != NULL) {
1.25 espie 721: ln = Lst_Find(sufflist, SuffSuffHasNameP, cp);
1.18 espie 722: if (ln != NULL) {
1.1 deraadt 723: /*
724: * Found target. Link in and return, since it can't be anything
725: * else.
726: */
727: s2 = (Suff *)Lst_Datum(ln);
728: SuffInsert(s2->children, s);
729: SuffInsert(s->parents, s2);
1.27 espie 730: return;
1.1 deraadt 731: }
732: }
733:
734: /*
735: * Not from, maybe to?
736: */
737: cp = SuffSuffIsSuffix(s, transform->name + strlen(transform->name));
1.27 espie 738: if (cp != NULL) {
1.1 deraadt 739: /*
740: * Null-terminate the source suffix in order to find it.
741: */
742: cp[1] = '\0';
1.25 espie 743: ln = Lst_Find(sufflist, SuffSuffHasNameP, transform->name);
1.1 deraadt 744: /*
745: * Replace the start of the target suffix
746: */
747: cp[1] = s->name[0];
1.18 espie 748: if (ln != NULL) {
1.1 deraadt 749: /*
750: * Found it -- establish the proper relationship
751: */
752: s2 = (Suff *)Lst_Datum(ln);
753: SuffInsert(s->children, s2);
754: SuffInsert(s2->parents, s);
755: }
756: }
757: }
758:
759: /*-
760: *-----------------------------------------------------------------------
761: * Suff_AddSuffix --
762: * Add the suffix in string to the end of the list of known suffixes.
763: * Should we restructure the suffix graph? Make doesn't...
764: *
765: * Results:
766: * None
767: *
768: * Side Effects:
769: * A GNode is created for the suffix and a Suff structure is created and
770: * added to the suffixes list unless the suffix was already known.
771: *-----------------------------------------------------------------------
772: */
773: void
774: Suff_AddSuffix (str)
775: char *str; /* the name of the suffix to add */
776: {
777: Suff *s; /* new suffix descriptor */
778: LstNode ln;
779:
1.25 espie 780: ln = Lst_Find(sufflist, SuffSuffHasNameP, str);
1.18 espie 781: if (ln == NULL) {
1.1 deraadt 782: s = (Suff *) emalloc (sizeof (Suff));
783:
1.4 briggs 784: s->name = estrdup (str);
1.1 deraadt 785: s->nameLen = strlen (s->name);
1.17 espie 786: s->searchPath = Lst_Init();
787: s->children = Lst_Init();
788: s->parents = Lst_Init();
789: s->ref = Lst_Init();
1.1 deraadt 790: s->sNum = sNum++;
791: s->flags = 0;
792:
1.25 espie 793: Lst_AtEnd(sufflist, s);
1.1 deraadt 794: /*
795: * Look for any existing transformations from or to this suffix.
796: * XXX: Only do this after a Suff_ClearSuffixes?
797: */
1.25 espie 798: Lst_ForEach(transforms, SuffRebuildGraph, s);
1.5 millert 799: }
1.1 deraadt 800: }
801:
802: /*-
803: *-----------------------------------------------------------------------
804: * Suff_GetPath --
805: * Return the search path for the given suffix, if it's defined.
806: *
807: * Results:
1.18 espie 808: * The searchPath for the desired suffix or NULL if the suffix isn't
1.1 deraadt 809: * defined.
810: *
811: * Side Effects:
812: * None
813: *-----------------------------------------------------------------------
814: */
815: Lst
816: Suff_GetPath (sname)
817: char *sname;
818: {
819: LstNode ln;
820: Suff *s;
821:
1.25 espie 822: ln = Lst_Find(sufflist, SuffSuffHasNameP, sname);
1.18 espie 823: if (ln == NULL) {
824: return (NULL);
1.1 deraadt 825: } else {
826: s = (Suff *) Lst_Datum (ln);
827: return (s->searchPath);
828: }
829: }
830:
831: /*-
832: *-----------------------------------------------------------------------
833: * Suff_DoPaths --
834: * Extend the search paths for all suffixes to include the default
835: * search path.
836: *
837: * Results:
838: * None.
839: *
840: * Side Effects:
841: * The searchPath field of all the suffixes is extended by the
842: * directories in dirSearchPath. If paths were specified for the
843: * ".h" suffix, the directories are stuffed into a global variable
844: * called ".INCLUDES" with each directory preceeded by a -I. The same
845: * is done for the ".a" suffix, except the variable is called
846: * ".LIBS" and the flag is -L.
847: *-----------------------------------------------------------------------
848: */
849: void
850: Suff_DoPaths()
851: {
852: register Suff *s;
853: register LstNode ln;
854: char *ptr;
855: Lst inIncludes; /* Cumulative .INCLUDES path */
856: Lst inLibs; /* Cumulative .LIBS path */
857:
858: if (Lst_Open (sufflist) == FAILURE) {
859: return;
860: }
861:
1.17 espie 862: inIncludes = Lst_Init();
863: inLibs = Lst_Init();
1.1 deraadt 864:
1.18 espie 865: while ((ln = Lst_Next (sufflist)) != NULL) {
1.1 deraadt 866: s = (Suff *) Lst_Datum (ln);
867: if (!Lst_IsEmpty (s->searchPath)) {
868: #ifdef INCLUDES
869: if (s->flags & SUFF_INCLUDE) {
870: Dir_Concat(inIncludes, s->searchPath);
871: }
872: #endif /* INCLUDES */
873: #ifdef LIBRARIES
874: if (s->flags & SUFF_LIBRARY) {
875: Dir_Concat(inLibs, s->searchPath);
876: }
877: #endif /* LIBRARIES */
878: Dir_Concat(s->searchPath, dirSearchPath);
879: } else {
1.27 espie 880: Lst_Destroy(s->searchPath, Dir_Destroy);
1.1 deraadt 881: s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir);
882: }
883: }
884:
885: Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
886: free(ptr);
887: Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
888: free(ptr);
889:
890: Lst_Destroy(inIncludes, Dir_Destroy);
891: Lst_Destroy(inLibs, Dir_Destroy);
892:
893: Lst_Close (sufflist);
894: }
895:
896: /*-
897: *-----------------------------------------------------------------------
898: * Suff_AddInclude --
899: * Add the given suffix as a type of file which gets included.
900: * Called from the parse module when a .INCLUDES line is parsed.
901: * The suffix must have already been defined.
902: *
903: * Results:
904: * None.
905: *
906: * Side Effects:
907: * The SUFF_INCLUDE bit is set in the suffix's flags field
908: *
909: *-----------------------------------------------------------------------
910: */
911: void
912: Suff_AddInclude (sname)
913: char *sname; /* Name of suffix to mark */
914: {
915: LstNode ln;
916: Suff *s;
917:
1.25 espie 918: ln = Lst_Find(sufflist, SuffSuffHasNameP, sname);
1.18 espie 919: if (ln != NULL) {
1.1 deraadt 920: s = (Suff *) Lst_Datum (ln);
921: s->flags |= SUFF_INCLUDE;
922: }
923: }
924:
925: /*-
926: *-----------------------------------------------------------------------
927: * Suff_AddLib --
928: * Add the given suffix as a type of file which is a library.
929: * Called from the parse module when parsing a .LIBS line. The
930: * suffix must have been defined via .SUFFIXES before this is
931: * called.
932: *
933: * Results:
934: * None.
935: *
936: * Side Effects:
937: * The SUFF_LIBRARY bit is set in the suffix's flags field
938: *
939: *-----------------------------------------------------------------------
940: */
941: void
942: Suff_AddLib (sname)
943: char *sname; /* Name of suffix to mark */
944: {
945: LstNode ln;
946: Suff *s;
947:
1.25 espie 948: ln = Lst_Find(sufflist, SuffSuffHasNameP, sname);
1.18 espie 949: if (ln != NULL) {
1.1 deraadt 950: s = (Suff *) Lst_Datum (ln);
951: s->flags |= SUFF_LIBRARY;
952: }
953: }
954:
955: /********** Implicit Source Search Functions *********/
956:
957: /*-
958: *-----------------------------------------------------------------------
959: * SuffAddSrc --
960: * Add a suffix as a Src structure to the given list with its parent
961: * being the given Src structure. If the suffix is the null suffix,
962: * the prefix is used unaltered as the file name in the Src structure.
963: *
964: * Side Effects:
965: * A Src structure is created and tacked onto the end of the list
966: *-----------------------------------------------------------------------
967: */
1.27 espie 968: static void
969: SuffAddSrc(sp, lsp)
1.28 ! espie 970: void *sp; /* suffix for which to create a Src structure */
! 971: void *lsp; /* list and parent for the new Src */
1.1 deraadt 972: {
1.27 espie 973: Suff *s = (Suff *)sp;
974: LstSrc *ls = (LstSrc *)lsp;
1.1 deraadt 975: Src *s2; /* new Src structure */
976: Src *targ; /* Target structure */
977:
978: targ = ls->s;
1.5 millert 979:
1.1 deraadt 980: if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
981: /*
982: * If the suffix has been marked as the NULL suffix, also create a Src
983: * structure for a file with no suffix attached. Two birds, and all
984: * that...
985: */
986: s2 = (Src *) emalloc (sizeof (Src));
1.4 briggs 987: s2->file = estrdup(targ->pref);
1.1 deraadt 988: s2->pref = targ->pref;
989: s2->parent = targ;
1.18 espie 990: s2->node = NULL;
1.1 deraadt 991: s2->suff = s;
992: s2->children = 0;
993: targ->children += 1;
1.25 espie 994: Lst_AtEnd(ls->l, s2);
1.1 deraadt 995: #ifdef DEBUG_SRC
1.17 espie 996: s2->cp = Lst_Init();
1.25 espie 997: Lst_AtEnd(targ->cp, s2);
1.1 deraadt 998: printf("1 add %x %x to %x:", targ, s2, ls->l);
1.27 espie 999: Lst_Every(ls->l, PrintAddr);
1.1 deraadt 1000: printf("\n");
1001: #endif
1002: }
1003: s2 = (Src *) emalloc (sizeof (Src));
1004: s2->file = str_concat (targ->pref, s->name, 0);
1005: s2->pref = targ->pref;
1006: s2->parent = targ;
1.18 espie 1007: s2->node = NULL;
1.1 deraadt 1008: s2->suff = s;
1009: s2->children = 0;
1010: targ->children += 1;
1.25 espie 1011: Lst_AtEnd(ls->l, s2);
1.1 deraadt 1012: #ifdef DEBUG_SRC
1.17 espie 1013: s2->cp = Lst_Init();
1.25 espie 1014: Lst_AtEnd(targ->cp, s2);
1.1 deraadt 1015: printf("2 add %x %x to %x:", targ, s2, ls->l);
1.27 espie 1016: Lst_Every(ls->l, PrintAddr);
1.1 deraadt 1017: printf("\n");
1018: #endif
1019: }
1020:
1021: /*-
1022: *-----------------------------------------------------------------------
1023: * SuffAddLevel --
1024: * Add all the children of targ as Src structures to the given list
1025: *
1026: * Results:
1027: * None
1028: *
1029: * Side Effects:
1030: * Lots of structures are created and added to the list
1031: *-----------------------------------------------------------------------
1032: */
1033: static void
1034: SuffAddLevel (l, targ)
1035: Lst l; /* list to which to add the new level */
1036: Src *targ; /* Src structure to use as the parent */
1037: {
1038: LstSrc ls;
1039:
1040: ls.s = targ;
1041: ls.l = l;
1042:
1.27 espie 1043: Lst_ForEach(targ->suff->children, SuffAddSrc, &ls);
1.1 deraadt 1044: }
1045:
1046: /*-
1047: *----------------------------------------------------------------------
1048: * SuffRemoveSrc --
1049: * Free all src structures in list that don't have a reference count
1050: *
1051: * Results:
1.26 espie 1052: * True if an src was removed
1.1 deraadt 1053: *
1054: * Side Effects:
1055: * The memory is free'd.
1056: *----------------------------------------------------------------------
1057: */
1058: static int
1059: SuffRemoveSrc (l)
1060: Lst l;
1061: {
1062: LstNode ln;
1063: Src *s;
1064: int t = 0;
1065:
1066: if (Lst_Open (l) == FAILURE) {
1067: return 0;
1068: }
1069: #ifdef DEBUG_SRC
1070: printf("cleaning %lx: ", (unsigned long) l);
1.27 espie 1071: Lst_Every(l, PrintAddr);
1.1 deraadt 1072: printf("\n");
1073: #endif
1074:
1075:
1.18 espie 1076: while ((ln = Lst_Next (l)) != NULL) {
1.1 deraadt 1077: s = (Src *) Lst_Datum (ln);
1078: if (s->children == 0) {
1.28 ! espie 1079: free(s->file);
1.1 deraadt 1080: if (!s->parent)
1.28 ! espie 1081: free(s->pref);
1.1 deraadt 1082: else {
1083: #ifdef DEBUG_SRC
1.25 espie 1084: LstNode ln = Lst_Member(s->parent->cp, s);
1.18 espie 1085: if (ln != NULL)
1.1 deraadt 1086: Lst_Remove(s->parent->cp, ln);
1087: #endif
1088: --s->parent->children;
1089: }
1090: #ifdef DEBUG_SRC
1091: printf("free: [l=%x] p=%x %d\n", l, s, s->children);
1092: Lst_Destroy(s->cp, NOFREE);
1093: #endif
1094: Lst_Remove(l, ln);
1.28 ! espie 1095: free(s);
1.1 deraadt 1096: t |= 1;
1097: Lst_Close(l);
1098: return TRUE;
1099: }
1100: #ifdef DEBUG_SRC
1101: else {
1102: printf("keep: [l=%x] p=%x %d: ", l, s, s->children);
1.27 espie 1103: Lst_Every(s->cp, PrintAddr);
1.1 deraadt 1104: printf("\n");
1105: }
1106: #endif
1107: }
1108:
1109: Lst_Close(l);
1110:
1111: return t;
1112: }
1113:
1114: /*-
1115: *-----------------------------------------------------------------------
1116: * SuffFindThem --
1117: * Find the first existing file/target in the list srcs
1118: *
1119: * Results:
1120: * The lowest structure in the chain of transformations
1121: *
1122: * Side Effects:
1123: * None
1124: *-----------------------------------------------------------------------
1125: */
1126: static Src *
1127: SuffFindThem (srcs, slst)
1128: Lst srcs; /* list of Src structures to search through */
1129: Lst slst;
1130: {
1131: Src *s; /* current Src */
1132: Src *rs; /* returned Src */
1133: char *ptr;
1134:
1135: rs = (Src *) NULL;
1136:
1.19 espie 1137: while ((s = (Src *)Lst_DeQueue(srcs)) != NULL) {
1.1 deraadt 1138: if (DEBUG(SUFF)) {
1139: printf ("\ttrying %s...", s->file);
1140: }
1141:
1142: /*
1143: * A file is considered to exist if either a node exists in the
1144: * graph for it or the file actually exists.
1145: */
1.18 espie 1146: if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
1.1 deraadt 1147: #ifdef DEBUG_SRC
1148: printf("remove %x from %x\n", s, srcs);
1149: #endif
1150: rs = s;
1151: break;
1152: }
1153:
1154: if ((ptr = Dir_FindFile (s->file, s->suff->searchPath)) != NULL) {
1155: rs = s;
1156: #ifdef DEBUG_SRC
1157: printf("remove %x from %x\n", s, srcs);
1158: #endif
1159: free(ptr);
1160: break;
1161: }
1162:
1163: if (DEBUG(SUFF)) {
1164: printf ("not there\n");
1165: }
1166:
1167: SuffAddLevel (srcs, s);
1.25 espie 1168: Lst_AtEnd(slst, s);
1.1 deraadt 1169: }
1170:
1171: if (DEBUG(SUFF) && rs) {
1172: printf ("got it\n");
1173: }
1174: return (rs);
1175: }
1176:
1177: /*-
1178: *-----------------------------------------------------------------------
1179: * SuffFindCmds --
1180: * See if any of the children of the target in the Src structure is
1181: * one from which the target can be transformed. If there is one,
1182: * a Src structure is put together for it and returned.
1183: *
1184: * Results:
1.18 espie 1185: * The Src structure of the "winning" child, or NULL if no such beast.
1.1 deraadt 1186: *
1187: * Side Effects:
1188: * A Src structure may be allocated.
1189: *
1190: *-----------------------------------------------------------------------
1191: */
1192: static Src *
1193: SuffFindCmds (targ, slst)
1194: Src *targ; /* Src structure to play with */
1195: Lst slst;
1196: {
1197: LstNode ln; /* General-purpose list node */
1198: register GNode *t, /* Target GNode */
1199: *s; /* Source GNode */
1200: int prefLen;/* The length of the defined prefix */
1201: Suff *suff; /* Suffix on matching beastie */
1202: Src *ret; /* Return value */
1203: char *cp;
1204:
1205: t = targ->node;
1206: (void) Lst_Open (t->children);
1207: prefLen = strlen (targ->pref);
1208:
1.18 espie 1209: while ((ln = Lst_Next (t->children)) != NULL) {
1.1 deraadt 1210: s = (GNode *)Lst_Datum (ln);
1211:
1212: cp = strrchr (s->name, '/');
1213: if (cp == (char *)NULL) {
1214: cp = s->name;
1215: } else {
1216: cp++;
1217: }
1218: if (strncmp (cp, targ->pref, prefLen) == 0) {
1219: /*
1220: * The node matches the prefix ok, see if it has a known
1221: * suffix.
1222: */
1.25 espie 1223: ln = Lst_Find(sufflist, SuffSuffHasNameP, &cp[prefLen]);
1.18 espie 1224: if (ln != NULL) {
1.1 deraadt 1225: /*
1226: * It even has a known suffix, see if there's a transformation
1227: * defined between the node's suffix and the target's suffix.
1228: *
1229: * XXX: Handle multi-stage transformations here, too.
1230: */
1231: suff = (Suff *)Lst_Datum (ln);
1232:
1.25 espie 1233: if (Lst_Member(suff->parents, targ->suff) != NULL)
1.1 deraadt 1234: {
1235: /*
1236: * Hot Damn! Create a new Src structure to describe
1237: * this transformation (making sure to duplicate the
1238: * source node's name so Suff_FindDeps can free it
1239: * again (ick)), and return the new structure.
1240: */
1241: ret = (Src *)emalloc (sizeof (Src));
1.4 briggs 1242: ret->file = estrdup(s->name);
1.1 deraadt 1243: ret->pref = targ->pref;
1244: ret->suff = suff;
1245: ret->parent = targ;
1246: ret->node = s;
1247: ret->children = 0;
1248: targ->children += 1;
1249: #ifdef DEBUG_SRC
1.17 espie 1250: ret->cp = Lst_Init();
1.1 deraadt 1251: printf("3 add %x %x\n", targ, ret);
1.25 espie 1252: Lst_AtEnd(targ->cp, ret);
1.1 deraadt 1253: #endif
1.25 espie 1254: Lst_AtEnd(slst, ret);
1.1 deraadt 1255: if (DEBUG(SUFF)) {
1256: printf ("\tusing existing source %s\n", s->name);
1257: }
1258: return (ret);
1259: }
1260: }
1261: }
1262: }
1263: Lst_Close (t->children);
1264: return ((Src *)NULL);
1265: }
1266:
1267: /*-
1268: *-----------------------------------------------------------------------
1269: * SuffExpandChildren --
1270: * Expand the names of any children of a given node that contain
1271: * variable invocations or file wildcards into actual targets.
1272: *
1273: * Side Effects:
1274: * The expanded node is removed from the parent's list of children,
1275: * and the parent's unmade counter is decremented, but other nodes
1276: * may be added.
1277: *
1278: *-----------------------------------------------------------------------
1279: */
1.27 espie 1280: static void
1.1 deraadt 1281: SuffExpandChildren(cgnp, pgnp)
1.28 ! espie 1282: void *cgnp; /* Child to examine */
! 1283: void *pgnp; /* Parent node being processed */
1.1 deraadt 1284: {
1.27 espie 1285: GNode *cgn = (GNode *)cgnp;
1286: GNode *pgn = (GNode *)pgnp;
1.1 deraadt 1287: GNode *gn; /* New source 8) */
1288: LstNode prevLN; /* Node after which new source should be put */
1289: LstNode ln; /* List element for old source */
1290: char *cp; /* Expanded value */
1291:
1292: /*
1293: * New nodes effectively take the place of the child, so place them
1294: * after the child
1295: */
1.25 espie 1296: prevLN = Lst_Member(pgn->children, cgn);
1.5 millert 1297:
1.1 deraadt 1298: /*
1299: * First do variable expansion -- this takes precedence over
1300: * wildcard expansion. If the result contains wildcards, they'll be gotten
1301: * to later since the resulting words are tacked on to the end of
1302: * the children list.
1303: */
1.27 espie 1304: if (strchr(cgn->name, '$') != NULL) {
1305: if (DEBUG(SUFF))
1.1 deraadt 1306: printf("Expanding \"%s\"...", cgn->name);
1.16 espie 1307: cp = Var_Subst(cgn->name, pgn, TRUE);
1.1 deraadt 1308:
1.27 espie 1309: if (cp != NULL) {
1.17 espie 1310: Lst members = Lst_Init();
1.5 millert 1311:
1.1 deraadt 1312: if (cgn->type & OP_ARCHV) {
1313: /*
1314: * Node was an archive(member) target, so we want to call
1315: * on the Arch module to find the nodes for us, expanding
1316: * variables in the parent's context.
1317: */
1318: char *sacrifice = cp;
1319:
1320: (void)Arch_ParseArchive(&sacrifice, members, pgn);
1321: } else {
1322: /*
1323: * Break the result into a vector of strings whose nodes
1324: * we can find, then add those nodes to the members list.
1325: * Unfortunately, we can't use brk_string b/c it
1326: * doesn't understand about variable specifications with
1327: * spaces in them...
1328: */
1329: char *start;
1330: char *initcp = cp; /* For freeing... */
1331:
1.5 millert 1332: for (start = cp; *start == ' ' || *start == '\t'; start++)
1.1 deraadt 1333: continue;
1334: for (cp = start; *cp != '\0'; cp++) {
1335: if (*cp == ' ' || *cp == '\t') {
1336: /*
1337: * White-space -- terminate element, find the node,
1338: * add it, skip any further spaces.
1339: */
1340: *cp++ = '\0';
1341: gn = Targ_FindNode(start, TARG_CREATE);
1.25 espie 1342: Lst_AtEnd(members, gn);
1.1 deraadt 1343: while (*cp == ' ' || *cp == '\t') {
1344: cp++;
1345: }
1346: /*
1347: * Adjust cp for increment at start of loop, but
1348: * set start to first non-space.
1349: */
1350: start = cp--;
1351: } else if (*cp == '$') {
1352: /*
1353: * Start of a variable spec -- contact variable module
1354: * to find the end so we can skip over it.
1355: */
1356: char *junk;
1.22 espie 1357: size_t len;
1.1 deraadt 1358: Boolean doFree;
1359:
1360: junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
1.27 espie 1361: if (junk != var_Error)
1.1 deraadt 1362: cp += len - 1;
1363:
1.27 espie 1364: if (doFree)
1.1 deraadt 1365: free(junk);
1366: } else if (*cp == '\\' && *cp != '\0') {
1367: /*
1368: * Escaped something -- skip over it
1369: */
1370: cp++;
1371: }
1372: }
1373:
1374: if (cp != start) {
1375: /*
1376: * Stuff left over -- add it to the list too
1377: */
1378: gn = Targ_FindNode(start, TARG_CREATE);
1.25 espie 1379: Lst_AtEnd(members, gn);
1.1 deraadt 1380: }
1381: /*
1382: * Point cp back at the beginning again so the variable value
1383: * can be freed.
1384: */
1385: cp = initcp;
1386: }
1387: /*
1388: * Add all elements of the members list to the parent node.
1389: */
1.19 espie 1390: while((gn = (GNode *)Lst_DeQueue(members)) != NULL) {
1.27 espie 1391: if (DEBUG(SUFF))
1.1 deraadt 1392: printf("%s...", gn->name);
1.25 espie 1393: if (Lst_Member(pgn->children, gn) == NULL) {
1394: Lst_Append(pgn->children, prevLN, gn);
1.1 deraadt 1395: prevLN = Lst_Succ(prevLN);
1.25 espie 1396: Lst_AtEnd(gn->parents, pgn);
1.1 deraadt 1397: pgn->unmade++;
1398: }
1399: }
1400: Lst_Destroy(members, NOFREE);
1401: /*
1402: * Free the result
1403: */
1.27 espie 1404: free(cp);
1.1 deraadt 1405: }
1406: /*
1407: * Now the source is expanded, remove it from the list of children to
1408: * keep it from being processed.
1409: */
1.25 espie 1410: ln = Lst_Member(pgn->children, cgn);
1.1 deraadt 1411: pgn->unmade--;
1412: Lst_Remove(pgn->children, ln);
1.27 espie 1413: if (DEBUG(SUFF))
1.1 deraadt 1414: printf("\n");
1415: } else if (Dir_HasWildcards(cgn->name)) {
1416: Lst exp; /* List of expansions */
1417: Lst path; /* Search path along which to expand */
1418:
1419: /*
1420: * Find a path along which to expand the word.
1421: *
1422: * If the word has a known suffix, use that path.
1423: * If it has no known suffix and we're allowed to use the null
1424: * suffix, use its path.
1425: * Else use the default system search path.
1426: */
1427: cp = cgn->name + strlen(cgn->name);
1.25 espie 1428: ln = Lst_Find(sufflist, SuffSuffIsSuffixP, cp);
1.1 deraadt 1429:
1.27 espie 1430: if (DEBUG(SUFF))
1.1 deraadt 1431: printf("Wildcard expanding \"%s\"...", cgn->name);
1.5 millert 1432:
1.18 espie 1433: if (ln != NULL) {
1.1 deraadt 1434: Suff *s = (Suff *)Lst_Datum(ln);
1435:
1.27 espie 1436: if (DEBUG(SUFF))
1.1 deraadt 1437: printf("suffix is \"%s\"...", s->name);
1438: path = s->searchPath;
1439: } else {
1440: /*
1441: * Use default search path
1442: */
1443: path = dirSearchPath;
1444: }
1445:
1446: /*
1447: * Expand the word along the chosen path
1448: */
1.17 espie 1449: exp = Lst_Init();
1.1 deraadt 1450: Dir_Expand(cgn->name, path, exp);
1451:
1.19 espie 1452: /* Fetch next expansion off the list and find its GNode. */
1453: while ((cp = (char *)Lst_DeQueue(exp)) != NULL) {
1.27 espie 1454: if (DEBUG(SUFF))
1.1 deraadt 1455: printf("%s...", cp);
1456: gn = Targ_FindNode(cp, TARG_CREATE);
1457:
1458: /*
1459: * If gn isn't already a child of the parent, make it so and
1460: * up the parent's count of unmade children.
1461: */
1.25 espie 1462: if (Lst_Member(pgn->children, gn) == NULL) {
1463: Lst_Append(pgn->children, prevLN, gn);
1.1 deraadt 1464: prevLN = Lst_Succ(prevLN);
1.25 espie 1465: Lst_AtEnd(gn->parents, pgn);
1.1 deraadt 1466: pgn->unmade++;
1467: }
1468: }
1469:
1470: /*
1471: * Nuke what's left of the list
1472: */
1473: Lst_Destroy(exp, NOFREE);
1.5 millert 1474:
1.1 deraadt 1475: /*
1476: * Now the source is expanded, remove it from the list of children to
1477: * keep it from being processed.
1478: */
1.25 espie 1479: ln = Lst_Member(pgn->children, cgn);
1.1 deraadt 1480: pgn->unmade--;
1481: Lst_Remove(pgn->children, ln);
1.27 espie 1482: if (DEBUG(SUFF))
1.1 deraadt 1483: printf("\n");
1484: }
1485: }
1486:
1487: /*-
1488: *-----------------------------------------------------------------------
1489: * SuffApplyTransform --
1490: * Apply a transformation rule, given the source and target nodes
1491: * and suffixes.
1492: *
1493: * Results:
1494: * TRUE if successful, FALSE if not.
1495: *
1496: * Side Effects:
1497: * The source and target are linked and the commands from the
1498: * transformation are added to the target node's commands list.
1499: * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1500: * to the target. The target also inherits all the sources for
1501: * the transformation rule.
1502: *
1503: *-----------------------------------------------------------------------
1504: */
1505: static Boolean
1506: SuffApplyTransform(tGn, sGn, t, s)
1507: GNode *tGn; /* Target node */
1508: GNode *sGn; /* Source node */
1509: Suff *t; /* Target suffix */
1510: Suff *s; /* Source suffix */
1511: {
1512: LstNode ln; /* General node */
1513: char *tname; /* Name of transformation rule */
1514: GNode *gn; /* Node for same */
1515:
1.25 espie 1516: if (Lst_Member(tGn->children, sGn) == NULL) {
1.1 deraadt 1517: /*
1518: * Not already linked, so form the proper links between the
1519: * target and source.
1520: */
1.25 espie 1521: Lst_AtEnd(tGn->children, sGn);
1522: Lst_AtEnd(sGn->parents, tGn);
1.1 deraadt 1523: tGn->unmade += 1;
1524: }
1525:
1526: if ((sGn->type & OP_OPMASK) == OP_DOUBLEDEP) {
1527: /*
1528: * When a :: node is used as the implied source of a node, we have
1529: * to link all its cohorts in as sources as well. Only the initial
1530: * sGn gets the target in its iParents list, however, as that
1531: * will be sufficient to get the .IMPSRC variable set for tGn
1532: */
1.18 espie 1533: for (ln=Lst_First(sGn->cohorts); ln != NULL; ln=Lst_Succ(ln)) {
1.1 deraadt 1534: gn = (GNode *)Lst_Datum(ln);
1535:
1.25 espie 1536: if (Lst_Member(tGn->children, gn) == NULL) {
1.1 deraadt 1537: /*
1538: * Not already linked, so form the proper links between the
1539: * target and source.
1540: */
1.25 espie 1541: Lst_AtEnd(tGn->children, gn);
1542: Lst_AtEnd(gn->parents, tGn);
1.1 deraadt 1543: tGn->unmade += 1;
1544: }
1545: }
1546: }
1547: /*
1548: * Locate the transformation rule itself
1549: */
1550: tname = str_concat(s->name, t->name, 0);
1.25 espie 1551: ln = Lst_Find(transforms, SuffGNHasNameP, tname);
1.1 deraadt 1552: free(tname);
1553:
1.18 espie 1554: if (ln == NULL) {
1.1 deraadt 1555: /*
1556: * Not really such a transformation rule (can happen when we're
1557: * called to link an OP_MEMBER and OP_ARCHV node), so return
1558: * FALSE.
1559: */
1560: return(FALSE);
1561: }
1562:
1563: gn = (GNode *)Lst_Datum(ln);
1.5 millert 1564:
1.1 deraadt 1565: if (DEBUG(SUFF)) {
1566: printf("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
1567: }
1568:
1569: /*
1570: * Record last child for expansion purposes
1571: */
1572: ln = Lst_Last(tGn->children);
1.5 millert 1573:
1.1 deraadt 1574: /*
1575: * Pass the buck to Make_HandleUse to apply the rule
1576: */
1577: (void)Make_HandleUse(gn, tGn);
1578:
1579: /*
1580: * Deal with wildcards and variables in any acquired sources
1581: */
1582: ln = Lst_Succ(ln);
1.27 espie 1583: Lst_ForEachFrom(ln, SuffExpandChildren, tGn);
1.1 deraadt 1584:
1585: /*
1586: * Keep track of another parent to which this beast is transformed so
1587: * the .IMPSRC variable can be set correctly for the parent.
1588: */
1.25 espie 1589: Lst_AtEnd(sGn->iParents, tGn);
1.1 deraadt 1590:
1591: return(TRUE);
1592: }
1593:
1594:
1595: /*-
1596: *-----------------------------------------------------------------------
1597: * SuffFindArchiveDeps --
1598: * Locate dependencies for an OP_ARCHV node.
1599: *
1600: * Results:
1601: * None
1602: *
1603: * Side Effects:
1604: * Same as Suff_FindDeps
1605: *
1606: *-----------------------------------------------------------------------
1607: */
1608: static void
1609: SuffFindArchiveDeps(gn, slst)
1610: GNode *gn; /* Node for which to locate dependencies */
1611: Lst slst;
1612: {
1613: char *eoarch; /* End of archive portion */
1614: char *eoname; /* End of member portion */
1615: GNode *mem; /* Node for member */
1616: static char *copy[] = { /* Variables to be copied from the member node */
1617: TARGET, /* Must be first */
1618: PREFIX, /* Must be second */
1619: };
1620: int i; /* Index into copy and vals */
1621: Suff *ms; /* Suffix descriptor for member */
1622: char *name; /* Start of member's name */
1.5 millert 1623:
1.1 deraadt 1624: /*
1625: * The node is an archive(member) pair. so we must find a
1626: * suffix for both of them.
1627: */
1628: eoarch = strchr (gn->name, '(');
1629: eoname = strchr (eoarch, ')');
1.6 millert 1630: if (eoarch == NULL || eoname == NULL)
1631: return;
1.1 deraadt 1632:
1633: *eoname = '\0'; /* Nuke parentheses during suffix search */
1634: *eoarch = '\0'; /* So a suffix can be found */
1635:
1636: name = eoarch + 1;
1.5 millert 1637:
1.1 deraadt 1638: /*
1639: * To simplify things, call Suff_FindDeps recursively on the member now,
1640: * so we can simply compare the member's .PREFIX and .TARGET variables
1641: * to locate its suffix. This allows us to figure out the suffix to
1642: * use for the archive without having to do a quadratic search over the
1643: * suffix list, backtracking for each one...
1644: */
1645: mem = Targ_FindNode(name, TARG_CREATE);
1646: SuffFindDeps(mem, slst);
1647:
1648: /*
1649: * Create the link between the two nodes right off
1650: */
1.25 espie 1651: if (Lst_Member(gn->children, mem) == NULL) {
1652: Lst_AtEnd(gn->children, mem);
1653: Lst_AtEnd(mem->parents, gn);
1.1 deraadt 1654: gn->unmade += 1;
1655: }
1.5 millert 1656:
1.1 deraadt 1657: /*
1658: * Copy in the variables from the member node to this one.
1659: */
1660: for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
1.15 espie 1661: Var_Set(copy[i], Var_Value(copy[i], mem), gn);
1.1 deraadt 1662:
1663: }
1664:
1665: ms = mem->suffix;
1666: if (ms == NULL) {
1667: /*
1668: * Didn't know what it was -- use .NULL suffix if not in make mode
1669: */
1670: if (DEBUG(SUFF)) {
1671: printf("using null suffix\n");
1672: }
1673: ms = suffNull;
1674: }
1675:
1676:
1677: /*
1678: * Set the other two local variables required for this target.
1679: */
1680: Var_Set (MEMBER, name, gn);
1681: Var_Set (ARCHIVE, gn->name, gn);
1682:
1683: if (ms != NULL) {
1684: /*
1685: * Member has a known suffix, so look for a transformation rule from
1686: * it to a possible suffix of the archive. Rather than searching
1687: * through the entire list, we just look at suffixes to which the
1688: * member's suffix may be transformed...
1689: */
1690: LstNode ln;
1691:
1692: /*
1693: * Use first matching suffix...
1694: */
1.21 espie 1695: ln = Lst_Find(ms->parents, SuffSuffIsSuffixP, eoarch);
1.1 deraadt 1696:
1.18 espie 1697: if (ln != NULL) {
1.1 deraadt 1698: /*
1699: * Got one -- apply it
1700: */
1701: if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) &&
1702: DEBUG(SUFF))
1703: {
1704: printf("\tNo transformation from %s -> %s\n",
1705: ms->name, ((Suff *)Lst_Datum(ln))->name);
1706: }
1707: }
1708: }
1709:
1710: /*
1711: * Replace the opening and closing parens now we've no need of the separate
1712: * pieces.
1713: */
1714: *eoarch = '('; *eoname = ')';
1715:
1716: /*
1717: * Pretend gn appeared to the left of a dependency operator so
1718: * the user needn't provide a transformation from the member to the
1719: * archive.
1720: */
1721: if (OP_NOP(gn->type)) {
1722: gn->type |= OP_DEPENDS;
1723: }
1724:
1725: /*
1726: * Flag the member as such so we remember to look in the archive for
1727: * its modification time.
1728: */
1729: mem->type |= OP_MEMBER;
1730: }
1731:
1732: /*-
1733: *-----------------------------------------------------------------------
1734: * SuffFindNormalDeps --
1735: * Locate implicit dependencies for regular targets.
1736: *
1737: * Results:
1738: * None.
1739: *
1740: * Side Effects:
1741: * Same as Suff_FindDeps...
1742: *
1743: *-----------------------------------------------------------------------
1744: */
1745: static void
1746: SuffFindNormalDeps(gn, slst)
1747: GNode *gn; /* Node for which to find sources */
1748: Lst slst;
1749: {
1750: char *eoname; /* End of name */
1751: char *sopref; /* Start of prefix */
1752: LstNode ln; /* Next suffix node to check */
1753: Lst srcs; /* List of sources at which to look */
1754: Lst targs; /* List of targets to which things can be
1755: * transformed. They all have the same file,
1756: * but different suff and pref fields */
1757: Src *bottom; /* Start of found transformation path */
1758: Src *src; /* General Src pointer */
1759: char *pref; /* Prefix to use */
1760: Src *targ; /* General Src target pointer */
1761:
1762:
1763: eoname = gn->name + strlen(gn->name);
1764:
1765: sopref = gn->name;
1.5 millert 1766:
1.1 deraadt 1767: /*
1768: * Begin at the beginning...
1769: */
1770: ln = Lst_First(sufflist);
1.17 espie 1771: srcs = Lst_Init();
1772: targs = Lst_Init();
1.1 deraadt 1773:
1774: /*
1775: * We're caught in a catch-22 here. On the one hand, we want to use any
1776: * transformation implied by the target's sources, but we can't examine
1777: * the sources until we've expanded any variables/wildcards they may hold,
1778: * and we can't do that until we've set up the target's local variables
1779: * and we can't do that until we know what the proper suffix for the
1780: * target is (in case there are two suffixes one of which is a suffix of
1781: * the other) and we can't know that until we've found its implied
1782: * source, which we may not want to use if there's an existing source
1783: * that implies a different transformation.
1784: *
1785: * In an attempt to get around this, which may not work all the time,
1786: * but should work most of the time, we look for implied sources first,
1787: * checking transformations to all possible suffixes of the target,
1788: * use what we find to set the target's local variables, expand the
1789: * children, then look for any overriding transformations they imply.
1790: * Should we find one, we discard the one we found before.
1791: */
1792:
1.18 espie 1793: while (ln != NULL) {
1.1 deraadt 1794: /*
1795: * Look for next possible suffix...
1796: */
1.21 espie 1797: ln = Lst_FindFrom(ln, SuffSuffIsSuffixP, eoname);
1.1 deraadt 1798:
1.18 espie 1799: if (ln != NULL) {
1.1 deraadt 1800: int prefLen; /* Length of the prefix */
1801: Src *targ;
1.5 millert 1802:
1.1 deraadt 1803: /*
1804: * Allocate a Src structure to which things can be transformed
1805: */
1806: targ = (Src *)emalloc(sizeof (Src));
1.4 briggs 1807: targ->file = estrdup(gn->name);
1.1 deraadt 1808: targ->suff = (Suff *)Lst_Datum(ln);
1809: targ->node = gn;
1810: targ->parent = (Src *)NULL;
1811: targ->children = 0;
1812: #ifdef DEBUG_SRC
1.17 espie 1813: targ->cp = Lst_Init();
1.1 deraadt 1814: #endif
1.5 millert 1815:
1.1 deraadt 1816: /*
1817: * Allocate room for the prefix, whose end is found by subtracting
1818: * the length of the suffix from the end of the name.
1819: */
1820: prefLen = (eoname - targ->suff->nameLen) - sopref;
1821: targ->pref = emalloc(prefLen + 1);
1822: memcpy(targ->pref, sopref, prefLen);
1823: targ->pref[prefLen] = '\0';
1824:
1825: /*
1826: * Add nodes from which the target can be made
1827: */
1828: SuffAddLevel(srcs, targ);
1829:
1830: /*
1831: * Record the target so we can nuke it
1832: */
1.25 espie 1833: Lst_AtEnd(targs, targ);
1.1 deraadt 1834:
1835: /*
1836: * Search from this suffix's successor...
1837: */
1838: ln = Lst_Succ(ln);
1839: }
1840: }
1841:
1842: /*
1843: * Handle target of unknown suffix...
1844: */
1845: if (Lst_IsEmpty(targs) && suffNull != NULL) {
1846: if (DEBUG(SUFF)) {
1.5 millert 1847: printf("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
1.1 deraadt 1848: }
1.5 millert 1849:
1.1 deraadt 1850: targ = (Src *)emalloc(sizeof (Src));
1.4 briggs 1851: targ->file = estrdup(gn->name);
1.1 deraadt 1852: targ->suff = suffNull;
1853: targ->node = gn;
1854: targ->parent = (Src *)NULL;
1855: targ->children = 0;
1.4 briggs 1856: targ->pref = estrdup(sopref);
1.1 deraadt 1857: #ifdef DEBUG_SRC
1.17 espie 1858: targ->cp = Lst_Init();
1.1 deraadt 1859: #endif
1860:
1861: /*
1862: * Only use the default suffix rules if we don't have commands
1863: * or dependencies defined for this gnode
1864: */
1865: if (Lst_IsEmpty(gn->commands) && Lst_IsEmpty(gn->children))
1866: SuffAddLevel(srcs, targ);
1867: else {
1.5 millert 1868: if (DEBUG(SUFF))
1.1 deraadt 1869: printf("not ");
1870: }
1871:
1.5 millert 1872: if (DEBUG(SUFF))
1.1 deraadt 1873: printf("adding suffix rules\n");
1874:
1.25 espie 1875: Lst_AtEnd(targs, targ);
1.1 deraadt 1876: }
1.5 millert 1877:
1.1 deraadt 1878: /*
1879: * Using the list of possible sources built up from the target suffix(es),
1880: * try and find an existing file/target that matches.
1881: */
1882: bottom = SuffFindThem(srcs, slst);
1883:
1884: if (bottom == (Src *)NULL) {
1885: /*
1886: * No known transformations -- use the first suffix found for setting
1887: * the local variables.
1888: */
1889: if (!Lst_IsEmpty(targs)) {
1890: targ = (Src *)Lst_Datum(Lst_First(targs));
1891: } else {
1892: targ = (Src *)NULL;
1893: }
1894: } else {
1895: /*
1896: * Work up the transformation path to find the suffix of the
1897: * target to which the transformation was made.
1898: */
1899: for (targ = bottom; targ->parent != NULL; targ = targ->parent)
1900: continue;
1901: }
1902:
1903: /*
1904: * The .TARGET variable we always set to be the name at this point,
1905: * since it's only set to the path if the thing is only a source and
1906: * if it's only a source, it doesn't matter what we put here as far
1907: * as expanding sources is concerned, since it has none...
1908: */
1909: Var_Set(TARGET, gn->name, gn);
1910:
1911: pref = (targ != NULL) ? targ->pref : gn->name;
1912: Var_Set(PREFIX, pref, gn);
1913:
1914: /*
1915: * Now we've got the important local variables set, expand any sources
1916: * that still contain variables or wildcards in their names.
1917: */
1.25 espie 1918: Lst_ForEach(gn->children, SuffExpandChildren, gn);
1.5 millert 1919:
1.1 deraadt 1920: if (targ == NULL) {
1921: if (DEBUG(SUFF)) {
1922: printf("\tNo valid suffix on %s\n", gn->name);
1923: }
1924:
1925: sfnd_abort:
1926: /*
1927: * Deal with finding the thing on the default search path if the
1928: * node is only a source (not on the lhs of a dependency operator
1929: * or [XXX] it has neither children or commands).
1930: */
1931: if (OP_NOP(gn->type) ||
1932: (Lst_IsEmpty(gn->children) && Lst_IsEmpty(gn->commands)))
1933: {
1934: gn->path = Dir_FindFile(gn->name,
1935: (targ == NULL ? dirSearchPath :
1936: targ->suff->searchPath));
1937: if (gn->path != NULL) {
1.2 deraadt 1938: char *ptr;
1.1 deraadt 1939: Var_Set(TARGET, gn->path, gn);
1940:
1941: if (targ != NULL) {
1942: /*
1943: * Suffix known for the thing -- trim the suffix off
1944: * the path to form the proper .PREFIX variable.
1945: */
1.2 deraadt 1946: int savep = strlen(gn->path) - targ->suff->nameLen;
1.1 deraadt 1947: char savec;
1948:
1949: gn->suffix = targ->suff;
1950:
1.2 deraadt 1951: savec = gn->path[savep];
1952: gn->path[savep] = '\0';
1.1 deraadt 1953:
1.2 deraadt 1954: if ((ptr = strrchr(gn->path, '/')) != NULL)
1955: ptr++;
1956: else
1957: ptr = gn->path;
1.1 deraadt 1958:
1.2 deraadt 1959: Var_Set(PREFIX, ptr, gn);
1960:
1961: gn->path[savep] = savec;
1.1 deraadt 1962: } else {
1963: /*
1964: * The .PREFIX gets the full path if the target has
1965: * no known suffix.
1966: */
1967: gn->suffix = NULL;
1968:
1.2 deraadt 1969: if ((ptr = strrchr(gn->path, '/')) != NULL)
1970: ptr++;
1971: else
1972: ptr = gn->path;
1973:
1974: Var_Set(PREFIX, ptr, gn);
1.1 deraadt 1975: }
1976: }
1977: } else {
1978: /*
1979: * Not appropriate to search for the thing -- set the
1980: * path to be the name so Dir_MTime won't go grovelling for
1981: * it.
1982: */
1983: gn->suffix = (targ == NULL) ? NULL : targ->suff;
1.11 espie 1984: efree(gn->path);
1.4 briggs 1985: gn->path = estrdup(gn->name);
1.1 deraadt 1986: }
1.5 millert 1987:
1.1 deraadt 1988: goto sfnd_return;
1989: }
1990:
1991: /*
1992: * If the suffix indicates that the target is a library, mark that in
1993: * the node's type field.
1994: */
1995: if (targ->suff->flags & SUFF_LIBRARY) {
1996: gn->type |= OP_LIB;
1997: }
1998:
1999: /*
2000: * Check for overriding transformation rule implied by sources
2001: */
2002: if (!Lst_IsEmpty(gn->children)) {
2003: src = SuffFindCmds(targ, slst);
2004:
2005: if (src != (Src *)NULL) {
2006: /*
2007: * Free up all the Src structures in the transformation path
2008: * up to, but not including, the parent node.
2009: */
2010: while (bottom && bottom->parent != NULL) {
1.25 espie 2011: if (Lst_Member(slst, bottom) == NULL) {
2012: Lst_AtEnd(slst, bottom);
1.1 deraadt 2013: }
2014: bottom = bottom->parent;
2015: }
2016: bottom = src;
2017: }
2018: }
2019:
2020: if (bottom == NULL) {
2021: /*
2022: * No idea from where it can come -- return now.
2023: */
2024: goto sfnd_abort;
2025: }
2026:
2027: /*
2028: * We now have a list of Src structures headed by 'bottom' and linked via
2029: * their 'parent' pointers. What we do next is create links between
2030: * source and target nodes (which may or may not have been created)
2031: * and set the necessary local variables in each target. The
2032: * commands for each target are set from the commands of the
2033: * transformation rule used to get from the src suffix to the targ
2034: * suffix. Note that this causes the commands list of the original
2035: * node, gn, to be replaced by the commands of the final
2036: * transformation rule. Also, the unmade field of gn is incremented.
1.5 millert 2037: * Etc.
1.1 deraadt 2038: */
1.18 espie 2039: if (bottom->node == NULL) {
1.1 deraadt 2040: bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2041: }
1.5 millert 2042:
1.1 deraadt 2043: for (src = bottom; src->parent != (Src *)NULL; src = src->parent) {
2044: targ = src->parent;
2045:
2046: src->node->suffix = src->suff;
2047:
1.18 espie 2048: if (targ->node == NULL) {
1.1 deraadt 2049: targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2050: }
2051:
2052: SuffApplyTransform(targ->node, src->node,
2053: targ->suff, src->suff);
2054:
2055: if (targ->node != gn) {
2056: /*
2057: * Finish off the dependency-search process for any nodes
2058: * between bottom and gn (no point in questing around the
2059: * filesystem for their implicit source when it's already
2060: * known). Note that the node can't have any sources that
2061: * need expanding, since SuffFindThem will stop on an existing
2062: * node, so all we need to do is set the standard and System V
2063: * variables.
2064: */
2065: targ->node->type |= OP_DEPS_FOUND;
2066:
2067: Var_Set(PREFIX, targ->pref, targ->node);
1.5 millert 2068:
1.1 deraadt 2069: Var_Set(TARGET, targ->node->name, targ->node);
2070: }
2071: }
2072:
2073: gn->suffix = src->suff;
2074:
2075: /*
2076: * So Dir_MTime doesn't go questing for it...
2077: */
1.11 espie 2078: efree(gn->path);
1.4 briggs 2079: gn->path = estrdup(gn->name);
1.1 deraadt 2080:
2081: /*
2082: * Nuke the transformation path and the Src structures left over in the
2083: * two lists.
2084: */
2085: sfnd_return:
2086: if (bottom)
1.25 espie 2087: if (Lst_Member(slst, bottom) == NULL)
2088: Lst_AtEnd(slst, bottom);
1.1 deraadt 2089:
2090: while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
2091: continue;
2092:
2093: Lst_Concat(slst, srcs, LST_CONCLINK);
2094: Lst_Concat(slst, targs, LST_CONCLINK);
2095: }
1.5 millert 2096:
2097:
1.1 deraadt 2098: /*-
2099: *-----------------------------------------------------------------------
2100: * Suff_FindDeps --
2101: * Find implicit sources for the target described by the graph node
2102: * gn
2103: *
2104: * Results:
2105: * Nothing.
2106: *
2107: * Side Effects:
2108: * Nodes are added to the graph below the passed-in node. The nodes
2109: * are marked to have their IMPSRC variable filled in. The
2110: * PREFIX variable is set for the given node and all its
2111: * implied children.
2112: *
2113: * Notes:
2114: * The path found by this target is the shortest path in the
2115: * transformation graph, which may pass through non-existent targets,
2116: * to an existing target. The search continues on all paths from the
2117: * root suffix until a file is found. I.e. if there's a path
2118: * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2119: * the .c and .l files don't, the search will branch out in
2120: * all directions from .o and again from all the nodes on the
2121: * next level until the .l,v node is encountered.
2122: *
2123: *-----------------------------------------------------------------------
2124: */
2125:
2126: void
2127: Suff_FindDeps(gn)
2128: GNode *gn;
2129: {
1.5 millert 2130:
1.1 deraadt 2131: SuffFindDeps(gn, srclist);
2132: while (SuffRemoveSrc(srclist))
2133: continue;
2134: }
2135:
2136:
2137: static void
2138: SuffFindDeps (gn, slst)
2139: GNode *gn; /* node we're dealing with */
2140: Lst slst;
2141: {
2142: if (gn->type & OP_DEPS_FOUND) {
2143: /*
2144: * If dependencies already found, no need to do it again...
2145: */
2146: return;
2147: } else {
2148: gn->type |= OP_DEPS_FOUND;
2149: }
1.5 millert 2150:
1.1 deraadt 2151: if (DEBUG(SUFF)) {
2152: printf ("SuffFindDeps (%s)\n", gn->name);
2153: }
1.5 millert 2154:
1.1 deraadt 2155: if (gn->type & OP_ARCHV) {
2156: SuffFindArchiveDeps(gn, slst);
2157: } else if (gn->type & OP_LIB) {
2158: /*
2159: * If the node is a library, it is the arch module's job to find it
2160: * and set the TARGET variable accordingly. We merely provide the
2161: * search path, assuming all libraries end in ".a" (if the suffix
2162: * hasn't been defined, there's nothing we can do for it, so we just
2163: * set the TARGET variable to the node's name in order to give it a
2164: * value).
2165: */
2166: LstNode ln;
2167: Suff *s;
1.5 millert 2168:
1.25 espie 2169: ln = Lst_Find(sufflist, SuffSuffHasNameP, LIBSUFF);
1.18 espie 2170: if (ln != NULL) {
1.1 deraadt 2171: gn->suffix = s = (Suff *) Lst_Datum (ln);
2172: Arch_FindLib (gn, s->searchPath);
2173: } else {
2174: gn->suffix = NULL;
2175: Var_Set (TARGET, gn->name, gn);
2176: }
2177: /*
2178: * Because a library (-lfoo) target doesn't follow the standard
2179: * filesystem conventions, we don't set the regular variables for
2180: * the thing. .PREFIX is simply made empty...
2181: */
2182: Var_Set(PREFIX, "", gn);
2183: } else {
2184: SuffFindNormalDeps(gn, slst);
2185: }
2186: }
2187:
2188: /*-
2189: *-----------------------------------------------------------------------
2190: * Suff_SetNull --
2191: * Define which suffix is the null suffix.
2192: *
2193: * Results:
2194: * None.
2195: *
2196: * Side Effects:
2197: * 'suffNull' is altered.
2198: *
2199: * Notes:
2200: * Need to handle the changing of the null suffix gracefully so the
2201: * old transformation rules don't just go away.
2202: *
2203: *-----------------------------------------------------------------------
2204: */
2205: void
2206: Suff_SetNull(name)
2207: char *name; /* Name of null suffix */
2208: {
2209: Suff *s;
2210: LstNode ln;
2211:
1.25 espie 2212: ln = Lst_Find(sufflist, SuffSuffHasNameP, name);
1.18 espie 2213: if (ln != NULL) {
1.1 deraadt 2214: s = (Suff *)Lst_Datum(ln);
2215: if (suffNull != (Suff *)NULL) {
2216: suffNull->flags &= ~SUFF_NULL;
2217: }
2218: s->flags |= SUFF_NULL;
2219: /*
2220: * XXX: Here's where the transformation mangling would take place
2221: */
2222: suffNull = s;
2223: } else {
2224: Parse_Error (PARSE_WARNING, "Desired null suffix %s not defined.",
2225: name);
2226: }
2227: }
2228:
2229: /*-
2230: *-----------------------------------------------------------------------
2231: * Suff_Init --
2232: * Initialize suffixes module
2233: *
2234: * Results:
2235: * None
2236: *
2237: * Side Effects:
2238: * Many
2239: *-----------------------------------------------------------------------
2240: */
2241: void
2242: Suff_Init ()
2243: {
1.17 espie 2244: sufflist = Lst_Init();
1.14 espie 2245: #ifdef CLEANUP
1.17 espie 2246: suffClean = Lst_Init();
1.14 espie 2247: #endif
1.17 espie 2248: srclist = Lst_Init();
2249: transforms = Lst_Init();
1.1 deraadt 2250:
2251: sNum = 0;
2252: /*
2253: * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2254: * actually go on the suffix list or everyone will think that's its
2255: * suffix.
2256: */
2257: emptySuff = suffNull = (Suff *) emalloc (sizeof (Suff));
2258:
1.4 briggs 2259: suffNull->name = estrdup ("");
1.1 deraadt 2260: suffNull->nameLen = 0;
1.17 espie 2261: suffNull->searchPath = Lst_Init();
1.1 deraadt 2262: Dir_Concat(suffNull->searchPath, dirSearchPath);
1.17 espie 2263: suffNull->children = Lst_Init();
2264: suffNull->parents = Lst_Init();
2265: suffNull->ref = Lst_Init();
1.1 deraadt 2266: suffNull->sNum = sNum++;
2267: suffNull->flags = SUFF_NULL;
2268:
2269: }
2270:
2271:
2272: /*-
2273: *----------------------------------------------------------------------
2274: * Suff_End --
2275: * Cleanup the this module
2276: *
2277: * Results:
2278: * None
2279: *
2280: * Side Effects:
2281: * The memory is free'd.
2282: *----------------------------------------------------------------------
2283: */
2284:
2285: void
2286: Suff_End()
2287: {
1.14 espie 2288: #ifdef CLEANUP
1.1 deraadt 2289: Lst_Destroy(sufflist, SuffFree);
2290: Lst_Destroy(suffClean, SuffFree);
2291: if (suffNull)
2292: SuffFree(suffNull);
2293: Lst_Destroy(srclist, NOFREE);
2294: Lst_Destroy(transforms, NOFREE);
1.14 espie 2295: #endif
1.1 deraadt 2296: }
2297:
2298:
2299: /********************* DEBUGGING FUNCTIONS **********************/
2300:
1.27 espie 2301: static void SuffPrintName(s)
1.28 ! espie 2302: void *s;
1.1 deraadt 2303: {
1.27 espie 2304: printf("%s ", ((Suff *)s)->name);
1.1 deraadt 2305: }
2306:
1.27 espie 2307: static void
2308: SuffPrintSuff(sp)
1.28 ! espie 2309: void *sp;
1.1 deraadt 2310: {
1.27 espie 2311: Suff *s = (Suff *)sp;
1.1 deraadt 2312: int flags;
2313: int flag;
2314:
1.27 espie 2315: printf("# `%s' ", s->name);
1.5 millert 2316:
1.1 deraadt 2317: flags = s->flags;
2318: if (flags) {
1.27 espie 2319: fputs(" (", stdout);
1.1 deraadt 2320: while (flags) {
2321: flag = 1 << (ffs(flags) - 1);
2322: flags &= ~flag;
2323: switch (flag) {
2324: case SUFF_NULL:
1.27 espie 2325: printf("NULL");
1.1 deraadt 2326: break;
2327: case SUFF_INCLUDE:
1.27 espie 2328: printf("INCLUDE");
1.1 deraadt 2329: break;
2330: case SUFF_LIBRARY:
1.27 espie 2331: printf("LIBRARY");
1.1 deraadt 2332: break;
2333: }
2334: fputc(flags ? '|' : ')', stdout);
2335: }
2336: }
1.27 espie 2337: printf("\n#\tTo: ");
2338: Lst_Every(s->parents, SuffPrintName);
2339: printf("\n#\tFrom: ");
2340: Lst_Every(s->children, SuffPrintName);
2341: printf("\n#\tSearch Path: ");
2342: Dir_PrintPath(s->searchPath);
2343: fputc('\n', stdout);
1.1 deraadt 2344: }
2345:
1.27 espie 2346: static void
2347: SuffPrintTrans(tp)
1.28 ! espie 2348: void *tp;
1.1 deraadt 2349: {
1.27 espie 2350: GNode *t = (GNode *)tp;
1.1 deraadt 2351:
1.27 espie 2352: printf("%-16s: ", t->name);
2353: Targ_PrintType(t->type);
2354: fputc('\n', stdout);
2355: Lst_Every(t->commands, Targ_PrintCmd);
2356: fputc('\n', stdout);
1.1 deraadt 2357: }
2358:
2359: void
2360: Suff_PrintAll()
2361: {
1.27 espie 2362: printf("#*** Suffixes:\n");
2363: Lst_Every(sufflist, SuffPrintSuff);
1.1 deraadt 2364:
1.27 espie 2365: printf("#*** Transformations:\n");
2366: Lst_Every(transforms, SuffPrintTrans);
1.1 deraadt 2367: }