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