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