Skip to content

Commit cbb403d

Browse files
committed
Enable calling build.rs externally v2
this time we write the file into OUT_DIR, then there's no CWD issues
1 parent 7b78a4f commit cbb403d

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

build.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,37 @@ pub fn main() {
1111
}
1212
}
1313

14+
// Used to detect the value of the `__ANDROID_API__`
15+
// builtin #define
16+
const MARKER: &str = "BACKTRACE_RS_ANDROID_APIVERSION";
17+
const ANDROID_API_C: &str = "
18+
BACKTRACE_RS_ANDROID_APIVERSION __ANDROID_API__
19+
";
20+
1421
fn build_android() {
15-
// Resolve `src/android-api.c` relative to this file.
22+
// Create `android-api.c` on demand.
1623
// Required to support calling this from the `std` build script.
17-
let android_api_c = Path::new(file!())
18-
.parent()
19-
.unwrap()
20-
.join("src/android-api.c");
21-
let expansion = match cc::Build::new().file(android_api_c).try_expand() {
24+
let out_dir = env::var_os("OUT_DIR").unwrap();
25+
let android_api_c = Path::new(&out_dir).join("android-api.c");
26+
std::fs::write(&android_api_c, ANDROID_API_C).unwrap();
27+
28+
let expansion = match cc::Build::new().file(&android_api_c).try_expand() {
2229
Ok(result) => result,
2330
Err(e) => {
24-
println!("failed to run C compiler: {}", e);
31+
eprintln!("warning: android detection failed while running C compiler: {}", e);
2532
return;
2633
}
2734
};
2835
let expansion = match std::str::from_utf8(&expansion) {
2936
Ok(s) => s,
3037
Err(_) => return,
3138
};
32-
println!("expanded android version detection:\n{}", expansion);
33-
let marker = "APIVERSION";
34-
let i = match expansion.find(marker) {
39+
eprintln!("expanded android version detection:\n{}", expansion);
40+
let i = match expansion.find(MARKER) {
3541
Some(i) => i,
3642
None => return,
3743
};
38-
let version = match expansion[i + marker.len() + 1..].split_whitespace().next() {
44+
let version = match expansion[i + MARKER.len() + 1..].split_whitespace().next() {
3945
Some(s) => s,
4046
None => return,
4147
};

src/android-api.c

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)