summaryrefslogtreecommitdiff
path: root/mbld/build.myr
blob: 19baa73f022f1097ef997fcd0c704e53e59fecf0 (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
use std

use "config.use"
use "deps.use"
use "opts.use"
use "parse.use"
use "types.use"
use "util.use"

pkg bld =
	const buildall	: (b : build# -> bool)
	const genall	: (b : build# -> bool)
	const build	: (b : build#, target : byte[:] -> bool)
	const buildbin	: (b : build#, bt : myrtarg#, addsrc : bool -> void)
	const buildlib	: (b : build#, lt : myrtarg# -> void)
;;

const buildall = {b
	for tn in b.all
		match gettarg(b.targs, tn)
		| `Bin bt:	buildbin(b, bt, false)
		| `Lib lt:	buildlib(b, lt)
		| `Test tt:	/* build on 'mbld test' by default */
		| `Gen gt:	genfiles(b, gt)
		| `Man m:	/* nothing needed */
		;;
	;;
	-> true
}

const genall = {b
	for tn in b.all
		match gettarg(b.targs, tn)
		| `Gen gt: run(gt.cmd)
		| _:	/* skip */
		;;
	;;
	/* genfiles will exit if the build fails; always return true */
	-> true
}

const build = {b, targ
	match std.htget(b.targs, targ)
	| `std.Some (`Bin bt):	buildbin(b, bt, false)
	| `std.Some (`Lib lt):	buildlib(b, lt)
	| `std.Some (`Test tt):	buildbin(b, tt, false)
	| `std.Some (`Gen gt):	run(gt.cmd)
	| `std.Some (`Man m):	/* nothing needed */
	| `std.None:	std.fatal(1, "invalid target %s\n", targ)
	;;
	-> true
}

const buildbin = {b, targ, addsrc
	var dg, src

	if targ.built
		->
	;;
	setdir(b, targ.dir)
	addincludes(b, targ)
	buildlibdeps(b, targ)
	std.put("%s...\n", targ.name)
	if !myrdeps(b, targ, false, false, addsrc, &dg)
		std.fatal(1, "Could not load dependencies for %s\n", targ.name)
	;;
	if !std.hthas(dg.deps, targ.name)
		std.fatal(1, "no input files for %s\n", targ.name)
	;;
	if builddep(b, &dg, targ.name, targ.incpath) || !freshlibs(targ, dg.libs)
		src = std.htkeys(dg.sources)
		linkbin(&dg, targ.name, src, targ.ldscript, targ.runtime, targ.incpath, targ.libdeps)
		std.slfree(src)
	;;
	targ.built = true
}

const buildlib = {b, targ
	var archive
	var u, l
	var dg
	var lib, src

	if targ.built
		->
	;;
	setdir(b, targ.dir)
	addincludes(b, targ)
	buildlibdeps(b, targ)
	lib = targ.name
	std.put("lib%s.a...\n", lib)
	archive = std.fmt("lib%s.a", lib)
	if !myrdeps(b, targ, true, false, false, &dg)
		std.fatal(1, "Could not load dependencies for %s\n", lib)
	;;
	if !std.hthas(dg.deps, lib)
		std.fatal(1, "no target declared for %s\n", lib)
	;;
	u = builddep(b, &dg, targ.name, targ.incpath)
	l = builddep(b, &dg, archive, targ.incpath)
	if  u || l || !freshlibs(targ, dg.libs)
		src = std.htkeys(dg.sources)
		mergeuse(&dg, lib, src, targ.incpath)
		archivelib(&dg, lib, src, targ.incpath)
		std.slfree(src)
	;;
	std.slfree(archive)
	targ.built = true
}

const genfiles = {b, gt
	for f in gt.out
		if !std.fexists(f)
			run(gt.cmd)
			break
		;;
	;;
}

const addincludes = {b, targ
	for (inc, lib, subtarg) in targ.libdeps
		if !hasinc(targ.incpath, inc)
			targ.incpath = std.slpush(targ.incpath, inc)
		;;
	;;
}

const buildlibdeps = {b, targ
	for (inc, lib, subtarg) in targ.libdeps
		build(b, subtarg)
	;;
}

const hasinc = {path, t
	for e in path
		if std.sleq(e, t)
			-> true
		;;
	;;
	-> false
}


const builddep = {b, dg, out, incs
	var stale

	stale = false
	/* short circuit walking the dep tree if we've already built this. */
	if std.htgetv(dg.updated, out, false)
		-> false
	;;

	match std.htget(dg.deps, out)
	| `std.Some deps:
		for d in deps
			if builddep(b, dg, d, incs)
				stale = true
			;;
			if !std.fexists(d)
				match std.htget(b.gensrc, d)
				| `std.Some gt:	run(gt.cmd)
				| `std.None:	std.fatal(1, "no input file %s\n", d)
				;;
			;;
			if !isfresh(d, out)
				stale = true
			;;
		;;
	| `std.None:
	;;

	match std.htget(dg.input, out)
	| `std.Some src:
		if stale
			compile(src, incs)
		;;
		std.htput(dg.updated, out, true)
	| `std.None:
	;;
	-> stale
}

const compile = {src, incs
	var o
	var cmd

	cmd = [][:]
	if std.hassuffix(src, ".myr")
		cmd = std.slpush(cmd, opt_mc)
		for inc in incs 
			cmd = std.slpush(cmd, "-I")
			cmd = std.slpush(cmd, inc)
		;;
		if opt_genasm
			cmd = std.slpush(cmd, "-S")
		;;
		cmd = std.slpush(cmd, src)
		run(cmd)
		std.slfree(cmd)
	elif std.hassuffix(src, ".s")
		o = srcswapsuffix(src, config.Objsuffix)
		for c in config.Ascmd
			cmd = std.slpush(cmd, c)
		;;
		cmd = std.slpush(cmd,"-o")
		cmd = std.slpush(cmd, o)
		cmd = std.slpush(cmd, src)
		run(cmd)
		std.slfree(o)
	else
		std.fatal(1, "Unknown file type for %s\n", src)
	;;
}

const linkbin = {dg, bin, srcfiles, ldscript, rt, incs, extralibs
	var cmd

	cmd = [][:]

	/* ld -o bin */
	for c in config.Linkcmd
		cmd = std.slpush(cmd, std.sldup(c))
	;;
	cmd = std.slpush(cmd, std.sldup(bin))

	/* [-T script] */
	if ldscript.len > 0
		cmd = std.slpush(cmd, std.sldup("-T"))
		cmd = std.slpush(cmd, std.sldup(ldscript))
	;;

	if rt.len != 0
		cmd = std.slpush(cmd, std.sldup(rt))
	else
		cmd = std.slpush(cmd, std.sldup(opt_runtime))
	;;

	/* input.o list.o... */
	for f in srcfiles
		cmd = std.slpush(cmd, srcswapsuffix(f, config.Objsuffix))
	;;

	/* -L path -l lib... */
	cmd = addlibs(cmd, dg.libs, incs)
	for (d, l, t) in extralibs
		cmd = std.slpush(cmd, std.fmt("-l%s", l))
	;;


	/* special for OSX: it warns if we don't add this */
	if std.sleq(opt_sys, "osx")
		cmd = std.slpush(cmd, std.sldup("-macosx_version_min"))
		cmd = std.slpush(cmd, std.sldup("10.6"))
	;;

	run(cmd)
	strlistfree(cmd)
}

const archivelib = {dg, lib, files, incs
	var cmd
	var obj

	cmd = [][:]
	for c in config.Arcmd
		cmd = std.slpush(cmd, std.sldup(c))
	;;
	cmd = std.slpush(cmd, std.fmt("lib%s.a", lib))
	for f in files
		obj = srcswapsuffix(f, config.Objsuffix)
		cmd = std.slpush(cmd, obj)
	;;
	run(cmd)
	strlistfree(cmd)
}

const mergeuse = {dg, lib, files, incs
	var cmd

	cmd = [][:]
	cmd = std.slpush(cmd, std.sldup(opt_muse))
	cmd = std.slpush(cmd, std.sldup("-o"))
	cmd = std.slpush(cmd, std.sldup(lib))
	for f in files
		if std.hassuffix(f, ".myr")
			cmd = std.slpush(cmd, srcswapsuffix(f, ".use"))
		elif !std.hassuffix(f, ".s")
			std.fatal(1, "unknown file type for %s\n", f)
		;;
	;;
	run(cmd)
	strlistfree(cmd)
}

const addlibs = {cmd, libgraph, incs
	var looped : std.htab(byte[:], bool)#
	var marked : std.htab(byte[:], bool)#
	var libs
	var head

	/* -L incpath... */
	if !config.Directlib
		for inc in incs
			cmd = std.slpush(cmd, std.fmt("-L%s", inc))
		;;
	;;

	libs = std.htkeys(libgraph)
	looped = std.mkht(std.strhash, std.streq)
	marked = std.mkht(std.strhash, std.streq)
	head = cmd.len

	for lib in libs
		cmd = visit(cmd, head, libgraph, lib, looped, marked, incs)
	;;

	-> cmd
}

const visit = {cmd, head, g, lib, looped, marked, incs
	if std.hthas(looped, lib)
		std.fatal(1, "cycle in library graph involving \"%s\"\n", lib)
	elif std.hthas(marked, lib)
		-> cmd
	;;

	std.htput(looped, lib, true)
	for dep in std.htgetv(g, lib, [][:])
		cmd = visit(cmd, head, g, dep, looped, marked, incs)
	;;
	std.htdel(looped, lib)
	std.htput(marked, lib, true)
	-> putlib(cmd, head, lib, incs)
}

const putlib = {cmd, head, lib, incs
	if !config.Directlib
		-> std.slput(cmd, head, std.fmt("-l%s", lib))
	else
		match findlib(lib, incs)
		| `std.None:
			std.fatal(1, "could not find library lib%s.a\n", lib)
		| `std.Some p:
			-> std.slput(cmd, head, p)
		;;
	;;
}

const findlib = {lib, incs
	var buf : byte[512]
	var sl, p

	sl = std.bfmt(buf[:], "lib%s.a", lib)
	for i in incs
		p = std.pathcat(i, sl)
		if std.fexists(p)
			-> `std.Some p
		;;
		std.slfree(p)
	;;
	-> `std.None
}

const freshlibs = {targ, libgraph
	var libs

	libs = std.htkeys(libgraph)
	for l in libs
		match findlib(l, targ.incpath)
		| `std.Some lib:
			if !isfresh(lib, targ.name)
				std.slfree(lib)
				-> false
			;;
			std.slfree(lib)
		| `std.None:
			std.fput(1, "%s: could not find library lib%s.a\n", targ.name, l)
			std.fput(1, "searched:\n")
			for inc in targ.incpath
				std.fput(1, "\t%s\n", inc)
			;;
			std.exit(1)
		;;
	;;
	std.slfree(libs)
	-> true
}

const isfresh = {src, dst
	var srcmt, dstmt

	/*
	OSX only has single second resolution on modification
	times. Since most builds happen within one second of each
	other, if we treat equal times as outdated, we do a lot of
	spurious rebuilding.

	So, we treat times where both secs and nsecs are equal as
	up to date.
	*/
	match std.fmtime(src)
	| `std.Some mt:	srcmt = mt
	| `std.None:	std.fatal(1, "could not stat %s\n", src)
	;;
	match std.fmtime(dst)
	| `std.Some mt:	dstmt = mt
	| `std.None:	-> false
	;;
	-> srcmt <= dstmt
}