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

Allow the library consumer to specify the Android API level #570

Closed
wants to merge 3 commits into from
Closed
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
53 changes: 39 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate cc;

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

Expand All @@ -11,14 +9,17 @@ 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 = "
#[cfg(not(no_cc))]
fn android_version_from_c_headers() -> Option<u32> {
extern crate cc;

// 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() {
// Create `android-api.c` on demand.
// Required to support calling this from the `std` build script.
let out_dir = env::var_os("OUT_DIR").unwrap();
Expand All @@ -32,26 +33,50 @@ fn build_android() {
"warning: android version detection failed while running C compiler: {}",
e
);
return;
return None;
}
};
let expansion = match std::str::from_utf8(&expansion) {
Ok(s) => s,
Err(_) => return,
Err(_) => return None,
};
eprintln!("expanded android version detection:\n{}", expansion);
let i = match expansion.find(MARKER) {
Some(i) => i,
None => return,
None => return None,
};
let version = match expansion[i + MARKER.len() + 1..].split_whitespace().next() {
Some(s) => s,
None => return,
None => return None,
};
let version = match version.parse::<u32>() {
Ok(n) => n,
Err(_) => return,
match version.parse::<u32>() {
Ok(n) => Some(n),
Err(_) => None,
}
}

/// Sets cfgs that depend on the Android API level.
///
/// This depends on the use of a C preprocessor to find the API level in system
/// headers. For build systems that do not want to use a C processor inside the
/// execution of build scripts, the build system can specify the API level
/// through the `__ANDROID_API__` environment variable. When `--cfg=no_cc` is
/// specified, the environment variable is used instead.
fn build_android() {
let version = {
#[cfg(no_cc)]
{
std::env::var("__ANDROID_API__")
.expect("__ANDROID_API__ must be set when building with `--cfg=no_cc`")
.parse::<u32>()
.expect("__ANDROID_API__ must be a number")
}
#[cfg(not(no_cc))]
{
android_version_from_c_headers().unwrap_or_default()
}
};

if version >= 21 {
println!("cargo:rustc-cfg=feature=\"dl_iterate_phdr\"");
}
Expand Down
Loading