Skip to content

Commit 42648ec

Browse files
committed
android: Set SDK/API level via version-suffxed --target triple
We haven't set the SDK/API level via the `__ANDROID_API__` define for a very long time and so far got away with it. However, while debugging why `backtrace` (and by extension Rust `std` which reuses that crate) wasn't generating symbolicated stacktraces in `panic_log`, and why `findshlibs` wasn't providing the list of loaded libraries to `sentry`, we found that both rely on expanding the `__ANDROID_API__` define via compiling a small C file via `cc` to make the code conditional on SDK/API >= 21: gimli-rs/findshlibs#65 rust-lang/backtrace-rs#415 (It would have been lovely if these crates emitted a `cargo:warning` when the define wasn't set at all, indicating an "incomplete" cross-compiler setup, and/or looked at the runtime Android API version via something like rust-mobile/ndk#479.) Note that `backtrace 0.3.74` / Rust 1.82 (rust-lang/rust@0763a3a) no longer rely on this because Rust 1.82 bumped the minimum SDK/API level to 21: rust-lang/backtrace-rs#656 We could set this define directly, or rely on `clang` to set it for us by appending the SDK/API level to the target triple, of the form `<arch>-linux-android<sdk level>`. The latter is more common. Keep in mind that the `cc` crate adds an unversioned `--target=<arch>-linux-android` to the command line arguments as well, but clang seems to deduplicate them (or look at the latter `--target` which contains our version). Note that this effectively reverts 32efed6 because we must now always pass the SDK level via the triple again, even if the host also happens to be Android with the same architecture.
1 parent 5662af2 commit 42648ec

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

xbuild/src/cargo/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,14 @@ impl CargoBuild {
281281
pub fn use_android_ndk(&mut self, path: &Path, target_sdk_version: u32) -> Result<()> {
282282
let path = dunce::canonicalize(path)?;
283283
let ndk_triple = self.target.ndk_triple();
284+
assert_eq!(Some(ndk_triple), self.triple);
285+
let ndk_versioned_triple = format!("{ndk_triple}{target_sdk_version}");
284286
self.cfg_tool(Tool::Cc, "clang");
285287
self.cfg_tool(Tool::Cxx, "clang++");
286288
self.cfg_tool(Tool::Ar, "llvm-ar");
287289
self.cfg_tool(Tool::Linker, "clang");
288290
self.set_sysroot(&path);
291+
self.add_cflag(&format!("--target={ndk_versioned_triple}"));
289292
self.add_cxxflag("-stdlib=libc++");
290293
let lib_dir = path.join("usr").join("lib").join(ndk_triple);
291294
let sdk_lib_dir = lib_dir.join(target_sdk_version.to_string());
@@ -295,9 +298,7 @@ impl CargoBuild {
295298
target_sdk_version
296299
);
297300
self.use_ld("lld");
298-
if let Some(triple) = self.triple {
299-
self.add_link_arg(&format!("--target={}", triple));
300-
}
301+
self.add_link_arg(&format!("--target={ndk_versioned_triple}"));
301302
self.add_link_arg(&format!("-B{}", sdk_lib_dir.display()));
302303
self.add_link_arg(&format!("-L{}", sdk_lib_dir.display()));
303304
self.add_link_arg(&format!("-L{}", lib_dir.display()));

0 commit comments

Comments
 (0)