-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuff.c
More file actions
2015 lines (1773 loc) · 52 KB
/
suff.c
File metadata and controls
2015 lines (1773 loc) · 52 KB
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
/* $OpenBSD: suff.c,v 1.79 2010/07/19 19:46:44 espie Exp $ */
/* $NetBSD: suff.c,v 1.13 1996/11/06 17:59:25 christos Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
* 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. 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.
*/
/*-
* 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_ClearSuffixes Clear out all the suffixes and defined
* transformations.
*
* 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_ParseAsTransform Line might be a suffix line, check it.
* If it's not, return NULL. Otherwise, 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.
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <signal.h>
#include <ohash.h>
#include "config.h"
#include "defines.h"
#include "dir.h"
#include "direxpand.h"
#include "engine.h"
#include "arch.h"
#include "suff.h"
#include "var.h"
#include "targ.h"
#include "error.h"
#include "str.h"
#include "lst.h"
#include "memory.h"
#include "gnode.h"
#include "make.h"
#include "stats.h"
/* XXX the suffixes hash is stored using a specific hash function, suitable
* for looking up suffixes in reverse.
*/
static struct ohash suffixes;
/* We remember the longest suffix, so we don't need to look beyond that. */
size_t maxLen;
static LIST srclist;
/* Transforms (.c.o) are stored in another hash, independently from suffixes.
* When make sees a target, it checks whether it's currently parsable as a
* transform (according to the active suffixes). If yes, it's stored as a
* new transform.
*
* XXX
* But transforms DO NOT have a canonical decomposition as a set of suffixes,
* and will be used as necessary later, when looking up implicit rules for
* actual targets.
*
* For instance, a transform .a.b.c can be parsed as .a -> .b.c if suffixes
* .a and .b.c are active, and then LATER, reused as .a.b -> .c if suffixes
* .a.b and .c are active.
*/
static struct ohash transforms;
/* conflicts between suffixes are solved by suffix declaration order. */
static int order = 0;
/*
* Structure describing an individual suffix.
*/
typedef struct Suff_ {
size_t nameLen; /* optimisation: strlen(name) */
short flags;
#define SUFF_INCLUDE 0x01 /* suffix marked with .INCLUDES keyword */
#define SUFF_LIBRARY 0x02 /* suffix marked with .LIBS keyword */
#define SUFF_NULL 0x04 /* The empty suffix (normally '', */
/* but see .EMPTY keyword) */
#define SUFF_ACTIVE 0x08 /* We never destroy suffixes and rules, */
/* we just deactivate them. */
#define SUFF_PATH 0x10 /* False suffix: actually, the path keyword */
LIST searchPath; /* The path along which files of this suffix
* may be found */
int order; /* order of declaration for conflict
* resolution. */
LIST parents; /* List of Suff we have a transformation to */
LIST children; /* List of Suff we have a transformation from */
char name[1];
} Suff;
static struct ohash_info suff_info = {
offsetof(struct Suff_, name), NULL,
hash_alloc, hash_free, element_alloc
};
/*
* 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
LIST 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;
static Suff *suffNull; /* The NULL suffix for this run */
static Suff *emptySuff; /* The empty suffix required for POSIX
* single-suffix transformation rules */
static void build_path_variable(struct ohash *, int, const char *, const char *);
static void add_property(const char *, const char *, int);
#define parse_transform(s, p, q) parse_transformi(s, s + strlen(s), p, q)
static bool parse_transformi(const char *, const char *, Suff **, Suff **);
#define new_suffix(s) new_suffixi(s, NULL)
static Suff *new_suffixi(const char *, const char *);
static void reverse_hash_add_char(uint32_t *, const char *);
static uint32_t reverse_hashi(const char *, const char **);
static unsigned int reverse_slot(struct ohash *, const char *, const char **);
static void clear_suffixes(void);
static void record_possible_suffix(Suff *, GNode *, char *, Lst, Lst);
static void record_possible_suffixes(GNode *, Lst, Lst);
static Suff *find_suffix_as_suffix(Lst, const char *, const char *);
static Suff *add_suffixi(const char *, const char *);
#ifdef CLEANUP
static void SuffFree(void *);
#endif
static void SuffInsert(Lst, Suff *);
static void 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 SuffExpandVarChildren(LstNode, GNode *, GNode *);
static void SuffExpandWildChildren(LstNode, GNode *, GNode *);
static bool SuffApplyTransform(GNode *, GNode *, Suff *, Suff *);
static void SuffFindDeps(GNode *, Lst);
static void SuffFindArchiveDeps(GNode *, Lst);
static void SuffFindNormalDeps(GNode *, Lst);
static void SuffPrintName(void *);
static void SuffPrintSuff(void *);
static void SuffPrintTrans(GNode *);
#define find_suff(name) find_suffi(name, NULL)
static Suff *find_suffi(const char *, const char *);
static Suff *find_best_suffix(const char *, const char *);
static GNode *find_transform(const char *);
static GNode *find_or_create_transformi(const char *, const char *);
static void setup_paths(void);
static void build_suffixes_graph(void);
static void special_path_hack(void);
#ifdef DEBUG_SRC
static void PrintAddr(void *);
#endif
/* hash functions for the suffixes hash */
/* add one char to the hash */
static void
reverse_hash_add_char(uint32_t *pk, const char *s)
{
*pk = ((*pk << 2) | (*pk >> 30)) ^ *s;
}
/* build a full hash from end to start */
static uint32_t
reverse_hashi(const char *s, const char **e)
{
const char *p;
uint32_t k;
if (*e == NULL)
*e = s + strlen(s);
p = *e;
if (p == s)
k = 0;
else
k = *--p;
while (p != s) {
reverse_hash_add_char(&k, --p);
}
return k;
}
static unsigned int
reverse_slot(struct ohash *h, const char *s, const char **e)
{
uint32_t hv;
hv = reverse_hashi(s, e);
return ohash_lookup_interval(h, s, *e, hv);
}
static char *
suffix_is_suffix(Suff *s, const char *str, const char *estr)
{
const char *p1; /* Pointer into suffix name */
const char *p2; /* Pointer into string being examined */
if (estr - str < (ptrdiff_t) s->nameLen)
return NULL;
p1 = s->name + s->nameLen;
p2 = estr;
while (p1 != s->name) {
p1--;
p2--;
if (*p1 != *p2)
return NULL;
}
return (char *)p2;
}
static Suff *
find_suffi(const char *name, const char *ename)
{
unsigned int slot;
#ifdef STATS_SUFF
STAT_SUFF_LOOKUP_NAME++;
#endif
slot = reverse_slot(&suffixes, name, &ename);
return ohash_find(&suffixes, slot);
}
static GNode *
find_transform(const char *name)
{
unsigned int slot;
#ifdef STATS_SUFF
STAT_TRANSFORM_LOOKUP_NAME++;
#endif
slot = ohash_qlookup(&transforms, name);
return ohash_find(&transforms, slot);
}
static GNode *
find_or_create_transformi(const char *name, const char *end)
{
GNode *r;
unsigned int slot;
#ifdef STATS_SUFF
STAT_TRANSFORM_LOOKUP_NAME++;
#endif
slot = ohash_qlookupi(&transforms, name, &end);
r = ohash_find(&transforms, slot);
if (r == NULL) {
r = Targ_NewGNi(name, end);
ohash_insert(&transforms, slot, r);
}
return r;
}
#ifdef CLEANUP
/*-
*-----------------------------------------------------------------------
* SuffFree --
* Free up all memory associated with the given suffix structure.
*
* Side Effects:
* the suffix entry is detroyed
*-----------------------------------------------------------------------
*/
static void
SuffFree(void *sp)
{
Suff *s = (Suff *)sp;
if (s == emptySuff)
emptySuff = NULL;
Lst_Destroy(&s->children, NOFREE);
Lst_Destroy(&s->parents, NOFREE);
Lst_Destroy(&s->searchPath, Dir_Destroy);
free(s->name);
free(s);
}
#endif
/*-
*-----------------------------------------------------------------------
* SuffInsert --
* Insert the suffix into the list keeping the list ordered by suffix
* numbers.
*
* 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 */
for (ln = Lst_First(l); ln != NULL; ln = Lst_Adv(ln)) {
s2 = (Suff *)Lst_Datum(ln);
if (s2->order >= s->order)
break;
}
if (DEBUG(SUFF))
printf("inserting %s(%d)...", s->name, s->order);
if (ln == NULL) {
if (DEBUG(SUFF))
printf("at end of list\n");
Lst_AtEnd(l, s);
} else if (s2->order != s->order) {
if (DEBUG(SUFF))
printf("before %s(%d)\n", s2->name, s2->order);
Lst_Insert(l, ln, s);
} else if (DEBUG(SUFF)) {
printf("already there\n");
}
}
/*-
*-----------------------------------------------------------------------
* Suff_ClearSuffixes --
* Nuke the list of suffixes but keep all transformation
* rules around.
*
* Side Effects:
* Current suffixes are reset
*-----------------------------------------------------------------------
*/
static void
clear_suffixes(void)
{
unsigned int i;
Suff *s;
for (s = ohash_first(&suffixes, &i); s != NULL;
s = ohash_next(&suffixes, &i))
s->flags &= ~SUFF_ACTIVE;
order = 0;
maxLen = 0;
suffNull = emptySuff;
}
void
Suff_ClearSuffixes(void)
{
clear_suffixes();
}
/* okay = parse_transform(str, &src, &targ);
* try parsing a string as a transformation rule, returns true if
* successful. Fills &src, &targ with the constituent suffixes.
* Special hack: source suffixes must exist OR be the special SUFF_PATH
* pseudo suffix (.PATH)
*/
static bool
parse_transformi(const char *str, const char *e, Suff **srcPtr, Suff **targPtr)
{
Suff *src, *target, *best_src, *best_target;
const char *p;
size_t len;
uint32_t hv;
unsigned int slot;
/* empty string -> no suffix */
if (e == str)
return false;
len = e - str;
if (len > 2 * maxLen)
return false;
p = e;
best_src = best_target = NULL;
hv = *--p;
while (p != str) {
slot = ohash_lookup_interval(&suffixes, p, e, hv);
/* no double suffix in there */
if (p - str <= (ptrdiff_t)maxLen) {
target = ohash_find(&suffixes, slot);
if (target != NULL && (target->flags & SUFF_ACTIVE)) {
src = find_suffi(str, p);
if (src != NULL &&
(src->flags & (SUFF_ACTIVE | SUFF_PATH))) {
/* XXX even if we find a set of suffixes, we
* have to keep going to find the best one,
* namely, the one whose src appears first in
* .SUFFIXES
*/
if (best_src == NULL ||
src->order < best_src->order) {
best_src = src;
best_target = target;
}
}
}
}
/* can't be a suffix anyways */
if (e - p >= (ptrdiff_t)maxLen)
break;
reverse_hash_add_char(&hv, --p);
}
if (p == str && best_src == NULL) {
/* no double suffix transformation, resort to single suffix if
* we find one. */
slot = ohash_lookup_interval(&suffixes, p, e, hv);
src = ohash_find(&suffixes, slot);
if (src != NULL && (src->flags & (SUFF_ACTIVE | SUFF_PATH))) {
best_src = src;
best_target = suffNull;
}
}
if (best_src != NULL) {
*srcPtr = best_src;
*targPtr = best_target;
return true;
} else {
return false;
}
}
static void
special_path_hack(void)
{
Suff *path = add_suffixi(".PATH", NULL);
path->flags |= SUFF_PATH;
}
static Suff *
find_best_suffix(const char *s, const char *e)
{
const char *p;
uint32_t hv;
unsigned int slot;
Suff *best = NULL;
Suff *suff;
if (e == s)
return NULL;
p = e;
hv = *--p;
while (p != s) {
slot = ohash_lookup_interval(&suffixes, p, e, hv);
suff = ohash_find(&suffixes, slot);
if (suff != NULL)
if (best == NULL || suff->order < best->order)
best = suff;
if (e - p >= (ptrdiff_t)maxLen)
break;
reverse_hash_add_char(&hv, --p);
}
return best;
}
/*-
*-----------------------------------------------------------------------
* Suff_ParseAsTransform --
* Try parsing a target line as a transformation rule, depending on
* existing suffixes.
*
* Possibly create anew transform, or reset an existing one.
*-----------------------------------------------------------------------
*/
GNode *
Suff_ParseAsTransform(const char *line, const char *end)
{
GNode *gn; /* GNode of transformation rule */
Suff *s; /* source suffix */
Suff *t; /* target suffix */
if (!parse_transformi(line, end, &s, &t))
return NULL;
gn = find_or_create_transformi(line, end);
/* In case the transform already exists, nuke old commands and children.
* Note we can't free them, since there might be stuff that references
* them elsewhere
*/
if (!Lst_IsEmpty(&gn->commands)) {
Lst_Destroy(&gn->commands, NOFREE);
Lst_Init(&gn->commands);
}
if (!Lst_IsEmpty(&gn->children)) {
Lst_Destroy(&gn->children, NOFREE);
Lst_Init(&gn->children);
}
gn->type = OP_TRANSFORM;
if (s->flags & SUFF_PATH) {
gn->special = SPECIAL_PATH | SPECIAL_TARGET;
gn->suffix = t;
}
if (DEBUG(SUFF))
printf("defining transformation from `%s' to `%s'\n",
s->name, t->name);
return gn;
}
static void
make_suffix_known(Suff *s)
{
if ((s->flags & SUFF_ACTIVE) == 0) {
s->order = order++;
s->flags |= SUFF_ACTIVE;
if (s->nameLen > maxLen)
maxLen = s->nameLen;
}
}
static Suff *
new_suffixi(const char *str, const char *eptr)
{
Suff *s;
s = ohash_create_entry(&suff_info, str, &eptr);
s->nameLen = eptr - str;
Lst_Init(&s->searchPath);
Lst_Init(&s->children);
Lst_Init(&s->parents);
s->flags = 0;
return s;
}
/*-
*-----------------------------------------------------------------------
* 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...
*
* Side Effects:
* A GNode is created for the suffix and a Suff structure is created and
* added to the known suffixes, unless it was already known.
*-----------------------------------------------------------------------
*/
void
Suff_AddSuffixi(const char *str, const char *end)
{
(void)add_suffixi(str, end);
}
static Suff *
add_suffixi(const char *str, const char *end)
{
Suff *s; /* new suffix descriptor */
unsigned int slot;
slot = reverse_slot(&suffixes, str, &end);
s = ohash_find(&suffixes, slot);
if (s == NULL) {
s = new_suffixi(str, end);
ohash_insert(&suffixes, slot, s);
}
make_suffix_known(s);
return s;
}
Lst
find_suffix_path(GNode *gn)
{
if (gn->suffix != NULL && gn->suffix != emptySuff)
return &(gn->suffix->searchPath);
else
return defaultPath;
}
/* find out the tagged suffixes, build a temporary path, and construct
* a variable based on that.
*/
static void
build_path_variable(struct ohash *h, int opt, const char *name,
const char *flag)
{
char *value;
LIST path;
Suff *s;
unsigned int i;
Lst_Init(&path);
for (s = ohash_first(h, &i); s != NULL; s = ohash_next(h, &i)) {
if (Lst_IsEmpty(&s->searchPath))
continue;
if (s->flags & opt)
Dir_Concat(&path, &s->searchPath);
}
value = Dir_MakeFlags(flag, &path);
Var_Set(name, value);
free(value);
Lst_Destroy(&path, Dir_Destroy);
}
static void
add_property(const char *sname, const char *end, int opt)
{
Suff *s;
s = find_suffi(sname, end);
if (s != NULL) {
s->flags |= opt;
}
}
void
Suff_AddIncludei(const char *sname, const char *end)
{
add_property(sname, end, SUFF_INCLUDE);
}
void
Suff_AddLibi(const char *sname, const char *end)
{
add_property(sname, end, SUFF_LIBRARY);
}
static void
build_suffixes_graph(void)
{
Suff *s, *s2;
GNode *gn;
unsigned int i;
for (gn = ohash_first(&transforms, &i); gn != NULL;
gn = ohash_next(&transforms, &i)) {
if (Lst_IsEmpty(&gn->commands) && Lst_IsEmpty(&gn->children))
continue;
if ((gn->special & SPECIAL_MASK) == SPECIAL_PATH)
continue;
if (parse_transform(gn->name, &s, &s2)) {
SuffInsert(&s2->children, s);
SuffInsert(&s->parents, s2);
}
}
}
/*-
*-----------------------------------------------------------------------
* setup_paths
* Extend the search paths for all suffixes to include the default
* search path.
*
* Side Effects:
* The searchPath field of all the suffixes is extended by the
* directories in defaultPath. If paths were specified for the
* ".h" suffix, the directories are stuffed into a global variable
* called ".INCLUDES" with each directory preceded by a -I. The same
* is done for the ".a" suffix, except the variable is called
* ".LIBS" and the flag is -L.
*-----------------------------------------------------------------------
*/
static void
setup_paths(void)
{
unsigned int i;
Suff *s;
for (s = ohash_first(&suffixes, &i); s != NULL;
s = ohash_next(&suffixes, &i)) {
if (!Lst_IsEmpty(&s->searchPath))
Dir_Concat(&s->searchPath, defaultPath);
else
Lst_Clone(&s->searchPath, defaultPath, Dir_CopyDir);
}
build_path_variable(&suffixes, SUFF_INCLUDE, ".INCLUDES", "-I");
build_path_variable(&suffixes, SUFF_LIBRARY, ".LIBS", "-L");
}
void
process_suffixes_after_makefile_is_read(void)
{
/* once the Makefile is finish reading, we can set up the default PATH
* stuff, and build the final suffixes graph
*/
setup_paths();
/* and we link all transforms to active suffixes at this point. */
build_suffixes_graph();
}
/********** Implicit Source Search Functions *********/
/*-
*-----------------------------------------------------------------------
* SuffAddSrc --
* Add a suffix as a Src structure to the given list with its parent
* being the given Src structure. If the suffix is the null suffix,
* the prefix is used unaltered as the file name in the Src structure.
*
* Side Effects:
* A Src structure is created and tacked onto the end of the list
*-----------------------------------------------------------------------
*/
static void
SuffAddSrc(
void *sp, /* suffix for which to create a Src structure */
void *lsp) /* list and parent for the new Src */
{
Suff *s = (Suff *)sp;
LstSrc *ls = (LstSrc *)lsp;
Src *s2; /* new Src structure */
Src *targ; /* Target structure */
targ = ls->s;
if ((s->flags & SUFF_NULL) && *s->name != '\0') {
/*
* If the suffix has been marked as the NULL suffix, also
* create a Src structure for a file with no suffix attached.
* Two birds, and all that...
*/
s2 = emalloc(sizeof(Src));
s2->file = estrdup(targ->pref);
s2->pref = targ->pref;
s2->parent = targ;
s2->node = NULL;
s2->suff = s;
s2->children = 0;
targ->children++;
Lst_AtEnd(ls->l, s2);
#ifdef DEBUG_SRC
Lst_Init(&s2->cp);
Lst_AtEnd(&targ->cp, s2);
printf("1 add %x %x to %x:", targ, s2, ls->l);
Lst_Every(ls->l, PrintAddr);
printf("\n");
#endif
}
s2 = emalloc(sizeof(Src));
s2->file = Str_concat(targ->pref, s->name, 0);
s2->pref = targ->pref;
s2->parent = targ;
s2->node = NULL;
s2->suff = s;
s2->children = 0;
targ->children++;
Lst_AtEnd(ls->l, s2);
#ifdef DEBUG_SRC
Lst_Init(&s2->cp);
Lst_AtEnd(&targ->cp, s2);
printf("2 add %x %x to %x:", targ, s2, ls->l);
Lst_Every(ls->l, PrintAddr);
printf("\n");
#endif
}
/*-
*-----------------------------------------------------------------------
* SuffAddLevel --
* Add all the children of targ as Src structures to the given list
*
* Side Effects:
* Lots of structures are created and added to the list
*-----------------------------------------------------------------------
*/
static void
SuffAddLevel(
Lst l, /* list to which to add the new level */
Src *targ) /* Src structure to use as the parent */
{
LstSrc ls;
ls.s = targ;
ls.l = l;
Lst_ForEach(&targ->suff->children, SuffAddSrc, &ls);
}
/*-
*----------------------------------------------------------------------
* SuffRemoveSrc --
* Free all src structures in list that don't have a reference count
*
* Results:
* Ture if an src was removed
*
* Side Effects:
* The memory is free'd.
*----------------------------------------------------------------------
*/
static int
SuffRemoveSrc(Lst l)
{
LstNode ln;
Src *s;
int t = 0;
#ifdef DEBUG_SRC
printf("cleaning %lx: ", (unsigned long)l);
Lst_Every(l, PrintAddr);
printf("\n");
#endif
for (ln = Lst_First(l); ln != NULL; ln = Lst_Adv(ln)) {
s = (Src *)Lst_Datum(ln);
if (s->children == 0) {
free(s->file);
if (!s->parent)
free(s->pref);
else {
#ifdef DEBUG_SRC
LstNode ln2 = Lst_Member(&s->parent->cp, s);
if (ln2 != NULL)
Lst_Remove(&s->parent->cp, ln2);
#endif
--s->parent->children;
}
#ifdef DEBUG_SRC
printf("free: [l=%x] p=%x %d\n", l, s, s->children);
Lst_Destroy(&s->cp, NOFREE);
#endif
Lst_Remove(l, ln);
free(s);
t |= 1;
return true;
}
#ifdef DEBUG_SRC
else {
printf("keep: [l=%x] p=%x %d: ", l, s, s->children);
Lst_Every(&s->cp, PrintAddr);
printf("\n");
}
#endif
}
return t;
}
/*-
*-----------------------------------------------------------------------
* SuffFindThem --
* Find the first existing file/target in the list srcs
*
* Results:
* The lowest structure in the chain of transformations
*-----------------------------------------------------------------------
*/
static Src *
SuffFindThem(
Lst srcs, /* list of Src structures to search through */
Lst slst)
{
Src *s; /* current Src */
Src *rs; /* returned Src */
char *ptr;
rs = NULL;
while ((s = (Src *)Lst_DeQueue(srcs)) != NULL) {
if (DEBUG(SUFF))
printf("\ttrying %s...", s->file);
/*
* A file is considered to exist if either a node exists in the
* graph for it or the file actually exists.
*/
if (Targ_FindNode(s->file, TARG_NOCREATE) != NULL) {
#ifdef DEBUG_SRC
printf("remove %x from %x\n", s, srcs);
#endif
rs = s;
break;
}
if ((ptr = Dir_FindFile(s->file, &s->suff->searchPath))
!= NULL) {
rs = s;
#ifdef DEBUG_SRC
printf("remove %x from %x\n", s, srcs);
#endif
free(ptr);
break;
}
if (DEBUG(SUFF))
printf("not there\n");
SuffAddLevel(srcs, s);
Lst_AtEnd(slst, s);
}
if (DEBUG(SUFF) && rs)
printf("got it\n");
return rs;
}
/*-
*-----------------------------------------------------------------------
* SuffFindCmds --
* See if any of the children of the target in the Src structure is
* one from which the target can be transformed. If there is one,
* a Src structure is put together for it and returned.
*
* Results:
* The Src structure of the "winning" child, or NULL if no such beast.
*
* Side Effects:
* A Src structure may be allocated.
*-----------------------------------------------------------------------
*/
static Src *
SuffFindCmds(
Src *targ, /* Src structure to play with */
Lst slst)
{
LstNode ln; /* General-purpose list node */
GNode *t; /* Target GNode */
GNode *s; /* Source GNode */
int prefLen; /* The length of the defined prefix */
Suff *suff; /* Suffix on matching beastie */
Src *ret; /* Return value */
const char *cp;