-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rs
182 lines (153 loc) · 5.47 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
extern crate bindgen;
use ::std::{env, fmt};
use ::std::path::PathBuf;
use ::std::process::{Command, Stdio};
fn build_jsc(cargo_manifest_dir: &PathBuf) -> self::fmt::Result {
// Initial build as JSCOnly;static;debug
match Command::new("make")
.args(&[
"-R",
"-f",
cargo_manifest_dir.join("makefile.cargo").to_str().expect("UTF-8"),
])
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.status() {
// Make sure our compilation succeeded; else bail
Ok(r) => assert!(r.success()),
Err(e) => panic!("Make command failed, err: {:?}",e),
}
Ok(())
}
fn generate_bindings(build_dir: &PathBuf, cargo_manifest_dir: &PathBuf) -> self::fmt::Result {
// Based on our build target, bind path of our FFI
// headers to inc_dir for use with bindgen
let inc_dir = if cfg!(target_os = "macos") {
// /Library/Developer/CommandLineTools/SDKs/MacOSX.*sdk
let output = Command::new("xcrun")
.arg("-show-sdk-path")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.expect("failed to execute xcrun")
.stdout;
String::from_utf8(output).unwrap()
} else { // target_os = "linux"
// ${OUT_DIR}/build/JavaScriptCore/Headers
format!(
"{}", build_dir.join("JavaScriptCore").join("Headers").display()
)
};
let mut builder = bindgen::builder()
.rust_target(bindgen::LATEST_STABLE_RUST)
.header(
cargo_manifest_dir
.join("WebKit")
.join("Source")
.join("JavaScriptCore")
.join("API")
.join("JavaScript.h")
.to_str().expect("UTF-8")
)
.clang_args(&["-I", &inc_dir])
.enable_cxx_namespaces()
// Translate every enum with the "rustified enum" strategy. We should
// investigate switching to the "constified module" strategy, which has
// similar ergonomics but avoids some potential Rust UB footguns.
.rustified_enum(".*")
// Translates csize_t to rust usize
.size_t_is_usize(true);
for ty in ALLOWLIST_TYPES {
builder = builder.allowlist_type(ty);
}
for func in ALLOWLIST_FUNCTIONS {
builder = builder.allowlist_function(func);
}
for item in BLOCKLIST_ITEMS {
builder = builder.blocklist_item(item);
}
let bindings = builder
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(build_dir.join("bindings.rs"))
.expect("Couldn't write bindings!");
Ok(())
}
fn main() {
let cargo_manifest_dir = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap());
let build_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("build");
// Bail if JSC build fails
assert_eq!(
build_jsc(&cargo_manifest_dir),
Ok(())
);
// Link our freshly built static libraries
// Applicable to both darwin and gnu
println!("cargo:rustc-link-search=all={}/lib", cargo_manifest_dir.display());
println!("cargo:rustc-link-lib=static=JavaScriptCore");
println!("cargo:rustc-link-lib=static=WTF");
println!("cargo:rustc-link-lib=static=bmalloc");
if cfg!(target_os = "macos") {
// x86_64-apple-darwin
println!("cargo:rustc-link-lib=icucore");
println!("cargo:rustc-link-lib=c++");
} else {
// target_os = "linux"
// x86_64-unknown-linux-gnu
println!("cargo:rustc-link-lib=icui18n");
println!("cargo:rustc-link-lib=icuuc");
println!("cargo:rustc-link-lib=icudata");
println!("cargo:rustc-link-lib=stdc++");
}
// Bail if bindgen fails
assert_eq!(
generate_bindings(&build_dir, &cargo_manifest_dir),
Ok(())
);
}
/// Types which we want to generate bindings for (and every other type they
/// transitively use).
const ALLOWLIST_TYPES: &'static [&'static str] = &[
// A group that associates JavaScript execution contexts with one another.
"JSContextGroupRef",
// A JavaScript execution context.
"JSContextRef",
// A global JavaScript execution context.
"JSGlobalContextRef",
// A UTF-16 character buffer.
"JSStringRef",
// A JavaScript class.
"JSClassRef",
// A JavaScript value.
"JSValueRef",
// A JavaScript object.
"JSObjectRef",
];
/// Functions we want to generate bindings to.
const ALLOWLIST_FUNCTIONS: &'static [&'static str] = &[
// Checks for syntax errors in a string of JavaScript.
"JSCheckScriptSyntax",
// Evaluates a string of JavaScript.
"JSEvaluateScript",
// Performs a JavaScript garbage collection.
"JSGarbageCollect",
// Impls for allowlisted types
"JSContextGroup.*",
"JSContext.*",
"JSGlobalContext.*",
"JSString.*",
"JSClass.*",
"JSValue.*",
"JSObject.*",
];
/// Types for which we should NEVER generate bindings, even if it is used within
/// a type or function signature that we are generating bindings for.
const BLOCKLIST_ITEMS: &'static [&'static str] = &[
// Functions for which we should NEVER generate bindings to.
//"JSString.*CFString.*",
// Types for which we should NEVER generate bindings, even if it is used within
// a type or function signature that we are generating bindings for.
//"CFString.*",
];