Skip to content

Commit 8d5c593

Browse files
committed
fix(libquickjs-sys): improve cross-compilation support for Android in build script
1 parent 4d1dc54 commit 8d5c593

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

libquickjs-sys/build.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ fn apply_patches(code_dir: &PathBuf) {
6565
}
6666

6767
fn compile_lib(code_dir: &Path) {
68-
cc::Build::new()
69-
.compiler("clang")
68+
let mut builder = cc::Build::new();
69+
70+
// android ndk has its own clang compiler, so we can't use the default one
71+
if !is_cargo_ndk() {
72+
builder.compiler("clang");
73+
}
74+
75+
builder
7076
.files(
7177
[
7278
// extensions.c has included quickjs.c
@@ -105,16 +111,34 @@ fn compile_lib(code_dir: &Path) {
105111
fn do_bindgen() {
106112
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
107113

108-
bindgen::Builder::default()
114+
let builder = bindgen::Builder::default()
109115
.header("embed/extensions.h")
110116
.allowlist_item("js_.+")
111117
.allowlist_item("JS.+")
112118
.clang_arg("-std=c11")
113-
.clang_arg(format!("-I{}", "embed/quickjs"))
119+
.clang_arg(format!("-I{}", "embed/quickjs"));
120+
121+
// detect if we are cross-compiling for android using cargo-ndk
122+
let builder = if is_cargo_ndk() {
123+
let target = env::var("TARGET").unwrap();
124+
let ndk_sysroot_path = env::var("CARGO_NDK_SYSROOT_PATH").unwrap();
125+
builder
126+
.clang_arg(format!("--sysroot={ndk_sysroot_path}"))
127+
.clang_arg(format!("--target={}", target))
128+
} else {
129+
builder
130+
};
131+
132+
builder
114133
.default_enum_style(bindgen::EnumVariation::Consts {})
115134
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
116135
.generate()
117136
.expect("Unable to generate bindings")
118137
.write_to_file(out_path.join("bindings.rs"))
119138
.expect("Couldn't write bindings!");
120139
}
140+
141+
fn is_cargo_ndk() -> bool {
142+
// cargo-ndk sets this variable so we use it to detect if we are cross-compiling for android
143+
env::var("CARGO_NDK_ANDROID_PLATFORM").is_ok()
144+
}

0 commit comments

Comments
 (0)