Skip to content

Commit

Permalink
Enable calling build.rs directly from std/build.rs (#556)
Browse files Browse the repository at this point in the history
Enable calling build.rs directly from std/build.rs and call buildscript in
`as-if-std` buildscript to test that it will work during bootstrap.
  • Loading branch information
pitaj authored Aug 18, 2023
1 parent 037356f commit 3479721
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
extern crate cc;

use std::env;
use std::path::Path;

fn main() {
// Must be public so the build script of `std` can call it.
pub fn main() {
match env::var("CARGO_CFG_TARGET_OS").unwrap_or_default().as_str() {
"android" => build_android(),
_ => {}
}
}

fn build_android() {
let expansion = match cc::Build::new().file("src/android-api.c").try_expand() {
// Resolve `src/android-api.c` relative to this file.
// 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() {
Ok(result) => result,
Err(e) => {
println!("failed to run C compiler: {}", e);
Expand Down
4 changes: 4 additions & 0 deletions crates/as-if-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ default-features = false
optional = true
features = ['read_core', 'elf', 'macho', 'pe', 'unaligned', 'archive']

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

[features]
default = ['backtrace']
backtrace = ['addr2line', 'object']
8 changes: 8 additions & 0 deletions crates/as-if-std/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// backtrace-rs requires a feature check on Android targets, so
// we need to run its build.rs as well.
#[allow(unused_extern_crates)]
#[path = "../../build.rs"]
mod backtrace_build_rs;

fn main() {
println!("cargo:rustc-cfg=backtrace_in_libstd");

backtrace_build_rs::main();
}

0 comments on commit 3479721

Please sign in to comment.