Skip to content

Commit a7f58fa

Browse files
asquared31415albertlarsan68
authored andcommitted
make x.py clippy download and use beta clippy
1 parent b5e51db commit a7f58fa

File tree

4 files changed

+92
-62
lines changed

4 files changed

+92
-62
lines changed

src/bootstrap/bin/rustc.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,25 @@ fn main() {
3737
Err(_) => 0,
3838
};
3939

40+
if verbose > 1 {
41+
eprintln!("target: {target:?}");
42+
eprintln!("version: {version:?}");
43+
}
44+
4045
// Use a different compiler for build scripts, since there may not yet be a
4146
// libstd for the real compiler to use. However, if Cargo is attempting to
4247
// determine the version of the compiler, the real compiler needs to be
4348
// used. Currently, these two states are differentiated based on whether
4449
// --target and -vV is/isn't passed.
4550
let (rustc, libdir) = if target.is_none() && version.is_none() {
51+
if verbose > 1 {
52+
eprintln!("Using snapshot complier");
53+
}
4654
("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
4755
} else {
56+
if verbose > 1 {
57+
eprintln!("Using real complier");
58+
}
4859
("RUSTC_REAL", "RUSTC_LIBDIR")
4960
};
5061
let stage = env::var("RUSTC_STAGE").unwrap_or_else(|_| {
@@ -75,6 +86,10 @@ fn main() {
7586
cmd.arg("-Ztime-passes");
7687
}
7788
}
89+
90+
if crate_name == "build_script_build" {
91+
eprintln!("building build scripts using sysroot {:?}", sysroot);
92+
}
7893
}
7994

8095
// Print backtrace in case of ICE
@@ -147,6 +162,15 @@ fn main() {
147162
cmd.arg("--check-cfg=values(bootstrap)");
148163
}
149164

165+
if let Ok(command) = env::var("RUSTC_COMMAND") {
166+
if command == "clippy" && target.is_none() {
167+
let libdir_string = libdir.to_string_lossy();
168+
let (sysroot, _) = libdir_string.rsplit_once('/').unwrap();
169+
eprintln!("passing clippy --sysroot {}", sysroot);
170+
cmd.arg("--sysroot").arg(&sysroot);
171+
}
172+
}
173+
150174
if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
151175
cmd.arg("--remap-path-prefix").arg(&map);
152176
}
@@ -220,6 +244,7 @@ fn main() {
220244
env::join_paths(&dylib_path).unwrap(),
221245
cmd,
222246
);
247+
eprintln!("{} SYSROOT: {:?}", prefix, env::var("SYSROOT"));
223248
eprintln!("{} sysroot: {:?}", prefix, sysroot);
224249
eprintln!("{} libdir: {:?}", prefix, libdir);
225250
}

src/bootstrap/bootstrap.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,8 @@ def download_toolchain(self):
518518
rustc_channel = self.stage0_compiler.version
519519
bin_root = self.bin_root()
520520

