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