Skip to content

Commit 188e8e9

Browse files
committed
improve filesearch::get_or_default_sysroot r=ozkanonur
Signed-off-by: Onur Özkan <[email protected]>
1 parent 6e6a803 commit 188e8e9

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
@@ -209,83 +209,21 @@ fn report_clippy_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
209209
interface::try_print_query_stack(&handler, num_frames);
210210
}
211211

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

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

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

291229
if orig_args.iter().any(|a| a == "--version" || a == "-V") {
@@ -308,14 +246,6 @@ pub fn main() {
308246
exit(0);
309247
}
310248

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

345275
let clippy_enabled = !cap_lints_allow && (!no_deps || in_primary_package);
346276
if clippy_enabled {
277+
let mut args: Vec<String> = orig_args.clone();
347278
args.extend(clippy_args);
348279
rustc_driver::RunCompiler::new(&args, &mut ClippyCallbacks { clippy_args_var }).run()
349280
} else {
350-
rustc_driver::RunCompiler::new(&args, &mut RustcCallbacks { clippy_args_var }).run()
281+
rustc_driver::RunCompiler::new(&orig_args, &mut RustcCallbacks { clippy_args_var }).run()
351282
}
352283
}))
353284
}

0 commit comments

Comments
 (0)