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