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