Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1ab2f80

Browse files
asquared31415jyn514
authored andcommitted
make x.py clippy download and use beta clippy
1 parent de7239a commit 1ab2f80

File tree

4 files changed

+99
-82
lines changed

4 files changed

+99
-82
lines changed

src/bootstrap/bootstrap.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ def download_toolchain(self):
570570
("rust-std-{}".format(toolchain_suffix), "rust-std-{}".format(self.build)),
571571
("rustc-{}".format(toolchain_suffix), "rustc"),
572572
("cargo-{}".format(toolchain_suffix), "cargo"),
573+
("clippy-{}".format(toolchain_suffix), "clippy-preview"),
573574
]
574575

575576
tarballs_download_info = [
@@ -616,22 +617,6 @@ def download_toolchain(self):
616617
with output(self.rustc_stamp()) as rust_stamp:
617618
rust_stamp.write(key)
618619

619-
def _download_component_helper(
620-
self, filename, pattern, tarball_suffix, rustc_cache,
621-
):
622-
key = self.stage0_compiler.date
623-
624-
tarball = os.path.join(rustc_cache, filename)
625-
if not os.path.exists(tarball):
626-
get(
627-
self.download_url,
628-
"dist/{}/{}".format(key, filename),
629-
tarball,
630-
self.checksums_sha256,
631-
verbose=self.verbose,
632-
)
633-
unpack(tarball, tarball_suffix, self.bin_root(), match=pattern, verbose=self.verbose)
634-
635620
def should_fix_bins_and_dylibs(self):
636621
"""Whether or not `fix_bin_or_dylib` needs to be run; can only be True
637622
on NixOS or if config.toml has `build.patch-binaries-for-nix` set.

src/bootstrap/src/bin/rustc.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ fn main() {
4949
// determine the version of the compiler, the real compiler needs to be
5050
// used. Currently, these two states are differentiated based on whether
5151
// --target and -vV is/isn't passed.
52-
let (rustc, libdir) = if target.is_none() && version.is_none() {
52+
let is_build_script = target.is_none() && version.is_none();
53+
let (rustc, libdir) = if is_build_script {
5354
("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
5455
} else {
5556
("RUSTC_REAL", "RUSTC_LIBDIR")
@@ -72,7 +73,15 @@ fn main() {
7273
.unwrap_or_else(|| env::var("CFG_COMPILER_HOST_TRIPLE").unwrap());
7374
let is_clippy = args[0].to_string_lossy().ends_with(&exe("clippy-driver", &target_name));
7475
let rustc_driver = if is_clippy {
75-
args.remove(0)
76+
if is_build_script {
77+
// Don't run clippy on build scripts (for one thing, we may not have libstd built with
78+
// the appropriate version yet, e.g. for stage 1 std).
79+
// Also remove the `clippy-driver` param in addition to the RUSTC param.
80+
args.drain(..2);
81+
rustc_real
82+
} else {
83+
args.remove(0)
84+
}
7685
} else {
7786
args.remove(0);
7887
rustc_real

src/bootstrap/src/core/builder.rs

Lines changed: 86 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use crate::core::build_steps::{check, clean, compile, dist, doc, install, run, s
1818
use crate::core::config::flags::{Color, Subcommand};
1919
use crate::core::config::{DryRun, SplitDebuginfo, TargetSelection};
2020
use crate::utils::cache::{Cache, Interned, INTERNER};
21-
use crate::utils::helpers::{self, add_dylib_path, add_link_lib_path, exe, libdir, output, t};
21+
use crate::utils::helpers::{
22+
self, add_dylib_path, add_link_lib_path, dylib_path, dylib_path_var, exe, libdir, output, t,
23+
};
2224
use crate::Crate;
2325
use crate::EXTRA_CHECK_CFGS;
2426
use crate::{Build, CLang, DocTests, GitRepo, Mode};
@@ -1151,6 +1153,43 @@ impl<'a> Builder<'a> {
11511153
self.ensure(tool::Rustdoc { compiler })
11521154
}
11531155

1156+
pub fn cargo_clippy_cmd(&self, run_compiler: Compiler) -> Command {
1157+
let initial_sysroot_bin = self.initial_rustc.parent().unwrap();
1158+
// Set PATH to include the sysroot bin dir so clippy can find cargo.
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.initial_rustc.parent().unwrap().join("cargo-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 = 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(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1189+
cmd.env("PATH", path);
1190+
cmd
1191+
}
1192+
11541193
pub fn rustdoc_cmd(&self, compiler: Compiler) -> Command {
11551194
let mut cmd = Command::new(&self.bootstrap_out.join("rustdoc"));
11561195
cmd.env("RUSTC_STAGE", compiler.stage.to_string())
@@ -1201,7 +1240,12 @@ impl<'a> Builder<'a> {
12011240
target: TargetSelection,
12021241
cmd: &str,
12031242
) -> Command {
1204-
let mut cargo = Command::new(&self.initial_cargo);
1243+
let mut cargo = if cmd == "clippy" {
1244+
self.cargo_clippy_cmd(compiler)
1245+
} else {
1246+
Command::new(&self.initial_cargo)
1247+
};
1248+
12051249
// Run cargo from the source root so it can find .cargo/config.
12061250
// This matters when using vendoring and the working directory is outside the repository.
12071251
cargo.current_dir(&self.src);
@@ -1325,6 +1369,23 @@ impl<'a> Builder<'a> {
13251369
compiler.stage
13261370
};
13271371

1372+
// We synthetically interpret a stage0 compiler used to build tools as a
1373+
// "raw" compiler in that it's the exact snapshot we download. Normally
1374+
// the stage0 build means it uses libraries build by the stage0
1375+
// compiler, but for tools we just use the precompiled libraries that
1376+
// we've downloaded
1377+
let use_snapshot = mode == Mode::ToolBootstrap;
1378+
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1379+
1380+
let maybe_sysroot = self.sysroot(compiler);
1381+
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1382+
let libdir = self.rustc_libdir(compiler);
1383+
1384+
let sysroot_str = sysroot.as_os_str().to_str().expect("sysroot should be UTF-8");
1385+
if !matches!(self.config.dry_run, DryRun::SelfCheck) {
1386+
self.verbose_than(0, &format!("using sysroot {sysroot_str}"));
1387+
}
1388+
13281389
let mut rustflags = Rustflags::new(target);
13291390
if stage != 0 {
13301391
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
@@ -1336,41 +1397,18 @@ impl<'a> Builder<'a> {
13361397
cargo.args(s.split_whitespace());
13371398
}
13381399
rustflags.env("RUSTFLAGS_BOOTSTRAP");
1339-
if cmd == "clippy" {
1340-
// clippy overwrites sysroot if we pass it to cargo.
1341-
// Pass it directly to clippy instead.
1342-
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
1343-
// so it has no way of knowing the sysroot.
1344-
rustflags.arg("--sysroot");
1345-
rustflags.arg(
1346-
self.sysroot(compiler)
1347-
.as_os_str()
1348-
.to_str()
1349-
.expect("sysroot must be valid UTF-8"),
1350-
);
1351-
// Only run clippy on a very limited subset of crates (in particular, not build scripts).
1352-
cargo.arg("-Zunstable-options");
1353-
// Explicitly does *not* set `--cfg=bootstrap`, since we're using a nightly clippy.
1354-
let host_version = Command::new("rustc").arg("--version").output().map_err(|_| ());
1355-
let output = host_version.and_then(|output| {
1356-
if output.status.success() {
1357-
Ok(output)
1358-
} else {
1359-
Err(())
1360-
}
1361-
}).unwrap_or_else(|_| {
1362-
eprintln!(
1363-
"ERROR: `x.py clippy` requires a host `rustc` toolchain with the `clippy` component"
1364-
);
1365-
eprintln!("HELP: try `rustup component add clippy`");
1366-
crate::exit!(1);
1367-
});
1368-
if !t!(std::str::from_utf8(&output.stdout)).contains("nightly") {
1369-
rustflags.arg("--cfg=bootstrap");
1370-
}
1371-
} else {
1372-
rustflags.arg("--cfg=bootstrap");
1373-
}
1400+
rustflags.arg("--cfg=bootstrap");
1401+
}
1402+
1403+
if cmd == "clippy" {
1404+
// clippy overwrites sysroot if we pass it to cargo.
1405+
// Pass it directly to clippy instead.
1406+
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
1407+
// so it has no way of knowing the sysroot.
1408+
rustflags.arg("--sysroot");
1409+
rustflags.arg(sysroot_str);
1410+
// Only run clippy on a very limited subset of crates (in particular, not build scripts).
1411+
cargo.arg("-Zunstable-options");
13741412
}
13751413

13761414
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
@@ -1565,18 +1603,6 @@ impl<'a> Builder<'a> {
15651603

15661604
let want_rustdoc = self.doc_tests != DocTests::No;
15671605

1568-
// We synthetically interpret a stage0 compiler used to build tools as a
1569-
// "raw" compiler in that it's the exact snapshot we download. Normally
1570-
// the stage0 build means it uses libraries build by the stage0
1571-
// compiler, but for tools we just use the precompiled libraries that
1572-
// we've downloaded
1573-
let use_snapshot = mode == Mode::ToolBootstrap;
1574-
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1575-
1576-
let maybe_sysroot = self.sysroot(compiler);
1577-
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1578-
let libdir = self.rustc_libdir(compiler);
1579-
15801606
// Clear the output directory if the real rustc we're using has changed;
15811607
// Cargo cannot detect this as it thinks rustc is bootstrap/debug/rustc.
15821608
//
@@ -1612,20 +1638,17 @@ impl<'a> Builder<'a> {
16121638
)
16131639
.env("RUSTC_ERROR_METADATA_DST", self.extended_error_dir())
16141640
.env("RUSTC_BREAK_ON_ICE", "1");
1615-
// Clippy support is a hack and uses the default `cargo-clippy` in path.
1616-
// Don't override RUSTC so that the `cargo-clippy` in path will be run.
1617-
if cmd != "clippy" {
1618-
// Set RUSTC_WRAPPER to the bootstrap shim, which switches between beta and in-tree
1619-
// sysroot depending on whether we're building build scripts.
1620-
// NOTE: we intentionally use RUSTC_WRAPPER so that we can support clippy - RUSTC is not
1621-
// respected by clippy-driver; RUSTC_WRAPPER happens earlier, before clippy runs.
1622-
cargo.env("RUSTC_WRAPPER", self.bootstrap_out.join("rustc"));
1623-
1624-
// Someone might have set some previous rustc wrapper (e.g.
1625-
// sccache) before bootstrap overrode it. Respect that variable.
1626-
if let Some(existing_wrapper) = env::var_os("RUSTC_WRAPPER") {
1627-
cargo.env("RUSTC_WRAPPER_REAL", existing_wrapper);
1628-
}
1641+
1642+
// Set RUSTC_WRAPPER to the bootstrap shim, which switches between beta and in-tree
1643+
// sysroot depending on whether we're building build scripts.
1644+
// NOTE: we intentionally use RUSTC_WRAPPER so that we can support clippy - RUSTC is not
1645+
// respected by clippy-driver; RUSTC_WRAPPER happens earlier, before clippy runs.
1646+
cargo.env("RUSTC_WRAPPER", self.bootstrap_out.join("rustc"));
1647+
1648+
// Someone might have set some previous rustc wrapper (e.g.
1649+
// sccache) before bootstrap overrode it. Respect that variable.
1650+
if let Some(existing_wrapper) = env::var_os("RUSTC_WRAPPER") {
1651+
cargo.env("RUSTC_WRAPPER_REAL", existing_wrapper);
16291652
}
16301653

16311654
// Dealing with rpath here is a little special, so let's go into some

src/tools/bump-stage0/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use indexmap::IndexMap;
44
use std::collections::HashMap;
55

66
const PATH: &str = "src/stage0.json";
7-
const COMPILER_COMPONENTS: &[&str] = &["rustc", "rust-std", "cargo"];
7+
const COMPILER_COMPONENTS: &[&str] = &["rustc", "rust-std", "cargo", "clippy-preview"];
88
const RUSTFMT_COMPONENTS: &[&str] = &["rustfmt-preview", "rustc"];
99

1010
struct Tool {

0 commit comments

Comments
 (0)