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
2532use  std:: collections:: { BTreeSet ,  HashMap ,  HashSet } ; 
2633use  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 ) ]  
109120pub  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 members. 
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
116135impl  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 [`Package`]s . 
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. 
235256pub  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. 
245267pub  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 ) ]  
251279pub  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`]. 
266300pub  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. 
273309pub  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. 
282319pub  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. 
298338pub  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). 
329373pub  fn  create_bcx < ' a ,  ' cfg > ( 
330374    ws :  & ' a  Workspace < ' cfg > , 
331375    options :  & ' a  CompileOptions , 
0 commit comments