Skip to content

Commit a23b95b

Browse files
committed
fix: set CMAKE_ANDROID_NDK for Android cross-compilation
cmake-rs sets CMAKE_SYSTEM_NAME=Android but not CMAKE_ANDROID_NDK, causing CMake >= 3.21 to fail with 'Neither the NDK or a standalone toolchain was found'. Detect NDK root from environment variables and pass it to cmake::Config.
1 parent 0bf93a1 commit a23b95b

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ license = "MIT"
99

1010
[build-dependencies]
1111
cmake = "0.1"
12+
cc = "1"
1213
bindgen = "0.72"

build.rs

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,61 @@
11
use std::env;
2-
use std::path::PathBuf;
2+
use std::path::{Path, PathBuf};
3+
4+
/// When cross-compiling for Android, the `cmake` crate (cmake-rs) sets
5+
/// `CMAKE_SYSTEM_NAME=Android` but does **not** set `CMAKE_ANDROID_NDK`.
6+
/// CMake ≥ 3.21's `Platform/Android-Determine.cmake` then fails because it
7+
/// cannot locate the NDK.
8+
///
9+
/// Detection strategy:
10+
/// 1. Check well-known environment variables (`ANDROID_NDK_ROOT`, etc.).
11+
/// 2. Infer from the C compiler path that `cc` selects for this target.
12+
/// NDK compilers live at `<NDK>/toolchains/llvm/prebuilt/<host>/bin/…`,
13+
/// so we walk up from the compiler looking for the `toolchains` dir.
14+
fn detect_android_ndk() -> Option<PathBuf> {
15+
// 1. Prefer explicit env vars (same ones CMake itself checks)
16+
for var in ["ANDROID_NDK_ROOT", "ANDROID_NDK_HOME", "ANDROID_NDK"] {
17+
if let Ok(val) = env::var(var) {
18+
let p = PathBuf::from(&val);
19+
if p.is_dir() {
20+
return Some(p);
21+
}
22+
}
23+
}
24+
25+
// 2. Infer from the C compiler path
26+
let compiler = cc::Build::new()
27+
.cargo_metadata(false)
28+
.opt_level(0)
29+
.warnings(false)
30+
.try_get_compiler()
31+
.ok()?;
32+
let cc_path = compiler.path().canonicalize().ok()?;
33+
let mut dir: &Path = cc_path.parent()?;
34+
loop {
35+
if dir.file_name().and_then(|n| n.to_str()) == Some("toolchains")
36+
&& dir.join("llvm").is_dir()
37+
{
38+
return dir.parent().map(|p| p.to_path_buf());
39+
}
40+
dir = dir.parent()?;
41+
}
42+
}
343

444
fn main() {
45+
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
46+
547
// Build libversion static library using cmake
6-
let dst = cmake::Config::new("libversion")
7-
.build_target("libversion_static")
8-
.build();
48+
let mut cmake_cfg = cmake::Config::new("libversion");
49+
cmake_cfg.build_target("libversion_static");
50+
51+
// Work around cmake-rs not setting CMAKE_ANDROID_NDK for Android targets.
52+
if target_os == "android" {
53+
if let Some(ndk_root) = detect_android_ndk() {
54+
cmake_cfg.define("CMAKE_ANDROID_NDK", &ndk_root);
55+
}
56+
}
57+
58+
let dst = cmake_cfg.build();
959

1060
let build_dir = dst.join("build").join("libversion");
1161
println!("cargo:rustc-link-search=native={}", build_dir.display());

0 commit comments

Comments
 (0)