Skip to content

Commit aab7b90

Browse files
committed
Added build.build_dir configuration option
This commit adds a `build_dir` option to the `build` table in `config.toml` and adds the equivalent field to `Workspace` and `GlobalContext`.
1 parent f6f2622 commit aab7b90

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

src/cargo/core/workspace.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ pub struct Workspace<'gctx> {
6767
/// `None` if the default path of `root/target` should be used.
6868
target_dir: Option<Filesystem>,
6969

70+
/// Shared build directory for intermediate build artifacts.
71+
/// This directory may be shared between multiple workspaces.
72+
build_dir: Option<Filesystem>,
73+
7074
/// List of members in this workspace with a listing of all their manifest
7175
/// paths. The packages themselves can be looked up through the `packages`
7276
/// set above.
@@ -209,6 +213,7 @@ impl<'gctx> Workspace<'gctx> {
209213
pub fn new(manifest_path: &Path, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
210214
let mut ws = Workspace::new_default(manifest_path.to_path_buf(), gctx);
211215
ws.target_dir = gctx.target_dir()?;
216+
ws.build_dir = gctx.build_dir()?;
212217

213218
if manifest_path.is_relative() {
214219
bail!(
@@ -238,6 +243,7 @@ impl<'gctx> Workspace<'gctx> {
238243
},
239244
root_manifest: None,
240245
target_dir: None,
246+
build_dir: None,
241247
members: Vec::new(),
242248
member_ids: HashSet::new(),
243249
default_members: Vec::new(),
@@ -282,6 +288,7 @@ impl<'gctx> Workspace<'gctx> {
282288
} else {
283289
ws.gctx.target_dir()?
284290
};
291+
ws.build_dir = ws.target_dir.clone();
285292
ws.members.push(ws.current_manifest.clone());
286293
ws.member_ids.insert(id);
287294
ws.default_members.push(ws.current_manifest.clone());
@@ -418,6 +425,15 @@ impl<'gctx> Workspace<'gctx> {
418425
.unwrap_or_else(|| self.default_target_dir())
419426
}
420427

428+
pub fn build_dir(&self) -> Filesystem {
429+
if !self.gctx().cli_unstable().build_dir {
430+
return self.target_dir();
431+
}
432+
self.build_dir
433+
.clone()
434+
.unwrap_or_else(|| self.default_target_dir())
435+
}
436+
421437
fn default_target_dir(&self) -> Filesystem {
422438
if self.root_maybe().is_embedded() {
423439
let hash = crate::util::hex::short_hash(&self.root_manifest().to_string_lossy());

src/cargo/util/context/mod.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ impl GlobalContext {
603603
///
604604
/// Returns `None` if the user has not chosen an explicit directory.
605605
///
606-
/// Callers should prefer `Workspace::target_dir` instead.
606+
/// Callers should prefer [`Workspace::target_dir`] instead.
607607
pub fn target_dir(&self) -> CargoResult<Option<Filesystem>> {
608608
if let Some(dir) = &self.target_dir {
609609
Ok(Some(dir.clone()))
@@ -634,6 +634,34 @@ impl GlobalContext {
634634
}
635635
}
636636

637+
/// The directory to use for intermediate build artifacts.
638+
///
639+
/// Falls back to the target directory if not specified.
640+
///
641+
/// Callers should prefer [`Workspace::build_dir`] instead.
642+
pub fn build_dir(&self) -> CargoResult<Option<Filesystem>> {
643+
if !self.cli_unstable().build_dir {
644+
return self.target_dir();
645+
}
646+
if let Some(val) = &self.build_config()?.build_dir {
647+
let path = val.resolve_path(self);
648+
649+
// Check if the target directory is set to an empty string in the config.toml file.
650+
if val.raw_value().is_empty() {
651+
bail!(
652+
"the build directory is set to an empty string in {}",
653+
val.value().definition
654+
)
655+
}
656+
657+
Ok(Some(Filesystem::new(path)))
658+
} else {
659+
// For now, fallback to the previous implementation.
660+
// This will change in the future.
661+
return self.target_dir();
662+
}
663+
}
664+
637665
/// Get a configuration value by key.
638666
///
639667
/// This does NOT look at environment variables. See `get_cv_with_env` for
@@ -2653,6 +2681,7 @@ pub struct CargoBuildConfig {
26532681
pub pipelining: Option<bool>,
26542682
pub dep_info_basedir: Option<ConfigRelativePath>,
26552683
pub target_dir: Option<ConfigRelativePath>,
2684+
pub build_dir: Option<ConfigRelativePath>,
26562685
pub incremental: Option<bool>,
26572686
pub target: Option<BuildTargetConfig>,
26582687
pub jobs: Option<JobsConfig>,

src/doc/src/reference/unstable.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Each new feature described below should explain how to use it.
7878
* [feature-unification](#feature-unification) --- Enable new feature unification modes in workspaces
7979
* Output behavior
8080
* [artifact-dir](#artifact-dir) --- Adds a directory where artifacts are copied to.
81+
* [build-dir](#build-dir) --- Adds a directory where intermediate build artifacts are stored.
8182
* [Different binary name](#different-binary-name) --- Assign a name to the built binary that is separate from the crate name.
8283
* [root-dir](#root-dir) --- Controls the root directory relative to which paths are printed
8384
* Compile behavior
@@ -238,6 +239,27 @@ This can also be specified in `.cargo/config.toml` files.
238239
artifact-dir = "out"
239240
```
240241

242+
## build-dir
243+
* Original Issue: [#14125](https://github.com/rust-lang/cargo/issues/14125)
244+
* Tracking Issue: [#14125](https://github.com/rust-lang/cargo/issues/14125)
245+
246+
The directory where intermediate build artifacts will be stored.
247+
Intermediate artifacts are produced by Rustc/Cargo during the build process.
248+
249+
```toml
250+
[build]
251+
build-dir = "out"
252+
```
253+
254+
### `build.build-dir`
255+
256+
* Type: string (path)
257+
* Default: Defaults to the value of `build.target-dir`
258+
* Environment: `CARGO_BUILD_BUILD_DIR`
259+
260+
The path to where internal files used as part of the build are placed.
261+
262+
241263
## root-dir
242264
* Original Issue: [#9887](https://github.com/rust-lang/cargo/issues/9887)
243265
* Tracking Issue: None (not currently slated for stabilization)

0 commit comments

Comments
 (0)