suff.c revision 1.46 1 /* $NetBSD: suff.c,v 1.46 2004/12/29 00:43:02 christos 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 #ifndef MAKE_NATIVE
72 static char rcsid[] = "$NetBSD: suff.c,v 1.46 2004/12/29 00:43:02 christos 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.46 2004/12/29 00:43:02 christos 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 suffixes 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 char *cp; /* Expanded value */
1510
1511 /*
1512 * First do variable expansion -- this takes precedence over
1513 * wildcard expansion. If the result contains wildcards, they'll be gotten
1514 * to later since the resulting words are tacked on to the end of
1515 * the children list.
1516 */
1517 if (strchr(cgn->name, '$') != (char *)NULL) {
1518 if (DEBUG(SUFF)) {
1519 printf("Expanding \"%s\"...", cgn->name);
1520 }
1521 cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
1522
1523 if (cp != (char *)NULL) {
1524 Lst members = Lst_Init(FALSE);
1525
1526 if (cgn->type & OP_ARCHV) {
1527 /*
1528 * Node was an archive(member) target, so we want to call
1529 * on the Arch module to find the nodes for us, expanding
1530 * variables in the parent's context.
1531 */
1532 char *sacrifice = cp;
1533
1534 (void)Arch_ParseArchive(&sacrifice, members, pgn);
1535 } else {
1536 /*
1537 * Break the result into a vector of strings whose nodes
1538 * we can find, then add those nodes to the members list.
1539 * Unfortunately, we can't use brk_string b/c it
1540 * doesn't understand about variable specifications with
1541 * spaces in them...
1542 */
1543 char *start;
1544 char *initcp = cp; /* For freeing... */
1545
1546 for (start = cp; *start == ' ' || *start == '\t'; start++)
1547 continue;
1548 for (cp = start; *cp != '\0'; cp++) {
1549 if (*cp == ' ' || *cp == '\t') {
1550 /*
1551 * White-space -- terminate element, find the node,
1552 * add it, skip any further spaces.
1553 */
1554 *cp++ = '\0';
1555 gn = Targ_FindNode(start, TARG_CREATE);
1556 (void)Lst_AtEnd(members, (ClientData)gn);
1557 while (*cp == ' ' || *cp == '\t') {
1558 cp++;
1559 }
1560 /*
1561 * Adjust cp for increment at start of loop, but
1562 * set start to first non-space.
1563 */
1564 start = cp--;
1565 } else if (*cp == '$') {
1566 /*
1567 * Start of a variable spec -- contact variable module
1568 * to find the end so we can skip over it.
1569 */
1570 char *junk;
1571 int len;
1572 Boolean doFree;
1573
1574 junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
1575 if (junk != var_Error) {
1576 cp += len - 1;
1577 }
1578
1579 if (doFree) {
1580 free(junk);
1581 }
1582 } else if (*cp == '\\' && *cp != '\0') {
1583 /*
1584 * Escaped something -- skip over it
1585 */
1586 cp++;
1587 }
1588 }
1589
1590 if (cp != start) {
1591 /*
1592 * Stuff left over -- add it to the list too
1593 */
1594 gn = Targ_FindNode(start, TARG_CREATE);
1595 (void)Lst_AtEnd(members, (ClientData)gn);
1596 }
1597 /*
1598 * Point cp back at the beginning again so the variable value
1599 * can be freed.
1600 */
1601 cp = initcp;
1602 }
1603 /*
1604 * Add all elements of the members list to the parent node.
1605 */
1606 while(!Lst_IsEmpty(members)) {
1607 gn = (GNode *)Lst_DeQueue(members);
1608
1609 if (DEBUG(SUFF)) {
1610 printf("%s...", gn->name);
1611 }
1612 if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
1613 (void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
1614 prevLN = Lst_Succ(prevLN);
1615 (void)Lst_AtEnd(gn->parents, (ClientData)pgn);
1616 pgn->unmade++;
1617 }
1618 }
1619 Lst_Destroy(members, NOFREE);
1620 /*
1621 * Free the result
1622 */
1623 free((char *)cp);
1624 }
1625 /*
1626 * Now the source is expanded, remove it from the list of children to
1627 * keep it from being processed.
1628 */
1629 if (DEBUG(SUFF)) {
1630 printf("\n");
1631 }
1632 return(1);
1633 } else if (Dir_HasWildcards(cgn->name)) {
1634 Lst explist; /* List of expansions */
1635 Lst path; /* Search path along which to expand */
1636
1637 path = Suff_FindPath(cgn);
1638
1639
1640 /*
1641 * Expand the word along the chosen path
1642 */
1643 explist = Lst_Init(FALSE);
1644 Dir_Expand(cgn->name, path, explist);
1645
1646 while (!Lst_IsEmpty(explist)) {
1647 /*
1648 * Fetch next expansion off the list and find its GNode
1649 */
1650 cp = (char *)Lst_DeQueue(explist);
1651
1652 if (DEBUG(SUFF)) {
1653 printf("%s...", cp);
1654 }
1655 gn = Targ_FindNode(cp, TARG_CREATE);
1656
1657 /*
1658 * If gn isn't already a child of the parent, make it so and
1659 * up the parent's count of unmade children.
1660 */
1661 if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
1662 (void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
1663 prevLN = Lst_Succ(prevLN);
1664 (void)Lst_AtEnd(gn->parents, (ClientData)pgn);
1665 pgn->unmade++;
1666 }
1667 }
1668
1669 /*
1670 * Nuke what's left of the list
1671 */
1672 Lst_Destroy(explist, NOFREE);
1673
1674 /*
1675 * Now the source is expanded, remove it from the list of children to
1676 * keep it from being processed.
1677 */
1678 if (DEBUG(SUFF)) {
1679 printf("\n");
1680 }
1681 return(1);
1682 }
1683
1684 return(0);
1685 }
1686
1687 /*
1688 * Find a path along which to expand the word.
1689 *
1690 * If the word has a known suffix, use that path.
1691 * If it has no known suffix and we're allowed to use the null
1692 * suffix, use its path.
1693 * Else use the default system search path.
1694 */
1695 Lst
1696 Suff_FindPath(GNode* gn)
1697 {
1698 Suff *suff = gn->suffix;
1699
1700 if (suff == NULL) {
1701 SuffixCmpData sd; /* Search string data */
1702 LstNode ln;
1703 sd.len = strlen(gn->name);
1704 sd.ename = gn->name + sd.len;
1705 ln = Lst_Find(sufflist, (ClientData)&sd, SuffSuffIsSuffixP);
1706
1707 if (DEBUG(SUFF)) {
1708 printf("Wildcard expanding \"%s\"...", gn->name);
1709 }
1710 if (ln != NILLNODE)
1711 suff = (Suff *)Lst_Datum(ln);
1712 /* XXX: Here we can save the suffix so we don't have to do this again */
1713 }
1714
1715 if (suff != NULL) {
1716 if (DEBUG(SUFF)) {
1717 printf("suffix is \"%s\"...", suff->name);
1718 }
1719 return suff->searchPath;
1720 } else {
1721 /*
1722 * Use default search path
1723 */
1724 return dirSearchPath;
1725 }
1726 }
1727
1728 /*-
1729 *-----------------------------------------------------------------------
1730 * SuffApplyTransform --
1731 * Apply a transformation rule, given the source and target nodes
1732 * and suffixes.
1733 *
1734 * Input:
1735 * tGn Target node
1736 * sGn Source node
1737 * t Target suffix
1738 * s Source suffix
1739 *
1740 * Results:
1741 * TRUE if successful, FALSE if not.
1742 *
1743 * Side Effects:
1744 * The source and target are linked and the commands from the
1745 * transformation are added to the target node's commands list.
1746 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1747 * to the target. The target also inherits all the sources for
1748 * the transformation rule.
1749 *
1750 *-----------------------------------------------------------------------
1751 */
1752 static Boolean
1753 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
1754 {
1755 LstNode ln, nln; /* General node */
1756 char *tname; /* Name of transformation rule */
1757 GNode *gn; /* Node for same */
1758
1759 /*
1760 * Form the proper links between the target and source.
1761 */
1762 (void)Lst_AtEnd(tGn->children, (ClientData)sGn);
1763 (void)Lst_AtEnd(sGn->parents, (ClientData)tGn);
1764 tGn->unmade += 1;
1765
1766 /*
1767 * Locate the transformation rule itself
1768 */
1769 tname = str_concat(s->name, t->name, 0);
1770 ln = Lst_Find(transforms, (ClientData)tname, SuffGNHasNameP);
1771 free(tname);
1772
1773 if (ln == NILLNODE) {
1774 /*
1775 * Not really such a transformation rule (can happen when we're
1776 * called to link an OP_MEMBER and OP_ARCHV node), so return
1777 * FALSE.
1778 */
1779 return(FALSE);
1780 }
1781
1782 gn = (GNode *)Lst_Datum(ln);
1783
1784 if (DEBUG(SUFF)) {
1785 printf("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
1786 }
1787
1788 /*
1789 * Record last child for expansion purposes
1790 */
1791 ln = Lst_Last(tGn->children);
1792
1793 /*
1794 * Pass the buck to Make_HandleUse to apply the rule
1795 */
1796 (void)Make_HandleUse(gn, tGn);
1797
1798 /*
1799 * Deal with wildcards and variables in any acquired sources
1800 */
1801 ln = Lst_Succ(ln);
1802 while (ln != NILLNODE) {
1803 if (SuffExpandChildren(ln, tGn)) {
1804 nln = Lst_Succ(ln);
1805 tGn->unmade--;
1806 Lst_Remove(tGn->children, ln);
1807 ln = nln;
1808 } else
1809 ln = Lst_Succ(ln);
1810 }
1811
1812 /*
1813 * Keep track of another parent to which this beast is transformed so
1814 * the .IMPSRC variable can be set correctly for the parent.
1815 */
1816 (void)Lst_AtEnd(sGn->iParents, (ClientData)tGn);
1817
1818 return(TRUE);
1819 }
1820
1821
1822 /*-
1823 *-----------------------------------------------------------------------
1824 * SuffFindArchiveDeps --
1825 * Locate dependencies for an OP_ARCHV node.
1826 *
1827 * Input:
1828 * gn Node for which to locate dependencies
1829 *
1830 * Results:
1831 * None
1832 *
1833 * Side Effects:
1834 * Same as Suff_FindDeps
1835 *
1836 *-----------------------------------------------------------------------
1837 */
1838 static void
1839 SuffFindArchiveDeps(GNode *gn, Lst slst)
1840 {
1841 char *eoarch; /* End of archive portion */
1842 char *eoname; /* End of member portion */
1843 GNode *mem; /* Node for member */
1844 static const char *copy[] = {
1845 /* Variables to be copied from the member node */
1846 TARGET, /* Must be first */
1847 PREFIX, /* Must be second */
1848 };
1849 int i; /* Index into copy and vals */
1850 Suff *ms; /* Suffix descriptor for member */
1851 char *name; /* Start of member's name */
1852
1853 /*
1854 * The node is an archive(member) pair. so we must find a
1855 * suffix for both of them.
1856 */
1857 eoarch = strchr (gn->name, '(');
1858 eoname = strchr (eoarch, ')');
1859
1860 *eoname = '\0'; /* Nuke parentheses during suffix search */
1861 *eoarch = '\0'; /* So a suffix can be found */
1862
1863 name = eoarch + 1;
1864
1865 /*
1866 * To simplify things, call Suff_FindDeps recursively on the member now,
1867 * so we can simply compare the member's .PREFIX and .TARGET variables
1868 * to locate its suffix. This allows us to figure out the suffix to
1869 * use for the archive without having to do a quadratic search over the
1870 * suffix list, backtracking for each one...
1871 */
1872 mem = Targ_FindNode(name, TARG_CREATE);
1873 SuffFindDeps(mem, slst);
1874
1875 /*
1876 * Create the link between the two nodes right off
1877 */
1878 (void)Lst_AtEnd(gn->children, (ClientData)mem);
1879 (void)Lst_AtEnd(mem->parents, (ClientData)gn);
1880 gn->unmade += 1;
1881
1882 /*
1883 * Copy in the variables from the member node to this one.
1884 */
1885 for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
1886 char *p1;
1887 Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn, 0);
1888 if (p1)
1889 free(p1);
1890
1891 }
1892
1893 ms = mem->suffix;
1894 if (ms == NULL) {
1895 /*
1896 * Didn't know what it was -- use .NULL suffix if not in make mode
1897 */
1898 if (DEBUG(SUFF)) {
1899 printf("using null suffix\n");
1900 }
1901 ms = suffNull;
1902 }
1903
1904
1905 /*
1906 * Set the other two local variables required for this target.
1907 */
1908 Var_Set (MEMBER, name, gn, 0);
1909 Var_Set (ARCHIVE, gn->name, gn, 0);
1910
1911 if (ms != NULL) {
1912 /*
1913 * Member has a known suffix, so look for a transformation rule from
1914 * it to a possible suffix of the archive. Rather than searching
1915 * through the entire list, we just look at suffixes to which the
1916 * member's suffix may be transformed...
1917 */
1918 LstNode ln;
1919 SuffixCmpData sd; /* Search string data */
1920
1921 /*
1922 * Use first matching suffix...
1923 */
1924 sd.len = eoarch - gn->name;
1925 sd.ename = eoarch;
1926 ln = Lst_Find(ms->parents, &sd, SuffSuffIsSuffixP);
1927
1928 if (ln != NILLNODE) {
1929 /*
1930 * Got one -- apply it
1931 */
1932 if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) &&
1933 DEBUG(SUFF))
1934 {
1935 printf("\tNo transformation from %s -> %s\n",
1936 ms->name, ((Suff *)Lst_Datum(ln))->name);
1937 }
1938 }
1939 }
1940
1941 /*
1942 * Replace the opening and closing parens now we've no need of the separate
1943 * pieces.
1944 */
1945 *eoarch = '('; *eoname = ')';
1946
1947 /*
1948 * Pretend gn appeared to the left of a dependency operator so
1949 * the user needn't provide a transformation from the member to the
1950 * archive.
1951 */
1952 if (OP_NOP(gn->type)) {
1953 gn->type |= OP_DEPENDS;
1954 }
1955
1956 /*
1957 * Flag the member as such so we remember to look in the archive for
1958 * its modification time.
1959 */
1960 mem->type |= OP_MEMBER;
1961 }
1962
1963 /*-
1964 *-----------------------------------------------------------------------
1965 * SuffFindNormalDeps --
1966 * Locate implicit dependencies for regular targets.
1967 *
1968 * Input:
1969 * gn Node for which to find sources
1970 *
1971 * Results:
1972 * None.
1973 *
1974 * Side Effects:
1975 * Same as Suff_FindDeps...
1976 *
1977 *-----------------------------------------------------------------------
1978 */
1979 static void
1980 SuffFindNormalDeps(GNode *gn, Lst slst)
1981 {
1982 char *eoname; /* End of name */
1983 char *sopref; /* Start of prefix */
1984 LstNode ln, nln; /* Next suffix node to check */
1985 Lst srcs; /* List of sources at which to look */
1986 Lst targs; /* List of targets to which things can be
1987 * transformed. They all have the same file,
1988 * but different suff and pref fields */
1989 Src *bottom; /* Start of found transformation path */
1990 Src *src; /* General Src pointer */
1991 char *pref; /* Prefix to use */
1992 Src *targ; /* General Src target pointer */
1993 SuffixCmpData sd; /* Search string data */
1994
1995
1996 sd.len = strlen(gn->name);
1997 sd.ename = eoname = gn->name + sd.len;
1998
1999 sopref = gn->name;
2000
2001 /*
2002 * Begin at the beginning...
2003 */
2004 ln = Lst_First(sufflist);
2005 srcs = Lst_Init(FALSE);
2006 targs = Lst_Init(FALSE);
2007
2008 /*
2009 * We're caught in a catch-22 here. On the one hand, we want to use any
2010 * transformation implied by the target's sources, but we can't examine
2011 * the sources until we've expanded any variables/wildcards they may hold,
2012 * and we can't do that until we've set up the target's local variables
2013 * and we can't do that until we know what the proper suffix for the
2014 * target is (in case there are two suffixes one of which is a suffix of
2015 * the other) and we can't know that until we've found its implied
2016 * source, which we may not want to use if there's an existing source
2017 * that implies a different transformation.
2018 *
2019 * In an attempt to get around this, which may not work all the time,
2020 * but should work most of the time, we look for implied sources first,
2021 * checking transformations to all possible suffixes of the target,
2022 * use what we find to set the target's local variables, expand the
2023 * children, then look for any overriding transformations they imply.
2024 * Should we find one, we discard the one we found before.
2025 */
2026
2027 while (ln != NILLNODE) {
2028 /*
2029 * Look for next possible suffix...
2030 */
2031 ln = Lst_FindFrom(sufflist, ln, &sd, SuffSuffIsSuffixP);
2032
2033 if (ln != NILLNODE) {
2034 int prefLen; /* Length of the prefix */
2035
2036 /*
2037 * Allocate a Src structure to which things can be transformed
2038 */
2039 targ = (Src *)emalloc(sizeof (Src));
2040 targ->file = estrdup(gn->name);
2041 targ->suff = (Suff *)Lst_Datum(ln);
2042 targ->suff->refCount++;
2043 targ->node = gn;
2044 targ->parent = (Src *)NULL;
2045 targ->children = 0;
2046 #ifdef DEBUG_SRC
2047 targ->cp = Lst_Init(FALSE);
2048 #endif
2049
2050 /*
2051 * Allocate room for the prefix, whose end is found by subtracting
2052 * the length of the suffix from the end of the name.
2053 */
2054 prefLen = (eoname - targ->suff->nameLen) - sopref;
2055 targ->pref = emalloc(prefLen + 1);
2056 memcpy(targ->pref, sopref, prefLen);
2057 targ->pref[prefLen] = '\0';
2058
2059 /*
2060 * Add nodes from which the target can be made
2061 */
2062 SuffAddLevel(srcs, targ);
2063
2064 /*
2065 * Record the target so we can nuke it
2066 */
2067 (void)Lst_AtEnd(targs, (ClientData)targ);
2068
2069 /*
2070 * Search from this suffix's successor...
2071 */
2072 ln = Lst_Succ(ln);
2073 }
2074 }
2075
2076 /*
2077 * Handle target of unknown suffix...
2078 */
2079 if (Lst_IsEmpty(targs) && suffNull != NULL) {
2080 if (DEBUG(SUFF)) {
2081 printf("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
2082 }
2083
2084 targ = (Src *)emalloc(sizeof (Src));
2085 targ->file = estrdup(gn->name);
2086 targ->suff = suffNull;
2087 targ->suff->refCount++;
2088 targ->node = gn;
2089 targ->parent = (Src *)NULL;
2090 targ->children = 0;
2091 targ->pref = estrdup(sopref);
2092 #ifdef DEBUG_SRC
2093 targ->cp = Lst_Init(FALSE);
2094 #endif
2095
2096 /*
2097 * Only use the default suffix rules if we don't have commands
2098 * defined for this gnode; traditional make programs used to
2099 * not define suffix rules if the gnode had children but we
2100 * don't do this anymore.
2101 */
2102 if (Lst_IsEmpty(gn->commands))
2103 SuffAddLevel(srcs, targ);
2104 else {
2105 if (DEBUG(SUFF))
2106 printf("not ");
2107 }
2108
2109 if (DEBUG(SUFF))
2110 printf("adding suffix rules\n");
2111
2112 (void)Lst_AtEnd(targs, (ClientData)targ);
2113 }
2114
2115 /*
2116 * Using the list of possible sources built up from the target suffix(es),
2117 * try and find an existing file/target that matches.
2118 */
2119 bottom = SuffFindThem(srcs, slst);
2120
2121 if (bottom == (Src *)NULL) {
2122 /*
2123 * No known transformations -- use the first suffix found for setting
2124 * the local variables.
2125 */
2126 if (!Lst_IsEmpty(targs)) {
2127 targ = (Src *)Lst_Datum(Lst_First(targs));
2128 } else {
2129 targ = (Src *)NULL;
2130 }
2131 } else {
2132 /*
2133 * Work up the transformation path to find the suffix of the
2134 * target to which the transformation was made.
2135 */
2136 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
2137 continue;
2138 }
2139
2140 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
2141
2142 pref = (targ != NULL) ? targ->pref : gn->name;
2143 Var_Set(PREFIX, pref, gn, 0);
2144
2145 /*
2146 * Now we've got the important local variables set, expand any sources
2147 * that still contain variables or wildcards in their names.
2148 */
2149 ln = Lst_First(gn->children);
2150 while (ln != NILLNODE) {
2151 if (SuffExpandChildren(ln, gn)) {
2152 nln = Lst_Succ(ln);
2153 gn->unmade--;
2154 Lst_Remove(gn->children, ln);
2155 ln = nln;
2156 } else
2157 ln = Lst_Succ(ln);
2158 }
2159
2160 if (targ == NULL) {
2161 if (DEBUG(SUFF)) {
2162 printf("\tNo valid suffix on %s\n", gn->name);
2163 }
2164
2165 sfnd_abort:
2166 /*
2167 * Deal with finding the thing on the default search path. We
2168 * always do that, not only if the node is only a source (not
2169 * on the lhs of a dependency operator or [XXX] it has neither
2170 * children or commands) as the old pmake did.
2171 */
2172 if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
2173 free(gn->path);
2174 gn->path = Dir_FindFile(gn->name,
2175 (targ == NULL ? dirSearchPath :
2176 targ->suff->searchPath));
2177 if (gn->path != NULL) {
2178 char *ptr;
2179 Var_Set(TARGET, gn->path, gn, 0);
2180
2181 if (targ != NULL) {
2182 /*
2183 * Suffix known for the thing -- trim the suffix off
2184 * the path to form the proper .PREFIX variable.
2185 */
2186 int savep = strlen(gn->path) - targ->suff->nameLen;
2187 char savec;
2188
2189 if (gn->suffix)
2190 gn->suffix->refCount--;
2191 gn->suffix = targ->suff;
2192 gn->suffix->refCount++;
2193
2194 savec = gn->path[savep];
2195 gn->path[savep] = '\0';
2196
2197 if ((ptr = strrchr(gn->path, '/')) != NULL)
2198 ptr++;
2199 else
2200 ptr = gn->path;
2201
2202 Var_Set(PREFIX, ptr, gn, 0);
2203
2204 gn->path[savep] = savec;
2205 } else {
2206 /*
2207 * The .PREFIX gets the full path if the target has
2208 * no known suffix.
2209 */
2210 if (gn->suffix)
2211 gn->suffix->refCount--;
2212 gn->suffix = NULL;
2213
2214 if ((ptr = strrchr(gn->path, '/')) != NULL)
2215 ptr++;
2216 else
2217 ptr = gn->path;
2218
2219 Var_Set(PREFIX, ptr, gn, 0);
2220 }
2221 }
2222 }
2223
2224 goto sfnd_return;
2225 }
2226
2227 /*
2228 * If the suffix indicates that the target is a library, mark that in
2229 * the node's type field.
2230 */
2231 if (targ->suff->flags & SUFF_LIBRARY) {
2232 gn->type |= OP_LIB;
2233 }
2234
2235 /*
2236 * Check for overriding transformation rule implied by sources
2237 */
2238 if (!Lst_IsEmpty(gn->children)) {
2239 src = SuffFindCmds(targ, slst);
2240
2241 if (src != (Src *)NULL) {
2242 /*
2243 * Free up all the Src structures in the transformation path
2244 * up to, but not including, the parent node.
2245 */
2246 while (bottom && bottom->parent != NULL) {
2247 if (Lst_Member(slst, (ClientData) bottom) == NILLNODE) {
2248 Lst_AtEnd(slst, (ClientData) bottom);
2249 }
2250 bottom = bottom->parent;
2251 }
2252 bottom = src;
2253 }
2254 }
2255
2256 if (bottom == NULL) {
2257 /*
2258 * No idea from where it can come -- return now.
2259 */
2260 goto sfnd_abort;
2261 }
2262
2263 /*
2264 * We now have a list of Src structures headed by 'bottom' and linked via
2265 * their 'parent' pointers. What we do next is create links between
2266 * source and target nodes (which may or may not have been created)
2267 * and set the necessary local variables in each target. The
2268 * commands for each target are set from the commands of the
2269 * transformation rule used to get from the src suffix to the targ
2270 * suffix. Note that this causes the commands list of the original
2271 * node, gn, to be replaced by the commands of the final
2272 * transformation rule. Also, the unmade field of gn is incremented.
2273 * Etc.
2274 */
2275 if (bottom->node == NILGNODE) {
2276 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2277 }
2278
2279 for (src = bottom; src->parent != (Src *)NULL; src = src->parent) {
2280 targ = src->parent;
2281
2282 if (src->node->suffix)
2283 src->node->suffix->refCount--;
2284 src->node->suffix = src->suff;
2285 src->node->suffix->refCount++;
2286
2287 if (targ->node == NILGNODE) {
2288 targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2289 }
2290
2291 SuffApplyTransform(targ->node, src->node,
2292 targ->suff, src->suff);
2293
2294 if (targ->node != gn) {
2295 /*
2296 * Finish off the dependency-search process for any nodes
2297 * between bottom and gn (no point in questing around the
2298 * filesystem for their implicit source when it's already
2299 * known). Note that the node can't have any sources that
2300 * need expanding, since SuffFindThem will stop on an existing
2301 * node, so all we need to do is set the standard and System V
2302 * variables.
2303 */
2304 targ->node->type |= OP_DEPS_FOUND;
2305
2306 Var_Set(PREFIX, targ->pref, targ->node, 0);
2307
2308 Var_Set(TARGET, targ->node->name, targ->node, 0);
2309 }
2310 }
2311
2312 if (gn->suffix)
2313 gn->suffix->refCount--;
2314 gn->suffix = src->suff;
2315 gn->suffix->refCount++;
2316
2317 /*
2318 * Nuke the transformation path and the Src structures left over in the
2319 * two lists.
2320 */
2321 sfnd_return:
2322 if (bottom)
2323 if (Lst_Member(slst, (ClientData) bottom) == NILLNODE)
2324 Lst_AtEnd(slst, (ClientData) bottom);
2325
2326 while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
2327 continue;
2328
2329 Lst_Concat(slst, srcs, LST_CONCLINK);
2330 Lst_Concat(slst, targs, LST_CONCLINK);
2331 }
2332
2333
2334 /*-
2335 *-----------------------------------------------------------------------
2336 * Suff_FindDeps --
2337 * Find implicit sources for the target described by the graph node
2338 * gn
2339 *
2340 * Results:
2341 * Nothing.
2342 *
2343 * Side Effects:
2344 * Nodes are added to the graph below the passed-in node. The nodes
2345 * are marked to have their IMPSRC variable filled in. The
2346 * PREFIX variable is set for the given node and all its
2347 * implied children.
2348 *
2349 * Notes:
2350 * The path found by this target is the shortest path in the
2351 * transformation graph, which may pass through non-existent targets,
2352 * to an existing target. The search continues on all paths from the
2353 * root suffix until a file is found. I.e. if there's a path
2354 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2355 * the .c and .l files don't, the search will branch out in
2356 * all directions from .o and again from all the nodes on the
2357 * next level until the .l,v node is encountered.
2358 *
2359 *-----------------------------------------------------------------------
2360 */
2361
2362 void
2363 Suff_FindDeps(GNode *gn)
2364 {
2365
2366 SuffFindDeps(gn, srclist);
2367 while (SuffRemoveSrc(srclist))
2368 continue;
2369 }
2370
2371
2372 /*
2373 * Input:
2374 * gn node we're dealing with
2375 *
2376 */
2377 static void
2378 SuffFindDeps(GNode *gn, Lst slst)
2379 {
2380 if (gn->type & (OP_DEPS_FOUND|OP_PHONY)) {
2381 /*
2382 * If dependencies already found, no need to do it again...
2383 * If this is a .PHONY target, we do not apply suffix rules.
2384 */
2385 return;
2386 } else {
2387 gn->type |= OP_DEPS_FOUND;
2388 }
2389
2390 if (DEBUG(SUFF)) {
2391 printf ("SuffFindDeps (%s)\n", gn->name);
2392 }
2393
2394 if (gn->type & OP_ARCHV) {
2395 SuffFindArchiveDeps(gn, slst);
2396 } else if (gn->type & OP_LIB) {
2397 /*
2398 * If the node is a library, it is the arch module's job to find it
2399 * and set the TARGET variable accordingly. We merely provide the
2400 * search path, assuming all libraries end in ".a" (if the suffix
2401 * hasn't been defined, there's nothing we can do for it, so we just
2402 * set the TARGET variable to the node's name in order to give it a
2403 * value).
2404 */
2405 LstNode ln;
2406 Suff *s;
2407
2408 ln = Lst_Find (sufflist, (ClientData)UNCONST(LIBSUFF),
2409 SuffSuffHasNameP);
2410 if (gn->suffix)
2411 gn->suffix->refCount--;
2412 if (ln != NILLNODE) {
2413 gn->suffix = s = (Suff *) Lst_Datum (ln);
2414 gn->suffix->refCount++;
2415 Arch_FindLib (gn, s->searchPath);
2416 } else {
2417 gn->suffix = NULL;
2418 Var_Set (TARGET, gn->name, gn, 0);
2419 }
2420 /*
2421 * Because a library (-lfoo) target doesn't follow the standard
2422 * filesystem conventions, we don't set the regular variables for
2423 * the thing. .PREFIX is simply made empty...
2424 */
2425 Var_Set(PREFIX, "", gn, 0);
2426 } else {
2427 SuffFindNormalDeps(gn, slst);
2428 }
2429 }
2430
2431 /*-
2432 *-----------------------------------------------------------------------
2433 * Suff_SetNull --
2434 * Define which suffix is the null suffix.
2435 *
2436 * Input:
2437 * name Name of null suffix
2438 *
2439 * Results:
2440 * None.
2441 *
2442 * Side Effects:
2443 * 'suffNull' is altered.
2444 *
2445 * Notes:
2446 * Need to handle the changing of the null suffix gracefully so the
2447 * old transformation rules don't just go away.
2448 *
2449 *-----------------------------------------------------------------------
2450 */
2451 void
2452 Suff_SetNull(char *name)
2453 {
2454 Suff *s;
2455 LstNode ln;
2456
2457 ln = Lst_Find(sufflist, (ClientData)name, SuffSuffHasNameP);
2458 if (ln != NILLNODE) {
2459 s = (Suff *)Lst_Datum(ln);
2460 if (suffNull != (Suff *)NULL) {
2461 suffNull->flags &= ~SUFF_NULL;
2462 }
2463 s->flags |= SUFF_NULL;
2464 /*
2465 * XXX: Here's where the transformation mangling would take place
2466 */
2467 suffNull = s;
2468 } else {
2469 Parse_Error (PARSE_WARNING, "Desired null suffix %s not defined.",
2470 name);
2471 }
2472 }
2473
2474 /*-
2475 *-----------------------------------------------------------------------
2476 * Suff_Init --
2477 * Initialize suffixes module
2478 *
2479 * Results:
2480 * None
2481 *
2482 * Side Effects:
2483 * Many
2484 *-----------------------------------------------------------------------
2485 */
2486 void
2487 Suff_Init(void)
2488 {
2489 sufflist = Lst_Init (FALSE);
2490 #ifdef CLEANUP
2491 suffClean = Lst_Init(FALSE);
2492 #endif
2493 srclist = Lst_Init (FALSE);
2494 transforms = Lst_Init (FALSE);
2495
2496 sNum = 0;
2497 /*
2498 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2499 * actually go on the suffix list or everyone will think that's its
2500 * suffix.
2501 */
2502 emptySuff = suffNull = (Suff *) emalloc (sizeof (Suff));
2503
2504 suffNull->name = estrdup ("");
2505 suffNull->nameLen = 0;
2506 suffNull->searchPath = Lst_Init (FALSE);
2507 Dir_Concat(suffNull->searchPath, dirSearchPath);
2508 suffNull->children = Lst_Init (FALSE);
2509 suffNull->parents = Lst_Init (FALSE);
2510 suffNull->ref = Lst_Init (FALSE);
2511 suffNull->sNum = sNum++;
2512 suffNull->flags = SUFF_NULL;
2513 suffNull->refCount = 1;
2514
2515 }
2516
2517
2518 /*-
2519 *----------------------------------------------------------------------
2520 * Suff_End --
2521 * Cleanup the this module
2522 *
2523 * Results:
2524 * None
2525 *
2526 * Side Effects:
2527 * The memory is free'd.
2528 *----------------------------------------------------------------------
2529 */
2530
2531 void
2532 Suff_End(void)
2533 {
2534 #ifdef CLEANUP
2535 Lst_Destroy(sufflist, SuffFree);
2536 Lst_Destroy(suffClean, SuffFree);
2537 if (suffNull)
2538 SuffFree(suffNull);
2539 Lst_Destroy(srclist, NOFREE);
2540 Lst_Destroy(transforms, NOFREE);
2541 #endif
2542 }
2543
2544
2545 /********************* DEBUGGING FUNCTIONS **********************/
2546
2547 static int SuffPrintName(ClientData s, ClientData dummy)
2548 {
2549 printf ("%s ", ((Suff *) s)->name);
2550 return (dummy ? 0 : 0);
2551 }
2552
2553 static int
2554 SuffPrintSuff(ClientData sp, ClientData dummy)
2555 {
2556 Suff *s = (Suff *) sp;
2557 int flags;
2558 int flag;
2559
2560 printf ("# `%s' [%d] ", s->name, s->refCount);
2561
2562 flags = s->flags;
2563 if (flags) {
2564 fputs (" (", stdout);
2565 while (flags) {
2566 flag = 1 << (ffs(flags) - 1);
2567 flags &= ~flag;
2568 switch (flag) {
2569 case SUFF_NULL:
2570 printf ("NULL");
2571 break;
2572 case SUFF_INCLUDE:
2573 printf ("INCLUDE");
2574 break;
2575 case SUFF_LIBRARY:
2576 printf ("LIBRARY");
2577 break;
2578 }
2579 fputc(flags ? '|' : ')', stdout);
2580 }
2581 }
2582 fputc ('\n', stdout);
2583 printf ("#\tTo: ");
2584 Lst_ForEach (s->parents, SuffPrintName, (ClientData)0);
2585 fputc ('\n', stdout);
2586 printf ("#\tFrom: ");
2587 Lst_ForEach (s->children, SuffPrintName, (ClientData)0);
2588 fputc ('\n', stdout);
2589 printf ("#\tSearch Path: ");
2590 Dir_PrintPath (s->searchPath);
2591 fputc ('\n', stdout);
2592 return (dummy ? 0 : 0);
2593 }
2594
2595 static int
2596 SuffPrintTrans(ClientData tp, ClientData dummy)
2597 {
2598 GNode *t = (GNode *) tp;
2599
2600 printf ("%-16s: ", t->name);
2601 Targ_PrintType (t->type);
2602 fputc ('\n', stdout);
2603 Lst_ForEach (t->commands, Targ_PrintCmd, (ClientData)0);
2604 fputc ('\n', stdout);
2605 return(dummy ? 0 : 0);
2606 }
2607
2608 void
2609 Suff_PrintAll(void)
2610 {
2611 printf ("#*** Suffixes:\n");
2612 Lst_ForEach (sufflist, SuffPrintSuff, (ClientData)0);
2613
2614 printf ("#*** Transformations:\n");
2615 Lst_ForEach (transforms, SuffPrintTrans, (ClientData)0);
2616 }
2617