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