Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub(crate) fn build_env(
ndk_home: &Path,
clang_target: &str,
bindgen: bool,
link_builtins: bool
) -> BTreeMap<String, OsString> {
let self_path = std::fs::canonicalize(env::args().next().unwrap())
.expect("Failed to canonicalize absolute path to cargo-ndk")
Expand All @@ -117,7 +118,6 @@ pub(crate) fn build_env(
// Environment variables for cargo
let cargo_ar_key = cargo_env_target_cfg(triple, "ar");
let cargo_linker_key = cargo_env_target_cfg(triple, "linker");
let cargo_rust_flags_key = cargo_env_target_cfg(triple, "rustflags");
let bindgen_clang_args_key = format!("BINDGEN_EXTRA_CLANG_ARGS_{}", &triple.replace('-', "_"));

let target_cc = ndk_home.join(ndk_tool(ARCH, "clang"));
Expand Down Expand Up @@ -152,20 +152,6 @@ pub(crate) fn build_env(
.join("lib")
.join("clang");

// choose the clang target with the highest version
// Should we filter for only numbers?
let clang_builtins_target = fs::read_dir(clang_folder)
.expect("Unable to get clang target directory")
.filter_map(|a| a.ok())
.max_by(|a, b| a.file_name().cmp(&b.file_name()))
.expect("Unable to get clang target")
.path();
let clang_rt = format!(
"-L{} -lstatic=clang_rt.builtins-{}-android",
clang_builtins_target.join("lib").join("linux").display(),
rt_builtins(triple)
);

let extra_include = format!(
"{}/usr/include/{}",
&cargo_ndk_sysroot_path.display(),
Expand Down Expand Up @@ -193,7 +179,6 @@ pub(crate) fn build_env(
cargo_ndk_sysroot_target_key.to_string(),
cargo_ndk_sysroot_target.into(),
),
(cargo_rust_flags_key, clang_rt.into()),
// Found this through a comment related to bindgen using the wrong clang for cross compiles
//
// https://github.com/rust-lang/rust-bindgen/issues/2962#issuecomment-2438297124
Expand All @@ -218,6 +203,32 @@ pub(crate) fn build_env(
.collect();
}

if link_builtins {
// TODO: Can we do this without RUSTFLAGS?
let cargo_rust_flags_key = cargo_env_target_cfg(triple, "rustflags");

// choose the clang target with the highest version
// Should we filter for only numbers?
let clang_builtins_target = fs::read_dir(clang_folder)
.expect("Unable to get clang target directory")
.filter_map(|a| a.ok())
.max_by(|a, b| a.file_name().cmp(&b.file_name()))
.expect("Unable to get clang target")
.path();


let clang_rt = format!(
"-L{} -lstatic=clang_rt.builtins-{}-android",
clang_builtins_target.join("lib").join("linux").display(),
rt_builtins(triple)
);

envs.insert(
cargo_rust_flags_key,
clang_rt.into(),
);
}

if bindgen {
let bindgen_args = format!(
"--sysroot={} -I{}",
Expand Down Expand Up @@ -257,6 +268,7 @@ pub(crate) fn run(
cargo_args: &[String],
cargo_manifest: &Path,
bindgen: bool,
link_builtins: bool,
#[allow(unused_variables)] out_dir: &Utf8PathBuf,
) -> Result<(std::process::ExitStatus, Vec<Artifact>)> {
if version.major < 23 {
Expand All @@ -276,7 +288,7 @@ pub(crate) fn run(
let clang_target = clang_target(triple, platform);
let cargo_bin = env::var("CARGO").unwrap_or_else(|_| "cargo".into());
let mut cargo_cmd = Command::new(&cargo_bin);
let envs = build_env(triple, ndk_home, &clang_target, bindgen);
let envs = build_env(triple, ndk_home, &clang_target, bindgen, link_builtins);

shell
.very_verbose(|shell| {
Expand Down
17 changes: 16 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ struct ArgsEnv {
)]
bindgen: bool,

#[options(
no_short,
help = "Enable linking clang_rt. Solves various issues related to compiling Rust and C++ using cc. Note however this will override RUSTFLAGS",
default = "false"
)]
link_builtins: bool,

#[options(
help = "triples for the target. Additionally, Android target names are supported: armeabi-v7a arm64-v8a x86 x86_64"
)]
Expand Down Expand Up @@ -88,6 +95,13 @@ struct Args {
)]
bindgen: bool,

#[options(
no_short,
help = "Enable linking clang_rt. Solves various issues related to compiling Rust and C++ using cc. Note however this will override RUSTFLAGS",
default = "false"
)]
link_builtins: bool,

#[options(
help = "triples for the target(s). Additionally, Android target names are supported: armeabi-v7a arm64-v8a x86 x86_64"
)]
Expand Down Expand Up @@ -375,7 +389,7 @@ pub fn run_env(args: Vec<String>) -> anyhow::Result<()> {
);

// Try command line, then config. Config falls back to defaults in any case.
let env = build_env(args.target.triple(), &ndk_home, &clang_target, args.bindgen)
let env = build_env(args.target.triple(), &ndk_home, &clang_target, args.bindgen, args.link_builtins)
.into_iter()
.filter(|(k, _)| !k.starts_with('_'))
.collect::<BTreeMap<_, _>>();
Expand Down Expand Up @@ -691,6 +705,7 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
&args.cargo_args,
&cargo_manifest,
args.bindgen,
args.link_builtins,
&out_dir,
)?;
let code = status.code().unwrap_or(-1);
Expand Down
Loading