Skip to content

Commit 8444fa1

Browse files
asquared31415albertlarsan68
authored andcommitted
make x.py clippy download and use beta clippy
1 parent 11d96b5 commit 8444fa1

File tree

4 files changed

+115
-47
lines changed

4 files changed

+115
-47
lines changed

src/bootstrap/bin/rustc.rs

+25
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").expect("RUSTC_STAGE was not set");
@@ -70,6 +81,10 @@ fn main() {
7081
cmd.arg("-Ztime-passes");
7182
}
7283
}
84+
85+
if crate_name == "build_script_build" {
86+
eprintln!("building build scripts using sysroot {:?}", sysroot);
87+
}
7388
}
7489

7590
// Print backtrace in case of ICE
@@ -142,6 +157,15 @@ fn main() {
142157
cmd.arg("--check-cfg=values(bootstrap)");
143158
}
144159

160+
if let Ok(command) = env::var("RUSTC_COMMAND") {
161+
if command == "clippy" && target.is_none() {
162+
let libdir_string = libdir.to_string_lossy();
163+
let (sysroot, _) = libdir_string.rsplit_once('/').unwrap();
164+
eprintln!("passing clippy --sysroot {}", sysroot);
165+
cmd.arg("--sysroot").arg(&sysroot);
166+
}
167+
}
168+
145169
if let Ok(map) = env::var("RUSTC_DEBUGINFO_MAP") {
146170
cmd.arg("--remap-path-prefix").arg(&map);
147171
}
@@ -215,6 +239,7 @@ fn main() {
215239
env::join_paths(&dylib_path).unwrap(),
216240
cmd,
217241
);
242+
eprintln!("{} SYSROOT: {:?}", prefix, env::var("SYSROOT"));
218243
eprintln!("{} sysroot: {:?}", prefix, sysroot);
219244
eprintln!("{} libdir: {:?}", prefix, libdir);
220245
}

src/bootstrap/bootstrap.py

+50-1
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,14 @@ def download_toolchain(self):
420420
rustc_channel = self.stage0_compiler.version
421421
bin_root = self.bin_root()
422422

423+
tarball_suffix = '.tar.gz' if lzma is None else '.tar.xz'
424+
423425
key = self.stage0_compiler.date
424426
if self.rustc().startswith(bin_root) and \
425427
(not os.path.exists(self.rustc()) or
426428
self.program_out_of_date(self.rustc_stamp(), key)):
427429
if os.path.exists(bin_root):
428430
shutil.rmtree(bin_root)
429-
tarball_suffix = '.tar.gz' if lzma is None else '.tar.xz'
430431
filename = "rust-std-{}-{}{}".format(
431432
rustc_channel, self.build, tarball_suffix)
432433
pattern = "rust-std-{}".format(self.build)
@@ -451,6 +452,29 @@ def download_toolchain(self):
451452
with output(self.rustc_stamp()) as rust_stamp:
452453
rust_stamp.write(key)
453454

