Skip to content

Commit 1073aea

Browse files
Rollup merge of #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 c43a59f + acc7ddf commit 1073aea

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()
@@ -2310,7 +2310,8 @@ pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path)
23102310
// FIXME: to make things simpler for now, limit this to the host and target where we know
23112311
// `strip -g` is both available and will fix the issue, i.e. on a x64 linux host that is not
23122312
// cross-compiling. Expand this to other appropriate targets in the future.
2313-
if target != "x86_64-unknown-linux-gnu" || target != builder.config.build || !path.exists() {
2313+
if target != "x86_64-unknown-linux-gnu" || !builder.is_builder_target(&target) || !path.exists()
2314+
{
23142315
return;
23152316
}
23162317

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
@@ -516,7 +516,7 @@ impl Step for Llvm {
516516
}
517517

518518
// https://llvm.org/docs/HowToCrossCompileLLVM.html
519-
if target != builder.config.build {
519+
if !builder.is_builder_target(&target) {
520520
let LlvmResult { llvm_config, .. } =
521521
builder.ensure(Llvm { target: builder.config.build });
522522
if !builder.config.dry_run() {
@@ -661,7 +661,7 @@ fn configure_cmake(
661661
}
662662
cfg.target(&target.triple).host(&builder.config.build.triple);
663663

664-
if target != builder.config.build {
664+
if !builder.is_builder_target(&target) {
665665
cfg.define("CMAKE_CROSSCOMPILING", "True");
666666

667667
if target.contains("netbsd") {
@@ -1111,7 +1111,7 @@ impl Step for Lld {
11111111
.define("LLVM_CMAKE_DIR", llvm_cmake_dir)
11121112
.define("LLVM_INCLUDE_TESTS", "OFF");
11131113

1114-
if target != builder.config.build {
1114+
if !builder.is_builder_target(&target) {
11151115
// Use the host llvm-tblgen binary.
11161116
cfg.define(
11171117
"LLVM_TABLEGEN_EXE",

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

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

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

+16
Original file line numberDiff line numberDiff line change
@@ -1065,3 +1065,19 @@ fn test_prebuilt_llvm_config_path_resolution() {
10651065
.join(exe("llvm-config", builder.config.build));
10661066
assert_eq!(expected, actual);
10671067
}
1068+
1069+
#[test]
1070+
fn test_is_builder_target() {
1071+
let target1 = TargetSelection::from_user(TEST_TRIPLE_1);
1072+
let target2 = TargetSelection::from_user(TEST_TRIPLE_2);
1073+
1074+
for (target1, target2) in [(target1, target2), (target2, target1)] {
1075+
let mut config = configure("build", &[], &[]);
1076+
config.build = target1;
1077+
let build = Build::new(config);
1078+
let builder = Builder::new(&build);
1079+
1080+
assert!(builder.is_builder_target(&target1));
1081+
assert!(!builder.is_builder_target(&target2));
1082+
}
1083+
}

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
@@ -739,7 +739,7 @@ impl Build {
739739
/// Note that if LLVM is configured externally then the directory returned
740740
/// will likely be empty.
741741
fn llvm_out(&self, target: TargetSelection) -> PathBuf {
742-
if self.config.llvm_from_ci && self.config.build == target {
742+
if self.config.llvm_from_ci && self.is_builder_target(&target) {
743743
self.config.ci_llvm_root()
744744
} else {
745745
self.out.join(target).join("llvm")
@@ -789,7 +789,7 @@ impl Build {
789789
fn is_system_llvm(&self, target: TargetSelection) -> bool {
790790
match self.config.target_config.get(&target) {
791791
Some(Target { llvm_config: Some(_), .. }) => {
792-
let ci_llvm = self.config.llvm_from_ci && target == self.config.build;
792+
let ci_llvm = self.config.llvm_from_ci && self.is_builder_target(&target);
793793
!ci_llvm
794794
}
795795
// We're building from the in-tree src/llvm-project sources.
@@ -1274,7 +1274,7 @@ Executed at: {executed_at}"#,
12741274
// need to use CXX compiler as linker to resolve the exception functions
12751275
// that are only existed in CXX libraries
12761276
Some(self.cxx.borrow()[&target].path().into())
1277-
} else if target != self.config.build
1277+
} else if !self.is_builder_target(&target)
12781278
&& helpers::use_host_linker(target)
12791279
&& !target.is_msvc()
12801280
{
@@ -1925,6 +1925,11 @@ to download LLVM rather than building it.
19251925
stream.reset().unwrap();
19261926
result
19271927
}
1928+
1929+
/// Checks if the given target is the same as the builder target.
1930+
fn is_builder_target(&self, target: &TargetSelection) -> bool {
1931+
&self.config.build == target
1932+
}
19281933
}
19291934

19301935
#[cfg(unix)]

0 commit comments

Comments
 (0)