-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
75 lines (67 loc) · 2.75 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
#[cfg(feature="bind_c")]
use std::env;
#[cfg(feature="bind_c")]
use std::path::PathBuf;
fn main() {
// Rerun on file changes
println!("cargo:rerun-if-changed=./witx/*.witx");
println!("cargo:rerun-if-changed=lib/*");
println!("cargo:rerun-if-changed=build.rs");
// Setup WASI root
// https://github.com/bytecodealliance/wasmtime/issues/3519
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
println!("cargo:rustc-env=WASI_ROOT={}/witx", dir);
// Export spec dir for other projects
println!("cargo:ROOT={}", dir);
// Generate C bindings if enabled
#[cfg(feature="bind_c")]
{
// Setup binding generation
let mut builder = bindgen::Builder::default()
.use_core()
.ctypes_prefix("::cty")
.header("inc/wasm_embedded/i2c.h")
.header("inc/wasm_embedded/spi.h")
.header("inc/wasm_embedded/uart.h")
.header("inc/wasm_embedded/gpio.h")
.allowlist_type("wasme.*")
.allowlist_type("i2c.*")
.allowlist_type("spi.*")
.allowlist_type("uart.*")
.allowlist_type("gpio.*");
// Patches to help bindgen with cross compiling
// See: https://github.com/rust-lang/rust-bindgen/issues/1229#issuecomment-366522257
builder = match std::env::var("TARGET").as_deref() {
Ok("armv7-unknown-linux-gnueabihf") => {
println!("cargo:rustc-env=CC=arm-linux-gnueabihf-gcc");
builder
.clang_arg("-target")
.clang_arg("arm-linux-gnueabihf")
.clang_arg("-I/usr/arm-linux-gnueabihf/include/")
}
Ok("aarch64-unknown-linux-gnu") => {
println!("cargo:rustc-env=CC=aarch64-linux-gnu-gcc");
builder
.clang_arg("-target")
.clang_arg("aarch64-linux-gnu")
.clang_arg("-I/usr/aarch64-linux-gnu/include/")
}
Ok("thumbv7em-none-eabihf") => {
println!("cargo:rustc-env=CC=arm-none-eabi-gcc");
builder
.use_core()
.clang_arg("-target")
.clang_arg("arm-none-eabihf")
// TODO: this seems... fragile
.clang_arg("-I/usr/lib/gcc/arm-none-eabi/8.3.1/include/")
}
_ => builder,
};
let bindings = builder.generate().expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
}