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