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