Skip to content

Commit c89baf2

Browse files
committed
feat(build-dir): Reorganize build-dir layout
1 parent 019479e commit c89baf2

File tree

6 files changed

+337
-192
lines changed

6 files changed

+337
-192
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,21 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
230230
self.export_dir.clone()
231231
}
232232

233-
/// Directory name to use for a package in the form `NAME-HASH`.
233+
/// Directory name to use for a package in the form `{NAME}/{HASH}`.
234234
///
235235
/// Note that some units may share the same directory, so care should be
236236
/// taken in those cases!
237237
fn pkg_dir(&self, unit: &Unit) -> String {
238+
let seperator = match self.ws.gctx().cli_unstable().build_dir_new_layout {
239+
true => "/",
240+
false => "-",
241+
};
238242
let name = unit.pkg.package_id().name();
239243
let meta = self.metas[unit];
240244
if let Some(c_extra_filename) = meta.c_extra_filename() {
241-
format!("{}-{}", name, c_extra_filename)
245+
format!("{}{}{}", name, seperator, c_extra_filename)
242246
} else {
243-
format!("{}-{}", name, self.target_short_hash(unit))
247+
format!("{}{}{}", name, seperator, self.target_short_hash(unit))
244248
}
245249
}
246250

@@ -255,14 +259,16 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
255259
}
256260

257261
/// Returns the host `deps` directory path.
258-
pub fn host_deps(&self) -> PathBuf {
259-
self.host.deps()
262+
pub fn host_deps(&self, unit: &Unit) -> PathBuf {
263+
let dir = self.pkg_dir(unit);
264+
self.host.deps(&dir)
260265
}
261266

262267
/// Returns the directories where Rust crate dependencies are found for the
263268
/// specified unit.
264269
pub fn deps_dir(&self, unit: &Unit) -> PathBuf {
265-
self.layout(unit.kind).deps()
270+
let dir = self.pkg_dir(unit);
271+
self.layout(unit.kind).deps(&dir)
266272
}
267273

268274
/// Directory where the fingerprint for the given unit should go.
@@ -342,7 +348,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
342348
assert!(unit.target.is_custom_build());
343349
assert!(unit.mode.is_run_custom_build());
344350
let dir = self.pkg_dir(unit);
345-
self.layout(unit.kind).build().join(dir)
351+
self.layout(unit.kind).build_script_execution(&dir)
346352
}
347353

348354
/// Returns the "`OUT_DIR`" directory for running a build script.

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::core::compiler::{self, Unit, artifact};
1010
use crate::util::cache_lock::CacheLockMode;
1111
use crate::util::errors::CargoResult;
1212
use anyhow::{Context as _, bail};
13+
use cargo_util::paths;
1314
use filetime::FileTime;
1415
use itertools::Itertools;
1516
use jobserver::Client;
@@ -401,9 +402,17 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
401402
self.compilation
402403
.root_output
403404
.insert(kind, layout.dest().to_path_buf());
404-
self.compilation
405-
.deps_output
406-
.insert(kind, layout.deps().to_path_buf());
405+
if self.bcx.gctx.cli_unstable().build_dir_new_layout {
406+
for (unit, _) in self.bcx.unit_graph.iter() {
407+
let dep_dir = self.files().deps_dir(unit);
408+
paths::create_dir_all(&dep_dir)?;
409+
self.compilation.deps_output.insert(kind, dep_dir);
410+
}
411+
} else {
412+
self.compilation
413+
.deps_output
414+
.insert(kind, layout.legacy_deps().to_path_buf());
415+
}
407416
}
408417
Ok(())
409418
}

src/cargo/core/compiler/layout.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub struct Layout {
142142
/// Will be `None` when the build-dir and target-dir are the same path as we cannot
143143
/// lock the same path twice.
144144
_build_lock: Option<FileLock>,
145+
is_new_layout: bool,
145146
}
146147

