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