1
+ //! # Interact with the compiler
2
+ //!
3
+ //! If you consider [`ops::cargo_compile::compile`] as a `rustc` driver but on
4
+ //! Cargo side, this module is kinda the `rustc_interface` for that merits.
5
+ //! It contains all the interaction between Cargo and the rustc compiler,
6
+ //! from preparing the context for the entire build process, to scheduling
7
+ //! and executing each unit of work (e.g. running `rustc`), to managing and
8
+ //! caching the output artifact of a build.
9
+ //!
10
+ //! However, it hasn't yet exposed a clear definition of each phase or session,
11
+ //! like what rustc has done[^1]. Also, no one knows if Cargo really needs that.
12
+ //! To be pragmatic, here we list a handful of items you may want to learn:
13
+ //!
14
+ //! * [`BuildContext`] is a static context containg all information you need
15
+ //! before a build gets started.
16
+ //! * [`Context`] is the center of the world, coordinating a running build and
17
+ //! collecting information from it.
18
+ //! * [`custom_build`] is the home of build script executions and output parsing.
19
+ //! * [`fingerprint`] not only defines but also executes a set of rules to
20
+ //! determine if a re-compile is needed.
21
+ //! * [`job_queue`] is where the parallelism, job scheduling, and communication
22
+ //! machinary happen between Cargo and the compiler.
23
+ //! * [`layout`] defines and manages output artifacts of a build in the filesystem.
24
+ //! * [`unit_dependencies`] is for building a dependency graph for compilation
25
+ //! from a result of dependency resolution.
26
+ //! * [`Unit`] contains sufficient information to build something, usually
27
+ //! turning into a compiler invocation in a later phase.
28
+ //!
29
+ //! [^1]: Maybe [`-Zbuild-plan`](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#build-plan)
30
+ //! was designed to serve that purpose but still [in flux](https://github.com/rust-lang/cargo/issues/7614).
31
+ //!
32
+ //! [`ops::cargo_compile::compile`]: crate::ops::compile
33
+
1
34
pub mod artifact;
2
35
mod build_config;
3
36
mod build_context;
@@ -67,18 +100,31 @@ use rustfix::diagnostics::Applicability;
67
100
68
101
const RUSTDOC_CRATE_VERSION_FLAG : & str = "--crate-version" ;
69
102
103
+ // TODO: Rename this to `ExtraLinkArgFor` or else, and move to compiler/custom_build.rs?
104
+ /// Represents one of the instruction from `cargo:rustc-link-arg-*` build script
105
+ /// instruction family.
106
+ ///
107
+ /// In other words, indicates targets that custom linker arguments applies to.
70
108
#[ derive( Clone , Hash , Debug , PartialEq , Eq ) ]
71
109
pub enum LinkType {
110
+ /// Represents `cargo:rustc-link-arg=FLAG`.
72
111
All ,
112
+ /// Represents `cargo:rustc-cdylib-link-arg=FLAG`.
73
113
Cdylib ,
114
+ /// Represents `cargo:rustc-link-arg-bins=FLAG`.
74
115
Bin ,
116
+ /// Represents `cargo:rustc-link-arg-bin=BIN=FLAG`.
75
117
SingleBin ( String ) ,
118
+ /// Represents `cargo:rustc-link-arg-tests=FLAG`.
76
119
Test ,
120
+ /// Represents `cargo:rustc-link-arg-benches=FLAG`.
77
121
Bench ,
122
+ /// Represents `cargo:rustc-link-arg-examples=FLAG`.
78
123
Example ,
79
124
}
80
125
81
126
impl LinkType {
127
+ /// Checks if this link type applies to a given [`Target`].
82
128
pub fn applies_to ( & self , target : & Target ) -> bool {
83
129
match self {
84
130
LinkType :: All => true ,
@@ -140,6 +186,15 @@ impl Executor for DefaultExecutor {
140
186
}
141
187
}
142
188
189
+ /// Builds up and enqueue a list of pending jobs onto the `job` queue.
190
+ ///
191
+ /// Starting from the `unit`, this function recursively calls itself to build
192
+ /// all jobs for dependencies of the `unit`. Each of these jobs represents
193
+ /// compiling a particular package.
194
+ ///
195
+ /// Note that **no actual work is executed as part of this**, that's all done
196
+ /// next as part of [`JobQueue::execute`] function which will run everything
197
+ /// in order with proper parallelism.
143
198
fn compile < ' cfg > (
144
199
cx : & mut Context < ' _ , ' cfg > ,
145
200
jobs : & mut JobQueue < ' cfg > ,
@@ -230,6 +285,7 @@ fn make_failed_scrape_diagnostic(
230
285
)
231
286
}
232
287
288
+ /// Creates a unit of work invoking `rustc` for building the `unit`.
233
289
fn rustc ( cx : & mut Context < ' _ , ' _ > , unit : & Unit , exec : & Arc < dyn Executor > ) -> CargoResult < Work > {
234
290
let mut rustc = prepare_rustc ( cx, & unit. target . rustc_crate_types ( ) , unit) ?;
235
291
let build_plan = cx. bcx . build_config . build_plan ;
@@ -638,6 +694,8 @@ where
638
694
search_path
639
695
}
640
696
697
+ // TODO: do we really need this as a separate function?
698
+ // Maybe we should reorganize `rustc` fn to make it more traceable and readable.
641
699
fn prepare_rustc (
642
700
cx : & mut Context < ' _ , ' _ > ,
643
701
crate_types : & [ CrateType ] ,
@@ -672,6 +730,7 @@ fn prepare_rustc(
672
730
Ok ( base)
673
731
}
674
732
733
+ /// Creates a unit of work invoking `rustdoc` for documenting the `unit`.
675
734
fn rustdoc ( cx : & mut Context < ' _ , ' _ > , unit : & Unit ) -> CargoResult < Work > {
676
735
let bcx = cx. bcx ;
677
736
// script_metadata is not needed here, it is only for tests.
@@ -857,6 +916,9 @@ fn append_crate_version_flag(unit: &Unit, rustdoc: &mut ProcessBuilder) {
857
916
. arg ( unit. pkg . version ( ) . to_string ( ) ) ;
858
917
}
859
918
919
+ /// Adds [`--cap-lints`] to the command to execute.
920
+ ///
921
+ /// [`--cap-lints`]: https://doc.rust-lang.org/nightly/rustc/lints/levels.html#capping-lints
860
922
fn add_cap_lints ( bcx : & BuildContext < ' _ , ' _ > , unit : & Unit , cmd : & mut ProcessBuilder ) {
861
923
// If this is an upstream dep we don't want warnings from, turn off all
862
924
// lints.
@@ -870,7 +932,9 @@ fn add_cap_lints(bcx: &BuildContext<'_, '_>, unit: &Unit, cmd: &mut ProcessBuild
870
932
}
871
933
}
872
934
873
- /// Forward -Zallow-features if it is set for cargo.
935
+ /// Forwards [`-Zallow-features`] if it is set for cargo.
936
+ ///
937
+ /// [`-Zallow-features`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#allow-features
874
938
fn add_allow_features ( cx : & Context < ' _ , ' _ > , cmd : & mut ProcessBuilder ) {
875
939
if let Some ( allow) = & cx. bcx . config . cli_unstable ( ) . allow_features {
876
940
let mut arg = String :: from ( "-Zallow-features=" ) ;
@@ -879,14 +943,16 @@ fn add_allow_features(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder) {
879
943
}
880
944
}
881
945
882
- /// Add error-format flags to the command.
946
+ /// Adds [`-- error-format`] to the command to execute .
883
947
///
884
948
/// Cargo always uses JSON output. This has several benefits, such as being
885
949
/// easier to parse, handles changing formats (for replaying cached messages),
886
950
/// ensures atomic output (so messages aren't interleaved), allows for
887
951
/// intercepting messages like rmeta artifacts, etc. rustc includes a
888
952
/// "rendered" field in the JSON message with the message properly formatted,
889
953
/// which Cargo will extract and display to the user.
954
+ ///
955
+ /// [`--error-format`]: https://doc.rust-lang.org/nightly/rustc/command-line-arguments.html#--error-format-control-how-errors-are-produced
890
956
fn add_error_format_and_color ( cx : & Context < ' _ , ' _ > , cmd : & mut ProcessBuilder ) {
891
957
cmd. arg ( "--error-format=json" ) ;
892
958
let mut json = String :: from ( "--json=diagnostic-rendered-ansi,artifacts,future-incompat" ) ;
@@ -905,6 +971,7 @@ fn add_error_format_and_color(cx: &Context<'_, '_>, cmd: &mut ProcessBuilder) {
905
971
}
906
972
}
907
973
974
+ /// Adds essential rustc flags and environment variables to the command to execute.
908
975
fn build_base_args (
909
976
cx : & mut Context < ' _ , ' _ > ,
910
977
cmd : & mut ProcessBuilder ,
@@ -1124,7 +1191,7 @@ fn build_base_args(
1124
1191
Ok ( ( ) )
1125
1192
}
1126
1193
1127
- /// All active features for the unit passed as --cfg
1194
+ /// All active features for the unit passed as ` --cfg features=<feature-name>`.
1128
1195
fn features_args ( unit : & Unit ) -> Vec < OsString > {
1129
1196
let mut args = Vec :: with_capacity ( unit. features . len ( ) * 2 ) ;
1130
1197
@@ -1136,7 +1203,10 @@ fn features_args(unit: &Unit) -> Vec<OsString> {
1136
1203
args
1137
1204
}
1138
1205
1139
- /// Generate the --check-cfg arguments for the unit
1206
+ /// Generates the `--check-cfg` arguments for the `unit`.
1207
+ /// See unstable feature [`check-cfg`].
1208
+ ///
1209
+ /// [`check-cfg`]: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#check-cfg
1140
1210
fn check_cfg_args ( cx : & Context < ' _ , ' _ > , unit : & Unit ) -> Vec < OsString > {
1141
1211
if let Some ( ( features, well_known_names, well_known_values, _output) ) =
1142
1212
cx. bcx . config . cli_unstable ( ) . check_cfg
@@ -1176,6 +1246,7 @@ fn check_cfg_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
1176
1246
}
1177
1247
}
1178
1248
1249
+ /// Adds LTO related codegen flags.
1179
1250
fn lto_args ( cx : & Context < ' _ , ' _ > , unit : & Unit ) -> Vec < OsString > {
1180
1251
let mut result = Vec :: new ( ) ;
1181
1252
let mut push = |arg : & str | {
@@ -1196,6 +1267,11 @@ fn lto_args(cx: &Context<'_, '_>, unit: &Unit) -> Vec<OsString> {
1196
1267
result
1197
1268
}
1198
1269
1270
+ /// Adds dependency-relevant rustc flags and environment variables
1271
+ /// to the command to execute, such as [`-L`] and [`--extern`].
1272
+ ///
1273
+ /// [`-L`]: https://doc.rust-lang.org/nightly/rustc/command-line-arguments.html#-l-add-a-directory-to-the-library-search-path
1274
+ /// [`--extern`]: https://doc.rust-lang.org/nightly/rustc/command-line-arguments.html#--extern-specify-where-an-external-library-is-located
1199
1275
fn build_deps_args (
1200
1276
cmd : & mut ProcessBuilder ,
1201
1277
cx : & mut Context < ' _ , ' _ > ,
@@ -1267,7 +1343,9 @@ fn build_deps_args(
1267
1343
Ok ( ( ) )
1268
1344
}
1269
1345
1270
- /// Add custom flags from the output a of build-script to a `ProcessBuilder`
1346
+ /// Adds extra rustc flags and environment variables collected from the output
1347
+ /// of a build-script to the command to execute, include custom environment
1348
+ /// variables and `cfg`.
1271
1349
fn add_custom_flags (
1272
1350
cmd : & mut ProcessBuilder ,
1273
1351
build_script_outputs : & BuildScriptOutputs ,
@@ -1377,6 +1455,8 @@ fn envify(s: &str) -> String {
1377
1455
. collect ( )
1378
1456
}
1379
1457
1458
+ /// Configuration of the display of messages emitted by the compiler,
1459
+ /// e.g. diagnostics, warnings, errors, and message caching.
1380
1460
struct OutputOptions {
1381
1461
/// What format we're emitting from Cargo itself.
1382
1462
format : MessageFormat ,
@@ -1395,7 +1475,9 @@ struct OutputOptions {
1395
1475
/// cache will be filled with diagnostics from dependencies. When the
1396
1476
/// cache is replayed without `-vv`, we don't want to show them.
1397
1477
show_diagnostics : bool ,
1478
+ /// Tracks the number of warnings we've seen so far.
1398
1479
warnings_seen : usize ,
1480
+ /// Tracks the number of errors we've seen so far.
1399
1481
errors_seen : usize ,
1400
1482
}
1401
1483
@@ -1677,6 +1759,9 @@ fn on_stderr_line_inner(
1677
1759
Ok ( true )
1678
1760
}
1679
1761
1762
+ /// Creates a unit of work that replays the cached compiler message.
1763
+ ///
1764
+ /// Usually used when a job is fresh and doesn't need to recompile.
1680
1765
fn replay_output_cache (
1681
1766
package_id : PackageId ,
1682
1767
manifest_path : PathBuf ,
0 commit comments