Skip to content

Commit e008032

Browse files
committed
Auto merge of #11241 - weihanglo:doc/build_context, r=epage
Polish docs for module `build_context`
2 parents 76293b3 + d86d3bf commit e008032

File tree

2 files changed

+99
-14
lines changed

2 files changed

+99
-14
lines changed

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! [`BuildContext`] is a (mostly) static information about a build task.
2+
13
use crate::core::compiler::unit_graph::UnitGraph;
24
use crate::core::compiler::{BuildConfig, CompileKind, Unit};
35
use crate::core::profiles::Profiles;
@@ -15,19 +17,42 @@ pub use self::target_info::{
1517
FileFlavor, FileType, RustDocFingerprint, RustcTargetData, TargetInfo,
1618
};
1719

18-
/// The build context, containing all information about a build task.
20+
/// The build context, containing complete infomration needed for a build task
21+
/// before it gets started.
1922
///
2023
/// It is intended that this is mostly static information. Stuff that mutates
21-
/// during the build can be found in the parent `Context`. (I say mostly,
24+
/// during the build can be found in the parent [`Context`]. (I say mostly,
2225
/// because this has internal caching, but nothing that should be observable
2326
/// or require &mut.)
27+
///
28+
/// As a result, almost every field on `BuildContext` is public, including
29+
///
30+
/// * a resolved [`UnitGraph`] of your dependencies,
31+
/// * a [`Profiles`] containing compiler flags presets,
32+
/// * a [`RustcTargetData`] containing host and target platform information,
33+
/// * and a [`PackageSet`] for further package downloads,
34+
///
35+
/// just to name a few. Learn more on each own documentation.
36+
///
37+
/// # How to use
38+
///
39+
/// To prepare a build task, you may not want to use [`BuildContext::new`] directly,
40+
/// since it is often too lower-level.
41+
/// Instead, [`ops::create_bcx`] is usually what you are looking for.
42+
///
43+
/// [`Context`]: crate::core::compiler::Context
44+
/// [`ops::create_bcx`]: crate::ops::create_bcx
2445
pub struct BuildContext<'a, 'cfg> {
2546
/// The workspace the build is for.
2647
pub ws: &'a Workspace<'cfg>,
2748

2849
/// The cargo configuration.
2950
pub config: &'cfg Config,
51+
52+
/// This contains a collection of compiler flags presets.
3053
pub profiles: Profiles,
54+
55+
/// Configuration information for a rustc build.
3156
pub build_config: &'a BuildConfig,
3257

3358
/// Extra compiler args for either `rustc` or `rustdoc`.
@@ -47,7 +72,7 @@ pub struct BuildContext<'a, 'cfg> {
4772
/// The dependency graph of units to compile.
4873
pub unit_graph: UnitGraph,
4974

50-
/// Reverse-dependencies of documented units, used by the rustdoc --scrape-examples flag.
75+
/// Reverse-dependencies of documented units, used by the `rustdoc --scrape-examples` flag.
5176
pub scrape_units: Vec<Unit>,
5277

5378
/// The list of all kinds that are involved in this build
@@ -88,6 +113,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
88113
})
89114
}
90115

116+
/// Information of the `rustc` this build task will use.
91117
pub fn rustc(&self) -> &Rustc {
92118
&self.target_data.rustc
93119
}
@@ -116,14 +142,36 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
116142
self.build_config.jobs
117143
}
118144

145+
/// Extra compiler flags to pass to `rustc` for a given unit.
146+
///
147+
/// Although it depends on the caller, in the current Cargo implementation,
148+
/// these flags take precendence over those from [`BuildContext::extra_args_for`].
149+
///
150+
/// As of now, these flags come from environment variables and configurations.
151+
/// See [`TargetInfo.rustflags`] for more on how Cargo collects them.
152+
///
153+
/// [`TargetInfo.rustflags`]: TargetInfo::rustflags
119154
pub fn rustflags_args(&self, unit: &Unit) -> &[String] {
120155
&self.target_data.info(unit.kind).rustflags
121156
}
122157

158+
/// Extra compiler flags to pass to `rustdoc` for a given unit.
159+
///
160+
/// Although it depends on the caller, in the current Cargo implementation,
161+
/// these flags take precendence over those from [`BuildContext::extra_args_for`].
162+
///
163+
/// As of now, these flags come from environment variables and configurations.
164+
/// See [`TargetInfo.rustdocflags`] for more on how Cargo collects them.
165+
///
166+
/// [`TargetInfo.rustdocflags`]: TargetInfo::rustdocflags
123167
pub fn rustdocflags_args(&self, unit: &Unit) -> &[String] {
124168
&self.target_data.info(unit.kind).rustdocflags
125169
}
126170

