Skip to content

Commit 16f8cc3

Browse files
committed
Layout docs and cleanup.
1 parent 275de19 commit 16f8cc3

File tree

6 files changed

+125
-75
lines changed

6 files changed

+125
-75
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ pub struct FileType {
5353
/// The kind of file.
5454
pub flavor: FileFlavor,
5555
/// The suffix for the file (for example, `.rlib`).
56+
/// This is an empty string for executables on Unix-like platforms.
5657
suffix: String,
5758
/// The prefix for the file (for example, `lib`).
59+
/// This is an empty string for things like executables.
5860
prefix: String,
59-
// Wasm bin target will generate two files in deps such as
60-
// "web-stuff.js" and "web_stuff.wasm". Note the different usages of
61-
// "-" and "_". should_replace_hyphens is a flag to indicate that
62-
// we need to convert the stem "web-stuff" to "web_stuff", so we
63-
// won't miss "web_stuff.wasm".
61+
/// Flag to convert hyphen to underscore.
62+
///
63+
/// wasm bin targets will generate two files in deps such as
64+
/// "web-stuff.js" and "web_stuff.wasm". Note the different usages of "-"
65+
/// and "_". This flag indicates that the stem "web-stuff" should be
66+
/// converted to "web_stuff".
6467
should_replace_hyphens: bool,
6568
}
6669

