@@ -1152,6 +1152,44 @@ 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+ // FIXME: once rust-clippy#11944 lands on beta, set `CARGO` directly instead.
1159+ let path = t ! ( env:: join_paths(
1160+ // The sysroot comes first in PATH to avoid using rustup's cargo.
1161+ std:: iter:: once( PathBuf :: from( initial_sysroot_bin) )
1162+ . chain( env:: split_paths( & t!( env:: var( "PATH" ) ) ) )
1163+ ) ) ;
1164+
1165+ if run_compiler. stage == 0 {
1166+ // `ensure(Clippy { stage: 0 })` *builds* clippy with stage0, it doesn't use the beta clippy.
1167+ let cargo_clippy = self . build . config . download_clippy ( ) ;
1168+ let mut cmd = Command :: new ( cargo_clippy) ;
1169+ cmd. env ( "PATH" , & path) ;
1170+ return cmd;
1171+ }
1172+
1173+ let build_compiler = self . compiler ( run_compiler. stage - 1 , self . build . build ) ;
1174+ self . ensure ( tool:: Clippy {
1175+ compiler : build_compiler,
1176+ target : self . build . build ,
1177+ extra_features : vec ! [ ] ,
1178+ } ) ;
1179+ let cargo_clippy = self . ensure ( tool:: CargoClippy {
1180+ compiler : build_compiler,
1181+ target : self . build . build ,
1182+ extra_features : vec ! [ ] ,
1183+ } ) ;
1184+ let mut dylib_path = helpers:: dylib_path ( ) ;
1185+ dylib_path. insert ( 0 , self . sysroot ( run_compiler) . join ( "lib" ) ) ;
1186+
1187+ let mut cmd = Command :: new ( cargo_clippy. unwrap ( ) ) ;
1188+ cmd. env ( helpers:: dylib_path_var ( ) , env:: join_paths ( & dylib_path) . unwrap ( ) ) ;
1189+ cmd. env ( "PATH" , path) ;
1190+ cmd
1191+ }
1192+
11551193 pub fn rustdoc_cmd ( & self , compiler : Compiler ) -> Command {
11561194 let mut cmd = Command :: new ( & self . bootstrap_out . join ( "rustdoc" ) ) ;
11571195 cmd. env ( "RUSTC_STAGE" , compiler. stage . to_string ( ) )
@@ -1200,7 +1238,12 @@ impl<'a> Builder<'a> {
12001238 target : TargetSelection ,
12011239 cmd : & str ,
12021240 ) -> Command {
1203- let mut cargo = Command :: new ( & self . initial_cargo ) ;
1241+ let mut cargo = if cmd == "clippy" {
1242+ self . cargo_clippy_cmd ( compiler)
1243+ } else {
1244+ Command :: new ( & self . initial_cargo )
1245+ } ;
1246+
12041247 // Run cargo from the source root so it can find .cargo/config.
12051248 // This matters when using vendoring and the working directory is outside the repository.
12061249 cargo. current_dir ( & self . src ) ;
@@ -1324,6 +1367,23 @@ impl<'a> Builder<'a> {
13241367 compiler. stage
13251368 } ;
13261369
1370+ // We synthetically interpret a stage0 compiler used to build tools as a
1371+ // "raw" compiler in that it's the exact snapshot we download. Normally
1372+ // the stage0 build means it uses libraries build by the stage0
1373+ // compiler, but for tools we just use the precompiled libraries that
1374+ // we've downloaded
1375+ let use_snapshot = mode == Mode :: ToolBootstrap ;
1376+ assert ! ( !use_snapshot || stage == 0 || self . local_rebuild) ;
1377+
1378+ let maybe_sysroot = self . sysroot ( compiler) ;
1379+ let sysroot = if use_snapshot { self . rustc_snapshot_sysroot ( ) } else { & maybe_sysroot } ;
1380+ let libdir = self . rustc_libdir ( compiler) ;
1381+
1382+ let sysroot_str = sysroot. as_os_str ( ) . to_str ( ) . expect ( "sysroot should be UTF-8" ) ;
1383+ if !matches ! ( self . config. dry_run, DryRun :: SelfCheck ) {
1384+ self . verbose_than ( 0 , & format ! ( "using sysroot {sysroot_str}" ) ) ;
1385+ }
1386+
13271387 let mut rustflags = Rustflags :: new ( target) ;
13281388 if stage != 0 {
13291389 if let Ok ( s) = env:: var ( "CARGOFLAGS_NOT_BOOTSTRAP" ) {
@@ -1335,41 +1395,16 @@ impl<'a> Builder<'a> {
13351395 cargo. args ( s. split_whitespace ( ) ) ;
13361396 }
13371397 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- }
1398+ rustflags. arg ( "--cfg=bootstrap" ) ;
1399+ }
1400+
1401+ if cmd == "clippy" {
1402+ // clippy overwrites sysroot if we pass it to cargo.
1403+ // Pass it directly to clippy instead.
1404+ // NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
1405+ // so it has no way of knowing the sysroot.
1406+ rustflags. arg ( "--sysroot" ) ;
1407+ rustflags. arg ( sysroot_str) ;
13731408 }
13741409
13751410 let use_new_symbol_mangling = match self . config . rust_new_symbol_mangling {
@@ -1564,18 +1599,6 @@ impl<'a> Builder<'a> {
15641599
15651600 let want_rustdoc = self . doc_tests != DocTests :: No ;
15661601
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-
15791602 // Clear the output directory if the real rustc we're using has changed;
15801603 // Cargo cannot detect this as it thinks rustc is bootstrap/debug/rustc.
15811604 //
@@ -1611,10 +1634,19 @@ impl<'a> Builder<'a> {
16111634 )
16121635 . env ( "RUSTC_ERROR_METADATA_DST" , self . extended_error_dir ( ) )
16131636 . 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- cargo. env ( "RUSTC" , self . bootstrap_out . join ( "rustc" ) ) ;
1637+
1638+ // Set RUSTC_WRAPPER to the bootstrap shim, which switches between beta and in-tree
1639+ // sysroot depending on whether we're building build scripts.
1640+ // NOTE: we intentionally use RUSTC_WRAPPER so that we can support clippy - RUSTC is not
1641+ // respected by clippy-driver; RUSTC_WRAPPER happens earlier, before clippy runs.
1642+ cargo. env ( "RUSTC_WRAPPER" , self . bootstrap_out . join ( "rustc" ) ) ;
1643+ // NOTE: we also need to set RUSTC so cargo can run `rustc -vV`; apparently that ignores RUSTC_WRAPPER >:(
1644+ cargo. env ( "RUSTC" , self . bootstrap_out . join ( "rustc" ) ) ;
1645+
1646+ // Someone might have set some previous rustc wrapper (e.g.
1647+ // sccache) before bootstrap overrode it. Respect that variable.
1648+ if let Some ( existing_wrapper) = env:: var_os ( "RUSTC_WRAPPER" ) {
1649+ cargo. env ( "RUSTC_WRAPPER_REAL" , existing_wrapper) ;
16181650 }
16191651
16201652 // Dealing with rpath here is a little special, so let's go into some
@@ -1991,7 +2023,11 @@ impl<'a> Builder<'a> {
19912023 // Environment variables *required* throughout the build
19922024 //
19932025 // FIXME: should update code to not require this env var
2026+
2027+ // The host this new compiler will *run* on.
19942028 cargo. env ( "CFG_COMPILER_HOST_TRIPLE" , target. triple ) ;
2029+ // The host this new compiler is being *built* on.
2030+ cargo. env ( "CFG_COMPILER_BUILD_TRIPLE" , compiler. host . triple ) ;
19952031
19962032 // Set this for all builds to make sure doc builds also get it.
19972033 cargo. env ( "CFG_RELEASE_CHANNEL" , & self . config . channel ) ;
0 commit comments