Skip to content

Commit ce76237

Browse files
committed
refactor(layout): Moved Layout accessors to BuildDirLayout and ArtifactDirLayout
1 parent df7dbd6 commit ce76237

File tree

7 files changed

+114
-69
lines changed

7 files changed

+114
-69
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ impl RustDocFingerprint {
11411141

11421142
let fingerprint_path = build_runner
11431143
.files()
1144-
.host_root()
1144+
.host_build_root()
11451145
.join(".rustdoc_fingerprint.json");
11461146
let write_fingerprint = || -> CargoResult<()> {
11471147
paths::write(
@@ -1181,7 +1181,7 @@ impl RustDocFingerprint {
11811181
.bcx
11821182
.all_kinds
11831183
.iter()
1184-
.map(|kind| build_runner.files().layout(*kind).doc())
1184+
.map(|kind| build_runner.files().layout(*kind).artifact_dir().doc())
11851185
.filter(|path| path.exists())
11861186
.try_for_each(|path| clean_doc(path))?;
11871187
write_fingerprint()?;

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,13 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
211211
// Docscrape units need to have doc/ set as the out_dir so sources for reverse-dependencies
212212
// will be put into doc/ and not into deps/ where the *.examples files are stored.
213213
if unit.mode.is_doc() || unit.mode.is_doc_scrape() {
214-
self.layout(unit.kind).doc().to_path_buf()
214+
self.layout(unit.kind).artifact_dir().doc().to_path_buf()
215215
} else if unit.mode.is_doc_test() {
216216
panic!("doc tests do not have an out dir");
217217
} else if unit.target.is_custom_build() {
218218
self.build_script_dir(unit)
219219
} else if unit.target.is_example() {
220-
self.layout(unit.kind).build_examples().to_path_buf()
220+
self.layout(unit.kind).build_dir().examples().to_path_buf()
221221
} else if unit.artifact.is_true() {
222222
self.artifact_dir(unit)
223223
} else {
@@ -250,41 +250,41 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
250250

251251
/// Returns the final artifact path for the host (`/…/target/debug`)
252252
pub fn host_dest(&self) -> &Path {
253-
self.host.dest()
253+
self.host.artifact_dir().dest()
254254
}
255255

256-
/// Returns the root of the build output tree for the host (`/…/target`)
257-
pub fn host_root(&self) -> &Path {
258-
self.host.root()
256+
/// Returns the root of the build output tree for the host (`/…/build-dir`)
257+
pub fn host_build_root(&self) -> &Path {
258+
self.host.build_dir().root()
259259
}
260260

261261
/// Returns the host `deps` directory path.
262262
pub fn host_deps(&self, unit: &Unit) -> PathBuf {
263263
let dir = self.pkg_dir(unit);
264-
self.host.deps(&dir)
264+
self.host.build_dir().deps(&dir)
265265
}
266266

267267
/// Returns the directories where Rust crate dependencies are found for the
268268
/// specified unit.
269269
pub fn deps_dir(&self, unit: &Unit) -> PathBuf {
270270
let dir = self.pkg_dir(unit);
271-
self.layout(unit.kind).deps(&dir)
271+
self.layout(unit.kind).build_dir().deps(&dir)
272272
}
273273

274274
/// Directory where the fingerprint for the given unit should go.
275275
pub fn fingerprint_dir(&self, unit: &Unit) -> PathBuf {
276276
let dir = self.pkg_dir(unit);
277-
self.layout(unit.kind).fingerprint(&dir)
277+
self.layout(unit.kind).build_dir().fingerprint(&dir)
278278
}
279279

280280
/// Directory where incremental output for the given unit should go.
281281
pub fn incremental_dir(&self, unit: &Unit) -> &Path {
282-
self.layout(unit.kind).incremental()
282+
self.layout(unit.kind).build_dir().incremental()
283283
}
284284

285285
/// Directory where timing output should go.
286286
pub fn timings_dir(&self) -> &Path {
287-
self.host.timings()
287+
self.host.artifact_dir().timings()
288288
}
289289

290290
/// Returns the path for a file in the fingerprint directory.
@@ -319,7 +319,9 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
319319
assert!(!unit.mode.is_run_custom_build());
320320
assert!(self.metas.contains_key(unit));
321321
let dir = self.pkg_dir(unit);
322-
self.layout(CompileKind::Host).build_script(&dir)
322+
self.layout(CompileKind::Host)
323+
.build_dir()
324+
.build_script(&dir)
323325
}
324326

325327
/// Returns the directory for compiled artifacts files.
@@ -343,7 +345,11 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
343345
invalid
344346
),
345347
};
346-
self.layout(unit.kind).artifact().join(dir).join(kind)
348+
self.layout(unit.kind)
349+
.build_dir()
350+
.artifact()
351+
.join(dir)
352+
.join(kind)
347353
}
348354

349355
/// Returns the directory where information about running a build script
@@ -353,7 +359,9 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
353359
assert!(unit.target.is_custom_build());
354360
assert!(unit.mode.is_run_custom_build());
355361
let dir = self.pkg_dir(unit);
356-
self.layout(unit.kind).build_script_execution(&dir)
362+
self.layout(unit.kind)
363+
.build_dir()
364+
.build_script_execution(&dir)
357365
}
358366

359367
/// Returns the "`OUT_DIR`" directory for running a build script.
@@ -372,7 +380,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
372380
bcx: &BuildContext<'_, '_>,
373381
) -> CargoResult<PathBuf> {
374382
assert!(target.is_bin());
375-
let dest = self.layout(kind).dest();
383+
let dest = self.layout(kind).artifact_dir().dest();
376384
let info = bcx.target_data.info(kind);
377385
let (file_types, _) = info
378386
.rustc_outputs(
@@ -440,11 +448,14 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
440448
let filename = file_type.uplift_filename(&unit.target);
441449
let uplift_path = if unit.target.is_example() {
442450
// Examples live in their own little world.
443-
self.layout(unit.kind).examples().join(filename)
451+
self.layout(unit.kind)
452+
.artifact_dir()
453+
.examples()
454+
.join(filename)
444455
} else if unit.target.is_custom_build() {
445456
self.build_script_dir(unit).join(filename)
446457
} else {
447-
self.layout(unit.kind).dest().join(filename)
458+
self.layout(unit.kind).artifact_dir().dest().join(filename)
448459
};
449460
if from_path == uplift_path {
450461
// This can happen with things like examples that reside in the

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
401401
let layout = files.layout(kind);
402402
self.compilation
403403
.root_output
404-
.insert(kind, layout.dest().to_path_buf());
404+
.insert(kind, layout.artifact_dir().dest().to_path_buf());
405405
if self.bcx.gctx.cli_unstable().build_dir_new_layout {
406406
for (unit, _) in self.bcx.unit_graph.iter() {
407407
let dep_dir = self.files().deps_dir(unit);
@@ -411,7 +411,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
411411
} else {
412412
self.compilation
413413
.deps_output
414-
.insert(kind, layout.legacy_deps().to_path_buf());
414+
.insert(kind, layout.build_dir().legacy_deps().to_path_buf());
415415
}
416416
}
417417
Ok(())

src/cargo/core/compiler/layout.rs

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -237,101 +237,125 @@ impl Layout {
237237

238238
/// Makes sure all directories stored in the Layout exist on the filesystem.
239239
pub fn prepare(&mut self) -> CargoResult<()> {
240-
if !self.build_dir.is_new_layout {
241-
paths::create_dir_all(&self.build_dir.deps)?;
242-
paths::create_dir_all(&self.build_dir.fingerprint)?;
243-
}
244-
paths::create_dir_all(&self.build_dir.incremental)?;
245-
paths::create_dir_all(&self.artifact_dir.examples)?;
246-
paths::create_dir_all(&self.build_dir.examples)?;
247-
paths::create_dir_all(&self.build_dir.build)?;
240+
self.artifact_dir.prepare()?;
241+
self.build_dir.prepare()?;
248242

249243
Ok(())
250244
}
251245

246+
pub fn artifact_dir(&self) -> &ArtifactDirLayout {
247+
&self.artifact_dir
248+
}
249+
250+
pub fn build_dir(&self) -> &BuildDirLayout {
251+
&self.build_dir
252+
}
253+
}
254+
255+
impl ArtifactDirLayout {
256+
/// Makes sure all directories stored in the Layout exist on the filesystem.
257+
pub fn prepare(&mut self) -> CargoResult<()> {
258+
paths::create_dir_all(&self.examples)?;
259+
260+
Ok(())
261+
}
252262
/// Fetch the destination path for final artifacts (`/…/target/debug`).
253263
pub fn dest(&self) -> &Path {
254-
&self.artifact_dir.dest
264+
&self.dest
265+
}
266+
/// Fetch the examples path.
267+
pub fn examples(&self) -> &Path {
268+
&self.examples
269+
}
270+
/// Fetch the doc path.
271+
pub fn doc(&self) -> &Path {
272+
&self.doc
273+
}
274+
/// Fetch the cargo-timings path.
275+
pub fn timings(&self) -> &Path {
276+
&self.timings
277+
}
278+
}
279+
280+
impl BuildDirLayout {
281+
/// Makes sure all directories stored in the Layout exist on the filesystem.
282+
pub fn prepare(&mut self) -> CargoResult<()> {
283+
if !self.is_new_layout {
284+
paths::create_dir_all(&self.deps)?;
285+
paths::create_dir_all(&self.fingerprint)?;
286+
}
287+
paths::create_dir_all(&self.incremental)?;
288+
paths::create_dir_all(&self.examples)?;
289+
paths::create_dir_all(&self.build)?;
290+
291+
Ok(())
255292
}
256293
/// Fetch the deps path.
257294
pub fn deps(&self, pkg_dir: &str) -> PathBuf {
258-
if self.build_dir.is_new_layout {
295+
if self.is_new_layout {
259296
self.build_unit(pkg_dir).join("deps")
260297
} else {
261298
self.legacy_deps().to_path_buf()
262299
}
263300
}
264301
/// Fetch the deps path. (old layout)
265302
pub fn legacy_deps(&self) -> &Path {
266-
&self.build_dir.deps
303+
&self.deps
267304
}
268-
/// Fetch the examples path.
269-
pub fn examples(&self) -> &Path {
270-
&self.artifact_dir.examples
305+
pub fn root(&self) -> &Path {
306+
&self.root
271307
}
272308
/// Fetch the build examples path.
273-
pub fn build_examples(&self) -> &Path {
274-
&self.build_dir.examples
275-
}
276-
/// Fetch the doc path.
277-
pub fn doc(&self) -> &Path {
278-
&self.artifact_dir.doc
279-
}
280-
/// Fetch the root path (`/…/target`).
281-
pub fn root(&self) -> &Path {
282-
&self.build_dir.root
309+
pub fn examples(&self) -> &Path {
310+
&self.examples
283311
}
284312
/// Fetch the incremental path.
285313
pub fn incremental(&self) -> &Path {
286-
&self.build_dir.incremental
287-
}
288-
/// Fetch the timings path.
289-
pub fn timings(&self) -> &Path {
290-
&self.artifact_dir.timings
314+
&self.incremental
291315
}
292316
/// Fetch the fingerprint path.
293317
pub fn fingerprint(&self, pkg_dir: &str) -> PathBuf {
294-
if self.build_dir.is_new_layout {
318+
if self.is_new_layout {
295319
self.build_unit(pkg_dir).join("fingerprint")
296320
} else {
297321
self.legacy_fingerprint().to_path_buf().join(pkg_dir)
298322
}
299323
}
300324
/// Fetch the fingerprint path. (old layout)
301325
pub fn legacy_fingerprint(&self) -> &Path {
302-
&self.build_dir.fingerprint
326+
&self.fingerprint
303327
}
304328
/// Fetch the build path.
305329
pub fn build(&self) -> &Path {
306-
&self.build_dir.build
330+
&self.build
307331
}
308332
/// Fetch the build script path.
309333
pub fn build_script(&self, pkg_dir: &str) -> PathBuf {
310-
if self.build_dir.is_new_layout {
334+
if self.is_new_layout {
311335
self.build_unit(pkg_dir).join("build-script")
312336
} else {
313337
self.build().join(pkg_dir)
314338
}
315339
}
316340
/// Fetch the build script execution path.
317341
pub fn build_script_execution(&self, pkg_dir: &str) -> PathBuf {
318-
if self.build_dir.is_new_layout {
342+
if self.is_new_layout {
319343
self.build_unit(pkg_dir).join("build-script-execution")
320344
} else {
321345
self.build().join(pkg_dir)
322346
}
323347
}
324348
/// Fetch the artifact path.
325349
pub fn artifact(&self) -> &Path {
326-
&self.build_dir.artifact
350+
&self.artifact
327351
}
328352
/// Fetch the build unit path
329353
pub fn build_unit(&self, pkg_dir: &str) -> PathBuf {
330354
self.build().join(pkg_dir)
331355
}
332356
/// Create and return the tmp path.
333357
pub fn prepare_tmp(&self) -> CargoResult<&Path> {
334-
paths::create_dir_all(&self.build_dir.tmp)?;
335-
Ok(&self.build_dir.tmp)
358+
paths::create_dir_all(&self.tmp)?;
359+
Ok(&self.tmp)
336360
}
337361
}

src/cargo/core/compiler/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,11 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
781781
}
782782

783783
if unit.target.is_test() || unit.target.is_bench() {
784-
let tmp = build_runner.files().layout(unit.kind).prepare_tmp()?;
784+
let tmp = build_runner
785+
.files()
786+
.layout(unit.kind)
787+
.build_dir()
788+
.prepare_tmp()?;
785789
base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());
786790
}
787791

src/cargo/core/compiler/output_depinfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn add_deps_for_unit(
5858
let dep_info_loc = fingerprint::dep_info_loc(build_runner, unit);
5959
if let Some(paths) = fingerprint::parse_dep_info(
6060
unit.pkg.root(),
61-
build_runner.files().host_root(),
61+
build_runner.files().host_build_root(),
6262
&dep_info_loc,
6363
)? {
6464
for path in paths.files.into_keys() {

0 commit comments

Comments
 (0)