-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathbuild.rs
315 lines (283 loc) Β· 10.9 KB
/
build.rs
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
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![allow(clippy::restriction)]
use std::env;
mod paths {
use std::env;
use std::path::PathBuf;
pub fn crate_root() -> PathBuf {
let root = env::var_os("CARGO_MANIFEST_DIR").expect("cargo-provided CARGO_MANIFEST_DIR env variable not set");
PathBuf::from(root)
}
pub fn mruby_root() -> PathBuf {
crate_root().join("vendor").join("mruby")
}
pub fn mrbgems_root() -> PathBuf {
crate_root().join("cext").join("mrbgems")
}
pub fn mrbsys_root() -> PathBuf {
crate_root().join("cext").join("mrbsys")
}
pub fn emscripten_root() -> PathBuf {
crate_root().join("vendor").join("emscripten")
}
pub fn bindgen_header() -> PathBuf {
crate_root().join("cext").join("include").join("bindgen.h")
}
}
mod libs {
use std::ffi::OsStr;
use std::path::PathBuf;
use std::str;
use std::thread;
use super::paths;
use crate::Wasm;
fn mruby_sources() -> impl Iterator<Item = PathBuf> {
[
"src/array.c",
"src/backtrace.c",
"src/class.c",
"src/codedump.c",
"src/compar.c",
"src/debug.c",
"src/dump.c",
"src/enum.c",
"src/error.c",
"src/etc.c",
"src/fmt_fp.c",
"src/gc.c",
"src/hash.c",
"src/init.c",
"src/kernel.c",
"src/load.c",
"src/numeric.c",
"src/numops.c",
"src/object.c",
"src/pool.c",
"src/print.c",
"src/proc.c",
"src/range.c",
"src/readfloat.c",
"src/readint.c",
"src/state.c",
"src/string.c",
"src/symbol.c",
"src/variable.c",
"src/version.c",
"src/vm.c",
]
.into_iter()
.map(|source| paths::mruby_root().join(source))
}
fn mruby_include_dirs() -> impl Iterator<Item = PathBuf> {
[
"include", // mruby core
]
.into_iter()
.map(|dir| paths::mruby_root().join(dir))
}
fn mrbgems_sources() -> impl Iterator<Item = PathBuf> {
[
"mrbgems/mruby-class-ext/src/class.c", // NOTE(GH-32): Pending removal.
"mrbgems/mruby-compiler/core/codegen.c", // Ruby parser and bytecode generation
"mrbgems/mruby-compiler/core/y.tab.c", // Ruby parser and bytecode generation
"mrbgems/mruby-error/src/exception.c", // `mrb_raise`, `mrb_protect`
"mrbgems/mruby-eval/src/eval.c", // eval, instance_eval, and friends
"mrbgems/mruby-fiber/src/fiber.c", // Fiber class from core, required by `Enumerator`
"mrbgems/mruby-metaprog/src/metaprog.c", // APIs on Kernel and Module for accessing classes and variables
"mrbgems/mruby-method/src/method.c", // `Method`, `UnboundMethod`, and method APIs on Kernel and Module
"mrbgems/mruby-pack/src/pack.c", // Array#pack and String#unpack
"mrbgems/mruby-proc-ext/src/proc.c", // NOTE(GH-32): This gem is required by `mruby-method`.
"mrbgems/mruby-sprintf/src/sprintf.c", // Kernel#sprintf, Kernel#format, String#%
]
.into_iter()
.map(|source| paths::mruby_root().join(source))
.chain(
["src/gem_init.c", "src/mrbgems.c"]
.into_iter()
.map(|source| paths::mrbgems_root().join(source)),
)
}
fn mrbgems_include_dirs() -> impl Iterator<Item = PathBuf> {
[
"mrbgems/mruby-class-ext/include", // NOTE(GH-32): Pending removal.
"mrbgems/mruby-compiler/core", // Ruby parser and bytecode generation
"mrbgems/mruby-error/include", // `mrb_raise`, `mrb_protect`
"mrbgems/mruby-eval/include", // eval, instance_eval, and friends
"mrbgems/mruby-fiber/include", // Fiber class from core, required by `Enumerator`
"mrbgems/mruby-metaprog/include", // APIs on Kernel and Module for accessing classes and variables
"mrbgems/mruby-method/include", // `Method`, `UnboundMethod`, and method APIs on Kernel and Module
"mrbgems/mruby-pack/include", // Array#pack and String#unpack
"mrbgems/mruby-proc-ext/include", // NOTE(GH-32): This gem is required by `mruby-method`.
"mrbgems/mruby-sprintf/include", // Kernel#sprintf, Kernel#format, String#%
]
.into_iter()
.map(|dir| paths::mruby_root().join(dir))
.chain(mruby_include_dirs())
}
fn mrbsys_sources() -> impl Iterator<Item = PathBuf> {
["src/ext.c"]
.into_iter()
.map(|source| paths::mrbsys_root().join(source))
}
fn mrbsys_include_dirs() -> impl Iterator<Item = PathBuf> {
["include"]
.into_iter()
.map(|dir| paths::mrbsys_root().join(dir))
.chain(mruby_include_dirs())
}
// From `emsdk/upstream/emscripten/tools/system_libs.py` in emsdk 3.1.12:
fn wasm_include_dirs() -> impl Iterator<Item = PathBuf> {
[
"system/include/compat",
"system/include",
"system/lib/libc/musl/include",
"system/lib/libc/musl/arch/emscripten",
"system/lib/compiler-rt/include",
"system/lib/libunwind/include",
]
.into_iter()
.map(|dir| paths::emscripten_root().join(dir))
}
fn staticlib(
wasm: Option<Wasm>,
name: &str,
include_dirs: impl Iterator<Item = PathBuf>,
sources: impl Iterator<Item = PathBuf>,
) {
assert!(
name.starts_with("lib"),
"Static lib name must be of the format libXXX.a, got {name}"
);
assert_eq!(
name.rsplit('.').next().map(|ext| ext.eq_ignore_ascii_case("a")),
Some(true),
"Static lib name must be of the format libXXX.a, got {name}"
);
assert!(
name.len() > 5,
"Static lib name must be of the format libXXX.a, got {name}"
);
let mut build = cc::Build::new();
build
.warnings(false)
.define("ARTICHOKE", None)
.define("MRB_ARY_NO_EMBED", None)
.define("MRB_GC_TURN_OFF_GENERATIONAL", None)
.define("MRB_INT64", None)
.define("MRB_NO_BOXING", None)
.define("MRB_NO_PRESYM", None)
.define("MRB_NO_STDIO", None)
.define("MRB_UTF8_STRING", None);
for source in sources {
println!("cargo:rerun-if-changed={}", source.to_str().unwrap());
// relative paths ensure that `cc` will preserve directory hierarchy
// which allows C sources with the same file name to be built.
let source = source
.strip_prefix(paths::crate_root())
.expect("All C sources are found within the crate root");
build.file(source);
}
for include_dir in include_dirs {
build.include(include_dir);
}
match wasm {
Some(Wasm::Emscripten) => {
for include_dir in wasm_include_dirs() {
build.include(include_dir);
}
build.define("MRB_API", Some(r#"__attribute__((used))"#));
}
Some(Wasm::Unknown) => {
for include_dir in wasm_include_dirs() {
build.include(include_dir);
}
build.define("MRB_API", Some(r#"__attribute__((visibility("default")))"#));
build.define("MRB_NO_DIRECT_THREADING", None);
}
None => {}
}
build.compile(name);
}
fn bindgen(wasm: Option<Wasm>, out_dir: &OsStr) {
let bindgen_header = paths::bindgen_header();
let header = bindgen_header.to_str().unwrap();
let bindings_out_path = PathBuf::from(out_dir).join("ffi.rs");
let mut builder = bindgen::builder()
.header(header)
.allowlist_function("^mrb.*")
.allowlist_type("^mrb.*")
.allowlist_var("^mrb.*")
.allowlist_var("^MRB.*")
.allowlist_var("^MRUBY.*")
.rustified_enum("^mrb.*")
.generate_comments(false)
.clang_args([
"-DARTICHOKE",
"-DMRB_ARY_NO_EMBED",
"-DMRB_GC_TURN_OFF_GENERATIONAL",
"-DMRB_INT64",
"-DMRB_NO_BOXING",
"-DMRB_NO_PRESYM",
"-DMRB_NO_STDIO",
"-DMRB_UTF8_STRING",
]);
for include_dir in mruby_include_dirs().chain(mrbsys_include_dirs()) {
let include_dir = include_dir.to_str().unwrap();
builder = builder.clang_arg("-I").clang_arg(include_dir);
}
if wasm.is_some() {
for include_dir in wasm_include_dirs() {
let include_dir = include_dir.to_str().unwrap();
builder = builder.clang_arg("-I").clang_arg(include_dir);
}
builder = builder.clang_arg(r#"-DMRB_API=__attribute__((visibility("default")))"#);
}
let bindings = builder.generate().unwrap();
bindings.write_to_file(bindings_out_path).unwrap();
}
pub fn build(wasm: Option<Wasm>, out_dir: &OsStr) {
thread::scope(|s| {
s.spawn(|| {
let include_dirs = mruby_include_dirs()
.chain(mrbgems_include_dirs())
.chain(mrbsys_include_dirs());
let sources = mruby_sources().chain(mrbgems_sources()).chain(mrbsys_sources());
staticlib(wasm, "libartichokemruby.a", include_dirs, sources);
});
s.spawn(|| {
bindgen(wasm, out_dir);
});
});
}
}
#[derive(Debug, Clone, Copy)]
pub enum Wasm {
Emscripten,
Unknown,
}
impl Wasm {
#[must_use]
pub fn from_env() -> Option<Self> {
// Ref: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
let arch =
env::var_os("CARGO_CFG_TARGET_ARCH").expect("cargo-provided CARGO_CFG_TARGET_ARCH env variable not set");
if !matches!(arch.to_str(), Some(arch) if arch == "wasm32") {
return None;
}
let os = env::var_os("CARGO_CFG_TARGET_OS").expect("cargo-provided CARGO_CFG_TARGET_OS env variable not set");
match os.to_str() {
Some("emscripten") => Some(Self::Emscripten),
Some("unknown") => Some(Self::Unknown),
Some(_) | None => None,
}
}
}
fn main() {
let wasm = Wasm::from_env();
if wasm.is_some() {
println!("cargo:rerun-if-env-changed=EMCC_CFLAGS");
}
let out_dir = env::var_os("OUT_DIR").expect("cargo-provided OUT_DIR env variable not set");
libs::build(wasm, &out_dir);
}