Skip to content

Commit e6cf910

Browse files
committed
refactor(layout): made artifact_dir optional in Layout
1 parent a1d20db commit e6cf910

File tree

9 files changed

+82
-51
lines changed

9 files changed

+82
-51
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,14 @@ impl RustDocFingerprint {
11811181
.bcx
11821182
.all_kinds
11831183
.iter()
1184-
.map(|kind| build_runner.files().layout(*kind).artifact_dir().doc())
1184+
.map(|kind| {
1185+
build_runner
1186+
.files()
1187+
.layout(*kind)
1188+
.artifact_dir()
1189+
.expect("artifact-dir was not locked")
1190+
.doc()
1191+
})
11851192
.filter(|path| path.exists())
11861193
.try_for_each(|path| clean_doc(path))?;
11871194
write_fingerprint()?;

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ 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).artifact_dir().doc().to_path_buf()
214+
self.layout(unit.kind)
215+
.artifact_dir()
216+
.expect("artifact-dir was not locked")
217+
.doc()
218+
.to_path_buf()
215219
} else if unit.mode.is_doc_test() {
216220
panic!("doc tests do not have an out dir");
217221
} else if unit.target.is_custom_build() {
@@ -249,8 +253,8 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
249253
}
250254

251255
/// Returns the final artifact path for the host (`/…/target/debug`)
252-
pub fn host_dest(&self) -> &Path {
253-
self.host.artifact_dir().dest()
256+
pub fn host_dest(&self) -> Option<&Path> {
257+
self.host.artifact_dir().map(|v| v.dest())
254258
}
255259

256260
/// Returns the root of the build output tree for the host (`/…/build-dir`)
@@ -283,8 +287,8 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
283287
}
284288

