Skip to content

Commit 268fb9b

Browse files
committed
Reduce the need for the host target triple
1 parent 099ef3b commit 268fb9b

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

src/lib.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ pub struct Build {
283283
ccbin: bool,
284284
std: Option<Arc<str>>,
285285
target: Option<Arc<str>>,
286+
/// The host compiler.
287+
///
288+
/// Try to not access this directly, and instead prefer `cfg!(...)`.
286289
host: Option<Arc<str>>,
287290
out_dir: Option<Arc<Path>>,
288291
opt_level: Option<Arc<str>>,
@@ -658,11 +661,13 @@ impl Build {
658661
.cargo_metadata(self.cargo_output.metadata)
659662
.target(target)
660663
.opt_level(0)
661-
.host(&self.get_host()?)
662664
.debug(false)
663665
.cpp(self.cpp)
664666
.cuda(self.cuda)
665667
.emit_rerun_if_env_changed(self.emit_rerun_if_env_changed);
668+
if let Some(host) = &self.host {
669+
cfg.host(host);
670+
}
666671
cfg.try_get_compiler()?
667672
};
668673

@@ -2866,7 +2871,6 @@ impl Build {
28662871
out_dir,
28672872
));
28682873
}
2869-
let host = self.get_host()?;
28702874
let target = self.get_target()?;
28712875
let target = &*target;
28722876
let (env, msvc, gnu, traditional, clang) = if self.cpp {
@@ -2879,7 +2883,7 @@ impl Build {
28792883
// is not flag-compatible with "gcc". This history casts a long shadow,
28802884
// and many modern illumos distributions today ship GCC as "gcc" without
28812885
// also making it available as "cc".
2882-
let default = if host.contains("solaris") || host.contains("illumos") {
2886+
let default = if cfg!(target_os = "solaris") || cfg!(target_os = "illumos") {
28832887
gnu
28842888
} else {
28852889
traditional
@@ -2944,7 +2948,7 @@ impl Build {
29442948
let tool = match tool_opt {
29452949
Some(t) => t,
29462950
None => {
2947-
let compiler = if host.contains("windows") && target.contains("windows") {
2951+
let compiler = if cfg!(windows) && target.contains("windows") {
29482952
if target.contains("msvc") {
29492953
msvc.to_string()
29502954
} else {
@@ -2958,7 +2962,7 @@ impl Build {
29582962
{
29592963
clang.to_string()
29602964
} else if target.contains("android") {
2961-
autodetect_android_compiler(target, &host, gnu, clang)
2965+
autodetect_android_compiler(target, gnu, clang)
29622966
} else if target.contains("cloudabi") {
29632967
format!("{}-{}", target, traditional)
29642968
} else if Build::is_wasi_target(target) {
@@ -2977,7 +2981,7 @@ impl Build {
29772981
format!("arm-kmc-eabi-{}", gnu)
29782982
} else if target.starts_with("aarch64-kmc-solid_") {
29792983
format!("aarch64-kmc-elf-{}", gnu)
2980-
} else if &*self.get_host()? != target {
2984+
} else if self.get_is_cross_compile()? {
29812985
let prefix = self.prefix_for_target(target);
29822986
match prefix {
29832987
Some(prefix) => {
@@ -3042,8 +3046,7 @@ impl Build {
30423046
// on Windows is restricted to around 8k characters instead of around 32k characters.
30433047
// To remove this limit, we call the main clang binary directly and construct the
30443048
// `--target=` ourselves.
3045-
if host.contains("windows") && android_clang_compiler_uses_target_arg_internally(&tool.path)
3046-
{
3049+
if cfg!(windows) && android_clang_compiler_uses_target_arg_internally(&tool.path) {
30473050
if let Some(path) = tool.path.file_name() {
30483051
let file_name = path.to_str().unwrap().to_owned();
30493052
let (target, clang) = file_name.split_at(file_name.rfind('-').unwrap());
@@ -3430,7 +3433,7 @@ impl Build {
34303433
// Use the GNU-variant to match other Unix systems.
34313434
name = format!("g{}", tool).into();
34323435
self.cmd(&name)
3433-
} else if self.get_host()? != target {
3436+
} else if self.get_is_cross_compile()? {
34343437
match self.prefix_for_target(&target) {
34353438
Some(p) => {
34363439
// GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both.
@@ -3644,11 +3647,13 @@ impl Build {
36443647
}
36453648
}
36463649

3647-
fn get_host(&self) -> Result<Cow<'_, str>, Error> {
3648-
match &self.host {
3649-
Some(h) => Ok(Cow::Borrowed(h)),
3650-
None => self.getenv_unwrap_str("HOST").map(Cow::Owned),
3651-
}
3650+
fn get_is_cross_compile(&self) -> Result<bool, Error> {
3651+
let target = self.get_target()?;
3652+
let host: Cow<'_, str> = match &self.host {
3653+
Some(h) => Cow::Borrowed(h),
3654+
None => Cow::Owned(self.getenv_unwrap_str("HOST")?),
3655+
};
3656+
Ok(host != target)
36523657
}
36533658

36543659
fn get_opt_level(&self) -> Result<Cow<'_, str>, Error> {
@@ -3765,8 +3770,11 @@ impl Build {
37653770

37663771
fn getenv_with_target_prefixes(&self, var_base: &str) -> Result<Arc<OsStr>, Error> {
37673772
let target = self.get_target()?;
3768-
let host = self.get_host()?;
3769-
let kind = if host == target { "HOST" } else { "TARGET" };
3773+
let kind = if self.get_is_cross_compile()? {
3774+
"TARGET"
3775+
} else {
3776+
"HOST"
3777+
};
37703778
let target_u = target.replace('-', "_");
37713779
let res = self
37723780
.getenv(&format!("{}_{}", var_base, target))
@@ -3799,8 +3807,7 @@ impl Build {
37993807

38003808
fn fix_env_for_apple_os(&self, cmd: &mut Command) -> Result<(), Error> {
38013809
let target = self.get_target()?;
3802-
let host = self.get_host()?;
3803-
if host.contains("apple-darwin") && target.contains("apple-darwin") {
3810+
if cfg!(target_os = "macos") && target.contains("apple-darwin") {
38043811
// Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at
38053812
// "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld",
38063813
// although this is apparently ignored when using the linker at "/usr/bin/ld".
@@ -4209,7 +4216,7 @@ fn android_clang_compiler_uses_target_arg_internally(clang_path: &Path) -> bool
42094216
false
42104217
}
42114218

4212-
fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str) -> String {
4219+
fn autodetect_android_compiler(target: &str, gnu: &str, clang: &str) -> String {
42134220
let new_clang_key = match target {
42144221
"aarch64-linux-android" => Some("aarch64"),
42154222
"armv7-linux-androideabi" => Some("armv7a"),
@@ -4249,7 +4256,7 @@ fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str)
42494256
// if not, use clang
42504257
if Command::new(&gnu_compiler).output().is_ok() {
42514258
gnu_compiler
4252-
} else if host.contains("windows") && Command::new(&clang_compiler_cmd).output().is_ok() {
4259+
} else if cfg!(windows) && Command::new(&clang_compiler_cmd).output().is_ok() {
42534260
clang_compiler_cmd
42544261
} else {
42554262
clang_compiler

0 commit comments

Comments
 (0)