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