VirtualBox

source: kBuild/trunk/src/kmk/suff.c@ 151

Last change on this file since 151 was 51, checked in by bird, 22 years ago

kMk and porting to kLib.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette