suff.c revision 1.38 1 /* $NetBSD: suff.c,v 1.38 2002/02/04 17:24:57 christos Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1989 by Berkeley Softworks
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Adam de Boor.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifdef MAKE_BOOTSTRAP
42 static char rcsid[] = "$NetBSD: suff.c,v 1.38 2002/02/04 17:24:57 christos Exp $";
43 #else
44 #include <sys/cdefs.h>
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94";
48 #else
49 __RCSID("$NetBSD: suff.c,v 1.38 2002/02/04 17:24:57 christos Exp $");
50 #endif
51 #endif /* not lint */
52 #endif
53
54 /*-
55 * suff.c --
56 * Functions to maintain suffix lists and find implicit dependents
57 * using suffix transformation rules
58 *
59 * Interface:
60 * Suff_Init Initialize all things to do with suffixes.
61 *
62 * Suff_End Cleanup the module
63 *
64 * Suff_DoPaths This function is used to make life easier
65 * when searching for a file according to its
66 * suffix. It takes the global search path,
67 * as defined using the .PATH: target, and appends
68 * its directories to the path of each of the
69 * defined suffixes, as specified using
70 * .PATH<suffix>: targets. In addition, all
71 * directories given for suffixes labeled as
72 * include files or libraries, using the .INCLUDES
73 * or .LIBS targets, are played with using
74 * Dir_MakeFlags to create the .INCLUDES and
75 * .LIBS global variables.
76 *
77 * Suff_ClearSuffixes Clear out all the suffixes and defined
78 * transformations.
79 *
80 * Suff_IsTransform Return TRUE if the passed string is the lhs
81 * of a transformation rule.
82 *
83 * Suff_AddSuffix Add the passed string as another known suffix.
84 *
85 * Suff_GetPath Return the search path for the given suffix.
86 *
87 * Suff_AddInclude Mark the given suffix as denoting an include
88 * file.
89 *
90 * Suff_AddLib Mark the given suffix as denoting a library.
91 *
92 * Suff_AddTransform Add another transformation to the suffix
93 * graph. Returns GNode suitable for framing, I
94 * mean, tacking commands, attributes, etc. on.
95 *
96 * Suff_SetNull Define the suffix to consider the suffix of
97 * any file that doesn't have a known one.
98 *
99 * Suff_FindDeps Find implicit sources for and the location of
100 * a target based on its suffix. Returns the
101 * bottom-most node added to the graph or NILGNODE
102 * if the target had no implicit sources.
103 */
104
105 #include <stdio.h>
106 #include "make.h"
107 #include "hash.h"
108 #include "dir.h"
109
110 static Lst sufflist; /* Lst of suffixes */
111 #ifdef CLEANUP
112 static Lst suffClean; /* Lst of suffixes to be cleaned */
113 #endif
114 static Lst srclist; /* Lst of sources */
115 static Lst transforms; /* Lst of transformation rules */
116
117 static int sNum = 0; /* Counter for assigning suffix numbers */
118
119 /*
120 * Structure describing an individual suffix.
121 */
122 typedef struct _Suff {
123 char *name; /* The suffix itself */
124 int nameLen; /* Length of the suffix */
125 short flags; /* Type of suffix */
126 #define SUFF_INCLUDE 0x01 /* One which is #include'd */
127 #define SUFF_LIBRARY 0x02 /* One which contains a library */
128 #define SUFF_NULL 0x04 /* The empty suffix */
129 Lst searchPath; /* The path along which files of this suffix
130 * may be found */
131 int sNum; /* The suffix number */
132 int refCount; /* Reference count of list membership */
133 Lst parents; /* Suffixes we have a transformation to */
134 Lst children; /* Suffixes we have a transformation from */
135 Lst ref; /* List of lists this suffix is referenced */
136 } Suff;
137
138 /*
139 * for SuffSuffIsSuffix
140 */
141 typedef struct {
142 char *ename; /* The end of the name */
143 int len; /* Length of the name */
144 } SuffixCmpData;
145
146 /*
147 * Structure used in the search for implied sources.
148 */
149 typedef struct _Src {
150 char *file; /* The file to look for */
151 char *pref; /* Prefix from which file was formed */
152 Suff *suff; /* The suffix on the file */
153 struct _Src *parent; /* The Src for which this is a source */
154 GNode *node; /* The node describing the file */
155 int children; /* Count of existing children (so we don't free
156 * this thing too early or never nuke it) */
157 #ifdef DEBUG_SRC
158 Lst cp; /* Debug; children list */
159 #endif
160 } Src;
161
162 /*
163 * A structure for passing more than one argument to the Lst-library-invoked
164 * function...
165 */
166 typedef struct {
167 Lst l;
168 Src *s;
169 } LstSrc;
170
171 typedef struct {
172 GNode **gn;
173 Suff *s;
174 Boolean r;
175 } GNodeSuff;
176
177 static Suff *suffNull; /* The NULL suffix for this run */
178 static Suff *emptySuff; /* The empty suffix required for POSIX
179 * single-suffix transformation rules */
180
181
182 static char *SuffStrIsPrefix __P((char *, char *));
183 static char *SuffSuffIsSuffix __P((Suff *, SuffixCmpData *));
184 static int SuffSuffIsSuffixP __P((ClientData, ClientData));
185 static int SuffSuffHasNameP __P((ClientData, ClientData));
186 static int SuffSuffIsPrefix __P((ClientData, ClientData));
187 static int SuffGNHasNameP __P((ClientData, ClientData));
188 static void SuffUnRef __P((ClientData, ClientData));
189 static void SuffFree __P((ClientData));
190 static void SuffInsert __P((Lst, Suff *));
191 static void SuffRemove __P((Lst, Suff *));
192 static Boolean SuffParseTransform __P((char *, Suff **, Suff **));
193 static int SuffRebuildGraph __P((ClientData, ClientData));
194 static int SuffScanTargets __P((ClientData, ClientData));
195 static int SuffAddSrc __P((ClientData, ClientData));
196 static int SuffRemoveSrc __P((Lst));
197 static void SuffAddLevel __P((Lst, Src *));
198 static Src *SuffFindThem __P((Lst, Lst));
199 static Src *SuffFindCmds __P((Src *, Lst));
200 static int SuffExpandChildren __P((LstNode, GNode *));
201 static Boolean SuffApplyTransform __P((GNode *, GNode *, Suff *, Suff *));
202 static void SuffFindDeps __P((GNode *, Lst));
203 static void SuffFindArchiveDeps __P((GNode *, Lst));
204 static void SuffFindNormalDeps __P((GNode *, Lst));
205 static int SuffPrintName __P((ClientData, ClientData));
206 static int SuffPrintSuff __P((ClientData, ClientData));
207 static int SuffPrintTrans __P((ClientData, ClientData));
208
209 /*************** Lst Predicates ****************/
210 /*-
211 *-----------------------------------------------------------------------
212 * SuffStrIsPrefix --
213 * See if pref is a prefix of str.
214 *
215 * Results:
216 * NULL if it ain't, pointer to character in str after prefix if so
217 *
218 * Side Effects:
219 * None
220 *-----------------------------------------------------------------------
221 */
222 static char *
223 SuffStrIsPrefix (pref, str)
224 register char *pref; /* possible prefix */
225 register char *str; /* string to check */
226 {
227 while (*str && *pref == *str) {
228 pref++;
229 str++;
230 }
231
232 return (*pref ? NULL : str);
233 }
234
235 /*-
236 *-----------------------------------------------------------------------
237 * SuffSuffIsSuffix --
238 * See if suff is a suffix of str. sd->ename should point to THE END
239 * of the string to check. (THE END == the null byte)
240 *
241 * Results:
242 * NULL if it ain't, pointer to character in str before suffix if
243 * it is.
244 *
245 * Side Effects:
246 * None
247 *-----------------------------------------------------------------------
248 */
249 static char *
250 SuffSuffIsSuffix (s, sd)
251 register Suff *s; /* possible suffix */
252 SuffixCmpData *sd; /* string to examine */
253 {
254 register char *p1; /* Pointer into suffix name */
255 register char *p2; /* Pointer into string being examined */
256
257 if (sd->len < s->nameLen)
258 return NULL; /* this string is shorter than the suffix */
259
260 p1 = s->name + s->nameLen;
261 p2 = sd->ename;
262
263 while (p1 >= s->name && *p1 == *p2) {
264 p1--;
265 p2--;
266 }
267
268 return (p1 == s->name - 1 ? p2 : NULL);
269 }
270
271 /*-
272 *-----------------------------------------------------------------------
273 * SuffSuffIsSuffixP --
274 * Predicate form of SuffSuffIsSuffix. Passed as the callback function
275 * to Lst_Find.
276 *
277 * Results:
278 * 0 if the suffix is the one desired, non-zero if not.
279 *
280 * Side Effects:
281 * None.
282 *
283 *-----------------------------------------------------------------------
284 */
285 static int
286 SuffSuffIsSuffixP(s, sd)
287 ClientData s;
288 ClientData sd;
289 {
290 return(!SuffSuffIsSuffix((Suff *) s, (SuffixCmpData *) sd));
291 }
292
293 /*-
294 *-----------------------------------------------------------------------
295 * SuffSuffHasNameP --
296 * Callback procedure for finding a suffix based on its name. Used by
297 * Suff_GetPath.
298 *
299 * Results:
300 * 0 if the suffix is of the given name. non-zero otherwise.
301 *
302 * Side Effects:
303 * None
304 *-----------------------------------------------------------------------
305 */
306 static int
307 SuffSuffHasNameP (s, sname)
308 ClientData s; /* Suffix to check */
309 ClientData sname; /* Desired name */
310 {
311 return (strcmp ((char *) sname, ((Suff *) s)->name));
312 }
313
314 /*-
315 *-----------------------------------------------------------------------
316 * SuffSuffIsPrefix --
317 * See if the suffix described by s is a prefix of the string. Care
318 * must be taken when using this to search for transformations and
319 * what-not, since there could well be two suffixes, one of which
320 * is a prefix of the other...
321 *
322 * Results:
323 * 0 if s is a prefix of str. non-zero otherwise
324 *
325 * Side Effects:
326 * None
327 *-----------------------------------------------------------------------
328 */
329 static int
330 SuffSuffIsPrefix (s, str)
331 ClientData s; /* suffix to compare */
332 ClientData str; /* string to examine */
333 {
334 return (SuffStrIsPrefix (((Suff *) s)->name, (char *) str) == NULL ? 1 : 0);
335 }
336
337 /*-
338 *-----------------------------------------------------------------------
339 * SuffGNHasNameP --
340 * See if the graph node has the desired name
341 *
342 * Results:
343 * 0 if it does. non-zero if it doesn't
344 *
345 * Side Effects:
346 * None
347 *-----------------------------------------------------------------------
348 */
349 static int
350 SuffGNHasNameP (gn, name)
351 ClientData gn; /* current node we're looking at */
352 ClientData name; /* name we're looking for */
353 {
354 return (strcmp ((char *) name, ((GNode *) gn)->name));
355 }
356
357 /*********** Maintenance Functions ************/
358
359 static void
360 SuffUnRef(lp, sp)
361 ClientData lp;
362 ClientData sp;
363 {
364 Lst l = (Lst) lp;
365
366 LstNode ln = Lst_Member(l, sp);
367 if (ln != NILLNODE) {
368 Lst_Remove(l, ln);
369 ((Suff *) sp)->refCount--;
370 }
371 }
372
373 /*-
374 *-----------------------------------------------------------------------
375 * SuffFree --
376 * Free up all memory associated with the given suffix structure.
377 *
378 * Results:
379 * none
380 *
381 * Side Effects:
382 * the suffix entry is detroyed
383 *-----------------------------------------------------------------------
384 */
385 static void
386 SuffFree (sp)
387 ClientData sp;
388 {
389 Suff *s = (Suff *) sp;
390
391 if (s == suffNull)
392 suffNull = NULL;
393
394 if (s == emptySuff)
395 emptySuff = NULL;
396
397 #ifdef notdef
398 /* We don't delete suffixes in order, so we cannot use this */
399 if (s->refCount)
400 Punt("Internal error deleting suffix `%s' with refcount = %d", s->name,
401 s->refCount);
402 #endif
403
404 Lst_Destroy (s->ref, NOFREE);
405 Lst_Destroy (s->children, NOFREE);
406 Lst_Destroy (s->parents, NOFREE);
407 Lst_Destroy (s->searchPath, Dir_Destroy);
408
409 free ((Address)s->name);
410 free ((Address)s);
411 }
412
413 /*-
414 *-----------------------------------------------------------------------
415 * SuffRemove --
416 * Remove the suffix into the list
417 *
418 * Results:
419 * None
420 *
421 * Side Effects:
422 * The reference count for the suffix is decremented and the
423 * suffix is possibly freed
424 *-----------------------------------------------------------------------
425 */
426 static void
427 SuffRemove(l, s)
428 Lst l;
429 Suff *s;
430 {
431 SuffUnRef((ClientData) l, (ClientData) s);
432 if (s->refCount == 0) {
433 SuffUnRef ((ClientData) sufflist, (ClientData) s);
434 SuffFree((ClientData) s);
435 }
436 }
437
438 /*-
440 *-----------------------------------------------------------------------
441 * SuffInsert --
442 * Insert the suffix into the list keeping the list ordered by suffix
443 * numbers.
444 *
445 * Results:
446 * None
447 *
448 * Side Effects:
449 * The reference count of the suffix is incremented
450 *-----------------------------------------------------------------------
451 */
452 static void
453 SuffInsert (l, s)
454 Lst l; /* the list where in s should be inserted */
455 Suff *s; /* the suffix to insert */
456 {
457 LstNode ln; /* current element in l we're examining */
458 Suff *s2 = NULL; /* the suffix descriptor in this element */
459
460 if (Lst_Open (l) == FAILURE) {
461 return;
462 }
463 while ((ln = Lst_Next (l)) != NILLNODE) {
464 s2 = (Suff *) Lst_Datum (ln);
465 if (s2->sNum >= s->sNum) {
466 break;
467 }
468 }
469
470 Lst_Close (l);
471 if (DEBUG(SUFF)) {
472 printf("inserting %s(%d)...", s->name, s->sNum);
473 }
474 if (ln == NILLNODE) {
475 if (DEBUG(SUFF)) {
476 printf("at end of list\n");
477 }
478 (void)Lst_AtEnd (l, (ClientData)s);
479 s->refCount++;
480 (void)Lst_AtEnd(s->ref, (ClientData) l);
481 } else if (s2->sNum != s->sNum) {
482 if (DEBUG(SUFF)) {
483 printf("before %s(%d)\n", s2->name, s2->sNum);
484 }
485 (void)Lst_Insert (l, ln, (ClientData)s);
486 s->refCount++;
487 (void)Lst_AtEnd(s->ref, (ClientData) l);
488 } else if (DEBUG(SUFF)) {
489 printf("already there\n");
490 }
491 }
492
493 /*-
494 *-----------------------------------------------------------------------
495 * Suff_ClearSuffixes --
496 * This is gross. Nuke the list of suffixes but keep all transformation
497 * rules around. The transformation graph is destroyed in this process,
498 * but we leave the list of rules so when a new graph is formed the rules
499 * will remain.
500 * This function is called from the parse module when a
501 * .SUFFIXES:\n line is encountered.
502 *
503 * Results:
504 * none
505 *
506 * Side Effects:
507 * the sufflist and its graph nodes are destroyed
508 *-----------------------------------------------------------------------
509 */
510 void
511 Suff_ClearSuffixes ()
512 {
513 #ifdef CLEANUP
514 Lst_Concat (suffClean, sufflist, LST_CONCLINK);
515 #endif
516 sufflist = Lst_Init(FALSE);
517 sNum = 0;
518 suffNull = emptySuff;
519 }
520
521 /*-
522 *-----------------------------------------------------------------------
523 * SuffParseTransform --
524 * Parse a transformation string to find its two component suffixes.
525 *
526 * Results:
527 * TRUE if the string is a valid transformation and FALSE otherwise.
528 *
529 * Side Effects:
530 * The passed pointers are overwritten.
531 *
532 *-----------------------------------------------------------------------
533 */
534 static Boolean
535 SuffParseTransform(str, srcPtr, targPtr)
536 char *str; /* String being parsed */
537 Suff **srcPtr; /* Place to store source of trans. */
538 Suff **targPtr; /* Place to store target of trans. */
539 {
540 register LstNode srcLn; /* element in suffix list of trans source*/
541 register Suff *src; /* Source of transformation */
542 register LstNode targLn; /* element in suffix list of trans target*/
543 register char *str2; /* Extra pointer (maybe target suffix) */
544 LstNode singleLn; /* element in suffix list of any suffix
545 * that exactly matches str */
546 Suff *single = NULL;/* Source of possible transformation to
547 * null suffix */
548
549 srcLn = NILLNODE;
550 singleLn = NILLNODE;
551
552 /*
553 * Loop looking first for a suffix that matches the start of the
554 * string and then for one that exactly matches the rest of it. If
555 * we can find two that meet these criteria, we've successfully
556 * parsed the string.
557 */
558 for (;;) {
559 if (srcLn == NILLNODE) {
560 srcLn = Lst_Find(sufflist, (ClientData)str, SuffSuffIsPrefix);
561 } else {
562 srcLn = Lst_FindFrom (sufflist, Lst_Succ(srcLn), (ClientData)str,
563 SuffSuffIsPrefix);
564 }
565 if (srcLn == NILLNODE) {
566 /*
567 * Ran out of source suffixes -- no such rule
568 */
569 if (singleLn != NILLNODE) {
570 /*
571 * Not so fast Mr. Smith! There was a suffix that encompassed
572 * the entire string, so we assume it was a transformation
573 * to the null suffix (thank you POSIX). We still prefer to
574 * find a double rule over a singleton, hence we leave this
575 * check until the end.
576 *
577 * XXX: Use emptySuff over suffNull?
578 */
579 *srcPtr = single;
580 *targPtr = suffNull;
581 return(TRUE);
582 }
583 return (FALSE);
584 }
585 src = (Suff *) Lst_Datum (srcLn);
586 str2 = str + src->nameLen;
587 if (*str2 == '\0') {
588 single = src;
589 singleLn = srcLn;
590 } else {
591 targLn = Lst_Find(sufflist, (ClientData)str2, SuffSuffHasNameP);
592 if (targLn != NILLNODE) {
593 *srcPtr = src;
594 *targPtr = (Suff *)Lst_Datum(targLn);
595 return (TRUE);
596 }
597 }
598 }
599 }
600
601 /*-
602 *-----------------------------------------------------------------------
603 * Suff_IsTransform --
604 * Return TRUE if the given string is a transformation rule
605 *
606 *
607 * Results:
608 * TRUE if the string is a concatenation of two known suffixes.
609 * FALSE otherwise
610 *
611 * Side Effects:
612 * None
613 *-----------------------------------------------------------------------
614 */
615 Boolean
616 Suff_IsTransform (str)
617 char *str; /* string to check */
618 {
619 Suff *src, *targ;
620
621 return (SuffParseTransform(str, &src, &targ));
622 }
623
624 /*-
625 *-----------------------------------------------------------------------
626 * Suff_AddTransform --
627 * Add the transformation rule described by the line to the
628 * list of rules and place the transformation itself in the graph
629 *
630 * Results:
631 * The node created for the transformation in the transforms list
632 *
633 * Side Effects:
634 * The node is placed on the end of the transforms Lst and links are
635 * made between the two suffixes mentioned in the target name
636 *-----------------------------------------------------------------------
637 */
638 GNode *
639 Suff_AddTransform (line)
640 char *line; /* name of transformation to add */
641 {
642 GNode *gn; /* GNode of transformation rule */
643 Suff *s, /* source suffix */
644 *t; /* target suffix */
645 LstNode ln; /* Node for existing transformation */
646
647 ln = Lst_Find (transforms, (ClientData)line, SuffGNHasNameP);
648 if (ln == NILLNODE) {
649 /*
650 * Make a new graph node for the transformation. It will be filled in
651 * by the Parse module.
652 */
653 gn = Targ_NewGN (line);
654 (void)Lst_AtEnd (transforms, (ClientData)gn);
655 } else {
656 /*
657 * New specification for transformation rule. Just nuke the old list
658 * of commands so they can be filled in again... We don't actually
659 * free the commands themselves, because a given command can be
660 * attached to several different transformations.
661 */
662 gn = (GNode *) Lst_Datum (ln);
663 Lst_Destroy (gn->commands, NOFREE);
664 Lst_Destroy (gn->children, NOFREE);
665 gn->commands = Lst_Init (FALSE);
666 gn->children = Lst_Init (FALSE);
667 }
668
669 gn->type = OP_TRANSFORM;
670
671 (void)SuffParseTransform(line, &s, &t);
672
673 /*
674 * link the two together in the proper relationship and order
675 */
676 if (DEBUG(SUFF)) {
677 printf("defining transformation from `%s' to `%s'\n",
678 s->name, t->name);
679 }
680 SuffInsert (t->children, s);
681 SuffInsert (s->parents, t);
682
683 return (gn);
684 }
685
686 /*-
687 *-----------------------------------------------------------------------
688 * Suff_EndTransform --
689 * Handle the finish of a transformation definition, removing the
690 * transformation from the graph if it has neither commands nor
691 * sources. This is a callback procedure for the Parse module via
692 * Lst_ForEach
693 *
694 * Results:
695 * === 0
696 *
697 * Side Effects:
698 * If the node has no commands or children, the children and parents
699 * lists of the affected suffices are altered.
700 *
701 *-----------------------------------------------------------------------
702 */
703 int
704 Suff_EndTransform(gnp, dummy)
705 ClientData gnp; /* Node for transformation */
706 ClientData dummy; /* Node for transformation */
707 {
708 GNode *gn = (GNode *) gnp;
709
710 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
711 gn = (GNode *) Lst_Datum (Lst_Last (gn->cohorts));
712 if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
713 Lst_IsEmpty(gn->children))
714 {
715 Suff *s, *t;
716
717 /*
718 * SuffParseTransform() may fail for special rules which are not
719 * actual transformation rules. (e.g. .DEFAULT)
720 */
721 if (SuffParseTransform(gn->name, &s, &t)) {
722 Lst p;
723
724 if (DEBUG(SUFF)) {
725 printf("deleting transformation from `%s' to `%s'\n",
726 s->name, t->name);
727 }
728
729 /*
730 * Store s->parents because s could be deleted in SuffRemove
731 */
732 p = s->parents;
733
734 /*
735 * Remove the source from the target's children list. We check for a
736 * nil return to handle a beanhead saying something like
737 * .c.o .c.o:
738 *
739 * We'll be called twice when the next target is seen, but .c and .o
740 * are only linked once...
741 */
742 SuffRemove(t->children, s);
743
744 /*
745 * Remove the target from the source's parents list
746 */
747 SuffRemove(p, t);
748 }
749 } else if ((gn->type & OP_TRANSFORM) && DEBUG(SUFF)) {
750 printf("transformation %s complete\n", gn->name);
751 }
752
753 return(dummy ? 0 : 0);
754 }
755
756 /*-
757 *-----------------------------------------------------------------------
758 * SuffRebuildGraph --
759 * Called from Suff_AddSuffix via Lst_ForEach to search through the
760 * list of existing transformation rules and rebuild the transformation
761 * graph when it has been destroyed by Suff_ClearSuffixes. If the
762 * given rule is a transformation involving this suffix and another,
763 * existing suffix, the proper relationship is established between
764 * the two.
765 *
766 * Results:
767 * Always 0.
768 *
769 * Side Effects:
770 * The appropriate links will be made between this suffix and
771 * others if transformation rules exist for it.
772 *
773 *-----------------------------------------------------------------------
774 */
775 static int
776 SuffRebuildGraph(transformp, sp)
777 ClientData transformp; /* Transformation to test */
778 ClientData sp; /* Suffix to rebuild */
779 {
780 GNode *transform = (GNode *) transformp;
781 Suff *s = (Suff *) sp;
782 char *cp;
783 LstNode ln;
784 Suff *s2;
785 SuffixCmpData sd;
786
787 /*
788 * First see if it is a transformation from this suffix.
789 */
790 cp = SuffStrIsPrefix(s->name, transform->name);
791 if (cp != (char *)NULL) {
792 ln = Lst_Find(sufflist, (ClientData)cp, SuffSuffHasNameP);
793 if (ln != NILLNODE) {
794 /*
795 * Found target. Link in and return, since it can't be anything
796 * else.
797 */
798 s2 = (Suff *)Lst_Datum(ln);
799 SuffInsert(s2->children, s);
800 SuffInsert(s->parents, s2);
801 return(0);
802 }
803 }
804
805 /*
806 * Not from, maybe to?
807 */
808 sd.len = strlen(transform->name);
809 sd.ename = transform->name + sd.len;
810 cp = SuffSuffIsSuffix(s, &sd);
811 if (cp != (char *)NULL) {
812 /*
813 * Null-terminate the source suffix in order to find it.
814 */
815 cp[1] = '\0';
816 ln = Lst_Find(sufflist, (ClientData)transform->name, SuffSuffHasNameP);
817 /*
818 * Replace the start of the target suffix
819 */
820 cp[1] = s->name[0];
821 if (ln != NILLNODE) {
822 /*
823 * Found it -- establish the proper relationship
824 */
825 s2 = (Suff *)Lst_Datum(ln);
826 SuffInsert(s->children, s2);
827 SuffInsert(s2->parents, s);
828 }
829 }
830 return(0);
831 }
832
833 /*-
834 *-----------------------------------------------------------------------
835 * SuffScanTargets --
836 * Called from Suff_AddSuffix via Lst_ForEach to search through the
837 * list of existing targets and find if any of the existing targets
838 * can be turned into a transformation rule.
839 *
840 * Results:
841 * 1 if a new main target has been selected, 0 otherwise.
842 *
843 * Side Effects:
844 * If such a target is found and the target is the current main
845 * target, the main target is set to NULL and the next target
846 * examined (if that exists) becomes the main target.
847 *
848 *-----------------------------------------------------------------------
849 */
850 static int
851 SuffScanTargets(targetp, gsp)
852 ClientData targetp;
853 ClientData gsp;
854 {
855 GNode *target = (GNode *) targetp;
856 GNodeSuff *gs = (GNodeSuff *) gsp;
857 Suff *s, *t;
858 char *ptr;
859
860 if (*gs->gn == NILGNODE && gs->r && (target->type & OP_NOTARGET) == 0) {
861 *gs->gn = target;
862 Targ_SetMain(target);
863 return 1;
864 }
865
866 if (target->type == OP_TRANSFORM)
867 return 0;
868
869 if ((ptr = strstr(target->name, gs->s->name)) == NULL ||
870 ptr == target->name)
871 return 0;
872
873 if (SuffParseTransform(target->name, &s, &t)) {
874 if (*gs->gn == target) {
875 gs->r = TRUE;
876 *gs->gn = NILGNODE;
877 Targ_SetMain(NILGNODE);
878 }
879 Lst_Destroy (target->children, NOFREE);
880 target->children = Lst_Init (FALSE);
881 target->type = OP_TRANSFORM;
882 /*
883 * link the two together in the proper relationship and order
884 */
885 if (DEBUG(SUFF)) {
886 printf("defining transformation from `%s' to `%s'\n",
887 s->name, t->name);
888 }
889 SuffInsert (t->children, s);
890 SuffInsert (s->parents, t);
891 }
892 return 0;
893 }
894
895 /*-
896 *-----------------------------------------------------------------------
897 * Suff_AddSuffix --
898 * Add the suffix in string to the end of the list of known suffixes.
899 * Should we restructure the suffix graph? Make doesn't...
900 *
901 * Results:
902 * None
903 *
904 * Side Effects:
905 * A GNode is created for the suffix and a Suff structure is created and
906 * added to the suffixes list unless the suffix was already known.
907 * The mainNode passed can be modified if a target mutated into a
908 * transform and that target happened to be the main target.
909 *-----------------------------------------------------------------------
910 */
911 void
912 Suff_AddSuffix (str, gn)
913 char *str; /* the name of the suffix to add */
914 GNode **gn;
915 {
916 Suff *s; /* new suffix descriptor */
917 LstNode ln;
918 GNodeSuff gs;
919
920 ln = Lst_Find (sufflist, (ClientData)str, SuffSuffHasNameP);
921 if (ln == NILLNODE) {
922 s = (Suff *) emalloc (sizeof (Suff));
923
924 s->name = estrdup (str);
925 s->nameLen = strlen (s->name);
926 s->searchPath = Lst_Init (FALSE);
927 s->children = Lst_Init (FALSE);
928 s->parents = Lst_Init (FALSE);
929 s->ref = Lst_Init (FALSE);
930 s->sNum = sNum++;
931 s->flags = 0;
932 s->refCount = 1;
933
934 (void)Lst_AtEnd (sufflist, (ClientData)s);
935 /*
936 * We also look at our existing targets list to see if adding
937 * this suffix will make one of our current targets mutate into
938 * a suffix rule. This is ugly, but other makes treat all targets
939 * that start with a . as suffix rules.
940 */
941 gs.gn = gn;
942 gs.s = s;
943 gs.r = FALSE;
944 Lst_ForEach (Targ_List(), SuffScanTargets, (ClientData) &gs);
945 /*
946 * Look for any existing transformations from or to this suffix.
947 * XXX: Only do this after a Suff_ClearSuffixes?
948 */
949 Lst_ForEach (transforms, SuffRebuildGraph, (ClientData) s);
950 }
951 }
952
953 /*-
954 *-----------------------------------------------------------------------
955 * Suff_GetPath --
956 * Return the search path for the given suffix, if it's defined.
957 *
958 * Results:
959 * The searchPath for the desired suffix or NILLST if the suffix isn't
960 * defined.
961 *
962 * Side Effects:
963 * None
964 *-----------------------------------------------------------------------
965 */
966 Lst
967 Suff_GetPath (sname)
968 char *sname;
969 {
970 LstNode ln;
971 Suff *s;
972
973 ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
974 if (ln == NILLNODE) {
975 return (NILLST);
976 } else {
977 s = (Suff *) Lst_Datum (ln);
978 return (s->searchPath);
979 }
980 }
981
982 /*-
983 *-----------------------------------------------------------------------
984 * Suff_DoPaths --
985 * Extend the search paths for all suffixes to include the default
986 * search path.
987 *
988 * Results:
989 * None.
990 *
991 * Side Effects:
992 * The searchPath field of all the suffixes is extended by the
993 * directories in dirSearchPath. If paths were specified for the
994 * ".h" suffix, the directories are stuffed into a global variable
995 * called ".INCLUDES" with each directory preceded by a -I. The same
996 * is done for the ".a" suffix, except the variable is called
997 * ".LIBS" and the flag is -L.
998 *-----------------------------------------------------------------------
999 */
1000 void
1001 Suff_DoPaths()
1002 {
1003 register Suff *s;
1004 register LstNode ln;
1005 char *ptr;
1006 Lst inIncludes; /* Cumulative .INCLUDES path */
1007 Lst inLibs; /* Cumulative .LIBS path */
1008
1009 if (Lst_Open (sufflist) == FAILURE) {
1010 return;
1011 }
1012
1013 inIncludes = Lst_Init(FALSE);
1014 inLibs = Lst_Init(FALSE);
1015
1016 while ((ln = Lst_Next (sufflist)) != NILLNODE) {
1017 s = (Suff *) Lst_Datum (ln);
1018 if (!Lst_IsEmpty (s->searchPath)) {
1019 #ifdef INCLUDES
1020 if (s->flags & SUFF_INCLUDE) {
1021 Dir_Concat(inIncludes, s->searchPath);
1022 }
1023 #endif /* INCLUDES */
1024 #ifdef LIBRARIES
1025 if (s->flags & SUFF_LIBRARY) {
1026 Dir_Concat(inLibs, s->searchPath);
1027 }
1028 #endif /* LIBRARIES */
1029 Dir_Concat(s->searchPath, dirSearchPath);
1030 } else {
1031 Lst_Destroy (s->searchPath, Dir_Destroy);
1032 s->searchPath = Lst_Duplicate(dirSearchPath, Dir_CopyDir);
1033 }
1034 }
1035
1036 Var_Set(".INCLUDES", ptr = Dir_MakeFlags("-I", inIncludes), VAR_GLOBAL, 0);
1037 free(ptr);
1038 Var_Set(".LIBS", ptr = Dir_MakeFlags("-L", inLibs), VAR_GLOBAL, 0);
1039 free(ptr);
1040
1041 Lst_Destroy(inIncludes, Dir_Destroy);
1042 Lst_Destroy(inLibs, Dir_Destroy);
1043
1044 Lst_Close (sufflist);
1045 }
1046
1047 /*-
1048 *-----------------------------------------------------------------------
1049 * Suff_AddInclude --
1050 * Add the given suffix as a type of file which gets included.
1051 * Called from the parse module when a .INCLUDES line is parsed.
1052 * The suffix must have already been defined.
1053 *
1054 * Results:
1055 * None.
1056 *
1057 * Side Effects:
1058 * The SUFF_INCLUDE bit is set in the suffix's flags field
1059 *
1060 *-----------------------------------------------------------------------
1061 */
1062 void
1063 Suff_AddInclude (sname)
1064 char *sname; /* Name of suffix to mark */
1065 {
1066 LstNode ln;
1067 Suff *s;
1068
1069 ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
1070 if (ln != NILLNODE) {
1071 s = (Suff *) Lst_Datum (ln);
1072 s->flags |= SUFF_INCLUDE;
1073 }
1074 }
1075
1076 /*-
1077 *-----------------------------------------------------------------------
1078 * Suff_AddLib --
1079 * Add the given suffix as a type of file which is a library.
1080 * Called from the parse module when parsing a .LIBS line. The
1081 * suffix must have been defined via .SUFFIXES before this is
1082 * called.
1083 *
1084 * Results:
1085 * None.
1086 *
1087 * Side Effects:
1088 * The SUFF_LIBRARY bit is set in the suffix's flags field
1089 *
1090 *-----------------------------------------------------------------------
1091 */
1092 void
1093 Suff_AddLib (sname)
1094 char *sname; /* Name of suffix to mark */
1095 {
1096 LstNode ln;
1097 Suff *s;
1098
1099 ln = Lst_Find (sufflist, (ClientData)sname, SuffSuffHasNameP);
1100 if (ln != NILLNODE) {
1101 s = (Suff *) Lst_Datum (ln);
1102 s->flags |= SUFF_LIBRARY;
1103 }
1104 }
1105
1106 /********** Implicit Source Search Functions *********/
1107
1108 /*-
1109 *-----------------------------------------------------------------------
1110 * SuffAddSrc --
1111 * Add a suffix as a Src structure to the given list with its parent
1112 * being the given Src structure. If the suffix is the null suffix,
1113 * the prefix is used unaltered as the file name in the Src structure.
1114 *
1115 * Results:
1116 * always returns 0
1117 *
1118 * Side Effects:
1119 * A Src structure is created and tacked onto the end of the list
1120 *-----------------------------------------------------------------------
1121 */
1122 static int
1123 SuffAddSrc (sp, lsp)
1124 ClientData sp; /* suffix for which to create a Src structure */
1125 ClientData lsp; /* list and parent for the new Src */
1126 {
1127 Suff *s = (Suff *) sp;
1128 LstSrc *ls = (LstSrc *) lsp;
1129 Src *s2; /* new Src structure */
1130 Src *targ; /* Target structure */
1131
1132 targ = ls->s;
1133
1134 if ((s->flags & SUFF_NULL) && (*s->name != '\0')) {
1135 /*
1136 * If the suffix has been marked as the NULL suffix, also create a Src
1137 * structure for a file with no suffix attached. Two birds, and all
1138 * that...
1139 */
1140 s2 = (Src *) emalloc (sizeof (Src));
1141 s2->file = estrdup(targ->pref);
1142 s2->pref = targ->pref;
1143 s2->parent = targ;
1144 s2->node = NILGNODE;
1145 s2->suff = s;
1146 s->refCount++;
1147 s2->children = 0;
1148 targ->children += 1;
1149 (void)Lst_AtEnd (ls->l, (ClientData)s2);
1150 #ifdef DEBUG_SRC
1151 s2->cp = Lst_Init(FALSE);
1152 Lst_AtEnd(targ->cp, (ClientData) s2);
1153 printf("1 add %x %x to %x:", targ, s2, ls->l);
1154 Lst_ForEach(ls->l, PrintAddr, (ClientData) 0);
1155 printf("\n");
1156 #endif
1157 }
1158 s2 = (Src *) emalloc (sizeof (Src));
1159 s2->file = str_concat (targ->pref, s->name, 0);
1160 s2->pref = targ->pref;
1161 s2->parent = targ;
1162 s2->node = NILGNODE;
1163 s2->suff = s;
1164 s->refCount++;
1165 s2->children = 0;
1166 targ->children += 1;
1167 (void)Lst_AtEnd (ls->l, (ClientData)s2);
1168 #ifdef DEBUG_SRC
1169 s2->cp = Lst_Init(FALSE);
1170 Lst_AtEnd(targ->cp, (ClientData) s2);
1171 printf("2 add %x %x to %x:", targ, s2, ls->l);
1172 Lst_ForEach(ls->l, PrintAddr, (ClientData) 0);
1173 printf("\n");
1174 #endif
1175
1176 return(0);
1177 }
1178
1179 /*-
1180 *-----------------------------------------------------------------------
1181 * SuffAddLevel --
1182 * Add all the children of targ as Src structures to the given list
1183 *
1184 * Results:
1185 * None
1186 *
1187 * Side Effects:
1188 * Lots of structures are created and added to the list
1189 *-----------------------------------------------------------------------
1190 */
1191 static void
1192 SuffAddLevel (l, targ)
1193 Lst l; /* list to which to add the new level */
1194 Src *targ; /* Src structure to use as the parent */
1195 {
1196 LstSrc ls;
1197
1198 ls.s = targ;
1199 ls.l = l;
1200
1201 Lst_ForEach (targ->suff->children, SuffAddSrc, (ClientData)&ls);
1202 }
1203
1204 /*-
1205 *----------------------------------------------------------------------
1206 * SuffRemoveSrc --
1207 * Free all src structures in list that don't have a reference count
1208 *
1209 * Results:
1210 * Ture if an src was removed
1211 *
1212 * Side Effects:
1213 * The memory is free'd.
1214 *----------------------------------------------------------------------
1215 */
1216 static int
1217 SuffRemoveSrc (l)
1218 Lst l;
1219 {
1220 LstNode ln;
1221 Src *s;
1222 int t = 0;
1223
1224 if (Lst_Open (l) == FAILURE) {
1225 return 0;
1226 }
1227 #ifdef DEBUG_SRC
1228 printf("cleaning %lx: ", (unsigned long) l);
1229 Lst_ForEach(l, PrintAddr, (ClientData) 0);
1230 printf("\n");
1231 #endif
1232
1233
1234 while ((ln = Lst_Next (l)) != NILLNODE) {
1235 s = (Src *) Lst_Datum (ln);
1236 if (s->children == 0) {
1237 free ((Address)s->file);
1238 if (!s->parent)
1239 free((Address)s->pref);
1240 else {
1241 #ifdef DEBUG_SRC
1242 LstNode ln = Lst_Member(s->parent->cp, (ClientData)s);
1243 if (ln != NILLNODE)
1244 Lst_Remove(s->parent->cp, ln);
1245 #endif
1246 --s->parent->children;
1247 }
1248 #ifdef DEBUG_SRC
1249 printf("free: [l=%x] p=%x %d\n", l, s, s->children);
1250 Lst_Destroy(s->cp, NOFREE);
1251 #endif
1252 Lst_Remove(l, ln);
1253 free ((Address)s);
1254 t |= 1;
1255 Lst_Close(l);
1256 return TRUE;
1257 }
1258 #ifdef DEBUG_SRC
1259 else {
1260 printf("keep: [l=%x] p=%x %d: ", l, s, s->children);
1261 Lst_ForEach(s->cp, PrintAddr, (ClientData) 0);
1262 printf("\n");
1263 }
1264 #endif
1265 }
1266
1267 Lst_Close(l);
1268
1269 return t;
1270 }
1271
1272 /*-
1273 *-----------------------------------------------------------------------
1274 * SuffFindThem --
1275 * Find the first existing file/target in the list srcs
1276 *
1277 * Results:
1278 * The lowest structure in the chain of transformations
1279 *
1280 * Side Effects:
1281 * None
1282 *-----------------------------------------------------------------------
1283 */
1284 static Src *
1285 SuffFindThem (srcs, slst)
1286 Lst srcs; /* list of Src structures to search through */
1287 Lst slst;
1288 {
1289 Src *s; /* current Src */
1290 Src *rs; /* returned Src */
1291 char *ptr;
1292
1293 rs = (Src *) NULL;
1294
1295 while (!Lst_IsEmpty (srcs)) {
1296 s = (Src *) Lst_DeQueue (srcs);
1297
1298 if (DEBUG(SUFF)) {
1299 printf ("\ttrying %s...", s->file);
1300 }
1301
1302 /*
1303 * A file is considered to exist if either a node exists in the
1304 * graph for it or the file actually exists.
1305 */
1306 if (Targ_FindNode(s->file, TARG_NOCREATE) != NILGNODE) {
1307 #ifdef DEBUG_SRC
1308 printf("remove %x from %x\n", s, srcs);
1309 #endif
1310 rs = s;
1311 break;
1312 }
1313
1314 if ((ptr = Dir_FindFile (s->file, s->suff->searchPath)) != NULL) {
1315 rs = s;
1316 #ifdef DEBUG_SRC
1317 printf("remove %x from %x\n", s, srcs);
1318 #endif
1319 free(ptr);
1320 break;
1321 }
1322
1323 if (DEBUG(SUFF)) {
1324 printf ("not there\n");
1325 }
1326
1327 SuffAddLevel (srcs, s);
1328 Lst_AtEnd(slst, (ClientData) s);
1329 }
1330
1331 if (DEBUG(SUFF) && rs) {
1332 printf ("got it\n");
1333 }
1334 return (rs);
1335 }
1336
1337 /*-
1338 *-----------------------------------------------------------------------
1339 * SuffFindCmds --
1340 * See if any of the children of the target in the Src structure is
1341 * one from which the target can be transformed. If there is one,
1342 * a Src structure is put together for it and returned.
1343 *
1344 * Results:
1345 * The Src structure of the "winning" child, or NIL if no such beast.
1346 *
1347 * Side Effects:
1348 * A Src structure may be allocated.
1349 *
1350 *-----------------------------------------------------------------------
1351 */
1352 static Src *
1353 SuffFindCmds (targ, slst)
1354 Src *targ; /* Src structure to play with */
1355 Lst slst;
1356 {
1357 LstNode ln; /* General-purpose list node */
1358 register GNode *t, /* Target GNode */
1359 *s; /* Source GNode */
1360 int prefLen;/* The length of the defined prefix */
1361 Suff *suff; /* Suffix on matching beastie */
1362 Src *ret; /* Return value */
1363 char *cp;
1364
1365 t = targ->node;
1366 (void) Lst_Open (t->children);
1367 prefLen = strlen (targ->pref);
1368
1369 while ((ln = Lst_Next (t->children)) != NILLNODE) {
1370 s = (GNode *)Lst_Datum (ln);
1371
1372 cp = strrchr (s->name, '/');
1373 if (cp == (char *)NULL) {
1374 cp = s->name;
1375 } else {
1376 cp++;
1377 }
1378 if (strncmp (cp, targ->pref, prefLen) == 0) {
1379 /*
1380 * The node matches the prefix ok, see if it has a known
1381 * suffix.
1382 */
1383 ln = Lst_Find (sufflist, (ClientData)&cp[prefLen],
1384 SuffSuffHasNameP);
1385 if (ln != NILLNODE) {
1386 /*
1387 * It even has a known suffix, see if there's a transformation
1388 * defined between the node's suffix and the target's suffix.
1389 *
1390 * XXX: Handle multi-stage transformations here, too.
1391 */
1392 suff = (Suff *)Lst_Datum (ln);
1393
1394 if (Lst_Member (suff->parents,
1395 (ClientData)targ->suff) != NILLNODE)
1396 {
1397 /*
1398 * Hot Damn! Create a new Src structure to describe
1399 * this transformation (making sure to duplicate the
1400 * source node's name so Suff_FindDeps can free it
1401 * again (ick)), and return the new structure.
1402 */
1403 ret = (Src *)emalloc (sizeof (Src));
1404 ret->file = estrdup(s->name);
1405 ret->pref = targ->pref;
1406 ret->suff = suff;
1407 suff->refCount++;
1408 ret->parent = targ;
1409 ret->node = s;
1410 ret->children = 0;
1411 targ->children += 1;
1412 #ifdef DEBUG_SRC
1413 ret->cp = Lst_Init(FALSE);
1414 printf("3 add %x %x\n", targ, ret);
1415 Lst_AtEnd(targ->cp, (ClientData) ret);
1416 #endif
1417 Lst_AtEnd(slst, (ClientData) ret);
1418 if (DEBUG(SUFF)) {
1419 printf ("\tusing existing source %s\n", s->name);
1420 }
1421 return (ret);
1422 }
1423 }
1424 }
1425 }
1426 Lst_Close (t->children);
1427 return ((Src *)NULL);
1428 }
1429
1430 /*-
1431 *-----------------------------------------------------------------------
1432 * SuffExpandChildren --
1433 * Expand the names of any children of a given node that contain
1434 * variable invocations or file wildcards into actual targets.
1435 *
1436 * Results:
1437 * === 0 (continue)
1438 *
1439 * Side Effects:
1440 * The expanded node is removed from the parent's list of children,
1441 * and the parent's unmade counter is decremented, but other nodes
1442 * may be added.
1443 *
1444 *-----------------------------------------------------------------------
1445 */
1446 static int
1447 SuffExpandChildren(prevLN, pgn)
1448 LstNode prevLN; /* Child to examine */
1449 GNode *pgn; /* Parent node being processed */
1450 {
1451 GNode *cgn = (GNode *) Lst_Datum(prevLN);
1452 GNode *gn; /* New source 8) */
1453 LstNode ln; /* List element for old source */
1454 char *cp; /* Expanded value */
1455
1456 /*
1457 * First do variable expansion -- this takes precedence over
1458 * wildcard expansion. If the result contains wildcards, they'll be gotten
1459 * to later since the resulting words are tacked on to the end of
1460 * the children list.
1461 */
1462 if (strchr(cgn->name, '$') != (char *)NULL) {
1463 if (DEBUG(SUFF)) {
1464 printf("Expanding \"%s\"...", cgn->name);
1465 }
1466 cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
1467
1468 if (cp != (char *)NULL) {
1469 Lst members = Lst_Init(FALSE);
1470
1471 if (cgn->type & OP_ARCHV) {
1472 /*
1473 * Node was an archive(member) target, so we want to call
1474 * on the Arch module to find the nodes for us, expanding
1475 * variables in the parent's context.
1476 */
1477 char *sacrifice = cp;
1478
1479 (void)Arch_ParseArchive(&sacrifice, members, pgn);
1480 } else {
1481 /*
1482 * Break the result into a vector of strings whose nodes
1483 * we can find, then add those nodes to the members list.
1484 * Unfortunately, we can't use brk_string b/c it
1485 * doesn't understand about variable specifications with
1486 * spaces in them...
1487 */
1488 char *start;
1489 char *initcp = cp; /* For freeing... */
1490
1491 for (start = cp; *start == ' ' || *start == '\t'; start++)
1492 continue;
1493 for (cp = start; *cp != '\0'; cp++) {
1494 if (*cp == ' ' || *cp == '\t') {
1495 /*
1496 * White-space -- terminate element, find the node,
1497 * add it, skip any further spaces.
1498 */
1499 *cp++ = '\0';
1500 gn = Targ_FindNode(start, TARG_CREATE);
1501 (void)Lst_AtEnd(members, (ClientData)gn);
1502 while (*cp == ' ' || *cp == '\t') {
1503 cp++;
1504 }
1505 /*
1506 * Adjust cp for increment at start of loop, but
1507 * set start to first non-space.
1508 */
1509 start = cp--;
1510 } else if (*cp == '$') {
1511 /*
1512 * Start of a variable spec -- contact variable module
1513 * to find the end so we can skip over it.
1514 */
1515 char *junk;
1516 int len;
1517 Boolean doFree;
1518
1519 junk = Var_Parse(cp, pgn, TRUE, &len, &doFree);
1520 if (junk != var_Error) {
1521 cp += len - 1;
1522 }
1523
1524 if (doFree) {
1525 free(junk);
1526 }
1527 } else if (*cp == '\\' && *cp != '\0') {
1528 /*
1529 * Escaped something -- skip over it
1530 */
1531 cp++;
1532 }
1533 }
1534
1535 if (cp != start) {
1536 /*
1537 * Stuff left over -- add it to the list too
1538 */
1539 gn = Targ_FindNode(start, TARG_CREATE);
1540 (void)Lst_AtEnd(members, (ClientData)gn);
1541 }
1542 /*
1543 * Point cp back at the beginning again so the variable value
1544 * can be freed.
1545 */
1546 cp = initcp;
1547 }
1548 /*
1549 * Add all elements of the members list to the parent node.
1550 */
1551 while(!Lst_IsEmpty(members)) {
1552 gn = (GNode *)Lst_DeQueue(members);
1553
1554 if (DEBUG(SUFF)) {
1555 printf("%s...", gn->name);
1556 }
1557 if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
1558 (void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
1559 prevLN = Lst_Succ(prevLN);
1560 (void)Lst_AtEnd(gn->parents, (ClientData)pgn);
1561 pgn->unmade++;
1562 }
1563 }
1564 Lst_Destroy(members, NOFREE);
1565 /*
1566 * Free the result
1567 */
1568 free((char *)cp);
1569 }
1570 /*
1571 * Now the source is expanded, remove it from the list of children to
1572 * keep it from being processed.
1573 */
1574 if (DEBUG(SUFF)) {
1575 printf("\n");
1576 }
1577 return(1);
1578 } else if (Dir_HasWildcards(cgn->name)) {
1579 Lst exp; /* List of expansions */
1580 Lst path; /* Search path along which to expand */
1581 SuffixCmpData sd; /* Search string data */
1582
1583 /*
1584 * Find a path along which to expand the word.
1585 *
1586 * If the word has a known suffix, use that path.
1587 * If it has no known suffix and we're allowed to use the null
1588 * suffix, use its path.
1589 * Else use the default system search path.
1590 */
1591 sd.len = strlen(cgn->name);
1592 sd.ename = cgn->name + sd.len;
1593 ln = Lst_Find(sufflist, (ClientData)&sd, SuffSuffIsSuffixP);
1594
1595 if (DEBUG(SUFF)) {
1596 printf("Wildcard expanding \"%s\"...", cgn->name);
1597 }
1598
1599 if (ln != NILLNODE) {
1600 Suff *s = (Suff *)Lst_Datum(ln);
1601
1602 if (DEBUG(SUFF)) {
1603 printf("suffix is \"%s\"...", s->name);
1604 }
1605 path = s->searchPath;
1606 } else {
1607 /*
1608 * Use default search path
1609 */
1610 path = dirSearchPath;
1611 }
1612
1613 /*
1614 * Expand the word along the chosen path
1615 */
1616 exp = Lst_Init(FALSE);
1617 Dir_Expand(cgn->name, path, exp);
1618
1619 while (!Lst_IsEmpty(exp)) {
1620 /*
1621 * Fetch next expansion off the list and find its GNode
1622 */
1623 cp = (char *)Lst_DeQueue(exp);
1624
1625 if (DEBUG(SUFF)) {
1626 printf("%s...", cp);
1627 }
1628 gn = Targ_FindNode(cp, TARG_CREATE);
1629
1630 /*
1631 * If gn isn't already a child of the parent, make it so and
1632 * up the parent's count of unmade children.
1633 */
1634 if (Lst_Member(pgn->children, (ClientData)gn) == NILLNODE) {
1635 (void)Lst_Append(pgn->children, prevLN, (ClientData)gn);
1636 prevLN = Lst_Succ(prevLN);
1637 (void)Lst_AtEnd(gn->parents, (ClientData)pgn);
1638 pgn->unmade++;
1639 }
1640 }
1641
1642 /*
1643 * Nuke what's left of the list
1644 */
1645 Lst_Destroy(exp, NOFREE);
1646
1647 /*
1648 * Now the source is expanded, remove it from the list of children to
1649 * keep it from being processed.
1650 */
1651 if (DEBUG(SUFF)) {
1652 printf("\n");
1653 }
1654 return(1);
1655 }
1656
1657 return(0);
1658 }
1659
1660 /*-
1661 *-----------------------------------------------------------------------
1662 * SuffApplyTransform --
1663 * Apply a transformation rule, given the source and target nodes
1664 * and suffixes.
1665 *
1666 * Results:
1667 * TRUE if successful, FALSE if not.
1668 *
1669 * Side Effects:
1670 * The source and target are linked and the commands from the
1671 * transformation are added to the target node's commands list.
1672 * All attributes but OP_DEPMASK and OP_TRANSFORM are applied
1673 * to the target. The target also inherits all the sources for
1674 * the transformation rule.
1675 *
1676 *-----------------------------------------------------------------------
1677 */
1678 static Boolean
1679 SuffApplyTransform(tGn, sGn, t, s)
1680 GNode *tGn; /* Target node */
1681 GNode *sGn; /* Source node */
1682 Suff *t; /* Target suffix */
1683 Suff *s; /* Source suffix */
1684 {
1685 LstNode ln, nln; /* General node */
1686 char *tname; /* Name of transformation rule */
1687 GNode *gn; /* Node for same */
1688
1689 /*
1690 * Form the proper links between the target and source.
1691 */
1692 (void)Lst_AtEnd(tGn->children, (ClientData)sGn);
1693 (void)Lst_AtEnd(sGn->parents, (ClientData)tGn);
1694 tGn->unmade += 1;
1695
1696 /*
1697 * Locate the transformation rule itself
1698 */
1699 tname = str_concat(s->name, t->name, 0);
1700 ln = Lst_Find(transforms, (ClientData)tname, SuffGNHasNameP);
1701 free(tname);
1702
1703 if (ln == NILLNODE) {
1704 /*
1705 * Not really such a transformation rule (can happen when we're
1706 * called to link an OP_MEMBER and OP_ARCHV node), so return
1707 * FALSE.
1708 */
1709 return(FALSE);
1710 }
1711
1712 gn = (GNode *)Lst_Datum(ln);
1713
1714 if (DEBUG(SUFF)) {
1715 printf("\tapplying %s -> %s to \"%s\"\n", s->name, t->name, tGn->name);
1716 }
1717
1718 /*
1719 * Record last child for expansion purposes
1720 */
1721 ln = Lst_Last(tGn->children);
1722
1723 /*
1724 * Pass the buck to Make_HandleUse to apply the rule
1725 */
1726 (void)Make_HandleUse(gn, tGn);
1727
1728 /*
1729 * Deal with wildcards and variables in any acquired sources
1730 */
1731 ln = Lst_Succ(ln);
1732 while (ln != NILLNODE) {
1733 if (SuffExpandChildren(ln, tGn)) {
1734 nln = Lst_Succ(ln);
1735 tGn->unmade--;
1736 Lst_Remove(tGn->children, ln);
1737 ln = nln;
1738 } else
1739 ln = Lst_Succ(ln);
1740 }
1741
1742 /*
1743 * Keep track of another parent to which this beast is transformed so
1744 * the .IMPSRC variable can be set correctly for the parent.
1745 */
1746 (void)Lst_AtEnd(sGn->iParents, (ClientData)tGn);
1747
1748 return(TRUE);
1749 }
1750
1751
1752 /*-
1753 *-----------------------------------------------------------------------
1754 * SuffFindArchiveDeps --
1755 * Locate dependencies for an OP_ARCHV node.
1756 *
1757 * Results:
1758 * None
1759 *
1760 * Side Effects:
1761 * Same as Suff_FindDeps
1762 *
1763 *-----------------------------------------------------------------------
1764 */
1765 static void
1766 SuffFindArchiveDeps(gn, slst)
1767 GNode *gn; /* Node for which to locate dependencies */
1768 Lst slst;
1769 {
1770 char *eoarch; /* End of archive portion */
1771 char *eoname; /* End of member portion */
1772 GNode *mem; /* Node for member */
1773 static char *copy[] = { /* Variables to be copied from the member node */
1774 TARGET, /* Must be first */
1775 PREFIX, /* Must be second */
1776 };
1777 int i; /* Index into copy and vals */
1778 Suff *ms; /* Suffix descriptor for member */
1779 char *name; /* Start of member's name */
1780
1781 /*
1782 * The node is an archive(member) pair. so we must find a
1783 * suffix for both of them.
1784 */
1785 eoarch = strchr (gn->name, '(');
1786 eoname = strchr (eoarch, ')');
1787
1788 *eoname = '\0'; /* Nuke parentheses during suffix search */
1789 *eoarch = '\0'; /* So a suffix can be found */
1790
1791 name = eoarch + 1;
1792
1793 /*
1794 * To simplify things, call Suff_FindDeps recursively on the member now,
1795 * so we can simply compare the member's .PREFIX and .TARGET variables
1796 * to locate its suffix. This allows us to figure out the suffix to
1797 * use for the archive without having to do a quadratic search over the
1798 * suffix list, backtracking for each one...
1799 */
1800 mem = Targ_FindNode(name, TARG_CREATE);
1801 SuffFindDeps(mem, slst);
1802
1803 /*
1804 * Create the link between the two nodes right off
1805 */
1806 (void)Lst_AtEnd(gn->children, (ClientData)mem);
1807 (void)Lst_AtEnd(mem->parents, (ClientData)gn);
1808 gn->unmade += 1;
1809
1810 /*
1811 * Copy in the variables from the member node to this one.
1812 */
1813 for (i = (sizeof(copy)/sizeof(copy[0]))-1; i >= 0; i--) {
1814 char *p1;
1815 Var_Set(copy[i], Var_Value(copy[i], mem, &p1), gn, 0);
1816 if (p1)
1817 free(p1);
1818
1819 }
1820
1821 ms = mem->suffix;
1822 if (ms == NULL) {
1823 /*
1824 * Didn't know what it was -- use .NULL suffix if not in make mode
1825 */
1826 if (DEBUG(SUFF)) {
1827 printf("using null suffix\n");
1828 }
1829 ms = suffNull;
1830 }
1831
1832
1833 /*
1834 * Set the other two local variables required for this target.
1835 */
1836 Var_Set (MEMBER, name, gn, 0);
1837 Var_Set (ARCHIVE, gn->name, gn, 0);
1838
1839 if (ms != NULL) {
1840 /*
1841 * Member has a known suffix, so look for a transformation rule from
1842 * it to a possible suffix of the archive. Rather than searching
1843 * through the entire list, we just look at suffixes to which the
1844 * member's suffix may be transformed...
1845 */
1846 LstNode ln;
1847 SuffixCmpData sd; /* Search string data */
1848
1849 /*
1850 * Use first matching suffix...
1851 */
1852 sd.len = eoarch - gn->name;
1853 sd.ename = eoarch;
1854 ln = Lst_Find(ms->parents, &sd, SuffSuffIsSuffixP);
1855
1856 if (ln != NILLNODE) {
1857 /*
1858 * Got one -- apply it
1859 */
1860 if (!SuffApplyTransform(gn, mem, (Suff *)Lst_Datum(ln), ms) &&
1861 DEBUG(SUFF))
1862 {
1863 printf("\tNo transformation from %s -> %s\n",
1864 ms->name, ((Suff *)Lst_Datum(ln))->name);
1865 }
1866 }
1867 }
1868
1869 /*
1870 * Replace the opening and closing parens now we've no need of the separate
1871 * pieces.
1872 */
1873 *eoarch = '('; *eoname = ')';
1874
1875 /*
1876 * Pretend gn appeared to the left of a dependency operator so
1877 * the user needn't provide a transformation from the member to the
1878 * archive.
1879 */
1880 if (OP_NOP(gn->type)) {
1881 gn->type |= OP_DEPENDS;
1882 }
1883
1884 /*
1885 * Flag the member as such so we remember to look in the archive for
1886 * its modification time.
1887 */
1888 mem->type |= OP_MEMBER;
1889 }
1890
1891 /*-
1892 *-----------------------------------------------------------------------
1893 * SuffFindNormalDeps --
1894 * Locate implicit dependencies for regular targets.
1895 *
1896 * Results:
1897 * None.
1898 *
1899 * Side Effects:
1900 * Same as Suff_FindDeps...
1901 *
1902 *-----------------------------------------------------------------------
1903 */
1904 static void
1905 SuffFindNormalDeps(gn, slst)
1906 GNode *gn; /* Node for which to find sources */
1907 Lst slst;
1908 {
1909 char *eoname; /* End of name */
1910 char *sopref; /* Start of prefix */
1911 LstNode ln, nln; /* Next suffix node to check */
1912 Lst srcs; /* List of sources at which to look */
1913 Lst targs; /* List of targets to which things can be
1914 * transformed. They all have the same file,
1915 * but different suff and pref fields */
1916 Src *bottom; /* Start of found transformation path */
1917 Src *src; /* General Src pointer */
1918 char *pref; /* Prefix to use */
1919 Src *targ; /* General Src target pointer */
1920 SuffixCmpData sd; /* Search string data */
1921
1922
1923 sd.len = strlen(gn->name);
1924 sd.ename = eoname = gn->name + sd.len;
1925
1926 sopref = gn->name;
1927
1928 /*
1929 * Begin at the beginning...
1930 */
1931 ln = Lst_First(sufflist);
1932 srcs = Lst_Init(FALSE);
1933 targs = Lst_Init(FALSE);
1934
1935 /*
1936 * We're caught in a catch-22 here. On the one hand, we want to use any
1937 * transformation implied by the target's sources, but we can't examine
1938 * the sources until we've expanded any variables/wildcards they may hold,
1939 * and we can't do that until we've set up the target's local variables
1940 * and we can't do that until we know what the proper suffix for the
1941 * target is (in case there are two suffixes one of which is a suffix of
1942 * the other) and we can't know that until we've found its implied
1943 * source, which we may not want to use if there's an existing source
1944 * that implies a different transformation.
1945 *
1946 * In an attempt to get around this, which may not work all the time,
1947 * but should work most of the time, we look for implied sources first,
1948 * checking transformations to all possible suffixes of the target,
1949 * use what we find to set the target's local variables, expand the
1950 * children, then look for any overriding transformations they imply.
1951 * Should we find one, we discard the one we found before.
1952 */
1953
1954 while (ln != NILLNODE) {
1955 /*
1956 * Look for next possible suffix...
1957 */
1958 ln = Lst_FindFrom(sufflist, ln, &sd, SuffSuffIsSuffixP);
1959
1960 if (ln != NILLNODE) {
1961 int prefLen; /* Length of the prefix */
1962
1963 /*
1964 * Allocate a Src structure to which things can be transformed
1965 */
1966 targ = (Src *)emalloc(sizeof (Src));
1967 targ->file = estrdup(gn->name);
1968 targ->suff = (Suff *)Lst_Datum(ln);
1969 targ->suff->refCount++;
1970 targ->node = gn;
1971 targ->parent = (Src *)NULL;
1972 targ->children = 0;
1973 #ifdef DEBUG_SRC
1974 targ->cp = Lst_Init(FALSE);
1975 #endif
1976
1977 /*
1978 * Allocate room for the prefix, whose end is found by subtracting
1979 * the length of the suffix from the end of the name.
1980 */
1981 prefLen = (eoname - targ->suff->nameLen) - sopref;
1982 targ->pref = emalloc(prefLen + 1);
1983 memcpy(targ->pref, sopref, prefLen);
1984 targ->pref[prefLen] = '\0';
1985
1986 /*
1987 * Add nodes from which the target can be made
1988 */
1989 SuffAddLevel(srcs, targ);
1990
1991 /*
1992 * Record the target so we can nuke it
1993 */
1994 (void)Lst_AtEnd(targs, (ClientData)targ);
1995
1996 /*
1997 * Search from this suffix's successor...
1998 */
1999 ln = Lst_Succ(ln);
2000 }
2001 }
2002
2003 /*
2004 * Handle target of unknown suffix...
2005 */
2006 if (Lst_IsEmpty(targs) && suffNull != NULL) {
2007 if (DEBUG(SUFF)) {
2008 printf("\tNo known suffix on %s. Using .NULL suffix\n", gn->name);
2009 }
2010
2011 targ = (Src *)emalloc(sizeof (Src));
2012 targ->file = estrdup(gn->name);
2013 targ->suff = suffNull;
2014 targ->suff->refCount++;
2015 targ->node = gn;
2016 targ->parent = (Src *)NULL;
2017 targ->children = 0;
2018 targ->pref = estrdup(sopref);
2019 #ifdef DEBUG_SRC
2020 targ->cp = Lst_Init(FALSE);
2021 #endif
2022
2023 /*
2024 * Only use the default suffix rules if we don't have commands
2025 * defined for this gnode; traditional make programs used to
2026 * not define suffix rules if the gnode had children but we
2027 * don't do this anymore.
2028 */
2029 if (Lst_IsEmpty(gn->commands))
2030 SuffAddLevel(srcs, targ);
2031 else {
2032 if (DEBUG(SUFF))
2033 printf("not ");
2034 }
2035
2036 if (DEBUG(SUFF))
2037 printf("adding suffix rules\n");
2038
2039 (void)Lst_AtEnd(targs, (ClientData)targ);
2040 }
2041
2042 /*
2043 * Using the list of possible sources built up from the target suffix(es),
2044 * try and find an existing file/target that matches.
2045 */
2046 bottom = SuffFindThem(srcs, slst);
2047
2048 if (bottom == (Src *)NULL) {
2049 /*
2050 * No known transformations -- use the first suffix found for setting
2051 * the local variables.
2052 */
2053 if (!Lst_IsEmpty(targs)) {
2054 targ = (Src *)Lst_Datum(Lst_First(targs));
2055 } else {
2056 targ = (Src *)NULL;
2057 }
2058 } else {
2059 /*
2060 * Work up the transformation path to find the suffix of the
2061 * target to which the transformation was made.
2062 */
2063 for (targ = bottom; targ->parent != NULL; targ = targ->parent)
2064 continue;
2065 }
2066
2067 Var_Set(TARGET, gn->path ? gn->path : gn->name, gn, 0);
2068
2069 pref = (targ != NULL) ? targ->pref : gn->name;
2070 Var_Set(PREFIX, pref, gn, 0);
2071
2072 /*
2073 * Now we've got the important local variables set, expand any sources
2074 * that still contain variables or wildcards in their names.
2075 */
2076 ln = Lst_First(gn->children);
2077 while (ln != NILLNODE) {
2078 if (SuffExpandChildren(ln, gn)) {
2079 nln = Lst_Succ(ln);
2080 gn->unmade--;
2081 Lst_Remove(gn->children, ln);
2082 ln = nln;
2083 } else
2084 ln = Lst_Succ(ln);
2085 }
2086
2087 if (targ == NULL) {
2088 if (DEBUG(SUFF)) {
2089 printf("\tNo valid suffix on %s\n", gn->name);
2090 }
2091
2092 sfnd_abort:
2093 /*
2094 * Deal with finding the thing on the default search path. We
2095 * always do that, not only if the node is only a source (not
2096 * on the lhs of a dependency operator or [XXX] it has neither
2097 * children or commands) as the old pmake did.
2098 */
2099 if ((gn->type & (OP_PHONY|OP_NOPATH)) == 0) {
2100 free(gn->path);
2101 gn->path = Dir_FindFile(gn->name,
2102 (targ == NULL ? dirSearchPath :
2103 targ->suff->searchPath));
2104 if (gn->path != NULL) {
2105 char *ptr;
2106 Var_Set(TARGET, gn->path, gn, 0);
2107
2108 if (targ != NULL) {
2109 /*
2110 * Suffix known for the thing -- trim the suffix off
2111 * the path to form the proper .PREFIX variable.
2112 */
2113 int savep = strlen(gn->path) - targ->suff->nameLen;
2114 char savec;
2115
2116 if (gn->suffix)
2117 gn->suffix->refCount--;
2118 gn->suffix = targ->suff;
2119 gn->suffix->refCount++;
2120
2121 savec = gn->path[savep];
2122 gn->path[savep] = '\0';
2123
2124 if ((ptr = strrchr(gn->path, '/')) != NULL)
2125 ptr++;
2126 else
2127 ptr = gn->path;
2128
2129 Var_Set(PREFIX, ptr, gn, 0);
2130
2131 gn->path[savep] = savec;
2132 } else {
2133 /*
2134 * The .PREFIX gets the full path if the target has
2135 * no known suffix.
2136 */
2137 if (gn->suffix)
2138 gn->suffix->refCount--;
2139 gn->suffix = NULL;
2140
2141 if ((ptr = strrchr(gn->path, '/')) != NULL)
2142 ptr++;
2143 else
2144 ptr = gn->path;
2145
2146 Var_Set(PREFIX, ptr, gn, 0);
2147 }
2148 }
2149 }
2150
2151 goto sfnd_return;
2152 }
2153
2154 /*
2155 * If the suffix indicates that the target is a library, mark that in
2156 * the node's type field.
2157 */
2158 if (targ->suff->flags & SUFF_LIBRARY) {
2159 gn->type |= OP_LIB;
2160 }
2161
2162 /*
2163 * Check for overriding transformation rule implied by sources
2164 */
2165 if (!Lst_IsEmpty(gn->children)) {
2166 src = SuffFindCmds(targ, slst);
2167
2168 if (src != (Src *)NULL) {
2169 /*
2170 * Free up all the Src structures in the transformation path
2171 * up to, but not including, the parent node.
2172 */
2173 while (bottom && bottom->parent != NULL) {
2174 if (Lst_Member(slst, (ClientData) bottom) == NILLNODE) {
2175 Lst_AtEnd(slst, (ClientData) bottom);
2176 }
2177 bottom = bottom->parent;
2178 }
2179 bottom = src;
2180 }
2181 }
2182
2183 if (bottom == NULL) {
2184 /*
2185 * No idea from where it can come -- return now.
2186 */
2187 goto sfnd_abort;
2188 }
2189
2190 /*
2191 * We now have a list of Src structures headed by 'bottom' and linked via
2192 * their 'parent' pointers. What we do next is create links between
2193 * source and target nodes (which may or may not have been created)
2194 * and set the necessary local variables in each target. The
2195 * commands for each target are set from the commands of the
2196 * transformation rule used to get from the src suffix to the targ
2197 * suffix. Note that this causes the commands list of the original
2198 * node, gn, to be replaced by the commands of the final
2199 * transformation rule. Also, the unmade field of gn is incremented.
2200 * Etc.
2201 */
2202 if (bottom->node == NILGNODE) {
2203 bottom->node = Targ_FindNode(bottom->file, TARG_CREATE);
2204 }
2205
2206 for (src = bottom; src->parent != (Src *)NULL; src = src->parent) {
2207 targ = src->parent;
2208
2209 if (src->node->suffix)
2210 src->node->suffix->refCount--;
2211 src->node->suffix = src->suff;
2212 src->node->suffix->refCount++;
2213
2214 if (targ->node == NILGNODE) {
2215 targ->node = Targ_FindNode(targ->file, TARG_CREATE);
2216 }
2217
2218 SuffApplyTransform(targ->node, src->node,
2219 targ->suff, src->suff);
2220
2221 if (targ->node != gn) {
2222 /*
2223 * Finish off the dependency-search process for any nodes
2224 * between bottom and gn (no point in questing around the
2225 * filesystem for their implicit source when it's already
2226 * known). Note that the node can't have any sources that
2227 * need expanding, since SuffFindThem will stop on an existing
2228 * node, so all we need to do is set the standard and System V
2229 * variables.
2230 */
2231 targ->node->type |= OP_DEPS_FOUND;
2232
2233 Var_Set(PREFIX, targ->pref, targ->node, 0);
2234
2235 Var_Set(TARGET, targ->node->name, targ->node, 0);
2236 }
2237 }
2238
2239 if (gn->suffix)
2240 gn->suffix->refCount--;
2241 gn->suffix = src->suff;
2242 gn->suffix->refCount++;
2243
2244 /*
2245 * Nuke the transformation path and the Src structures left over in the
2246 * two lists.
2247 */
2248 sfnd_return:
2249 if (bottom)
2250 if (Lst_Member(slst, (ClientData) bottom) == NILLNODE)
2251 Lst_AtEnd(slst, (ClientData) bottom);
2252
2253 while (SuffRemoveSrc(srcs) || SuffRemoveSrc(targs))
2254 continue;
2255
2256 Lst_Concat(slst, srcs, LST_CONCLINK);
2257 Lst_Concat(slst, targs, LST_CONCLINK);
2258 }
2259
2260
2261 /*-
2262 *-----------------------------------------------------------------------
2263 * Suff_FindDeps --
2264 * Find implicit sources for the target described by the graph node
2265 * gn
2266 *
2267 * Results:
2268 * Nothing.
2269 *
2270 * Side Effects:
2271 * Nodes are added to the graph below the passed-in node. The nodes
2272 * are marked to have their IMPSRC variable filled in. The
2273 * PREFIX variable is set for the given node and all its
2274 * implied children.
2275 *
2276 * Notes:
2277 * The path found by this target is the shortest path in the
2278 * transformation graph, which may pass through non-existent targets,
2279 * to an existing target. The search continues on all paths from the
2280 * root suffix until a file is found. I.e. if there's a path
2281 * .o -> .c -> .l -> .l,v from the root and the .l,v file exists but
2282 * the .c and .l files don't, the search will branch out in
2283 * all directions from .o and again from all the nodes on the
2284 * next level until the .l,v node is encountered.
2285 *
2286 *-----------------------------------------------------------------------
2287 */
2288
2289 void
2290 Suff_FindDeps(gn)
2291 GNode *gn;
2292 {
2293
2294 SuffFindDeps(gn, srclist);
2295 while (SuffRemoveSrc(srclist))
2296 continue;
2297 }
2298
2299
2300 static void
2301 SuffFindDeps (gn, slst)
2302 GNode *gn; /* node we're dealing with */
2303 Lst slst;
2304 {
2305 if (gn->type & (OP_DEPS_FOUND|OP_PHONY)) {
2306 /*
2307 * If dependencies already found, no need to do it again...
2308 * If this is a .PHONY target, we do not apply suffix rules.
2309 */
2310 return;
2311 } else {
2312 gn->type |= OP_DEPS_FOUND;
2313 }
2314
2315 if (DEBUG(SUFF)) {
2316 printf ("SuffFindDeps (%s)\n", gn->name);
2317 }
2318
2319 if (gn->type & OP_ARCHV) {
2320 SuffFindArchiveDeps(gn, slst);
2321 } else if (gn->type & OP_LIB) {
2322 /*
2323 * If the node is a library, it is the arch module's job to find it
2324 * and set the TARGET variable accordingly. We merely provide the
2325 * search path, assuming all libraries end in ".a" (if the suffix
2326 * hasn't been defined, there's nothing we can do for it, so we just
2327 * set the TARGET variable to the node's name in order to give it a
2328 * value).
2329 */
2330 LstNode ln;
2331 Suff *s;
2332
2333 ln = Lst_Find (sufflist, (ClientData)LIBSUFF, SuffSuffHasNameP);
2334 if (gn->suffix)
2335 gn->suffix->refCount--;
2336 if (ln != NILLNODE) {
2337 gn->suffix = s = (Suff *) Lst_Datum (ln);
2338 gn->suffix->refCount++;
2339 Arch_FindLib (gn, s->searchPath);
2340 } else {
2341 gn->suffix = NULL;
2342 Var_Set (TARGET, gn->name, gn, 0);
2343 }
2344 /*
2345 * Because a library (-lfoo) target doesn't follow the standard
2346 * filesystem conventions, we don't set the regular variables for
2347 * the thing. .PREFIX is simply made empty...
2348 */
2349 Var_Set(PREFIX, "", gn, 0);
2350 } else {
2351 SuffFindNormalDeps(gn, slst);
2352 }
2353 }
2354
2355 /*-
2356 *-----------------------------------------------------------------------
2357 * Suff_SetNull --
2358 * Define which suffix is the null suffix.
2359 *
2360 * Results:
2361 * None.
2362 *
2363 * Side Effects:
2364 * 'suffNull' is altered.
2365 *
2366 * Notes:
2367 * Need to handle the changing of the null suffix gracefully so the
2368 * old transformation rules don't just go away.
2369 *
2370 *-----------------------------------------------------------------------
2371 */
2372 void
2373 Suff_SetNull(name)
2374 char *name; /* Name of null suffix */
2375 {
2376 Suff *s;
2377 LstNode ln;
2378
2379 ln = Lst_Find(sufflist, (ClientData)name, SuffSuffHasNameP);
2380 if (ln != NILLNODE) {
2381 s = (Suff *)Lst_Datum(ln);
2382 if (suffNull != (Suff *)NULL) {
2383 suffNull->flags &= ~SUFF_NULL;
2384 }
2385 s->flags |= SUFF_NULL;
2386 /*
2387 * XXX: Here's where the transformation mangling would take place
2388 */
2389 suffNull = s;
2390 } else {
2391 Parse_Error (PARSE_WARNING, "Desired null suffix %s not defined.",
2392 name);
2393 }
2394 }
2395
2396 /*-
2397 *-----------------------------------------------------------------------
2398 * Suff_Init --
2399 * Initialize suffixes module
2400 *
2401 * Results:
2402 * None
2403 *
2404 * Side Effects:
2405 * Many
2406 *-----------------------------------------------------------------------
2407 */
2408 void
2409 Suff_Init ()
2410 {
2411 sufflist = Lst_Init (FALSE);
2412 #ifdef CLEANUP
2413 suffClean = Lst_Init(FALSE);
2414 #endif
2415 srclist = Lst_Init (FALSE);
2416 transforms = Lst_Init (FALSE);
2417
2418 sNum = 0;
2419 /*
2420 * Create null suffix for single-suffix rules (POSIX). The thing doesn't
2421 * actually go on the suffix list or everyone will think that's its
2422 * suffix.
2423 */
2424 emptySuff = suffNull = (Suff *) emalloc (sizeof (Suff));
2425
2426 suffNull->name = estrdup ("");
2427 suffNull->nameLen = 0;
2428 suffNull->searchPath = Lst_Init (FALSE);
2429 Dir_Concat(suffNull->searchPath, dirSearchPath);
2430 suffNull->children = Lst_Init (FALSE);
2431 suffNull->parents = Lst_Init (FALSE);
2432 suffNull->ref = Lst_Init (FALSE);
2433 suffNull->sNum = sNum++;
2434 suffNull->flags = SUFF_NULL;
2435 suffNull->refCount = 1;
2436
2437 }
2438
2439
2440 /*-
2441 *----------------------------------------------------------------------
2442 * Suff_End --
2443 * Cleanup the this module
2444 *
2445 * Results:
2446 * None
2447 *
2448 * Side Effects:
2449 * The memory is free'd.
2450 *----------------------------------------------------------------------
2451 */
2452
2453 void
2454 Suff_End()
2455 {
2456 #ifdef CLEANUP
2457 Lst_Destroy(sufflist, SuffFree);
2458 Lst_Destroy(suffClean, SuffFree);
2459 if (suffNull)
2460 SuffFree(suffNull);
2461 Lst_Destroy(srclist, NOFREE);
2462 Lst_Destroy(transforms, NOFREE);
2463 #endif
2464 }
2465
2466
2467 /********************* DEBUGGING FUNCTIONS **********************/
2468
2469 static int SuffPrintName(s, dummy)
2470 ClientData s;
2471 ClientData dummy;
2472 {
2473 printf ("%s ", ((Suff *) s)->name);
2474 return (dummy ? 0 : 0);
2475 }
2476
2477 static int
2478 SuffPrintSuff (sp, dummy)
2479 ClientData sp;
2480 ClientData dummy;
2481 {
2482 Suff *s = (Suff *) sp;
2483 int flags;
2484 int flag;
2485
2486 printf ("# `%s' [%d] ", s->name, s->refCount);
2487
2488 flags = s->flags;
2489 if (flags) {
2490 fputs (" (", stdout);
2491 while (flags) {
2492 flag = 1 << (ffs(flags) - 1);
2493 flags &= ~flag;
2494 switch (flag) {
2495 case SUFF_NULL:
2496 printf ("NULL");
2497 break;
2498 case SUFF_INCLUDE:
2499 printf ("INCLUDE");
2500 break;
2501 case SUFF_LIBRARY:
2502 printf ("LIBRARY");
2503 break;
2504 }
2505 fputc(flags ? '|' : ')', stdout);
2506 }
2507 }
2508 fputc ('\n', stdout);
2509 printf ("#\tTo: ");
2510 Lst_ForEach (s->parents, SuffPrintName, (ClientData)0);
2511 fputc ('\n', stdout);
2512 printf ("#\tFrom: ");
2513 Lst_ForEach (s->children, SuffPrintName, (ClientData)0);
2514 fputc ('\n', stdout);
2515 printf ("#\tSearch Path: ");
2516 Dir_PrintPath (s->searchPath);
2517 fputc ('\n', stdout);
2518 return (dummy ? 0 : 0);
2519 }
2520
2521 static int
2522 SuffPrintTrans (tp, dummy)
2523 ClientData tp;
2524 ClientData dummy;
2525 {
2526 GNode *t = (GNode *) tp;
2527
2528 printf ("%-16s: ", t->name);
2529 Targ_PrintType (t->type);
2530 fputc ('\n', stdout);
2531 Lst_ForEach (t->commands, Targ_PrintCmd, (ClientData)0);
2532 fputc ('\n', stdout);
2533 return(dummy ? 0 : 0);
2534 }
2535
2536 void
2537 Suff_PrintAll()
2538 {
2539 printf ("#*** Suffixes:\n");
2540 Lst_ForEach (sufflist, SuffPrintSuff, (ClientData)0);
2541
2542 printf ("#*** Transformations:\n");
2543 Lst_ForEach (transforms, SuffPrintTrans, (ClientData)0);
2544 }
2545