Skip to content

Commit fa50fc1

Browse files
committed
doc(target_info): add missing doc comments
1 parent afadab2 commit fa50fc1

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

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)