521+
tarball_suffix = '.tar.gz' if lzma is None else '.tar.xz'
522+
521523
key = self.stage0_compiler.date
522524
if self.rustc().startswith(bin_root) and \
523525
(not os.path.exists(self.rustc()) or
@@ -602,22 +604,6 @@ def download_toolchain(self):
602604
with output(self.rustc_stamp()) as rust_stamp:
603605
rust_stamp.write(key)
604606

605-
def _download_component_helper(
606-
self, filename, pattern, tarball_suffix, rustc_cache,
607-
):
608-
key = self.stage0_compiler.date
609-
610-
tarball = os.path.join(rustc_cache, filename)
611-
if not os.path.exists(tarball):
612-
get(
613-
self.download_url,
614-
"dist/{}/{}".format(key, filename),
615-
tarball,
616-
self.checksums_sha256,
617-
verbose=self.verbose,
618-
)
619-
unpack(tarball, tarball_suffix, self.bin_root(), match=pattern, verbose=self.verbose)
620-
621607
def should_fix_bins_and_dylibs(self):
622608
"""Whether or not `fix_bin_or_dylib` needs to be run; can only be True
623609
on NixOS.
@@ -740,6 +726,27 @@ def rustc_stamp(self):
740726
"""
741727
return os.path.join(self.bin_root(), '.rustc-stamp')
742728

729+
def rustfmt_stamp(self):
730+
"""Return the path for .rustfmt-stamp
731+
732+
>>> rb = RustBuild()
733+
>>> rb.build_dir = "build"
734+
>>> rb.rustfmt_stamp() == os.path.join("build", "stage0", ".rustfmt-stamp")
735+
True
736+
"""
737+
return os.path.join(self.bin_root(), '.rustfmt-stamp')
738+
739+
def clippy_stamp(self):
740+
"""Return the path for .clippy-stamp
741+
742+
>>> rb = RustBuild()
743+
>>> rb.build_dir = "build"
744+
>>> rb.clippy_stamp() == os.path.join("build", "stage0", ".clippy-stamp")
745+
True
746+
"""
747+
return os.path.join(self.bin_root(), '.clippy-stamp')
748+
749+
743750
def program_out_of_date(self, stamp_path, key):
744751
"""Check if the given program stamp is out of date"""
745752
if not os.path.exists(stamp_path) or self.clean:
@@ -810,6 +817,10 @@ def rustc(self):
810817
"""Return config path for rustc"""
811818
return self.program_config('rustc')
812819

820+
def clippy(self):
821+
"""Return config path for clippy"""
822+
return self.program_config('cargo-clippy')
823+
813824
def program_config(self, program):
814825
"""Return config path for the given program at the given stage
815826

src/bootstrap/builder.rs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::process::Command;
1313
use std::time::{Duration, Instant};
1414

1515
use crate::cache::{Cache, Interned, INTERNER};
16-
use crate::config::{SplitDebuginfo, TargetSelection};
16+
use crate::config::{SplitDebuginfo, TargetSelection, DryRun};
1717
use crate::doc;
1818
use crate::flags::{Color, Subcommand};
1919
use crate::install;
@@ -1171,7 +1171,12 @@ impl<'a> Builder<'a> {
11711171
target: TargetSelection,
11721172
cmd: &str,
11731173
) -> Command {
1174-
let mut cargo = Command::new(&self.initial_cargo);
1174+
let mut cargo = if cmd == "clippy" {
1175+
Command::new(self.initial_rustc.parent().unwrap().join("cargo-clippy"))
1176+
} else {
1177+
Command::new(&self.initial_cargo)
1178+
};
1179+
11751180
// Run cargo from the source root so it can find .cargo/config.
11761181
// This matters when using vendoring and the working directory is outside the repository.
11771182
cargo.current_dir(&self.src);
@@ -1293,6 +1298,24 @@ impl<'a> Builder<'a> {
12931298
compiler.stage
12941299
};
12951300

1301+
// We synthetically interpret a stage0 compiler used to build tools as a
1302+
// "raw" compiler in that it's the exact snapshot we download. Normally
1303+
// the stage0 build means it uses libraries build by the stage0
1304+
// compiler, but for tools we just use the precompiled libraries that
1305+
// we've downloaded
1306+
let use_snapshot = mode == Mode::ToolBootstrap;
1307+
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1308+
1309+
let maybe_sysroot = self.sysroot(compiler);
1310+
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1311+
let libdir = self.rustc_libdir(compiler);
1312+
1313+
let sysroot_str = sysroot.as_os_str().to_str().expect("sysroot should be UTF-8");
1314+
if !matches!(self.config.dry_run, DryRun::SelfCheck) {
1315+
self.verbose_than(0, &format!("using sysroot {sysroot_str}"));
1316+
self.verbose_than(1, &format!("running cargo with mode {mode:?}"));
1317+
}
1318+
12961319
let mut rustflags = Rustflags::new(target);
12971320
if stage != 0 {
12981321
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
@@ -1310,35 +1333,12 @@ impl<'a> Builder<'a> {
13101333
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
13111334
// so it has no way of knowing the sysroot.
13121335
rustflags.arg("--sysroot");
1313-
rustflags.arg(
1314-
self.sysroot(compiler)
1315-
.as_os_str()
1316-
.to_str()
1317-
.expect("sysroot must be valid UTF-8"),
1318-
);
1336+
rustflags.arg(sysroot_str);
13191337
// Only run clippy on a very limited subset of crates (in particular, not build scripts).
13201338
cargo.arg("-Zunstable-options");
1321-
// Explicitly does *not* set `--cfg=bootstrap`, since we're using a nightly clippy.
1322-
let host_version = Command::new("rustc").arg("--version").output().map_err(|_| ());
1323-
let output = host_version.and_then(|output| {
1324-
if output.status.success() {
1325-
Ok(output)
1326-
} else {
1327-
Err(())
1328-
}
1329-
}).unwrap_or_else(|_| {
1330-
eprintln!(
1331-
"error: `x.py clippy` requires a host `rustc` toolchain with the `clippy` component"
1332-
);
1333-
eprintln!("help: try `rustup component add clippy`");
1334-
crate::detail_exit_macro!(1);
1335-
});
1336-
if !t!(std::str::from_utf8(&output.stdout)).contains("nightly") {
1337-
rustflags.arg("--cfg=bootstrap");
1338-
}
1339-
} else {
1340-
rustflags.arg("--cfg=bootstrap");
13411339
}
1340+
1341+
rustflags.arg("--cfg=bootstrap");
13421342
}
13431343

13441344
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
@@ -1470,6 +1470,10 @@ impl<'a> Builder<'a> {
14701470
Mode::Std | Mode::Rustc | Mode::Codegen | Mode::ToolRustc => String::new(),
14711471
};
14721472

1473+
if self.jobs() > 1 {
1474+
//panic!("TESTING: Run with one job only!");
1475+
}
1476+
14731477
cargo.arg("-j").arg(self.jobs().to_string());
14741478

14751479
// FIXME: Temporary fix for https://github.com/rust-lang/cargo/issues/3005
@@ -1520,18 +1524,6 @@ impl<'a> Builder<'a> {
15201524

15211525
let want_rustdoc = self.doc_tests != DocTests::No;
15221526

1523-
// We synthetically interpret a stage0 compiler used to build tools as a
1524-
// "raw" compiler in that it's the exact snapshot we download. Normally
1525-
// the stage0 build means it uses libraries build by the stage0
1526-
// compiler, but for tools we just use the precompiled libraries that
1527-
// we've downloaded
1528-
let use_snapshot = mode == Mode::ToolBootstrap;
1529-
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1530-
1531-
let maybe_sysroot = self.sysroot(compiler);
1532-
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1533-
let libdir = self.rustc_libdir(compiler);
1534-
15351527
// Clear the output directory if the real rustc we're using has changed;
15361528
// Cargo cannot detect this as it thinks rustc is bootstrap/debug/rustc.
15371529
//
@@ -1554,6 +1546,11 @@ impl<'a> Builder<'a> {
15541546
.env("RUSTBUILD_NATIVE_DIR", self.native_dir(target))
15551547
.env("RUSTC_REAL", self.rustc(compiler))
15561548
.env("RUSTC_STAGE", stage.to_string())
1549+
1550+
// set for clippy to know the sysroot
1551+
.env("SYSROOT", &sysroot)
1552+
.env("RUSTC_COMMAND", cmd)
1553+
15571554
.env("RUSTC_SYSROOT", &sysroot)
15581555
.env("RUSTC_LIBDIR", &libdir)
15591556
.env("RUSTDOC", self.bootstrap_out.join("rustdoc"))
@@ -1567,11 +1564,8 @@ impl<'a> Builder<'a> {
15671564
)
15681565
.env("RUSTC_ERROR_METADATA_DST", self.extended_error_dir())
15691566
.env("RUSTC_BREAK_ON_ICE", "1");
1570-
// Clippy support is a hack and uses the default `cargo-clippy` in path.
1571-
// Don't override RUSTC so that the `cargo-clippy` in path will be run.
1572-
if cmd != "clippy" {
1573-
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
1574-
}
1567+
1568+
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
15751569

15761570
// Dealing with rpath here is a little special, so let's go into some
15771571
// detail. First off, `-rpath` is a linker option on Unix platforms

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)