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