Skip to content

Commit df7dbd6

Browse files
committed
refactor(layout): Split Layout internally into BuildDirLayout and ArtifactDirLayout
1 parent d89d1de commit df7dbd6

File tree

1 file changed

+75
-61
lines changed

1 file changed

+75
-61
lines changed

src/cargo/core/compiler/layout.rs

Lines changed: 75 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,48 @@ use std::path::{Path, PathBuf};
111111
///
112112
/// See module docs for more information.
113113
pub struct Layout {
114-
/// The root directory: `/path/to/target`.
115-
/// If cross compiling: `/path/to/target/$TRIPLE`.
116-
root: PathBuf,
117-
/// The final artifact destination: `$root/debug` (or `release`).
114+
artifact_dir: ArtifactDirLayout,
115+
build_dir: BuildDirLayout,
116+
}
117+
118+
pub struct ArtifactDirLayout {
119+
/// The final artifact destination: `<artifact-dir>/debug` (or `release`).
118120
dest: PathBuf,
119-
/// The directory with rustc artifacts: `$dest/deps`
121+
/// The directory for examples
122+
examples: PathBuf,
123+
/// The directory for rustdoc output
124+
doc: PathBuf,
125+
/// The directory for --timings output
126+
timings: PathBuf,
127+
/// The lockfile for a build (`.cargo-lock`). Will be unlocked when this
128+
/// struct is `drop`ped.
129+
_lock: FileLock,
130+
}
131+
132+
pub struct BuildDirLayout {
133+
/// The root directory: `/path/to/build-dir`.
134+
/// If cross compiling: `/path/to/build-dir/$TRIPLE`.
135+
root: PathBuf,
136+
/// The directory with rustc artifacts
120137
deps: PathBuf,
121-
/// The directory for build scripts: `$dest/build`
138+
/// The primary directory for build files
122139
build: PathBuf,
123-
/// The directory for artifacts, i.e. binaries, cdylibs, staticlibs: `$dest/deps/artifact`
140+
/// The directory for artifacts, i.e. binaries, cdylibs, staticlibs
124141
artifact: PathBuf,
125-
/// The directory for incremental files: `$dest/incremental`
142+
/// The directory for incremental files
126143
incremental: PathBuf,
127-
/// The directory for fingerprints: `$dest/.fingerprint`
144+
/// The directory for fingerprints
128145
fingerprint: PathBuf,
129-
/// The directory for examples: `$dest/examples`
146+
/// The directory for pre-uplifted examples: `build-dir/debug/examples`
130147
examples: PathBuf,
131-
/// The directory for pre-uplifted examples: `$build-dir/debug/examples`
132-
build_examples: PathBuf,
133-
/// The directory for rustdoc output: `$root/doc`
134-
doc: PathBuf,
135-
/// The directory for --timings output
136-
timings: PathBuf,
137-
/// The directory for temporary data of integration tests and benches: `$dest/tmp`
148+
/// The directory for temporary data of integration tests and benches
138149
tmp: PathBuf,
139150
/// The lockfile for a build (`.cargo-lock`). Will be unlocked when this
140151
/// struct is `drop`ped.
141-
_lock: FileLock,
142-
/// Same as `_lock` but for the build directory.
143152
///
144153
/// Will be `None` when the build-dir and target-dir are the same path as we cannot
145154
/// lock the same path twice.
146-
_build_lock: Option<FileLock>,
155+
_lock: Option<FileLock>,
147156
is_new_layout: bool,
148157
}
149158