455+
clippy_path = self.clippy()
456+
clippy_needs_download = not os.path.exists(clippy_path) \
457+
or self.program_out_of_date(self.clippy_stamp(), key)
458+
if clippy_path.startswith(bin_root) and clippy_needs_download:
459+
# download Clippy
460+
# the component name is clippy, but the bin containing folder name is clippy-preview
461+
filename = self._format_component_filename(
462+
"clippy",
463+
rustc_channel,
464+
self.build,
465+
tarball_suffix
466+
)
467+
self._download_component_helper(filename, "clippy-preview", tarball_suffix)
468+
if self.should_fix_bins_and_dylibs():
469+
self.fix_bin_or_dylib("{}/bin/clippy-driver".format(bin_root))
470+
self.fix_bin_or_dylib("{}/bin/cargo-clippy".format(bin_root))
471+
472+
with output(self.clippy_stamp()) as clippy_stamp:
473+
clippy_stamp.write(key)
474+
475+
def _format_component_filename(self, component_name, version, build, tarball_suffix):
476+
return "{}-{}-{}{}".format(component_name, version, build, tarball_suffix)
477+
454478
def _download_component_helper(
455479
self, filename, pattern, tarball_suffix,
456480
):
@@ -592,6 +616,27 @@ def rustc_stamp(self):
592616
"""
593617
return os.path.join(self.bin_root(), '.rustc-stamp')
594618

619+
def rustfmt_stamp(self):
620+
"""Return the path for .rustfmt-stamp
621+
622+
>>> rb = RustBuild()
623+
>>> rb.build_dir = "build"
624+
>>> rb.rustfmt_stamp() == os.path.join("build", "stage0", ".rustfmt-stamp")
625+
True
626+
"""
627+
return os.path.join(self.bin_root(), '.rustfmt-stamp')
628+
629+
def clippy_stamp(self):
630+
"""Return the path for .clippy-stamp
631+
632+
>>> rb = RustBuild()
633+
>>> rb.build_dir = "build"
634+
>>> rb.clippy_stamp() == os.path.join("build", "stage0", ".clippy-stamp")
635+
True
636+
"""
637+
return os.path.join(self.bin_root(), '.clippy-stamp')
638+
639+
595640
def program_out_of_date(self, stamp_path, key):
596641
"""Check if the given program stamp is out of date"""
597642
if not os.path.exists(stamp_path) or self.clean:
@@ -665,6 +710,10 @@ def rustc(self):
665710
"""Return config path for rustc"""
666711
return self.program_config('rustc')
667712

713+
def clippy(self):
714+
"""Return config path for clippy"""
715+
return self.program_config('cargo-clippy')
716+
668717
def program_config(self, program):
669718
"""Return config path for the given program at the given stage
670719

src/bootstrap/builder.rs

+39-45
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::process::Command;
1212
use std::time::{Duration, Instant};
1313

