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