suff.c revision 1.190 1 /* $NetBSD: suff.c,v 1.190 2020/10/18 17:41:06 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.190 2020/10/18 17:41:06 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 SuffFlags {
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 **inout_main, Suff *gs_s, Boolean *gs_r)
700 {
701 Suff *s, *t;
702 char *ptr;
703
704 if (*inout_main == NULL && *gs_r && !(target->type & OP_NOTARGET)) {
705 *inout_main = 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 (*inout_main == target) {
719 *gs_r = TRUE;
720 *inout_main = 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 /* Look at all existing targets to see if adding this suffix will make one
738 * of the current targets mutate into a suffix rule.
739 *
740 * This is ugly, but other makes treat all targets that start with a '.' as
741 * suffix rules. */
742 static void
743 UpdateTargets(GNode **inout_main, Suff *s)
744 {
745 Boolean r = FALSE;
746 GNodeListNode *ln;
747 for (ln = Targ_List()->first; ln != NULL; ln = ln->next) {
748 GNode *gn = ln->datum;
749 if (SuffScanTargets(gn, inout_main, s, &r))
750 break;
751 }
752 }
753
754 /* Add the suffix to the end of the list of known suffixes.
755 * Should we restructure the suffix graph? Make doesn't...
756 *
757 * A GNode is created for the suffix and a Suff structure is created and
758 * added to the suffixes list unless the suffix was already known.
759 * The mainNode passed can be modified if a target mutated into a
760 * transform and that target happened to be the main target.
761 *
762 * Input:
763 * name the name of the suffix to add
764 */
765 void
766 Suff_AddSuffix(const char *name, GNode **inout_main)
767 {
768 Suff *s = FindSuffByName(name);
769 if (s != NULL)
770 return;
771
772 s = SuffNew(name);
773 Lst_Append(sufflist, s);
774
775 UpdateTargets(inout_main, s);
776
777 /*
778 * Look for any existing transformations from or to this suffix.
779 * XXX: Only do this after a Suff_ClearSuffixes?
780 */
781 Lst_ForEach(transforms, SuffRebuildGraph, s);
782 }
783
784 /* Return the search path for the given suffix, or NULL. */
785 SearchPath *
786 Suff_GetPath(const char *sname)
787 {
788 Suff *s = FindSuffByName(sname);
789 return s != NULL ? s->searchPath : NULL;
790 }
791
792 /* Extend the search paths for all suffixes to include the default search
793 * path.
794 *
795 * The searchPath field of all the suffixes is extended by the directories
796 * in dirSearchPath. If paths were specified for the ".h" suffix, the
797 * directories are stuffed into a global variable called ".INCLUDES" with
798 * each directory preceded by a -I. The same is done for the ".a" suffix,
799 * except the variable is called ".LIBS" and the flag is -L.
800 */
801 void
802 Suff_DoPaths(void)
803 {
804 SuffListNode *ln;
805 char *ptr;
806 SearchPath *inIncludes; /* Cumulative .INCLUDES path */
807 SearchPath *inLibs; /* Cumulative .LIBS path */
808
809 inIncludes = Lst_New();
810 inLibs = Lst_New();
811
812 for (ln = sufflist->first; ln != NULL; ln = ln->next) {
813 Suff *s = LstNode_Datum(ln);
814 if (!Lst_IsEmpty(s->searchPath)) {
815 #ifdef INCLUDES
816 if (s->flags & SUFF_INCLUDE) {
817 Dir_Concat(inIncludes, s->searchPath);
818 }
819 #endif
820 #ifdef LIBRARIES
821 if (s->flags & SUFF_LIBRARY) {
822 Dir_Concat(inLibs, s->searchPath);
823 }
824 #endif
825 Dir_Concat(s->searchPath, dirSearchPath);
826 } else {
827 Lst_Destroy(s->searchPath, Dir_Destroy);
828 s->searchPath = Lst_Copy(dirSearchPath, Dir_CopyDir);
829 }
830 }
831
832 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL);
833 free(ptr);
834 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL);
835 free(ptr);
836
837 Lst_Destroy(inIncludes, Dir_Destroy);
838 Lst_Destroy(inLibs, Dir_Destroy);
839 }
840
841 /* Add the given suffix as a type of file which gets included.
842 * Called from the parse module when a .INCLUDES line is parsed.
843 * The suffix must have already been defined.
844 * The SUFF_INCLUDE bit is set in the suffix's flags field.
845 *
846 * Input:
847 * sname Name of the suffix to mark
848 */
849 void
850 Suff_AddInclude(const char *sname)
851 {
852 Suff *suff = FindSuffByName(sname);
853 if (suff != NULL)
854 suff->flags |= SUFF_INCLUDE;
855 }
856
857 /* Add the given suffix as a type of file which is a library.
858 * Called from the parse module when parsing a .LIBS line.
859 * The suffix must have been defined via .SUFFIXES before this is called.
860 * The SUFF_LIBRARY bit is set in the suffix's flags field.
861 *
862 * Input:
863 * sname Name of the suffix to mark
864 */
865 void
866 Suff_AddLib(const char *sname)
867 {
868 Suff *suff = FindSuffByName(sname);
869 if (suff != NULL)
870 suff->flags |= SUFF_LIBRARY;
871 }
872
873 /********** Implicit Source Search Functions *********/
874
875 #ifdef DEBUG_SRC
876 static void
877 PrintAddr(void *a, void *b MAKE_ATTR_UNUSED)
878 {
879 printf("%lx ", (unsigned long) a);
880 }
881 #endif
882
883 static void
884 SuffAddSrc(Suff *suff, SrcList *srcList, Src *targ, char *srcName,
885 const char *debug_tag)
886 {
887 Src *s2 = bmake_malloc(sizeof(Src));
888 s2->file = srcName;
889 s2->pref = targ->pref;
890 s2->parent = targ;
891 s2->node = NULL;
892 s2->suff = suff;
893 suff->refCount++;
894 s2->children = 0;
895 targ->children++;
896 Lst_Append(srcList, s2);
897 #ifdef DEBUG_SRC
898 s2->cp = Lst_New();
899 Lst_Append(targ->cp, s2);
900 debug_printf("%s add %p %p to %p:", debug_tag, targ, s2, srcList);
901 Lst_ForEach(srcList, PrintAddr, NULL);
902 debug_printf("\n");
903 #endif
904 }
905
906 /* Add a suffix as a Src structure to the given list with its parent
907 * being the given Src structure. If the suffix is the null suffix,
908 * the prefix is used unaltered as the file name in the Src structure.
909 *
910 * Input:
911 * suff suffix for which to create a Src structure
912 * srcList list for the new Src
913 * targ parent for the new Src
914 */
915 static void
916 SuffAddSources(Suff *suff, SrcList *srcList, Src *targ)
917 {
918 if ((suff->flags & SUFF_NULL) && suff->name[0] != '\0') {
919 /*
920 * If the suffix has been marked as the NULL suffix, also create a Src
921 * structure for a file with no suffix attached. Two birds, and all
922 * that...
923 */
924 SuffAddSrc(suff, srcList, targ, bmake_strdup(targ->pref), "1");
925 }
926 SuffAddSrc(suff, srcList, targ, str_concat2(targ->pref, suff->name), "2");
927 }
928
929 /* Add all the children of targ as Src structures to the given list.
930 *
931 * Input:
932 * l list to which to add the new level
933 * targ Src structure to use as the parent
934 */
935 static void
936 SuffAddLevel(SrcList *l, Src *targ)
937 {
938 SrcListNode *ln;
939 for (ln = targ->suff->children->first; ln != NULL; ln = ln->next) {
940 Suff *childSuff = ln->datum;
941 SuffAddSources(childSuff, l, targ);
942 }
943 }
944
945 /* Free the first Src in the list that doesn't have a reference count.
946 * Return whether a Src was removed. */
947 static Boolean
948 SuffRemoveSrc(SrcList *l)
949 {
950 SrcListNode *ln;
951
952 Lst_Open(l);
953
954 #ifdef DEBUG_SRC
955 debug_printf("cleaning %lx: ", (unsigned long) l);
956 Lst_ForEach(l, PrintAddr, NULL);
957 debug_printf("\n");
958 #endif
959
960 while ((ln = Lst_Next(l)) != NULL) {
961 Src *s = LstNode_Datum(ln);
962 if (s->children == 0) {
963 free(s->file);
964 if (!s->parent)
965 free(s->pref);
966 else {
967 #ifdef DEBUG_SRC
968 SrcListNode *ln2 = Lst_FindDatum(s->parent->cp, s);
969 if (ln2 != NULL)
970 Lst_Remove(s->parent->cp, ln2);
971 #endif
972 --s->parent->children;
973 }
974 #ifdef DEBUG_SRC
975 debug_printf("free: [l=%p] p=%p %d\n", l, s, s->children);
976 Lst_Free(s->cp);
977 #endif
978 Lst_Remove(l, ln);
979 free(s);
980 Lst_Close(l);
981 return TRUE;
982 }
983 #ifdef DEBUG_SRC
984 else {
985 debug_printf("keep: [l=%p] p=%p %d: ", l, s, s->children);
986 Lst_ForEach(s->cp, PrintAddr, NULL);
987 debug_printf("\n");
988 }
989 #endif
990 }
991
992 Lst_Close(l);
993
994 return FALSE;
995 }
996
997 /* Find the first existing file/target in the list srcs.
998 *
999 * Input:
1000 * srcs list of Src structures to search through
1001 *
1002 * Results:
1003 * The lowest structure in the chain of transformations, or NULL.
1004 */
1005 static Src *
1006 SuffFindThem(SrcList *srcs, SrcList *slst)
1007 {
1008 Src *s; /* current Src */
1009 Src *rs; /* returned Src */
1010 char *ptr;
1011
1012 rs = NULL;
1013
1014 while (!Lst_IsEmpty(srcs)) {
1015 s = Lst_Dequeue(srcs);
1016
1017 SUFF_DEBUG1("\ttrying %s...", s->file);
1018
1019 /*
1020 * A file is considered to exist if either a node exists in the
1021 * graph for it or the file actually exists.
1022 */
1023 if (Targ_FindNode(s->file) != NULL) {
1024 #ifdef DEBUG_SRC
1025 debug_printf("remove %p from %p\n", s, srcs);
1026 #endif
1027 rs = s;
1028 break;
1029 }
1030
1031 if ((ptr = Dir_FindFile(s->file, s->suff->searchPath)) != NULL) {
1032 rs = s;
1033 #ifdef DEBUG_SRC
1034 debug_printf("remove %p from %p\n", s, srcs);
1035 #endif
1036 free(ptr);
1037 break;
1038 }
1039
1040 SUFF_DEBUG0("not there\n");
1041
1042 SuffAddLevel(srcs, s);
1043 Lst_Append(slst, s);
1044 }
1045
1046 if (rs) {
1047 SUFF_DEBUG0("got it\n");
1048 }
1049 return rs;
1050 }
1051
1052 /* See if any of the children of the target in the Src structure is one from
1053 * which the target can be transformed. If there is one, a Src structure is
1054 * put together for it and returned.
1055 *
1056 * Input:
1057 * targ Src to play with
1058 *
1059 * Results:
1060 * The Src of the "winning" child, or NULL.
1061 */
1062 static Src *
1063 SuffFindCmds(Src *targ, SrcList *slst)
1064 {
1065 GNodeListNode *gln;
1066 GNode *t; /* Target GNode */
1067 GNode *s; /* Source GNode */
1068 size_t prefLen; /* The length of the defined prefix */
1069 Suff *suff; /* Suffix on matching beastie */
1070 Src *ret; /* Return value */
1071 char *cp;
1072
1073 t = targ->node;
1074 Lst_Open(t->children);
1075 prefLen = strlen(targ->pref);
1076
1077 for (;;) {
1078 gln = Lst_Next(t->children);
1079 if (gln == NULL) {
1080 Lst_Close(t->children);
1081 return NULL;
1082 }
1083 s = LstNode_Datum(gln);
1084
1085 if (s->type & OP_OPTIONAL && Lst_IsEmpty(t->commands)) {
1086 /*
1087 * We haven't looked to see if .OPTIONAL files exist yet, so
1088 * don't use one as the implicit source.
1089 * This allows us to use .OPTIONAL in .depend files so make won't
1090 * complain "don't know how to make xxx.h' when a dependent file
1091 * has been moved/deleted.
1092 */
1093 continue;
1094 }
1095
1096 cp = strrchr(s->name, '/');
1097 if (cp == NULL) {
1098 cp = s->name;
1099 } else {
1100 cp++;
1101 }
1102 if (strncmp(cp, targ->pref, prefLen) != 0)
1103 continue;
1104 /*
1105 * The node matches the prefix ok, see if it has a known
1106 * suffix.
1107 */
1108 suff = FindSuffByName(cp + prefLen);
1109 if (suff == NULL)
1110 continue;
1111
1112 /*
1113 * It even has a known suffix, see if there's a transformation
1114 * defined between the node's suffix and the target's suffix.
1115 *
1116 * XXX: Handle multi-stage transformations here, too.
1117 */
1118
1119 /* XXX: Can targ->suff be NULL here? */
1120 if (targ->suff != NULL &&
1121 Lst_FindDatum(suff->parents, targ->suff) != NULL)
1122 break;
1123 }
1124
1125 /*
1126 * Hot Damn! Create a new Src structure to describe
1127 * this transformation (making sure to duplicate the
1128 * source node's name so Suff_FindDeps can free it
1129 * again (ick)), and return the new structure.
1130 */
1131 ret = bmake_malloc(sizeof(Src));
1132 ret->file = bmake_strdup(s->name);
1133 ret->pref = targ->pref;
1134 ret->suff = suff;
1135 suff->refCount++;
1136 ret->parent = targ;
1137 ret->node = s;
1138 ret->children = 0;
1139 targ->children++;
1140 #ifdef DEBUG_SRC
1141 ret->cp = Lst_New();
1142 debug_printf("3 add %p %p\n", targ, ret);
1143 Lst_Append(targ->cp, ret);
1144 #endif
1145 Lst_Append(slst, ret);
1146 SUFF_DEBUG1("\tusing existing source %s\n", s->name);
1147 Lst_Close(t->children);
1148 return ret;
1149 }
1150
1151 /* Expand the names of any children of a given node that contain variable
1152 * expressions or file wildcards into actual targets.
1153 *
1154 * The expanded node is removed from the parent's list of children, and the
1155 * parent's unmade counter is decremented, but other nodes may be added.
1156 *
1157 * Input:
1158 * cln Child to examine
1159 * pgn Parent node being processed
1160 */
1161 static void
1162 SuffExpandChildren(GNodeListNode *cln, GNode *pgn)
1163 {
1164 GNode *cgn = LstNode_Datum(cln);
1165 GNode *gn; /* New source 8) */
1166 char *cp; /* Expanded value */
1167
1168 if (!Lst_IsEmpty(cgn->order_pred) || !Lst_IsEmpty(cgn->order_succ))
1169 /* It is all too hard to process the result of .ORDER */
1170 return;
1171
1172 if (cgn->type & OP_WAIT)
1173 /* Ignore these (& OP_PHONY ?) */
1174 return;
1175
1176 /*
1177 * First do variable expansion -- this takes precedence over
1178 * wildcard expansion. If the result contains wildcards, they'll be gotten
1179 * to later since the resulting words are tacked on to the end of
1180 * the children list.
1181 */
1182 if (strchr(cgn->name, '$') == NULL) {
1183 SuffExpandWildcards(cln, pgn);
1184 return;
1185 }
1186
1187 SUFF_DEBUG1("Expanding \"%s\"...", cgn->name);
1188 (void)Var_Subst(cgn->name, pgn, VARE_UNDEFERR|VARE_WANTRES, &cp);
1189 /* TODO: handle errors */
1190
1191 {
1192 GNodeList *members = Lst_New();
1193
1194 if (cgn->type & OP_ARCHV) {
1195 /*
1196 * Node was an archive(member) target, so we want to call
1197 * on the Arch module to find the nodes for us, expanding
1198 * variables in the parent's context.
1199 */
1200 char *sacrifice = cp;
1201
1202 (void)Arch_ParseArchive(&sacrifice, members, pgn);
1203 } else {
1204 /*
1205 * Break the result into a vector of strings whose nodes
1206 * we can find, then add those nodes to the members list.
1207 * Unfortunately, we can't use brk_string b/c it
1208 * doesn't understand about variable specifications with
1209 * spaces in them...
1210 */
1211 char *start;
1212 char *initcp = cp; /* For freeing... */
1213
1214 for (start = cp; *start == ' ' || *start == '\t'; start++)
1215 continue;
1216 cp = start;
1217 while (*cp != '\0') {
1218 if (*cp == ' ' || *cp == '\t') {
1219 /*
1220 * White-space -- terminate element, find the node,
1221 * add it, skip any further spaces.
1222 */
1223 *cp++ = '\0';
1224 gn = Targ_GetNode(start);
1225 Lst_Append(members, gn);
1226 while (*cp == ' ' || *cp == '\t') {
1227 cp++;
1228 }
1229 start = cp; /* Continue at the next non-space. */
1230 } else if (*cp == '$') {
1231 /*
1232 * Start of a variable spec -- contact variable module
1233 * to find the end so we can skip over it.
1234 */
1235 const char *nested_p = cp;
1236 const char *junk;
1237 void *freeIt;
1238
1239 /* XXX: Why VARE_WANTRES when the result is not used? */
1240 (void)Var_Parse(&nested_p, pgn,
1241 VARE_UNDEFERR|VARE_WANTRES,
1242 &junk, &freeIt);
1243 /* TODO: handle errors */
1244 if (junk == var_Error) {
1245 Parse_Error(PARSE_FATAL,
1246 "Malformed variable expression at \"%s\"",
1247 cp);
1248 cp++;
1249 } else {
1250 cp += nested_p - cp;
1251 }
1252
1253 free(freeIt);
1254 } else if (*cp == '\\' && cp[1] != '\0') {
1255 /*
1256 * Escaped something -- skip over it
1257 */
1258 /* XXX: In other places, escaping at this syntactical
1259 * position is done by a '$', not a '\'. The '\' is only
1260 * used in variable modifiers. */
1261 cp += 2;
1262 } else {
1263 cp++;
1264 }
1265 }
1266
1267 if (cp != start) {
1268 /*
1269 * Stuff left over -- add it to the list too
1270 */
1271 gn = Targ_GetNode(start);
1272 Lst_Append(members, gn);
1273 }
1274 /*
1275 * Point cp back at the beginning again so the variable value
1276 * can be freed.
1277 */
1278 cp = initcp;
1279 }
1280
1281 /*
1282 * Add all elements of the members list to the parent node.
1283 */
1284 while(!Lst_IsEmpty(members)) {
1285 gn = Lst_Dequeue(members);
1286
1287 SUFF_DEBUG1("%s...", gn->name);
1288 /* Add gn to the parents child list before the original child */
1289 Lst_InsertBefore(pgn->children, cln, gn);
1290 Lst_Append(gn->parents, pgn);
1291 pgn->unmade++;
1292 /* Expand wildcards on new node */
1293 SuffExpandWildcards(cln->prev, pgn);
1294 }
1295 Lst_Free(members);
1296
1297 /*
1298 * Free the result
1299 */
1300 free(cp);
1301 }
1302
1303 SUFF_DEBUG0("\n");
1304
1305 /*
1306 * Now the source is expanded, remove it from the list of children to
1307 * keep it from being processed.
1308 */
1309 pgn->unmade--;
1310 Lst_Remove(pgn->children, cln);
1311 Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
1312 }
1313
1314 static void
1315 SuffExpandWildcards(GNodeListNode *cln, GNode *pgn)
1316 {
1317 GNode *cgn = LstNode_Datum(cln);
1318 StringList *explist;
1319
1320 if (!Dir_HasWildcards(cgn->name))
1321 return;
1322
1323 /*
1324 * Expand the word along the chosen path
1325 */
1326 explist = Lst_New();
1327 Dir_Expand(cgn->name, Suff_FindPath(cgn), explist);
1328
1329 while (!Lst_IsEmpty(explist)) {
1330 GNode *gn;
1331 /*
1332 * Fetch next expansion off the list and find its GNode
1333 */
1334 char *cp = Lst_Dequeue(explist);
1335
1336 SUFF_DEBUG1("%s...", cp);
1337 gn = Targ_GetNode(cp);
1338
1339 /* Add gn to the parents child list before the original child */
1340 Lst_InsertBefore(pgn->children, cln, gn);
1341 Lst_Append(gn->parents, pgn);
1342 pgn->unmade++;
1343 }
1344
1345 Lst_Free(explist);
1346
1347 SUFF_DEBUG0("\n");
1348
1349 /*
1350 * Now the source is expanded, remove it from the list of children to
1351 * keep it from being processed.
1352 */
1353 pgn->unmade--;
1354 Lst_Remove(pgn->children, cln);
1355 Lst_Remove(cgn->parents, Lst_FindDatum(cgn->parents, pgn));
1356 }
1357
1358 /* Find a path along which to expand the node.
1359 *
1360 * If the word has a known suffix, use that path.
1361 * If it has no known suffix, use the default system search path.
1362 *
1363 * Input:
1364 * gn Node being examined
1365 *
1366 * Results:
1367 * The appropriate path to search for the GNode.
1368 */
1369 SearchPath *
1370 Suff_FindPath(GNode* gn)
1371 {
1372 Suff *suff = gn->suffix;
1373
1374 if (suff == NULL) {
1375 struct SuffSuffGetSuffixArgs sd; /* Search string data */
1376 SuffListNode *ln;
1377 sd.name_len = strlen(gn->name);
1378 sd.name_end = gn->name + sd.name_len;
1379 ln = Lst_Find(sufflist, SuffSuffIsSuffix, &sd);
1380
1381 SUFF_DEBUG1("Wildcard expanding \"%s\"...", gn->name);
1382 if (ln != NULL)
1383 suff = LstNode_Datum(ln);
1384 /* XXX: Here we can save the suffix so we don't have to do this again */
1385 }
1386
1387 if (suff != NULL) {
1388 SUFF_DEBUG1("suffix is \"%s\"...", suff->name);
1389 return suff->searchPath;
1390 } else {
1391 /*
1392 * Use default search path
1393 */
1394 return dirSearchPath;
1395 }
1396 }
1397
1398 /* Apply a transformation rule, given the source and target nodes and
1399 * suffixes.
1400 *
1401 * Input:
1402 * tGn Target node
1403 * sGn Source node
1404 * t Target suffix
1405 * s Source suffix
1406 *
1407 * Results:
1408 * TRUE if successful, FALSE if not.
1409 *
1410 * Side Effects:
1411 * The source and target are linked and the commands from the
1412 * transformation are added to the target node's commands list.
1413 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1414 * to the target. The target also inherits all the sources for
1415 * the transformation rule.
1416 */
1417 static Boolean
1418 SuffApplyTransform(GNode *tGn, GNode *sGn, Suff *t, Suff *s)
1419 {
1420 GNodeListNode *ln, *nln; /* General node */
1421 char *tname; /* Name of transformation rule */
1422 GNode *gn; /* Node for same */
1423
1424 /*
1425 * Form the proper links between the target and source.
1426 */
1427 Lst_Append(tGn->children, sGn);
1428 Lst_Append(sGn->parents, tGn);
1429 tGn->unmade++;
1430
1431 /*
1432 * Locate the transformation rule itself
1433 */
1434 tname = str_concat2(s->name, t->name);
1435 ln = Lst_Find(transforms, SuffGNHasName, tname);
1436 free(tname);
1437
1438 if (ln == NULL) {
1439 /*
1440 * Not really such a transformation rule (can happen when we're
1441 * called to link an OP_MEMBER and OP_ARCHV node), so return
1442 * FALSE.
1443 */
1444 return FALSE;
1445 }
1446
1447 gn = LstNode_Datum(ln);
1448
1449 SUFF_DEBUG3("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
1450
1451 /*
1452 * Record last child for expansion purposes
1453 */
1454 ln = Lst_Last(tGn->children);
1455
1456 /*
1457 * Pass the buck to Make_HandleUse to apply the rule
1458 */
1459 (void)Make_HandleUse(gn, tGn);
1460
1461 /*
1462 * Deal with wildcards and variables in any acquired sources
1463 */
1464 for (ln = ln != NULL ? ln->next : NULL; ln != NULL; ln = nln) {
1465 nln = ln->next;
1466 SuffExpandChildren(ln, tGn);
1467 }
1468
1469 /*
1470 * Keep track of another parent to which this beast is transformed so
1471 * the .IMPSRC variable can be set correctly for the parent.
1472 */
1473 Lst_Append(sGn->implicitParents, tGn);
1474
1475 return TRUE;
1476 }
1477
1478
1479 /* Locate dependencies for an OP_ARCHV node.
1480 *
1481 * Input:
1482 * gn Node for which to locate dependencies
1483 *
1484 * Side Effects:
1485 * Same as Suff_FindDeps
1486 */
1487 static void
1488 SuffFindArchiveDeps(GNode *gn, SrcList *slst)
1489 {
1490 char *eoarch; /* End of archive portion */
1491 char *eoname; /* End of member portion */
1492 GNode *mem; /* Node for member */
1493 SuffListNode *ln, *nln; /* Next suffix node to check */
1494 Suff *ms; /* Suffix descriptor for member */
1495 char *name; /* Start of member's name */
1496
1497 /*
1498 * The node is an archive(member) pair. so we must find a
1499 * suffix for both of them.
1500 */
1501 eoarch = strchr(gn->name, '(');
1502 eoname = strchr(eoarch, ')');
1503
1504 /*
1505 * Caller guarantees the format `libname(member)', via
1506 * Arch_ParseArchive.
1507 */
1508 assert(eoarch != NULL);
1509 assert(eoname != NULL);
1510
1511 *eoname = '\0'; /* Nuke parentheses during suffix search */
1512 *eoarch = '\0'; /* So a suffix can be found */
1513
1514 name = eoarch + 1;
1515
1516 /*
1517 * To simplify things, call Suff_FindDeps recursively on the member now,
1518 * so we can simply compare the member's .PREFIX and .TARGET variables
1519 * to locate its suffix. This allows us to figure out the suffix to
1520 * use for the archive without having to do a quadratic search over the
1521 * suffix list, backtracking for each one...
1522 */
1523 mem = Targ_GetNode(name);
1524 SuffFindDeps(mem, slst);
1525
1526 /*
1527 * Create the link between the two nodes right off
1528 */
1529 Lst_Append(gn->children, mem);
1530 Lst_Append(mem->parents, gn);
1531 gn->unmade++;
1532
1533 /*
1534 * Copy in the variables from the member node to this one.
1535 */
1536 {
1537 char *freeIt;
1538 Var_Set(PREFIX, Var_Value(PREFIX, mem, &freeIt), gn);
1539 bmake_free(freeIt);
1540 Var_Set(TARGET, Var_Value(TARGET, mem, &freeIt), gn);
1541 bmake_free(freeIt);
1542 }
1543
1544 ms = mem->suffix;
1545 if (ms == NULL) {
1546 /*
1547 * Didn't know what it was -- use .NULL suffix if not in make mode
1548 */
1549 SUFF_DEBUG0("using null suffix\n");
1550 ms = suffNull;
1551 }
1552
1553
1554 /*
1555 * Set the other two local variables required for this target.
1556 */
1557 Var_Set(MEMBER, name, gn);
1558 Var_Set(ARCHIVE, gn->name, gn);
1559
1560 /*
1561 * Set $@ for compatibility with other makes
1562 */
1563 Var_Set(TARGET, gn->name, gn);
1564
1565 /*
1566 * Now we've got the important local variables set, expand any sources
1567 * that still contain variables or wildcards in their names.
1568 */
1569 for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
1570 nln = ln->next;
1571 SuffExpandChildren(ln, gn);
1572 }
1573
1574 if (ms != NULL) {
1575 /*
1576 * Member has a known suffix, so look for a transformation rule from
1577 * it to a possible suffix of the archive. Rather than searching
1578 * through the entire list, we just look at suffixes to which the
1579 * member's suffix may be transformed...
1580 */
1581 struct SuffSuffGetSuffixArgs sd; /* Search string data */
1582
1583 /*
1584 * Use first matching suffix...
1585 */
1586 sd.name_len = (size_t)(eoarch - gn->name);
1587 sd.name_end = eoarch;
1588 ln = Lst_Find(ms->parents, SuffSuffIsSuffix, &sd);
1589
1590 if (ln != NULL) {
1591 /*
1592 * Got one -- apply it
1593 */
1594 Suff *suff = LstNode_Datum(ln);
1595 if (!SuffApplyTransform(gn, mem, suff, ms)) {
1596 SUFF_DEBUG2("\tNo transformation from %s -> %s\n",
1597 ms->name, suff->name);
1598 }
1599 }
1600 }
1601
1602 /*
1603 * Replace the opening and closing parens now we've no need of the separate
1604 * pieces.
1605 */
1606 *eoarch = '('; *eoname = ')';
1607
1608 /*
1609 * Pretend gn appeared to the left of a dependency operator so
1610 * the user needn't provide a transformation from the member to the
1611 * archive.
1612 */
1613 if (OP_NOP(gn->type)) {
1614 gn->type |= OP_DEPENDS;
1615 }
1616
1617 /*
1618 * Flag the member as such so we remember to look in the archive for
1619 * its modification time. The OP_JOIN | OP_MADE is needed because this
1620 * target should never get made.
1621 */
1622 mem->type |= OP_MEMBER | OP_JOIN | OP_MADE;
1623 }
1624
1625 /* Locate implicit dependencies for regular targets.
1626 *
1627 * Input:
1628 * gn Node for which to find sources
1629 *
1630 * Side Effects:
1631 * Same as Suff_FindDeps
1632 */
1633 static void
1634 SuffFindNormalDeps(GNode *gn, SrcList *slst)
1635 {
1636 char *eoname; /* End of name */
1637 char *sopref; /* Start of prefix */
1638 SuffListNode *ln, *nln;
1639 SrcList *srcs; /* List of sources at which to look */
1640 SrcList *targs; /* List of targets to which things can be
1641 * transformed. They all have the same file,
1642 * but different suff and pref fields */
1643 Src *bottom; /* Start of found transformation path */
1644 Src *src; /* General Src pointer */
1645 char *pref; /* Prefix to use */
1646 Src *targ; /* General Src target pointer */
1647 struct SuffSuffGetSuffixArgs sd; /* Search string data */
1648
1649
1650 sd.name_len = strlen(gn->name);
1651 sd.name_end = eoname = gn->name + sd.name_len;
1652
1653 sopref = gn->name;
1654
1655 /*
1656 * Begin at the beginning...
1657 */
1658 ln = Lst_First(sufflist);
1659 srcs = Lst_New();
1660 targs = Lst_New();
1661
1662 /*
1663 * We're caught in a catch-22 here. On the one hand, we want to use any
1664 * transformation implied by the target's sources, but we can't examine
1665 * the sources until we've expanded any variables/wildcards they may hold,
1666 * and we can't do that until we've set up the target's local variables
1667 * and we can't do that until we know what the proper suffix for the
1668 * target is (in case there are two suffixes one of which is a suffix of
1669 * the other) and we can't know that until we've found its implied
1670 * source, which we may not want to use if there's an existing source
1671 * that implies a different transformation.
1672 *
1673 * In an attempt to get around this, which may not work all the time,
1674 * but should work most of the time, we look for implied sources first,
1675 * checking transformations to all possible suffixes of the target,
1676 * use what we find to set the target's local variables, expand the
1677 * children, then look for any overriding transformations they imply.
1678 * Should we find one, we discard the one we found before.
1679 */
1680 bottom = NULL;
1681 targ = NULL;
1682
1683 if (!(gn->type & OP_PHONY)) {
1684
1685 while (ln != NULL) {
1686 /*
1687 * Look for next possible suffix...
1688 */
1689 ln = Lst_FindFrom(sufflist, ln, SuffSuffIsSuffix, &sd);
1690
1691 if (ln != NULL) {
1692 const char *eopref;
1693
1694 /*
1695 * Allocate a Src structure to which things can be transformed
1696 */
1697 targ = bmake_malloc(sizeof(Src));
1698 targ->file = bmake_strdup(gn->name);
1699 targ->suff = LstNode_Datum(ln);
1700 targ->suff->refCount++;
1701 targ->node = gn;
1702 targ->parent = NULL;
1703 targ->children = 0;
1704 #ifdef DEBUG_SRC
1705 targ->cp = Lst_New();
1706 #endif
1707
1708 eopref = eoname - targ->suff->nameLen;
1709 targ->pref = bmake_strsedup(sopref, eopref);
1710
1711 /*
1712 * Add nodes from which the target can be made
1713 */
1714 SuffAddLevel(srcs, targ);
1715
1716 /*
1717 * Record the target so we can nuke it
1718 */
1719 Lst_Append(targs, targ);
1720
1721 /*
1722 * Search from this suffix's successor...
1723 */
1724 ln = ln->next;
1725 }
1726 }
1727
1728 /*
1729 * Handle target of unknown suffix...
1730 */
1731 if (Lst_IsEmpty(targs) && suffNull != NULL) {
1732 SUFF_DEBUG1("\tNo known suffix on %s. Using .NULL suffix\n",
1733 gn->name);
1734
1735 targ = bmake_malloc(sizeof(Src));
1736 targ->file = bmake_strdup(gn->name);
1737 targ->suff = suffNull;
1738 targ->suff->refCount++;
1739 targ->node = gn;
1740 targ->parent = NULL;
1741 targ->children = 0;
1742 targ->pref = bmake_strdup(sopref);
1743 #ifdef DEBUG_SRC
1744 targ->cp = Lst_New();
1745 #endif
1746
1747 /*
1748 * Only use the default suffix rules if we don't have commands
1749 * defined for this gnode; traditional make programs used to
1750 * not define suffix rules if the gnode had children but we
1751 * don't do this anymore.
1752 */
1753 if (Lst_IsEmpty(gn->commands))
1754 SuffAddLevel(srcs, targ);
1755 else {
1756 SUFF_DEBUG0("not ");
1757 }
1758
1759 SUFF_DEBUG0("adding suffix rules\n");
1760
1761 Lst_Append(targs, targ);
1762 }
1763
1764 /*
1765 * Using the list of possible sources built up from the target
1766 * suffix(es), try and find an existing file/target that matches.
1767 */
1768 bottom = SuffFindThem(srcs, slst);
1769
1770 if (bottom == NULL) {
1771 /*
1772 * No known transformations -- use the first suffix found
1773 * for setting the local variables.
1774 */
1775 if (!Lst_IsEmpty(targs)) {
1776 targ = LstNode_Datum(Lst_First(targs));
1777 } else {
1778 targ = NULL;
1779 }
1780 } else {
1781 /*
1782 * Work up the transformation path to find the suffix of the
1783 * target to which the transformation was made.
1784 */
1785 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
1786 continue;
1787 }
1788 }
1789
1790 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
1791
1792 pref = (targ != NULL) ? targ->pref : gn->name;
1793 Var_Set(PREFIX, pref, gn);
1794
1795 /*
1796 * Now we've got the important local variables set, expand any sources
1797 * that still contain variables or wildcards in their names.
1798 */
1799 for (ln = Lst_First(gn->children); ln != NULL; ln = nln) {
1800 nln = ln->next;
1801 SuffExpandChildren(ln, gn);
1802 }
1803
1804 if (targ == NULL) {
1805 SUFF_DEBUG1("\tNo valid suffix on %s\n", gn->name);
1806
1807 sfnd_abort:
1808 /*
1809 * Deal with finding the thing on the default search path. We
1810 * always do that, not only if the node is only a source (not
1811 * on the lhs of a dependency operator or [XXX] it has neither
1812 * children or commands) as the old pmake did.
1813 */
1814 if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
1815 free(gn->path);
1816 gn->path = Dir_FindFile(gn->name,
1817 (targ == NULL ? dirSearchPath :
1818 targ->suff->searchPath));
1819 if (gn->path != NULL) {
1820 char *ptr;
1821 Var_Set(TARGET, gn->path, gn);
1822
1823 if (targ != NULL) {
1824 /*
1825 * Suffix known for the thing -- trim the suffix off
1826 * the path to form the proper .PREFIX variable.
1827 */
1828 size_t savep = strlen(gn->path) - targ->suff->nameLen;
1829 char savec;
1830
1831 if (gn->suffix)
1832 gn->suffix->refCount--;
1833 gn->suffix = targ->suff;
1834 gn->suffix->refCount++;
1835
1836 savec = gn->path[savep];
1837 gn->path[savep] = '\0';
1838
1839 if ((ptr = strrchr(gn->path, '/')) != NULL)
1840 ptr++;
1841 else
1842 ptr = gn->path;
1843
1844 Var_Set(PREFIX, ptr, gn);
1845
1846 gn->path[savep] = savec;
1847 } else {
1848 /*
1849 * The .PREFIX gets the full path if the target has
1850 * no known suffix.
1851 */
1852 if (gn->suffix)
1853 gn->suffix->refCount--;
1854 gn->suffix = NULL;
1855
1856 if ((ptr = strrchr(gn->path, '/')) != NULL)
1857 ptr++;
1858 else
1859 ptr = gn->path;
1860
1861 Var_Set(PREFIX, ptr, gn);
1862 }
1863 }
1864 }
1865
1866 goto sfnd_return;
1867 }
1868
1869 /*
1870 * If the suffix indicates that the target is a library, mark that in
1871 * the node's type field.
1872 */
1873 if (targ->suff->flags & SUFF_LIBRARY) {
1874 gn->type |= OP_LIB;
1875 }
1876
1877 /*
1878 * Check for overriding transformation rule implied by sources
1879 */
1880 if (!Lst_IsEmpty(gn->children)) {
1881 src = SuffFindCmds(targ, slst);
1882
1883 if (src != NULL) {
1884 /*
1885 * Free up all the Src structures in the transformation path
1886 * up to, but not including, the parent node.
1887 */
1888 while (bottom && bottom->parent != NULL) {
1889 if (Lst_FindDatum(slst, bottom) == NULL) {
1890 Lst_Append(slst, bottom);
1891 }
1892 bottom = bottom->parent;
1893 }
1894 bottom = src;
1895 }
1896 }
1897
1898 if (bottom == NULL) {
1899 /*
1900 * No idea from where it can come -- return now.
1901 */
1902 goto sfnd_abort;
1903 }
1904
1905 /*
1906 * We now have a list of Src structures headed by 'bottom' and linked via
1907 * their 'parent' pointers. What we do next is create links between
1908 * source and target nodes (which may or may not have been created)
1909 * and set the necessary local variables in each target. The
1910 * commands for each target are set from the commands of the
1911 * transformation rule used to get from the src suffix to the targ
1912 * suffix. Note that this causes the commands list of the original
1913 * node, gn, to be replaced by the commands of the final
1914 * transformation rule. Also, the unmade field of gn is incremented.
1915 * Etc.
1916 */
1917 if (bottom->node == NULL) {
1918 bottom->node = Targ_GetNode(bottom->file);
1919 }
1920
1921 for (src = bottom; src->parent != NULL; src = src->parent) {
1922 targ = src->parent;
1923
1924 if (src->node->suffix)
1925 src->node->suffix->refCount--;
1926 src->node->suffix = src->suff;
1927 src->node->suffix->refCount++;
1928
1929 if (targ->node == NULL) {
1930 targ->node = Targ_GetNode(targ->file);
1931 }
1932
1933 SuffApplyTransform(targ->node, src->node,
1934 targ->suff, src->suff);
1935
1936 if (targ->node != gn) {
1937 /*
1938 * Finish off the dependency-search process for any nodes
1939 * between bottom and gn (no point in questing around the
1940 * filesystem for their implicit source when it's already
1941 * known). Note that the node can't have any sources that
1942 * need expanding, since SuffFindThem will stop on an existing
1943 * node, so all we need to do is set the standard and System V
1944 * variables.
1945 */
1946 targ->node->type |= OP_DEPS_FOUND;
1947
1948 Var_Set(PREFIX, targ->pref, targ->node);
1949
1950 Var_Set(TARGET, targ->node->name, targ->node);
1951 }
1952 }
1953
1954 if (gn->suffix)
1955 gn->suffix->refCount--;
1956 gn->suffix = src->suff;
1957 gn->suffix->refCount++;
1958
1959 /*
1960 * Nuke the transformation path and the Src structures left over in the
1961 * two lists.
1962 */
1963 sfnd_return:
1964 if (bottom)
1965 if (Lst_FindDatum(slst, bottom) == NULL)
1966 Lst_Append(slst, bottom);
1967
1968 while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
1969 continue;
1970
1971 Lst_MoveAll(slst, srcs);
1972 Lst_MoveAll(slst, targs);
1973 }
1974
1975
1976 /* Find implicit sources for the target described by the graph node.
1977 *
1978 * Nodes are added to the graph below the passed-in node. The nodes are
1979 * marked to have their IMPSRC variable filled in. The PREFIX variable is set
1980 * for the given node and all its implied children.
1981 *
1982 * The path found by this target is the shortest path in the transformation
1983 * graph, which may pass through non-existent targets, to an existing target.
1984 * The search continues on all paths from the root suffix until a file is
1985 * found. I.e. if there's a path .o -> .c -> .l -> .l,v from the root and the
1986 * .l,v file exists but the .c and .l files don't, the search will branch out
1987 * in all directions from .o and again from all the nodes on the next level
1988 * until the .l,v node is encountered.
1989 */
1990 void
1991 Suff_FindDeps(GNode *gn)
1992 {
1993
1994 SuffFindDeps(gn, srclist);
1995 while (SuffRemoveSrc(srclist))
1996 continue;
1997 }
1998
1999 static void
2000 SuffFindDeps(GNode *gn, SrcList *slst)
2001 {
2002 if (gn->type & OP_DEPS_FOUND)
2003 return;
2004 gn->type |= OP_DEPS_FOUND;
2005
2006 /*
2007 * Make sure we have these set, may get revised below.
2008 */
2009 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn);
2010 Var_Set(PREFIX, gn->name, gn);
2011
2012 SUFF_DEBUG1("SuffFindDeps (%s)\n", gn->name);
2013
2014 if (gn->type & OP_ARCHV) {
2015 SuffFindArchiveDeps(gn, slst);
2016 } else if (gn->type & OP_LIB) {
2017 /*
2018 * If the node is a library, it is the arch module's job to find it
2019 * and set the TARGET variable accordingly. We merely provide the
2020 * search path, assuming all libraries end in ".a" (if the suffix
2021 * hasn't been defined, there's nothing we can do for it, so we just
2022 * set the TARGET variable to the node's name in order to give it a
2023 * value).
2024 */
2025 Suff *s = FindSuffByName(LIBSUFF);
2026 if (gn->suffix)
2027 gn->suffix->refCount--;
2028 if (s != NULL) {
2029 gn->suffix = s;
2030 gn->suffix->refCount++;
2031 Arch_FindLib(gn, s->searchPath);
2032 } else {
2033 gn->suffix = NULL;
2034 Var_Set(TARGET, gn->name, gn);
2035 }
2036 /*
2037 * Because a library (-lfoo) target doesn't follow the standard
2038 * filesystem conventions, we don't set the regular variables for
2039 * the thing. .PREFIX is simply made empty...
2040 */
2041 Var_Set(PREFIX, "", gn);
2042 } else {
2043 SuffFindNormalDeps(gn, slst);
2044 }
2045 }
2046
2047 /* Define which suffix is the null suffix.
2048 *
2049 * Need to handle the changing of the null suffix gracefully so the old
2050 * transformation rules don't just go away.
2051 *
2052 * Input:
2053 * name Name of null suffix
2054 */
2055 void
2056 Suff_SetNull(const char *name)
2057 {
2058 Suff *s = FindSuffByName(name);
2059 if (s == NULL) {
2060 Parse_Error(PARSE_WARNING, "Desired null suffix %s not defined.",
2061 name);
2062 return;
2063 }
2064
2065 if (suffNull != NULL) {
2066 suffNull->flags &= ~(unsigned)SUFF_NULL;
2067 }
2068 s->flags |= SUFF_NULL;
2069 /*
2070 * XXX: Here's where the transformation mangling would take place
2071 */
2072 suffNull = s;
2073 }
2074
2075 /* Initialize the suffixes module. */
2076 void
2077 Suff_Init(void)
2078 {
2079 #ifdef CLEANUP
2080 suffClean = Lst_New();
2081 sufflist = Lst_New();
2082 #endif
2083 srclist = Lst_New();
2084 transforms = Lst_New();
2085
2086 /*
2087 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2088 * actually go on the suffix list or everyone will think that's its
2089 * suffix.
2090 */
2091 Suff_ClearSuffixes();
2092 }
2093
2094
2095 /* Clean up the suffixes module. */
2096 void
2097 Suff_End(void)
2098 {
2099 #ifdef CLEANUP
2100 Lst_Destroy(sufflist, SuffFree);
2101 Lst_Destroy(suffClean, SuffFree);
2102 if (suffNull)
2103 SuffFree(suffNull);
2104 Lst_Free(srclist);
2105 Lst_Free(transforms);
2106 #endif
2107 }
2108
2109
2110 /********************* DEBUGGING FUNCTIONS **********************/
2111
2112 static void
2113 SuffPrintName(void *s, void *dummy MAKE_ATTR_UNUSED)
2114 {
2115 debug_printf("%s ", ((Suff *)s)->name);
2116 }
2117
2118 static void
2119 SuffPrintSuff(void *sp, void *dummy MAKE_ATTR_UNUSED)
2120 {
2121 Suff *s = (Suff *)sp;
2122
2123 debug_printf("# `%s' [%d] ", s->name, s->refCount);
2124
2125 if (s->flags != 0) {
2126 char flags_buf[SuffFlags_ToStringSize];
2127
2128 debug_printf(" (%s)",
2129 Enum_FlagsToString(flags_buf, sizeof flags_buf,
2130 s->flags, SuffFlags_ToStringSpecs));
2131 }
2132 fputc('\n', debug_file);
2133 debug_printf("#\tTo: ");
2134 Lst_ForEach(s->parents, SuffPrintName, NULL);
2135 fputc('\n', debug_file);
2136 debug_printf("#\tFrom: ");
2137 Lst_ForEach(s->children, SuffPrintName, NULL);
2138 fputc('\n', debug_file);
2139 debug_printf("#\tSearch Path: ");
2140 Dir_PrintPath(s->searchPath);
2141 fputc('\n', debug_file);
2142 }
2143
2144 static void
2145 SuffPrintTrans(void *tp, void *dummy MAKE_ATTR_UNUSED)
2146 {
2147 GNode *t = (GNode *)tp;
2148
2149 debug_printf("%-16s:", t->name);
2150 Targ_PrintType(t->type);
2151 fputc('\n', debug_file);
2152 Targ_PrintCmds(t);
2153 fputc('\n', debug_file);
2154 }
2155
2156 void
2157 Suff_PrintAll(void)
2158 {
2159 debug_printf("#*** Suffixes:\n");
2160 Lst_ForEach(sufflist, SuffPrintSuff, NULL);
2161
2162 debug_printf("#*** Transformations:\n");
2163 Lst_ForEach(transforms, SuffPrintTrans, NULL);
2164 }
2165