1414
use crate::cache::{Cache, Interned, INTERNER};
15-
use crate::config::{SplitDebuginfo, TargetSelection};
15+
use crate::config::{SplitDebuginfo, TargetSelection, DryRun};
1616
use crate::doc;
1717
use crate::flags::{Color, Subcommand};
1818
use crate::install;
@@ -1114,7 +1114,12 @@ impl<'a> Builder<'a> {
11141114
target: TargetSelection,
11151115
cmd: &str,
11161116
) -> Command {
1117-
let mut cargo = Command::new(&self.initial_cargo);
1117+
let mut cargo = if cmd == "clippy" {
1118+
Command::new(self.initial_rustc.parent().unwrap().join("cargo-clippy"))
1119+
} else {
1120+
Command::new(&self.initial_cargo)
1121+
};
1122+
11181123
// Run cargo from the source root so it can find .cargo/config.
11191124
// This matters when using vendoring and the working directory is outside the repository.
11201125
cargo.current_dir(&self.src);
@@ -1236,6 +1241,24 @@ impl<'a> Builder<'a> {
12361241
compiler.stage
12371242
};
12381243

1244+
// We synthetically interpret a stage0 compiler used to build tools as a
1245+
// "raw" compiler in that it's the exact snapshot we download. Normally
1246+
// the stage0 build means it uses libraries build by the stage0
1247+
// compiler, but for tools we just use the precompiled libraries that
1248+
// we've downloaded
1249+
let use_snapshot = mode == Mode::ToolBootstrap;
1250+
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1251+
1252+
let maybe_sysroot = self.sysroot(compiler);
1253+
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1254+
let libdir = self.rustc_libdir(compiler);
1255+
1256+
let sysroot_str = sysroot.as_os_str().to_str().expect("sysroot should be UTF-8");
1257+
if !matches!(self.config.dry_run, DryRun::SelfCheck) {
1258+
self.verbose_than(0, &format!("using sysroot {sysroot_str}"));
1259+
self.verbose_than(1, &format!("running cargo with mode {mode:?}"));
1260+
}
1261+
12391262
let mut rustflags = Rustflags::new(target);
12401263
if stage != 0 {
12411264
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
@@ -1253,35 +1276,12 @@ impl<'a> Builder<'a> {
12531276
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
12541277
// so it has no way of knowing the sysroot.
12551278
rustflags.arg("--sysroot");
1256-
rustflags.arg(
1257-
self.sysroot(compiler)
1258-
.as_os_str()
1259-
.to_str()
1260-
.expect("sysroot must be valid UTF-8"),
1261-
);
1279+
rustflags.arg(sysroot_str);
12621280
// Only run clippy on a very limited subset of crates (in particular, not build scripts).
12631281
cargo.arg("-Zunstable-options");
1264-
// Explicitly does *not* set `--cfg=bootstrap`, since we're using a nightly clippy.
1265-
let host_version = Command::new("rustc").arg("--version").output().map_err(|_| ());
1266-
let output = host_version.and_then(|output| {
1267-
if output.status.success() {
1268-
Ok(output)
1269-
} else {
1270-
Err(())
1271-
}
1272-
}).unwrap_or_else(|_| {
1273-
eprintln!(
1274-
"error: `x.py clippy` requires a host `rustc` toolchain with the `clippy` component"
1275-
);
1276-
eprintln!("help: try `rustup component add clippy`");
1277-
crate::detail_exit(1);
1278-
});
1279-
if !t!(std::str::from_utf8(&output.stdout)).contains("nightly") {
1280-
rustflags.arg("--cfg=bootstrap");
1281-
}
1282-
} else {
1283-
rustflags.arg("--cfg=bootstrap");
12841282
}
1283+
1284+
rustflags.arg("--cfg=bootstrap");
12851285
}
12861286

12871287
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
@@ -1405,6 +1405,10 @@ impl<'a> Builder<'a> {
14051405
Mode::Std | Mode::Rustc | Mode::Codegen | Mode::ToolRustc => String::new(),
14061406
};
14071407

1408+
if self.jobs() > 1 {
1409+
//panic!("TESTING: Run with one job only!");
1410+
}
1411+
14081412
cargo.arg("-j").arg(self.jobs().to_string());
14091413

14101414
// FIXME: Temporary fix for https://github.com/rust-lang/cargo/issues/3005
@@ -1455,18 +1459,6 @@ impl<'a> Builder<'a> {
14551459

14561460
let want_rustdoc = self.doc_tests != DocTests::No;
14571461

1458-
// We synthetically interpret a stage0 compiler used to build tools as a
1459-
// "raw" compiler in that it's the exact snapshot we download. Normally
1460-
// the stage0 build means it uses libraries build by the stage0
1461-
// compiler, but for tools we just use the precompiled libraries that
1462-
// we've downloaded
1463-
let use_snapshot = mode == Mode::ToolBootstrap;
1464-
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1465-
1466-
let maybe_sysroot = self.sysroot(compiler);
1467-
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1468-
let libdir = self.rustc_libdir(compiler);
1469-
14701462
// Clear the output directory if the real rustc we're using has changed;
14711463
// Cargo cannot detect this as it thinks rustc is bootstrap/debug/rustc.
14721464
//
@@ -1489,6 +1481,11 @@ impl<'a> Builder<'a> {
14891481
.env("RUSTBUILD_NATIVE_DIR", self.native_dir(target))
14901482
.env("RUSTC_REAL", self.rustc(compiler))
14911483
.env("RUSTC_STAGE", stage.to_string())
1484+
1485+
// set for clippy to know the sysroot
1486+
.env("SYSROOT", &sysroot)
1487+
.env("RUSTC_COMMAND", cmd)
1488+
14921489
.env("RUSTC_SYSROOT", &sysroot)
14931490
.env("RUSTC_LIBDIR", &libdir)
14941491
.env("RUSTDOC", self.bootstrap_out.join("rustdoc"))
@@ -1502,11 +1499,8 @@ impl<'a> Builder<'a> {
15021499
)
15031500
.env("RUSTC_ERROR_METADATA_DST", self.extended_error_dir())
15041501
.env("RUSTC_BREAK_ON_ICE", "1");
1505-
// Clippy support is a hack and uses the default `cargo-clippy` in path.
1506-
// Don't override RUSTC so that the `cargo-clippy` in path will be run.
1507-
if cmd != "clippy" {
1508-
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
1509-
}
1502+
1503+
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
15101504

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::HashMap;
55
use std::convert::TryInto;
66

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

1111
struct Tool {

0 commit comments

Comments
 (0)