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