285289
/// Directory where timing output should go.
286-
pub fn timings_dir(&self) -> &Path {
287-
self.host.artifact_dir().timings()
290+
pub fn timings_dir(&self) -> Option<&Path> {
291+
self.host.artifact_dir().map(|v| v.timings())
288292
}
289293

290294
/// Returns the path for a file in the fingerprint directory.
@@ -378,9 +382,11 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
378382
target: &Target,
379383
kind: CompileKind,
380384
bcx: &BuildContext<'_, '_>,
381-
) -> CargoResult<PathBuf> {
385+
) -> CargoResult<Option<PathBuf>> {
382386
assert!(target.is_bin());
383-
let dest = self.layout(kind).artifact_dir().dest();
387+
let Some(dest) = self.layout(kind).artifact_dir().map(|v| v.dest()) else {
388+
return Ok(None);
389+
};
384390
let info = bcx.target_data.info(kind);
385391
let (file_types, _) = info
386392
.rustc_outputs(
@@ -396,7 +402,7 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
396402
.find(|file_type| file_type.flavor == FileFlavor::Normal)
397403
.expect("target must support `bin`");
398404

399-
Ok(dest.join(file_type.uplift_filename(target)))
405+
Ok(Some(dest.join(file_type.uplift_filename(target))))
400406
}
401407

402408
/// Returns the filenames that the given unit will generate.
@@ -450,12 +456,17 @@ impl<'a, 'gctx: 'a> CompilationFiles<'a, 'gctx> {
450456
// Examples live in their own little world.
451457
self.layout(unit.kind)
452458
.artifact_dir()
459+
.expect("artifact-dir was not locked")
453460
.examples()
454461
.join(filename)
455462
} else if unit.target.is_custom_build() {
456463
self.build_script_dir(unit).join(filename)
457464
} else {
458-
self.layout(unit.kind).artifact_dir().dest().join(filename)
465+
self.layout(unit.kind)
466+
.artifact_dir()
467+
.expect("artifact-dir was not locked")
468+
.dest()
469+
.join(filename)
459470
};
460471
if from_path == uplift_path {
461472
// This can happen with things like examples that reside in the

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::core::compiler::{self, Unit, artifact};
1010
use crate::util::cache_lock::CacheLockMode;
1111
use crate::util::errors::CargoResult;
1212
use annotate_snippets::{Level, Message};
13-
use anyhow::{Context as _, bail};
13+
use anyhow::{Context, bail};
1414
use cargo_util::paths;
1515
use filetime::FileTime;
1616
use itertools::Itertools;
@@ -392,9 +392,11 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
392392
let files = self.files.as_ref().unwrap();
393393
for &kind in self.bcx.all_kinds.iter() {
394394
let layout = files.layout(kind);
395-
self.compilation
396-
.root_output
397-
.insert(kind, layout.artifact_dir().dest().to_path_buf());
395+
if let Some(artifact_dir) = layout.artifact_dir() {
396+
self.compilation
397+
.root_output
398+
.insert(kind, artifact_dir.dest().to_path_buf());
399+
}
398400
if self.bcx.gctx.cli_unstable().build_dir_new_layout {
399401
for (unit, _) in self.bcx.unit_graph.iter() {
400402
let dep_dir = self.files().deps_dir(unit);

src/cargo/core/compiler/compilation.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,14 @@ impl<'gctx> Compilation<'gctx> {
305305
}
306306
search_path.push(self.deps_output[&CompileKind::Host].clone());
307307
} else {
308-
search_path.extend(super::filter_dynamic_search_path(
309-
self.native_dirs.iter(),
310-
&self.root_output[&kind],
311-
));
308+
if let Some(path) = self.root_output.get(&kind) {
309+
search_path.extend(super::filter_dynamic_search_path(
310+
self.native_dirs.iter(),
311+
path,
312+
));
313+
search_path.push(path.clone());
314+
}
312315
search_path.push(self.deps_output[&kind].clone());
313-
search_path.push(self.root_output[&kind].clone());
314316
// For build-std, we don't want to accidentally pull in any shared
315317
// libs from the sysroot that ships with rustc. This may not be
316318
// required (at least I cannot craft a situation where it

src/cargo/core/compiler/custom_build.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
473473
let output_file = script_run_dir.join("output");
474474
let err_file = script_run_dir.join("stderr");
475475
let root_output_file = script_run_dir.join("root-output");
476-
let host_target_root = build_runner.files().host_dest().to_path_buf();
476+
let host_target_root = build_runner.files().host_dest().map(|v| v.to_path_buf());
477477
let all = (
478478
id,
479479
library_name.clone(),
@@ -541,12 +541,14 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
541541
);
542542
}
543543
}
544-
if let Some(build_scripts) = build_scripts {
544+
if let Some(build_scripts) = build_scripts
545+
&& let Some(ref host_target_root) = host_target_root
546+
{
545547
super::add_plugin_deps(
546548
&mut cmd,
547549
&build_script_outputs,
548550
&build_scripts,
549-
&host_target_root,
551+
host_target_root,
550552
)?;
551553
}
552554
}

src/cargo/core/compiler/layout.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ use std::path::{Path, PathBuf};
112112
///
113113
/// See module docs for more information.
114114
pub struct Layout {
115-
artifact_dir: ArtifactDirLayout,
115+
artifact_dir: Option<ArtifactDirLayout>,
116116
build_dir: BuildDirLayout,
117117
}
118118

@@ -177,13 +177,13 @@ impl Layout {
177177
let artifact = deps.join("artifact");
178178

179179
Ok(Layout {
180-
artifact_dir: ArtifactDirLayout {
180+
artifact_dir: Some(ArtifactDirLayout {
181181
dest: dest.clone(),
182182
examples: dest.join("examples"),
183183
doc: root.join("doc"),
184184
timings: root.join("cargo-timings"),
185185
_lock: artifact_dir_lock,
186-
},
186+
}),
187187
build_dir: BuildDirLayout {
188188
root: build_root.clone(),
189189
deps,
@@ -201,14 +201,16 @@ impl Layout {
201201

202202
/// Makes sure all directories stored in the Layout exist on the filesystem.
203203
pub fn prepare(&mut self) -> CargoResult<()> {
204-
self.artifact_dir.prepare()?;
204+
if let Some(ref mut artifact_dir) = self.artifact_dir {
205+
artifact_dir.prepare()?;
206+
}
205207
self.build_dir.prepare()?;
206208

207209
Ok(())
208210
}
209211

210-
pub fn artifact_dir(&self) -> &ArtifactDirLayout {
211-
&self.artifact_dir
212+
pub fn artifact_dir(&self) -> Option<&ArtifactDirLayout> {
213+
self.artifact_dir.as_ref()
212214
}
213215

214216
pub fn build_dir(&self) -> &BuildDirLayout {

src/cargo/core/compiler/mod.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn rustc(
298298
exec.init(build_runner, unit);
299299
let exec = exec.clone();
300300

301-
let root_output = build_runner.files().host_dest().to_path_buf();
301+
let root_output = build_runner.files().host_dest().map(|v| v.to_path_buf());
302302
let build_dir = build_runner.bcx.ws.build_dir().into_path_unlocked();
303303
let pkg_root = unit.pkg.root().to_path_buf();
304304
let cwd = rustc
@@ -361,7 +361,9 @@ fn rustc(
361361
current_id,
362362
mode,
363363
)?;
364-
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, &root_output)?;
364+
if let Some(ref root_output) = root_output {
365+
add_plugin_deps(&mut rustc, &script_outputs, &build_scripts, root_output)?;
366+
}
365367
add_custom_flags(&mut rustc, &script_outputs, script_metadatas)?;
366368
}
367369

@@ -1399,16 +1401,17 @@ fn build_base_args(
13991401
.iter()
14001402
.filter(|target| target.is_bin())
14011403
{
1402-
let exe_path = build_runner.files().bin_link_for_target(
1403-
bin_target,
1404-
unit.kind,
1405-
build_runner.bcx,
1406-
)?;
1407-
let name = bin_target
1408-
.binary_filename()
1409-
.unwrap_or(bin_target.name().to_string());
1410-
let key = format!("CARGO_BIN_EXE_{}", name);
1411-
cmd.env(&key, exe_path);
1404+
if let Some(exe_path) =
1405+
build_runner
1406+
.files()
1407+
.bin_link_for_target(bin_target, unit.kind, build_runner.bcx)?
1408+
{
1409+
let name = bin_target
1410+
.binary_filename()
1411+
.unwrap_or(bin_target.name().to_string());
1412+
let key = format!("CARGO_BIN_EXE_{}", name);
1413+
cmd.env(&key, exe_path);
1414+
}
14121415
}
14131416
}
14141417
Ok(())

src/cargo/core/compiler/timings.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::util::log_message::LogMessage;
1111
use crate::util::machine_message::{self, Message};
1212
use crate::util::style;
1313
use crate::util::{CargoResult, GlobalContext};
14-
use anyhow::Context as _;
14+
use anyhow::Context;
1515
use cargo_util::paths;
1616
use indexmap::IndexMap;
1717
use itertools::Itertools;
@@ -457,7 +457,10 @@ impl<'gctx> Timings<'gctx> {
457457
) -> CargoResult<()> {
458458
let duration = self.start.elapsed().as_secs_f64();
459459
let timestamp = self.start_str.replace(&['-', ':'][..], "");
460-
let timings_path = build_runner.files().timings_dir();
460+
let timings_path = build_runner
461+
.files()
462+
.timings_dir()
463+
.expect("artifact-dir was not locked");
461464
paths::create_dir_all(&timings_path)?;
462465
let filename = timings_path.join(format!("cargo-timing-{}.html", timestamp));
463466
let mut f = BufWriter::new(paths::create(&filename)?);

src/cargo/ops/cargo_clean.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::util::edit_distance;
77
use crate::util::errors::CargoResult;
88
use crate::util::interning::InternedString;
99
use crate::util::{GlobalContext, Progress, ProgressStyle};
10-
use anyhow::bail;
10+
use anyhow::{Context, bail};
1111
use cargo_util::paths;
1212
use std::collections::{HashMap, HashSet};
1313
use std::fs;
@@ -236,19 +236,18 @@ fn clean_specs(
236236
let (file_types, _unsupported) = target_data
237237
.info(*compile_kind)
238238
.rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;
239+
let artifact_dir = layout
240+
.artifact_dir()
241+
.context("artifact-dir was not locked during clean")?;
239242
let (dir, uplift_dir) = match target.kind() {
240-
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => (
241-
layout.build_dir().examples(),
242-
Some(layout.artifact_dir().examples()),
243-
),
243+
TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {
244+
(layout.build_dir().examples(), Some(artifact_dir.examples()))
245+
}
244246
// Tests/benchmarks are never uplifted.
245247
TargetKind::Test | TargetKind::Bench => {
246248
(layout.build_dir().legacy_deps(), None)
247249
}
248-
_ => (
249-
layout.build_dir().legacy_deps(),
250-
Some(layout.artifact_dir().dest()),
251-
),
250+
_ => (layout.build_dir().legacy_deps(), Some(artifact_dir.dest())),
252251
};
253252
let mut dir_glob_str = escape_glob_path(dir)?;
254253
let dir_glob = Path::new(&dir_glob_str);

0 commit comments

Comments
 (0)