Skip to content

Commit d297274

Browse files
committed
make x.py clippy download and use beta clippy
1 parent 1aabd8a commit d297274

File tree

15 files changed

+112
-59
lines changed

15 files changed

+112
-59
lines changed

Cargo.lock

+1-4
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ dependencies = [
359359
"libgit2-sys",
360360
"log",
361361
"memchr",
362-
"num_cpus",
363362
"opener",
364363
"openssl",
365364
"os_info",
@@ -471,7 +470,7 @@ dependencies = [
471470

472471
[[package]]
473472
name = "cargo-util"
474-
version = "0.1.4"
473+
version = "0.1.3"
475474
dependencies = [
476475
"anyhow",
477476
"core-foundation",
@@ -5556,8 +5555,6 @@ dependencies = [
55565555
"pretty_assertions 1.2.1",
55575556
"regex",
55585557
"rustc_version",
5559-
"serde",
5560-
"serde_json",
55615558
]
55625559

55635560
[[package]]

src/bootstrap/bin/rustc.rs

+16
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ fn main() {
3636
Err(_) => 0,
3737
};
3838

39+
if verbose > 1 {
40+
eprintln!("target: {target:?}");
41+
eprintln!("version: {version:?}");
42+
}
43+
3944
// Use a different compiler for build scripts, since there may not yet be a
4045
// libstd for the real compiler to use. However, if Cargo is attempting to
4146
// determine the version of the compiler, the real compiler needs to be
4247
// used. Currently, these two states are differentiated based on whether
4348
// --target and -vV is/isn't passed.
4449
let (rustc, libdir) = if target.is_none() && version.is_none() {
50+
if verbose > 1 {
51+
eprintln!("Using snapshot complier");
52+
}
4553
("RUSTC_SNAPSHOT", "RUSTC_SNAPSHOT_LIBDIR")
4654
} else {
55+
if verbose > 1 {
56+
eprintln!("Using real complier");
57+
}
4758
("RUSTC_REAL", "RUSTC_LIBDIR")
4859
};
4960
let stage = env::var("RUSTC_STAGE").expect("RUSTC_STAGE was not set");
@@ -70,6 +81,10 @@ fn main() {
7081
cmd.arg("-Ztime");
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
@@ -179,6 +194,7 @@ fn main() {
179194
env::join_paths(&dylib_path).unwrap(),
180195
cmd,
181196
);
197+
eprintln!("{} SYSROOT: {:?}", prefix, env::var("SYSROOT"));
182198
eprintln!("{} sysroot: {:?}", prefix, sysroot);
183199
eprintln!("{} libdir: {:?}", prefix, libdir);
184200
}

src/bootstrap/bootstrap.py

+49-1
Original file line numberDiff line numberDiff line change
@@ -454,13 +454,14 @@ def download_toolchain(self):
454454
rustc_channel = self.stage0_compiler.version
455455
bin_root = self.bin_root()
456456

457+
tarball_suffix = '.tar.xz' if support_xz() else '.tar.gz'
458+
457459
key = self.stage0_compiler.date
458460
if self.rustc().startswith(bin_root) and \
459461
(not os.path.exists(self.rustc()) or
460462
self.program_out_of_date(self.rustc_stamp(), key)):
461463
if os.path.exists(bin_root):
462464
shutil.rmtree(bin_root)
463-
tarball_suffix = '.tar.xz' if support_xz() else '.tar.gz'
464465
filename = "rust-std-{}-{}{}".format(
465466
rustc_channel, self.build, tarball_suffix)
466467
pattern = "rust-std-{}".format(self.build)
@@ -482,6 +483,28 @@ def download_toolchain(self):
482483
with output(self.rustc_stamp()) as rust_stamp:
483484
rust_stamp.write(key)
484485

486+
clippy_path = self.clippy()
487+
clippy_needs_download = not os.path.exists(clippy_path) \
488+
or self.program_out_of_date(self.clippy_stamp(), key)
489+
if clippy_path.startswith(bin_root) and clippy_needs_download:
490+
# download Clippy
491+
# the component name is clippy, but the bin containing folder name is clippy-preview
492+
filename = self._format_component_filename(
493+
"clippy",
494+
rustc_channel,
495+
self.build,
496+
tarball_suffix
497+
)
498+
self._download_component_helper(filename, "clippy-preview", tarball_suffix)
499+
self.fix_bin_or_dylib("{}/bin/clippy-driver".format(bin_root))
500+
self.fix_bin_or_dylib("{}/bin/cargo-clippy".format(bin_root))
501+
502+
with output(self.clippy_stamp()) as clippy_stamp:
503+
clippy_stamp.write(key)
504+
505+
def _format_component_filename(self, component_name, version, build, tarball_suffix):
506+
return "{}-{}-{}{}".format(component_name, version, build, tarball_suffix)
507+
485508
def _download_component_helper(
486509
self, filename, pattern, tarball_suffix,
487510
):
@@ -610,6 +633,27 @@ def rustc_stamp(self):
610633
"""
611634
return os.path.join(self.bin_root(), '.rustc-stamp')
612635

636+
def rustfmt_stamp(self):
637+
"""Return the path for .rustfmt-stamp
638+
639+
>>> rb = RustBuild()
640+
>>> rb.build_dir = "build"
641+
>>> rb.rustfmt_stamp() == os.path.join("build", "stage0", ".rustfmt-stamp")
642+
True
643+
"""
644+
return os.path.join(self.bin_root(), '.rustfmt-stamp')
645+
646+
def clippy_stamp(self):
647+
"""Return the path for .clippy-stamp
648+
649+
>>> rb = RustBuild()
650+
>>> rb.build_dir = "build"
651+
>>> rb.clippy_stamp() == os.path.join("build", "stage0", ".clippy-stamp")
652+
True
653+
"""
654+
return os.path.join(self.bin_root(), '.clippy-stamp')
655+
656+
613657
def program_out_of_date(self, stamp_path, key):
614658
"""Check if the given program stamp is out of date"""
615659
if not os.path.exists(stamp_path) or self.clean:
@@ -683,6 +727,10 @@ def rustc(self):
683727
"""Return config path for rustc"""
684728
return self.program_config('rustc')
685729

730+
def clippy(self):
731+
"""Return config path for clippy"""
732+
return self.program_config('cargo-clippy')
733+
686734
def program_config(self, program):
687735
"""Return config path for the given program at the given stage
688736

src/bootstrap/builder.rs

+35-44
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,12 @@ impl<'a> Builder<'a> {
13101310
target: TargetSelection,
13111311
cmd: &str,
13121312
) -> Cargo {
1313-
let mut cargo = Command::new(&self.initial_cargo);
1313+
let mut cargo = if cmd == "clippy" {
1314+
Command::new(self.initial_rustc.parent().unwrap().join("cargo-clippy"))
1315+
} else {
1316+
Command::new(&self.initial_cargo)
1317+
};
1318+
13141319
let out_dir = self.stage_out(compiler, mode);
13151320

13161321
// Codegen backends are not yet tracked by -Zbinary-dep-depinfo,
@@ -1391,6 +1396,22 @@ impl<'a> Builder<'a> {
13911396
compiler.stage
13921397
};
13931398

1399+
// We synthetically interpret a stage0 compiler used to build tools as a
1400+
// "raw" compiler in that it's the exact snapshot we download. Normally
1401+
// the stage0 build means it uses libraries build by the stage0
1402+
// compiler, but for tools we just use the precompiled libraries that
1403+
// we've downloaded
1404+
let use_snapshot = mode == Mode::ToolBootstrap;
1405+
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1406+
1407+
let maybe_sysroot = self.sysroot(compiler);
1408+
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1409+
let libdir = self.rustc_libdir(compiler);
1410+
1411+
let sysroot_str = sysroot.as_os_str().to_str().expect("sysroot should be UTF-8");
1412+
self.verbose_than(0, &format!("using sysroot {sysroot_str}"));
1413+
self.verbose_than(1, &format!("running cargo with mode {mode:?}"));
1414+
13941415
let mut rustflags = Rustflags::new(target);
13951416
if stage != 0 {
13961417
if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") {
@@ -1408,35 +1429,12 @@ impl<'a> Builder<'a> {
14081429
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
14091430
// so it has no way of knowing the sysroot.
14101431
rustflags.arg("--sysroot");
1411-
rustflags.arg(
1412-
self.sysroot(compiler)
1413-
.as_os_str()
1414-
.to_str()
1415-
.expect("sysroot must be valid UTF-8"),
1416-
);
1432+
rustflags.arg(sysroot_str);
14171433
// Only run clippy on a very limited subset of crates (in particular, not build scripts).
14181434
cargo.arg("-Zunstable-options");
1419-
// Explicitly does *not* set `--cfg=bootstrap`, since we're using a nightly clippy.
1420-
let host_version = Command::new("rustc").arg("--version").output().map_err(|_| ());
1421-
let output = host_version.and_then(|output| {
1422-
if output.status.success() {
1423-
Ok(output)
1424-
} else {
1425-
Err(())
1426-
}
1427-
}).unwrap_or_else(|_| {
1428-
eprintln!(
1429-
"error: `x.py clippy` requires a host `rustc` toolchain with the `clippy` component"
1430-
);
1431-
eprintln!("help: try `rustup component add clippy`");
1432-
std::process::exit(1);
1433-
});
1434-
if !t!(std::str::from_utf8(&output.stdout)).contains("nightly") {
1435-
rustflags.arg("--cfg=bootstrap");
1436-
}
1437-
} else {
1438-
rustflags.arg("--cfg=bootstrap");
14391435
}
1436+
1437+
rustflags.arg("--cfg=bootstrap");
14401438
}
14411439

14421440
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
@@ -1556,6 +1554,10 @@ impl<'a> Builder<'a> {
15561554
Mode::Std | Mode::Rustc | Mode::Codegen | Mode::ToolRustc => {}
15571555
}
15581556

1557+
if self.jobs() > 1 {
1558+
//panic!("TESTING: Run with one job only!");
1559+
}
1560+
15591561
cargo.arg("-j").arg(self.jobs().to_string());
15601562
// Remove make-related flags to ensure Cargo can correctly set things up
15611563
cargo.env_remove("MAKEFLAGS");
@@ -1609,18 +1611,6 @@ impl<'a> Builder<'a> {
16091611

16101612
let want_rustdoc = self.doc_tests != DocTests::No;
16111613

1612-
// We synthetically interpret a stage0 compiler used to build tools as a
1613-
// "raw" compiler in that it's the exact snapshot we download. Normally
1614-
// the stage0 build means it uses libraries build by the stage0
1615-
// compiler, but for tools we just use the precompiled libraries that
1616-
// we've downloaded
1617-
let use_snapshot = mode == Mode::ToolBootstrap;
1618-
assert!(!use_snapshot || stage == 0 || self.local_rebuild);
1619-
1620-
let maybe_sysroot = self.sysroot(compiler);
1621-
let sysroot = if use_snapshot { self.rustc_snapshot_sysroot() } else { &maybe_sysroot };
1622-
let libdir = self.rustc_libdir(compiler);
1623-
16241614
// Clear the output directory if the real rustc we're using has changed;
16251615
// Cargo cannot detect this as it thinks rustc is bootstrap/debug/rustc.
16261616
//
@@ -1643,6 +1633,10 @@ impl<'a> Builder<'a> {
16431633
.env("RUSTBUILD_NATIVE_DIR", self.native_dir(target))
16441634
.env("RUSTC_REAL", self.rustc(compiler))
16451635
.env("RUSTC_STAGE", stage.to_string())
1636+
1637+
// set for clippy to know the sysroot
1638+
.env("SYSROOT", &sysroot)
1639+
16461640
.env("RUSTC_SYSROOT", &sysroot)
16471641
.env("RUSTC_LIBDIR", &libdir)
16481642
.env("RUSTDOC", self.bootstrap_out.join("rustdoc"))
@@ -1656,11 +1650,8 @@ impl<'a> Builder<'a> {
16561650
)
16571651
.env("RUSTC_ERROR_METADATA_DST", self.extended_error_dir())
16581652
.env("RUSTC_BREAK_ON_ICE", "1");
1659-
// Clippy support is a hack and uses the default `cargo-clippy` in path.
1660-
// Don't override RUSTC so that the `cargo-clippy` in path will be run.
1661-
if cmd != "clippy" {
1662-
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
1663-
}
1653+
1654+
cargo.env("RUSTC", self.bootstrap_out.join("rustc"));
16641655

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

src/bootstrap/flags.rs

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub struct Flags {
8181
}
8282

8383
#[cfg_attr(test, derive(Clone))]
84+
#[derive(Debug)]
8485
pub enum Subcommand {
8586
Build {
8687
paths: Vec<PathBuf>,

src/bootstrap/setup.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{
1111
io::{self, Write},
1212
};
1313

14-
#[derive(Clone, Copy, Eq, PartialEq)]
14+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1515
pub enum Profile {
1616
Compiler,
1717
Codegen,

src/doc/book

Submodule book updated 280 files

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"];
1010

1111
struct Tool {

src/tools/cargo

Submodule cargo updated 210 files

src/tools/miri

Submodule miri updated 1165 files

src/tools/rls

Submodule rls updated 1 file

0 commit comments

Comments
 (0)