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