Skip to content
Merged
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
49 changes: 43 additions & 6 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ fn is_64bit(triple: &str) -> bool {
triple.starts_with("aarch64") || triple.starts_with("x86_64")
}

#[allow(clippy::too_many_arguments)]
pub(crate) fn build_env(
triple: &str,
ndk_home: &Path,
ndk_version: &Version,
clang_target: &str,
platform: u8,
android_abi: &str,
link_builtins: bool,
link_cxx_shared: bool,
) -> BTreeMap<String, OsString> {
Expand Down Expand Up @@ -146,6 +149,12 @@ pub(crate) fn build_env(
CARGO_NDK_SYSROOT_TARGET_KEY.to_string(),
cargo_ndk_sysroot_target.into(),
),
(
"CARGO_NDK_ANDROID_PLATFORM".to_string(),
platform.to_string().into(),
),
("ANDROID_PLATFORM".to_string(), platform.to_string().into()),
("ANDROID_ABI".to_string(), android_abi.into()),
// https://github.com/KyleMayes/clang-sys?tab=readme-ov-file#environment-variables
("CLANG_PATH".into(), target_cc.clone().into()),
("_CARGO_NDK_LINK_TARGET".into(), clang_target.into()), // Recognized by main() so we know when we're acting as a wrapper
Expand Down Expand Up @@ -241,6 +250,7 @@ pub(crate) fn run(
version: &Version,
triple: &str,
platform: u8,
android_abi: &str,
link_builtins: bool,
link_cxx_shared: bool,
cargo_args: &[String],
Expand All @@ -260,6 +270,8 @@ pub(crate) fn run(
ndk_home,
version,
&clang_target,
platform,
android_abi,
link_builtins,
link_cxx_shared,
);
Expand Down Expand Up @@ -299,12 +311,10 @@ pub(crate) fn run(
let mut cargo_args = cargo_args.to_vec();

match dir.parent() {
Some(parent) => {
if parent != dir {
// log::debug!("Working directory does not match manifest-path");
cargo_args.push("--manifest-path".into());
cargo_args.push(cargo_manifest.into());
}
Some(parent) if parent != dir => {
// log::debug!("Working directory does not match manifest-path");
cargo_args.push("--manifest-path".into());
cargo_args.push(cargo_manifest.into());
}
_ => {
// log::warn!("Parent of current working directory does not exist");
Expand Down Expand Up @@ -346,3 +356,30 @@ pub(crate) fn run(

Ok((status, artifacts))
}

#[cfg(test)]
mod tests {
use std::path::Path;

use cargo_metadata::semver::Version;

use super::build_env;

#[test]
fn build_env_exports_android_platform_and_abi() {
let env = build_env(
"aarch64-linux-android",
Path::new("/opt/android-ndk"),
&Version::new(28, 0, 0),
"--target=aarch64-linux-android28",
28,
"arm64-v8a",
false,
false,
);

assert_eq!(env["CARGO_NDK_ANDROID_PLATFORM"], "28");
assert_eq!(env["ANDROID_PLATFORM"], "28");
assert_eq!(env["ANDROID_ABI"], "arm64-v8a");
}
}
2 changes: 2 additions & 0 deletions src/cli/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
&ndk_home,
&ndk_version,
&clang_target,
args.platform,
&args.target.to_string(),
args.link_builtins,
args.link_libcxx_shared,
)
Expand Down
13 changes: 10 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
)
})?;
unsafe {
std::env::set_var("ANDROID_ABI", android_abi);
std::env::set_var("ANDROID_ABI", &android_abi);
}

let (status, artifacts) = crate::cargo::run(
Expand All @@ -664,6 +664,7 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
&ndk_version,
triple,
platform,
&android_abi,
args.link_builtins,
args.link_libcxx_shared,
&args.cargo_args,
Expand Down Expand Up @@ -758,9 +759,15 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
.filter(|a| artifact_is_cdylib(a))
.filter(|a| a.package_id == current_package_id)
{
let Some(file) = artifact.filenames.iter().find(|name| name.extension() == Some("so"))
let Some(file) = artifact
.filenames
.iter()
.find(|name| name.extension() == Some("so"))
else {
shell.error(format!("No cdylib file found to copy in\n{:#?}", artifact.filenames))?;
shell.error(format!(
"No cdylib file found to copy in\n{:#?}",
artifact.filenames
))?;
std::process::exit(1);
};
let dest = arch_output_dir.join(file.file_name().unwrap());
Expand Down
2 changes: 2 additions & 0 deletions src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
&ndk_home,
&ndk_version,
&clang_target,
platform,
&target.to_string(),
args.link_builtins,
args.link_cxx_shared,
);
Expand Down
Loading