-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuff.c
2644 lines (2394 loc) · 69.6 KB
/
suff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: suff.c,v 1.67 2009/01/23 21:58:28 dsl Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: suff.c,v 1.67 2009/01/23 21:58:28 dsl Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)suff.c 8.4 (Berkeley) 3/21/94";
#else
__RCSID("$NetBSD: suff.c,v 1.67 2009/01/23 21:58:28 dsl Exp $");
#endif
#endif /* not lint */
#endif
/*-
* suff.c --
* Functions to maintain suffix lists and find implicit dependents
* using suffix transformation rules
*
* Interface:
* Suff_Init Initialize all things to do with suffixes.
*
* Suff_End Cleanup the module
*
* Suff_DoPaths This function is used to make life easier
* when searching for a file according to its
* suffix. It takes the global search path,
* as defined using the .PATH: target, and appends
* its directories to the path of each of the
* defined suffixes, as specified using
* .PATH<suffix>: targets. In addition, all
* directories given for suffixes labeled as
* include files or libraries, using the .INCLUDES
* or .LIBS targets, are played with using
* Dir_MakeFlags to create the .INCLUDES and
* .LIBS global variables.
*
* Suff_ClearSuffixes Clear out all the suffixes and defined
* transformations.
*
* Suff_IsTransform Return TRUE if the passed string is the lhs
* of a transformation rule.
*
* Suff_AddSuffix Add the passed string as another known suffix.
*
* Suff_GetPath Return the search path for the given suffix.
*
* Suff_AddInclude Mark the given suffix as denoting an include
* file.
*
* Suff_AddLib Mark the given suffix as denoting a library.
*
* Suff_AddTransform Add another transformation to the suffix
* graph. Returns GNode suitable for framing, I
* mean, tacking commands, attributes, etc. on.
*
* Suff_SetNull Define the suffix to consider the suffix of
* any file that doesn't have a known one.
*
* Suff_FindDeps Find implicit sources for and the location of
* a target based on its suffix. Returns the
* bottom-most node added to the graph or NULL
* if the target had no implicit sources.
*
* Suff_FindPath Return the appropriate path to search in
* order to find the node.
*/
#include <stdio.h>
#include "make.h"
#include "hash.h"
#include "dir.h"
static Lst sufflist; /* Lst of suffixes */
#ifdef CLEANUP
static Lst suffClean; /* Lst of suffixes to be cleaned */
#endif
static Lst srclist; /* Lst of sources */
static Lst transforms; /* Lst of transformation rules */
static int sNum = 0; /* Counter for assigning suffix numbers */
/*
* Structure describing an individual suffix.
*/
typedef struct _Suff {
char *name; /* The suffix itself */
int nameLen; /* Length of the suffix */
short flags; /* Type of suffix */
#define SUFF_INCLUDE 0x01 /* One which is #include'd */
#define SUFF_LIBRARY 0x02 /* One which contains a library */
#define SUFF_NULL 0x04 /* The empty suffix */
Lst searchPath; /* The path along which files of this suffix
* may be found */
int sNum; /* The suffix number */
int refCount; /* Reference count of list membership */
Lst parents; /* Suffixes we have a transformation to */
Lst children; /* Suffixes we have a transformation from */
Lst ref; /* List of lists this suffix is referenced */
} Suff;
/*
* for SuffSuffIsSuffix
*/
typedef struct {
char *ename; /* The end of the name */
int len; /* Length of the name */
} SuffixCmpData;
/*
* Structure used in the search for implied sources.
*/
typedef struct _Src {
char *file; /* The file to look for */
char *pref; /* Prefix from which file was formed */
Suff *suff; /* The suffix on the file */
struct _Src *parent; /* The Src for which this is a source */
GNode *node; /* The node describing the file */
int children; /* Count of existing children (so we don't free
* this thing too early or never nuke it) */
#ifdef DEBUG_SRC
Lst cp; /* Debug; children list */
#endif
} Src;
/*
* A structure for passing more than one argument to the Lst-library-invoked
* function...
*/
typedef struct {
Lst l;
Src *s;
} LstSrc;
typedef struct {
GNode **gn;
Suff *s;
Boolean r;
} GNodeSuff;
static Suff *suffNull; /* The NULL suffix for this run */
static Suff *emptySuff; /* The empty suffix required for POSIX
* single-suffix transformation rules */
static const char *SuffStrIsPrefix(const char *, const char *);
static char *SuffSuffIsSuffix(const Suff *, const SuffixCmpData *);
static int SuffSuffIsSuffixP(const void *, const void *);
static int SuffSuffHasNameP(const void *, const void *);
static int SuffSuffIsPrefix(const void *, const void *);
static int SuffGNHasNameP(const void *, const void *);
static void SuffUnRef(void *, void *);
static void SuffFree(void *);
static void SuffInsert(Lst, Suff *);
static void SuffRemove(Lst, Suff *);
static Boolean SuffParseTransform(char *, Suff **, Suff **);
static int SuffRebuildGraph(void *, void *);
static int SuffScanTargets(void *, void *);
static int SuffAddSrc(void *, void *);
static int SuffRemoveSrc(Lst);
static void SuffAddLevel(Lst, Src *);
static Src *SuffFindThem(Lst, Lst);
static Src *SuffFindCmds(Src *, Lst);
static void SuffExpandChildren(LstNode, GNode *);
static void SuffExpandWildcards(LstNode, GNode *);
static Boolean SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
static void SuffFindDeps(GNode *, Lst);
static void SuffFindArchiveDeps(GNode *, Lst);
static void SuffFindNormalDeps(GNode *, Lst);
static int SuffPrintName(void *, void *);
static int SuffPrintSuff(void *, void *);
static int SuffPrintTrans(void *, void *);
/*************** Lst Predicates ****************/
/*-
*-----------------------------------------------------------------------
* SuffStrIsPrefix --
* See if pref is a prefix of str.
*
* Input:
* pref possible prefix
* str string to check
*
* Results:
* NULL if it ain't, pointer to character in str after prefix if so
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
static const char *
SuffStrIsPrefix(const char *pref, const char *str)
{
while (*str && *pref == *str) {
pref++;
str++;
}
return (*pref ? NULL : str);
}
/*-
*-----------------------------------------------------------------------
* SuffSuffIsSuffix --
* See if suff is a suffix of str. sd->ename should point to THE END
* of the string to check. (THE END == the null byte)
*
* Input:
* s possible suffix
* sd string to examine
*
* Results:
* NULL if it ain't, pointer to character in str before suffix if
* it is.
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
static char *
SuffSuffIsSuffix(const Suff *s, const SuffixCmpData *sd)
{
char *p1; /* Pointer into suffix name */
char *p2; /* Pointer into string being examined */
if (sd->len < s->nameLen)
return NULL; /* this string is shorter than the suffix */
p1 = s->name + s->nameLen;
p2 = sd->ename;
while (p1 >= s->name && *p1 == *p2) {
p1--;
p2--;
}
return (p1 == s->name - 1 ? p2 : NULL);
}
/*-
*-----------------------------------------------------------------------
* SuffSuffIsSuffixP --
* Predicate form of SuffSuffIsSuffix. Passed as the callback function
* to Lst_Find.
*
* Results:
* 0 if the suffix is the one desired, non-zero if not.
*
* Side Effects:
* None.
*
*-----------------------------------------------------------------------
*/
static int
SuffSuffIsSuffixP(const void *s, const void *sd)
{
return(!SuffSuffIsSuffix(s, sd));
}
/*-
*-----------------------------------------------------------------------
* SuffSuffHasNameP --
* Callback procedure for finding a suffix based on its name. Used by
* Suff_GetPath.
*
* Input:
* s Suffix to check
* sd Desired name
*
* Results:
* 0 if the suffix is of the given name. non-zero otherwise.
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
static int
SuffSuffHasNameP(const void *s, const void *sname)
{
return (strcmp(sname, ((const Suff *)s)->name));
}
/*-
*-----------------------------------------------------------------------
* SuffSuffIsPrefix --
* See if the suffix described by s is a prefix of the string. Care
* must be taken when using this to search for transformations and
* what-not, since there could well be two suffixes, one of which
* is a prefix of the other...
*
* Input:
* s suffix to compare
* str string to examine
*
* Results:
* 0 if s is a prefix of str. non-zero otherwise
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
static int
SuffSuffIsPrefix(const void *s, const void *str)
{
return SuffStrIsPrefix(((const Suff *)s)->name, str) == NULL;
}
/*-
*-----------------------------------------------------------------------
* SuffGNHasNameP --
* See if the graph node has the desired name
*
* Input:
* gn current node we're looking at
* name name we're looking for
*
* Results:
* 0 if it does. non-zero if it doesn't
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
static int
SuffGNHasNameP(const void *gn, const void *name)
{
return (strcmp(name, ((const GNode *)gn)->name));
}
/*********** Maintenance Functions ************/
static void
SuffUnRef(void *lp, void *sp)
{
Lst l = (Lst) lp;
LstNode ln = Lst_Member(l, sp);
if (ln != NULL) {
Lst_Remove(l, ln);
((Suff *)sp)->refCount--;
}
}
/*-
*-----------------------------------------------------------------------
* SuffFree --
* Free up all memory associated with the given suffix structure.
*
* Results:
* none
*
* Side Effects:
* the suffix entry is detroyed
*-----------------------------------------------------------------------
*/
static void
SuffFree(void *sp)
{
Suff *s = (Suff *)sp;
if (s == suffNull)
suffNull = NULL;
if (s == emptySuff)
emptySuff = NULL;
#ifdef notdef
/* We don't delete suffixes in order, so we cannot use this */
if (s->refCount)
Punt("Internal error deleting suffix `%s' with refcount = %d", s->name,
s->refCount);
#endif
Lst_Destroy(s->ref, NULL);
Lst_Destroy(s->children, NULL);
Lst_Destroy(s->parents, NULL);
Lst_Destroy(s->searchPath, Dir_Destroy);
free(s->name);
free(s);
}
/*-
*-----------------------------------------------------------------------
* SuffRemove --
* Remove the suffix into the list
*
* Results:
* None
*
* Side Effects:
* The reference count for the suffix is decremented and the
* suffix is possibly freed
*-----------------------------------------------------------------------
*/
static void
SuffRemove(Lst l, Suff *s)
{
SuffUnRef(l, s);
if (s->refCount == 0) {
SuffUnRef(sufflist, s);
SuffFree(s);
}
}
/*-
*-----------------------------------------------------------------------
* SuffInsert --
* Insert the suffix into the list keeping the list ordered by suffix
* numbers.
*
* Input:
* l the list where in s should be inserted
* s the suffix to insert
*
* Results:
* None
*
* Side Effects:
* The reference count of the suffix is incremented
*-----------------------------------------------------------------------
*/
static void
SuffInsert(Lst l, Suff *s)
{
LstNode ln; /* current element in l we're examining */
Suff *s2 = NULL; /* the suffix descriptor in this element */
if (Lst_Open(l) == FAILURE) {
return;
}
while ((ln = Lst_Next(l)) != NULL) {
s2 = (Suff *)Lst_Datum(ln);
if (s2->sNum >= s->sNum) {
break;
}
}
Lst_Close(l);
if (DEBUG(SUFF)) {
fprintf(debug_file, "inserting %s(%d)...", s->name, s->sNum);
}
if (ln == NULL) {
if (DEBUG(SUFF)) {
fprintf(debug_file, "at end of list\n");
}
(void)Lst_AtEnd(l, s);
s->refCount++;
(void)Lst_AtEnd(s->ref, l);
} else if (s2->sNum != s->sNum) {
if (DEBUG(SUFF)) {
fprintf(debug_file, "before %s(%d)\n", s2->name, s2->sNum);
}
(void)Lst_InsertBefore(l, ln, s);
s->refCount++;
(void)Lst_AtEnd(s->ref, l);
} else if (DEBUG(SUFF)) {
fprintf(debug_file, "already there\n");
}
}
/*-
*-----------------------------------------------------------------------
* Suff_ClearSuffixes --
* This is gross. Nuke the list of suffixes but keep all transformation
* rules around. The transformation graph is destroyed in this process,
* but we leave the list of rules so when a new graph is formed the rules
* will remain.
* This function is called from the parse module when a
* .SUFFIXES:\n line is encountered.
*
* Results:
* none
*
* Side Effects:
* the sufflist and its graph nodes are destroyed
*-----------------------------------------------------------------------
*/
void
Suff_ClearSuffixes(void)
{
#ifdef CLEANUP
Lst_Concat(suffClean, sufflist, LST_CONCLINK);
#endif
sufflist = Lst_Init(FALSE);
sNum = 0;
suffNull = emptySuff;
}
/*-
*-----------------------------------------------------------------------
* SuffParseTransform --
* Parse a transformation string to find its two component suffixes.
*
* Input:
* str String being parsed
* srcPtr Place to store source of trans.
* targPtr Place to store target of trans.
*
* Results:
* TRUE if the string is a valid transformation and FALSE otherwise.
*
* Side Effects:
* The passed pointers are overwritten.
*
*-----------------------------------------------------------------------
*/
static Boolean
SuffParseTransform(char *str, Suff **srcPtr, Suff **targPtr)
{
LstNode srcLn; /* element in suffix list of trans source*/
Suff *src; /* Source of transformation */
LstNode targLn; /* element in suffix list of trans target*/
char *str2; /* Extra pointer (maybe target suffix) */
LstNode singleLn; /* element in suffix list of any suffix
* that exactly matches str */
Suff *single = NULL;/* Source of possible transformation to
* null suffix */
srcLn = NULL;
singleLn = NULL;
/*
* Loop looking first for a suffix that matches the start of the
* string and then for one that exactly matches the rest of it. If
* we can find two that meet these criteria, we've successfully
* parsed the string.
*/
for (;;) {
if (srcLn == NULL) {
srcLn = Lst_Find(sufflist, str, SuffSuffIsPrefix);
} else {
srcLn = Lst_FindFrom(sufflist, Lst_Succ(srcLn), str,
SuffSuffIsPrefix);
}
if (srcLn == NULL) {
/*
* Ran out of source suffixes -- no such rule
*/
if (singleLn != NULL) {
/*
* Not so fast Mr. Smith! There was a suffix that encompassed
* the entire string, so we assume it was a transformation
* to the null suffix (thank you POSIX). We still prefer to
* find a double rule over a singleton, hence we leave this
* check until the end.
*
* XXX: Use emptySuff over suffNull?
*/
*srcPtr = single;
*targPtr = suffNull;
return(TRUE);
}
return (FALSE);
}
src = (Suff *)Lst_Datum(srcLn);
str2 = str + src->nameLen;
if (*str2 == '\0') {
single = src;
singleLn = srcLn;
} else {
targLn = Lst_Find(sufflist, str2, SuffSuffHasNameP);
if (targLn != NULL) {
*srcPtr = src;
*targPtr = (Suff *)Lst_Datum(targLn);
return (TRUE);
}
}
}
}
/*-
*-----------------------------------------------------------------------
* Suff_IsTransform --
* Return TRUE if the given string is a transformation rule
*
*
* Input:
* str string to check
*
* Results:
* TRUE if the string is a concatenation of two known suffixes.
* FALSE otherwise
*
* Side Effects:
* None
*-----------------------------------------------------------------------
*/
Boolean
Suff_IsTransform(char *str)
{
Suff *src, *targ;
return (SuffParseTransform(str, &src, &targ));
}
/*-
*-----------------------------------------------------------------------
* Suff_AddTransform --
* Add the transformation rule described by the line to the
* list of rules and place the transformation itself in the graph
*
* Input:
* line name of transformation to add
*
* Results:
* The node created for the transformation in the transforms list
*
* Side Effects:
* The node is placed on the end of the transforms Lst and links are
* made between the two suffixes mentioned in the target name
*-----------------------------------------------------------------------
*/
GNode *
Suff_AddTransform(char *line)
{
GNode *gn; /* GNode of transformation rule */
Suff *s, /* source suffix */
*t; /* target suffix */
LstNode ln; /* Node for existing transformation */
ln = Lst_Find(transforms, line, SuffGNHasNameP);
if (ln == NULL) {
/*
* Make a new graph node for the transformation. It will be filled in
* by the Parse module.
*/
gn = Targ_NewGN(line);
(void)Lst_AtEnd(transforms, gn);
} else {
/*
* New specification for transformation rule. Just nuke the old list
* of commands so they can be filled in again... We don't actually
* free the commands themselves, because a given command can be
* attached to several different transformations.
*/
gn = (GNode *)Lst_Datum(ln);
Lst_Destroy(gn->commands, NULL);
Lst_Destroy(gn->children, NULL);
gn->commands = Lst_Init(FALSE);
gn->children = Lst_Init(FALSE);
}
gn->type = OP_TRANSFORM;
(void)SuffParseTransform(line, &s, &t);
/*
* link the two together in the proper relationship and order
*/
if (DEBUG(SUFF)) {
fprintf(debug_file, "defining transformation from `%s' to `%s'\n",
s->name, t->name);
}
SuffInsert(t->children, s);
SuffInsert(s->parents, t);
return (gn);
}
/*-
*-----------------------------------------------------------------------
* Suff_EndTransform --
* Handle the finish of a transformation definition, removing the
* transformation from the graph if it has neither commands nor
* sources. This is a callback procedure for the Parse module via
* Lst_ForEach
*
* Input:
* gnp Node for transformation
* dummy Node for transformation
*
* Results:
* === 0
*
* Side Effects:
* If the node has no commands or children, the children and parents
* lists of the affected suffixes are altered.
*
*-----------------------------------------------------------------------
*/
int
Suff_EndTransform(void *gnp, void *dummy)
{
GNode *gn = (GNode *)gnp;
if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
if ((gn->type & OP_TRANSFORM) && Lst_IsEmpty(gn->commands) &&
Lst_IsEmpty(gn->children))
{
Suff *s, *t;
/*
* SuffParseTransform() may fail for special rules which are not
* actual transformation rules. (e.g. .DEFAULT)
*/
if (SuffParseTransform(gn->name, &s, &t)) {
Lst p;
if (DEBUG(SUFF)) {
fprintf(debug_file, "deleting transformation from `%s' to `%s'\n",
s->name, t->name);
}
/*
* Store s->parents because s could be deleted in SuffRemove
*/
p = s->parents;
/*
* Remove the source from the target's children list. We check for a
* nil return to handle a beanhead saying something like
* .c.o .c.o:
*
* We'll be called twice when the next target is seen, but .c and .o
* are only linked once...
*/
SuffRemove(t->children, s);
/*
* Remove the target from the source's parents list
*/
SuffRemove(p, t);
}
} else if ((gn->type & OP_TRANSFORM) && DEBUG(SUFF)) {
fprintf(debug_file, "transformation %s complete\n", gn->name);
}
return(dummy ? 0 : 0);
}
/*-
*-----------------------------------------------------------------------
* SuffRebuildGraph --
* Called from Suff_AddSuffix via Lst_ForEach to search through the
* list of existing transformation rules and rebuild the transformation
* graph when it has been destroyed by Suff_ClearSuffixes. If the
* given rule is a transformation involving this suffix and another,
* existing suffix, the proper relationship is established between
* the two.
*
* Input:
* transformp Transformation to test
* sp Suffix to rebuild
*
* Results:
* Always 0.
*
* Side Effects:
* The appropriate links will be made between this suffix and
* others if transformation rules exist for it.
*
*-----------------------------------------------------------------------
*/
static int
SuffRebuildGraph(void *transformp, void *sp)
{
GNode *transform = (GNode *)transformp;
Suff *s = (Suff *)sp;
char *cp;
LstNode ln;
Suff *s2;
SuffixCmpData sd;
/*
* First see if it is a transformation from this suffix.
*/
cp = UNCONST(SuffStrIsPrefix(s->name, transform->name));
if (cp != NULL) {
ln = Lst_Find(sufflist, cp, SuffSuffHasNameP);
if (ln != NULL) {
/*
* Found target. Link in and return, since it can't be anything
* else.
*/
s2 = (Suff *)Lst_Datum(ln);
SuffInsert(s2->children, s);
SuffInsert(s->parents, s2);
return(0);
}
}
/*
* Not from, maybe to?
*/
sd.len = strlen(transform->name);
sd.ename = transform->name + sd.len;
cp = SuffSuffIsSuffix(s, &sd);
if (cp != NULL) {
/*
* Null-terminate the source suffix in order to find it.
*/
cp[1] = '\0';
ln = Lst_Find(sufflist, transform->name, SuffSuffHasNameP);
/*
* Replace the start of the target suffix
*/
cp[1] = s->name[0];
if (ln != NULL) {
/*
* Found it -- establish the proper relationship
*/
s2 = (Suff *)Lst_Datum(ln);
SuffInsert(s->children, s2);
SuffInsert(s2->parents, s);
}
}
return(0);
}
/*-
*-----------------------------------------------------------------------
* SuffScanTargets --
* Called from Suff_AddSuffix via Lst_ForEach to search through the
* list of existing targets and find if any of the existing targets
* can be turned into a transformation rule.
*
* Results:
* 1 if a new main target has been selected, 0 otherwise.
*
* Side Effects:
* If such a target is found and the target is the current main
* target, the main target is set to NULL and the next target
* examined (if that exists) becomes the main target.
*
*-----------------------------------------------------------------------
*/
static int
SuffScanTargets(void *targetp, void *gsp)
{
GNode *target = (GNode *)targetp;
GNodeSuff *gs = (GNodeSuff *)gsp;
Suff *s, *t;
char *ptr;
if (*gs->gn == NULL && gs->r && (target->type & OP_NOTARGET) == 0) {
*gs->gn = target;
Targ_SetMain(target);
return 1;
}
if ((unsigned int)target->type == OP_TRANSFORM)
return 0;
if ((ptr = strstr(target->name, gs->s->name)) == NULL ||
ptr == target->name)
return 0;
if (SuffParseTransform(target->name, &s, &t)) {
if (*gs->gn == target) {
gs->r = TRUE;
*gs->gn = NULL;
Targ_SetMain(NULL);
}
Lst_Destroy(target->children, NULL);
target->children = Lst_Init(FALSE);
target->type = OP_TRANSFORM;
/*
* link the two together in the proper relationship and order
*/
if (DEBUG(SUFF)) {
fprintf(debug_file, "defining transformation from `%s' to `%s'\n",
s->name, t->name);
}
SuffInsert(t->children, s);
SuffInsert(s->parents, t);
}
return 0;
}
/*-
*-----------------------------------------------------------------------
* Suff_AddSuffix --
* Add the suffix in string to the end of the list of known suffixes.
* Should we restructure the suffix graph? Make doesn't...
*
* Input:
* str the name of the suffix to add
*
* Results:
* None
*
* Side Effects:
* A GNode is created for the suffix and a Suff structure is created and
* added to the suffixes list unless the suffix was already known.
* The mainNode passed can be modified if a target mutated into a
* transform and that target happened to be the main target.
*-----------------------------------------------------------------------
*/
void
Suff_AddSuffix(char *str, GNode **gn)
{
Suff *s; /* new suffix descriptor */
LstNode ln;
GNodeSuff gs;
ln = Lst_Find(sufflist, str, SuffSuffHasNameP);
if (ln == NULL) {
s = bmake_malloc(sizeof(Suff));
s->name = bmake_strdup(str);
s->nameLen = strlen(s->name);
s->searchPath = Lst_Init(FALSE);
s->children = Lst_Init(FALSE);
s->parents = Lst_Init(FALSE);
s->ref = Lst_Init(FALSE);
s->sNum = sNum++;
s->flags = 0;
s->refCount = 1;
(void)Lst_AtEnd(sufflist, s);
/*
* We also look at our existing targets list to see if adding
* this suffix will make one of our current targets mutate into
* a suffix rule. This is ugly, but other makes treat all targets
* that start with a . as suffix rules.
*/
gs.gn = gn;
gs.s = s;
gs.r = FALSE;
Lst_ForEach(Targ_List(), SuffScanTargets, &gs);
/*
* Look for any existing transformations from or to this suffix.
* XXX: Only do this after a Suff_ClearSuffixes?
*/
Lst_ForEach(transforms, SuffRebuildGraph, s);
}
}
/*-