Skip to content

Commit

Permalink
Introduce target-specific env vars
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nox committed Oct 16, 2023
1 parent bad1c21 commit 16958ce
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions boring-sys/build/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -107,19 +111,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)
}
.map(PathBuf::from)
};
Expand All @@ -129,9 +145,9 @@ impl Env {
include_path: boringssl_var("BORING_BSSL_INCLUDE_PATH"),
source_path: boringssl_var("BORING_BSSL_SOURCE_PATH"),
precompiled_bcm_o: boringssl_var("BORING_BSSL_PRECOMPILED_BCM_O"),
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),
}
}
}
Expand Down

0 comments on commit 16958ce

Please sign in to comment.