Skip to content

Commit

Permalink
Unrolled build for rust-lang#136767
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rust-timer authored Feb 12, 2025
2 parents 021fb9c + acc7ddf commit 9ea1324
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Step for Std {
// The LLD wrappers and `rust-lld` are self-contained linking components that can be
// necessary to link the stdlib on some targets. We'll also need to copy these binaries to
// the `stage0-sysroot` to ensure the linker is found when bootstrapping on such a target.
if compiler.stage == 0 && compiler.host == builder.config.build {
if compiler.stage == 0 && builder.is_builder_target(&compiler.host) {
// We want to copy the host `bin` folder within the `rustlib` folder in the sysroot.
let src_sysroot_bin = builder
.rustc_snapshot_sysroot()
Expand Down Expand Up @@ -2310,7 +2310,8 @@ pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path)
// FIXME: to make things simpler for now, limit this to the host and target where we know
// `strip -g` is both available and will fix the issue, i.e. on a x64 linux host that is not
// cross-compiling. Expand this to other appropriate targets in the future.
if target != "x86_64-unknown-linux-gnu" || target != builder.config.build || !path.exists() {
if target != "x86_64-unknown-linux-gnu" || !builder.is_builder_target(&target) || !path.exists()
{
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl Step for DebuggerScripts {
fn skip_host_target_lib(builder: &Builder<'_>, compiler: Compiler) -> bool {
// The only true set of target libraries came from the build triple, so
// let's reduce redundant work by only producing archives from that host.
if compiler.host != builder.config.build {
if !builder.is_builder_target(&compiler.host) {
builder.info("\tskipping, not a build host");
true
} else {
Expand Down Expand Up @@ -637,7 +637,7 @@ fn copy_target_libs(
for (path, dependency_type) in builder.read_stamp_file(stamp) {
if dependency_type == DependencyType::TargetSelfContained {
builder.copy_link(&path, &self_contained_dst.join(path.file_name().unwrap()));
} else if dependency_type == DependencyType::Target || builder.config.build == target {
} else if dependency_type == DependencyType::Target || builder.is_builder_target(&target) {
builder.copy_link(&path, &dst.join(path.file_name().unwrap()));
}
}
Expand Down Expand Up @@ -786,7 +786,7 @@ impl Step for Analysis {
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
let compiler = self.compiler;
let target = self.target;
if compiler.host != builder.config.build {
if !builder.is_builder_target(&compiler.host) {
return None;
}

Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ impl Step for Llvm {
}

// https://llvm.org/docs/HowToCrossCompileLLVM.html
if target != builder.config.build {
if !builder.is_builder_target(&target) {
let LlvmResult { llvm_config, .. } =
builder.ensure(Llvm { target: builder.config.build });
if !builder.config.dry_run() {
Expand Down Expand Up @@ -661,7 +661,7 @@ fn configure_cmake(
}
cfg.target(&target.triple).host(&builder.config.build.triple);

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

if target.contains("netbsd") {
Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl Step for Lld {
.define("LLVM_CMAKE_DIR", llvm_cmake_dir)
.define("LLVM_INCLUDE_TESTS", "OFF");

if target != builder.config.build {
if !builder.is_builder_target(&target) {
// Use the host llvm-tblgen binary.
cfg.define(
"LLVM_TABLEGEN_EXE",
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2742,7 +2742,7 @@ impl Step for Crate {
cargo
} else {
// Also prepare a sysroot for the target.
if builder.config.build != target {
if !builder.is_builder_target(&target) {
builder.ensure(compile::Std::new(compiler, target).force_recompile(true));
builder.ensure(RemoteCopyLibs { compiler, target });
}
Expand Down
16 changes: 16 additions & 0 deletions src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,19 @@ fn test_prebuilt_llvm_config_path_resolution() {
.join(exe("llvm-config", builder.config.build));
assert_eq!(expected, actual);
}

#[test]
fn test_is_builder_target() {
let target1 = TargetSelection::from_user(TEST_TRIPLE_1);
let target2 = TargetSelection::from_user(TEST_TRIPLE_2);

for (target1, target2) in [(target1, target2), (target2, target1)] {
let mut config = configure("build", &[], &[]);
config.build = target1;
let build = Build::new(config);
let builder = Builder::new(&build);

assert!(builder.is_builder_target(&target1));
assert!(!builder.is_builder_target(&target2));
}
}
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ than building it.
if target.contains("musl") && !target.contains("unikraft") {
// If this is a native target (host is also musl) and no musl-root is given,
// fall back to the system toolchain in /usr before giving up
if build.musl_root(*target).is_none() && build.config.build == *target {
if build.musl_root(*target).is_none() && build.is_builder_target(target) {
let target = build.config.target_config.entry(*target).or_default();
target.musl_root = Some("/usr".into());
}
Expand Down
11 changes: 8 additions & 3 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ impl Build {
/// Note that if LLVM is configured externally then the directory returned
/// will likely be empty.
fn llvm_out(&self, target: TargetSelection) -> PathBuf {
if self.config.llvm_from_ci && self.config.build == target {
if self.config.llvm_from_ci && self.is_builder_target(&target) {
self.config.ci_llvm_root()
} else {
self.out.join(target).join("llvm")
Expand Down Expand Up @@ -789,7 +789,7 @@ impl Build {
fn is_system_llvm(&self, target: TargetSelection) -> bool {
match self.config.target_config.get(&target) {
Some(Target { llvm_config: Some(_), .. }) => {
let ci_llvm = self.config.llvm_from_ci && target == self.config.build;
let ci_llvm = self.config.llvm_from_ci && self.is_builder_target(&target);
!ci_llvm
}
// We're building from the in-tree src/llvm-project sources.
Expand Down Expand Up @@ -1274,7 +1274,7 @@ Executed at: {executed_at}"#,
// need to use CXX compiler as linker to resolve the exception functions
// that are only existed in CXX libraries
Some(self.cxx.borrow()[&target].path().into())
} else if target != self.config.build
} else if !self.is_builder_target(&target)
&& helpers::use_host_linker(target)
&& !target.is_msvc()
{
Expand Down Expand Up @@ -1925,6 +1925,11 @@ to download LLVM rather than building it.
stream.reset().unwrap();
result
}

/// Checks if the given target is the same as the builder target.
fn is_builder_target(&self, target: &TargetSelection) -> bool {
&self.config.build == target
}
}

#[cfg(unix)]
Expand Down

0 comments on commit 9ea1324

Please sign in to comment.