-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathu-config.c
1997 lines (1768 loc) · 49.1 KB
/
u-config.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
// u-config: a small, simple, portable pkg-config clone
// https://github.com/skeeto/u-config
// This is free and unencumbered software released into the public domain.
// Fundamental definitions
#define VERSION "0.30.1"
typedef int Size;
#define Size_MASK ((unsigned)-1)
#define Size_MAX ((Size)(Size_MASK>>1) - (ALIGN-1))
#define SIZEOF(x) (Size)(sizeof(x))
#define ALIGN SIZEOF(void *)
#define COUNTOF(a) (SIZEOF(a)/SIZEOF(a[0]))
typedef int Bool;
typedef unsigned char Byte;
#if __GNUC__
#define TRAP __builtin_trap()
#define NORETURN __attribute__((noreturn))
#elif _MSC_VER
#define TRAP __debugbreak()
#define NORETURN __declspec(noreturn)
#else
#define TRAP *(volatile int *)0 = 0
#define NORETURN
#endif
#ifdef DEBUG
#define ASSERT(c) if (!(c)) TRAP
#else
#define ASSERT(c)
#endif
typedef struct {
Byte *s;
Size len;
} Str;
#ifdef __cplusplus
#define S(s) makestr((Byte *)s, SIZEOF(s)-1)
static inline Str makestr(Byte *s, Size len)
{
Str r = {s, len};
return r;
}
#else
#define S(s) (Str){(Byte *)s, SIZEOF(s)-1}
#endif
typedef struct {
Str mem;
Size off;
} Arena;
typedef struct {
Arena arena;
Str *args;
Size nargs;
Str envpath; // $PKG_CONFIG_PATH or empty
Str fixedpath; // $PKG_CONFIG_LIBDIR or default
Str top_builddir; // $PKG_CONFIG_TOP_BUILD_DIR or default
Str sys_incpath; // $PKG_CONFIG_SYSTEM_INCLUDE_PATH or default
Str sys_libpath; // $PKG_CONFIG_SYSTEM_LIBRARY_PATH or default
Bool define_prefix;
Byte delim;
} Config;
// Platform API
// Application entry point. Returning from this function indicates the
// application itself completed successfully. However, an os_write error
// may result in a non-zero exit.
static void appmain(Config);
typedef enum {MapFile_OK, MapFile_NOTFOUND, MapFile_READERR} MapFileStatus;
typedef struct {
Str contents;
MapFileStatus status;
} MapFileResult;
// Load a file into memory, maybe using the arena. The path must include
// a null terminator since it may be passed directly to the OS interface.
static MapFileResult os_mapfile(Arena *, Str path);
// Write buffer to stdout (1) or stderr (2). The platform must detect
// write errors and arrange for an eventual non-zero exit status.
static void os_write(int fd, Str);
// Immediately exit the program with a non-zero status.
NORETURN static void os_fail(void);
// Application
NORETURN static void oom(void)
{
os_write(2, S("pkg-config: out of memory\n"));
os_fail();
}
static Bool digit(Byte c)
{
return c>='0' && c<='9';
}
static Bool whitespace(Byte c)
{
switch (c) {
case '\t': case '\n': case '\b': case '\f': case '\r': case ' ':
return 1;
}
return 0;
}
static Bool pathsep(Byte c)
{
return c=='/' || c=='\\';
}
static Str fillstr(Str s, Byte b)
{
for (Size i = 0; i < s.len; i++) {
s.s[i] = b;
}
return s;
}
static void *alloc(Arena *a, Size size)
{
ASSERT(size >= 0);
ASSERT(size <= Size_MAX);
size += -size & (ALIGN - 1);
Size avail = a->mem.len - a->off;
if (avail < size) {
oom();
}
Byte *p = a->mem.s + a->off;
a->off += size;
return p;
}
static void *allocarray(Arena *a, Size size, Size count)
{
ASSERT(size > 0);
ASSERT(count >= 0);
if (count > Size_MAX/size) {
oom();
}
return alloc(a, size*count);
}
static Str newstr(Arena *a, Size len)
{
Str r = {(Byte *)alloc(a, len), len};
return r;
}
static void *zalloc(Arena *a, Size size)
{
Str r = newstr(a, size);
return fillstr(r, 0).s;
}
static Str maxstr(Arena *a)
{
Size len = a->mem.len - a->off;
return newstr(a, len);
}
// Fill free space with garbage when debugging.
static void shredfree(Arena *a)
{
(void)a;
#ifdef DEBUG
Arena temp = *a;
fillstr(maxstr(&temp), 0xa5);
#endif
}
static Str fromptrs(Byte *beg, Byte *end)
{
ASSERT(beg);
ASSERT(end);
ASSERT(end >= beg);
Str s = {beg, (Size)(end - beg)};
return s;
}
// Copy src into dst returning the remaining portion of dst.
static Str copy(Str dst, Str src)
{
ASSERT(dst.len >= src.len);
for (Size i = 0; i < src.len; i++) {
dst.s[i] = src.s[i];
}
Str r = {dst.s+src.len, dst.len-src.len};
return r;
}
// Compare strings, returning -1, 0, or +1.
static int orderstr(Str a, Str b)
{
// NOTE: "null" strings are still valid strings
Size len = a.len<b.len ? a.len : b.len;
for (Size i = 0; i < len; i++) {
int d = a.s[i] - b.s[i];
if (d) {
return d < 0 ? -1 : +1;
}
}
if (a.len == b.len) {
return 0;
}
return a.len<b.len ? -1 : +1;
}
static Bool equals(Str a, Str b)
{
return 0 == orderstr(a, b);
}
static Str cuthead(Str s, Size off)
{
ASSERT(off >= 0);
ASSERT(off <= s.len);
s.s += off;
s.len -= off;
return s;
}
static Str takehead(Str s, Size len)
{
ASSERT(len >= 0);
ASSERT(len <= s.len);
s.len = len;
return s;
}
static Str cuttail(Str s, Size len)
{
ASSERT(len >= 0);
ASSERT(len <= s.len);
Str r = {s.s, s.len-len};
return r;
}
static Str taketail(Str s, Size len)
{
return cuthead(s, s.len-len);
}
static Bool startswith(Str s, Str prefix)
{
return s.len>=prefix.len && equals(takehead(s, prefix.len), prefix);
}
static Size hash(Str s)
{
unsigned long long h = 257;
for (Size i = 0; i < s.len; i++) {
h ^= s.s[i];
h *= 1111111111111111111;
}
h ^= h >> 33;
return (Size)(h & Size_MASK);
}
typedef struct {
Str head;
Str tail;
} StrPair;
static StrPair digits(Str s)
{
Size i = 0;
for (; i<s.len && digit(s.s[i]); i++) {}
StrPair r = {{s.s, i}, {s.s+i, s.len-i}};
return r;
}
static Bool tokenspace(Byte c)
{
return whitespace(c) || c==',';
}
static Str skiptokenspace(Str s)
{
for (; s.len && tokenspace(*s.s); s = cuthead(s, 1)) {}
return s;
}
static StrPair nexttoken(Str s)
{
s = skiptokenspace(s);
Size len = 0;
for (; len<s.len && !tokenspace(s.s[len]); len++) {}
StrPair r = {{s.s, len}, cuthead(s, len)};
return r;
}
typedef struct {
Str head;
Str tail;
Bool ok;
} Cut;
static Cut cut(Str s, Byte delim)
{
Size len = 0;
for (; len < s.len; len++) {
if (s.s[len] == delim) {
break;
}
}
if (len == s.len) {
Cut r = {s, cuthead(s, s.len), 0};
return r;
}
Cut r = {{s.s, len}, cuthead(s, len+1), 1};
return r;
}
// Intrusive treap, embed at the beginning of nodes
typedef struct Treap {
struct Treap *parent;
struct Treap *child[2];
Str key;
} Treap;
// Low-level treap search and insertion. It uses the given size when
// allocating a new node, which must be the size of the node containing
// the intrusive Treap struct. If given no arena, returns null when the
// key is not found.
static void *treapinsert(Arena *a, Treap **t, Str key, Size size)
{
// Traverse down to a matching node or its leaf location
Treap *parent = 0;
Treap **target = t;
while (*target) {
parent = *target;
switch (orderstr(key, parent->key)) {
case -1: target = parent->child + 0; break;
case 0: return parent;
case +1: target = parent->child + 1; break;
}
}
// None found, insert a new leaf
if (!a) {
return 0; // "only browsing, thanks"
}
Treap *node = (Treap *)zalloc(a, size);
node->key = key;
node->parent = parent;
*target = node;
// Randomly rotate the tree according to the hash
Size keyhash = hash(key);
while (node->parent && hash(node->parent->key)<keyhash) {
parent = node->parent;
// Swap places with parent, also updating grandparent
node->parent = parent->parent;
parent->parent = node;
if (node->parent) {
int i = node->parent->child[0] == parent;
node->parent->child[!i] = node;
} else {
*t = node;
}
// Move the opposing child to the ex-parent
int i = parent->child[0] == node;
parent->child[!i] = node->child[i];
if (node->child[i]) {
node->child[i]->parent = parent;
}
node->child[i] = parent;
}
return node;
}
typedef struct {
Str buf;
Str avail;
Arena *a;
int fd;
} Out;
// Buffered output for os_write().
static Out newoutput(Arena *a, int fd, Size len)
{
Str buf = newstr(a, len);
Out out = {buf, buf, 0, fd};
return out;
}
static Out newnullout(void)
{
Out out = {0};
out.fd = -1;
return out;
}
// Output to a dynamically-grown arena buffer. The arena cannot be used
// again until this buffer is finalized.
static Out newmembuf(Arena *a)
{
Str max = maxstr(a);
Out out = {max, max, a, 0};
return out;
}
// Close the stream and release the arena, returning the result buffer.
static Str finalize(Out *out)
{
ASSERT(!out->fd);
Size len = out->buf.len - out->avail.len;
out->a->off -= out->buf.len;
return newstr(out->a, len);
}
static void flush(Out *out)
{
ASSERT(out->fd);
if (out->buf.len != out->avail.len) {
Str fill = {out->buf.s, out->buf.len-out->avail.len};
os_write(out->fd, fill);
out->avail = out->buf;
}
}
static void outstr(Out *out, Str s)
{
if (out->fd == -1) {
return; // /dev/null
}
if (out->fd == 0) {
// Output to a memory buffer, not a stream
if (out->avail.len < s.len) {
oom();
}
out->avail = copy(out->avail, s);
return;
}
// Copy into the stream buffer
while (s.len) {
if (out->avail.len >= s.len) {
out->avail = copy(out->avail, s);
s.len = 0;
} else if (out->buf.len==out->avail.len && s.len>=out->buf.len) {
os_write(out->fd, s);
s.len = 0;
} else {
Size len = out->avail.len;
Str head = takehead(s, len);
s = cuthead(s, len);
out->avail = copy(out->avail, head);
flush(out);
}
}
}
static void outbyte(Out *out, Byte b)
{
Str s = {&b, 1};
outstr(out, s);
}
typedef struct Var {
Treap node;
Str value;
} Var;
typedef struct {
Treap *vars;
} Env;
// Return a pointer to the binding so that the caller can choose to fill
// it. The arena is optional. If given, the binding will be created and
// set to a null string. An unallocated, zero-initialized environment is
// a valid empty environment.
static Str *insert(Arena *a, Env *e, Str name)
{
Var *var = (Var *)treapinsert(a, &e->vars, name, SIZEOF(*var));
return var ? &var->value : 0;
}
// Try to find the binding in the global environment, then failing that,
// the second environment. Returns a null string if no entry was found.
// An unallocated, zero-initialized environment is valid for lookups.
static Str lookup(Env *global, Env *env, Str name)
{
Str *s = insert(0, global, name);
if (s) {
return *s;
}
s = insert(0, env, name);
if (s) {
return *s;
}
Str r = {0};
return r;
}
static Str dirname(Str path)
{
Size len = path.len;
while (len>0 && !pathsep(path.s[--len])) {}
return takehead(path, len);
}
static Str basename(Str path)
{
Size len = path.len;
for (; len>0 && !pathsep(path.s[len-1]); len--) {}
return taketail(path, path.len-len);
}
static Str buildpath(Arena *a, Str dir, Str pc)
{
Str sep = S("/");
Str suffix = S(".pc\0");
Size pathlen = dir.len + sep.len + pc.len + suffix.len;
Str path = newstr(a, pathlen);
Str p = path;
p = copy(p, dir);
p = copy(p, sep);
p = copy(p, pc);
copy(p, suffix);
return path;
}
typedef enum {Pkg_DIRECT=1<<0, Pkg_PUBLIC=1<<1} PkgFlags;
typedef struct Pkg {
Treap node;
struct Pkg *list; // total load order list
Str path;
Str realname;
Str contents;
Env env;
int flags;
#define PKG_NFIELDS 10
Str name;
Str description;
Str url;
Str version;
Str requires;
Str requiresprivate;
Str conflicts;
Str libs;
Str libsprivate;
Str cflags;
} Pkg;
static Str *fieldbyid(Pkg *p, int id)
{
ASSERT(id >= 0);
ASSERT(id < PKG_NFIELDS);
return &p->name + id;
}
static Str *fieldbyname(Pkg *p, Str name)
{
static const unsigned char offs[] = {0,4,15,18,25,25,41,50,50,62};
static const unsigned char lens[] = {4,11,3,7,8,16,9,4,12,6};
static const Byte fields[] =
"Name" "Description" "URL" "Version" "Requires.private"
"Conflicts" "Libs.private" "Cflags";
for (int i = 0; i < COUNTOF(offs); i++) {
Str field = {(Byte *)fields+offs[i], lens[i]};
if (equals(field, name)) {
return fieldbyid(p, i);
}
}
return 0;
}
typedef struct {
Treap *pkgs;
Pkg *head, *tail;
Size count;
} Pkgs;
// Locate a previously-loaded package, or allocate zero-initialized
// space in the set for a new package.
static Pkg *locate(Arena *a, Pkgs *t, Str realname)
{
Pkg *p = (Pkg *)treapinsert(a, &t->pkgs, realname, SIZEOF(*p));
if (!p->realname.s) {
t->count++;
p->realname = realname;
if (!t->head) {
t->head = t->tail = p;
} else {
t->tail->list = p;
t->tail = p;
}
}
return p;
}
typedef enum {
Parse_OK,
Parse_DUPFIELD,
Parse_DUPVARABLE
} ParseStatus;
typedef struct {
Pkg pkg;
Str dupname;
ParseStatus status;
} ParseResult;
// Return the number of escape bytes at the beginning of the input.
static Size escaped(Str s)
{
if (startswith(s, S("\\\n"))) {
return 2;
}
if (startswith(s, S("\\\r\n"))) {
return 3;
}
return 0;
}
// Return a copy of the input with the escapes squashed out.
static Str stripescapes(Arena *a, Str s)
{
Size len = 0;
Str c = newstr(a, s.len);
for (Size i = 0; i < s.len; i++) {
Byte b = s.s[i];
if (b == '\\') {
Size r = escaped(cuthead(s, i));
if (r) {
i += r - 1;
} else if (i<s.len-1 && s.s[i+1]=='#') {
// do not store escape
} else {
c.s[len++] = b;
}
} else {
c.s[len++] = b;
}
}
return takehead(c, len);
}
static ParseResult parsepackage(Arena *a, Str src)
{
Byte *p = src.s;
Byte *e = src.s + src.len;
ParseResult result = {0};
result.status = Parse_OK;
result.pkg.contents = src;
while (p < e) {
for (; p<e && whitespace(*p); p++) {}
if (p<e && *p=='#') {
while (p<e && *p++!='\n') {}
continue;
}
Byte *beg = p;
Byte *end = p;
Byte c = 0;
while (p < e) {
c = *p++;
if (c=='\n' || c=='=' || c==':') {
break;
}
end = whitespace(c) ? end : p;
}
Str name = fromptrs(beg, end);
Str *field = 0;
switch (c) {
default:
continue;
case '=':
field = insert(a, &result.pkg.env, name);
if (field->s) {
ParseResult dup = {0};
dup.dupname = name;
dup.status = Parse_DUPVARABLE;
return dup;
}
break;
case ':':
field = fieldbyname(&result.pkg, name);
if (field && field->s) {
ParseResult dup = {0};
dup.dupname = name;
dup.status = Parse_DUPFIELD;
return dup;
}
break;
}
// Skip leading space; newlines may be escaped with a backslash
while (p < e) {
if (*p == '\\') {
Size r = escaped(fromptrs(p, e));
if (r) {
p += r;
} else {
break;
}
} else if (*p=='\n' || !whitespace(*p)) {
break;
} else {
p++;
}
}
Bool cleanup = 0;
end = beg = p;
for (; p<e && *p!='\n'; p++) {
if (*p == '#') {
while (p<e && *p++!='\n') {}
break;
} else if (*p == '\\') {
if (p<e-1 && p[1]=='#') {
// Escaped #, include in token and skip over
p++;
end = p + 1;
cleanup = 1;
}
Size r = escaped(fromptrs(p, e));
if (r) {
// Escaped newline, skip over
p += r - 1;
cleanup = 1;
}
} else {
end = whitespace(*p) ? end : p+1;
}
}
if (field) {
*field = fromptrs(beg, end);
if (cleanup) {
// Field contains excess characters. Contents must be
// modified, so save a copy of it instead.
*field = stripescapes(a, *field);
}
}
}
return result;
}
static void missing(Out *err, Str option)
{
outstr(err, S("pkg-config: "));
outstr(err, S("argument missing for -"));
outstr(err, option);
outbyte(err, '\n');
flush(err);
os_fail();
}
typedef struct {
Size nargs;
Str *args;
Size index;
} OptionParser;
static OptionParser newoptionparser(Str *args, Size nargs)
{
OptionParser r = {nargs, args, 0};
return r;
}
typedef struct {
Str arg;
Str value;
Bool isoption;
Bool ok;
} OptionResult;
static OptionResult nextoption(OptionParser *p)
{
if (p->index == p->nargs) {
OptionResult r = {0};
return r;
}
Str arg = p->args[p->index++];
if (arg.len<2 || arg.s[0]!='-') {
OptionResult r = {0};
r.arg = arg;
r.ok = 1;
return r;
}
OptionResult r = {0};
r.isoption = 1;
r.ok = 1;
arg = cuthead(arg, 1);
Cut c = cut(arg, '=');
if (c.ok) {
r.arg = c.head;
r.value = c.tail;
} else {
r.arg = arg;
}
return r;
}
static Str getargopt(Out *err, OptionParser *p, Str option)
{
if (p->index == p->nargs) {
missing(err, option);
}
return p->args[p->index++];
}
static void usage(Out *out)
{
static const char usage[] =
"u-config " VERSION " https://github.com/skeeto/u-config\n"
"free and unencumbered software released into the public domain\n"
"usage: pkg-config [OPTIONS...] [PACKAGES...]\n"
" --cflags, --cflags-only-I, --cflags-only-other\n"
" --define-prefix, --dont-define-prefix\n"
" --define-variable=NAME=VALUE, --variable=NAME\n"
" --exists, --validate\n"
" --keep-system-cflags, --keep-system-libs\n"
" --libs, --libs-only-L, --libs-only-l, --libs-only-other\n"
" --maximum-traverse-depth=N\n"
" --modversion\n"
" --msvc-syntax\n"
" --newlines\n"
" --static\n"
" --with-path=PATH\n"
" -h, --help, --version\n"
"environment:\n"
" PKG_CONFIG_PATH\n"
" PKG_CONFIG_LIBDIR\n"
" PKG_CONFIG_TOP_BUILD_DIR\n"
" PKG_CONFIG_SYSTEM_INCLUDE_PATH\n"
" PKG_CONFIG_SYSTEM_LIBRARY_PATH\n";
outstr(out, S(usage));
}
typedef struct StrListNode {
struct StrListNode *next;
Str entry;
} StrListNode;
typedef struct {
StrListNode *head;
StrListNode *tail;
} StrList;
static void append(Arena *a, StrList *list, Str str)
{
StrListNode *node = (StrListNode *)alloc(a, SIZEOF(*node));
node->next = 0;
node->entry = str;
if (list->tail) {
ASSERT(list->head);
list->tail->next = node;
} else {
ASSERT(!list->tail);
list->head = node;
}
list->tail = node;
}
typedef struct {
StrList list;
Byte delim;
} Search;
static Search newsearch(Byte delim)
{
Search r = {0};
r.delim = delim;
return r;
}
static void appendpath(Arena *a, Search *dirs, Str path)
{
while (path.len) {
Cut c = cut(path, dirs->delim);
Str dir = c.head;
if (dir.len) {
append(a, &dirs->list, dir);
}
path = c.tail;
}
}
static void prependpath(Arena *a, Search *dirs, Str path)
{
if (!dirs->list.head) {
// Empty, so appending is the same a prepending
appendpath(a, dirs, path);
} else {
// Append to an empty Search, then transplant in front
Search temp = newsearch(dirs->delim);
appendpath(a, &temp, path);
temp.list.tail->next = dirs->list.head;
dirs->list.head = temp.list.head;
}
}
static Bool realnameispath(Str realname)
{
return realname.len>3 && equals(taketail(realname, 3), S(".pc"));
}
static Str pathtorealname(Str path)
{
if (!realnameispath(path)) {
return path;
}
Size baselen = 0;
for (Size i = 0; i < path.len; i++) {
if (pathsep(path.s[i])) {
baselen = i + 1;
}
}
Str name = cuthead(path, baselen);
return cuttail(name, 3);
}
static Str readpackage(Arena *a, Out *err, Str path, Str realname)
{
if (equals(realname, S("pkg-config"))) {
return S(
"Name: u-config\n"
"Version: " VERSION "\n"
"Description:\n"
);
}
Str null = {0};
MapFileResult m = os_mapfile(a, path);
switch (m.status) {
case MapFile_NOTFOUND:
return null;
case MapFile_READERR:
outstr(err, S("pkg-config: "));
outstr(err, S("could not read package '"));
outstr(err, realname);
outstr(err, S("' from '"));
outstr(err, path);
outstr(err, S("'\n"));
flush(err);
os_fail();
case MapFile_OK:
return m.contents;
}
ASSERT(0);
return null;
}
static void expand(Out *out, Out *err, Env *global, Pkg *p, Str str)
{
int top = 0;
Str stack[128];
stack[top] = str;
while (top >= 0) {
Str s = stack[top--];
for (Size i = 0; i < s.len-1; i++) {
if (s.s[i]=='$' && s.s[i+1]=='{') {
if (top >= COUNTOF(stack)-2) {
outstr(err, S("pkg-config: "));
outstr(err, S("exceeded max recursion depth in '"));
outstr(err, p->path);
outstr(err, S("'\n"));
flush(err);
os_fail();
}
Str head = {s.s, i};
outstr(out, head);
Size beg = i + 2;
Size end = beg;
for (; end<s.len && s.s[end]!='}'; end++) {}