Skip to content

Commit e9fd58e

Browse files
StantonMattbbqsrc
authored andcommitted
Export Android platform vars from ndk-env
Signed-off-by: Matthew Stanton <stantonmatthewj@gmail.com>
1 parent 9672f44 commit e9fd58e

4 files changed

Lines changed: 57 additions & 9 deletions

File tree

src/cargo.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,14 @@ fn is_64bit(triple: &str) -> bool {
6666
triple.starts_with("aarch64") || triple.starts_with("x86_64")
6767
}
6868

69+
#[allow(clippy::too_many_arguments)]
6970
pub(crate) fn build_env(
7071
triple: &str,
7172
ndk_home: &Path,
7273
ndk_version: &Version,
7374
clang_target: &str,
75+
platform: u8,
76+
android_abi: &str,
7477
link_builtins: bool,
7578
link_cxx_shared: bool,
7679
) -> BTreeMap<String, OsString> {
@@ -146,6 +149,12 @@ pub(crate) fn build_env(
146149
CARGO_NDK_SYSROOT_TARGET_KEY.to_string(),
147150
cargo_ndk_sysroot_target.into(),
148151
),
152+
(
153+
"CARGO_NDK_ANDROID_PLATFORM".to_string(),
154+
platform.to_string().into(),
155+
),
156+
("ANDROID_PLATFORM".to_string(), platform.to_string().into()),
157+
("ANDROID_ABI".to_string(), android_abi.into()),
149158
// https://github.com/KyleMayes/clang-sys?tab=readme-ov-file#environment-variables
150159
("CLANG_PATH".into(), target_cc.clone().into()),
151160
("_CARGO_NDK_LINK_TARGET".into(), clang_target.into()), // Recognized by main() so we know when we're acting as a wrapper
@@ -241,6 +250,7 @@ pub(crate) fn run(
241250
version: &Version,
242251
triple: &str,
243252
platform: u8,
253+
android_abi: &str,
244254
link_builtins: bool,
245255
link_cxx_shared: bool,
246256
cargo_args: &[String],
@@ -260,6 +270,8 @@ pub(crate) fn run(
260270
ndk_home,
261271
version,
262272
&clang_target,
273+
platform,
274+
android_abi,
263275
link_builtins,
264276
link_cxx_shared,
265277
);
@@ -299,12 +311,10 @@ pub(crate) fn run(
299311
let mut cargo_args = cargo_args.to_vec();
300312

301313
match dir.parent() {
302-
Some(parent) => {
303-
if parent != dir {
304-
// log::debug!("Working directory does not match manifest-path");
305-
cargo_args.push("--manifest-path".into());
306-
cargo_args.push(cargo_manifest.into());
307-
}
314+
Some(parent) if parent != dir => {
315+
// log::debug!("Working directory does not match manifest-path");
316+
cargo_args.push("--manifest-path".into());
317+
cargo_args.push(cargo_manifest.into());
308318
}
309319
_ => {
310320
// log::warn!("Parent of current working directory does not exist");
@@ -346,3 +356,30 @@ pub(crate) fn run(
346356

347357
Ok((status, artifacts))
348358
}
359+
360+
#[cfg(test)]
361+
mod tests {
362+
use std::path::Path;
363+
364+
use cargo_metadata::semver::Version;
365+
366+
use super::build_env;
367+
368+
#[test]
369+
fn build_env_exports_android_platform_and_abi() {
370+
let env = build_env(
371+
"aarch64-linux-android",
372+
Path::new("/opt/android-ndk"),
373+
&Version::new(28, 0, 0),
374+
"--target=aarch64-linux-android28",
375+
28,
376+
"arm64-v8a",
377+
false,
378+
false,
379+
);
380+
381+
assert_eq!(env["CARGO_NDK_ANDROID_PLATFORM"], "28");
382+
assert_eq!(env["ANDROID_PLATFORM"], "28");
383+
assert_eq!(env["ANDROID_ABI"], "arm64-v8a");
384+
}
385+
}

src/cli/env.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
6060
&ndk_home,
6161
&ndk_version,
6262
&clang_target,
63+
args.platform,
64+
&args.target.to_string(),
6365
args.link_builtins,
6466
args.link_libcxx_shared,
6567
)

src/cli/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
654654
)
655655
})?;
656656
unsafe {
657-
std::env::set_var("ANDROID_ABI", android_abi);
657+
std::env::set_var("ANDROID_ABI", &android_abi);
658658
}
659659

660660
let (status, artifacts) = crate::cargo::run(
@@ -664,6 +664,7 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
664664
&ndk_version,
665665
triple,
666666
platform,
667+
&android_abi,
667668
args.link_builtins,
668669
args.link_libcxx_shared,
669670
&args.cargo_args,
@@ -758,9 +759,15 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
758759
.filter(|a| artifact_is_cdylib(a))
759760
.filter(|a| a.package_id == current_package_id)
760761
{
761-
let Some(file) = artifact.filenames.iter().find(|name| name.extension() == Some("so"))
762+
let Some(file) = artifact
763+
.filenames
764+
.iter()
765+
.find(|name| name.extension() == Some("so"))
762766
else {
763-
shell.error(format!("No cdylib file found to copy in\n{:#?}", artifact.filenames))?;
767+
shell.error(format!(
768+
"No cdylib file found to copy in\n{:#?}",
769+
artifact.filenames
770+
))?;
764771
std::process::exit(1);
765772
};
766773
let dest = arch_output_dir.join(file.file_name().unwrap());

src/cli/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ pub fn run(args: Vec<String>) -> anyhow::Result<()> {
139139
&ndk_home,
140140
&ndk_version,
141141
&clang_target,
142+
platform,
143+
&target.to_string(),
142144
args.link_builtins,
143145
args.link_cxx_shared,
144146
);

0 commit comments

Comments
 (0)