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