From 0cd72ec8d89ce726596ab2a910acdc1a76318d0d Mon Sep 17 00:00:00 2001 From: Lucas Jenss Date: Wed, 9 Mar 2022 14:39:57 +0100 Subject: [PATCH] Add ability to pass sysroot setting when building w/ bindgen * Allows `cargo ndk build` to build crates that use bindgen Fixes #52 Closes #53 Co-authored-by: Mackenzie Powers --- src/cargo.rs | 20 ++++++++++++++++++++ src/cli.rs | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/src/cargo.rs b/src/cargo.rs index a0978a3..9670b04 100644 --- a/src/cargo.rs +++ b/src/cargo.rs @@ -57,6 +57,18 @@ fn toolchain_suffix(triple: &str, arch: &str, bin: &str) -> PathBuf { .collect() } +fn sysroot_suffix(arch: &str) -> PathBuf { + [ + "toolchains", + "llvm", + "prebuilt", + arch, + "sysroot" + ] + .iter() + .collect() +} + fn cargo_env_target_cfg(triple: &str, key: &str) -> String { format!("CARGO_TARGET_{}_{}", &triple.replace("-", "_"), key).to_uppercase() } @@ -68,14 +80,17 @@ pub(crate) fn run( platform: u8, cargo_args: &[String], cargo_manifest: &Path, + bindgen: bool, ) -> std::process::ExitStatus { let target_ar = Path::new(&ndk_home).join(toolchain_suffix(triple, ARCH, "ar")); let target_linker = Path::new(&ndk_home).join(clang_suffix(triple, ARCH, platform, "")); let target_cxx = Path::new(&ndk_home).join(clang_suffix(triple, ARCH, platform, "++")); + let target_sysroot = Path::new(&ndk_home).join(sysroot_suffix(ARCH)); let cc_key = format!("CC_{}", &triple); let ar_key = format!("AR_{}", &triple); let cxx_key = format!("CXX_{}", &triple); + let bindgen_clang_args_key = format!("BINDGEN_EXTRA_CLANG_ARGS_{}", &triple); let cargo_bin = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()); log::debug!("cargo: {}", &cargo_bin); @@ -84,6 +99,7 @@ pub(crate) fn run( log::debug!("{}={}", &cxx_key, &target_cxx.display()); log::debug!("{}={}", cargo_env_target_cfg(&triple, "ar"), &target_ar.display()); log::debug!("{}={}", cargo_env_target_cfg(&triple, "linker"), &target_linker.display()); + log::debug!("{}={}", &bindgen_clang_args_key, &target_sysroot.display()); log::debug!("Args: {:?}", &cargo_args); let mut cargo_cmd = Command::new(cargo_bin); @@ -96,6 +112,10 @@ pub(crate) fn run( .env(cargo_env_target_cfg(triple, "linker"), &target_linker) .args(cargo_args); + if bindgen { + cargo_cmd.env(bindgen_clang_args_key, format!("--sysroot={}", &target_sysroot.display())); + } + match dir.parent() { Some(parent) => { if parent != dir { diff --git a/src/cli.rs b/src/cli.rs index f63cfec..5e9748e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -48,6 +48,13 @@ struct Args { help = "path to Cargo.toml\n (limitations: https://github.com/rust-lang/cargo/issues/7856)" )] manifest_path: Option, + + #[options( + no_short, + help = "set bindgen-specific environment variables (BINDGEN_EXTRA_CLANG_ARGS_*) when building", + default = "false" + )] + bindgen: bool, } fn highest_version_ndk_in_path(ndk_dir: &Path) -> Option { @@ -222,6 +229,7 @@ pub(crate) fn run(args: Vec) { platform, &args.cargo_args, cargo_manifest, + args.bindgen, ); let code = status.code().unwrap_or(-1);