Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke backtrace-rs' buildscript in std's buildscript for Android targets #99883

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5042,6 +5042,7 @@ version = "0.0.0"
dependencies = [
"addr2line 0.16.0",
"alloc",
"cc",
"cfg-if 0.1.10",
"compiler_builtins",
"core",
Expand Down
4 changes: 4 additions & 0 deletions library/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive']
[dev-dependencies]
rand = "0.7"

[build-dependencies]
# Dependency of the `backtrace` crate
cc = "1.0.67"

[target.'cfg(any(all(target_family = "wasm", not(target_os = "emscripten")), all(target_vendor = "fortanix", target_env = "sgx")))'.dependencies]
dlmalloc = { version = "0.2.3", features = ['rustc-dep-of-std'] }

Expand Down
16 changes: 16 additions & 0 deletions library/std/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
use std::env;

// backtrace-rs requires a feature check on Android targets, so
// we need to run its build.rs as well. Note that we use an
// include! rather than #[path = ""] because backtrace-rs's main
// function is private.
#[allow(unused_extern_crates)]
mod backtrace_rs_build_rs {
include!("../backtrace/build.rs");

#[inline]
pub fn call_main() {
main();
}
}

fn main() {
println!("cargo:rerun-if-changed=build.rs");
let target = env::var("TARGET").expect("TARGET was not set");
Expand Down Expand Up @@ -49,4 +63,6 @@ fn main() {
}
println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap());
println!("cargo:rustc-cfg=backtrace_in_libstd");

backtrace_rs_build_rs::call_main();
}
4 changes: 4 additions & 0 deletions library/std/src/android-api.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Used from backtrace-rs' build script to detect the value of the `__ANDROID_API__`
// builtin #define

APIVERSION __ANDROID_API__
Copy link
Contributor

@pitaj pitaj May 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a little messy to have to duplicate this file. Can we instead modify backtrace-rs/build.rs to make it directory-independent somehow?

Maybe change it to have pub fn main instead and define the module like

#[path = "../backtrace/build.rs"]
mod backtrace_build_rs;

And then it can just do

use std::path::Path;

fn build_android() {
    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() {
        // ...