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

use "config"
use "opts"
use "types"

pkg bld =
	const addlibs	: (b : build#, \
		sl : byte[:][:]#, \
		libs : byte[:][:], \
		incs : byte[:][:] -> void)

	const builtlib	: (b : build#, \
		lt : myrtarg#, \
		dep : byte[:][:], \
		dyndep : byte[:][:] -> void)

	const scrapelib : (b : build#, \
		targ : byte[:], \
		libs : byte[:], \
		incs : byte[:][:] -> void)
;;

const Abiversion = 14

const builtlib = {b, mt, dep, dyndep
	var ldep, l, u

	l = std.fmt("lib{}.a", mt.name)
	u = std.fmt("lib{}.use", mt.name)
	ldep = std.mk([
		.name=mt.name,
		.dir=std.pathcat(opt_objdir, mt.dir),
		.dep=dep,
		.dyndep=dyndep,
		.genuse=std.pathjoin([opt_objdir, mt.dir, u][:]),
		.genar=std.pathjoin([opt_objdir, mt.dir, l][:]),
	])
	std.slfree(l)
	std.slfree(u)
	for d : dep
		scrapelib(b, mt.name, d, mt.incpath)
	;;
	std.htput(b.libs, mt.name, ldep)
}

const scrapelib = {b, targ, lib, incs
	var path, dep, dyndep, ldep
	var f, dir

	if std.hthas(b.libs, lib)
		-> void
	;;

	(f, path, dir) = openlib(lib, targ, incs)
	match bio.getc(f)
	| `std.Ok 'U':	/* ok */
	| `std.Ok _:	std.fput(1, "{}/{}: not a usefile\n", dir, lib)
	| `std.Err e:	std.fatal("{}/{}: error reading: {}\n", dir, lib, e)
	;;
	match bio.getbe32(f)
	| `std.Ok Abiversion:	/* nothing: version matches. */
	| `std.Ok v:	std.fput(1, "{}/{}: mismatched abi {}\n", dir, lib, v)
	| `std.Err e:	std.fatal("{}/{}: error reading: {}\n", dir, lib, e)
	;;
	std.slfree(rdstr(f))

	dep = [][:]
	dyndep = [][:]
	while true
		match bio.getc(f)
		| `std.Ok 'L':	std.slpush(&dep, rdstr(f))
		| `std.Ok 'X':	std.slpush(&dyndep, rdstr(f))
		| `std.Err e:	std.fatal("{}: error reading {}\n", lib, e)
		| _:		break
		;;
	;;
	bio.close(f)
	ldep = std.mk([
		.name=lib,
		.dir=dir,
		.dep=dep,
		.dyndep=dyndep,
		.genuse="",
		.genar="",
		.mtime=std.tryv(std.fmtime(path), 0)
	])
	std.slfree(path)
	std.htput(b.libs, lib, ldep)

	for d : dep
		scrapelib(b, targ, d, incs)
	;;
}

const openlib = {lib, targ, incs
	var path, libname

	path = ""
	libname = ""
	for p : incs
		libname = std.fmt("lib{}.use", lib)
		path = std.pathjoin([p, libname][:])
		std.slfree(libname)
		if std.fisreg(path)
			match  bio.open(path, bio.Rd)
			| `std.Ok file:	-> (file, path, p)
			| `std.Err m:	/* next */
			;;
			std.fatal("{}: {} does not exist in {j=, }\n", targ, lib, incs)
		;;
		std.slfree(path)
	;;
	std.fatal("{}: {} does not exist in {j= }\n", targ, lib, incs)
}

const addlibs = {b, sl, libs, incs
	var added, diradded, looped
	var lo

	added  = std.mkht(std.strhash, std.streq)
	looped  = std.mkht(std.strhash, std.streq)
	diradded  = std.mkht(std.strhash, std.streq)

	lo = sl#.len
	for l : libs
		addlib(b, sl, l, added, diradded, looped)
	;;
	for var i = 0; i < sl#[lo:].len/2; i++
		std.swap(&sl#[lo+i], &sl#[sl#.len - i - 1])
	;;

	std.htfree(diradded)
	std.htfree(looped)
	std.htfree(added)
}

const addlib = {b, sl, lib, added, diradded, looped
	var ar

	if std.hthas(looped, lib)
		std.fatal("{}: loop in deps\n", lib)
	;;
	std.htput(looped, lib, void)
	match std.htget(b.libs, lib)
	| `std.None:
		std.slpush(sl, std.fmt("-l{}", lib))
	| `std.Some ld:
		for l : ld.dep
			addlib(b, sl, l, added, diradded, looped)
		;;
		for l : ld.dyndep
			addlib(b, sl, l, added, diradded, looped)
		;;
		if !std.hthas(added, lib)
			if config.Directlib
				ar = std.fmt("lib{}.a", lib)
				std.slpush(sl, std.pathcat(ld.dir, ar))
				std.slfree(ar)
			else
				std.slpush(sl, std.fmt("-l{}", lib))
			;;
			std.htput(added, lib, void)
		;;
		if !config.Directlib && !std.hthas(diradded, ld.dir)
			std.htput(diradded, ld.dir, void)
			std.slpush(sl, std.fmt("-L{}", ld.dir))
		;;
	;;
	std.htdel(looped, lib)
}


const rdstr = {f -> byte[:]
	var len
	var sl

	match bio.getbe32(f)
	| `std.Ok l:
		len = l
		sl = std.slalloc(len)
	| `std.Err e:	std.fatal("error while reading string: {}", e)
	;;
	bio.read(f, sl)
	-> sl
}