From e943ece38873d0cace3deebdb33ae45a40c05ec5 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Thu, 9 Jan 2025 14:18:21 +0100 Subject: [PATCH] Remove `build.rs` for Android when assuming API level is always >=21 Since Rust 1.82 the minimum Android API level is 21. In `backtrace`, this warranted the removal of the API version check via `__ANDROID_API__` in https://github.com/rust-lang/backtrace-rs/pull/656 since it is now always known to be >=21. When `findshlibs` bumps its MSRV the same could be done. This change is sparked by a long search for why backtraces always had `` symbols on Android - and when solving that by upgrading Rust (for `std::backtrace`) or our `backtrace` dependency, why Sentry reports did not record what images/libraries were loaded at which offsets to resolve stack addresses back to function symbols. It turned out that the `xbuild` build tool never set `__ANDROID_API__` (by not telling `clang` about the target API level via the triple) which would cause this code to never set `feature = "dl_iterate_phdr"` to search for libraries on Android: https://github.com/rust-mobile/xbuild/pull/209 --- Cargo.toml | 6 +----- build.rs | 41 ----------------------------------------- src/android-api.c | 4 ---- src/lib.rs | 15 ++++----------- 4 files changed, 5 insertions(+), 61 deletions(-) delete mode 100644 build.rs delete mode 100644 src/android-api.c diff --git a/Cargo.toml b/Cargo.toml index 79ac814..6211774 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,15 +8,11 @@ keywords = ["dyld", "dylib", "shared", "library", "dl_iterate_phdr"] license = "MIT OR Apache-2.0" readme = "./README.md" repository = "https://github.com/gimli-rs/findshlibs" +rust-version = "1.82" [dependencies] libc = "0.2.104" -[build-dependencies] -# Only needed for Android, but cannot be target dependent -# https://github.com/rust-lang/cargo/issues/4932 -cc = "1.0.67" - [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] lazy_static = "1.4" diff --git a/build.rs b/build.rs deleted file mode 100644 index 29415f2..0000000 --- a/build.rs +++ /dev/null @@ -1,41 +0,0 @@ -extern crate cc; - -use std::env; - -fn main() { - match env::var("CARGO_CFG_TARGET_OS").unwrap_or_default().as_str() { - "android" => build_android(), - _ => {} - } -} - -fn build_android() { - let expansion = match cc::Build::new().file("src/android-api.c").try_expand() { - Ok(result) => result, - Err(e) => { - println!("cargo:warning=failed to run C compiler: {}", e); - return; - } - }; - - let expansion = match std::str::from_utf8(&expansion) { - Ok(s) => s, - Err(_) => return, - }; - - let marker = "APIVERSION"; - let i = expansion.find(marker).unwrap_or_default(); - - let version = expansion[i + marker.len() + 1..] - .split_whitespace() - .next() - .unwrap_or(""); - let version = version.parse::().unwrap_or_else(|_| { - println!("cargo:warning=failed to get android api version."); - 0 - }); - - if version >= 21 { - println!("cargo:rustc-cfg=feature=\"dl_iterate_phdr\""); - } -} diff --git a/src/android-api.c b/src/android-api.c deleted file mode 100644 index 8af17b4..0000000 --- a/src/android-api.c +++ /dev/null @@ -1,4 +0,0 @@ -// Used from the build script to detect the value of the `__ANDROID_API__` -// builtin #define - -APIVERSION __ANDROID_API__ \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 08c8a94..302a967 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,10 +100,7 @@ #[cfg(any(target_os = "macos", target_os = "ios"))] pub mod macos; -#[cfg(any( - target_os = "linux", - all(target_os = "android", feature = "dl_iterate_phdr") -))] +#[cfg(any(target_os = "linux", target_os = "android"))] pub mod linux; #[cfg(target_os = "windows")] @@ -111,14 +108,10 @@ pub mod windows; use std::ffi::OsStr; use std::fmt::{self, Debug}; -use std::usize; pub mod unsupported; -#[cfg(any( - target_os = "linux", - all(target_os = "android", feature = "dl_iterate_phdr") -))] +#[cfg(any(target_os = "linux", target_os = "android"))] use crate::linux as native_mod; #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -131,7 +124,7 @@ use crate::windows as native_mod; target_os = "macos", target_os = "ios", target_os = "linux", - all(target_os = "android", feature = "dl_iterate_phdr"), + target_os = "android", target_os = "windows" )))] use unsupported as native_mod; @@ -145,7 +138,7 @@ pub const TARGET_SUPPORTED: bool = cfg!(any( target_os = "macos", target_os = "ios", target_os = "linux", - all(target_os = "android", feature = "dl_iterate_phdr"), + target_os = "android", target_os = "windows" ));