summaryrefslogtreecommitdiff
path: root/mbld/main.myr
blob: 0523faf697915ef5e5dfd83d68b19b5dda44b515 (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
use std
use regex

use "build"
use "clean"
use "config"
use "deps"
use "install"
use "opts"
use "parse"
use "test"
use "types"
use "util"
use "syssel"

const main = {args : byte[:][:]
	var b : bld.build#
	var cleanfirst
	var dumponly
	var bintarg
	var targname
	var runsrc
	var tmp
	var cmd 
	var tags
	var ok, r

	cmd = std.optparse(args, &[
		.argdesc = "[inputs...]",
		.opts = [
			[.opt='c', .desc="clean before building"],
			[.opt='T', .desc="list all available targets"],
			[.opt='t', .arg="tag", .desc="build with specified systag"],
			[.opt='S', .desc="generate assembly when building"],
			[.opt='d', .desc="dump debugging information for mbld"],
			[.opt='I', .arg="inc", .desc="add 'inc' to your include path"],
			[.opt='R', .arg="runsrc", .desc="source to compile and run"],
			[.opt='B', .arg="base", .desc="install into 'base'"],
			[.opt='b', .arg="bin", .desc="compile binary named 'bin' from inputs"],
			[.opt='l', .arg="lib", .desc="compile lib named 'lib' from inputs"],
			[.opt='r', .arg="rt", .desc="link against runtime 'rt' instead of default"],
			[.opt='C', .arg="mc", .desc="compile with 'mc' instead of the default compiler"],
			[.opt='M', .arg="mu", .desc="merge uses with 'mu' instead of the default muse"],
		][:]
	])

	tags = [][:]
	runsrc = ""
	targname = ""
	dumponly = false
	cleanfirst = false
	ok = true

	bld.initopts()
	for opt in cmd.opts
		match opt
		| ('T', ""):	dumponly = true
		| ('S', ""):	bld.opt_genasm = true
		| ('c', ""):	cleanfirst = true
		| ('I', arg):	std.slpush(&bld.opt_incpaths, arg)
		| ('B', arg):	bld.opt_instbase = arg
		| ('t', tag):	std.slpush(&tags, tag)
		| ('R', arg):	runsrc = arg
		| ('b', arg):
			targname = arg
			bintarg = true
		| ('l', arg):
			targname = arg
			bintarg = false
		| ('r', arg):
			if std.sleq(arg, "none")
				bld.opt_runtime = ""
			else
				bld.opt_runtime = arg
			;;
		/*
		internal undocumented args; used by compiler suite for
		building with an uninstalled compiler.
		*/
		| ('d', arg): bld.opt_debug = true
		| ('C', arg): bld.opt_mc = arg
		| ('M', arg): bld.opt_muse = arg
		| _:	std.die("unreachable\n")

		;;
	;;

	for (e, v) in config.Env
		std.setenv(e, v)
	;;

	b = mkbuild(tags)
	if targname.len != 0
		buildimm(b, targname, cmd.args, bintarg, cleanfirst)
	elif runsrc.len != 0
		bld.opt_silent = true
		tmp = std.mktemppath("runmyr")
		ok = buildimm(b, tmp, [runsrc][:], true, cleanfirst)
		if ok
			ok = runcmd(tmp, cmd.args)
		;;
		std.remove(tmp)
	elif dumponly
		findproj(b, "bld.proj")
		bld.load(b)
		for t in b.all
			bld.mbldput("{}\n", t)
		;;
	else
		findproj(b, "bld.proj")
		bld.load(b)
		/*bld.configure()*/
		/* default: buildall */
		if cleanfirst
			bld.cleanall(b)
		;;
		if cmd.args.len == 0
			bld.buildall(b)
		else
			for c in cmd.args
				match c
				| "all":	r = bld.buildall(b)
				| "gen":	r = bld.genall(b)
				| "clean":	r = bld.cleanall(b)
				| "install":	r = bld.install(b)
				| "uninstall":	r = bld.uninstall(b)
				| "test":	r = bld.test(b)
				| target:	r = bld.buildtarg(b, target)
				;;
				ok = ok && r
			;;
		;;
	;;
	if !ok
		std.exit(1)
	;;
}

const buildimm = {b, targ, inputs, bintarg, cleanfirst -> bool
	var mt : bld.myrtarg

	mt = [
		.name=targ,
		.inputs=inputs,
		.runtime=bld.opt_runtime,
		.incpath=bld.opt_incpaths,
		.libdeps=[][:]
	]
	if cleanfirst
		bld.cleanmyr(b, &mt)
	;;
	if bintarg
		-> bld.buildbin(b, &mt, true)
	else
		-> bld.buildlib(b, &mt)
	;;
}

const runcmd = {bin, args
	var sl

	sl = std.sldup([bin][:])
	-> bld.run(std.sljoin(&sl, args))
}

const mkbuild = {tags
	var b

	b = std.zalloc()
	b.targs = std.mkht(std.strhash, std.streq)
	b.gensrc = std.mkht(std.strhash, std.streq)
	b.built = std.mkht(std.strhash, std.streq)
	b.tags = std.mkht(std.strhash, std.streq)
	bld.addsysattrs(b, tags)
	-> b
}

const findproj = {b, bldfile
	if !findbase(b, bldfile) || !std.chdir(b.basedir)
		std.fatal("could not find {}\n", bldfile)
	;;
	bld.setdir(b, "")
}

const findbase = {b, file
	var p, bld, dir

	dir = std.getcwd()
	while !std.sleq(dir, "/")
		bld = std.pathcat(dir, file)
		if std.fexists(bld)
			bld.mbldput("project base {}:\n", dir)
			b.basedir = dir
			b.bldfile = bld
			-> true
		;;
		p = std.pathcat(dir, "..")
		std.slfree(dir)
		dir = p
	;;
	-> false
}