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