Skip to content

Commit

Permalink
Add ability to pass sysroot setting when building w/ bindgen
Browse files Browse the repository at this point in the history
* Allows `cargo ndk build` to build crates that use bindgen

Fixes bbqsrc#52
Closes bbqsrc#53

Co-authored-by: Mackenzie Powers <mackenzie@junelife.com>
  • Loading branch information
2 people authored and bbqsrc committed Mar 10, 2022
1 parent f91b56d commit 0cd72ec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ struct Args {
help = "path to Cargo.toml\n (limitations: https://github.com/rust-lang/cargo/issues/7856)"
)]
manifest_path: Option<PathBuf>,

#[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<PathBuf> {
Expand Down Expand Up @@ -222,6 +229,7 @@ pub(crate) fn run(args: Vec<String>) {
platform,
&args.cargo_args,
cargo_manifest,
args.bindgen,
);
let code = status.code().unwrap_or(-1);

Expand Down

0 comments on commit 0cd72ec

Please sign in to comment.