1
- //! The Cargo "compile" operation.
1
+ //! # The Cargo "compile" operation
2
2
//!
3
3
//! This module contains the entry point for starting the compilation process
4
4
//! for commands like `build`, `test`, `doc`, `rustc`, etc.
5
5
//!
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
7
7
//! rough outline is:
8
8
//!
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`] ).
11
11
//! - 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`]).
18
17
//! - Create a job queue (see `JobQueue`). The queue checks the
19
18
//! fingerprint of each `Unit` to determine if it should run or be
20
19
//! skipped.
21
20
//! - Execute the queue. Each leaf in the queue's dependency graph is
22
21
//! executed, and then removed from the graph when finished. This
23
22
//! 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
24
31
25
32
use std:: collections:: { BTreeSet , HashMap , HashSet } ;
26
33
use std:: fmt:: Write ;
@@ -50,9 +57,9 @@ use anyhow::{bail, Context as _};
50
57
51
58
/// Contains information about how a package should be compiled.
52
59
///
53
- /// Note on distinction between `CompileOptions` and `BuildConfig`:
60
+ /// Note on distinction between `CompileOptions` and [ `BuildConfig`] :
54
61
/// `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
56
63
/// of it as `CompileOptions` are high-level settings requested on the
57
64
/// command-line, and `BuildConfig` are low-level settings for actually
58
65
/// driving `rustc`.
@@ -105,15 +112,28 @@ impl CompileOptions {
105
112
}
106
113
}
107
114
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.
108
119
#[ derive( PartialEq , Eq , Debug ) ]
109
120
pub enum Packages {
121
+ /// Pacakges selected by default. Ususally means no flag provided.
110
122
Default ,
123
+ /// Opt in all packages.
124
+ ///
125
+ /// As of the time of this writing, it only works on opting in all workspace mebeer
111
126
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.
112
130
OptOut ( Vec < String > ) ,
131
+ /// A sequence of hand-picked packages that will be built. Normally done by `-p` flag.
113
132
Packages ( Vec < String > ) ,
114
133
}
115
134
116
135
impl Packages {
136
+ /// Creates a `Packages` from flags which are generally equivalent to command line flags.
117
137
pub fn from_flags ( all : bool , exclude : Vec < String > , package : Vec < String > ) -> CargoResult < Self > {
118
138
Ok ( match ( all, exclude. len ( ) , package. len ( ) ) {
119
139
( false , 0 , 0 ) => Packages :: Default ,
@@ -124,7 +144,7 @@ impl Packages {
124
144
} )
125
145
}
126
146
127
- /// Converts selected packages from a workspace to `PackageIdSpec`s.
147
+ /// Converts selected packages to [ `PackageIdSpec`] s.
128
148
pub fn to_package_id_specs ( & self , ws : & Workspace < ' _ > ) -> CargoResult < Vec < PackageIdSpec > > {
129
149
let specs = match self {
130
150
Packages :: All => ws
@@ -186,7 +206,7 @@ impl Packages {
186
206
Ok ( specs)
187
207
}
188
208
189
- /// Gets a list of selected packages from a workspace .
209
+ /// Gets a list of selected Packages .
190
210
pub fn get_packages < ' ws > ( & self , ws : & ' ws Workspace < ' _ > ) -> CargoResult < Vec < & ' ws Package > > {
191
211
let packages: Vec < _ > = match self {
192
212
Packages :: Default => ws. default_members ( ) . collect ( ) ,
@@ -232,6 +252,7 @@ impl Packages {
232
252
}
233
253
234
254
#[ derive( Debug , PartialEq , Eq ) ]
255
+ /// Indicates whether or not the library target gets included.
235
256
pub enum LibRule {
236
257
/// Include the library, fail if not present
237
258
True ,
@@ -242,18 +263,28 @@ pub enum LibRule {
242
263
}
243
264
244
265
#[ derive( Debug ) ]
266
+ /// Indicates which Cargo targets will be selected to be built.
245
267
pub enum FilterRule {
268
+ /// All included.
246
269
All ,
270
+ /// Just a subset of Cargo targets based on names given.
247
271
Just ( Vec < String > ) ,
248
272
}
249
273
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`].
250
278
#[ derive( Debug ) ]
251
279
pub enum CompileFilter {
280
+ /// The default set of Cargo targets.
252
281
Default {
253
282
/// Flag whether targets can be safely skipped when required-features are not satisfied.
254
283
required_features_filterable : bool ,
255
284
} ,
285
+ /// Only includes a subset of all Cargo targets.
256
286
Only {
287
+ /// Include all Cargo targets.
257
288
all_targets : bool ,
258
289
lib : LibRule ,
259
290
bins : FilterRule ,
@@ -263,13 +294,18 @@ pub enum CompileFilter {
263
294
} ,
264
295
}
265
296
297
+ /// Compiles!
298
+ ///
299
+ /// This uses the [`DefaultExecutor`]. To use a custom [`Executor`], see [`compile_with_exec`].
266
300
pub fn compile < ' a > ( ws : & Workspace < ' a > , options : & CompileOptions ) -> CargoResult < Compilation < ' a > > {
267
301
let exec: Arc < dyn Executor > = Arc :: new ( DefaultExecutor ) ;
268
302
compile_with_exec ( ws, options, & exec)
269
303
}
270
304
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.
273
309
pub fn compile_with_exec < ' a > (
274
310
ws : & Workspace < ' a > ,
275
311
options : & CompileOptions ,
@@ -279,6 +315,7 @@ pub fn compile_with_exec<'a>(
279
315
compile_ws ( ws, options, exec)
280
316
}
281
317
318
+ /// Like [`compile_with_exec`] but without warnings from manifest parsing.
282
319
pub fn compile_ws < ' a > (
283
320
ws : & Workspace < ' a > ,
284
321
options : & CompileOptions ,
@@ -295,6 +332,9 @@ pub fn compile_ws<'a>(
295
332
cx. compile ( exec)
296
333
}
297
334
335
+ /// Executes `rustc --print <VALUE>`.
336
+ ///
337
+ /// * `print_opt_value` is the VALUE passed through.
298
338
pub fn print < ' a > (
299
339
ws : & Workspace < ' a > ,
300
340
options : & CompileOptions ,
@@ -326,6 +366,10 @@ pub fn print<'a>(
326
366
Ok ( ( ) )
327
367
}
328
368
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).
329
373
pub fn create_bcx < ' a , ' cfg > (
330
374
ws : & ' a Workspace < ' cfg > ,
331
375
options : & ' a CompileOptions ,
0 commit comments