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