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