diff --git a/Cargo.toml b/Cargo.toml index 71db617fa..a72d53369 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "backtrace" version = "0.3.57" authors = ["The Rust Project Developers"] +build = "build.rs" license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-lang/backtrace-rs" @@ -22,7 +23,7 @@ exclude = ['crates/without_debuginfo', 'crates/macos_frames_test', 'crates/line- cfg-if = "1.0" rustc-demangle = "0.1.4" backtrace-sys = { path = "crates/backtrace-sys", version = "0.1.35", optional = true, default_features = false } -libc = { version = "0.2.87", default-features = false } +libc = { version = "0.2.94", default-features = false } # Optionally enable the ability to serialize a `Backtrace`, controlled through # the `serialize-*` features below. @@ -46,6 +47,11 @@ features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive'] [target.'cfg(windows)'.dependencies] winapi = { version = "0.3.3", optional = true } +[build-dependencies] +# Only needed for Android, but cannot be target dependent +# https://github.com/rust-lang/cargo/issues/4932 +cc = "1.0.67" + [dev-dependencies] dylib-dep = { path = "crates/dylib-dep" } libloading = "0.6" diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..abe896dfe --- /dev/null +++ b/build.rs @@ -0,0 +1,35 @@ +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 = cc::Build::new().file("src/android-api.c").expand(); + let expansion = match std::str::from_utf8(&expansion) { + Ok(s) => s, + Err(_) => return, + }; + println!("expanded android version detection:\n{}", expansion); + let marker = "APIVERSION"; + let i = match expansion.find(marker) { + Some(i) => i, + None => return, + }; + let version = match expansion[i + marker.len() + 1..].split_whitespace().next() { + Some(s) => s, + None => return, + }; + let version = match version.parse::() { + Ok(n) => n, + Err(_) => return, + }; + if version >= 21 { + println!("cargo:rustc-cfg=feature=\"dl_iterate_phdr\""); + } +} diff --git a/src/android-api.c b/src/android-api.c new file mode 100644 index 000000000..1bfeadf5b --- /dev/null +++ b/src/android-api.c @@ -0,0 +1,4 @@ +// Used from the build script to detect the value of the `__ANDROID_API__` +// builtin #define + +APIVERSION __ANDROID_API__ diff --git a/src/symbolize/gimli.rs b/src/symbolize/gimli.rs index bcd362ceb..6002426ab 100644 --- a/src/symbolize/gimli.rs +++ b/src/symbolize/gimli.rs @@ -154,6 +154,7 @@ cfg_if::cfg_if! { target_os = "linux", target_os = "fuchsia", target_os = "freebsd", + all(target_os = "android", feature = "dl_iterate_phdr"), ), not(target_env = "uclibc"), ))] {