Skip to content

Commit b137a08

Browse files
committed
feat(build-dir): Reorganize build-dir layout
1 parent bf3fc46 commit b137a08

File tree

6 files changed

+424
-236
lines changed

6 files changed

+424
-236
lines changed

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,25 @@ 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}{SEPERATOR}{HASH}`.
234+
///
235+
/// The seperator will usually be `/` making nested hashed directories, but will sometimes be
236+
/// `-` for backwards compatiblity (artifact directory)
234237
///
235238
/// Note that some units may share the same directory, so care should be
236239
/// taken in those cases!
237-
fn pkg_dir(&self, unit: &Unit) -> String {
240+
fn pkg_dir(&self, unit: &Unit, seperator: &str) -> String {
241+
let seperator = match self.ws.gctx().cli_unstable().build_dir_new_layout {
242+
true => seperator,
243+
false => "-",
244+
};
245+
238246
let name = unit.pkg.package_id().name();
239247
let meta = self.metas[unit];
240248
if let Some(c_extra_filename) = meta.c_extra_filename() {
241-
format!("{}-{}", name, c_extra_filename)
249+
format!("{}{}{}", name, seperator, c_extra_filename)
242250
} else {
243-
format!("{}-{}", name, self.target_short_hash(unit))
251+
format!("{}{}{}", name, seperator, self.target_short_hash(unit))
244252
}
245253
}
246254

@@ -255,20 +263,27 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
255263
}
256264

257265
/// Returns the host `deps` directory path.
258-
pub fn host_deps(&self) -> &Path {
259-
self.host.deps()
266+
pub fn host_deps(&self, unit: &Unit) -> PathBuf {
267+
let dir = self.pkg_dir(unit, "/");
268+
self.host.deps(&dir)
260269
}
261270

262271
/// Returns the directories where Rust crate dependencies are found for the
263272
/// specified unit.
264-
pub fn deps_dir(&self, unit: &Unit) -> &Path {
265-
self.layout(unit.kind).deps()
273+
pub fn deps_dir(&self, unit: &Unit) -> PathBuf {
274+
let dir = self.pkg_dir(unit, "/");
275+
self.layout(unit.kind).deps(&dir)
266276
}
267277

268278
/// Directory where the fingerprint for the given unit should go.
269279
pub fn fingerprint_dir(&self, unit: &Unit) -> PathBuf {
270-
let dir = self.pkg_dir(unit);
271-
self.layout(unit.kind).fingerprint().join(dir)
280+
let dir = self.pkg_dir(unit, "/");
281+
self.layout(unit.kind).fingerprint(&dir)
282+
}
283+
284+
/// Directory where incremental output for the given unit should go.
285+
pub fn incremental_dir(&self, unit: &Unit) -> &Path {
286+
self.layout(unit.kind).incremental()
272287
}
273288

274289
/// Returns the path for a file in the fingerprint directory.
@@ -302,16 +317,16 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
302317
assert!(unit.target.is_custom_build());
303318
assert!(!unit.mode.is_run_custom_build());
304319
assert!(self.metas.contains_key(unit));
305-
let dir = self.pkg_dir(unit);
306-
self.layout(CompileKind::Host).build().join(dir)
320+
let dir = self.pkg_dir(unit, "/");
321+
self.layout(CompileKind::Host).build_script(&dir)
307322
}
308323

309324
/// Returns the directory for compiled artifacts files.
310325
/// `/path/to/target/{debug,release}/deps/artifact/KIND/PKG-HASH`
311326
fn artifact_dir(&self, unit: &Unit) -> PathBuf {
312327
assert!(self.metas.contains_key(unit));
313328
assert!(unit.artifact.is_true());
314-
let dir = self.pkg_dir(unit);
329+
let dir = self.pkg_dir(unit, "-");
315330
let kind = match unit.target.kind() {
316331
TargetKind::Bin => "bin",
317332
TargetKind::Lib(lib_kinds) => match lib_kinds.as_slice() {
@@ -336,8 +351,8 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
336351
pub fn build_script_run_dir(&self, unit: &Unit) -> PathBuf {
337352
assert!(unit.target.is_custom_build());
338353
assert!(unit.mode.is_run_custom_build());
339-
let dir = self.pkg_dir(unit);
340-
self.layout(unit.kind).build().join(dir)
354+
let dir = self.pkg_dir(unit, "/");
355+
self.layout(unit.kind).build_script_execution(&dir)
341356
}
342357

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

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use std::sync::{Arc, Mutex};
66

77
use crate::core::PackageId;
88
use crate::core::compiler::compilation::{self, UnitOutput};
9-
use crate::core::compiler::{self, Unit, artifact};
9+
use crate::core::compiler::{self, CompileTarget, 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;
@@ -358,11 +359,18 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
358359
#[tracing::instrument(skip_all)]
359360
pub fn prepare_units(&mut self) -> CargoResult<()> {
360361
let dest = self.bcx.profiles.get_dir_name();
361-
let host_layout = Layout::new(self.bcx.ws, None, &dest)?;
362+
let host = &self.compilation.host;
363+
let host_target = CompileTarget::new(&host)?;
364+
let host_layout = if self.bcx.gctx.cli_unstable().build_dir_new_layout {
365+
Layout::new(self.bcx.ws, Some(host_target), &dest, true)?
366+
} else {
367+
Layout::new(self.bcx.ws, None, &dest, true)?
368+
};
362369
let mut targets = HashMap::new();
363370
for kind in self.bcx.all_kinds.iter() {
364371
if let CompileKind::Target(target) = *kind {
365-
let layout = Layout::new(self.bcx.ws, Some(target), &dest)?;
372+
let is_host = target == host_target;
373+
let layout = Layout::new(self.bcx.ws, Some(target), &dest, is_host)?;
366374
targets.insert(target, layout);
367375
}
368376
}
@@ -401,9 +409,17 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
401409
self.compilation
402410
.root_output
403411
.insert(kind, layout.dest().to_path_buf());
404-
self.compilation
405-
.deps_output
406-
.insert(kind, layout.deps().to_path_buf());
412+
if self.bcx.gctx.cli_unstable().build_dir_new_layout {
413+
for (unit, _) in self.bcx.unit_graph.iter() {
414+
let dep_dir = self.files().deps_dir(unit);
415+
paths::create_dir_all(&dep_dir)?;
416+
self.compilation.deps_output.insert(kind, dep_dir);
417+
}
418+
} else {
419+
self.compilation
420+
.deps_output
421+
.insert(kind, layout.legacy_deps().to_path_buf());
422+
}
407423
}
408424
Ok(())
409425
}

src/cargo/core/compiler/layout.rs

Lines changed: 59 additions & 8 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 {
@@ -155,12 +156,23 @@ impl Layout {
155156
ws: &Workspace<'_>,
156157
target: Option<CompileTarget>,
157158
dest: &str,
159+
is_host_layout: bool,
158160
) -> CargoResult<Layout> {
161+
let is_new_layout = ws.gctx().cli_unstable().build_dir_new_layout;
159162
let mut root = ws.target_dir();
160163
let mut build_root = ws.build_dir();
161-
if let Some(target) = target {
162-
root.push(target.short_name());
163-
build_root.push(target.short_name());
164+
if is_new_layout {
165+
assert!(target.is_some());
166+
let short_name = target.as_ref().unwrap().short_name();
167+
if !is_host_layout {
168+
root.push(short_name);
169+
}
170+
build_root.push(short_name);
171+
} else {
172+
if let Some(target) = target {
173+
root.push(target.short_name());
174+
build_root.push(target.short_name());
175+
}
164176
}
165177
let build_dest = build_root.join(dest);
166178
let dest = root.join(dest);
@@ -212,14 +224,17 @@ impl Layout {
212224
dest,
213225
_lock: lock,
214226
_build_lock: build_lock,
227+
is_new_layout,
215228
})
216229
}
217230

218231
/// Makes sure all directories stored in the Layout exist on the filesystem.
219232
pub fn prepare(&mut self) -> CargoResult<()> {
220-
paths::create_dir_all(&self.deps)?;
233+
if !self.is_new_layout {
234+
paths::create_dir_all(&self.deps)?;
235+
paths::create_dir_all(&self.fingerprint)?;
236+
}
221237
paths::create_dir_all(&self.incremental)?;
222-
paths::create_dir_all(&self.fingerprint)?;
223238
paths::create_dir_all(&self.examples)?;
224239
paths::create_dir_all(&self.build_examples)?;
225240
paths::create_dir_all(&self.build)?;
@@ -232,7 +247,15 @@ impl Layout {
232247
&self.dest
233248
}
234249
/// Fetch the deps path.
235-
pub fn deps(&self) -> &Path {
250+
pub fn deps(&self, pkg_dir: &str) -> PathBuf {
251+
if self.is_new_layout {
252+
self.build_unit(pkg_dir).join("deps")
253+
} else {
254+
self.deps.clone()
255+
}
256+
}
257+
/// Fetch the deps path. (old layout)
258+
pub fn legacy_deps(&self) -> &Path {
236259
&self.deps
237260
}
238261
/// Fetch the examples path.
@@ -256,17 +279,45 @@ impl Layout {
256279
&self.incremental
257280
}
258281
/// Fetch the fingerprint path.
259-
pub fn fingerprint(&self) -> &Path {
282+
pub fn fingerprint(&self, pkg_dir: &str) -> PathBuf {
283+
if self.is_new_layout {
284+
self.build_unit(pkg_dir).join("fingerprint")
285+
} else {
286+
self.fingerprint.join(pkg_dir)
287+
}
288+
}
289+
/// Fetch the fingerprint path. (old layout)
290+
pub fn legacy_fingerprint(&self) -> &Path {
260291
&self.fingerprint
261292
}
262-
/// Fetch the build script path.
293+
/// Fetch the build path.
263294
pub fn build(&self) -> &Path {
264295
&self.build
265296
}
297+
/// Fetch the build script path.
298+
pub fn build_script(&self, pkg_dir: &str) -> PathBuf {
299+
if self.is_new_layout {
300+
self.build_unit(pkg_dir).join("build-script")
301+
} else {
302+
self.build().join(pkg_dir)
303+
}
304+
}
305+
/// Fetch the build script execution path.
306+
pub fn build_script_execution(&self, pkg_dir: &str) -> PathBuf {
307+
if self.is_new_layout {
308+
self.build_unit(pkg_dir).join("build-script-execution")
309+
} else {
310+
self.build().join(pkg_dir)
311+
}
312+
}
266313
/// Fetch the artifact path.
267314
pub fn artifact(&self) -> &Path {
268315
&self.artifact
269316
}
317+
/// Fetch the build unit path
318+
pub fn build_unit(&self, pkg_dir: &str) -> PathBuf {
319+
self.build().join(pkg_dir)
320+
}
270321
/// Create and return the tmp path.
271322
pub fn prepare_tmp(&self) -> CargoResult<&Path> {
272323
paths::create_dir_all(&self.tmp)?;

src/cargo/core/compiler/mod.rs

Lines changed: 42 additions & 13 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};
@@ -1353,12 +1354,8 @@ fn build_base_args(
13531354
.map(|s| s.as_ref()),
13541355
);
13551356
if incremental {
1356-
let dir = build_runner
1357-
.files()
1358-
.layout(unit.kind)
1359-
.incremental()
1360-
.as_os_str();
1361-
opt(cmd, "-C", "incremental=", Some(dir));
1357+
let dir = build_runner.files().incremental_dir(&unit);
1358+
opt(cmd, "-C", "incremental=", Some(dir.as_os_str()));
13621359
}
13631360

13641361
let pkg_hint_mostly_unused = match hints.mostly_unused {
@@ -1668,18 +1665,35 @@ fn build_deps_args(
16681665
unit: &Unit,
16691666
) -> CargoResult<()> {
16701667
let bcx = build_runner.bcx;
1671-
cmd.arg("-L").arg(&{
1672-
let mut deps = OsString::from("dependency=");
1673-
deps.push(build_runner.files().deps_dir(unit));
1674-
deps
1675-
});
1668+
if build_runner.bcx.gctx.cli_unstable().build_dir_new_layout {
1669+
let mut map = BTreeMap::new();
1670+
1671+
// Recusively 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+
}
16761690

16771691
// Be sure that the host path is also listed. This'll ensure that proc macro
16781692
// dependencies are correctly found (for reexported macros).
16791693
if !unit.kind.is_host() {
16801694
cmd.arg("-L").arg(&{
16811695
let mut deps = OsString::from("dependency=");
1682-
deps.push(build_runner.files().host_deps());
1696+
deps.push(build_runner.files().host_deps(unit));
16831697
deps
16841698
});
16851699
}
@@ -1755,6 +1769,21 @@ fn build_deps_args(
17551769
Ok(())
17561770
}
17571771

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+
map.insert(&unit, build_runner.files().deps_dir(&unit));
1778+
1779+
for dep in build_runner.unit_deps(unit) {
1780+
if map.contains_key(&dep.unit) {
1781+
continue;
1782+
}
1783+
add_dep_arg(map, build_runner, &dep.unit);
1784+
}
1785+
}
1786+
17581787
/// Adds extra rustc flags and environment variables collected from the output
17591788
/// of a build-script to the command to execute, include custom environment
17601789
/// variables and `cfg`.

0 commit comments

Comments
 (0)