Introduce target-specific env vars

The logic is stolen from cmake-rs, and it is important to
follow it as we will need to look for CMAKE_TOOLCHAIN_FILE
the same way cmake-rs does.

When checking for env variable BORING_BSSL_PATH during a
cross build for target x86_64-unknown-linux-gnu, boring-sys
build script will attempt to read:

  BORING_BSSL_PATH_x86_64-unknown-linux-gnu
  BORING_BSSL_PATH_x86_64_unknown_linux_gnu
  TARGET_BORING_BSSL_PATH
  BORING_BSSL_PATH
This commit is contained in:
Anthony Ramine 2023-10-16 10:31:13 +02:00 committed by Alessandro Ghedini
parent d8c2122c73
commit 80b97c8318
1 changed files with 23 additions and 7 deletions

View File

@ -43,7 +43,11 @@ impl Config {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let features = Features::from_env();
let env = Env::from_env(features.fips || features.fips_link_precompiled);
let env = Env::from_env(
&host,
&target,
features.fips || features.fips_link_precompiled,
);
let config = Self {
manifest_dir,
@ -105,19 +109,31 @@ impl Features {
}
impl Env {
fn from_env(is_fips_like: bool) -> Self {
fn from_env(target: &str, host: &str, is_fips_like: bool) -> Self {
const NORMAL_PREFIX: &str = "BORING_BSSL";
const FIPS_PREFIX: &str = "BORING_BSSL_FIPS";
let target_with_underscores = target.replace('-', "_");
// Logic stolen from cmake-rs.
let target_var = |name: &str| {
let kind = if host == target { "HOST" } else { "TARGET" };
var(&format!("{}_{}", name, target))
.or_else(|| var(&format!("{}_{}", name, target_with_underscores)))
.or_else(|| var(&format!("{}_{}", kind, name)))
.or_else(|| var(name))
};
let boringssl_var = |name: &str| {
// The passed name is the non-fips version of the environment variable,
// to help look for them in the repository.
assert!(name.starts_with(NORMAL_PREFIX));
if is_fips_like {
var(&name.replace(NORMAL_PREFIX, FIPS_PREFIX))
target_var(&name.replace(NORMAL_PREFIX, FIPS_PREFIX))
} else {
var(name)
target_var(name)
}
};
@ -128,9 +144,9 @@ impl Env {
precompiled_bcm_o: boringssl_var("BORING_BSSL_PRECOMPILED_BCM_O").map(PathBuf::from),
assume_patched: boringssl_var("BORING_BSSL_ASSUME_PATCHED")
.is_some_and(|v| !v.is_empty()),
debug: var("DEBUG"),
opt_level: var("OPT_LEVEL"),
android_ndk_home: var("ANDROID_NDK_HOME").map(Into::into),
debug: target_var("DEBUG"),
opt_level: target_var("OPT_LEVEL"),
android_ndk_home: target_var("ANDROID_NDK_HOME").map(Into::into),
}
}
}