forked from wasm3/wasm3-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
160 lines (145 loc) · 4.67 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
use std::env;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
static WASM3_SOURCE: &str = "wasm3/source";
const WHITELIST_REGEX_FUNCTION: &str = "([A-Z]|m3_).*";
const WHITELIST_REGEX_TYPE: &str = "(?:I|c_)?[Mm]3.*";
const WHITELIST_REGEX_VAR: &str = WHITELIST_REGEX_TYPE;
const PRIMITIVES: &[&str] = &[
"f64", "f32", "u64", "i64", "u32", "i32", "u16", "i16", "u8", "i8",
];
fn gen_wrapper(out_path: &Path) -> PathBuf {
let wrapper_file = out_path.join("wrapper.h");
let header_files = [
"wasm3.h",
#[cfg(feature = "wasi")]
"m3_api_wasi.h",
];
let contents = header_files
.map(|header_file| format!("#include \"{}\"\n", header_file))
.join("");
fs::write(&wrapper_file, contents).expect("failed to create wasm3 wrapper file");
wrapper_file
}
#[cfg(not(feature = "build-bindgen"))]
fn gen_bindings() {
let out_path = PathBuf::from(&env::var("OUT_DIR").unwrap());
let wrapper_file = gen_wrapper(&out_path);
let mut bindgen = std::process::Command::new("bindgen");
bindgen
.arg(wrapper_file)
.arg("--use-core")
.arg("--ctypes-prefix")
.arg("cty")
.arg("--no-layout-tests")
.arg("--default-enum-style=moduleconsts")
.arg("--no-doc-comments")
.arg("--whitelist-function")
.arg(WHITELIST_REGEX_FUNCTION)
.arg("--whitelist-type")
.arg(WHITELIST_REGEX_TYPE)
.arg("--whitelist-var")
.arg(WHITELIST_REGEX_VAR)
.arg("--no-derive-debug");
for &ty in PRIMITIVES.iter() {
bindgen.arg("--blacklist-type").arg(ty);
}
bindgen
.arg("-o")
.arg(out_path.join("bindings.rs").to_str().unwrap());
bindgen
.arg("--")
.arg(format!(
"-Dd_m3Use32BitSlots={}",
if cfg!(feature = "use-32bit-slots") {
1
} else {
0
}
))
.arg("-Dd_m3LogOutput=0")
.arg("-Iwasm3/source");
let status = bindgen.status().expect("Unable to generate bindings");
if !status.success() {
panic!("Failed to run bindgen: {:?}", status);
}
}
#[cfg(feature = "build-bindgen")]
fn gen_bindings() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let wrapper_file = gen_wrapper(&out_path);
let mut bindgen = bindgen::builder()
.header(wrapper_file.to_str().unwrap())
.use_core()
.ctypes_prefix("cty")
.layout_tests(false)
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.generate_comments(false)
.allowlist_function(WHITELIST_REGEX_FUNCTION)
.allowlist_type(WHITELIST_REGEX_TYPE)
.allowlist_var(WHITELIST_REGEX_VAR)
.derive_debug(false);
bindgen = PRIMITIVES
.iter()
.fold(bindgen, |bindgen, ty| bindgen.blocklist_type(ty));
bindgen
.clang_args(
[
&format!(
"-Dd_m3Use32BitSlots={}",
if cfg!(feature = "use-32bit-slots") {
1
} else {
0
}
),
"-Dd_m3LogOutput=0",
"-Iwasm3/source",
]
.iter(),
)
.generate()
.expect("Failed to generate bindings")
.write_to_file(out_path.join("bindings.rs").to_str().unwrap())
.expect("Failed to write bindings");
}
fn main() {
gen_bindings();
let mut cfg = cc::Build::new();
cfg.files(
fs::read_dir(WASM3_SOURCE)
.unwrap_or_else(|_| panic!("failed to read {} directory", WASM3_SOURCE))
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|p| p.extension().and_then(OsStr::to_str) == Some("c")),
);
cfg.cpp(false)
.define("d_m3LogOutput", Some("0"))
.warnings(false)
.extra_warnings(false)
.include(WASM3_SOURCE);
// Add any extra arguments from the environment to the CC command line.
if let Ok(extra_clang_args) = std::env::var("BINDGEN_EXTRA_CLANG_ARGS") {
// Try to parse it with shell quoting. If we fail, make it one single big argument.
if let Some(strings) = shlex::split(&extra_clang_args) {
strings.iter().for_each(|string| {
cfg.flag(string);
})
} else {
cfg.flag(&extra_clang_args);
};
}
if cfg!(feature = "wasi") {
cfg.define("d_m3HasWASI", None);
}
cfg.define(
"d_m3Use32BitSlots",
if cfg!(feature = "use-32bit-slots") {
Some("1")
} else {
Some("0")
},
);
cfg.compile("wasm3");
}