Rollup merge of #145156 - Kobzol:cargo-build-dir, r=lqd,jieyouxu

Override custom Cargo `build-dir` in bootstrap

The context for this issue is in https://github.com/rust-lang/rust/issues/145107. The issue is that if people configure `build-dir`, it would break bootstrap. For now, we just hard-code it to our self-contained target directories inside the build directory.

Tested by putting the following:
```toml
[build]
build-dir = "/tmp/foo"

[unstable]
build-dir = true
```

into `<rustc-checkout>/.cargo/config.toml`. `x build` works with this PR, doesn't work without this PR.

Fixes: https://github.com/rust-lang/rust/issues/145107
This commit is contained in:
Stuart Cook 2025-08-10 19:45:53 +10:00 committed by GitHub
commit 53eab6bc75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -433,6 +433,15 @@ impl Builder<'_> {
let out_dir = self.stage_out(compiler, mode);
cargo.env("CARGO_TARGET_DIR", &out_dir);
// Bootstrap makes a lot of assumptions about the artifacts produced in the target
// directory. If users override the "build directory" using `build-dir`
// (https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-dir), then
// bootstrap couldn't find these artifacts. So we forcefully override that option to our
// target directory here.
// In the future, we could attempt to read the build-dir location from Cargo and actually
// respect it.
cargo.env("CARGO_BUILD_BUILD_DIR", &out_dir);
// Found with `rg "init_env_logger\("`. If anyone uses `init_env_logger`
// from out of tree it shouldn't matter, since x.py is only used for
// building in-tree.