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