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
|
use std
use "types"
use "util"
use "opts"
use "syssel"
pkg bld =
const mkbuild : (tags : byte[:][:], resolve : bool -> build#)
const load : (b : build# -> bool)
$noret const failparse : (p : parser#, msg : byte[:], args : ... -> void)
;;
type parser = struct
/* parse input */
data : byte[:]
rest : byte[:]
fname : byte[:]
fdir : byte[:] /* directory relative to base */
basedir : byte[:]
line : int
/* all targets */
targs : targ[:]
/* default parameter values */
incpath : byte[:][:]
libdeps : byte[:][:]
tstdeps : byte[:][:]
runtime : byte[:]
ldscript: byte[:]
install : bool
/* extracted data for further parsing */
subdirs : byte[:][:]
targsel : syssel((byte[:], targ))#
;;
const mkbuild = {tags, resolve
var b
b = std.zalloc()
b.libs = std.mkht()
b.proc = std.mkht()
b.targs = std.mkht()
b.tags = std.mkht()
b.resolve = resolve
b.deps = std.mk([
.targs = std.mkht(),
.gen = std.mkht(),
.leaves = [][:],
.nodes = [][:],
])
bld.addsysattrs(b, tags)
-> b
}
const load = {b
var ok, sel
var targs
sel = mksyssel(b, "mbld", 0, "mbld")
ok = loadall(b, "bld.proj", "", sel, true)
targs = sysselfin(sel)
for (name, targ) : targs
std.slpush(&b.all, name)
std.put("targ name: {}\n", name)
std.htput(b.targs, name, targ)
;;
if b.resolve
if ok
ok = sortdeps(b)
;;
std.slfree(targs)
;;
-> ok
}
const sortdeps = {b
var looped
var marked
var all
all = [][:]
looped = std.mkht()
marked = std.mkht()
for dep : b.all
match gettarg(b.targs, dep)
| `Bin _: all = visit(all, b, "all", dep, looped, marked)
| `Lib _: all = visit(all, b, "all", dep, looped, marked)
| targ: std.slpush(&all, dep)
;;
;;
std.htfree(marked)
std.htfree(looped)
std.slfree(b.all)
b.all = all
-> true
}
const visit = {all, b, parent, targ, looped, marked
if std.hthas(looped, targ)
std.fatal("cycle in build depgraph involving {}\n", targ)
elif std.hthas(marked, targ)
-> all
;;
std.htput(looped, targ, true)
for (dir, lib, dep) : getdeps(b, parent, targ)
all = visit(all, b, targ, dep, looped, marked)
;;
std.htdel(looped, targ)
std.htput(marked, targ, true)
-> std.slpush(&all, targ)
}
const getdeps = {b, parent, targname
match std.htget(b.targs, targname)
| `std.Some targ:
match targ
| `Bin t: -> t.libdeps
| `Lib t: -> t.libdeps
| _: std.fatal("{} depends on non-library target", parent)
;;
| `std.None:
std.fatal("{}: could not find dependency {}\n", parent, targname)
;;
}
/* recursively load all bld.{proj,sub} files in project */
const loadall = {b, path, dir, sel, isroot
var p : parser#
var subbld, subproj, ok
var curbase
p = mkparser(path, dir, b.basedir, sel)
ok = bld.parse(b, p, "")
for targ : p.targs
match targ
| `Bin t: setmyropt(p, t)
| `Lib t: setmyropt(p, t)
| _: /* only myr targets have settable defaults so far */
;;
;;
for sub : p.subdirs
subbld = std.pathcat(sub, "bld.sub")
subproj = std.pathcat(sub, "bld.proj")
/*
bld.sub is a subbuild. It doesn't change the
build root.
*/
if std.fexists(subbld)
loadall(b, subbld, sub, sel, false)
/*
bld.proj reroots the project -- @/
is relative to the most recent bld.proj
in the heirarchy.
*/
elif std.fexists(subproj)
curbase = b.basedir
b.basedir = sub
loadall(b, subproj, sub, sel, false)
b.basedir = curbase
else
std.fatal("could not open {} or {} \n", subbld, subproj)
;;
std.slfree(subbld)
std.slfree(subproj)
;;
if isroot
match std.diropen(opt_depsdir)
| `std.Err _: /* no deps to load */
| `std.Ok depdir:
for ent : std.byentry(depdir)
subbld = std.pathjoin([opt_depsdir, ent, "bld.sub"][:])
subproj = std.pathjoin([opt_depsdir, ent, "bld.proj"][:])
if std.fexists(subbld)
loadall(b, subbld, std.dirname(subbld), sel, false)
elif std.fexists(subproj)
curbase = b.basedir
b.basedir = std.dirname(subproj)
loadall(b, subproj, std.dirname(subproj), sel, false)
b.basedir = curbase
;;
std.slfree(subbld)
std.slfree(subproj)
;;
;;
;;
freeparser(p)
-> ok
}
const setmyropt = {p, t
var libdep, tstdep
t.install = t.install && p.install
if t.runtime.len == 0
t.runtime = p.runtime
;;
if t.ldscript.len == 0
t.ldscript = p.ldscript
;;
std.sljoin(&t.incpath, p.incpath)
for l : p.libdeps
libdep = libpath(p, l)
std.slpush(&t.libdeps, libdep)
;;
for l : p.tstdeps
tstdep = libpath(p, l)
std.slpush(&t.tstdeps, tstdep)
;;
}
const parse = {b, p, path
while true
if !target(b, p)
break
;;
;;
skipspace(p)
if p.rest.len > 0
failparse(p, "junk in file near {}\n", p.rest[:std.min(p.rest.len, 10)])
-> false
;;
-> true
}
const target = {b, p
match word(p)
/* targets */
| `std.Some "bin": bintarget(b, p)
| `std.Some "lib": libtarget(b, p)
| `std.Some "test": testtarget(b, p)
| `std.Some "bench": benchtarget(b, p)
| `std.Some "gen": cmdtarget(b, p, "gen", false)
| `std.Some "cmd": cmdtarget(b, p, "cmd", true)
| `std.Some "data": datatarget(b, p, "data")
| `std.Some "man": mantarget(b, p)
| `std.Some "sub": subtarget(b, p)
/* global attributes */
| `std.Some "incpath": incpath(b, p, &p.incpath, "incpath")
| `std.Some "libdeps": incpath(b, p, &p.libdeps, "libdeps")
| `std.Some "testdeps": incpath(b, p, &p.tstdeps, "testdeps")
| `std.Some "runtime": p.runtime = expectword(b, p, "runtime")
| `std.Some "ldscript": p.runtime = expectword(b, p, "ldscript")
| `std.Some "noinst": p.install = false
/* no word */
| `std.Some targtype: failparse(p, "unknown keyword {}\n", targtype)
| `std.None: -> false
;;
-> true
}
const mkparser = {path, dir, basedir, sel
var p
p = std.mk([
.line = 1,
.fname = std.sldup(path),
.fdir = std.sldup(dir),
.basedir = std.sldup(basedir),
.targsel = sel,
.incpath = [][:],
.libdeps = [][:],
.tstdeps = [][:],
.runtime = "",
.ldscript = "",
.install = true,
])
match std.slurp(path)
| `std.Ok d: p.data = d
| `std.Err _: std.fatal("could not open '{}'\n", path)
;;
p.rest = p.data
-> p
}
const freeparser = {p
std.slfree(p.fname)
std.slfree(p.fdir)
std.slfree(p.basedir)
std.slfree(p.subdirs)
std.slfree(p.data)
std.free(p)
}
/* <name>: '=' wordlist ';;' */
const incpath = {b, p, words, name
skipspace(p)
if !matchc(p, '=')
failparse(p, "expected '=' after {}\n", name)
;;
match wordlist(p)
| `std.Some path: words# = path
| `std.None:
;;
skipspace(p)
if !matchc(p, ';') || !matchc(p, ';')
failparse(p, "expected ';;' after {} list\n", name)
;;
}
const expectword = {b, p, attr
match word(p)
| `std.Some w: -> w
| `std.None: failparse(p, "expected word after {}\n", attr)
;;
}
/* bintarget: myrtarget */
const bintarget = {b, p
var t
t = myrtarget(b, p, "bin")
addtarg(p, b, t.name, t.tags, `Bin t)
}
/* libtarget: myrtarget */
const libtarget = {b, p
var t
t = myrtarget(b, p, "lib")
t.islib = true
addtarg(p, b, t.name, t.tags, `Lib t)
}
/* testtarget: myrtarget */
const testtarget = {b, p
var t
t = myrtarget(b, p, "test")
t.istest = true
t.install = false
addtarg(p, b, t.name, t.tags, `Bin t)
}
/* benchtarget: myrtarget */
const benchtarget = {b, p
var t
t = myrtarget(b, p, "bench")
t.isbench = true
t.install = false
addtarg(p, b, t.name, t.tags, `Bin t)
}
/* mantarget: anontarget */
const mantarget = {b, p
var targ
targ = std.mk([
.dir=std.sldup(p.fdir),
.pages=anontarget(p, "man")
])
addtarg(p, b, "__man__", [][:], `Man targ)
}
/* subtarget : anontarget */
const subtarget = {b, p
var subs
subs = anontarget(p, "sub")
for s : subs
std.slpush(&p.subdirs, std.pathcat(p.fdir, s))
;;
}
/* gentarget: wordlist {attrs} = wordlist ;; */
const cmdtarget = {b, p, cmd, iscmd
var outlist, deplist, cmdlist, tags
var durable, istest, isbench
var attrs
var gt
match wordlist(p)
| `std.None: failparse(p, "{} target missing output files\n", cmd)
| `std.Some out:
if iscmd && out.len != 1
failparse(p, "{} target takes one name\n", cmd)
;;
outlist = out
;;
match attrlist(p)
| `std.Some al: attrs = al
| `std.None: failparse(p, "invalid attr list for {}\n", outlist[0])
;;
skipspace(p)
if !matchc(p, '=')
failparse(p, "expected '=' after '{} {}'\n", cmd, outlist[outlist.len-1])
;;
match wordlist(p)
| `std.None: failparse(p, "{} target missing command\n", cmd)
| `std.Some cl:
cmdlist = cl
;;
if !matchc(p, ';') || !matchc(p, ';')
failparse(p, "expected ';;' terminating genfile command, got {}\n", peekc(p))
;;
durable = false
istest = false
isbench = false
deplist = [][:]
tags = [][:]
for elt : attrs
match elt
| ("durable", ""): durable = true
| ("test", ""): istest = true
| ("bench", ""): isbench = true
| ("notest", ""): istest = false
| ("dep", depname): std.slpush(&deplist, depname)
| ("tag", tag): std.slpush(&tags, tag)
| (attr, ""):
failparse(p, "attribute {} not valid on {} targets\n", attr, cmd)
| (attr, val):
failparse(p, "attribute {}={} not valid on {} targets\n", attr, val, cmd)
;;
;;
gt = std.mk([
.dir=std.sldup(p.fdir),
.gen=outlist,
.durable=durable,
.istest=istest,
.isbench=isbench,
.deps=deplist,
.cmd=cmdlist,
.tags=tags,
])
for o : outlist
if iscmd
addtarg(p, b, o, gt.tags, `Cmd gt)
else
addtarg(p, b, o, gt.tags, `Gen gt)
;;
;;
}
/*
myrtarget: name '=' inputlist ';;'
| name attrlist = inputlist ';;'
*/
const myrtarget = {b, p, targ
var ldscript, runtime, install, incpath, tags
var libdeps, name, inputs, attrs
var istest, isbench
var fsel
match word(p)
| `std.Some n: name = n
| `std.None: failparse(p, "expected target name after '{}'\n", targ)
;;
match attrlist(p)
| `std.Some al: attrs = al
| `std.None: failparse(p, "invalid attr list for {} {}\n", targ, name)
;;
skipspace(p)
if !matchc(p, '=')
failparse(p, "expected '=' after '{} {}'\n", targ, name)
;;
fsel = mksyssel(b, p.fname, p.line, name)
match inputlist(p)
| `std.Some (wl, libs):
libdeps = libs
for w : wl
sysseladd(fsel, w)
;;
inputs = sysselfin(fsel)
std.slfree(wl)
| `std.None: failparse(p, "expected list of file names after '{} {}'\n", targ, name)
;;
skipspace(p)
if !matchc(p, ';') || !matchc(p, ';')
failparse(p, "expected ';;' terminating input list, got {}\n", peekc(p))
;;
install = true
ldscript = ""
runtime = ""
incpath = [][:]
tags = [][:]
istest = false
isbench = false
for elt : attrs
match elt
| ("ldscript", lds): ldscript = std.sldup(lds)
| ("runtime", rt): runtime = std.sldup(rt)
| ("inc", path): std.slpush(&incpath, std.sldup(path))
| ("tag", tag): std.slpush(&tags, tag)
| ("inst", ""): install = true
| ("noinst", ""): install = false
| ("test", ""): istest = true
| ("bench", ""): isbench = true
| ("notest", ""): istest = false
| (invalid, ""): std.fatal("{}: invalid attr '{}'\n", targ, invalid)
| (invalid, attr): std.fatal("{}: invalid attr '{} = {}'\n", targ, invalid, attr)
;;
;;
std.sljoin(&incpath, bld.opt_incpaths)
-> std.mk([
/* target */
.dir=std.sldup(p.fdir),
.name=name,
.inputs=inputs,
.libdeps=libdeps,
.islib=false,
.istest=istest,
.isbench=isbench,
.isdyn=false,
/* attrs */
.tags=tags,
.install=install,
.ldscript=ldscript,
.runtime=runtime,
.incpath=incpath,
])
}
const datatarget = {b, p, targ
var name, attrs, blobs
var tags, path
match word(p)
| `std.Some n: name = n
| `std.None: failparse(p, "expected target name after '{}'\n", targ)
;;
skipspace(p)
attrs = [][:]
match attrlist(p)
| `std.Some al: attrs = al
| `std.None: failparse(p, "invalid attr list for {} {}\n", targ, name)
;;
skipspace(p)
if !matchc(p, '=')
failparse(p, "expected '=' after '{} {}'\n", targ, name)
;;
match wordlist(p)
| `std.Some wl: blobs = wl
| `std.None: failparse(p, "expected file list after '{} {}'\n", targ, name)
;;
skipspace(p)
if !matchc(p, ';') || !matchc(p, ';')
failparse(p, "expected ';;' after input list, got {}\n", peekc(p))
;;
tags = [][:]
for elt : attrs
match elt
| ("tag", tag): std.slpush(&tags, tag)
| ("path", pathdir): path = pathdir
| (tag, ""):
std.fatal("{}: invalid attr '{}'\n", targ, tag)
| (tag, attr):
std.fatal("{}: invalid attr '{}={}'\n", targ, tag, attr)
;;
;;
addtarg(p, b, name, tags, `Data std.mk([
.dir=std.sldup(p.fdir),
.name=name,
.tags=tags,
.path=path,
.blobs=blobs,
]))
}
/* anontarget: '=' wordlist ';;' */
const anontarget = {p, targ
var inputs
inputs = [][:]
skipspace(p)
if !matchc(p, '=')
failparse(p, "expected '=' after '{}' target\n", targ)
;;
match wordlist(p)
| `std.Some wl: inputs = wl
| `std.None: failparse(p, "expected file list after '{}' target\n", targ)
;;
skipspace(p)
if !matchc(p, ';') || !matchc(p, ';')
failparse(p, "expected ';;' terminating input list\n")
;;
-> inputs
}
/*
attrlist: EMPTY |'{' attrs '}'
attrs : EMPTY
| attrs attr
attr : name
| name '=' name
*/
const attrlist = {p
var al
al = [][:]
skipspace(p)
if !matchc(p, '{')
-> `std.Some al
;;
while true
match word(p)
| `std.Some k:
if matchc(p, '=')
match word(p)
| `std.Some v:
std.slpush(&al, (k, v))
| `std.None:
failparse(p, "invalid attr in attribute list\n")
;;
else
std.slpush(&al, (k, [][:]))
;;
if !matchc(p, ',')
break
;;
| `std.None: break
;;
;;
if !matchc(p, '}')
failparse(p, "expected '}' at end of attr list, got '{}'\n", peekc(p))
;;
if al.len == 0
-> `std.None
else
-> `std.Some al
;;
}
/*
inputlist: EMPTY
| inputlist input
input : word
| "lib" word
*/
const inputlist = {p
var dir, lib, targ
var wl, libs
wl = [][:]
libs = [][:]
while true
match word(p)
| `std.Some "lib":
match word(p)
| `std.Some l:
(dir, lib, targ) = libpath(p, l)
std.slpush(&libs, (dir, lib, targ))
| `std.None:
failparse(p, "expected lib name after 'lib'\n")
;;
| `std.Some w: std.slpush(&wl, w)
| `std.None: break
;;
;;
if wl.len == 0
-> `std.None
else
-> `std.Some (wl, libs)
;;
}
/* wordlist: EMPTY | wordlist word */
const wordlist = {p
var wl
wl = [][:]
while true
match word(p)
| `std.Some w: std.slpush(&wl, w)
| `std.None: break
;;
;;
if wl.len == 0
-> `std.None
else
-> `std.Some wl
;;
}
/* word: /wordchar*/
/* word: /wordchar*/
const word = {p
var c, n
var start
skipspace(p)
c = peekc(p)
if c == '"'
n = 0
getc(p)
start = p.rest
while p.rest.len > 0
c = peekc(p)
if c == '"'
getc(p)
goto done
elif c == '\\'
c = getc(p)
;;
getc(p)
n += std.charlen(c)
;;
failparse(p, "input ended within quoted word\n")
else
n = 0
start = p.rest
while p.rest.len > 0
c = peekc(p)
if wordchar(c)
getc(p)
n += std.charlen(c)
else
break
;;
;;
;;
:done
if n > 0
-> `std.Some std.sldup(start[:n])
else
-> `std.None
;;
}
const wordchar = {c
-> std.isalnum(c) || \
c == '.' || c == '_' || c == '$' || c == '-' || \
c == '/' || c == '@' || c == '!' || c == '~' || \
c == '+' || c == '%' || c == '&' || c == '(' || \
c == ')' || c == '[' || c == ']' || c == ':'
}
const skipspace = {p
var c, r
r = p.rest
while r.len > 0
c = peekc(p)
match c
| ' ': getc(p)
| '\t': getc(p)
| '\n':
getc(p)
p.line++
| '#':
while p.rest.len > 0 && peekc(p) != '\n'
getc(p)
;;
| _:
break
;;
;;
}
const matchc = {p, c
var chr, s
/* safe to use at eof: charstep returns (-1, "") */
(chr, s) = std.charstep(p.rest)
if c == chr
p.rest = s
-> true
else
-> false
;;
}
const peekc = {p
-> std.decode(p.rest)
}
const getc = {p
var c, s
(c, s) = std.charstep(p.rest)
p.rest = s
-> c
}
const addtarg = {p, b, name, tags, targ
var tn
tn = std.fmt("{}:{}", p.fdir, name)
sysseladdlist(p.targsel, tn, tags, (tn, targ))
std.slpush(&p.targs, targ)
}
const libpath = {p, libpath
var dir, lib, targ
match std.strrfind(libpath, ":")
| `std.None:
dir = std.sldup(p.fdir)
lib = std.sldup(libpath)
targ = std.fmt("{}:{}", p.fdir, lib)
| `std.Some idx:
if idx == libpath.len
std.fatal("libdep {} missing library after ':'\n")
;;
/* absolute path */
if std.hasprefix(libpath, "@deps/")
dir = std.pathcat(opt_depsdir, libpath[6:idx])
lib = std.sldup(libpath[idx+1:])
targ = std.pathcat(".deps", libpath[6:])
elif std.hasprefix(libpath, "@/") || std.hasprefix(libpath, "@:")
dir = std.sldup(libpath[2:idx])
lib = std.sldup(libpath[idx+1:])
targ = std.sldup(libpath[2:])
/* relative path */
else
dir = std.pathcat(p.fdir, libpath[:idx])
lib = std.sldup(libpath[idx+1:])
targ = std.pathcat(p.fdir, libpath)
if std.hasprefix(targ, "../")
std.fatal("library {} outside of project\n", libpath)
;;
;;
;;
-> (dir, lib, targ)
}
$noret const failparse = {p, msg, args : ...
var buf : byte[1024]
var ap
var sl
ap = std.vastart(&args)
sl = std.bfmtv(buf[:], msg, &ap)
std.fput(1, "{}:{}: {}", p.fname, p.line, sl)
std.exit(1)
}
|