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

use "opts.use"

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

	generic mksyssel	: (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[:])
;;

generic mksyssel = {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 = std.mkht(std.strhash, std.streq),
	])
	addsysattrs(syssel.sysattrs)
	-> 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

	nmatch = 0
	for a in attrs
		if std.hthas(syssel.sysattrs, a)
			nmatch++
		else
			nmatch = -1
			break
		;;
	;;
	curbest = std.htgetv(syssel._match, base, -1)
	if curbest < nmatch
		std.htput(syssel._match, base, nmatch)
		std.htput(syssel._best, base, val)
	;;
}

generic sysselfin = {syssel
	var keys, nmatch, ret

	keys = std.htkeys(syssel._match)
	ret = [][:]
	for k in 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)
		;;
		ret = std.slpush(ret, std.get(std.htget(syssel._best, k)))
	;;
	std.htfree(syssel._match)
	std.htfree(syssel._best)
	std.htfree(syssel.sysattrs)
	-> ret
}

const addsysattrs = {sa
	var attrs
	match opt_sys
	| "freebsd":	attrs = ["freebsd", "posixy"][:]
	| "osx":	attrs = ["osx", "posixy"][:]
	| "linux":	attrs = ["linux", "posixy"][:]
	| "plan9":	attrs = ["plan9"][:]
	| unknown:	std.fatal("unknown systemx \"{}\"\n", unknown)
	;;
	for a in attrs
		std.htput(sa, a, true)
	;;

	match opt_arch
	| "x64":	attrs = ["x64"][:]
	| unknown:	std.fatal("unknown architecture {}\n", unknown)
	;;
	for a in attrs
		std.htput(sa, a, true)
	;;
}