src/cargo/core/compiler/compilation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct Doctest {
2222
/// A structure returning the result of a compilation.
2323
pub struct Compilation<'cfg> {
2424
/// An array of all tests created during this compilation.
25+
/// `(package, target, path_to_test_exe)`
2526
pub tests: Vec<(Package, Target, PathBuf)>,
2627

2728
/// An array of all binaries created.

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
145145
/// target.
146146
pub fn out_dir(&self, unit: &Unit<'a>) -> PathBuf {
147147
if unit.mode.is_doc() {
148-
self.layout(unit.kind).root().parent().unwrap().join("doc")
148+
self.layout(unit.kind).doc().to_path_buf()
149149
} else if unit.mode.is_doc_test() {
150150
panic!("doc tests do not have an out dir");
151151
} else if unit.target.is_custom_build() {
@@ -169,11 +169,6 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
169169
}
170170
}
171171

172-
/// Returns the root of the build output tree for the target
173-
pub fn target_root(&self) -> &Path {
174-
self.target.as_ref().unwrap_or(&self.host).dest()
175-
}
176-
177172
/// Returns the root of the build output tree for the host
178173
pub fn host_root(&self) -> &Path {
179174
self.host.dest()
@@ -261,8 +256,8 @@ impl<'a, 'cfg: 'a> CompilationFiles<'a, 'cfg> {
261256
/// (eg a dependent lib).
262257
fn link_stem(&self, unit: &Unit<'a>) -> Option<(PathBuf, String)> {
263258
let out_dir = self.out_dir(unit);
264-
let bin_stem = self.bin_stem(unit);
265-
let file_stem = self.file_stem(unit);
259+
let bin_stem = self.bin_stem(unit); // Stem without metadata.
260+
let file_stem = self.file_stem(unit); // Stem with metadata.
266261

267262
// We currently only lift files up from the `deps` directory. If
268263
// it was compiled into something like `example/` or `doc/` then

src/cargo/core/compiler/layout.rs

Lines changed: 111 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,123 @@
88
//! # places all of its output here.
99
//! target/
1010
//!
11-
//! # This is the root directory for all output of *dependencies*
12-
//! deps/
11+
//! # Cache of `rustc -Vv` output for performance.
12+
//! .rustc-info.json
1313
//!
14-
//! # Root directory for all compiled examples
15-
//! examples/
14+
//! # All final artifacts are linked into this directory from `deps`.
15+
//! debug/ # or release/
16+
//!
17+
//! # File used to lock the directory to prevent multiple cargo processes
18+
//! # from using it at the same time.
19+
//! .cargo-lock
20+
//!
21+
//! # Hidden directory that holds all of the fingerprint files for all
22+
//! # packages
23+
//! .fingerprint/
24+
//! # Each package is in a separate directory.
25+
//! $pkgname-$META/
26+
//! # Set of source filenames for this package.
27+
//! dep-lib-$pkgname-$META
28+
//! # Timestamp when this package was last built.
29+
//! invoked.timestamp
30+
//! # The fingerprint hash.
31+
//! lib-$pkgname-$META
32+
//! # Detailed information used for logging the reason why
33+
//! # something is being recompiled.
34+
//! lib-$pkgname-$META.json
35+
//!
36+
//! # This is the root directory for all rustc artifacts except build
37+
//! # scripts, examples, and test and bench executables. Almost every
38+
//! # artifact should have a metadata hash added to its filename to
39+
//! # prevent collisions. One notable exception is dynamic libraries.
40+
//! deps/
41+
//!
42+
//! # Root directory for all compiled examples.
43+
//! examples/
44+
//!
45+
//! # Directory used to store incremental data for the compiler (when
46+
//! # incremental is enabled.
47+
//! incremental/
1648
//!
1749
//! # This is the location at which the output of all custom build
18-
//! # commands are rooted
50+
//! # commands are rooted.
1951
//! build/
2052
//!
2153
//! # Each package gets its own directory where its build script and
2254
//! # script output are placed
23-
//! $pkg1/
24-
//! $pkg2/
25-
//! $pkg3/
55+
//! $pkgname-$META/ # For the build script itself.
56+
//! # The build script executable (name may be changed by user).
57+
//! build-script-build-$META
58+
//! # Hard link to build-script-build-$META.
59+
//! build-script-build
60+
//! # Dependency information generated by rustc.
61+
//! build-script-build-$META.d
62+
//! # Debug information, depending on platform and profile
63+
//! # settings.
64+
//! <debug symbols>
2665
//!
27-
//! # Each directory package has a `out` directory where output
28-
//! # is placed.
66+
//! # The package shows up twice with two different metadata hashes.
67+
//! $pkgname-$META/ # For the output of the build script.
68+
//! # Timestamp when the build script was last executed.
69+
//! invoked.timestamp
70+
//! # Directory where script can output files ($OUT_DIR).
2971
//! out/
72+
//! # Output from the build script.
73+
//! output
74+
//! # Path to `out`, used to help when the target directory is
75+
//! # moved.
76+
//! root-output
77+
//! # Stderr output from the build script.
78+
//! stderr
79+
//!
80+
//! # Output from rustdoc
81+
//! doc/
3082
//!
31-
//! # This is the location at which the output of all old custom build
32-
//! # commands are rooted
33-
//! native/
34-
//!
35-
//! # Each package gets its own directory for where its output is
36-
//! # placed. We can't track exactly what's getting put in here, so
37-
//! # we just assume that all relevant output is in these
38-
//! # directories.
39-
//! $pkg1/
40-
//! $pkg2/
41-
//! $pkg3/
42-
//!
43-
//! # Directory used to store incremental data for the compiler (when
44-
//! # incremental is enabled.
45-
//! incremental/
46-
//!
47-
//! # Hidden directory that holds all of the fingerprint files for all
48-
//! # packages
49-
//! .fingerprint/
83+
//! # Used by `cargo package` and `cargo publish` to build a `.crate` file.
84+
//! package/
85+
//!
86+
//! # Experimental feature for generated build scripts.
87+
//! .metabuild/
5088
//! ```
89+
//!
90+
//! When cross-compiling, the layout is the same, except it appears in
91+
//! `target/$TRIPLE`.
5192
5293
use std::fs;
5394
use std::io;
5495
use std::path::{Path, PathBuf};
5596

5697
use crate::core::Workspace;
57-
use crate::util::{CargoResult, Config, FileLock, Filesystem};
98+
use crate::util::{CargoResult, FileLock};
5899

59100
/// Contains the paths of all target output locations.
60101
///
61102
/// See module docs for more information.
62103
pub struct Layout {
104+
/// The root directory: `/path/to/target`.
105+
/// If cross compiling: `/path/to/target/$TRIPLE`.
63106
root: PathBuf,
107+
/// The final artifact destination: `$root/debug` (or `release`).
108+
dest: PathBuf,
109+
/// The directory with rustc artifacts: `$dest/deps`
64110
deps: PathBuf,
65-
native: PathBuf,
111+
/// The directory for build scripts: `$dest/build`
66112
build: PathBuf,
113+
/// The directory for incremental files: `$dest/incremental`
67114
incremental: PathBuf,
115+
/// The directory for fingerprints: `$dest/.fingerprint`
68116
fingerprint: PathBuf,
117+
/// The directory for examples: `$dest/examples`
69118
examples: PathBuf,
70-
/// The lock file for a build, will be unlocked when this struct is `drop`ped.
119+
/// The directory for rustdoc output: `$root/doc`
120+
doc: PathBuf,
121+
/// The lockfile for a build (`.cargo-lock`). Will be unlocked when this
122+
/// struct is `drop`ped.
71123
_lock: FileLock,
72124
}
73125

74126
pub fn is_bad_artifact_name(name: &str) -> bool {
75-
["deps", "examples", "build", "native", "incremental"]
127+
["deps", "examples", "build", "incremental"]
76128
.iter()
77129
.any(|&reserved| reserved == name)
78130
}
@@ -82,63 +134,57 @@ impl Layout {
82134
///
83135
/// This function will block if the directory is already locked.
84136
///
85-
/// Differs from `at` in that this calculates the root path from the workspace target directory,
86-
/// adding the target triple and the profile (debug, release, ...).
137+
/// `dest` should be the final artifact directory name. Currently either
138+
/// "debug" or "release".
87139
pub fn new(ws: &Workspace<'_>, triple: Option<&str>, dest: &str) -> CargoResult<Layout> {
88-
let mut path = ws.target_dir();
140+
let mut root = ws.target_dir();
89141
// Flexible target specifications often point at json files, so interpret
90142
// the target triple as a Path and then just use the file stem as the
91143
// component for the directory name in that case.
92144
if let Some(triple) = triple {
93145
let triple = Path::new(triple);
94146
if triple.extension().and_then(|s| s.to_str()) == Some("json") {
95-
path.push(
147+
root.push(
96148
triple
97149
.file_stem()
98150
.ok_or_else(|| failure::format_err!("invalid target"))?,
99151
);
100152
} else {
101-
path.push(triple);
153+
root.push(triple);
102154
}
103155
}
104-
path.push(dest);
105-
Layout::at(ws.config(), path)
106-
}
107-
108-
/// Calculate the paths for build output, lock the build directory, and return as a Layout.
109-
///
110-
/// This function will block if the directory is already locked.
111-
pub fn at(config: &Config, root: Filesystem) -> CargoResult<Layout> {
156+
let dest = root.join(dest);
112157
// If the root directory doesn't already exist go ahead and create it
113158
// here. Use this opportunity to exclude it from backups as well if the
114159
// system supports it since this is a freshly created folder.
115-
if !root.as_path_unlocked().exists() {
116-
root.create_dir()?;
117-
exclude_from_backups(root.as_path_unlocked());
160+
if !dest.as_path_unlocked().exists() {
161+
dest.create_dir()?;
162+
exclude_from_backups(dest.as_path_unlocked());
118163
}
119164

120165
// For now we don't do any more finer-grained locking on the artifact
121166
// directory, so just lock the entire thing for the duration of this
122167
// compile.
123-
let lock = root.open_rw(".cargo-lock", config, "build directory")?;
168+
let lock = dest.open_rw(".cargo-lock", ws.config(), "build directory")?;
124169
let root = root.into_path_unlocked();
170+
let dest = dest.into_path_unlocked();
125171

126172
Ok(Layout {
127-
deps: root.join("deps"),
128-
native: root.join("native"),
129-
build: root.join("build"),
130-
incremental: root.join("incremental"),
131-
fingerprint: root.join(".fingerprint"),
132-
examples: root.join("examples"),
173+
deps: dest.join("deps"),
174+
build: dest.join("build"),
175+
incremental: dest.join("incremental"),
176+
fingerprint: dest.join(".fingerprint"),
177+
examples: dest.join("examples"),
178+
doc: root.join("doc"),
133179
root,
180+
dest,
134181
_lock: lock,
135182
})
136183
}
137184

138185
/// Makes sure all directories stored in the Layout exist on the filesystem.
139186
pub fn prepare(&mut self) -> io::Result<()> {
140187
mkdir(&self.deps)?;
141-
mkdir(&self.native)?;
142188
mkdir(&self.incremental)?;
143189
mkdir(&self.fingerprint)?;
144190
mkdir(&self.examples)?;
@@ -154,9 +200,9 @@ impl Layout {
154200
}
155201
}
156202

157-
/// Fetch the root path.
203+
/// Fetch the destination path for final artifacts (`/…/target/debug`).
158204
pub fn dest(&self) -> &Path {
159-
&self.root
205+
&self.dest
160206
}
161207
/// Fetch the deps path.
162208
pub fn deps(&self) -> &Path {
@@ -166,7 +212,11 @@ impl Layout {
166212
pub fn examples(&self) -> &Path {
167213
&self.examples
168214
}
169-
/// Fetch the root path.
215+
/// Fetch the doc path.
216+
pub fn doc(&self) -> &Path {
217+
&self.doc
218+
}
219+
/// Fetch the root path (`/…/target`).
170220
pub fn root(&self) -> &Path {
171221
&self.root
172222
}
@@ -178,7 +228,7 @@ impl Layout {
178228
pub fn fingerprint(&self) -> &Path {
179229
&self.fingerprint
180230
}
181-
/// Fetch the build path.
231+
/// Fetch the build script path.
182232
pub fn build(&self) -> &Path {
183233
&self.build
184234
}

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ fn link_targets<'a, 'cfg>(
480480
}))
481481
}
482482

483+
/// Hardlink (file) or symlink (dir) src to dst if possible, otherwise copy it.
483484
fn hardlink_or_copy(src: &Path, dst: &Path) -> CargoResult<()> {
484485
debug!("linking {} to {}", src.display(), dst.display());
485486
if is_same_file(src, dst).unwrap_or(false) {

src/doc/src/reference/manifest.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ dependencies residing in the workspace directory become members. You can add
628628
additional packages to the workspace by listing them in the `members` key. Note
629629
that members of the workspaces listed explicitly will also have their path
630630
dependencies included in the workspace. Sometimes a package may have a lot of
631-
workspace members and it can be onerous to keep up to date. The path dependency
631+
workspace members and it can be onerous to keep up to date. The `members` list
632632
can also use [globs][globs] to match multiple paths. Finally, the `exclude`
633633
key can be used to blacklist paths from being included in a workspace. This can
634634
be useful if some path dependencies aren't desired to be in the workspace at

0 commit comments

Comments
 (0)