Skip to content

Commit d3b82ea

Browse files
authored
Simplified build script bin names in new layout (#16812)
### What does this PR try to resolve? This PR spawned out of a [comment](#16807 (comment)) from @epage raising that we didn't remove the hashes for some bins. However we did remove the hashes from "regular" bins in #16351 This PR * Removes the use of `-Cextra-filename` for build scripts when `-Zbuild-dir-new-layout` is enabled. * Does not uplift build-scripts in the `build-dir` internally. (its not really uplifting, its more of a rename via hardlink) Previously, we had a directory like ``` build/<pkg>/<hash>/out/ build_script_build-[HASH].d build_script_build-[HASH][EXE] build-script-build[EXE] ``` After this change ``` build/<pkg>/<hash>/out/ build_script_build.d build_script_build[EXE] ``` Part of: #15010 ### How to test and review this PR? See the test updates in each commit. I could split into 2 PRs if needed but I felt the changes were related and small enough to go together.
2 parents 9cb2a0c + 6c08b3f commit d3b82ea

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/cargo/core/compiler/build_runner/compilation_files.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,13 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
449449
///
450450
/// Returns `None` if the unit shouldn't be uplifted (for example, a
451451
/// dependent rlib).
452-
fn uplift_to(&self, unit: &Unit, file_type: &FileType, from_path: &Path) -> Option<PathBuf> {
452+
fn uplift_to(
453+
&self,
454+
unit: &Unit,
455+
file_type: &FileType,
456+
from_path: &Path,
457+
bcx: &BuildContext<'_, '_>,
458+
) -> Option<PathBuf> {
453459
// Tests, check, doc, etc. should not be uplifted.
454460
if unit.mode != CompileMode::Build || file_type.flavor == FileFlavor::Rmeta {
455461
return None;
@@ -460,6 +466,11 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
460466
return None;
461467
}
462468

469+
// Build script bins are never uplifted.
470+
if bcx.gctx.cli_unstable().build_dir_new_layout && unit.target.is_custom_build() {
471+
return None;
472+
}
473+
463474
// - Binaries: The user always wants to see these, even if they are
464475
// implicitly built (for example for integration tests).
465476
// - dylibs: This ensures that the dynamic linker pulls in all the
@@ -645,7 +656,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
645656

646657
// If, the `different_binary_name` feature is enabled, the name of the hardlink will
647658
// be the name of the binary provided by the user in `Cargo.toml`.
648-
let hardlink = self.uplift_to(unit, &file_type, &path);
659+
let hardlink = self.uplift_to(unit, &file_type, &path, bcx);
649660
let export_path = if unit.target.is_custom_build() {
650661
None
651662
} else {
@@ -905,8 +916,8 @@ fn use_extra_filename(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
905916
}
906917
// No metadata in these cases:
907918
//
908-
// - dylib, cdylib, executable: `pkg_dir` avoids collisions for us and rustc isn't looking these
909-
// up by `-Cextra-filename`
919+
// - dylib, cdylib, executable, build-scripts: `pkg_dir` avoids collisions for us and rustc isn't
920+
// looking these up by `-Cextra-filename`
910921
//
911922
// The __CARGO_DEFAULT_LIB_METADATA env var is used to override this to
912923
// force metadata in the hash. This is only used for building libstd. For
@@ -915,7 +926,10 @@ fn use_extra_filename(bcx: &BuildContext<'_, '_>, unit: &Unit) -> bool {
915926
// installs. In addition it prevents accidentally loading a libstd of a
916927
// different compiler at runtime.
917928
// See https://github.com/rust-lang/cargo/issues/3005
918-
if (unit.target.is_dylib() || unit.target.is_cdylib() || unit.target.is_executable())
929+
if (unit.target.is_dylib()
930+
|| unit.target.is_cdylib()
931+
|| unit.target.is_executable()
932+
|| unit.target.is_custom_build())
919933
&& bcx.gctx.get_env("__CARGO_DEFAULT_LIB_METADATA").is_err()
920934
{
921935
return false;

src/cargo/core/compiler/custom_build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,12 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
349349
}
350350

351351
// Building the command to execute
352-
let to_exec = script_dir.join(unit.target.name());
352+
let bin_name = if bcx.gctx.cli_unstable().build_dir_new_layout {
353+
unit.target.crate_name()
354+
} else {
355+
unit.target.name().to_string()
356+
};
357+
let to_exec = script_dir.join(bin_name);
353358

354359
// Start preparing the process to execute, starting out with some
355360
// environment variables. Note that the profile-related environment

tests/testsuite/build_dir.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,8 @@ fn build_script_should_output_to_build_dir() {
281281
[ROOT]/foo/build-dir/CACHEDIR.TAG
282282
[ROOT]/foo/build-dir/debug/.cargo-build-lock
283283
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/foo.txt
284-
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/build_script_build[..].d
285-
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/build_script_build[..][EXE]
286-
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/build-script-build[EXE]
284+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/build_script_build.d
285+
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/build_script_build[EXE]
287286
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/foo[..][EXE]
288287
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/out/foo[..].d
289288
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/invoked.timestamp

tests/testsuite/clean_new_layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn build_script() {
401401
.with_stderr_data(str![[r#"
402402
[COMPILING] foo v0.0.1 ([ROOT]/foo)
403403
[RUNNING] `rustc [..] build.rs [..]`
404-
[RUNNING] `[ROOT]/foo/target/debug/build/foo/[HASH]/out/build-script-build`
404+
[RUNNING] `[ROOT]/foo/target/debug/build/foo/[HASH]/out/build_script_build`
405405
[RUNNING] `rustc [..] src/main.rs [..]`
406406
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
407407

0 commit comments

Comments
 (0)