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