summaryrefslogtreecommitdiff
path: root/mbld/syssel.myr
blob: 09574d355741569f3b4f3abfc59f6abdf56c17d9 (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
use std

use "opts"
use "types"

pkg bld =
	type syssel(@a) = struct
		file	: byte[:]
		line	: int
		targ	: byte[:]
		sysattrs	: std.htab(byte[:], (int, int, int))#
		_match	: std.htab(byte[:], int)#
		_best	: std.htab(byte[:], @a)#
	;;

	generic mksyssel	: (b : build#, f : byte[:], line : int, targ : byte[:] -> syssel(@a)#)
	generic sysseladd	: (syssel : syssel(byte[:])#, file : byte[:] -> void)
	generic sysseladdlist	: (syssel : syssel(@a)#, base : byte[:], attrs : byte[:][:], val : @a -> void)
	generic sysselfin	: (syssel : syssel(@a)# -> @a[:])
	const addsysattrs	: (sa : build#, tags : byte[:][:] -> void)
;;

generic mksyssel = {b, file, line, targ
	var syssel

	syssel = std.mk([
		.file = file,
		.line = line,
		.targ = targ,
		._match = std.mkht(std.strhash, std.streq),
		._best = std.mkht(std.strhash, std.streq),
		.sysattrs = b.tags
	])
	-> syssel
}

generic sysseladd = {syssel, f
	var basename, attrs
	var attrlist

	match std.strfind(f, "+")
	| `std.Some i:
		basename = f[:i]
		match std.strrfind(f[i+1:], ".")
		| `std.Some j:	attrs = f[i+1:][:j]
		| `std.None:	std.fatal("unrecognized type for file {}\n", f)
		;;
	| `std.None:
		match std.strrfind(f, ".")
		| `std.None:	std.fatal("unrecognized type for file {}\n", f)
		| `std.Some i:
			basename = f[:i]
			attrs = ""
		;;
	;;

	attrlist = std.strsplit(attrs, "-")
	sysseladdlist(syssel, basename, attrlist, f)
	std.slfree(attrlist)
}

generic sysseladdlist = {syssel, base, attrs, val
	var nmatch, curbest, n, v

	nmatch = 0
	for a : attrs
		match std.strfind(a, ":")
		| `std.Some i:
			n = a[:i]
			v = parseversion(a[i+1:])
		| `std.None:
			n = a
			v = (-1, -1, -1)
		;;

		match std.htget(syssel.sysattrs, n)
		| `std.None:
			nmatch = -1
			break
		| `std.Some av:
			if newenough(syssel, av, v)
				nmatch++
			;;
		;;
	;;
	curbest = std.htgetv(syssel._match, base, -1)
	if curbest < nmatch
		std.htput(syssel._match, base, nmatch)
		std.htput(syssel._best, base, val)
	;;
}

const newenough = {syssel, attr, vers
	match (attr, vers)
	| ((a0, a1, a2), (v0, v1, v2)):
		if a0 == -1 && a1 == -1 && a2 == -1
			-> true
		else
			-> a0 >= v0 && a1 >= v1 && a2 >= v2
		;;
	;;
}

generic sysselfin = {syssel
	var keys, nmatch, ret

	keys = std.htkeys(syssel._match)
	ret = [][:]
	for k : keys
		nmatch = std.htgetv(syssel._match, k, -1)
		if nmatch == -1
			std.fatal("{}:{}: target {}, no applicable file for '{}'\n", \
				syssel.file, syssel.line, syssel.targ, k)
		;;
		std.slpush(&ret, std.get(std.htget(syssel._best, k)))
	;;
	std.htfree(syssel._match)
	std.htfree(syssel._best)
	-> ret
}

const addsysattrs = {b, tags
	var tagfile

	std.htput(b.tags, opt_sys, opt_sysvers)
	match opt_sys
	| "freebsd":	std.htput(b.tags, "posixy", (-1, -1, -1))
	| "netbsd":	std.htput(b.tags, "posixy", (-1, -1, -1))
	| "openbsd":	std.htput(b.tags, "posixy", (-1, -1, -1))
	| "osx":	std.htput(b.tags, "posixy", (-1, -1, -1))
	| "linux":	std.htput(b.tags, "posixy", (-1, -1, -1))
	| "plan9":
	| unknown:	std.fatal("unknown system \"{}\"\n", unknown)
	;;

	match opt_arch
	| "x64":	tag(b.tags, ["x64"][:])
	| unknown:	std.fatal("unknown architecture {}\n", unknown)
	;;
	tag(b.tags, tags)

	tagfile = std.pathcat(b.basedir, " bld.tag")
	if std.fexists(tagfile)
		loadtagfile(b, tagfile)
	;;
	std.slfree(tagfile)
}

const loadtagfile = {b, tagfile
	var data, tag

	data = std.try(std.slurp(tagfile))
	while true
		data = skipspace(data)
		(tag, data) = word(data) 
		match tag
		| `std.Some w:
			std.htput(b.tags, w, (-1, -1, -1))
		| `std.None:
			if data.len > 0
				std.fatal("junk character near '{}'\n", trailing(data, 10))
			else
				break
			;;
		;;
	;;
}

const skipspace = {data
	var c

	c = std.decode(data)
	while std.isspace(c)
		data = data[std.charlen(c):]
	;;
	-> data
}

const word = {data
	var c, split

	split = 0
	c = std.decode(data[:])
	while std.isalnum(c) || c == '.' || c == '_' || c == '$'
		split += std.charlen(c)
	;;
	if split > 0
		-> (`std.Some data[:split], data[split:])
	else
		-> (`std.None, data)
	;;
}

const tag  = {sa, tags
	for t : tags
		std.htput(sa, t, (-1, -1, -1))
	;;
}

const trailing = {str, len
	-> str[:std.min(len, str.len)]
}