From d80896c072a97948b6d9dcb4148f4907cbeecc4e Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 5 Oct 2023 02:19:13 -0600 Subject: [PATCH] Enable calling build.rs externally v2 (rust-lang/backtrace-rs#568) this time we write the file into OUT_DIR, then there's no CWD issues --- build.rs | 31 ++++++++++++++++++++----------- src/android-api.c | 4 ---- 2 files changed, 20 insertions(+), 15 deletions(-) delete mode 100644 src/android-api.c diff --git a/build.rs b/build.rs index 9bd3abd16..ed4e07a85 100644 --- a/build.rs +++ b/build.rs @@ -11,17 +11,27 @@ pub fn main() { } } +// Used to detect the value of the `__ANDROID_API__` +// builtin #define +const MARKER: &str = "BACKTRACE_RS_ANDROID_APIVERSION"; +const ANDROID_API_C: &str = " +BACKTRACE_RS_ANDROID_APIVERSION __ANDROID_API__ +"; + fn build_android() { - // Resolve `src/android-api.c` relative to this file. + // Create `android-api.c` on demand. // Required to support calling this from the `std` build script. - let android_api_c = Path::new(file!()) - .parent() - .unwrap() - .join("src/android-api.c"); - let expansion = match cc::Build::new().file(android_api_c).try_expand() { + let out_dir = env::var_os("OUT_DIR").unwrap(); + let android_api_c = Path::new(&out_dir).join("android-api.c"); + std::fs::write(&android_api_c, ANDROID_API_C).unwrap(); + + let expansion = match cc::Build::new().file(&android_api_c).try_expand() { Ok(result) => result, Err(e) => { - println!("failed to run C compiler: {}", e); + eprintln!( + "warning: android version detection failed while running C compiler: {}", + e + ); return; } }; @@ -29,13 +39,12 @@ fn build_android() { Ok(s) => s, Err(_) => return, }; - println!("expanded android version detection:\n{}", expansion); - let marker = "APIVERSION"; - let i = match expansion.find(marker) { + eprintln!("expanded android version detection:\n{}", expansion); + let i = match expansion.find(MARKER) { Some(i) => i, None => return, }; - let version = match expansion[i + marker.len() + 1..].split_whitespace().next() { + let version = match expansion[i + MARKER.len() + 1..].split_whitespace().next() { Some(s) => s, None => return, }; diff --git a/src/android-api.c b/src/android-api.c deleted file mode 100644 index 1bfeadf5b..000000000 --- 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__