@@ -12,16 +12,6 @@ use crate::core::builder::{
1212use crate :: core:: config:: TargetSelection ;
1313use crate :: { Compiler , Mode , Subcommand } ;
1414
15- pub fn cargo_subcommand ( kind : Kind ) -> & ' static str {
16- match kind {
17- Kind :: Check
18- // We ensure check steps for both std and rustc from build_steps/clippy, so handle `Kind::Clippy` as well.
19- | Kind :: Clippy => "check" ,
20- Kind :: Fix => "fix" ,
21- _ => unreachable ! ( ) ,
22- }
23- }
24-
2515#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
2616pub struct Std {
2717 pub target : TargetSelection ,
@@ -31,11 +21,22 @@ pub struct Std {
3121 ///
3222 /// [`compile::Rustc`]: crate::core::build_steps::compile::Rustc
3323 crates : Vec < String > ,
24+ /// Override `Builder::kind` on cargo invocations.
25+ ///
26+ /// By default, `Builder::kind` is propagated as the subcommand to the cargo invocations.
27+ /// However, there are cases when this is not desirable. For example, when running `x clippy $tool_name`,
28+ /// passing `Builder::kind` to cargo invocations would run clippy on the entire compiler and library,
29+ /// which is not useful if we only want to lint a few crates with specific rules.
30+ override_build_kind : Option < Kind > ,
3431}
3532
3633impl Std {
3734 pub fn new ( target : TargetSelection ) -> Self {
38- Self { target, crates : vec ! [ ] }
35+ Self :: new_with_build_kind ( target, None )
36+ }
37+
38+ pub fn new_with_build_kind ( target : TargetSelection , kind : Option < Kind > ) -> Self {
39+ Self { target, crates : vec ! [ ] , override_build_kind : kind }
3940 }
4041}
4142
@@ -49,7 +50,7 @@ impl Step for Std {
4950
5051 fn make_run ( run : RunConfig < ' _ > ) {
5152 let crates = run. make_run_crates ( Alias :: Library ) ;
52- run. builder . ensure ( Std { target : run. target , crates } ) ;
53+ run. builder . ensure ( Std { target : run. target , crates, override_build_kind : None } ) ;
5354 }
5455
5556 fn run ( self , builder : & Builder < ' _ > ) {
@@ -64,7 +65,7 @@ impl Step for Std {
6465 Mode :: Std ,
6566 SourceType :: InTree ,
6667 target,
67- cargo_subcommand ( builder. kind ) ,
68+ self . override_build_kind . unwrap_or ( builder. kind ) ,
6869 ) ;
6970
7071 std_cargo ( builder, target, compiler. stage , & mut cargo) ;
@@ -118,7 +119,7 @@ impl Step for Std {
118119 Mode :: Std ,
119120 SourceType :: InTree ,
120121 target,
121- cargo_subcommand ( builder. kind ) ,
122+ self . override_build_kind . unwrap_or ( builder. kind ) ,
122123 ) ;
123124
124125 // If we're not in stage 0, tests and examples will fail to compile
@@ -159,16 +160,31 @@ pub struct Rustc {
159160 ///
160161 /// [`compile::Rustc`]: crate::core::build_steps::compile::Rustc
161162 crates : Vec < String > ,
163+ /// Override `Builder::kind` on cargo invocations.
164+ ///
165+ /// By default, `Builder::kind` is propagated as the subcommand to the cargo invocations.
166+ /// However, there are cases when this is not desirable. For example, when running `x clippy $tool_name`,
167+ /// passing `Builder::kind` to cargo invocations would run clippy on the entire compiler and library,
168+ /// which is not useful if we only want to lint a few crates with specific rules.
169+ override_build_kind : Option < Kind > ,
162170}
163171
164172impl Rustc {
165173 pub fn new ( target : TargetSelection , builder : & Builder < ' _ > ) -> Self {
174+ Self :: new_with_build_kind ( target, builder, None )
175+ }
176+
177+ pub fn new_with_build_kind (
178+ target : TargetSelection ,
179+ builder : & Builder < ' _ > ,
180+ kind : Option < Kind > ,
181+ ) -> Self {
166182 let crates = builder
167183 . in_tree_crates ( "rustc-main" , Some ( target) )
168184 . into_iter ( )
169185 . map ( |krate| krate. name . to_string ( ) )
170186 . collect ( ) ;
171- Self { target, crates }
187+ Self { target, crates, override_build_kind : kind }
172188 }
173189}
174190
@@ -183,7 +199,7 @@ impl Step for Rustc {
183199
184200 fn make_run ( run : RunConfig < ' _ > ) {
185201 let crates = run. make_run_crates ( Alias :: Compiler ) ;
186- run. builder . ensure ( Rustc { target : run. target , crates } ) ;
202+ run. builder . ensure ( Rustc { target : run. target , crates, override_build_kind : None } ) ;
187203 }
188204
189205 /// Builds the compiler.
@@ -204,7 +220,7 @@ impl Step for Rustc {
204220 builder. ensure ( crate :: core:: build_steps:: compile:: Std :: new ( compiler, compiler. host ) ) ;
205221 builder. ensure ( crate :: core:: build_steps:: compile:: Std :: new ( compiler, target) ) ;
206222 } else {
207- builder. ensure ( Std :: new ( target) ) ;
223+ builder. ensure ( Std :: new_with_build_kind ( target, self . override_build_kind ) ) ;
208224 }
209225
210226 let mut cargo = builder:: Cargo :: new (
@@ -213,7 +229,7 @@ impl Step for Rustc {
213229 Mode :: Rustc ,
214230 SourceType :: InTree ,
215231 target,
216- cargo_subcommand ( builder. kind ) ,
232+ self . override_build_kind . unwrap_or ( builder. kind ) ,
217233 ) ;
218234
219235 rustc_cargo ( builder, & mut cargo, target, & compiler) ;
@@ -291,7 +307,7 @@ impl Step for CodegenBackend {
291307 Mode :: Codegen ,
292308 SourceType :: InTree ,
293309 target,
294- cargo_subcommand ( builder. kind ) ,
310+ builder. kind ,
295311 ) ;
296312
297313 cargo
@@ -349,7 +365,7 @@ impl Step for RustAnalyzer {
349365 compiler,
350366 Mode :: ToolRustc ,
351367 target,
352- cargo_subcommand ( builder. kind ) ,
368+ builder. kind ,
353369 "src/tools/rust-analyzer" ,
354370 SourceType :: InTree ,
355371 & [ "in-rust-tree" . to_owned ( ) ] ,
@@ -417,7 +433,7 @@ macro_rules! tool_check_step {
417433 compiler,
418434 Mode :: ToolRustc ,
419435 target,
420- cargo_subcommand ( builder. kind) ,
436+ builder. kind,
421437 $path,
422438 $source_type,
423439 & [ ] ,
0 commit comments