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