147148
impl Layout {
@@ -156,6 +157,7 @@ impl Layout {
156157
target: Option<CompileTarget>,
157158
dest: &str,
158159
) -> CargoResult<Layout> {
160+
let is_new_layout = ws.gctx().cli_unstable().build_dir_new_layout;
159161
let mut root = ws.target_dir();
160162
let mut build_root = ws.build_dir();
161163
if let Some(target) = target {
@@ -212,14 +214,17 @@ impl Layout {
212214
dest,
213215
_lock: lock,
214216
_build_lock: build_lock,
217+
is_new_layout,
215218
})
216219
}
217220

218221
/// Makes sure all directories stored in the Layout exist on the filesystem.
219222
pub fn prepare(&mut self) -> CargoResult<()> {
220-
paths::create_dir_all(&self.deps)?;
223+
if !self.is_new_layout {
224+
paths::create_dir_all(&self.deps)?;
225+
paths::create_dir_all(&self.fingerprint)?;
226+
}
221227
paths::create_dir_all(&self.incremental)?;
222-
paths::create_dir_all(&self.fingerprint)?;
223228
paths::create_dir_all(&self.examples)?;
224229
paths::create_dir_all(&self.build_examples)?;
225230
paths::create_dir_all(&self.build)?;
@@ -232,8 +237,12 @@ impl Layout {
232237
&self.dest
233238
}
234239
/// Fetch the deps path.
235-
pub fn deps(&self) -> PathBuf {
236-
self.legacy_deps().to_path_buf()
240+
pub fn deps(&self, pkg_dir: &str) -> PathBuf {
241+
if self.is_new_layout {
242+
self.build_unit(pkg_dir).join("deps")
243+
} else {
244+
self.legacy_deps().to_path_buf()
245+
}
237246
}
238247
/// Fetch the deps path. (old layout)
239248
pub fn legacy_deps(&self) -> &Path {
@@ -261,7 +270,11 @@ impl Layout {
261270
}
262271
/// Fetch the fingerprint path.
263272
pub fn fingerprint(&self, pkg_dir: &str) -> PathBuf {
264-
self.legacy_fingerprint().to_path_buf().join(pkg_dir)
273+
if self.is_new_layout {
274+
self.build_unit(pkg_dir).join("fingerprint")
275+
} else {
276+
self.legacy_fingerprint().to_path_buf().join(pkg_dir)
277+
}
265278
}
266279
/// Fetch the fingerprint path. (old layout)
267280
pub fn legacy_fingerprint(&self) -> &Path {
@@ -273,16 +286,28 @@ impl Layout {
273286
}
274287
/// Fetch the build script path.
275288
pub fn build_script(&self, pkg_dir: &str) -> PathBuf {
276-
self.build().join(pkg_dir)
289+
if self.is_new_layout {
290+
self.build_unit(pkg_dir).join("build-script")
291+
} else {
292+
self.build().join(pkg_dir)
293+
}
277294
}
278295
/// Fetch the build script execution path.
279296
pub fn build_script_execution(&self, pkg_dir: &str) -> PathBuf {
280-
self.build().join(pkg_dir)
297+
if self.is_new_layout {
298+
self.build_unit(pkg_dir).join("build-script-execution")
299+
} else {
300+
self.build().join(pkg_dir)
301+
}
281302
}
282303
/// Fetch the artifact path.
283304
pub fn artifact(&self) -> &Path {
284305
&self.artifact
285306
}
307+
/// Fetch the build unit path
308+
pub fn build_unit(&self, pkg_dir: &str) -> PathBuf {
309+
self.build().join(pkg_dir)
310+
}
286311
/// Create and return the tmp path.
287312
pub fn prepare_tmp(&self) -> CargoResult<&Path> {
288313
paths::create_dir_all(&self.tmp)?;

src/cargo/core/compiler/mod.rs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub mod unit_dependencies;
5656
pub mod unit_graph;
5757

5858
use std::borrow::Cow;
59-
use std::collections::{HashMap, HashSet};
59+
use std::collections::{BTreeMap, HashMap, HashSet};
6060
use std::env;
6161
use std::ffi::{OsStr, OsString};
6262
use std::fmt::Display;
@@ -69,6 +69,7 @@ use std::sync::{Arc, LazyLock};
6969
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
7070
use anyhow::{Context as _, Error};
7171
use cargo_platform::{Cfg, Platform};
72+
use itertools::Itertools;
7273
use lazycell::LazyCell;
7374
use regex::Regex;
7475
use tracing::{debug, instrument, trace};
@@ -1664,18 +1665,35 @@ fn build_deps_args(
16641665
unit: &Unit,
16651666
) -> CargoResult<()> {
16661667
let bcx = build_runner.bcx;
1667-
cmd.arg("-L").arg(&{
1668-
let mut deps = OsString::from("dependency=");
1669-
deps.push(build_runner.files().deps_dir(unit));
1670-
deps
1671-
});
1668+
if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout {
1669+
let mut map = BTreeMap::new();
1670+
1671+
// Recursively add all depenendency args to rustc process
1672+
add_dep_arg(&mut map, build_runner, unit);
1673+
1674+
let paths = map.into_iter().map(|(_, path)| path).sorted_unstable();
1675+
1676+
for path in paths {
1677+
cmd.arg("-L").arg(&{
1678+
let mut deps = OsString::from("dependency=");
1679+
deps.push(path);
1680+
deps
1681+
});
1682+
}
1683+
} else {
1684+
cmd.arg("-L").arg(&{
1685+
let mut deps = OsString::from("dependency=");
1686+
deps.push(build_runner.files().deps_dir(unit));
1687+
deps
1688+
});
1689+
}
16721690

16731691
// Be sure that the host path is also listed. This'll ensure that proc macro
16741692
// dependencies are correctly found (for reexported macros).
16751693
if !unit.kind.is_host() {
16761694
cmd.arg("-L").arg(&{
16771695
let mut deps = OsString::from("dependency=");
1678-
deps.push(build_runner.files().host_deps());
1696+
deps.push(build_runner.files().host_deps(unit));
16791697
deps
16801698
});
16811699
}
@@ -1751,6 +1769,21 @@ fn build_deps_args(
17511769
Ok(())
17521770
}
17531771

1772+
fn add_dep_arg<'a, 'b: 'a>(
1773+
map: &mut BTreeMap<&'a Unit, PathBuf>,
1774+
build_runner: &'b BuildRunner<'b, '_>,
1775+
unit: &'a Unit,
1776+
) {
1777+
if map.contains_key(&unit) {
1778+
return;
1779+
}
1780+
map.insert(&unit, build_runner.files().deps_dir(&unit));
1781+
1782+
for dep in build_runner.unit_deps(unit) {
1783+
add_dep_arg(map, build_runner, &dep.unit);
1784+
}
1785+
}
1786+
17541787
/// Adds extra rustc flags and environment variables collected from the output
17551788
/// of a build-script to the command to execute, include custom environment
17561789
/// variables and `cfg`.

src/cargo/ops/cargo_clean.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,13 @@ fn clean_specs(
226226
CompileMode::Check { test: false },
227227
] {
228228
for (compile_kind, layout) in &layouts {
229-
let triple = target_data.short_name(compile_kind);
229+
if clean_ctx.gctx.cli_unstable().build_dir_new_layout {
230+
let dir = layout.build_unit(&pkg.name());
231+
clean_ctx.rm_rf_glob(&dir)?;
232+
continue;
233+
}
230234

235+
let triple = target_data.short_name(compile_kind);
231236
let (file_types, _unsupported) = target_data
232237
.info(*compile_kind)
233238
.rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;

0 commit comments

Comments
 (0)