Skip to content

Commit 11756f7

Browse files
jyn514albertlarsan68
authored andcommitted
[wip] working!!! for --stage 1 at least
1 parent 8444fa1 commit 11756f7

File tree

4 files changed

+105
-40
lines changed

4 files changed

+105
-40
lines changed

src/bootstrap/bin/rustc.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn main() {
8282
}
8383
}
8484

85-
if crate_name == "build_script_build" {
85+
if crate_name == "build_script_build" && verbose > 0 {
8686
eprintln!("building build scripts using sysroot {:?}", sysroot);
8787
}
8888
}
@@ -161,8 +161,10 @@ fn main() {
161161
if command == "clippy" && target.is_none() {
162162
let libdir_string = libdir.to_string_lossy();
163163
let (sysroot, _) = libdir_string.rsplit_once('/').unwrap();
164-
eprintln!("passing clippy --sysroot {}", sysroot);
165-
cmd.arg("--sysroot").arg(&sysroot);
164+
if !args.iter().any(|arg| arg == "--sysroot") {
165+
eprintln!("passing clippy --sysroot {}", sysroot);
166+
cmd.arg("--sysroot").arg(&sysroot);
167+
}
166168
}
167169
}
168170

src/bootstrap/bootstrap.py

-10
Original file line numberDiff line numberDiff line change
@@ -616,16 +616,6 @@ def rustc_stamp(self):
616616
"""
617617
return os.path.join(self.bin_root(), '.rustc-stamp')
618618

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-
629619
def clippy_stamp(self):
630620
"""Return the path for .clippy-stamp
631621

src/bootstrap/builder.rs

+76-22
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, DryRun};
15+
use crate::config::{DryRun, SplitDebuginfo, TargetSelection};
1616
use crate::doc;
1717
use crate::flags::{Color, Subcommand};
1818
use crate::install;
@@ -21,7 +21,9 @@ use crate::run;
2121
use crate::setup;
2222
use crate::test;
2323
use crate::tool::{self, SourceType};
24-
use crate::util::{self, add_dylib_path, add_link_lib_path, exe, libdir, output, t};
24+
use crate::util::{
25+
self, add_dylib_path, add_link_lib_path, dylib_path, dylib_path_var, exe, libdir, output, t,
26+
};
2527
use crate::EXTRA_CHECK_CFGS;
2628
use crate::{check, compile, Crate};
2729
use crate::{clean, dist};
@@ -1061,6 +1063,44 @@ impl<'a> Builder<'a> {
10611063
self.ensure(tool::Rustdoc { compiler })
10621064
}
10631065

