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