Skip to content

Commit 40dca3d

Browse files
authored
Rollup merge of rust-lang#103660 - ozkanonur:master, r=jyn514
improve `filesearch::get_or_default_sysroot` `fn get_or_default_sysroot` is now improved and used in `miri` and `clippy`, and tests are still passing as they should. So we no longer need to implement custom workarounds/hacks to find sysroot in tools like miri/clippy. Resolves rust-lang#98832 re-opened from rust-lang#103581
2 parents 9f2852f + 188e8e9 commit 40dca3d

File tree

1 file changed

+5
-74
lines changed

1 file changed

+5
-74
lines changed

src/driver.rs

+5-74
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use std::borrow::Cow;
2323
use std::env;
2424
use std::ops::Deref;
2525
use std::panic;
26-
use std::path::{Path, PathBuf};
27-
use std::process::{exit, Command};
26+
use std::path::Path;
27+
use std::process::exit;
2828
use std::sync::LazyLock;
2929

3030
/// If a command-line option matches `find_arg`, then apply the predicate `pred` on its value. If
@@ -210,83 +210,21 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
210210
interface::try_print_query_stack(&handler, num_frames);
211211
}
212212

213-
fn toolchain_path(home: Option<String>, toolchain: Option<String>) -> Option<PathBuf> {
214-
home.and_then(|home| {
215-
toolchain.map(|toolchain| {
216-
let mut path = PathBuf::from(home);
217-
path.push("toolchains");
218-
path.push(toolchain);
219-
path
220-
})
221-
})
222-
}
223-
224213
#[allow(clippy::too_many_lines)]
225214
pub fn main() {
226215
rustc_driver::init_rustc_env_logger();
227216
LazyLock::force(&ICE_HOOK);
228217
exit(rustc_driver::catch_with_exit_code(move || {
229218
let mut orig_args: Vec<String> = env::args().collect();
230219

231-
// Get the sysroot, looking from most specific to this invocation to the least:
232-
// - command line
233-
// - runtime environment
234-
// - SYSROOT
235-
// - RUSTUP_HOME, MULTIRUST_HOME, RUSTUP_TOOLCHAIN, MULTIRUST_TOOLCHAIN
236-
// - sysroot from rustc in the path
237-
// - compile-time environment
238-
// - SYSROOT
239-
// - RUSTUP_HOME, MULTIRUST_HOME, RUSTUP_TOOLCHAIN, MULTIRUST_TOOLCHAIN
240-
let sys_root_arg = arg_value(&orig_args, "--sysroot", |_| true);
241-
let have_sys_root_arg = sys_root_arg.is_some();
242-
let sys_root = sys_root_arg
243-
.map(PathBuf::from)
244-
.or_else(|| std::env::var("SYSROOT").ok().map(PathBuf::from))
245-
.or_else(|| {
246-
let home = std::env::var("RUSTUP_HOME")
247-
.or_else(|_| std::env::var("MULTIRUST_HOME"))
248-
.ok();
249-
let toolchain = std::env::var("RUSTUP_TOOLCHAIN")
250-
.or_else(|_| std::env::var("MULTIRUST_TOOLCHAIN"))
251-
.ok();
252-
toolchain_path(home, toolchain)
253-
})
254-
.or_else(|| {
255-
Command::new("rustc")
256-
.arg("--print")
257-
.arg("sysroot")
258-
.output()
259-
.ok()
260-
.and_then(|out| String::from_utf8(out.stdout).ok())
261-
.map(|s| PathBuf::from(s.trim()))
262-
})
263-
.or_else(|| option_env!("SYSROOT").map(PathBuf::from))
264-
.or_else(|| {
265-
let home = option_env!("RUSTUP_HOME")
266-
.or(option_env!("MULTIRUST_HOME"))
267-
.map(ToString::to_string);
268-
let toolchain = option_env!("RUSTUP_TOOLCHAIN")
269-
.or(option_env!("MULTIRUST_TOOLCHAIN"))
270-
.map(ToString::to_string);
271-
toolchain_path(home, toolchain)
272-
})
273-
.map(|pb| pb.to_string_lossy().to_string())
274-
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust");
275-
276220
// make "clippy-driver --rustc" work like a subcommand that passes further args to "rustc"
277221
// for example `clippy-driver --rustc --version` will print the rustc version that clippy-driver
278222
// uses
279223
if let Some(pos) = orig_args.iter().position(|arg| arg == "--rustc") {
280224
orig_args.remove(pos);
281225
orig_args[0] = "rustc".to_string();
282226

283-
// if we call "rustc", we need to pass --sysroot here as well
284-
let mut args: Vec<String> = orig_args.clone();
285-
if !have_sys_root_arg {
286-
args.extend(vec!["--sysroot".into(), sys_root]);
287-
};
288-
289-
return rustc_driver::RunCompiler::new(&args, &mut DefaultCallbacks).run();
227+
return rustc_driver::RunCompiler::new(&orig_args, &mut DefaultCallbacks).run();
290228
}
291229

292230
if orig_args.iter().any(|a| a == "--version" || a == "-V") {
@@ -309,14 +247,6 @@ pub fn main() {
309247
exit(0);
310248
}
311249

312-
// this conditional check for the --sysroot flag is there so users can call
313-
// `clippy_driver` directly
314-
// without having to pass --sysroot or anything
315-
let mut args: Vec<String> = orig_args.clone();
316-
if !have_sys_root_arg {
317-
args.extend(vec!["--sysroot".into(), sys_root]);
318-
};
319-
320250
let mut no_deps = false;
321251
let clippy_args_var = env::var("CLIPPY_ARGS").ok();
322252
let clippy_args = clippy_args_var
@@ -345,10 +275,11 @@ pub fn main() {
345275

346276
let clippy_enabled = !cap_lints_allow && (!no_deps || in_primary_package);
347277
if clippy_enabled {
278+
let mut args: Vec<String> = orig_args.clone();
348279
args.extend(clippy_args);
349280
rustc_driver::RunCompiler::new(&args, &mut ClippyCallbacks { clippy_args_var }).run()
350281
} else {
351-
rustc_driver::RunCompiler::new(&args, &mut RustcCallbacks { clippy_args_var }).run()
282+
rustc_driver::RunCompiler::new(&orig_args, &mut RustcCallbacks { clippy_args_var }).run()
352283
}
353284
}))
354285
}

0 commit comments

Comments
 (0)