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