Skip to content

Commit a50692e

Browse files
authored
Rollup merge of rust-lang#136767 - onur-ozkan:is-host-target, r=albertlarsan68,jieyouxu
improve host/cross target checking Using an invalid equality operator on `builder.config.build !=/==` can be hard to detect in reviews (which is quite dangerous). Replaced them with `is_host_target`, which is much clearer as it explicitly states what it does.
2 parents dcd0153 + c3856e9 commit a50692e

File tree

7 files changed

+35
-13
lines changed

7 files changed

+35
-13
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Step for Std {
191191
// The LLD wrappers and `rust-lld` are self-contained linking components that can be
192192
// necessary to link the stdlib on some targets. We'll also need to copy these binaries to
193193
// the `stage0-sysroot` to ensure the linker is found when bootstrapping on such a target.
194-
if compiler.stage == 0 && compiler.host == builder.config.build {
194+
if compiler.stage == 0 && builder.is_builder_target(&compiler.host) {
195195
// We want to copy the host `bin` folder within the `rustlib` folder in the sysroot.
196196
let src_sysroot_bin = builder
197197
.rustc_snapshot_sysroot()
@@ -2267,7 +2267,8 @@ pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path)
22672267
// FIXME: to make things simpler for now, limit this to the host and target where we know
22682268
// `strip -g` is both available and will fix the issue, i.e. on a x64 linux host that is not
22692269
// cross-compiling. Expand this to other appropriate targets in the future.
2270-
if target != "x86_64-unknown-linux-gnu" || target != builder.config.build || !path.exists() {
2270+
if target != "x86_64-unknown-linux-gnu" || !builder.is_builder_target(&target) || !path.exists()
2271+
{
22712272
return;
22722273
}
22732274

src/bootstrap/src/core/build_steps/dist.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ impl Step for DebuggerScripts {
582582
fn skip_host_target_lib(builder: &Builder<'_>, compiler: Compiler) -> bool {
583583
// The only true set of target libraries came from the build triple, so
584584
// let's reduce redundant work by only producing archives from that host.
585-
if compiler.host != builder.config.build {
585+
if !builder.is_builder_target(&compiler.host) {
586586
builder.info("\tskipping, not a build host");
587587
true
588588
} else {
@@ -637,7 +637,7 @@ fn copy_target_libs(
637637
for (path, dependency_type) in builder.read_stamp_file(stamp) {
638638
if dependency_type == DependencyType::TargetSelfContained {
639639
builder.copy_link(&path, &self_contained_dst.join(path.file_name().unwrap()));
640-
} else if dependency_type == DependencyType::Target || builder.config.build == target {
640+
} else if dependency_type == DependencyType::Target || builder.is_builder_target(&target) {
641641
builder.copy_link(&path, &dst.join(path.file_name().unwrap()));
642642
}
643643
}
@@ -786,7 +786,7 @@ impl Step for Analysis {
786786
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
787787
let compiler = self.compiler;
788788
let target = self.target;
789-
if compiler.host != builder.config.build {
789+
if !builder.is_builder_target(&compiler.host) {
790790
return None;
791791
}
792792

src/bootstrap/src/core/build_steps/llvm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl Step for Llvm {
498498
}
499499

500500
// https://llvm.org/docs/HowToCrossCompileLLVM.html
501-
if target != builder.config.build {
501+
if !builder.is_builder_target(&target) {
502502
let LlvmResult { llvm_config, .. } =
503503
builder.ensure(Llvm { target: builder.config.build });
504504
if !builder.config.dry_run() {
@@ -643,7 +643,7 @@ fn configure_cmake(
643643
}
644644
cfg.target(&target.triple).host(&builder.config.build.triple);
645645

646-
if target != builder.config.build {
646+
if !builder.is_builder_target(&target) {
647647
cfg.define("CMAKE_CROSSCOMPILING", "True");
648648

649649
if target.contains("netbsd") {
@@ -1085,7 +1085,7 @@ impl Step for Lld {
10851085
.define("LLVM_CMAKE_DIR", llvm_cmake_dir)
10861086
.define("LLVM_INCLUDE_TESTS", "OFF");
10871087

1088-
if target != builder.config.build {
1088+
if !builder.is_builder_target(&target) {
10891089
// Use the host llvm-tblgen binary.
10901090
cfg.define(
10911091
"LLVM_TABLEGEN_EXE",

src/bootstrap/src/core/build_steps/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2738,7 +2738,7 @@ impl Step for Crate {
27382738
cargo
27392739
} else {
27402740
// Also prepare a sysroot for the target.
2741-
if builder.config.build != target {
2741+
if !builder.is_builder_target(&target) {
27422742
builder.ensure(compile::Std::new(compiler, target).force_recompile(true));
27432743
builder.ensure(RemoteCopyLibs { compiler, target });
27442744
}

src/bootstrap/src/core/builder/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -954,3 +954,19 @@ fn test_test_coverage() {
954954
assert_eq!(modes, expected);
955955
}
956956
}
957+
958+
#[test]
959+
fn test_is_builder_target() {
960+
let target1 = TargetSelection::from_user(TEST_TRIPLE_1);
961+
let target2 = TargetSelection::from_user(TEST_TRIPLE_2);
962+
963+
for (target1, target2) in [(target1, target2), (target2, target1)] {
964+
let mut config = configure("build", &[], &[]);
965+
config.build = target1;
966+
let build = Build::new(config);
967+
let builder = Builder::new(&build);
968+
969+
assert!(builder.is_builder_target(&target1));
970+
assert!(!builder.is_builder_target(&target2));
971+
}
972+
}

src/bootstrap/src/core/sanity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ than building it.
329329
if target.contains("musl") && !target.contains("unikraft") {
330330
// If this is a native target (host is also musl) and no musl-root is given,
331331
// fall back to the system toolchain in /usr before giving up
332-
if build.musl_root(*target).is_none() && build.config.build == *target {
332+
if build.musl_root(*target).is_none() && build.is_builder_target(target) {
333333
let target = build.config.target_config.entry(*target).or_default();
334334
target.musl_root = Some("/usr".into());
335335
}

src/bootstrap/src/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl Build {
738738
/// Note that if LLVM is configured externally then the directory returned
739739
/// will likely be empty.
740740
fn llvm_out(&self, target: TargetSelection) -> PathBuf {
741-
if self.config.llvm_from_ci && self.config.build == target {
741+
if self.config.llvm_from_ci && self.is_builder_target(&target) {
742742
self.config.ci_llvm_root()
743743
} else {
744744
self.out.join(target).join("llvm")
@@ -788,7 +788,7 @@ impl Build {
788788
fn is_system_llvm(&self, target: TargetSelection) -> bool {
789789
match self.config.target_config.get(&target) {
790790
Some(Target { llvm_config: Some(_), .. }) => {
791-
let ci_llvm = self.config.llvm_from_ci && target == self.config.build;
791+
let ci_llvm = self.config.llvm_from_ci && self.is_builder_target(&target);
792792
!ci_llvm
793793
}
794794
// We're building from the in-tree src/llvm-project sources.
@@ -1264,7 +1264,7 @@ Executed at: {executed_at}"#,
12641264
// need to use CXX compiler as linker to resolve the exception functions
12651265
// that are only existed in CXX libraries
12661266
Some(self.cxx.borrow()[&target].path().into())
1267-
} else if target != self.config.build
1267+
} else if !self.is_builder_target(&target)
12681268
&& helpers::use_host_linker(target)
12691269
&& !target.is_msvc()
12701270
{
@@ -1915,6 +1915,11 @@ to download LLVM rather than building it.
19151915
stream.reset().unwrap();
19161916
result
19171917
}
1918+
1919+
/// Checks if the given target is the same as the builder target.
1920+
fn is_builder_target(&self, target: &TargetSelection) -> bool {
1921+
&self.config.build == target
1922+
}
19181923
}
19191924

19201925
#[cfg(unix)]

0 commit comments

Comments
 (0)