suff.c revision 1.87 1 /* $NetBSD: suff.c,v 1.87 2020/07/02 15:47:38 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.87 2020/07/02 15:47:38 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.87 2020/07/02 15:47:38 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 /*
154 * Structure describing an individual suffix.
155 */
156 typedef struct _Suff {
157 char *name; /* The suffix itself */
158 int nameLen; /* Length of the suffix */
159 short flags; /* Type of suffix */
160 #define SUFF_INCLUDE 0x01 /* One which is #include'd */
161 #define SUFF_LIBRARY 0x02 /* One which contains a library */
162 #define SUFF_NULL 0x04 /* The empty 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_Member(l, sp);
408 if (ln != NULL) {
409 Lst_Remove(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 /*-
478 *-----------------------------------------------------------------------
479 * SuffInsert --
480 * Insert the suffix into the list keeping the list ordered by suffix
481 * numbers.
482 *
483 * Input:
484 * l the list where in s should be inserted
485 * s the suffix to insert
486 *
487 * Results:
488 * None
489 *
490 * Side Effects:
491 * The reference count of the suffix is incremented
492 *-----------------------------------------------------------------------
493 */
494 static void
495 SuffInsert(Lst l, Suff *s)
496 {
497 LstNode ln; /* current element in l we're examining */
498 Suff *s2 = NULL; /* the suffix descriptor in this element */
499
500 if (Lst_Open(l) == FAILURE) {
501 return;
502 }
503 while ((ln = Lst_Next(l)) != NULL) {
504 s2 = (Suff *)Lst_Datum(ln);
505 if (s2->sNum >= s->sNum) {
506 break;
507 }
508 }
509
510 Lst_Close(l);
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 (void)Lst_AtEnd(l, s);
519 s->refCount++;
520 (void)Lst_AtEnd(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 (void)Lst_InsertBefore(l, ln, s);
526 s->refCount++;
527 (void)Lst_AtEnd(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(FALSE);
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(FALSE);
565 Dir_Concat(suffNull->searchPath, dirSearchPath);
566 suffNull->children = Lst_Init(FALSE);
567 suffNull->parents = Lst_Init(FALSE);
568 suffNull->ref = Lst_Init(FALSE);
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 = (Suff *)Lst_Datum(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 = (Suff *)Lst_Datum(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 (void)Lst_AtEnd(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 = (GNode *)Lst_Datum(ln);
722 Lst_Destroy(gn->commands, NULL);
723 Lst_Destroy(gn->children, NULL);
724 gn->commands = Lst_Init(FALSE);
725 gn->children = Lst_Init(FALSE);
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 = (GNode *)Lst_Datum(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 = (Suff *)Lst_Datum(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 = (Suff *)Lst_Datum(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 ((unsigned int)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(FALSE);
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(FALSE);
989 s->children = Lst_Init(FALSE);
990 s->parents = Lst_Init(FALSE);
991 s->ref = Lst_Init(FALSE);
992 s->sNum = sNum++;
993 s->flags = 0;
994 s->refCount = 1;
995
996 (void)Lst_AtEnd(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 = (Suff *)Lst_Datum(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 if (Lst_Open(sufflist) == FAILURE) {
1071 return;
1072 }
1073
1074 inIncludes = Lst_Init(FALSE);
1075 inLibs = Lst_Init(FALSE);
1076
1077 while ((ln = Lst_Next(sufflist)) != NULL) {
1078 s = (Suff *)Lst_Datum(ln);
1079 if (!Lst_IsEmpty (s->searchPath)) {
1080 #ifdef INCLUDES
1081 if (s->flags & SUFF_INCLUDE) {
1082 Dir_Concat(inIncludes, s->searchPath);
1083 }
1084 #endif /* INCLUDES */
1085 #ifdef LIBRARIES
1086 if (s->flags & SUFF_LIBRARY) {
1087 Dir_Concat(inLibs, s->searchPath);
1088 }
1089 #endif /* LIBRARIES */
1090 Dir_Concat(s->searchPath, dirSearchPath);
1091 } else {
1092 Lst_Destroy(s->searchPath, Dir_Destroy);
1093 s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir);
1094 }
1095 }
1096
1097 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
1098 free(ptr);
1099 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
1100 free(ptr);
1101
1102 Lst_Destroy(inIncludes, Dir_Destroy);
1103 Lst_Destroy(inLibs, Dir_Destroy);
1104
1105 Lst_Close(sufflist);
1106 }
1107
1108 /*-
1109 *-----------------------------------------------------------------------
1110 * Suff_AddInclude --
1111 * Add the given suffix as a type of file which gets included.
1112 * Called from the parse module when a .INCLUDES line is parsed.
1113 * The suffix must have already been defined.
1114 *
1115 * Input:
1116 * sname Name of the suffix to mark
1117 *
1118 * Results:
1119 * None.
1120 *
1121 * Side Effects:
1122 * The SUFF_INCLUDE bit is set in the suffix's flags field
1123 *
1124 *-----------------------------------------------------------------------
1125 */
1126 void
1127 Suff_AddInclude(char *sname)
1128 {
1129 LstNode ln;
1130 Suff *s;
1131
1132 ln = Lst_Find(sufflist, sname, SuffSuffHasNameP);
1133 if (ln != NULL) {
1134 s = (Suff *)Lst_Datum(ln);
1135 s->flags |= SUFF_INCLUDE;
1136 }
1137 }
1138
1139 /*-
1140 *-----------------------------------------------------------------------
1141 * Suff_AddLib --
1142 * Add the given suffix as a type of file which is a library.
1143 * Called from the parse module when parsing a .LIBS line. The
1144 * suffix must have been defined via .SUFFIXES before this is
1145 * called.
1146 *
1147 * Input:
1148 * sname Name of the suffix to mark
1149 *
1150 * Results:
1151 * None.
1152 *
1153 * Side Effects:
1154 * The SUFF_LIBRARY bit is set in the suffix's flags field
1155 *
1156 *-----------------------------------------------------------------------
1157 */
1158 void
1159 Suff_AddLib(char *sname)
1160 {
1161 LstNode ln;
1162 Suff *s;
1163
1164 ln = Lst_Find(sufflist, sname, SuffSuffHasNameP);
1165 if (ln != NULL) {
1166 s = (Suff *)Lst_Datum(ln);
1167 s->flags |= SUFF_LIBRARY;
1168 }
1169 }
1170
1171 /********** Implicit Source Search Functions *********/
1172
1173 /*-
1174 *-----------------------------------------------------------------------
1175 * SuffAddSrc --
1176 * Add a suffix as a Src structure to the given list with its parent
1177 * being the given Src structure. If the suffix is the null suffix,
1178 * the prefix is used unaltered as the file name in the Src structure.
1179 *
1180 * Input:
1181 * sp suffix for which to create a Src structure
1182 * lsp list and parent for the new Src
1183 *
1184 * Results:
1185 * always returns 0
1186 *
1187 * Side Effects:
1188 * A Src structure is created and tacked onto the end of the list
1189 *-----------------------------------------------------------------------
1190 */
1191 static int
1192 SuffAddSrc(void *sp, void *lsp)
1193 {
1194 Suff *s = (Suff *)sp;
1195 LstSrc *ls = (LstSrc *)lsp;
1196 Src *s2; /* new Src structure */
1197 Src *targ; /* Target structure */
1198
1199 targ = ls->s;
1200
1201 if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
1202 /*
1203 * If the suffix has been marked as the NULL suffix, also create a Src
1204 * structure for a file with no suffix attached. Two birds, and all
1205 * that...
1206 */
1207 s2 = bmake_malloc(sizeof(Src));
1208 s2->file = bmake_strdup(targ->pref);
1209 s2->pref = targ->pref;
1210 s2->parent = targ;
1211 s2->node = NULL;
1212 s2->suff = s;
1213 s->refCount++;
1214 s2->children = 0;
1215 targ->children += 1;
1216 (void)Lst_AtEnd(ls->l, s2);
1217 #ifdef DEBUG_SRC
1218 s2->cp = Lst_Init(FALSE);
1219 Lst_AtEnd(targ->cp, s2);
1220 fprintf(debug_file, "1 add %p %p to %p:", targ, s2, ls->l);
1221 Lst_ForEach(ls->l, PrintAddr, NULL);
1222 fprintf(debug_file, "\n");
1223 #endif
1224 }
1225 s2 = bmake_malloc(sizeof(Src));
1226 s2->file = str_concat(targ->pref, s->name, 0);
1227 s2->pref = targ->pref;
1228 s2->parent = targ;
1229 s2->node = NULL;
1230 s2->suff = s;
1231 s->refCount++;
1232 s2->children = 0;
1233 targ->children += 1;
1234 (void)Lst_AtEnd(ls->l, s2);
1235 #ifdef DEBUG_SRC
1236 s2->cp = Lst_Init(FALSE);
1237 Lst_AtEnd(targ->cp, s2);
1238 fprintf(debug_file, "2 add %p %p to %p:", targ, s2, ls->l);
1239 Lst_ForEach(ls->l, PrintAddr, NULL);
1240 fprintf(debug_file, "\n");
1241 #endif
1242
1243 return(0);
1244 }
1245
1246 /*-
1247 *-----------------------------------------------------------------------
1248 * SuffAddLevel --
1249 * Add all the children of targ as Src structures to the given list
1250 *
1251 * Input:
1252 * l list to which to add the new level
1253 * targ Src structure to use as the parent
1254 *
1255 * Results:
1256 * None
1257 *
1258 * Side Effects:
1259 * Lots of structures are created and added to the list
1260 *-----------------------------------------------------------------------
1261 */
1262 static void
1263 SuffAddLevel(Lst l, Src *targ)
1264 {
1265 LstSrc ls;
1266
1267 ls.s = targ;
1268 ls.l = l;
1269
1270 Lst_ForEach(targ->suff->children, SuffAddSrc, &ls);
1271 }
1272
1273 /*-
1274 *----------------------------------------------------------------------
1275 * SuffRemoveSrc --
1276 * Free all src structures in list that don't have a reference count
1277 *
1278 * Results:
1279 * Ture if an src was removed
1280 *
1281 * Side Effects:
1282 * The memory is free'd.
1283 *----------------------------------------------------------------------
1284 */
1285 static int
1286 SuffRemoveSrc(Lst l)
1287 {
1288 LstNode ln;
1289 Src *s;
1290 int t = 0;
1291
1292 if (Lst_Open(l) == FAILURE) {
1293 return 0;
1294 }
1295 #ifdef DEBUG_SRC
1296 fprintf(debug_file, "cleaning %lx: ", (unsigned long) l);
1297 Lst_ForEach(l, PrintAddr, NULL);
1298 fprintf(debug_file, "\n");
1299 #endif
1300
1301
1302 while ((ln = Lst_Next(l)) != NULL) {
1303 s = (Src *)Lst_Datum(ln);
1304 if (s->children == 0) {
1305 free(s->file);
1306 if (!s->parent)
1307 free(s->pref);
1308 else {
1309 #ifdef DEBUG_SRC
1310 LstNode ln2 = Lst_Member(s->parent->cp, s);
1311 if (ln2 != NULL)
1312 Lst_Remove(s->parent->cp, ln2);
1313 #endif
1314 --s->parent->children;
1315 }
1316 #ifdef DEBUG_SRC
1317 fprintf(debug_file, "free: [l=%p] p=%p %d\n", l, s, s->children);
1318 Lst_Destroy(s->cp, NULL);
1319 #endif
1320 Lst_Remove(l, ln);
1321 free(s);
1322 t |= 1;
1323 Lst_Close(l);
1324 return TRUE;
1325 }
1326 #ifdef DEBUG_SRC
1327 else {
1328 fprintf(debug_file, "keep: [l=%p] p=%p %d: ", l, s, s->children);
1329 Lst_ForEach(s->cp, PrintAddr, NULL);
1330 fprintf(debug_file, "\n");
1331 }
1332 #endif
1333 }
1334
1335 Lst_Close(l);
1336
1337 return t;
1338 }
1339
1340 /*-
1341 *-----------------------------------------------------------------------
1342 * SuffFindThem --
1343 * Find the first existing file/target in the list srcs
1344 *
1345 * Input:
1346 * srcs list of Src structures to search through
1347 *
1348 * Results:
1349 * The lowest structure in the chain of transformations
1350 *
1351 * Side Effects:
1352 * None
1353 *-----------------------------------------------------------------------
1354 */
1355 static Src *
1356 SuffFindThem(Lst srcs, Lst slst)
1357 {
1358 Src *s; /* current Src */
1359 Src *rs; /* returned Src */
1360 char *ptr;
1361
1362 rs = NULL;
1363
1364 while (!Lst_IsEmpty (srcs)) {
1365 s = (Src *)Lst_DeQueue(srcs);
1366
1367 if (DEBUG(SUFF)) {
1368 fprintf(debug_file, "\ttrying %s...", s->file);
1369 }
1370
1371 /*
1372 * A file is considered to exist if either a node exists in the
1373 * graph for it or the file actually exists.
1374 */
1375 if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
1376 #ifdef DEBUG_SRC
1377 fprintf(debug_file, "remove %p from %p\n", s, srcs);
1378 #endif
1379 rs = s;
1380 break;
1381 }
1382
1383 if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) {
1384 rs = s;
1385 #ifdef DEBUG_SRC
1386 fprintf(debug_file, "remove %p from %p\n", s, srcs);
1387 #endif
1388 free(ptr);
1389 break;
1390 }
1391
1392 if (DEBUG(SUFF)) {
1393 fprintf(debug_file, "not there\n");
1394 }
1395
1396 SuffAddLevel(srcs, s);
1397 Lst_AtEnd(slst, s);
1398 }
1399
1400 if (DEBUG(SUFF) && rs) {
1401 fprintf(debug_file, "got it\n");
1402 }
1403 return (rs);
1404 }
1405
1406 /*-
1407 *-----------------------------------------------------------------------
1408 * SuffFindCmds --
1409 * See if any of the children of the target in the Src structure is
1410 * one from which the target can be transformed. If there is one,
1411 * a Src structure is put together for it and returned.
1412 *
1413 * Input:
1414 * targ Src structure to play with
1415 *
1416 * Results:
1417 * The Src structure of the "winning" child, or NULL if no such beast.
1418 *
1419 * Side Effects:
1420 * A Src structure may be allocated.
1421 *
1422 *-----------------------------------------------------------------------
1423 */
1424 static Src *
1425 SuffFindCmds(Src *targ, Lst slst)
1426 {
1427 LstNode ln; /* General-purpose list node */
1428 GNode *t, /* Target GNode */
1429 *s; /* Source GNode */
1430 int prefLen;/* The length of the defined prefix */
1431 Suff *suff; /* Suffix on matching beastie */
1432 Src *ret; /* Return value */
1433 char *cp;
1434
1435 t = targ->node;
1436 (void)Lst_Open(t->children);
1437 prefLen = strlen(targ->pref);
1438
1439 for (;;) {
1440 ln = Lst_Next(t->children);
1441 if (ln == NULL) {
1442 Lst_Close(t->children);
1443 return NULL;
1444 }
1445 s = (GNode *)Lst_Datum(ln);
1446
1447 if (s->type & OP_OPTIONAL && Lst_IsEmpty(t->commands)) {
1448 /*
1449 * We haven't looked to see if .OPTIONAL files exist yet, so
1450 * don't use one as the implicit source.
1451 * This allows us to use .OPTIONAL in .depend files so make won't
1452 * complain "don't know how to make xxx.h' when a dependent file
1453 * has been moved/deleted.
1454 */
1455 continue;
1456 }
1457
1458 cp = strrchr(s->name, '/');
1459 if (cp == NULL) {
1460 cp = s->name;
1461 } else {
1462 cp++;
1463 }
1464 if (strncmp(cp, targ->pref, prefLen) != 0)
1465 continue;
1466 /*
1467 * The node matches the prefix ok, see if it has a known
1468 * suffix.
1469 */
1470 ln = Lst_Find(sufflist, &cp[prefLen], SuffSuffHasNameP);
1471 if (ln == NULL)
1472 continue;
1473 /*
1474 * It even has a known suffix, see if there's a transformation
1475 * defined between the node's suffix and the target's suffix.
1476 *
1477 * XXX: Handle multi-stage transformations here, too.
1478 */
1479 suff = (Suff *)Lst_Datum(ln);
1480
1481 if (Lst_Member(suff->parents, targ->suff) != NULL)
1482 break;
1483 }
1484
1485 /*
1486 * Hot Damn! Create a new Src structure to describe
1487 * this transformation (making sure to duplicate the
1488 * source node's name so Suff_FindDeps can free it
1489 * again (ick)), and return the new structure.
1490 */
1491 ret = bmake_malloc(sizeof(Src));
1492 ret->file = bmake_strdup(s->name);
1493 ret->pref = targ->pref;
1494 ret->suff = suff;
1495 suff->refCount++;
1496 ret->parent = targ;
1497 ret->node = s;
1498 ret->children = 0;
1499 targ->children += 1;
1500 #ifdef DEBUG_SRC
1501 ret->cp = Lst_Init(FALSE);
1502 fprintf(debug_file, "3 add %p %p\n", targ, ret);
1503 Lst_AtEnd(targ->cp, ret);
1504 #endif
1505 Lst_AtEnd(slst, ret);
1506 if (DEBUG(SUFF)) {
1507 fprintf(debug_file, "\tusing existing source %s\n", s->name);
1508 }
1509 return (ret);
1510 }
1511
1512 /*-
1513 *-----------------------------------------------------------------------
1514 * SuffExpandChildren --
1515 * Expand the names of any children of a given node that contain
1516 * variable invocations or file wildcards into actual targets.
1517 *
1518 * Input:
1519 * cln Child to examine
1520 * pgn Parent node being processed
1521 *
1522 * Results:
1523 * === 0 (continue)
1524 *
1525 * Side Effects:
1526 * The expanded node is removed from the parent's list of children,
1527 * and the parent's unmade counter is decremented, but other nodes
1528 * may be added.
1529 *
1530 *-----------------------------------------------------------------------
1531 */
1532 static void
1533 SuffExpandChildren(LstNode cln, GNode *pgn)
1534 {
1535 GNode *cgn = (GNode *)Lst_Datum(cln);
1536 GNode *gn; /* New source 8) */
1537 char *cp; /* Expanded value */
1538
1539 if (!Lst_IsEmpty(cgn->order_pred) || !Lst_IsEmpty(cgn->order_succ))
1540 /* It is all too hard to process the result of .ORDER */
1541 return;
1542
1543 if (cgn->type & OP_WAIT)
1544 /* Ignore these (& OP_PHONY ?) */
1545 return;
1546
1547 /*
1548 * First do variable expansion -- this takes precedence over
1549 * wildcard expansion. If the result contains wildcards, they'll be gotten
1550 * to later since the resulting words are tacked on to the end of
1551 * the children list.
1552 */
1553 if (strchr(cgn->name, '$') == NULL) {
1554 SuffExpandWildcards(cln, pgn);
1555 return;
1556 }
1557
1558 if (DEBUG(SUFF)) {
1559 fprintf(debug_file, "Expanding \"%s\"...", cgn->name);
1560 }
1561 cp = Var_Subst(NULL, cgn->name, pgn, VARF_UNDEFERR|VARF_WANTRES);
1562
1563 if (cp != NULL) {
1564 Lst members = Lst_Init(FALSE);
1565
1566 if (cgn->type & OP_ARCHV) {
1567 /*
1568 * Node was an archive(member) target, so we want to call
1569 * on the Arch module to find the nodes for us, expanding
1570 * variables in the parent's context.
1571 */
1572 char *sacrifice = cp;
1573
1574 (void)Arch_ParseArchive(&sacrifice, members, pgn);
1575 } else {
1576 /*
1577 * Break the result into a vector of strings whose nodes
1578 * we can find, then add those nodes to the members list.
1579 * Unfortunately, we can't use brk_string b/c it
1580 * doesn't understand about variable specifications with
1581 * spaces in them...
1582 */
1583 char *start;
1584 char *initcp = cp; /* For freeing... */
1585
1586 for (start = cp; *start == ' ' || *start == '\t'; start++)
1587 continue;
1588 for (cp = start; *cp != '\0'; cp++) {
1589 if (*cp == ' ' || *cp == '\t') {
1590 /*
1591 * White-space -- terminate element, find the node,
1592 * add it, skip any further spaces.
1593 */
1594 *cp++ = '\0';
1595 gn = Targ_FindNode(start, TARG_CREATE);
1596 (void)Lst_AtEnd(members, gn);
1597 while (*cp == ' ' || *cp == '\t') {
1598 cp++;
1599 }
1600 /*
1601 * Adjust cp for increment at start of loop, but
1602 * set start to first non-space.
1603 */
1604 start = cp--;
1605 } else if (*cp == '$') {
1606 /*
1607 * Start of a variable spec -- contact variable module
1608 * to find the end so we can skip over it.
1609 */
1610 char *junk;
1611 int len;
1612 void *freeIt;
1613
1614 junk = Var_Parse(cp, pgn, VARF_UNDEFERR|VARF_WANTRES,
1615 &len, &freeIt);
1616 if (junk != var_Error) {
1617 cp += len - 1;
1618 }
1619
1620 free(freeIt);
1621 } else if (*cp == '\\' && cp[1] != '\0') {
1622 /*
1623 * Escaped something -- skip over it
1624 */
1625 cp++;
1626 }
1627 }
1628
1629 if (cp != start) {
1630 /*
1631 * Stuff left over -- add it to the list too
1632 */
1633 gn = Targ_FindNode(start, TARG_CREATE);
1634 (void)Lst_AtEnd(members, gn);
1635 }
1636 /*
1637 * Point cp back at the beginning again so the variable value
1638 * can be freed.
1639 */
1640 cp = initcp;
1641 }
1642
1643 /*
1644 * Add all elements of the members list to the parent node.
1645 */
1646 while(!Lst_IsEmpty(members)) {
1647 gn = (GNode *)Lst_DeQueue(members);
1648
1649 if (DEBUG(SUFF)) {
1650 fprintf(debug_file, "%s...", gn->name);
1651 }
1652 /* Add gn to the parents child list before the original child */
1653 (void)Lst_InsertBefore(pgn->children, cln, gn);
1654 (void)Lst_AtEnd(gn->parents, pgn);
1655 pgn->unmade++;
1656 /* Expand wildcards on new node */
1657 SuffExpandWildcards(Lst_Prev(cln), pgn);
1658 }
1659 Lst_Destroy(members, NULL);
1660
1661 /*
1662 * Free the result
1663 */
1664 free(cp);
1665 }
1666 if (DEBUG(SUFF)) {
1667 fprintf(debug_file, "\n");
1668 }
1669
1670 /*
1671 * Now the source is expanded, remove it from the list of children to
1672 * keep it from being processed.
1673 */
1674 pgn->unmade--;
1675 Lst_Remove(pgn->children, cln);
1676 Lst_Remove(cgn->parents, Lst_Member(cgn->parents, pgn));
1677 }
1678
1679 static void
1680 SuffExpandWildcards(LstNode cln, GNode *pgn)
1681 {
1682 GNode *cgn = (GNode *)Lst_Datum(cln);
1683 GNode *gn; /* New source 8) */
1684 char *cp; /* Expanded value */
1685 Lst explist; /* List of expansions */
1686
1687 if (!Dir_HasWildcards(cgn->name))
1688 return;
1689
1690 /*
1691 * Expand the word along the chosen path
1692 */
1693 explist = Lst_Init(FALSE);
1694 Dir_Expand(cgn->name, Suff_FindPath(cgn), explist);
1695
1696 while (!Lst_IsEmpty(explist)) {
1697 /*
1698 * Fetch next expansion off the list and find its GNode
1699 */
1700 cp = (char *)Lst_DeQueue(explist);
1701
1702 if (DEBUG(SUFF)) {
1703 fprintf(debug_file, "%s...", cp);
1704 }
1705 gn = Targ_FindNode(cp, TARG_CREATE);
1706
1707 /* Add gn to the parents child list before the original child */
1708 (void)Lst_InsertBefore(pgn->children, cln, gn);
1709 (void)Lst_AtEnd(gn->parents, pgn);
1710 pgn->unmade++;
1711 }
1712
1713 /*
1714 * Nuke what's left of the list
1715 */
1716 Lst_Destroy(explist, NULL);
1717
1718 if (DEBUG(SUFF)) {
1719 fprintf(debug_file, "\n");
1720 }
1721
1722 /*
1723 * Now the source is expanded, remove it from the list of children to
1724 * keep it from being processed.
1725 */
1726 pgn->unmade--;
1727 Lst_Remove(pgn->children, cln);
1728 Lst_Remove(cgn->parents, Lst_Member(cgn->parents, pgn));
1729 }
1730
1731 /*-
1732 *-----------------------------------------------------------------------
1733 * Suff_FindPath --
1734 * Find a path along which to expand the node.
1735 *
1736 * If the word has a known suffix, use that path.
1737 * If it has no known suffix, use the default system search path.
1738 *
1739 * Input:
1740 * gn Node being examined
1741 *
1742 * Results:
1743 * The appropriate path to search for the GNode.
1744 *
1745 * Side Effects:
1746 * XXX: We could set the suffix here so that we don't have to scan
1747 * again.
1748 *
1749 *-----------------------------------------------------------------------
1750 */
1751 Lst
1752 Suff_FindPath(GNode* gn)
1753 {
1754 Suff *suff = gn->suffix;
1755
1756 if (suff == NULL) {
1757 SuffixCmpData sd; /* Search string data */
1758 LstNode ln;
1759 sd.len = strlen(gn->name);
1760 sd.ename = gn->name + sd.len;
1761 ln = Lst_Find(sufflist, &sd, SuffSuffIsSuffixP);
1762
1763 if (DEBUG(SUFF)) {
1764 fprintf(debug_file, "Wildcard expanding \"%s\"...", gn->name);
1765 }
1766 if (ln != NULL)
1767 suff = (Suff *)Lst_Datum(ln);
1768 /* XXX: Here we can save the suffix so we don't have to do this again */
1769 }
1770
1771 if (suff != NULL) {
1772 if (DEBUG(SUFF)) {
1773 fprintf(debug_file, "suffix is \"%s\"...", suff->name);
1774 }
1775 return suff->searchPath;
1776 } else {
1777 /*
1778 * Use default search path
1779 */
1780 return dirSearchPath;
1781 }
1782 }
1783
1784 /*-
1785 *-----------------------------------------------------------------------
1786 * SuffApplyTransform --
1787 * Apply a transformation rule, given the source and target nodes
1788 * and suffixes.
1789 *
1790 * Input:
1791 * tGn Target node
1792 * sGn Source node
1793 * t Target suffix
1794 * s Source suffix
1795 *
1796 * Results:
1797 * TRUE if successful, FALSE if not.
1798 *
1799 * Side Effects:
1800 * The source and target are linked and the commands from the
1801 * transformation are added to the target node's commands list.
1802 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1803 * to the target. The target also inherits all the sources for
1804 * the transformation rule.
1805 *
1806 *-----------------------------------------------------------------------
1807 */
1808 static Boolean
1809 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
1810 {
1811 LstNode ln, nln; /* General node */
1812 char *tname; /* Name of transformation rule */
1813 GNode *gn; /* Node for same */
1814
1815 /*
1816 * Form the proper links between the target and source.
1817 */
1818 (void)Lst_AtEnd(tGn->children, sGn);
1819 (void)Lst_AtEnd(sGn->parents, tGn);
1820 tGn->unmade += 1;
1821
1822 /*
1823 * Locate the transformation rule itself
1824 */
1825 tname = str_concat(s->name, t->name, 0);
1826 ln = Lst_Find(transforms, tname, SuffGNHasNameP);
1827 free(tname);
1828
1829 if (ln == NULL) {
1830 /*
1831 * Not really such a transformation rule (can happen when we're
1832 * called to link an OP_MEMBER and OP_ARCHV node), so return
1833 * FALSE.
1834 */
1835 return(FALSE);
1836 }
1837
1838 gn = (GNode *)Lst_Datum(ln);
1839
1840 if (DEBUG(SUFF)) {
1841 fprintf(debug_file, "\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
1842 }
1843
1844 /*
1845 * Record last child for expansion purposes
1846 */
1847 ln = Lst_Last(tGn->children);
1848
1849 /*
1850 * Pass the buck to Make_HandleUse to apply the rule
1851 */
1852 (void)Make_HandleUse(gn, tGn);
1853
1854 /*
1855 * Deal with wildcards and variables in any acquired sources
1856 */
1857 for (ln = Lst_Succ(ln); ln != NULL; ln = nln) {
1858 nln = Lst_Succ(ln);
1859 SuffExpandChildren(ln, tGn);
1860 }
1861
1862 /*
1863 * Keep track of another parent to which this beast is transformed so
1864 * the .IMPSRC variable can be set correctly for the parent.
1865 */
1866 (void)Lst_AtEnd(sGn->iParents, tGn);
1867
1868 return(TRUE);
1869 }
1870
1871
1872 /*-
1873 *-----------------------------------------------------------------------
1874 * SuffFindArchiveDeps --
1875 * Locate dependencies for an OP_ARCHV node.
1876 *
1877 * Input:
1878 * gn Node for which to locate dependencies
1879 *
1880 * Results:
1881 * None
1882 *
1883 * Side Effects:
1884 * Same as Suff_FindDeps
1885 *
1886 *-----------------------------------------------------------------------
1887 */
1888 static void
1889 SuffFindArchiveDeps(GNode *gn, Lst slst)
1890 {
1891 char *eoarch; /* End of archive portion */
1892 char *eoname; /* End of member portion */
1893 GNode *mem; /* Node for member */
1894 static const char *copy[] = {
1895 /* Variables to be copied from the member node */
1896 TARGET, /* Must be first */
1897 PREFIX, /* Must be second */
1898 };
1899 LstNode ln, nln; /* Next suffix node to check */
1900 int i; /* Index into copy and vals */
1901 Suff *ms; /* Suffix descriptor for member */
1902 char *name; /* Start of member's name */
1903
1904 /*
1905 * The node is an archive(member) pair. so we must find a
1906 * suffix for both of them.
1907 */
1908 eoarch = strchr(gn->name, '(');
1909 eoname = strchr(eoarch, ')');
1910
1911 /*
1912 * Caller guarantees the format `libname(member)', via
1913 * Arch_ParseArchive.
1914 */
1915 assert(eoarch != NULL);
1916 assert(eoname != NULL);
1917
1918 *eoname = '\0'; /* Nuke parentheses during suffix search */
1919 *eoarch = '\0'; /* So a suffix can be found */
1920
1921 name = eoarch + 1;
1922
1923 /*
1924 * To simplify things, call Suff_FindDeps recursively on the member now,
1925 * so we can simply compare the member's .PREFIX and .TARGET variables
1926 * to locate its suffix. This allows us to figure out the suffix to
1927 * use for the archive without having to do a quadratic search over the
1928 * suffix list, backtracking for each one...
1929 */
1930 mem = Targ_FindNode(name, TARG_CREATE);
1931 SuffFindDeps(mem, slst);
1932
1933 /*
1934 * Create the link between the two nodes right off
1935 */
1936 (void)Lst_AtEnd(gn->children, mem);
1937 (void)Lst_AtEnd(mem->parents, gn);
1938 gn->unmade += 1;
1939
1940 /*
1941 * Copy in the variables from the member node to this one.
1942 */
1943 for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
1944 char *p1;
1945 Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn);
1946 free(p1);
1947
1948 }
1949
1950 ms = mem->suffix;
1951 if (ms == NULL) {
1952 /*
1953 * Didn't know what it was -- use .NULL suffix if not in make mode
1954 */
1955 if (DEBUG(SUFF)) {
1956 fprintf(debug_file, "using null suffix\n");
1957 }
1958 ms = suffNull;
1959 }
1960
1961
1962 /*
1963 * Set the other two local variables required for this target.
1964 */
1965 Var_Set(MEMBER, name, gn);
1966 Var_Set(ARCHIVE, gn->name, gn);
1967
1968 /*
1969 * Set $@ for compatibility with other makes
1970 */
1971 Var_Set(TARGET, gn->name, gn);
1972
1973 /*
1974 * Now we've got the important local variables set, expand any sources
1975 * that still contain variables or wildcards in their names.
1976 */
1977 for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
1978 nln = Lst_Succ(ln);
1979 SuffExpandChildren(ln, gn);
1980 }
1981
1982 if (ms != NULL) {
1983 /*
1984 * Member has a known suffix, so look for a transformation rule from
1985 * it to a possible suffix of the archive. Rather than searching
1986 * through the entire list, we just look at suffixes to which the
1987 * member's suffix may be transformed...
1988 */
1989 SuffixCmpData sd; /* Search string data */
1990
1991 /*
1992 * Use first matching suffix...
1993 */
1994 sd.len = eoarch - gn->name;
1995 sd.ename = eoarch;
1996 ln = Lst_Find(ms->parents, &sd, SuffSuffIsSuffixP);
1997
1998 if (ln != NULL) {
1999 /*
2000 * Got one -- apply it
2001 */
2002 if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) &&
2003 DEBUG(SUFF))
2004 {
2005 fprintf(debug_file, "\tNo transformation from %s -> %s\n",
2006 ms->name, ((Suff *)Lst_Datum(ln))->name);
2007 }
2008 }
2009 }
2010
2011 /*
2012 * Replace the opening and closing parens now we've no need of the separate
2013 * pieces.
2014 */
2015 *eoarch = '('; *eoname = ')';
2016
2017 /*
2018 * Pretend gn appeared to the left of a dependency operator so
2019 * the user needn't provide a transformation from the member to the
2020 * archive.
2021 */
2022 if (OP_NOP(gn->type)) {
2023 gn->type |= OP_DEPENDS;
2024 }
2025
2026 /*
2027 * Flag the member as such so we remember to look in the archive for
2028 * its modification time. The OP_JOIN | OP_MADE is needed because this
2029 * target should never get made.
2030 */
2031 mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
2032 }
2033
2034 /*-
2035 *-----------------------------------------------------------------------
2036 * SuffFindNormalDeps --
2037 * Locate implicit dependencies for regular targets.
2038 *
2039 * Input:
2040 * gn Node for which to find sources
2041 *
2042 * Results:
2043 * None.
2044 *
2045 * Side Effects:
2046 * Same as Suff_FindDeps...
2047 *
2048 *-----------------------------------------------------------------------
2049 */
2050 static void
2051 SuffFindNormalDeps(GNode *gn, Lst slst)
2052 {
2053 char *eoname; /* End of name */
2054 char *sopref; /* Start of prefix */
2055 LstNode ln, nln; /* Next suffix node to check */
2056 Lst srcs; /* List of sources at which to look */
2057 Lst targs; /* List of targets to which things can be
2058 * transformed. They all have the same file,
2059 * but different suff and pref fields */
2060 Src *bottom; /* Start of found transformation path */
2061 Src *src; /* General Src pointer */
2062 char *pref; /* Prefix to use */
2063 Src *targ; /* General Src target pointer */
2064 SuffixCmpData sd; /* Search string data */
2065
2066
2067 sd.len = strlen(gn->name);
2068 sd.ename = eoname = gn->name + sd.len;
2069
2070 sopref = gn->name;
2071
2072 /*
2073 * Begin at the beginning...
2074 */
2075 ln = Lst_First(sufflist);
2076 srcs = Lst_Init(FALSE);
2077 targs = Lst_Init(FALSE);
2078
2079 /*
2080 * We're caught in a catch-22 here. On the one hand, we want to use any
2081 * transformation implied by the target's sources, but we can't examine
2082 * the sources until we've expanded any variables/wildcards they may hold,
2083 * and we can't do that until we've set up the target's local variables
2084 * and we can't do that until we know what the proper suffix for the
2085 * target is (in case there are two suffixes one of which is a suffix of
2086 * the other) and we can't know that until we've found its implied
2087 * source, which we may not want to use if there's an existing source
2088 * that implies a different transformation.
2089 *
2090 * In an attempt to get around this, which may not work all the time,
2091 * but should work most of the time, we look for implied sources first,
2092 * checking transformations to all possible suffixes of the target,
2093 * use what we find to set the target's local variables, expand the
2094 * children, then look for any overriding transformations they imply.
2095 * Should we find one, we discard the one we found before.
2096 */
2097 bottom = NULL;
2098 targ = NULL;
2099
2100 if (!(gn->type & OP_PHONY)) {
2101
2102 while (ln != NULL) {
2103 /*
2104 * Look for next possible suffix...
2105 */
2106 ln = Lst_FindFrom(sufflist, ln, &sd, SuffSuffIsSuffixP);
2107
2108 if (ln != NULL) {
2109 int prefLen; /* Length of the prefix */
2110
2111 /*
2112 * Allocate a Src structure to which things can be transformed
2113 */
2114 targ = bmake_malloc(sizeof(Src));
2115 targ->file = bmake_strdup(gn->name);
2116 targ->suff = (Suff *)Lst_Datum(ln);
2117 targ->suff->refCount++;
2118 targ->node = gn;
2119 targ->parent = NULL;
2120 targ->children = 0;
2121 #ifdef DEBUG_SRC
2122 targ->cp = Lst_Init(FALSE);
2123 #endif
2124
2125 /*
2126 * Allocate room for the prefix, whose end is found by
2127 * subtracting the length of the suffix from
2128 * the end of the name.
2129 */
2130 prefLen = (eoname - targ->suff->nameLen) - sopref;
2131 targ->pref = bmake_malloc(prefLen + 1);
2132 memcpy(targ->pref, sopref, prefLen);
2133 targ->pref[prefLen] = '\0';
2134
2135 /*
2136 * Add nodes from which the target can be made
2137 */
2138 SuffAddLevel(srcs, targ);
2139
2140 /*
2141 * Record the target so we can nuke it
2142 */
2143 (void)Lst_AtEnd(targs, targ);
2144
2145 /*
2146 * Search from this suffix's successor...
2147 */
2148 ln = Lst_Succ(ln);
2149 }
2150 }
2151
2152 /*
2153 * Handle target of unknown suffix...
2154 */
2155 if (Lst_IsEmpty(targs) && suffNull != NULL) {
2156 if (DEBUG(SUFF)) {
2157 fprintf(debug_file, "\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
2158 }
2159
2160 targ = bmake_malloc(sizeof(Src));
2161 targ->file = bmake_strdup(gn->name);
2162 targ->suff = suffNull;
2163 targ->suff->refCount++;
2164 targ->node = gn;
2165 targ->parent = NULL;
2166 targ->children = 0;
2167 targ->pref = bmake_strdup(sopref);
2168 #ifdef DEBUG_SRC
2169 targ->cp = Lst_Init(FALSE);
2170 #endif
2171
2172 /*
2173 * Only use the default suffix rules if we don't have commands
2174 * defined for this gnode; traditional make programs used to
2175 * not define suffix rules if the gnode had children but we
2176 * don't do this anymore.
2177 */
2178 if (Lst_IsEmpty(gn->commands))
2179 SuffAddLevel(srcs, targ);
2180 else {
2181 if (DEBUG(SUFF))
2182 fprintf(debug_file, "not ");
2183 }
2184
2185 if (DEBUG(SUFF))
2186 fprintf(debug_file, "adding suffix rules\n");
2187
2188 (void)Lst_AtEnd(targs, targ);
2189 }
2190
2191 /*
2192 * Using the list of possible sources built up from the target
2193 * suffix(es), try and find an existing file/target that matches.
2194 */
2195 bottom = SuffFindThem(srcs, slst);
2196
2197 if (bottom == NULL) {
2198 /*
2199 * No known transformations -- use the first suffix found
2200 * for setting the local variables.
2201 */
2202 if (!Lst_IsEmpty(targs)) {
2203 targ = (Src *)Lst_Datum(Lst_First(targs));
2204 } else {
2205 targ = NULL;
2206 }
2207 } else {
2208 /*
2209 * Work up the transformation path to find the suffix of the
2210 * target to which the transformation was made.
2211 */
2212 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
2213 continue;
2214 }
2215 }
2216
2217 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
2218
2219 pref = (targ != NULL) ? targ->pref : gn->name;
2220 Var_Set(PREFIX, pref, gn);
2221
2222 /*
2223 * Now we've got the important local variables set, expand any sources
2224 * that still contain variables or wildcards in their names.
2225 */
2226 for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
2227 nln = Lst_Succ(ln);
2228 SuffExpandChildren(ln, gn);
2229 }
2230
2231 if (targ == NULL) {
2232 if (DEBUG(SUFF)) {
2233 fprintf(debug_file, "\tNo valid suffix on %s\n", gn->name);
2234 }
2235
2236 sfnd_abort:
2237 /*
2238 * Deal with finding the thing on the default search path. We
2239 * always do that, not only if the node is only a source (not
2240 * on the lhs of a dependency operator or [XXX] it has neither
2241 * children or commands) as the old pmake did.
2242 */
2243 if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
2244 free(gn->path);
2245 gn->path = Dir_FindFile(gn->name,
2246 (targ == NULL ? dirSearchPath :
2247 targ->suff->searchPath));
2248 if (gn->path != NULL) {
2249 char *ptr;
2250 Var_Set(TARGET, gn->path, gn);
2251
2252 if (targ != NULL) {
2253 /*
2254 * Suffix known for the thing -- trim the suffix off
2255 * the path to form the proper .PREFIX variable.
2256 */
2257 int savep = strlen(gn->path) - targ->suff->nameLen;
2258 char savec;
2259
2260 if (gn->suffix)
2261 gn->suffix->refCount--;
2262 gn->suffix = targ->suff;
2263 gn->suffix->refCount++;
2264
2265 savec = gn->path[savep];
2266 gn->path[savep] = '\0';
2267
2268 if ((ptr = strrchr(gn->path, '/')) != NULL)
2269 ptr++;
2270 else
2271 ptr = gn->path;
2272
2273 Var_Set(PREFIX, ptr, gn);
2274
2275 gn->path[savep] = savec;
2276 } else {
2277 /*
2278 * The .PREFIX gets the full path if the target has
2279 * no known suffix.
2280 */
2281 if (gn->suffix)
2282 gn->suffix->refCount--;
2283 gn->suffix = NULL;
2284
2285 if ((ptr = strrchr(gn->path, '/')) != NULL)
2286 ptr++;
2287 else
2288 ptr = gn->path;
2289
2290 Var_Set(PREFIX, ptr, gn);
2291 }
2292 }
2293 }
2294
2295 goto sfnd_return;
2296 }
2297
2298 /*
2299 * If the suffix indicates that the target is a library, mark that in
2300 * the node's type field.
2301 */
2302 if (targ->suff->flags & SUFF_LIBRARY) {
2303 gn->type |= OP_LIB;
2304 }
2305
2306 /*
2307 * Check for overriding transformation rule implied by sources
2308 */
2309 if (!Lst_IsEmpty(gn->children)) {
2310 src = SuffFindCmds(targ, slst);
2311
2312 if (src != NULL) {
2313 /*
2314 * Free up all the Src structures in the transformation path
2315 * up to, but not including, the parent node.
2316 */
2317 while (bottom && bottom->parent != NULL) {
2318 if (Lst_Member(slst, bottom) == NULL) {
2319 Lst_AtEnd(slst, bottom);
2320 }
2321 bottom = bottom->parent;
2322 }
2323 bottom = src;
2324 }
2325 }
2326
2327 if (bottom == NULL) {
2328 /*
2329 * No idea from where it can come -- return now.
2330 */
2331 goto sfnd_abort;
2332 }
2333
2334 /*
2335 * We now have a list of Src structures headed by 'bottom' and linked via
2336 * their 'parent' pointers. What we do next is create links between
2337 * source and target nodes (which may or may not have been created)
2338 * and set the necessary local variables in each target. The
2339 * commands for each target are set from the commands of the
2340 * transformation rule used to get from the src suffix to the targ
2341 * suffix. Note that this causes the commands list of the original
2342 * node, gn, to be replaced by the commands of the final
2343 * transformation rule. Also, the unmade field of gn is incremented.
2344 * Etc.
2345 */
2346 if (bottom->node == NULL) {
2347 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2348 }
2349
2350 for (src = bottom; src->parent != NULL; src = src->parent) {
2351 targ = src->parent;
2352
2353 if (src->node->suffix)
2354 src->node->suffix->refCount--;
2355 src->node->suffix = src->suff;
2356 src->node->suffix->refCount++;
2357
2358 if (targ->node == NULL) {
2359 targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2360 }
2361
2362 SuffApplyTransform(targ->node, src->node,
2363 targ->suff, src->suff);
2364
2365 if (targ->node != gn) {
2366 /*
2367 * Finish off the dependency-search process for any nodes
2368 * between bottom and gn (no point in questing around the
2369 * filesystem for their implicit source when it's already
2370 * known). Note that the node can't have any sources that
2371 * need expanding, since SuffFindThem will stop on an existing
2372 * node, so all we need to do is set the standard and System V
2373 * variables.
2374 */
2375 targ->node->type |= OP_DEPS_FOUND;
2376
2377 Var_Set(PREFIX, targ->pref, targ->node);
2378
2379 Var_Set(TARGET, targ->node->name, targ->node);
2380 }
2381 }
2382
2383 if (gn->suffix)
2384 gn->suffix->refCount--;
2385 gn->suffix = src->suff;
2386 gn->suffix->refCount++;
2387
2388 /*
2389 * Nuke the transformation path and the Src structures left over in the
2390 * two lists.
2391 */
2392 sfnd_return:
2393 if (bottom)
2394 if (Lst_Member(slst, bottom) == NULL)
2395 Lst_AtEnd(slst, bottom);
2396
2397 while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
2398 continue;
2399
2400 Lst_Concat(slst, srcs, LST_CONCLINK);
2401 Lst_Concat(slst, targs, LST_CONCLINK);
2402 }
2403
2404
2405 /*-
2406 *-----------------------------------------------------------------------
2407 * Suff_FindDeps --
2408 * Find implicit sources for the target described by the graph node
2409 * gn
2410 *
2411 * Results:
2412 * Nothing.
2413 *
2414 * Side Effects:
2415 * Nodes are added to the graph below the passed-in node. The nodes
2416 * are marked to have their IMPSRC variable filled in. The
2417 * PREFIX variable is set for the given node and all its
2418 * implied children.
2419 *
2420 * Notes:
2421 * The path found by this target is the shortest path in the
2422 * transformation graph, which may pass through non-existent targets,
2423 * to an existing target. The search continues on all paths from the
2424 * root suffix until a file is found. I.e. if there's a path
2425 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2426 * the .c and .l files don't, the search will branch out in
2427 * all directions from .o and again from all the nodes on the
2428 * next level until the .l,v node is encountered.
2429 *
2430 *-----------------------------------------------------------------------
2431 */
2432
2433 void
2434 Suff_FindDeps(GNode *gn)
2435 {
2436
2437 SuffFindDeps(gn, srclist);
2438 while (SuffRemoveSrc(srclist))
2439 continue;
2440 }
2441
2442
2443 /*
2444 * Input:
2445 * gn node we're dealing with
2446 *
2447 */
2448 static void
2449 SuffFindDeps(GNode *gn, Lst slst)
2450 {
2451 if (gn->type & OP_DEPS_FOUND) {
2452 /*
2453 * If dependencies already found, no need to do it again...
2454 */
2455 return;
2456 } else {
2457 gn->type |= OP_DEPS_FOUND;
2458 }
2459 /*
2460 * Make sure we have these set, may get revised below.
2461 */
2462 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
2463 Var_Set(PREFIX, gn->name, gn);
2464
2465 if (DEBUG(SUFF)) {
2466 fprintf(debug_file, "SuffFindDeps (%s)\n", gn->name);
2467 }
2468
2469 if (gn->type & OP_ARCHV) {
2470 SuffFindArchiveDeps(gn, slst);
2471 } else if (gn->type & OP_LIB) {
2472 /*
2473 * If the node is a library, it is the arch module's job to find it
2474 * and set the TARGET variable accordingly. We merely provide the
2475 * search path, assuming all libraries end in ".a" (if the suffix
2476 * hasn't been defined, there's nothing we can do for it, so we just
2477 * set the TARGET variable to the node's name in order to give it a
2478 * value).
2479 */
2480 LstNode ln;
2481 Suff *s;
2482
2483 ln = Lst_Find(sufflist, LIBSUFF, SuffSuffHasNameP);
2484 if (gn->suffix)
2485 gn->suffix->refCount--;
2486 if (ln != NULL) {
2487 gn->suffix = s = (Suff *)Lst_Datum(ln);
2488 gn->suffix->refCount++;
2489 Arch_FindLib(gn, s->searchPath);
2490 } else {
2491 gn->suffix = NULL;
2492 Var_Set(TARGET, gn->name, gn);
2493 }
2494 /*
2495 * Because a library (-lfoo) target doesn't follow the standard
2496 * filesystem conventions, we don't set the regular variables for
2497 * the thing. .PREFIX is simply made empty...
2498 */
2499 Var_Set(PREFIX, "", gn);
2500 } else {
2501 SuffFindNormalDeps(gn, slst);
2502 }
2503 }
2504
2505 /*-
2506 *-----------------------------------------------------------------------
2507 * Suff_SetNull --
2508 * Define which suffix is the null suffix.
2509 *
2510 * Input:
2511 * name Name of null suffix
2512 *
2513 * Results:
2514 * None.
2515 *
2516 * Side Effects:
2517 * 'suffNull' is altered.
2518 *
2519 * Notes:
2520 * Need to handle the changing of the null suffix gracefully so the
2521 * old transformation rules don't just go away.
2522 *
2523 *-----------------------------------------------------------------------
2524 */
2525 void
2526 Suff_SetNull(char *name)
2527 {
2528 Suff *s;
2529 LstNode ln;
2530
2531 ln = Lst_Find(sufflist, name, SuffSuffHasNameP);
2532 if (ln != NULL) {
2533 s = (Suff *)Lst_Datum(ln);
2534 if (suffNull != NULL) {
2535 suffNull->flags &= ~SUFF_NULL;
2536 }
2537 s->flags |= SUFF_NULL;
2538 /*
2539 * XXX: Here's where the transformation mangling would take place
2540 */
2541 suffNull = s;
2542 } else {
2543 Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
2544 name);
2545 }
2546 }
2547
2548 /*-
2549 *-----------------------------------------------------------------------
2550 * Suff_Init --
2551 * Initialize suffixes module
2552 *
2553 * Results:
2554 * None
2555 *
2556 * Side Effects:
2557 * Many
2558 *-----------------------------------------------------------------------
2559 */
2560 void
2561 Suff_Init(void)
2562 {
2563 #ifdef CLEANUP
2564 suffClean = Lst_Init(FALSE);
2565 #endif
2566 srclist = Lst_Init(FALSE);
2567 transforms = Lst_Init(FALSE);
2568
2569 /*
2570 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2571 * actually go on the suffix list or everyone will think that's its
2572 * suffix.
2573 */
2574 Suff_ClearSuffixes();
2575 }
2576
2577
2578 /*-
2579 *----------------------------------------------------------------------
2580 * Suff_End --
2581 * Cleanup the this module
2582 *
2583 * Results:
2584 * None
2585 *
2586 * Side Effects:
2587 * The memory is free'd.
2588 *----------------------------------------------------------------------
2589 */
2590
2591 void
2592 Suff_End(void)
2593 {
2594 #ifdef CLEANUP
2595 Lst_Destroy(sufflist, SuffFree);
2596 Lst_Destroy(suffClean, SuffFree);
2597 if (suffNull)
2598 SuffFree(suffNull);
2599 Lst_Destroy(srclist, NULL);
2600 Lst_Destroy(transforms, NULL);
2601 #endif
2602 }
2603
2604
2605 /********************* DEBUGGING FUNCTIONS **********************/
2606
2607 static int SuffPrintName(void *s, void *dummy MAKE_ATTR_UNUSED)
2608 {
2609
2610 fprintf(debug_file, "%s ", ((Suff *)s)->name);
2611 return 0;
2612 }
2613
2614 static int
2615 SuffPrintSuff(void *sp, void *dummy MAKE_ATTR_UNUSED)
2616 {
2617 Suff *s = (Suff *)sp;
2618 int flags;
2619 int flag;
2620
2621 fprintf(debug_file, "# `%s' [%d] ", s->name, s->refCount);
2622
2623 flags = s->flags;
2624 if (flags) {
2625 fputs(" (", debug_file);
2626 while (flags) {
2627 flag = 1 << (ffs(flags) - 1);
2628 flags &= ~flag;
2629 switch (flag) {
2630 case SUFF_NULL:
2631 fprintf(debug_file, "NULL");
2632 break;
2633 case SUFF_INCLUDE:
2634 fprintf(debug_file, "INCLUDE");
2635 break;
2636 case SUFF_LIBRARY:
2637 fprintf(debug_file, "LIBRARY");
2638 break;
2639 }
2640 fputc(flags ? '|' : ')', debug_file);
2641 }
2642 }
2643 fputc('\n', debug_file);
2644 fprintf(debug_file, "#\tTo: ");
2645 Lst_ForEach(s->parents, SuffPrintName, NULL);
2646 fputc('\n', debug_file);
2647 fprintf(debug_file, "#\tFrom: ");
2648 Lst_ForEach(s->children, SuffPrintName, NULL);
2649 fputc('\n', debug_file);
2650 fprintf(debug_file, "#\tSearch Path: ");
2651 Dir_PrintPath(s->searchPath);
2652 fputc('\n', debug_file);
2653 return 0;
2654 }
2655
2656 static int
2657 SuffPrintTrans(void *tp, void *dummy MAKE_ATTR_UNUSED)
2658 {
2659 GNode *t = (GNode *)tp;
2660
2661 fprintf(debug_file, "%-16s: ", t->name);
2662 Targ_PrintType(t->type);
2663 fputc('\n', debug_file);
2664 Lst_ForEach(t->commands, Targ_PrintCmd, NULL);
2665 fputc('\n', debug_file);
2666 return 0;
2667 }
2668
2669 void
2670 Suff_PrintAll(void)
2671 {
2672 fprintf(debug_file, "#*** Suffixes:\n");
2673 Lst_ForEach(sufflist, SuffPrintSuff, NULL);
2674
2675 fprintf(debug_file, "#*** Transformations:\n");
2676 Lst_ForEach(transforms, SuffPrintTrans, NULL);
2677 }
2678