Skip to content

Commit 702d383

Browse files
committed
fix: missing backtrace when using std::backtrace::Backtrace on Android
1 parent fdf61d4 commit 702d383

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

library/Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
4242

4343
[[package]]
4444
name = "cc"
45-
version = "1.0.99"
45+
version = "1.1.10"
4646
source = "registry+https://github.com/rust-lang/crates.io-index"
47-
checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695"
47+
checksum = "e9e8aabfac534be767c909e0690571677d49f41bd8465ae876fe043d52ba5292"
4848

4949
[[package]]
5050
name = "cfg-if"
@@ -318,6 +318,7 @@ version = "0.0.0"
318318
dependencies = [
319319
"addr2line",
320320
"alloc",
321+
"cc",
321322
"cfg-if",
322323
"compiler_builtins",
323324
"core",

library/std/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,6 @@ check-cfg = [
147147
# of declared features, we therefor expect any feature cfg
148148
'cfg(feature, values(any()))',
149149
]
150+
151+
[build-dependencies]
152+
cc = { version = "1.1.10" }

library/std/build.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::env;
2+
use std::path::Path;
23

34
fn main() {
45
println!("cargo:rerun-if-changed=build.rs");
@@ -20,7 +21,6 @@ fn main() {
2021

2122
println!("cargo:rustc-check-cfg=cfg(restricted_std)");
2223
if target_os == "linux"
23-
|| target_os == "android"
2424
|| target_os == "netbsd"
2525
|| target_os == "dragonfly"
2626
|| target_os == "openbsd"
@@ -58,6 +58,9 @@ fn main() {
5858
|| env::var("RUSTC_BOOTSTRAP_SYNTHETIC_TARGET").is_ok()
5959
{
6060
// These platforms don't have any special requirements.
61+
} else if target_os == "android" {
62+
// If ANDROID_API >= 21, enable `dl_iterate_phdr` feature.
63+
build_android();
6164
} else {
6265
// This is for Cargo's build-std support, to mark std as unstable for
6366
// typically no_std platforms.
@@ -183,3 +186,45 @@ fn main() {
183186
println!("cargo:rustc-cfg=reliable_f128_math");
184187
}
185188
}
189+
190+
fn build_android() {
191+
// Used to detect the value of the `__ANDROID_API__`
192+
// builtin #define
193+
const MARKER: &str = "BACKTRACE_RS_ANDROID_APIVERSION";
194+
const ANDROID_API_C: &str = "
195+
BACKTRACE_RS_ANDROID_APIVERSION __ANDROID_API__
196+
";
197+
198+
// Create `android-api.c` on demand.
199+
let out_dir = env::var_os("OUT_DIR").unwrap();
200+
let android_api_c = Path::new(&out_dir).join("android-api.c");
201+
std::fs::write(&android_api_c, ANDROID_API_C).unwrap();
202+
203+
let expansion = match cc::Build::new().file(&android_api_c).try_expand() {
204+
Ok(result) => result,
205+
Err(e) => {
206+
eprintln!("warning: android version detection failed while running C compiler: {e}");
207+
return;
208+
}
209+
};
210+
let expansion = match std::str::from_utf8(&expansion) {
211+
Ok(s) => s,
212+
Err(_) => return,
213+
};
214+
eprintln!("expanded android version detection:\n{expansion}");
215+
let i = match expansion.find(MARKER) {
216+
Some(i) => i,
217+
None => return,
218+
};
219+
let version = match expansion[i + MARKER.len() + 1..].split_whitespace().next() {
220+
Some(s) => s,
221+
None => return,
222+
};
223+
let version = match version.parse::<u32>() {
224+
Ok(n) => n,
225+
Err(_) => return,
226+
};
227+
if version >= 21 {
228+
println!("cargo:rustc-cfg=feature=\"dl_iterate_phdr\"");
229+
}
230+
}

0 commit comments

Comments
 (0)