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