@@ -1152,6 +1152,43 @@ impl<'a> Builder<'a> {
11521152 self . ensure ( tool:: Rustdoc { compiler } )
11531153 }
11541154
1155+ pub fn cargo_clippy_cmd ( & self , run_compiler : Compiler ) -> Command {
1156+ let initial_sysroot_bin = self . initial_rustc . parent ( ) . unwrap ( ) ;
1157+ // Set PATH to include the sysroot bin dir so clippy can find cargo.
1158+ let path = t ! ( env:: join_paths(
1159+ // The sysroot comes first in PATH to avoid using rustup's cargo.
1160+ std:: iter:: once( PathBuf :: from( initial_sysroot_bin) )
1161+ . chain( env:: split_paths( & t!( env:: var( "PATH" ) ) ) )
1162+ ) ) ;
1163+
1164+ if run_compiler. stage == 0 {
1165+ // `ensure(Clippy { stage: 0 })` *builds* clippy with stage0, it doesn't use the beta clippy.
1166+ let cargo_clippy = self . initial_rustc . parent ( ) . unwrap ( ) . join ( "cargo-clippy" ) ;
1167+ let mut cmd = Command :: new ( cargo_clippy) ;
1168+ cmd. env ( "PATH" , & path) ;
1169+ return cmd;
1170+ }
1171+
1172+ let build_compiler = self . compiler ( run_compiler. stage - 1 , self . build . build ) ;
1173+ self . ensure ( tool:: Clippy {
1174+ compiler : build_compiler,
1175+ target : self . build . build ,
1176+ extra_features : vec ! [ ] ,
1177+ } ) ;
1178+ let cargo_clippy = self . ensure ( tool:: CargoClippy {
1179+ compiler : build_compiler,
1180+ target : self . build . build ,
1181+ extra_features : vec ! [ ] ,
1182+ } ) ;
1183+ let mut dylib_path = helpers:: dylib_path ( ) ;
1184+ dylib_path. insert ( 0 , self . sysroot ( run_compiler) . join ( "lib" ) ) ;
1185+
1186+ let mut cmd = Command :: new ( cargo_clippy. unwrap ( ) ) ;
1187+ cmd. env ( helpers:: dylib_path_var ( ) , env:: join_paths ( & dylib_path) . unwrap ( ) ) ;
1188+ cmd. env ( "PATH" , path) ;
1189+ cmd
1190+ }
1191+
11551192 pub fn rustdoc_cmd ( & self , compiler : Compiler ) -> Command {
11561193 let mut cmd = Command :: new ( & self . bootstrap_out . join ( "rustdoc" ) ) ;
11571194 cmd. env ( "RUSTC_STAGE" , compiler. stage . to_string ( ) )
@@ -1200,7 +1237,12 @@ impl<'a> Builder<'a> {
12001237 target : TargetSelection ,
12011238 cmd : & str ,
12021239 ) -> Command {
1203- let mut cargo = Command :: new ( & self . initial_cargo ) ;
1240+ let mut cargo = if cmd == "clippy" {
1241+ self . cargo_clippy_cmd ( compiler)
1242+ } else {
1243+ Command :: new ( & self . initial_cargo )
1244+ } ;
1245+
12041246 // Run cargo from the source root so it can find .cargo/config.
12051247 // This matters when using vendoring and the working directory is outside the repository.
12061248 cargo. current_dir ( & self . src ) ;
@@ -1324,6 +1366,23 @@ impl<'a> Builder<'a> {
13241366 compiler. stage
13251367 } ;
13261368
1369+ // We synthetically interpret a stage0 compiler used to build tools as a
1370+ // "raw" compiler in that it's the exact snapshot we download. Normally
1371+ // the stage0 build means it uses libraries build by the stage0
1372+ // compiler, but for tools we just use the precompiled libraries that
1373+ // we've downloaded
1374+ let use_snapshot = mode == Mode :: ToolBootstrap ;
1375+ assert ! ( !use_snapshot || stage == 0 || self . local_rebuild) ;
1376+
1377+ let maybe_sysroot = self . sysroot ( compiler) ;
1378+ let sysroot = if use_snapshot { self . rustc_snapshot_sysroot ( ) } else { & maybe_sysroot } ;
1379+ let libdir = self . rustc_libdir ( compiler) ;
1380+
1381+ let sysroot_str = sysroot. as_os_str ( ) . to_str ( ) . expect ( "sysroot should be UTF-8" ) ;
1382+ if !matches ! ( self . config. dry_run, DryRun :: SelfCheck ) {
1383+ self . verbose_than ( 0 , & format ! ( "using sysroot {sysroot_str}" ) ) ;
1384+ }
1385+
13271386 let mut rustflags = Rustflags :: new ( target) ;
13281387 if stage != 0 {
13291388 if let Ok ( s) = env:: var ( "CARGOFLAGS_NOT_BOOTSTRAP" ) {
@@ -1335,41 +1394,18 @@ impl<'a> Builder<'a> {
13351394 cargo. args ( s. split_whitespace ( ) ) ;
13361395 }
13371396 rustflags. env ( "RUSTFLAGS_BOOTSTRAP" ) ;
1338- if cmd == "clippy" {
1339- // clippy overwrites sysroot if we pass it to cargo.
1340- // Pass it directly to clippy instead.
1341- // NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
1342- // so it has no way of knowing the sysroot.
1343- rustflags. arg ( "--sysroot" ) ;
1344- rustflags. arg (
1345- self . sysroot ( compiler)
1346- . as_os_str ( )
1347- . to_str ( )
1348- . expect ( "sysroot must be valid UTF-8" ) ,
1349- ) ;
1350- // Only run clippy on a very limited subset of crates (in particular, not build scripts).
1351- cargo. arg ( "-Zunstable-options" ) ;
1352- // Explicitly does *not* set `--cfg=bootstrap`, since we're using a nightly clippy.
1353- let host_version = Command :: new ( "rustc" ) . arg ( "--version" ) . output ( ) . map_err ( |_| ( ) ) ;
1354- let output = host_version. and_then ( |output| {
1355- if output. status . success ( ) {
1356- Ok ( output)
1357- } else {
1358- Err ( ( ) )
1359- }
1360- } ) . unwrap_or_else ( |_| {
1361- eprintln ! (
1362- "ERROR: `x.py clippy` requires a host `rustc` toolchain with the `clippy` component"
1363- ) ;
1364- eprintln ! ( "HELP: try `rustup component add clippy`" ) ;
1365- crate :: exit!( 1 ) ;
1366- } ) ;
1367- if !t ! ( std:: str :: from_utf8( & output. stdout) ) . contains ( "nightly" ) {
1368- rustflags. arg ( "--cfg=bootstrap" ) ;
1369- }
1370- } else {
1371- rustflags. arg ( "--cfg=bootstrap" ) ;
1372- }
1397+ rustflags. arg ( "--cfg=bootstrap" ) ;
1398+ }
1399+
1400+ if cmd == "clippy" {
1401+ // clippy overwrites sysroot if we pass it to cargo.
1402+ // Pass it directly to clippy instead.
1403+ // NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
1404+ // so it has no way of knowing the sysroot.
1405+ rustflags. arg ( "--sysroot" ) ;
1406+ rustflags. arg ( sysroot_str) ;
1407+ // Only run clippy on a very limited subset of crates (in particular, not build scripts).
1408+ cargo. arg ( "-Zunstable-options" ) ;
13731409 }
13741410
13751411 let use_new_symbol_mangling = match self . config . rust_new_symbol_mangling {
@@ -1564,18 +1600,6 @@ impl<'a> Builder<'a> {
15641600
15651601 let want_rustdoc = self . doc_tests != DocTests :: No ;
15661602
1567- // We synthetically interpret a stage0 compiler used to build tools as a
1568- // "raw" compiler in that it's the exact snapshot we download. Normally
1569- // the stage0 build means it uses libraries build by the stage0
1570- // compiler, but for tools we just use the precompiled libraries that
1571- // we've downloaded
1572- let use_snapshot = mode == Mode :: ToolBootstrap ;
1573- assert ! ( !use_snapshot || stage == 0 || self . local_rebuild) ;
1574-
1575- let maybe_sysroot = self . sysroot ( compiler) ;
1576- let sysroot = if use_snapshot { self . rustc_snapshot_sysroot ( ) } else { & maybe_sysroot } ;
1577- let libdir = self . rustc_libdir ( compiler) ;
1578-
15791603 // Clear the output directory if the real rustc we're using has changed;
15801604 // Cargo cannot detect this as it thinks rustc is bootstrap/debug/rustc.
15811605 //
@@ -1611,20 +1635,17 @@ impl<'a> Builder<'a> {
16111635 )
16121636 . env ( "RUSTC_ERROR_METADATA_DST" , self . extended_error_dir ( ) )
16131637 . env ( "RUSTC_BREAK_ON_ICE" , "1" ) ;
1614- // Clippy support is a hack and uses the default `cargo-clippy` in path.
1615- // Don't override RUSTC so that the `cargo-clippy` in path will be run.
1616- if cmd != "clippy" {
1617- // Set RUSTC_WRAPPER to the bootstrap shim, which switches between beta and in-tree
1618- // sysroot depending on whether we're building build scripts.
1619- // NOTE: we intentionally use RUSTC_WRAPPER so that we can support clippy - RUSTC is not
1620- // respected by clippy-driver; RUSTC_WRAPPER happens earlier, before clippy runs.
1621- cargo. env ( "RUSTC_WRAPPER" , self . bootstrap_out . join ( "rustc" ) ) ;
1622-
1623- // Someone might have set some previous rustc wrapper (e.g.
1624- // sccache) before bootstrap overrode it. Respect that variable.
1625- if let Some ( existing_wrapper) = env:: var_os ( "RUSTC_WRAPPER" ) {
1626- cargo. env ( "RUSTC_WRAPPER_REAL" , existing_wrapper) ;
1627- }
1638+
1639+ // Set RUSTC_WRAPPER to the bootstrap shim, which switches between beta and in-tree
1640+ // sysroot depending on whether we're building build scripts.
1641+ // NOTE: we intentionally use RUSTC_WRAPPER so that we can support clippy - RUSTC is not
1642+ // respected by clippy-driver; RUSTC_WRAPPER happens earlier, before clippy runs.
1643+ cargo. env ( "RUSTC_WRAPPER" , self . bootstrap_out . join ( "rustc" ) ) ;
1644+
1645+ // Someone might have set some previous rustc wrapper (e.g.
1646+ // sccache) before bootstrap overrode it. Respect that variable.
1647+ if let Some ( existing_wrapper) = env:: var_os ( "RUSTC_WRAPPER" ) {
1648+ cargo. env ( "RUSTC_WRAPPER_REAL" , existing_wrapper) ;
16281649 }
16291650
16301651 // Dealing with rpath here is a little special, so let's go into some
0 commit comments