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
|
use std
use regex
use bio
use "config.use"
use "opts.use"
use "types.use"
use "util.use"
pkg bld =
const myrdeps : (b : build#, mt : myrtarg#, doclean : bool, addsrc : bool -> depgraph#)
;;
const Abiversion = 8
var usepat : regex.regex#
var cflagpat : regex.regex#
var clibpat : regex.regex#
const __init__ = {
usepat = std.try(regex.compile("^\\s*use\\s+((\\<\\S+\\>)|\"(\\S+)\").*"))
cflagpat = std.try(regex.compile("/\\*\\s*CFLAGS:\\s*(.*)\\s*\\*/"))
clibpat = std.try(regex.compile("/\\*\\s*LIBS:\\s*(.*)\\s*\\*/"))
}
type dep = union
`Local (byte[:], int)
`Lib (byte[:], int)
;;
type depscan = struct
doclean : bool
addsrc : bool
tagsel : std.htab(byte[:], byte[:])#
targ : myrtarg#
incs : byte[:][:]
depstk : byte[:][:]
;;
const myrdeps = {b, mt, doclean, addsrc
var objs, uses, srcs
var cflags, libs
var out, useout
var dg : depgraph#
var ds : depscan
var i
dg = std.mk([
.deps = std.mkht(std.strhash, std.streq),
.libs = std.mkht(std.strhash, std.streq),
.input = std.mkht(std.strhash, std.streq),
.sources = std.mkht(std.strhash, std.streq),
.updated = std.mkht(std.strhash, std.streq),
.seen = std.mkht(std.strhash, std.streq),
.done = std.mkht(std.strhash, std.streq),
.cflags = std.mkht(std.strhash, std.streq),
.extlibs = [][:],
.dynamic = false,
])
/* direct dependencies of binary */
if mt.islib
out = std.fmt("lib{}.a", mt.name)
useout = std.sldup(mt.name)
else
out = std.sldup(mt.name)
useout = ""
;;
ds = [
.doclean = doclean,
.addsrc = addsrc,
.incs = mt.incpath,
.targ = mt,
.depstk = [][:],
]
srcs = mt.inputs
objs = swapall(srcs, config.Objsuffix)
uses = swapall(srcs, ".use")
for i = 0; i < srcs.len; i++
std.htput(dg.input, objs[i], srcs[i])
std.htput(dg.sources, srcs[i], true)
pushdep(dg, srcs[i], objs[i])
if std.hassuffix(srcs[i], ".myr")
std.htput(dg.input, uses[i], srcs[i])
pushdep(dg, srcs[i], uses[i])
elif std.hassuffix(srcs[i], ".glue.c")
(cflags, libs) = scrapecflags(b, dg, srcs[i])
std.htput(dg.cflags, srcs[i], cflags)
dg.extlibs = std.sljoin(dg.extlibs, libs)
dg.dynamic = true
;;
;;
for i = 0; i < srcs.len; i++
pushdep(dg, objs[i], out)
if !std.hassuffix(srcs[i], ".myr")
continue
;;
if mt.islib
pushdep(dg, uses[i], useout)
;;
srcdeps(b, &ds, dg, srcs[i], objs[i], uses[i])
;;
dumpgraph(dg)
-> dg
}
const swapall = {srcs, suff
var sl
sl = [][:]
for s in srcs
sl = std.slpush(sl, srcswapsuffix(s, suff))
;;
-> sl
}
const dumpgraph = {dg
var keys
if !opt_debug
->
;;
keys = std.htkeys(dg.deps)
std.put("digraph dg {{\n")
for k in keys
for v in std.htgetv(dg.deps, k, ["WTFUNKNOWN!"][:])
std.put("\t\"{}\" -> \"{}\";\n", k, v)
;;
;;
std.put("}\n")
}
const srcdeps = {b, ds, g, path, obj, usefile
var deps
if std.hthas(g.done, path)
->
;;
ds.depstk = std.slpush(ds.depstk, path)
if std.htgetv(g.seen, path, false)
std.fput(1, "dependency loop involving {}:\n", path)
for d in ds.depstk
std.fput(1, "\t{}\n", d)
;;
std.exit(1)
;;
deps = getdeps(b, ds, path)
std.htput(g.seen, path, true)
for d in deps
match d
| `Lib (lib, lnum):
/*
If we're cleaning, we don't care about libraries; at best, this does nothing. At
worst, this will cause failure if the library is a local library that gets cleand.
*/
if !ds.doclean
scrapelibs(g, lib, ds.incs)
;;
| `Local (l, lnum):
if !std.hassuffix(l, ".use")
std.fatal("{}:{}: local dependency \"{}\" should end with .use\n", path, lnum, l)
;;
if obj.len != 0
pushdep(g, l, obj)
;;
if usefile.len != 0
pushdep(g, l, usefile)
;;
addusedep(b, ds, g, path, l, lnum)
;;
;;
ds.depstk = std.slgrow(ds.depstk, ds.depstk.len - 1)
std.htput(g.seen, path, false)
std.htput(g.done, path, true)
}
const addusedep = {b, ds, g, f, usefile, line
var src
if std.hthas(g.done, usefile)
if opt_debug
std.put("already loaded deps for {}\n", usefile)
;;
->
;;
match std.htget(g.input, usefile)
| `std.Some path:
src = std.sldup(path)
| `std.None:
src = swapsuffix(usefile, ".use", ".myr")
if ds.addsrc
std.htput(g.sources, src, true)
elif !std.hthas(g.input, usefile)
std.fatal("{}:{}: source file {} not listed in bldfile\n", f, line, src)
;;
;;
pushdep(g, src, usefile)
std.htput(g.input, usefile, src)
srcdeps(b, ds, g, src, "", usefile)
std.htput(g.done, usefile, true)
}
const scrapecflags = {b, ds, path
var cflags, libs, lnum
var f
lnum = 0
cflags = [][:]
libs = [][:]
f = opensrc(b, path)
while true
lnum++
match bio.readln(f)
| `bio.Err: std.fatal("unable to read {}\n", path)
| `bio.Eof: break
| `bio.Ok ln:
(cflags, libs) = getcflags(ln, cflags, libs)
std.slfree(ln)
;;
;;
bio.close(f)
-> (cflags, libs)
}
const getcflags = {ln, cflags, libs
var flags
match regex.exec(cflagpat, ln)
| `std.None:
| `std.Some m:
flags = std.strtok(m[1])
for fl in flags
cflags = std.slpush(cflags, std.sldup(fl))
;;
std.slfree(flags)
;;
match regex.exec(clibpat, ln)
| `std.None:
| `std.Some m:
flags = std.strtok(m[1])
for fl in flags
libs = std.slpush(libs, std.sldup(fl))
;;
std.slfree(flags)
;;
-> (cflags, libs)
}
const getdeps = {b, ds, path
var deps, lnum
var f
lnum = 0
deps = [][:]
f = opensrc(b, path)
while true
lnum++
match bio.readln(f)
| `bio.Err: std.fatal("unable to read {}\n", path)
| `bio.Eof: break
| `bio.Ok ln:
deps = depname(deps, ln, lnum)
std.slfree(ln)
;;
;;
bio.close(f)
-> deps
}
const opensrc = {b, path
if !std.fexists(path)
match std.htget(b.gensrc, path)
| `std.Some gt: run(gt.cmd)
| `std.None: std.fatal("no input file {}\n", path)
;;
;;
match bio.open(path, bio.Rd)
| `std.Fail m: std.fatal("could not open {}: {}\n", path, m)
| `std.Ok f: -> f
;;
}
const depname = {deps, ln, lnum
/*
the regex pattern does some contortions to either grab
an unquoted path and put it into uses[4], or a quoted
path, and put it (minus the quotes) into uses[2]
*/
match regex.exec(usepat, ln)
| `std.Some uses:
if uses[2].len > 0
deps = std.slpush(deps, `Lib (std.sldup(uses[2]), lnum))
else
deps = std.slpush(deps, `Local (std.sldup(uses[3]), lnum))
;;
| `std.None:
/* nothing to do */
;;
-> deps
}
const scrapelibs = {dg, lib, incs
var deps, d
var f
var done
if std.hthas(dg.libs, lib)
->
;;
f = openlib(lib, incs)
match bio.getc(f)
| `bio.Ok 'U': /* nothing */
| `bio.Ok _: std.fatal("library {}: corrupt or invalid usefile\n", lib)
| `bio.Err: std.fatal("library {}: could not read usefile\n", lib)
| `bio.Eof: std.fatal("library {}: could not read usefile\n", lib)
;;
match bio.getbe32(f)
| `bio.Ok Abiversion: /* nothing: version matches. */
| `bio.Ok v:
if v < Abiversion
std.fput(1, "library {}: warning: old abi version {}\n", lib, v)
else
std.fput(1, "library {}: usefile version {} unknown\n", lib, v)
;;
| `bio.Eof: std.fatal("library {}: corrupt or truncated usefile\n", lib)
| `bio.Err: std.fatal("library {}: error reading usefile\n", lib)
;;
std.slfree(rdstr(f))
done = false
deps = [][:]
while !done
match bio.getc(f)
| `bio.Ok 'L':
d = rdstr(f)
deps = std.slpush(deps, d)
| `bio.Ok 'X':
d = rdstr(f)
dg.extlibs = std.slpush(dg.extlibs, d)
| `bio.Ok _: done = true
| `bio.Eof: done = true
| `bio.Err: std.fatal("io error reading {}", lib)
;;
;;
bio.close(f)
std.htput(dg.libs, lib, deps)
for dep in deps
scrapelibs(dg, dep, incs)
;;
}
const openlib = {lib, incs
var path
for p in incs
path = std.pathjoin([p, lib][:])
match bio.open(path, bio.Rd)
| `std.Ok file: -> file
| `std.Fail m: /* nothing */
;;
;;
path = std.pathjoin([opt_instroot, config.Libpath, lib][:])
match bio.open(path, bio.Rd)
| `std.Ok file: -> file
| `std.Fail m: /* nothing */
;;
std.fatal("could not find library {}.\n", lib)
}
/* pushes a dep into the dependency list */
const pushdep = {dg, src, dst
var sl
if opt_debug
std.put("{} <= {}\n", dst, src)
;;
std.assert(dst.len < 200, "BUG!")
sl = std.htgetv(dg.deps, dst, [][:])
sl = std.slpush(sl, src)
std.htput(dg.deps, dst, sl)
}
const rdstr = {f
var len
var sl
match bio.getbe32(f)
| `bio.Ok l:
len = l
sl = std.slalloc(len)
| `bio.Eof: std.die("end of file while reading string")
| `bio.Err: std.die("error while reading string")
;;
bio.read(f, sl)
-> sl
}
|