Skip to content

Commit c8ef066

Browse files
committed
Polish docs for cargo_compile, unit_dependencies, and profile
1 parent e691e18 commit c8ef066

File tree

3 files changed

+81
-22
lines changed

3 files changed

+81
-22
lines changed

src/cargo/core/compiler/unit_dependencies.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//! Constructs the dependency graph for compilation.
1+
//! # Constructs the dependency graph for compilation
22
//!
33
//! Rust code is typically organized as a set of Cargo packages. The
44
//! dependencies between the packages themselves are stored in the
5-
//! `Resolve` struct. However, we can't use that information as is for
5+
//! [`Resolve`] struct. However, we can't use that information as is for
66
//! compilation! A package typically contains several targets, or crates,
77
//! and these targets has inter-dependencies. For example, you need to
88
//! compile the `lib` target before the `bin` one, and you need to compile
@@ -13,7 +13,7 @@
1313
//! is exactly what this module is doing! Well, almost exactly: another
1414
//! complication is that we might want to compile the same target several times
1515
//! (for example, with and without tests), so we actually build a dependency
16-
//! graph of `Unit`s, which capture these properties.
16+
//! graph of [`Unit`]s, which capture these properties.
1717
1818
use std::collections::{HashMap, HashSet};
1919

@@ -35,23 +35,27 @@ use crate::CargoResult;
3535

3636
const IS_NO_ARTIFACT_DEP: Option<&'static Artifact> = None;
3737

38-
/// Collection of stuff used while creating the `UnitGraph`.
38+
/// Collection of stuff used while creating the [`UnitGraph`].
3939
struct State<'a, 'cfg> {
4040
ws: &'a Workspace<'cfg>,
4141
config: &'cfg Config,
42+
/// Stores the result of building the [`UnitGraph`].
4243
unit_dependencies: UnitGraph,
4344
package_set: &'a PackageSet<'cfg>,
4445
usr_resolve: &'a Resolve,
4546
usr_features: &'a ResolvedFeatures,
47+
/// Like `usr_resolve` but for building standard library (`-Zbuild-std`).
4648
std_resolve: Option<&'a Resolve>,
49+
/// Like `usr_features` but for building standard library (`-Zbuild-std`).
4750
std_features: Option<&'a ResolvedFeatures>,
48-
/// This flag is `true` while generating the dependencies for the standard
49-
/// library.
51+
/// `true` while generating the dependencies for the standard library.
5052
is_std: bool,
53+
/// The mode we are compiling in. Used for preventing from building lib thrice.
5154
global_mode: CompileMode,
5255
target_data: &'a RustcTargetData<'cfg>,
5356
profiles: &'a Profiles,
5457
interner: &'a UnitInterner,
58+
// Units for `-Zrustdoc-scrape-examples`.
5559
scrape_units: &'a [Unit],
5660

5761
/// A set of edges in `unit_dependencies` where (a, b) means that the
@@ -73,6 +77,9 @@ impl IsArtifact {
7377
}
7478
}
7579

80+
/// Then entry point for building a dependency graph of compilation units.
81+
///
82+
/// You can find some information for arguments from doc of [`State`].
7683
pub fn build_unit_dependencies<'a, 'cfg>(
7784
ws: &'a Workspace<'cfg>,
7885
package_set: &'a PackageSet<'cfg>,
@@ -1015,6 +1022,7 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_>) {
10151022
}
10161023

