From 6b13969dae31b45dd6dd45515f0c326f2bab2de0 Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Mon, 26 Apr 2021 11:11:22 -0400 Subject: [PATCH] Add Android support to gimli (#415) --- Cargo.toml | 8 +++++++- build.rs | 35 +++++++++++++++++++++++++++++++++++ src/android-api.c | 4 ++++ src/symbolize/gimli.rs | 1 + 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 build.rs create mode 100644 src/android-api.c diff --git a/Cargo.toml b/Cargo.toml index 71db617f..a72d5336 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 00000000..abe896df --- /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 00000000..1bfeadf5 --- /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 bcd362ce..6002426a 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"), ))] {