1066+
pub fn cargo_clippy_cmd(&self, run_compiler: Compiler) -> Command {
1067+
let initial_sysroot_bin = self.initial_rustc.parent().unwrap();
1068+
// Set PATH to include the sysroot bin dir so clippy can find cargo.
1069+
let path = t!(env::join_paths(
1070+
// The sysroot comes first in PATH to avoid using rustup's cargo.
1071+
std::iter::once(PathBuf::from(initial_sysroot_bin))
1072+
.chain(env::split_paths(&t!(env::var("PATH"))))
1073+
));
1074+
1075+
if run_compiler.stage == 0 {
1076+
// `ensure(Clippy { stage: 0 })` *builds* clippy with stage0, it doesn't use the beta clippy.
1077+
let cargo_clippy = self.initial_rustc.parent().unwrap().join("cargo-clippy");
1078+
let mut cmd = Command::new(cargo_clippy);
1079+
cmd.env("PATH", &path);
1080+
return cmd;
1081+
}
1082+
1083+
let build_compiler = self.compiler(run_compiler.stage - 1, self.build.build);
1084+
self.ensure(tool::Clippy {
1085+
compiler: build_compiler,
1086+
target: self.build.build,
1087+
extra_features: vec![],
1088+
});
1089+
let cargo_clippy = self.ensure(tool::CargoClippy {
1090+
compiler: build_compiler,
1091+
target: self.build.build,
1092+
extra_features: vec![],
1093+
});
1094+
let mut dylib_path = dylib_path();
1095+
let run_compiler = self.compiler(build_compiler.stage + 1, self.build.build);
1096+
dylib_path.insert(0, self.sysroot(run_compiler).join("lib"));
1097+
1098+
let mut cmd = Command::new(cargo_clippy.unwrap());
1099+
cmd.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap());
1100+
cmd.env("PATH", path);
1101+
cmd
1102+
}
1103+
10641104
pub fn rustdoc_cmd(&self, compiler: Compiler) -> Command {
10651105
let mut cmd = Command::new(&self.bootstrap_out.join("rustdoc"));
10661106
cmd.env("RUSTC_STAGE", compiler.stage.to_string())
@@ -1115,7 +1155,7 @@ impl<'a> Builder<'a> {
11151155
cmd: &str,
11161156
) -> Command {
11171157
let mut cargo = if cmd == "clippy" {
1118-
Command::new(self.initial_rustc.parent().unwrap().join("cargo-clippy"))
1158+
self.cargo_clippy_cmd(compiler)
11191159
} else {
11201160
Command::new(&self.initial_cargo)
11211161
};
@@ -1270,20 +1310,20 @@ impl<'a> Builder<'a> {
12701310
cargo.args(s.split_whitespace());
12711311
}
12721312
rustflags.env("RUSTFLAGS_BOOTSTRAP");
1273-
if cmd == "clippy" {
1274-
// clippy overwrites sysroot if we pass it to cargo.
1275-
// Pass it directly to clippy instead.
1276-
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
1277-
// so it has no way of knowing the sysroot.
1278-
rustflags.arg("--sysroot");
1279-
rustflags.arg(sysroot_str);
1280-
// Only run clippy on a very limited subset of crates (in particular, not build scripts).
1281-
cargo.arg("-Zunstable-options");
1282-
}
1283-
12841313
rustflags.arg("--cfg=bootstrap");
12851314
}
12861315

1316+
if cmd == "clippy" {
1317+
// clippy overwrites sysroot if we pass it to cargo.
1318+
// Pass it directly to clippy instead.
1319+
// NOTE: this can't be fixed in clippy because we explicitly don't set `RUSTC`,
1320+
// so it has no way of knowing the sysroot.
1321+
rustflags.arg("--sysroot");
1322+
rustflags.arg(sysroot_str);
1323+
// Only run clippy on a very limited subset of crates (in particular, not build scripts).
1324+
cargo.arg("-Zunstable-options");
1325+
}
1326+
12871327
let use_new_symbol_mangling = match self.config.rust_new_symbol_mangling {
12881328
Some(setting) => {
12891329
// If an explicit setting is given, use that
@@ -1405,10 +1445,6 @@ impl<'a> Builder<'a> {
14051445
Mode::Std | Mode::Rustc | Mode::Codegen | Mode::ToolRustc => String::new(),
14061446
};
14071447

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

14141450
// FIXME: Temporary fix for https://github.com/rust-lang/cargo/issues/3005
@@ -1471,6 +1507,28 @@ impl<'a> Builder<'a> {
14711507
self.clear_if_dirty(&out_dir, &self.rustc(compiler));
14721508
}
14731509

1510+
// // Cargo doesn't pass `--sysroot` for build scripts and proc-macros, which is exactly when we want to use a different sysroot.
1511+
// if
1512+
1513+
if cmd == "clippy" {
1514+
let build_script_sysroot = if stage != 0 {
1515+
// Our fake rustc shim passes `-Zmark-unstable-if-unmarked` for stage != 0, which we can't
1516+
// replicate because clippy doesn't normally run the shim. We should talk with the clippy
1517+
// team about whether there's a way to do this; maybe cargo-clippy can invoke the shim
1518+
// which invokes clippy-driver?
1519+
cargo.env("RUSTC_CLIPPY_IGNORE_BUILD_SCRIPTS_AND_PROC_MACROS", "1");
1520+
self.initial_rustc.ancestors().nth(2).unwrap()
1521+
} else {
1522+
sysroot.clone()
1523+
};
1524+
// HACK: clippy will pass `--sysroot` to `RunCompiler` if and only if SYSROOT is set and
1525+
// `--sysroot is not already passed. We bypass clippy-driver altogether in stage 1
1526+
// because there's no way to set `-Zforce-unstable-if-unmarked` for only the correct
1527+
// crates, but for stage 0 build scripts and proc-macros we want to still set the right
1528+
// sysroot.
1529+
cargo.env("SYSROOT", build_script_sysroot);
1530+
}
1531+
14741532
// Customize the compiler we're running. Specify the compiler to cargo
14751533
// as our shim and then pass it some various options used to configure
14761534
// how the actual compiler itself is called.
@@ -1481,11 +1539,7 @@ impl<'a> Builder<'a> {
14811539
.env("RUSTBUILD_NATIVE_DIR", self.native_dir(target))
14821540
.env("RUSTC_REAL", self.rustc(compiler))
14831541
.env("RUSTC_STAGE", stage.to_string())
1484-
1485-
// set for clippy to know the sysroot
1486-
.env("SYSROOT", &sysroot)
14871542
.env("RUSTC_COMMAND", cmd)
1488-
14891543
.env("RUSTC_SYSROOT", &sysroot)
14901544
.env("RUSTC_LIBDIR", &libdir)
14911545
.env("RUSTDOC", self.bootstrap_out.join("rustdoc"))

src/tools/clippy/src/driver.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::env;
2525
use std::ops::Deref;
2626
use std::panic;
2727
use std::path::Path;
28-
use std::process::exit;
28+
use std::process::{Command, exit};
2929
use std::sync::LazyLock;
3030

3131
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
@@ -290,17 +290,36 @@ pub fn main() {
290290
// We're invoking the compiler programmatically, so we ignore this/
291291
let wrapper_mode = orig_args.get(1).map(Path::new).and_then(Path::file_stem) == Some("rustc".as_ref());
292292

293-
if wrapper_mode {
294-
// we still want to be able to invoke it normally though
295-
orig_args.remove(1);
296-
}
293+
// we still want to be able to invoke it normally though
294+
let orig_rustc_cmd = if wrapper_mode {
295+
Some(orig_args.remove(1))
296+
} else {
297+
None
298+
};
297299

298300
if !wrapper_mode && (orig_args.iter().any(|a| a == "--help" || a == "-h") || orig_args.len() == 1) {
299301
display_help();
300302
exit(0);
301303
}
302304

303305
let mut args: Vec<String> = orig_args.clone();
306+
307+
let is_build_script = arg_value(&orig_args, "--crate-name", |val| val == "build_script_build").is_some();
308+
let is_proc_macro = arg_value(&orig_args, "--crate-type", |val| val == "proc-macro").is_some();
309+
let ignore_crate_completely = (is_build_script || is_proc_macro) && env::var("RUSTC_CLIPPY_IGNORE_BUILD_SCRIPTS_AND_PROC_MACROS").map_or(false, |x| x == "1");
310+
311+
// This is a more extreme version of `clippy_enabled`: it launches the original `rustc`
312+
// command (which may be a `RUSTC` wrapper, not rustc itself) rather than running
313+
// clippy-driver.
314+
if ignore_crate_completely {
315+
if let Some(orig_cmd) = orig_rustc_cmd {
316+
// Respect RUSTC shim.
317+
args.remove(0);
318+
let status = Command::new(orig_cmd).args(args).status();
319+
exit(status.ok().and_then(|status| status.code()).unwrap_or(1));
320+
}
321+
}
322+
304323
pass_sysroot_env_if_given(&mut args, sys_root_env);
305324

306325
let mut no_deps = false;

0 commit comments

Comments
 (0)