10171024
impl<'a, 'cfg> State<'a, 'cfg> {
1025+
/// Gets `std_resolve` during building std, otherwise `usr_resolve`.
10181026
fn resolve(&self) -> &'a Resolve {
10191027
if self.is_std {
10201028
self.std_resolve.unwrap()
@@ -1023,6 +1031,7 @@ impl<'a, 'cfg> State<'a, 'cfg> {
10231031
}
10241032
}
10251033

1034+
/// Gets `std_features` during building std, otherwise `usr_features`.
10261035
fn features(&self) -> &'a ResolvedFeatures {
10271036
if self.is_std {
10281037
self.std_features.unwrap()

src/cargo/ops/cargo_compile.rs

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1-
//! The Cargo "compile" operation.
1+
//! # The Cargo "compile" operation
22
//!
33
//! This module contains the entry point for starting the compilation process
44
//! for commands like `build`, `test`, `doc`, `rustc`, etc.
55
//!
6-
//! The `compile` function will do all the work to compile a workspace. A
6+
//! The [`compile`] function will do all the work to compile a workspace. A
77
//! rough outline is:
88
//!
9-
//! - Resolve the dependency graph (see `ops::resolve`).
10-
//! - Download any packages needed (see `PackageSet`).
9+
//! - Resolve the dependency graph (see [`ops::resolve`]).
10+
//! - Download any packages needed (see [`PackageSet`]).
1111
//! - Generate a list of top-level "units" of work for the targets the user
12-
//! requested on the command-line. Each `Unit` corresponds to a compiler
13-
//! invocation. This is done in this module (`generate_targets`).
14-
//! - Build the graph of `Unit` dependencies (see
15-
//! `core::compiler::context::unit_dependencies`).
16-
//! - Create a `Context` which will perform the following steps:
17-
//! - Prepare the `target` directory (see `Layout`).
12+
//! requested on the command-line. Each [`Unit`] corresponds to a compiler
13+
//! invocation. This is done in this module ([`generate_targets`]).
14+
//! - Build the graph of `Unit` dependencies (see [`unit_dependencies`]).
15+
//! - Create a [`Context`] which will perform the following steps:
16+
//! - Prepare the `target` directory (see [`Layout`]).
1817
//! - Create a job queue (see `JobQueue`). The queue checks the
1918
//! fingerprint of each `Unit` to determine if it should run or be
2019
//! skipped.
2120
//! - Execute the queue. Each leaf in the queue's dependency graph is
2221
//! executed, and then removed from the graph when finished. This
2322
//! repeats until the queue is empty.
23+
//!
24+
//! **Note**: "target" inside this module generally refers to ["Cargo Target"],
25+
//! which corresponds to artifact that will be built in a package. Not to be
26+
//! confused with target-triple or target architecture.
27+
//!
28+
//! [`unit_dependencies`]: crate::core::compiler::unit_dependencies
29+
//! [`Layout`]: crate::core::compiler::Layout
30+
//! ["Cargo Target"]: https://doc.rust-lang.org/nightly/cargo/reference/cargo-targets.html
2431
2532
use std::collections::{BTreeSet, HashMap, HashSet};
2633
use std::fmt::Write;
@@ -50,9 +57,9 @@ use anyhow::{bail, Context as _};
5057

5158
/// Contains information about how a package should be compiled.
5259
///
53-
/// Note on distinction between `CompileOptions` and `BuildConfig`:
60+
/// Note on distinction between `CompileOptions` and [`BuildConfig`]:
5461
/// `BuildConfig` contains values that need to be retained after
55-
/// `BuildContext` is created. The other fields are no longer necessary. Think
62+
/// [`BuildContext`] is created. The other fields are no longer necessary. Think
5663
/// of it as `CompileOptions` are high-level settings requested on the
5764
/// command-line, and `BuildConfig` are low-level settings for actually
5865
/// driving `rustc`.
@@ -105,15 +112,28 @@ impl CompileOptions {
105112
}
106113
}
107114

115+
/// Represents the selected pacakges that will be built.
116+
///
117+
/// Generally, it represents the combination of all `-p` flag. When working within
118+
/// a workspace, `--exclude` and `--workspace` flags also contribute to it.
108119
#[derive(PartialEq, Eq, Debug)]
109120
pub enum Packages {
121+
/// Pacakges selected by default. Ususally means no flag provided.
110122
Default,
123+
/// Opt in all packages.
124+
///
125+
/// As of the time of this writing, it only works on opting in all workspace mebeer
111126
All,
127+
/// Opt out of packages passed in.
128+
///
129+
/// As of the time of this writing, it only works on opting out workspace members.
112130
OptOut(Vec<String>),
131+
/// A sequence of hand-picked packages that will be built. Normally done by `-p` flag.
113132
Packages(Vec<String>),
114133
}
115134

116135
impl Packages {
136+
/// Creates a `Packages` from flags which are generally equivalent to command line flags.
117137
pub fn from_flags(all: bool, exclude: Vec<String>, package: Vec<String>) -> CargoResult<Self> {
118138
Ok(match (all, exclude.len(), package.len()) {
119139
(false, 0, 0) => Packages::Default,
@@ -124,7 +144,7 @@ impl Packages {
124144
})
125145
}
126146

127-
/// Converts selected packages from a workspace to `PackageIdSpec`s.
147+
/// Converts selected packages to [`PackageIdSpec`]s.
128148
pub fn to_package_id_specs(&self, ws: &Workspace<'_>) -> CargoResult<Vec<PackageIdSpec>> {
129149
let specs = match self {
130150
Packages::All => ws
@@ -186,7 +206,7 @@ impl Packages {
186206
Ok(specs)
187207
}
188208

189-
/// Gets a list of selected packages from a workspace.
209+
/// Gets a list of selected Packages.
190210
pub fn get_packages<'ws>(&self, ws: &'ws Workspace<'_>) -> CargoResult<Vec<&'ws Package>> {
191211
let packages: Vec<_> = match self {
192212
Packages::Default => ws.default_members().collect(),
@@ -232,6 +252,7 @@ impl Packages {
232252
}
233253

234254
#[derive(Debug, PartialEq, Eq)]
255+
/// Indicates whether or not the library target gets included.
235256
pub enum LibRule {
236257
/// Include the library, fail if not present
237258
True,
@@ -242,18 +263,28 @@ pub enum LibRule {
242263
}
243264

244265
#[derive(Debug)]
266+
/// Indicates which Cargo targets will be selected to be built.
245267
pub enum FilterRule {
268+
/// All included.
246269
All,
270+
/// Just a subset of Cargo targets based on names given.
247271
Just(Vec<String>),
248272
}
249273

274+
/// Filter to apply to the root package to select which Cargo targets will be built.
275+
/// (examples, bins, benches, tests, ...)
276+
///
277+
/// The actual filter process happens inside [`generate_targets`].
250278
#[derive(Debug)]
251279
pub enum CompileFilter {
280+
/// The default set of Cargo targets.
252281
Default {
253282
/// Flag whether targets can be safely skipped when required-features are not satisfied.
254283
required_features_filterable: bool,
255284
},
285+
/// Only includes a subset of all Cargo targets.
256286
Only {
287+
/// Include all Cargo targets.
257288
all_targets: bool,
258289
lib: LibRule,
259290
bins: FilterRule,
@@ -263,13 +294,18 @@ pub enum CompileFilter {
263294
},
264295
}
265296

297+
/// Compiles!
298+
///
299+
/// This uses the [`DefaultExecutor`]. To use a custom [`Executor`], see [`compile_with_exec`].
266300
pub fn compile<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult<Compilation<'a>> {
267301
let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
268302
compile_with_exec(ws, options, &exec)
269303
}
270304

271-
/// Like `compile` but allows specifying a custom `Executor` that will be able to intercept build
272-
/// calls and add custom logic. `compile` uses `DefaultExecutor` which just passes calls through.
305+
/// Like [`compile`] but allows specifying a custom [`Executor`]
306+
/// that will be able to intercept build calls and add custom logic.
307+
///
308+
/// [`compile`] uses [`DefaultExecutor`] which just passes calls through.
273309
pub fn compile_with_exec<'a>(
274310
ws: &Workspace<'a>,
275311
options: &CompileOptions,
@@ -279,6 +315,7 @@ pub fn compile_with_exec<'a>(
279315
compile_ws(ws, options, exec)
280316
}
281317

318+
/// Like [`compile_with_exec`] but without warnings from manifest parsing.
282319
pub fn compile_ws<'a>(
283320
ws: &Workspace<'a>,
284321
options: &CompileOptions,
@@ -295,6 +332,9 @@ pub fn compile_ws<'a>(
295332
cx.compile(exec)
296333
}
297334

335+
/// Executes `rustc --print <VALUE>`.
336+
///
337+
/// * `print_opt_value` is the VALUE passed through.
298338
pub fn print<'a>(
299339
ws: &Workspace<'a>,
300340
options: &CompileOptions,
@@ -326,6 +366,10 @@ pub fn print<'a>(
326366
Ok(())
327367
}
328368

369+
/// Prepares all required information for the actual compilation.
370+
///
371+
/// For how it works and what data it collects,
372+
/// please see the [module-level documentation](self).
329373
pub fn create_bcx<'a, 'cfg>(
330374
ws: &'a Workspace<'cfg>,
331375
options: &'a CompileOptions,

src/cargo/util/profile.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
//! # An internal profiler for Cargo itself
2+
//!
3+
//! > **Note**: This might not be the module you are looking for.
4+
//! > For information about how Cargo handles compiler flags with profiles,
5+
//! > please see the module [`cargo::core::profiles`](crate::core::profiles).
6+
17
use std::cell::RefCell;
28
use std::env;
39
use std::fmt;

0 commit comments

Comments
 (0)