Skip to content

Commit

Permalink
Enable calling build.rs externally v2
Browse files Browse the repository at this point in the history
this time we write the file into OUT_DIR, then there's no CWD issues
  • Loading branch information
pitaj committed Oct 4, 2023
1 parent 7b78a4f commit 367c953
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
31 changes: 20 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,40 @@ 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;
}
};
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) {
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,
};
Expand Down
4 changes: 0 additions & 4 deletions src/android-api.c

This file was deleted.

0 comments on commit 367c953

Please sign in to comment.