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