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