@@ -184,9 +193,10 @@ impl Layout {
184193
// For now we don't do any more finer-grained locking on the artifact
185194
// directory, so just lock the entire thing for the duration of this
186195
// compile.
187-
let lock = dest.open_rw_exclusive_create(".cargo-lock", ws.gctx(), "build directory")?;
196+
let artifact_dir_lock =
197+
dest.open_rw_exclusive_create(".cargo-lock", ws.gctx(), "build directory")?;
188198

189-
let build_lock = if root != build_root {
199+
let build_dir_lock = if root != build_root {
190200
Some(build_dest.open_rw_exclusive_create(
191201
".cargo-lock",
192202
ws.gctx(),
@@ -203,121 +213,125 @@ impl Layout {
203213
let artifact = deps.join("artifact");
204214

205215
Ok(Layout {
206-
deps,
207-
build: build_dest.join("build"),
208-
artifact,
209-
incremental: build_dest.join("incremental"),
210-
fingerprint: build_dest.join(".fingerprint"),
211-
examples: dest.join("examples"),
212-
build_examples: build_dest.join("examples"),
213-
doc: root.join("doc"),
214-
timings: root.join("cargo-timings"),
215-
tmp: build_root.join("tmp"),
216-
root,
217-
dest,
218-
_lock: lock,
219-
_build_lock: build_lock,
220-
is_new_layout,
216+
artifact_dir: ArtifactDirLayout {
217+
dest: dest.clone(),
218+
examples: dest.join("examples"),
219+
doc: root.join("doc"),
220+
timings: root.join("cargo-timings"),
221+
_lock: artifact_dir_lock,
222+
},
223+
build_dir: BuildDirLayout {
224+
root: build_root.clone(),
225+
deps,
226+
build: build_dest.join("build"),
227+
artifact,
228+
incremental: build_dest.join("incremental"),
229+
fingerprint: build_dest.join(".fingerprint"),
230+
examples: build_dest.join("examples"),
231+
tmp: build_root.join("tmp"),
232+
_lock: build_dir_lock,
233+
is_new_layout,
234+
},
221235
})
222236
}
223237

224238
/// Makes sure all directories stored in the Layout exist on the filesystem.
225239
pub fn prepare(&mut self) -> CargoResult<()> {
226-
if !self.is_new_layout {
227-
paths::create_dir_all(&self.deps)?;
228-
paths::create_dir_all(&self.fingerprint)?;
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)?;
229243
}
230-
paths::create_dir_all(&self.incremental)?;
231-
paths::create_dir_all(&self.examples)?;
232-
paths::create_dir_all(&self.build_examples)?;
233-
paths::create_dir_all(&self.build)?;
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)?;
234248

235249
Ok(())
236250
}
237251

238252
/// Fetch the destination path for final artifacts (`/…/target/debug`).
239253
pub fn dest(&self) -> &Path {
240-
&self.dest
254+
&self.artifact_dir.dest
241255
}
242256
/// Fetch the deps path.
243257
pub fn deps(&self, pkg_dir: &str) -> PathBuf {
244-
if self.is_new_layout {
258+
if self.build_dir.is_new_layout {
245259
self.build_unit(pkg_dir).join("deps")
246260
} else {
247261
self.legacy_deps().to_path_buf()
248262
}
249263
}
250264
/// Fetch the deps path. (old layout)
251265
pub fn legacy_deps(&self) -> &Path {
252-
&self.deps
266+
&self.build_dir.deps
253267
}
254268
/// Fetch the examples path.
255269
pub fn examples(&self) -> &Path {
256-
&self.examples
270+
&self.artifact_dir.examples
257271
}
258272
/// Fetch the build examples path.
259273
pub fn build_examples(&self) -> &Path {
260-
&self.build_examples
274+
&self.build_dir.examples
261275
}
262276
/// Fetch the doc path.
263277
pub fn doc(&self) -> &Path {
264-
&self.doc
278+
&self.artifact_dir.doc
265279
}
266280
/// Fetch the root path (`/…/target`).
267281
pub fn root(&self) -> &Path {
268-
&self.root
282+
&self.build_dir.root
269283
}
270284
/// Fetch the incremental path.
271285
pub fn incremental(&self) -> &Path {
272-
&self.incremental
286+
&self.build_dir.incremental
273287
}
274288
/// Fetch the timings path.
275289
pub fn timings(&self) -> &Path {
276-
&self.timings
290+
&self.artifact_dir.timings
277291
}
278292
/// Fetch the fingerprint path.
279293
pub fn fingerprint(&self, pkg_dir: &str) -> PathBuf {
280-
if self.is_new_layout {
294+
if self.build_dir.is_new_layout {
281295
self.build_unit(pkg_dir).join("fingerprint")
282296
} else {
283297
self.legacy_fingerprint().to_path_buf().join(pkg_dir)
284298
}
285299
}
286300
/// Fetch the fingerprint path. (old layout)
287301
pub fn legacy_fingerprint(&self) -> &Path {
288-
&self.fingerprint
302+
&self.build_dir.fingerprint
289303
}
290304
/// Fetch the build path.
291305
pub fn build(&self) -> &Path {
292-
&self.build
306+
&self.build_dir.build
293307
}
294308
/// Fetch the build script path.
295309
pub fn build_script(&self, pkg_dir: &str) -> PathBuf {
296-
if self.is_new_layout {
310+
if self.build_dir.is_new_layout {
297311
self.build_unit(pkg_dir).join("build-script")
298312
} else {
299313
self.build().join(pkg_dir)
300314
}
301315
}
302316
/// Fetch the build script execution path.
303317
pub fn build_script_execution(&self, pkg_dir: &str) -> PathBuf {
304-
if self.is_new_layout {
318+
if self.build_dir.is_new_layout {
305319
self.build_unit(pkg_dir).join("build-script-execution")
306320
} else {
307321
self.build().join(pkg_dir)
308322
}
309323
}
310324
/// Fetch the artifact path.
311325
pub fn artifact(&self) -> &Path {
312-
&self.artifact
326+
&self.build_dir.artifact
313327
}
314328
/// Fetch the build unit path
315329
pub fn build_unit(&self, pkg_dir: &str) -> PathBuf {
316330
self.build().join(pkg_dir)
317331
}
318332
/// Create and return the tmp path.
319333
pub fn prepare_tmp(&self) -> CargoResult<&Path> {
320-
paths::create_dir_all(&self.tmp)?;
321-
Ok(&self.tmp)
334+
paths::create_dir_all(&self.build_dir.tmp)?;
335+
Ok(&self.build_dir.tmp)
322336
}
323337
}

0 commit comments

Comments
 (0)