summaryrefslogtreecommitdiff
path: root/mbld/deps.myr
blob: 79f90c4abeee0f52a92b9a8643ed52f575329266 (plain)
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
use std
use regex
use bio

use "config"
use "opts"
use "types"
use "util"
use "libs"

pkg bld =
	const deps	: (b : build# -> void)
	const testdeps	: (b : build# -> void)
	const resolve	: (b : build# -> void)
;;

type dep = union
	`Xdep	byte[:]
	`Ldep	byte[:]
;;

const Abiversion = 13

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*\\*/"))
}

const deps = {b
	for (name, `Gen gt) : std.byhtkeyvals(b.targs)
		cmddeps(b, name, gt)
	;;
	for name : b.all
		match gettarg(b.targs, name)
		| `Bin bt:	myrdeps(b, name, bt)
		| `Lib lt:	myrdeps(b, name, lt)
		| `Cmd ct:	cmddeps(b, name, ct)
		| `Man mt:	mandeps(b, name, mt)
		| `Data dt:	datdeps(b, name, dt)
		| `Gen gt:	/* gen was dealt with earlier */
		;;
	;;
}

const testdeps = {b
	for name : b.all
		match gettarg(b.targs, name)
		| `Bin bt:	addtests(b, name, bt)
		| `Lib lt:	addtests(b, name, lt)
		| _:		/* skip */
		;;
	;;
}

const myrdeps = {b, name, mt
	var t, p, o, u, n, to, tu
	var libs, dynlibs
	var cflags, ll
	var g, a, deps
	var gu, go

	g = b.deps
	if mt.islib
		u = std.fmt("lib{}.use", mt.name)
		o = std.fmt("lib{}.a", mt.name)
		tu = std.pathjoin([opt_objdir, mt.dir, u][:])
		to = std.pathjoin([opt_objdir, mt.dir, o][:])

		gu = node(g, tu)
		go = node(g, to)
		go.instdir = config.Libpath
		gu.instdir = config.Libpath
		go.instmod = 0o644
		gu.instmod = 0o644
		generates(g, gu, tu)
		generates(g, go, to)

		a = std.htgetv(g.targs, "all", [][:])
		std.slpush(&a, gu)
		std.slpush(&a, go)
		std.htput(g.targs, "all", a)
		std.htput(g.targs, name, std.sldup([gu, go][:]))

		std.slfree(o)
		std.slfree(u)
	else
		u = std.fmt("{}.use", mt.name)
		tu = std.pathjoin([opt_objdir, mt.dir, u][:])
		to = std.pathjoin([opt_objdir, mt.dir, mt.name][:])

		go = node(g, to)
		gu = node(g, tu)

		generates(g, go, to)
		std.htput(g.targs, name, std.sldup([go][:]))
		if mt.istest
			n = node(g, mt.name)
			depends(g, n, to)
			addnode(g, "test", n)

			n.wdir = std.sldup(mt.dir)
			std.slpush(&n.cmd, std.pathjoin([b.basedir, opt_objdir, mt.dir, mt.name][:]))
		elif mt.isbench
			n = node(g, mt.name)
			depends(g, n, to)
			addnode(g, "bench", n)

			n.wdir = std.sldup(mt.dir)
			std.slpush(&n.cmd, std.pathjoin([b.basedir, opt_objdir, mt.dir, mt.name][:]))
		else
			addnode(g, "all", go)
			go.instdir = config.Binpath
			go.instmod = 0o755
		;;
		std.slfree(u)
	;;

	libs = [][:]
	dynlibs = [][:]
	for f : mt.inputs
		p = std.pathcat(mt.dir, f)
		leaf(g, p)
		if std.hassuffix(f, ".myr")
			t = changesuffix(p, config.Objsuffix)
			o = std.pathcat(opt_objdir, t)
			std.slfree(t)

			t = changesuffix(p, ".use")
			u = std.pathcat(opt_objdir, t)
			std.slfree(t)

			depends(g, go, o)
			depends(g, gu, u)

			n = node(g, o)
			generates(g, n, o)
			generates(g, n, u)
			depends(g, n, p)

			deps = scrapedeps(b, mt, p)
			for `Ldep d : deps
				depends(g, n, d)
			;;
			for `Xdep d : deps
				scrapelib(b, mt.name, d, mt.incpath)
				xdepends(b, g, n, d)
				std.slpush(&libs, d)
			;;
			myrcmd(b, n, mt, p, false)
			std.slfree(deps)
		elif std.hassuffix(f, ".s")
			t = changesuffix(p, config.Objsuffix)
			o = std.pathcat(opt_objdir, t)
			std.slfree(t)

			depends(g, go, o)
			n = node(g, o)
			generates(g, n, o)
			depends(g, n, p)
			ascmd(b, n, mt, o, p)
		elif std.hassuffix(f, ".glue.c")
			t = changesuffix(p, config.Objsuffix)
			o = std.pathcat(opt_objdir, t)
			std.slfree(t)

			(cflags, ll) = scrapecflags(b, mt, p)
			depends(g, go, o)
			n = node(g, o)
			generates(g, n, o)
			depends(g, n, p)
			ccmd(b, n, mt, o, p, cflags)
			for l : ll
				std.slpush(&dynlibs, l)
			;;
		elif std.hassuffix(f, config.Objsuffix)
			depends(g, go, p)
		else
			std.fatal("don't know how to build {}/{}\n", mt.dir, f)
		;;
	;;

	if mt.islib
		arcmd(b, go, mt, to)
		musecmd(b, gu, mt, tu)
		builtlib(b, mt, libs, dynlibs)
	else
		linkcmd(b, go, mt, to, libs, dynlibs, false)
		std.slfree(libs)
	;;
}

const cmddeps = {b, name, ct
	var n, a, p, gen, pid

	n = node(b.deps, std.strjoin(ct.cmd, " "))
	n.wdir = std.sldup(ct.dir)
	n.durable = ct.durable

	for c : ct.cmd
		std.slpush(&n.cmd, std.sldup(c))
	;;

	if ct.deps.len == 0
		std.slpush(&b.deps.leaves, n)
	else
		for d : ct.deps
			leaf(b.deps, d)
			depends(b.deps, n, d)
		;;
	;;

	gen = false
	for g : ct.gen
		p = std.pathcat(ct.dir, g)
		gen = gen || !std.fexists(p)
		generates(b.deps, n, p)
	;;

	if ct.istest
		a = std.htgetv(b.deps.targs, "test", [][:])
		std.slpush(&a, n)
		std.htput(b.deps.targs, "test", a)
	elif ct.isbench
		a = std.htgetv(b.deps.targs, "bench", [][:])
		std.slpush(&a, n)
		std.htput(b.deps.targs, "bench", a)
	elif gen 
		pid = run(ct.cmd, ct.dir)
		match std.wait(pid)
		| `std.Wfailure:	std.fatal("FAIL: {j= }\n", ct.cmd)
		| `std.Wsignalled:	std.fatal("CRASH: {j= }\n", ct.cmd)
		| `std.Waiterror:	std.fatal("WAT: {j= }\n", ct.cmd)
		| `std.Wsuccess:	/* ok */
		;;
	;;
}

const mandeps = {b, name, mt
	var p, r, n

	for pg : mt.pages
		p = std.pathcat(mt.dir, pg)
		n = leaf(b.deps, p)
		match std.strrfind(pg, ".")
		| `std.None:	std.fatal("manpage {} missing section\n", pg)
		| `std.Some i:	r = std.strcat(config.Manpath, pg[i + 1:])
		;;
		n.instdir = r
		n.instmod = 0o644
		addnode(b.deps, "all", n)
	;;
}

const datdeps = {b, name, dt
	var p, n

	for db : dt.blobs
		p = std.pathcat(dt.dir, db)
		n = leaf(b.deps, p)
		if dt.path.len == 0
			n.instdir = config.Sharepath
		else
			n.instdir = dt.path
		;;
		n.instmod = 0o644
		addnode(b.deps, "all", n)
	;;
}

const addtests = {b, name, mt
	for f : mt.inputs
		addtest(b, mt, f)
		addbench(b, mt, f)
	;;
}

const addtest = {b, mt, f
	addalt(b, mt, "test", f)
}

const addbench = {b, mt, f
	addalt(b, mt, "bench", f)
}

const addalt = {b, mt, kind, f
	var libs, deps
	var sp, tp, op
	var s, t, o
	var g, n
	var testinc

	/*
	change of suffix is to support testing assembly,
	C glue, and foo+sys.myr forms.
	*/
	g = b.deps
	s = changesuffix(f, ".myr")
	sp = std.pathjoin([mt.dir, kind, s][:])
	std.slfree(s)
	if !std.fexists(sp)
		std.slfree(sp)
		-> void
	;;

	libs = [][:]
	leaf(g, sp)

	t = changesuffix(f, "")
	tp = std.pathjoin([opt_objdir, mt.dir, kind, t][:])
	std.slfree(t)

	o = changesuffix(f, config.Objsuffix)
	op = std.pathjoin([opt_objdir, mt.dir, kind, o][:])
	std.slfree(o)

	n = node(g, sp)
	generates(g, n, op)
	depends(g, n, sp)
	testinc = [][:]
	std.slpush(&testinc, mt.dir)
	std.sljoin(&testinc, mt.incpath)
	deps = scrapedeps(b, mt, sp)
	for `Ldep d : deps
		depends(g, n, d)
	;;
	for `Xdep d : deps
		scrapelib(b, mt.name, d, mt.incpath)
		xdepends(b, g, n, d)
		std.slpush(&libs, d)
	;;
	myrcmd(b, n, mt, sp, true)
	std.slfree(mt.incpath)

	n = node(g, tp)
	generates(g, n, tp)
	depends(g, n, op)
	linkcmd(b, n, mt, tp, libs, [][:], true)
	std.slfree(libs)

	n = node(g, tp)
	depends(g, n, tp)
	n.wdir = std.sldup(std.dirname(std.dirname(sp)))
	std.slpush(&n.cmd, std.pathjoin([b.basedir, opt_objdir, mt.dir, kind, std.basename(tp)][:]))

	addnode(g, kind, n)
}

const resolve = {b
	var visited, looped, stk
	var g

	g = b.deps
	for n : g.nodes
		for e : n.dep
			edge(g, n, e)
		;;
	;;

	stk = [][:]
	visited = std.mkht()
	looped = std.mkht()
	for n : g.nodes
		checkloop(g, n, visited, looped, &stk)
	;;
	std.htfree(visited)
	std.htfree(looped)
}

const edge = {g, n, e
	match std.htget(g.gen, e)
	| `std.None:
		std.fatal("{}: missing build rule for {}\n", n.lbl, e)
	| `std.Some d:	
		std.slpush(&n.ndep, d)
		std.slpush(&d.ngen, n)
		n.nblock++
	;;
}

const checkloop = {g, n, visited, looped, stk
	if std.hthas(looped, n)
		std.slpush(stk, n.lbl)
		std.fatal("dependency loop: {j= -> }\n", stk#)
	;;
	if std.hthas(visited, n)
		-> void
	;;
	std.slpush(stk, n.lbl)
	std.htput(visited, n, void)
	std.htput(looped, n, void)
	for d : n.ndep
		checkloop(g, d, visited, looped, stk)
	;;
	std.slpop(stk)
	std.htdel(looped, n)
}

const musecmd = {b, n, mt, mu
	std.slpush(&n.cmd, std.sldup(opt_muse))
	for o : opt_museflags
		std.slpush(&n.cmd, o)
	;;
	std.slpush(&n.cmd, std.sldup("-o"))
	std.slpush(&n.cmd, std.sldup(mu))
	std.slpush(&n.cmd, std.sldup("-p"))
	std.slpush(&n.cmd, std.sldup(mt.name))
	for u : n.dep
		std.slpush(&n.cmd, std.sldup(u))
	;;
}

const arcmd = {b, n, mt, ar
	for c : config.Arcmd
		std.slpush(&n.cmd, std.sldup(c))
	;;
	std.slpush(&n.cmd, std.sldup(ar))
	for obj : n.dep
		std.slpush(&n.cmd, std.sldup(obj))
	;;
}

const linkcmd = {b, n, mt, bin, libs, dynlibs, istest
	for c : config.Linkcmd
		std.slpush(&n.cmd, std.sldup(c))
	;;
	for o : opt_ldflags
		std.slpush(&n.cmd, o)
	;;

	std.slpush(&n.cmd, "-o")
	std.slpush(&n.cmd, std.sldup(bin))
	if mt.ldscript.len > 0
		std.slpush(&n.cmd, std.sldup("-T"))
		std.slpush(&n.cmd, std.sldup(mt.ldscript))
	;;

	if mt.runtime.len == 0 || std.sleq(mt.runtime, "none")
		std.slpush(&n.cmd, std.sldup(opt_runtime))
	else
		std.slpush(&n.cmd, std.sldup(mt.runtime))
	;;

	for o : n.dep
		std.slpush(&n.cmd, std.sldup(o))
	;;

	addlibs(b, &n.cmd, libs, mt.incpath)
	for l : dynlibs
		std.slpush(&n.cmd, std.fmt("-l{}", l))
	;;

	/* OSX warns if we don't add a version */
	if std.sleq(opt_sys, "osx")
		std.slpush(&n.cmd, std.sldup("-macosx_version_min"))
		std.slpush(&n.cmd, std.sldup("10.6"))
	elif std.sleq(opt_sys, "linux") && dynlibs.len != 0
		std.slpush(&n.cmd, std.sldup("-dynamic-linker"))
		std.slpush(&n.cmd, std.sldup("/lib64/ld-linux-x86-64.so.2"))
	;;

}

const myrcmd = {b, n, mt, src, istest
	std.slpush(&n.cmd, std.sldup(opt_mc))
	for o : opt_mcflags
		std.slpush(&n.cmd, o)
	;;
	if opt_objdir.len > 0
		pushopt(&n.cmd, "-O", std.sldup(opt_objdir))
	;;
	for inc : mt.incpath[:mt.incpath.len - 1]
		pushopt(&n.cmd, "-I", std.pathcat(opt_objdir, inc))
	;;
	if istest
		for (dir, _, _) : mt.tstdeps
			pushopt(&n.cmd, "-I", std.pathcat(opt_objdir, dir))
		;;
		pushopt(&n.cmd, "-I", std.pathcat(opt_objdir, mt.dir))
	;;
	for (dir, _, _) : mt.libdeps
		pushopt(&n.cmd, "-I", std.pathcat(opt_objdir, dir))
	;;
	if opt_genasm
		std.slpush(&n.cmd, "-S")
	;;
	std.slpush(&n.cmd, src)
}

const ascmd = {b, n, mt, out, src
	for c : config.Ascmd
		std.slpush(&n.cmd, c)
	;;
	std.slpush(&n.cmd,"-o")
	std.slpush(&n.cmd, out)
	std.slpush(&n.cmd, src)
}

const ccmd = {b, n, mt, out, src, cflags
	std.slpush(&n.cmd, "cc")
	std.slpush(&n.cmd,"-c")
	std.slpush(&n.cmd,"-o")
	std.slpush(&n.cmd, out)
	std.slpush(&n.cmd, src)
	for flg : cflags
		std.slpush(&n.cmd, flg)
	;;
}

const scrapedeps = {b : build#, mt, path
	var p, f, l

	match bio.open(path, bio.Rd)
	| `std.Ok fd:	f = fd
	| `std.Err e:	std.fatal("error opening {}: {}\n", path, e)
	;;

	l = [][:]
	for ln : bio.byline(f)
		match regex.exec(usepat, ln)
		| `std.None:
		| `std.Some uses:
			if uses[2].len > 0
				/* external library */
				p = std.sldup(uses[2])
				std.slpush(&l, `Xdep p)
			else
				/* internal library */
				p = std.pathjoin([opt_objdir, mt.dir, uses[3]][:])
				std.sljoin(&p, ".use")
				std.slpush(&l, `Ldep p)
			;;
			regex.matchfree(uses)
		;;
	;;
	bio.close(f)
	-> l
}

const scrapecflags = {b, mt, path
	var f, fl, cflags, libs

	match bio.open(path, bio.Rd)
	| `std.Ok fd:	f = fd
	| `std.Err e:	std.fatal("error opening {}: {}\n", path, e)
	;;

	cflags = [][:]
	libs = [][:]
	for ln : bio.byline(f)
		match regex.exec(cflagpat, ln)
		| `std.None:	/* skip */
		| `std.Some m:
			fl = std.strtok(m[1])
			for s : fl
				std.slpush(&cflags, std.sldup(s))
			;;
			std.slfree(fl)
		;;
		match regex.exec(clibpat, ln)
		| `std.None:	/* skip */
		| `std.Some m:
			fl = std.strtok(m[1])
			for s : fl
				std.slpush(&libs, std.sldup(s))
			;;
			std.slfree(fl)
		;;
	;;
	-> (cflags, libs)
}

const generates = {g, n, dep
	std.mkpath(std.dirname(dep))
	std.slpush(&n.gen, dep)
	std.htput(g.gen, dep, n)
}

const depends = {g, n, dep
	std.slpush(&n.dep, dep)
}

const xdepends = {b, g, n, dep
	match std.htget(b.libs, dep)
	| `std.Some ldep:
		if ldep.genuse.len != 0
			depends(g, n, ldep.genuse)
			depends(g, n, ldep.genar)
		;;
		n.deptime = std.max(n.deptime, ldep.mtime)
	| `std.None:
		std.fatal("unknown xdep {} (known: {})\n", dep, std.htkeys(b.libs))
	;;
}

const leaf = {g, f
	var nod

	match std.htget(g.gen, f)
	| `std.Some n:
		-> n
	| `std.None:	
		nod = node(g, f)
		nod.durable = true
		generates(g, nod, f)
		std.slpush(&g.leaves, nod)
		-> nod
	;;
}

const addnode = {g, targ, n
	var nl

	nl = std.htgetv(g.targs, targ, [][:])
	std.slpush(&nl, n)
	std.htput(g.targs, targ, nl)
}

const pushopt = {lst, pfx, dir
	std.slpush(lst, std.sldup(pfx))
	std.slpush(lst, std.pathnorm(dir))
}

const node = {g, lbl
	var nod

	nod = std.mk([
		.lbl=lbl,
		.cmd=[][:],
		.gen=[][:],
		.dep=[][:],

		.nblock=0,
		.mtime=0,
	])
	std.slpush(&g.nodes, nod)
	-> nod
}