VirtualBox

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

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

kMk changes. Made extensions configurable from config.h. fixed parents.

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

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