171+
/// Extra compiler args for either `rustc` or `rustdoc`.
172+
///
173+
/// As of now, these flags come from the trailing args of either
174+
/// `cargo rustc` or `cargo rustdoc`.
127175
pub fn extra_args_for(&self, unit: &Unit) -> Option<&Vec<String>> {
128176
self.extra_compiler_args.get(unit)
129177
}

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

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! This modules contains types storing information of target platfroms.
2+
//!
3+
//! Normally, call [`RustcTargetData::new`] to construct all the target
4+
//! platform once, and then query info on your demand. For example,
5+
//!
6+
//! * [`RustcTargetData::dep_platform_activated`] to check if platform is activated.
7+
//! * [`RustcTargetData::info`] to get a [`TargetInfo`] for an in-depth query.
8+
//! * [`TargetInfo::rustc_outputs`] to get a list of supported file types.
9+
110
use crate::core::compiler::{
211
BuildOutput, CompileKind, CompileMode, CompileTarget, Context, CrateType,
312
};
@@ -16,8 +25,9 @@ use std::str::{self, FromStr};
1625

1726
/// Information about the platform target gleaned from querying rustc.
1827
///
19-
/// `RustcTargetData` keeps two of these, one for the host and one for the
20-
/// target. If no target is specified, it uses a clone from the host.
28+
/// [`RustcTargetData`] keeps several of these, one for the host and the others
29+
/// for other specified targets. If no target is specified, it uses a clone from
30+
/// the host.
2131
#[derive(Clone)]
2232
pub struct TargetInfo {
2333
/// A base process builder for discovering crate type information. In
@@ -41,9 +51,9 @@ pub struct TargetInfo {
4151
/// Path to the "lib" directory in the sysroot which rustc uses for linking
4252
/// target libraries.
4353
pub sysroot_target_libdir: PathBuf,
44-
/// Extra flags to pass to `rustc`, see `env_args`.
54+
/// Extra flags to pass to `rustc`, see [`extra_args`].
4555
pub rustflags: Vec<String>,
46-
/// Extra flags to pass to `rustdoc`, see `env_args`.
56+
/// Extra flags to pass to `rustdoc`, see [`extra_args`].
4757
pub rustdocflags: Vec<String>,
4858
/// Whether or not rustc supports the `-Csplit-debuginfo` flag.
4959
pub supports_split_debuginfo: bool,
@@ -132,13 +142,20 @@ impl FileType {
132142
}
133143

134144
impl TargetInfo {
145+
/// Learns the information of target platform from `rustc` invocation(s).
146+
///
147+
/// Generally, the first time calling this function is expensive, as it may
148+
/// query `rustc` several times. To reduce the cost, output of each `rustc`
149+
/// invocation is cached by [`Rustc::cached_output`].
150+
///
151+
/// Search `Tricky` to learn why querying `rustc` several times is needed.
135152
pub fn new(
136153
config: &Config,
137154
requested_kinds: &[CompileKind],
138155
rustc: &Rustc,
139156
kind: CompileKind,
140157
) -> CargoResult<TargetInfo> {
141-
let mut rustflags = env_args(
158+
let mut rustflags = extra_args(
142159
config,
143160
requested_kinds,
144161
&rustc.host,
@@ -149,6 +166,13 @@ impl TargetInfo {
149166
let mut turn = 0;
150167
loop {
151168
let extra_fingerprint = kind.fingerprint_hash();
169+
170+
// Query rustc for several kinds of info from each line of output:
171+
// 0) file-names (to determine output file prefix/suffix for given crate type)
172+
// 1) sysroot
173+
// 2) cfg
174+
//
175+
// Search `--print` to see what we query so far.
152176
let mut process = rustc.workspace_process();
153177
process
154178
.arg("-")
@@ -174,6 +198,8 @@ impl TargetInfo {
174198
for crate_type in KNOWN_CRATE_TYPES.iter() {
175199
process.arg("--crate-type").arg(crate_type.as_str());
176200
}
201+
202+
// An extra `rustc` call to determine `-Csplit-debuginfo=packed` support.
177203
let supports_split_debuginfo = rustc
178204
.cached_output(
179205
process.clone().arg("-Csplit-debuginfo=packed"),
@@ -233,7 +259,7 @@ impl TargetInfo {
233259

234260
// recalculate `rustflags` from above now that we have `cfg`
235261
// information
236-
let new_flags = env_args(
262+
let new_flags = extra_args(
237263
config,
238264
requested_kinds,
239265
&rustc.host,
@@ -268,7 +294,7 @@ impl TargetInfo {
268294
sysroot_host_libdir,
269295
sysroot_target_libdir,
270296
rustflags,
271-
rustdocflags: env_args(
297+
rustdocflags: extra_args(
272298
config,
273299
requested_kinds,
274300
&rustc.host,
@@ -294,7 +320,7 @@ impl TargetInfo {
294320
true
295321
}
296322

297-
/// All the target `cfg` settings.
323+
/// All the target [`Cfg`] settings.
298324
pub fn cfg(&self) -> &[Cfg] {
299325
&self.cfg
300326
}
@@ -580,6 +606,7 @@ fn output_err_info(cmd: &ProcessBuilder, stdout: &str, stderr: &str) -> String {
580606
result
581607
}
582608

609+
/// Compiler flags for either rustc or rustdoc.
583610
#[derive(Debug, Copy, Clone)]
584611
enum Flags {
585612
Rust,
@@ -614,6 +641,7 @@ impl Flags {
614641
/// - `target.*.rustflags` from the config (.cargo/config)
615642
/// - `target.cfg(..).rustflags` from the config
616643
/// - `host.*.rustflags` from the config if compiling a host artifact or without `--target`
644+
/// (requires `-Zhost-config`)
617645
///
618646
/// then if none of those were found
619647
///
@@ -624,7 +652,7 @@ impl Flags {
624652
/// For those artifacts, _only_ `host.*.rustflags` is respected, and no other configuration
625653
/// sources, _regardless of the value of `target-applies-to-host`_. This is counterintuitive, but
626654
/// necessary to retain backwards compatibility with older versions of Cargo.
627-
fn env_args(
655+
fn extra_args(
628656
config: &Config,
629657
requested_kinds: &[CompileKind],
630658
host_triple: &str,
@@ -669,6 +697,8 @@ fn env_args(
669697
}
670698
}
671699

700+
/// Gets compiler flags from environment variables.
701+
/// See [`extra_args`] for more.
672702
fn rustflags_from_env(flags: Flags) -> Option<Vec<String>> {
673703
// First try CARGO_ENCODED_RUSTFLAGS from the environment.
674704
// Prefer this over RUSTFLAGS since it's less prone to encoding errors.
@@ -693,6 +723,8 @@ fn rustflags_from_env(flags: Flags) -> Option<Vec<String>> {
693723
None
694724
}
695725

726+
/// Gets compiler flags from `[target]` section in the config.
727+
/// See [`extra_args`] for more.
696728
fn rustflags_from_target(
697729
config: &Config,
698730
host_triple: &str,
@@ -734,6 +766,8 @@ fn rustflags_from_target(
734766
}
735767
}
736768

769+
/// Gets compiler flags from `[host]` section in the config.
770+
/// See [`extra_args`] for more.
737771
fn rustflags_from_host(
738772
config: &Config,
739773
flag: Flags,
@@ -750,6 +784,8 @@ fn rustflags_from_host(
750784
Ok(list.as_ref().map(|l| l.val.as_slice().to_vec()))
751785
}
752786

787+
/// Gets compiler flags from `[build]` section in the config.
788+
/// See [`extra_args`] for more.
753789
fn rustflags_from_build(config: &Config, flag: Flags) -> CargoResult<Option<Vec<String>>> {
754790
// Then the `build.rustflags` value.
755791
let build = config.build_config()?;
@@ -773,11 +809,12 @@ pub struct RustcTargetData<'cfg> {
773809
/// `rustc` is invoked without a `--target` flag. This is used for
774810
/// procedural macros, build scripts, etc.
775811
host_config: TargetConfig,
812+
/// Information about the host platform.
776813
host_info: TargetInfo,
777814

778-
/// Build information for targets that we're building for. This will be
779-
/// empty if the `--target` flag is not passed.
815+
/// Build information for targets that we're building for.
780816
target_config: HashMap<CompileTarget, TargetConfig>,
817+
/// Information about the target platform that we're building for.
781818
target_info: HashMap<CompileTarget, TargetInfo>,
782819
}
783820

0 commit comments

Comments
 (0)