-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuff.c
2146 lines (1864 loc) · 54.1 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.380 2024/06/02 15:31:26 rillig 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.
*/
/*
* Maintain suffix lists and find implicit dependents using suffix
* transformation rules such as ".c.o".
*
* Interface:
* Suff_Init Initialize the module.
*
* Suff_End Clean up the module.
*
* Suff_ExtendPaths
* Extend the search path of each suffix to include the
* default search path.
*
* Suff_ClearSuffixes
* Clear out all the suffixes and transformations.
*
* Suff_IsTransform
* See if the passed string is 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.
*
* 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 "make.h"
#include "dir.h"
/* "@(#)suff.c 8.4 (Berkeley) 3/21/94" */
typedef List SuffixList;
typedef ListNode SuffixListNode;
typedef List CandidateList;
typedef ListNode CandidateListNode;
/* The defined suffixes, such as '.c', '.o', '.l'. */
static SuffixList sufflist = LST_INIT;
#ifdef CLEANUP
/* The suffixes to be cleaned up at the end. */
static SuffixList suffClean = LST_INIT;
#endif
/*
* The transformation rules, such as '.c.o' to transform '.c' into '.o',
* or simply '.c' to transform 'file.c' into 'file'.
*/
static GNodeList transforms = LST_INIT;
/*
* Counter for assigning suffix numbers.
* TODO: What are these suffix numbers used for?
*/
static int sNum = 0;
/*
* A suffix such as ".c" or ".o" that may be used in suffix transformation
* rules such as ".c.o:".
*/
typedef struct Suffix {
/* The suffix itself, such as ".c" */
char *name;
/* Length of the name, to avoid strlen calls */
size_t nameLen;
/*
* This suffix marks include files. Their search path ends up in the
* undocumented special variable '.INCLUDES'.
*/
bool include:1;
/*
* This suffix marks library files. Their search path ends up in the
* undocumented special variable '.LIBS'.
*/
bool library:1;
/*
* The empty suffix.
*
* XXX: What is the difference between the empty suffix and the null
* suffix?
*
* XXX: Why is SUFF_NULL needed at all? Wouldn't nameLen == 0 mean
* the same?
*/
bool isNull:1;
/* The path along which files of this suffix may be found */
SearchPath *searchPath;
/* The suffix number; TODO: document the purpose of this number */
int sNum;
/* Reference count of list membership and several other places */
int refCount;
/* Suffixes we have a transformation to */
SuffixList parents;
/* Suffixes we have a transformation from */
SuffixList children;
} Suffix;
/*
* A candidate when searching for implied sources.
*
* For example, when "src.o" is to be made, a typical candidate is "src.c"
* via the transformation rule ".c.o". If that doesn't exist, maybe there is
* another transformation rule ".pas.c" that would make "src.pas" an indirect
* candidate as well. The first such chain that leads to an existing file or
* node is finally chosen to be made.
*/
typedef struct Candidate {
/* The file or node to look for. */
char *file;
/*
* The prefix from which file was formed. Its memory is shared among
* all candidates.
*/
char *prefix;
/* The suffix on the file. */
Suffix *suff;
/*
* The candidate that can be made from this, or NULL for the
* top-level candidate.
*/
struct Candidate *parent;
/* The node describing the file. */
GNode *node;
/*
* Count of existing children, only used for memory management, so we
* don't free this candidate too early or too late.
*/
int numChildren;
#ifdef DEBUG_SRC
CandidateList childrenList;
#endif
} Candidate;
typedef struct CandidateSearcher {
CandidateList list;
/*
* TODO: Add HashSet for seen entries, to avoid endless loops such as
* in suff-transform-endless.mk.
*/
} CandidateSearcher;
/* TODO: Document the difference between nullSuff and emptySuff. */
/* The NULL suffix is used when a file has no known suffix */
static Suffix *nullSuff;
/* The empty suffix required for POSIX single-suffix transformation rules */
static Suffix *emptySuff;
static Suffix *
Suffix_Ref(Suffix *suff)
{
suff->refCount++;
return suff;
}
/* Change the value of a Suffix variable, adjusting the reference counts. */
static void
Suffix_Reassign(Suffix **var, Suffix *suff)
{
if (*var != NULL)
(*var)->refCount--;
*var = suff;
suff->refCount++;
}
/* Set a Suffix variable to NULL, adjusting the reference count. */
static void
Suffix_Unassign(Suffix **var)
{
if (*var != NULL)
(*var)->refCount--;
*var = NULL;
}
/*
* See if pref is a prefix of str.
* Return NULL if it ain't, pointer to character in str after prefix if so.
*/
static const char *
StrTrimPrefix(const char *pref, const char *str)
{
while (*str != '\0' && *pref == *str) {
pref++;
str++;
}
return *pref != '\0' ? NULL : str;
}
/*
* See if suff is a suffix of str, and if so, return the pointer to the suffix
* in str, which at the same time marks the end of the prefix.
*/
static const char *
StrTrimSuffix(const char *str, size_t strLen, const char *suff, size_t suffLen)
{
const char *suffInStr;
size_t i;
if (strLen < suffLen)
return NULL;
suffInStr = str + strLen - suffLen;
for (i = 0; i < suffLen; i++)
if (suff[i] != suffInStr[i])
return NULL;
return suffInStr;
}
/*
* See if suff is a suffix of name, and if so, return the end of the prefix
* in name.
*/
static const char *
Suffix_TrimSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd)
{
return StrTrimSuffix(nameEnd - nameLen, nameLen,
suff->name, suff->nameLen);
}
static bool
Suffix_IsSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd)
{
return Suffix_TrimSuffix(suff, nameLen, nameEnd) != NULL;
}
static Suffix *
FindSuffixByNameLen(const char *name, size_t nameLen)
{
SuffixListNode *ln;
for (ln = sufflist.first; ln != NULL; ln = ln->next) {
Suffix *suff = ln->datum;
if (suff->nameLen == nameLen &&
memcmp(suff->name, name, nameLen) == 0)
return suff;
}
return NULL;
}
static Suffix *
FindSuffixByName(const char *name)
{
return FindSuffixByNameLen(name, strlen(name));
}
static GNode *
FindTransformByName(const char *name)
{
GNodeListNode *ln;
for (ln = transforms.first; ln != NULL; ln = ln->next) {
GNode *gn = ln->datum;
if (strcmp(gn->name, name) == 0)
return gn;
}
return NULL;
}
static void
SuffixList_Unref(SuffixList *list, Suffix *suff)
{
SuffixListNode *ln = Lst_FindDatum(list, suff);
if (ln != NULL) {
Lst_Remove(list, ln);
suff->refCount--;
}
}
static void
Suffix_Free(Suffix *suff)
{
if (suff == nullSuff)
nullSuff = NULL;
if (suff == emptySuff)
emptySuff = NULL;
#if 0
/* We don't delete suffixes in order, so we cannot use this */
if (suff->refCount != 0)
Punt("Internal error deleting suffix `%s' with refcount = %d",
suff->name, suff->refCount);
#endif
Lst_Done(&suff->children);
Lst_Done(&suff->parents);
SearchPath_Free(suff->searchPath);
free(suff->name);
free(suff);
}
/* Remove the suffix from the list, and free if it is otherwise unused. */
static void
SuffixList_Remove(SuffixList *list, Suffix *suff)
{
SuffixList_Unref(list, suff);
if (suff->refCount == 0) {
/* XXX: can lead to suff->refCount == -1 */
SuffixList_Unref(&sufflist, suff);
DEBUG1(SUFF, "Removing suffix \"%s\"\n", suff->name);
Suffix_Free(suff);
}
}
/*
* Insert the suffix into the list, keeping the list ordered by suffix
* number.
*/
static void
SuffixList_Insert(SuffixList *list, Suffix *suff)
{
SuffixListNode *ln;
Suffix *listSuff = NULL;
for (ln = list->first; ln != NULL; ln = ln->next) {
listSuff = ln->datum;
if (listSuff->sNum >= suff->sNum)
break;
}
if (ln == NULL) {
DEBUG2(SUFF, "inserting \"%s\" (%d) at end of list\n",
suff->name, suff->sNum);
Lst_Append(list, Suffix_Ref(suff));
} else if (listSuff->sNum != suff->sNum) {
DEBUG4(SUFF, "inserting \"%s\" (%d) before \"%s\" (%d)\n",
suff->name, suff->sNum, listSuff->name, listSuff->sNum);
Lst_InsertBefore(list, ln, Suffix_Ref(suff));
} else {
DEBUG2(SUFF, "\"%s\" (%d) is already there\n",
suff->name, suff->sNum);
}
}
static void
Relate(Suffix *srcSuff, Suffix *targSuff)
{
SuffixList_Insert(&targSuff->children, srcSuff);
SuffixList_Insert(&srcSuff->parents, targSuff);
}
static Suffix *
Suffix_New(const char *name)
{
Suffix *suff = bmake_malloc(sizeof *suff);
suff->name = bmake_strdup(name);
suff->nameLen = strlen(suff->name);
suff->searchPath = SearchPath_New();
Lst_Init(&suff->children);
Lst_Init(&suff->parents);
suff->sNum = sNum++;
suff->include = false;
suff->library = false;
suff->isNull = false;
suff->refCount = 1; /* XXX: why 1? It's not assigned anywhere yet. */
return suff;
}
/*
* 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 when a line '.SUFFIXES:' with an empty suffixes list is
* encountered in a makefile.
*/
void
Suff_ClearSuffixes(void)
{
#ifdef CLEANUP
Lst_MoveAll(&suffClean, &sufflist);
#endif
DEBUG0(SUFF, "Clearing all suffixes\n");
Lst_Init(&sufflist);
sNum = 0;
if (nullSuff != NULL)
Suffix_Free(nullSuff);
emptySuff = nullSuff = Suffix_New("");
SearchPath_AddAll(nullSuff->searchPath, &dirSearchPath);
nullSuff->include = false;
nullSuff->library = false;
nullSuff->isNull = true;
}
/*
* Parse a transformation string such as ".c.o" to find its two component
* suffixes (the source ".c" and the target ".o"). If there are no such
* suffixes, try a single-suffix transformation as well.
*
* Return true if the string is a valid transformation.
*/
static bool
ParseTransform(const char *str, Suffix **out_src, Suffix **out_targ)
{
SuffixListNode *ln;
Suffix *single = 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 (ln = sufflist.first; ln != NULL; ln = ln->next) {
Suffix *src = ln->datum;
if (StrTrimPrefix(src->name, str) == NULL)
continue;
if (str[src->nameLen] == '\0') {
single = src;
} else {
Suffix *targ = FindSuffixByName(str + src->nameLen);
if (targ != NULL) {
*out_src = src;
*out_targ = targ;
return true;
}
}
}
if (single != NULL) {
/*
* There was a suffix that encompassed the entire string, so we
* assume it was a transformation to the null suffix (thank you
* POSIX; search for "single suffix" or "single-suffix").
*
* We still prefer to find a double rule over a singleton,
* hence we leave this check until the end.
*
* XXX: Use emptySuff over nullSuff?
*/
*out_src = single;
*out_targ = nullSuff;
return true;
}
return false;
}
/*
* Return true if the given string is a transformation rule, that is, a
* concatenation of two known suffixes such as ".c.o" or a single suffix
* such as ".o".
*/
bool
Suff_IsTransform(const char *str)
{
Suffix *src, *targ;
return ParseTransform(str, &src, &targ);
}
/*
* Add the transformation rule to the list of rules and place the
* transformation itself in the graph.
*
* The transformation is linked to the two suffixes mentioned in the name.
*
* Input:
* name must have the form ".from.to" or just ".from"
*
* Results:
* The created or existing transformation node in the transforms list
*/
GNode *
Suff_AddTransform(const char *name)
{
Suffix *srcSuff;
Suffix *targSuff;
GNode *gn = FindTransformByName(name);
if (gn == NULL) {
/*
* Make a new graph node for the transformation. It will be
* filled in by the Parse module.
*/
gn = GNode_New(name);
Lst_Append(&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.
*/
Lst_Done(&gn->commands);
Lst_Init(&gn->commands);
Lst_Done(&gn->children);
Lst_Init(&gn->children);
}
gn->type = OP_TRANSFORM;
{
/* TODO: Avoid the redundant parsing here. */
bool ok = ParseTransform(name, &srcSuff, &targSuff);
assert(ok);
/* LINTED 129 *//* expression has null effect */
(void)ok;
}
/* Link the two together in the proper relationship and order. */
DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n",
srcSuff->name, targSuff->name);
Relate(srcSuff, targSuff);
return gn;
}
/*
* Handle the finish of a transformation definition, removing the
* transformation from the graph if it has neither commands nor sources.
*
* If the node has no commands or children, the children and parents lists
* of the affected suffixes are altered.
*
* Input:
* gn Node for transformation
*/
void
Suff_EndTransform(GNode *gn)
{
Suffix *srcSuff, *targSuff;
SuffixList *srcSuffParents;
if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty(&gn->cohorts))
gn = gn->cohorts.last->datum;
if (!(gn->type & OP_TRANSFORM))
return;
if (!Lst_IsEmpty(&gn->commands) || !Lst_IsEmpty(&gn->children)) {
DEBUG1(SUFF, "transformation %s complete\n", gn->name);
return;
}
/*
* SuffParseTransform() may fail for special rules which are not
* actual transformation rules. (e.g. .DEFAULT)
*/
if (!ParseTransform(gn->name, &srcSuff, &targSuff))
return;
DEBUG2(SUFF, "deleting incomplete transformation from `%s' to `%s'\n",
srcSuff->name, targSuff->name);
/*
* Remember the parents since srcSuff could be deleted in
* SuffixList_Remove.
*/
srcSuffParents = &srcSuff->parents;
SuffixList_Remove(&targSuff->children, srcSuff);
SuffixList_Remove(srcSuffParents, targSuff);
}
/*
* Called from Suff_AddSuffix 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.
*
* The appropriate links will be made between this suffix and others if
* transformation rules exist for it.
*
* Input:
* transform Transformation to test
* suff Suffix to rebuild
*/
static void
RebuildGraph(GNode *transform, Suffix *suff)
{
const char *name = transform->name;
size_t nameLen = strlen(name);
const char *toName;
/*
* See if it is a transformation from this suffix to another suffix.
*/
toName = StrTrimPrefix(suff->name, name);
if (toName != NULL) {
Suffix *to = FindSuffixByName(toName);
if (to != NULL) {
Relate(suff, to);
return;
}
}
/*
* See if it is a transformation from another suffix to this suffix.
*/
toName = Suffix_TrimSuffix(suff, nameLen, name + nameLen);
if (toName != NULL) {
Suffix *from = FindSuffixByNameLen(name,
(size_t)(toName - name));
if (from != NULL)
Relate(from, suff);
}
}
/*
* During Suff_AddSuffix, search through the list of existing targets and find
* if any of the existing targets can be turned into a transformation rule.
*
* 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.
*
* Results:
* true iff a new main target has been selected.
*/
static bool
UpdateTarget(GNode *target, Suffix *suff, bool *inout_removedMain)
{
Suffix *srcSuff, *targSuff;
char *ptr;
if (mainNode == NULL && *inout_removedMain &&
GNode_IsMainCandidate(target)) {
DEBUG1(MAKE, "Setting main node to \"%s\"\n", target->name);
mainNode = target;
/*
* XXX: Why could it be a good idea to return true here?
* The main task of this function is to turn ordinary nodes
* into transformations, no matter whether or not a new .MAIN
* node has been found.
*/
/*
* XXX: Even when changing this to false, none of the existing
* unit tests fails.
*/
return true;
}
if (target->type == OP_TRANSFORM)
return false;
/*
* XXX: What about a transformation ".cpp.c"? If ".c" is added as
* a new suffix, it seems wrong that this transformation would be
* skipped just because ".c" happens to be a prefix of ".cpp".
*/
ptr = strstr(target->name, suff->name);
if (ptr == NULL)
return false;
/*
* XXX: In suff-rebuild.mk, in the line '.SUFFIXES: .c .b .a', this
* condition prevents the rule '.b.c' from being added again during
* Suff_AddSuffix(".b").
*
* XXX: Removing this paragraph makes suff-add-later.mk use massive
* amounts of memory.
*/
if (ptr == target->name)
return false;
if (ParseTransform(target->name, &srcSuff, &targSuff)) {
if (mainNode == target) {
DEBUG1(MAKE,
"Setting main node from \"%s\" back to null\n",
target->name);
*inout_removedMain = true;
mainNode = NULL;
}
Lst_Done(&target->children);
Lst_Init(&target->children);
target->type = OP_TRANSFORM;
/*
* Link the two together in the proper relationship and order.
*/
DEBUG2(SUFF, "defining transformation from `%s' to `%s'\n",
srcSuff->name, targSuff->name);
Relate(srcSuff, targSuff);
}
return false;
}
/*
* Look at all existing targets to see if adding this suffix will make one
* of the current targets mutate into a suffix rule.
*
* This is ugly, but other makes treat all targets that start with a '.' as
* suffix rules.
*/
static void
UpdateTargets(Suffix *suff)
{
bool removedMain = false;
GNodeListNode *ln;
for (ln = Targ_List()->first; ln != NULL; ln = ln->next) {
GNode *gn = ln->datum;
if (UpdateTarget(gn, suff, &removedMain))
break;
}
}
/* Add the suffix to the end of the list of known suffixes. */
void
Suff_AddSuffix(const char *name)
{
GNodeListNode *ln;
Suffix *suff = FindSuffixByName(name);
if (suff != NULL)
return;
suff = Suffix_New(name);
Lst_Append(&sufflist, suff);
DEBUG1(SUFF, "Adding suffix \"%s\"\n", suff->name);
UpdateTargets(suff);
/*
* Look for any existing transformations from or to this suffix.
* XXX: Only do this after a Suff_ClearSuffixes?
*/
for (ln = transforms.first; ln != NULL; ln = ln->next)
RebuildGraph(ln->datum, suff);
}
/* Return the search path for the given suffix, or NULL. */
SearchPath *
Suff_GetPath(const char *name)
{
Suffix *suff = FindSuffixByName(name);
return suff != NULL ? suff->searchPath : NULL;
}
/*
* Extend the search paths for all suffixes to include the default search
* path (dirSearchPath).
*
* The default search path can be defined using the special target '.PATH'.
* The search path of each suffix can be defined using the special target
* '.PATH<suffix>'.
*
* If paths were specified for the ".h" suffix, the directories are stuffed
* into a global variable called ".INCLUDES" with each directory preceded by
* '-I'. The same is done for the ".lib" suffix, except the variable is called
* ".LIBS" and the flag is '-L'.
*/
void
Suff_ExtendPaths(void)
{
SuffixListNode *ln;
char *flags;
SearchPath *includesPath = SearchPath_New();
SearchPath *libsPath = SearchPath_New();
for (ln = sufflist.first; ln != NULL; ln = ln->next) {
Suffix *suff = ln->datum;
if (!Lst_IsEmpty(&suff->searchPath->dirs)) {
if (suff->include)
SearchPath_AddAll(includesPath,
suff->searchPath);
if (suff->library)
SearchPath_AddAll(libsPath, suff->searchPath);
SearchPath_AddAll(suff->searchPath, &dirSearchPath);
} else {
SearchPath_Free(suff->searchPath);
suff->searchPath = Dir_CopyDirSearchPath();
}
}
flags = SearchPath_ToFlags(includesPath, "-I");
Global_Set(".INCLUDES", flags);
free(flags);
flags = SearchPath_ToFlags(libsPath, "-L");
Global_Set(".LIBS", flags);
free(flags);
SearchPath_Free(includesPath);
SearchPath_Free(libsPath);
}
/*
* Add the given suffix as a type of file which gets included.
* Called when a '.INCLUDES: .h' line is parsed.
* To have an effect, the suffix must already exist.
* This affects the magic variable '.INCLUDES'.
*/
void
Suff_AddInclude(const char *suffName)
{
Suffix *suff = FindSuffixByName(suffName);
if (suff != NULL)
suff->include = true;
}
/*
* Add the given suffix as a type of file which is a library.
* Called when a '.LIBS: .lib' line is parsed.
* To have an effect, the suffix must already exist.
* This affects the magic variable '.LIBS'.
*/
void
Suff_AddLib(const char *suffName)
{
Suffix *suff = FindSuffixByName(suffName);
if (suff != NULL)
suff->library = true;
}
/********** Implicit Source Search Functions *********/
static void
CandidateSearcher_Init(CandidateSearcher *cs)
{
Lst_Init(&cs->list);
}
static void
CandidateSearcher_Done(CandidateSearcher *cs)
{
Lst_Done(&cs->list);
}
static void
CandidateSearcher_Add(CandidateSearcher *cs, Candidate *cand)
{
/* TODO: filter duplicates */
Lst_Append(&cs->list, cand);
}
static void
CandidateSearcher_AddIfNew(CandidateSearcher *cs, Candidate *cand)
{
/* TODO: filter duplicates */
if (Lst_FindDatum(&cs->list, cand) == NULL)
Lst_Append(&cs->list, cand);
}
static void
CandidateSearcher_MoveAll(CandidateSearcher *cs, CandidateList *list)
{
/* TODO: filter duplicates */
Lst_MoveAll(&cs->list, list);
}
#ifdef DEBUG_SRC
static void
CandidateList_PrintAddrs(CandidateList *list)
{
CandidateListNode *ln;
for (ln = list->first; ln != NULL; ln = ln->next) {
Candidate *cand = ln->datum;
debug_printf(" %p:%s", cand, cand->file);
}
debug_printf("\n");
}
#endif
static Candidate *
Candidate_New(char *name, char *prefix, Suffix *suff, Candidate *parent,
GNode *gn)
{
Candidate *cand = bmake_malloc(sizeof *cand);
cand->file = name;
cand->prefix = prefix;
cand->suff = Suffix_Ref(suff);
cand->parent = parent;
cand->node = gn;
cand->numChildren = 0;
#ifdef DEBUG_SRC
Lst_Init(&cand->childrenList);
#endif
return cand;
}
/* Add a new candidate to the list. */
/*ARGSUSED*/
static void
CandidateList_Add(CandidateList *list, char *srcName, Candidate *targ,
Suffix *suff, const char *debug_tag MAKE_ATTR_UNUSED)
{
Candidate *cand = Candidate_New(srcName, targ->prefix, suff, targ,
NULL);
targ->numChildren++;
Lst_Append(list, cand);
#ifdef DEBUG_SRC
Lst_Append(&targ->childrenList, cand);
debug_printf("%s add suff %p:%s candidate %p:%s to list %p:",
debug_tag, targ, targ->file, cand, cand->file, list);
CandidateList_PrintAddrs(list);
#endif
}
/*
* Add all candidates to the list that can be formed by applying a suffix to