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