Skip to content

Commit 2b71fd6

Browse files
authored
Stabilize build.build-dir (#15833)
This PR stabilizes `build.build-dir` which moves intermediate build artifacts out of `target-dir`. * Tracking issue: #14125 * Call for testing: #14125 (comment) * FCP: #14125 (comment) * Implementation: #15104 Closes #14125
2 parents a2f54c7 + a24ae6f commit 2b71fd6

21 files changed

+159
-218
lines changed

src/cargo/core/features.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,6 @@ unstable_cli_options!(
844844
avoid_dev_deps: bool = ("Avoid installing dev-dependencies if possible"),
845845
binary_dep_depinfo: bool = ("Track changes to dependency artifacts"),
846846
bindeps: bool = ("Allow Cargo packages to depend on bin, cdylib, and staticlib crates, and use the artifacts built by those crates"),
847-
build_dir: bool = ("Enable the `build.build-dir` option in .cargo/config.toml file"),
848847
#[serde(deserialize_with = "deserialize_comma_separated_list")]
849848
build_std: Option<Vec<String>> = ("Enable Cargo to compile the standard library itself as part of a crate graph compilation"),
850849
#[serde(deserialize_with = "deserialize_comma_separated_list")]
@@ -970,6 +969,8 @@ const STABILIZED_DOCTEST_XCOMPILE: &str = "Doctest cross-compiling is now always
970969
const STABILIZED_PACKAGE_WORKSPACE: &str =
971970
"Workspace packaging and publishing (a.k.a. `-Zpackage-workspace`) is now always enabled.";
972971

972+
const STABILIZED_BUILD_DIR: &str = "build.build-dir is now always enabled.";
973+
973974
fn deserialize_comma_separated_list<'de, D>(
974975
deserializer: D,
975976
) -> Result<Option<Vec<String>>, D::Error>
@@ -1352,6 +1353,7 @@ impl CliUnstable {
13521353
"registry-auth" => stabilized_warn(k, "1.74", STABILIZED_REGISTRY_AUTH),
13531354
"check-cfg" => stabilized_warn(k, "1.80", STABILIZED_CHECK_CFG),
13541355
"package-workspace" => stabilized_warn(k, "1.89", STABILIZED_PACKAGE_WORKSPACE),
1356+
"build-dir" => stabilized_warn(k, "1.91", STABILIZED_BUILD_DIR),
13551357

13561358
// Unstable features
13571359
// Sorted alphabetically:
@@ -1360,7 +1362,6 @@ impl CliUnstable {
13601362
"avoid-dev-deps" => self.avoid_dev_deps = parse_empty(k, v)?,
13611363
"binary-dep-depinfo" => self.binary_dep_depinfo = parse_empty(k, v)?,
13621364
"bindeps" => self.bindeps = parse_empty(k, v)?,
1363-
"build-dir" => self.build_dir = parse_empty(k, v)?,
13641365
"build-std" => self.build_std = Some(parse_list(v)),
13651366
"build-std-features" => self.build_std_features = Some(parse_list(v)),
13661367
"cargo-lints" => self.cargo_lints = parse_empty(k, v)?,

src/cargo/core/workspace.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,6 @@ impl<'gctx> Workspace<'gctx> {
443443
}
444444

445445
pub fn build_dir(&self) -> Filesystem {
446-
if !self.gctx().cli_unstable().build_dir {
447-
return self.target_dir();
448-
}
449446
self.build_dir.clone().unwrap_or_else(|| self.target_dir())
450447
}
451448

src/cargo/ops/cargo_clean.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn clean(ws: &Workspace<'_>, opts: &CleanOptions<'_>) -> CargoResult<()> {
7878
// Note that we don't bother grabbing a lock here as we're just going to
7979
// blow it all away anyway.
8080
if opts.spec.is_empty() {
81-
let paths: &[PathBuf] = if gctx.cli_unstable().build_dir && build_dir != target_dir {
81+
let paths: &[PathBuf] = if build_dir != target_dir {
8282
&[
8383
target_dir.into_path_unlocked(),
8484
build_dir.into_path_unlocked(),

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,7 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
5252
.collect(),
5353
resolve,
5454
target_directory: ws.target_dir().into_path_unlocked(),
55-
build_directory: ws
56-
.gctx()
57-
.cli_unstable()
58-
.build_dir
59-
.then(|| ws.build_dir().into_path_unlocked()),
55+
build_directory: ws.build_dir().into_path_unlocked(),
6056
version: VERSION,
6157
workspace_root: ws.root().to_path_buf(),
6258
metadata: ws.custom_metadata().cloned(),
@@ -73,8 +69,7 @@ pub struct ExportInfo {
7369
workspace_default_members: Vec<PackageIdSpec>,
7470
resolve: Option<MetadataResolve>,
7571
target_directory: PathBuf,
76-
#[serde(skip_serializing_if = "Option::is_none")]
77-
build_directory: Option<PathBuf>,
72+
build_directory: PathBuf,
7873
version: u32,
7974
workspace_root: PathBuf,
8075
metadata: Option<toml::Value>,

src/cargo/ops/cargo_package/verify.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,7 @@ pub fn run_verify(
6969
// When packaging we use an ephemeral workspace but reuse the build cache to reduce
7070
// verification time if the user has already compiled the dependencies and the fingerprint
7171
// is unchanged.
72-
let target_dir = if gctx.cli_unstable().build_dir {
73-
Some(ws.build_dir())
74-
} else {
75-
Some(ws.target_dir())
76-
};
77-
78-
let mut ws = Workspace::ephemeral(new_pkg, gctx, target_dir, true)?;
72+
let mut ws = Workspace::ephemeral(new_pkg, gctx, Some(ws.build_dir()), true)?;
7973
if let Some(local_reg) = local_reg {
8074
ws.add_local_overlay(
8175
local_reg.upstream,

src/cargo/util/context/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,6 @@ impl GlobalContext {
650650
///
651651
/// Callers should prefer [`Workspace::build_dir`] instead.
652652
pub fn build_dir(&self, workspace_manifest_path: &PathBuf) -> CargoResult<Option<Filesystem>> {
653-
if !self.cli_unstable().build_dir {
654-
return self.target_dir();
655-
}
656653
if let Some(val) = &self.build_config()?.build_dir {
657654
let replacements = vec![
658655
(

src/doc/src/reference/build-cache.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
# Build cache
22

3-
Cargo stores the output of a build into the "target" directory. By default,
4-
this is the directory named `target` in the root of your
5-
[*workspace*][def-workspace]. To change the location, you can set the
3+
Cargo stores the output of a build into the "target" and "build" directories. By default,
4+
both directories point to a directory named `target` in the root of your
5+
[*workspace*][def-workspace]. To change the location of the target-dir, you can set the
66
`CARGO_TARGET_DIR` [environment variable], the [`build.target-dir`] config
7-
value, or the `--target-dir` command-line flag.
7+
value, or the `--target-dir` command-line flag. To change the location of the build-dir, you can set the
8+
`CARGO_BUILD_BUILD_DIR` [environment variable] or the [`build.build-dir`] config value.
9+
10+
Artifacts are split in two catagories:
11+
* Final build artifacts
12+
* Final build artifacts are output meant for end users of Cargo
13+
* e.g. binaries for bin crates, output of `cargo doc`, Cargo `--timings` reports
14+
* Stored in the target-dir
15+
* Intermediate build artifacts
16+
* Intermediate build artifacts are internal to Cargo and the Rust compiler
17+
* End users will generally not need to interact with intermediate build artifacts
18+
* Stored in the Cargo build-dir
819

920
The directory layout depends on whether or not you are using the `--target`
1021
flag to build for a specific platform. If `--target` is not specified, Cargo
@@ -53,15 +64,15 @@ Directory | Description
5364
<code style="white-space: nowrap">target/doc/</code> | Contains rustdoc documentation ([`cargo doc`]).
5465
<code style="white-space: nowrap">target/package/</code> | Contains the output of the [`cargo package`] and [`cargo publish`] commands.
5566

56-
Cargo also creates several other directories and files needed for the build
57-
process. Their layout is considered internal to Cargo, and is subject to
67+
Cargo also creates several other directories and files in the build-dir needed for the build
68+
process. The build-dir layout is considered internal to Cargo, and is subject to
5869
change. Some of these directories are:
5970

6071
Directory | Description
6172
----------|------------
62-
<code style="white-space: nowrap">target/debug/deps/</code> | Dependencies and other artifacts.
63-
<code style="white-space: nowrap">target/debug/incremental/</code> | `rustc` [incremental output], a cache used to speed up subsequent builds.
64-
<code style="white-space: nowrap">target/debug/build/</code> | Output from [build scripts].
73+
<code style="white-space: nowrap">build-dir/debug/deps/</code> | Dependencies and other artifacts.
74+
<code style="white-space: nowrap">build-dir/debug/incremental/</code> | `rustc` [incremental output], a cache used to speed up subsequent builds.
75+
<code style="white-space: nowrap">build-dir/debug/build/</code> | Output from [build scripts].
6576

6677
## Dep-info files
6778

@@ -92,6 +103,7 @@ configuration][config]. Refer to sccache documentation for more details.
92103
[`build.dep-info-basedir`]: ../reference/config.md#builddep-info-basedir
93104
[`build.rustc-wrapper`]: ../reference/config.md#buildrustc-wrapper
94105
[`build.target-dir`]: ../reference/config.md#buildtarget-dir
106+
[`build.build-dir`]: ../reference/config.md#buildbuild-dir
95107
[`cargo doc`]: ../commands/cargo-doc.md
96108
[`cargo package`]: ../commands/cargo-package.md
97109
[`cargo publish`]: ../commands/cargo-publish.md

src/doc/src/reference/config.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ rustc-wrapper = "…" # run this wrapper instead of `rustc`
7070
rustc-workspace-wrapper = "" # run this wrapper instead of `rustc` for workspace members
7171
rustdoc = "rustdoc" # the doc generator tool
7272
target = "triple" # build for the target triple (ignored by `cargo install`)
73-
target-dir = "target" # path of where to place all generated artifacts
73+
target-dir = "target" # path of where to place generated artifacts
74+
build-dir = "target" # path of where to place intermediate build artifacts
7475
rustflags = ["", ""] # custom flags to pass to all compiler invocations
7576
rustdocflags = ["", ""] # custom flags to pass to rustdoc
7677
incremental = true # whether or not to enable incremental compilation
@@ -482,6 +483,26 @@ is a directory named `target` located at the root of the workspace.
482483

483484
Can be overridden with the `--target-dir` CLI option.
484485

486+
For more information see the [build cache documentation](../reference/build-cache.md).
487+
488+
#### `build.build-dir`
489+
490+
* Type: string (path)
491+
* Default: Defaults to the value of `build.target-dir`
492+
* Environment: `CARGO_BUILD_BUILD_DIR`
493+
494+
The directory where intermediate build artifacts will be stored.
495+
Intermediate artifacts are produced by Rustc/Cargo during the build process.
496+
497+
This option supports path templating.
498+
499+
Available template variables:
500+
* `{workspace-root}` resolves to root of the current workspace.
501+
* `{cargo-cache-home}` resolves to `CARGO_HOME`
502+
* `{workspace-path-hash}` resolves to a hash of the manifest path
503+
504+
For more information see the [build cache documentation](../reference/build-cache.md).
505+
485506
#### `build.rustflags`
486507
* Type: string or array of strings
487508
* Default: none

src/doc/src/reference/environment-variables.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ In summary, the supported environment variables are:
9595
* `CARGO_BUILD_RUSTDOC` --- The `rustdoc` executable, see [`build.rustdoc`].
9696
* `CARGO_BUILD_TARGET` --- The default target platform, see [`build.target`].
9797
* `CARGO_BUILD_TARGET_DIR` --- The default output directory, see [`build.target-dir`].
98+
* `CARGO_BUILD_BUILD_DIR` --- The default build directory, see [`build.build-dir`].
9899
* `CARGO_BUILD_RUSTFLAGS` --- Extra `rustc` flags, see [`build.rustflags`].
99100
* `CARGO_BUILD_RUSTDOCFLAGS` --- Extra `rustdoc` flags, see [`build.rustdocflags`].
100101
* `CARGO_BUILD_INCREMENTAL` --- Incremental compilation, see [`build.incremental`].
@@ -160,6 +161,7 @@ In summary, the supported environment variables are:
160161
[`build.rustdoc`]: config.md#buildrustdoc
161162
[`build.target`]: config.md#buildtarget
162163
[`build.target-dir`]: config.md#buildtarget-dir
164+
[`build.build-dir`]: config.md#buildbuild-dir
163165
[`build.rustflags`]: config.md#buildrustflags
164166
[`build.rustdocflags`]: config.md#buildrustdocflags
165167
[`build.incremental`]: config.md#buildincremental

src/doc/src/reference/unstable.md

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ Each new feature described below should explain how to use it.
8080
* [feature-unification](#feature-unification) --- Enable new feature unification modes in workspaces
8181
* Output behavior
8282
* [artifact-dir](#artifact-dir) --- Adds a directory where artifacts are copied to.
83-
* [build-dir](#build-dir) --- Adds a directory where intermediate build artifacts are stored.
8483
* [Different binary name](#different-binary-name) --- Assign a name to the built binary that is separate from the crate name.
8584
* [root-dir](#root-dir) --- Controls the root directory relative to which paths are printed
8685
* Compile behavior
@@ -247,34 +246,6 @@ This can also be specified in `.cargo/config.toml` files.
247246
artifact-dir = "out"
248247
```
249248

250-
## build-dir
251-
* Original Issue: [#14125](https://github.com/rust-lang/cargo/issues/14125)
252-
* Tracking Issue: [#14125](https://github.com/rust-lang/cargo/issues/14125)
253-
254-
The directory where intermediate build artifacts will be stored.
255-
Intermediate artifacts are produced by Rustc/Cargo during the build process.
256-
257-
```toml
258-
[build]
259-
build-dir = "out"
260-
```
261-
262-
### `build.build-dir`
263-
264-
* Type: string (path)
265-
* Default: Defaults to the value of `build.target-dir`
266-
* Environment: `CARGO_BUILD_BUILD_DIR`
267-
268-
The path to where internal files used as part of the build are placed.
269-
270-
This option supports path templating.
271-
272-
Available template variables:
273-
* `{workspace-root}` resolves to root of the current workspace.
274-
* `{cargo-cache-home}` resolves to `CARGO_HOME`
275-
* `{workspace-path-hash}` resolves to a hash of the manifest path
276-
277-
278249
## root-dir
279250
* Original Issue: [#9887](https://github.com/rust-lang/cargo/issues/9887)
280251
* Tracking Issue: None (not currently slated for stabilization)
@@ -2230,3 +2201,9 @@ Example:
22302201
cargo +nightly build --compile-time-deps -Z unstable-options
22312202
cargo +nightly check --compile-time-deps --all-targets -Z unstable-options
22322203
```
2204+
2205+
## build-dir
2206+
2207+
Support for `build.build-dir` was stabilized in the 1.91 release.
2208+
See the [config documentation](config.md#buildbuild-dir) for information about changing the build-dir
2209+

0 commit comments

Comments
 (0)