Skip to content

Commit 571d4cc

Browse files
committed
Add sysroot getting code to tests.
1 parent 69813d6 commit 571d4cc

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

tests/compile-test.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::ffi::OsStr;
1717
use std::fs;
1818
use std::io;
1919
use std::path::{Path, PathBuf};
20+
use std::process::Command;
2021

2122
fn clippy_driver_path() -> PathBuf {
2223
if let Some(path) = option_env!("CLIPPY_DRIVER_PATH") {
@@ -42,6 +43,28 @@ fn rustc_lib_path() -> PathBuf {
4243
option_env!("RUSTC_LIB_PATH").unwrap().into()
4344
}
4445

46+
fn rustc_sysroot_path() -> PathBuf {
47+
option_env!("SYSROOT")
48+
.map(String::from)
49+
.or_else(|| std::env::var("SYSROOT").ok())
50+
.or_else(|| {
51+
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
52+
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
53+
home.and_then(|home| toolchain.map(|toolchain| format!("{}/toolchains/{}", home, toolchain)))
54+
})
55+
.or_else(|| {
56+
Command::new("rustc")
57+
.arg("--print")
58+
.arg("sysroot")
59+
.output()
60+
.ok()
61+
.and_then(|out| String::from_utf8(out.stdout).ok())
62+
.map(|s| s.trim().to_owned())
63+
})
64+
.expect("need to specify SYSROOT env var during clippy compilation, or use rustup or multirust")
65+
.into()
66+
}
67+
4568
fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
4669
let mut config = compiletest::Config::default();
4770

@@ -55,7 +78,7 @@ fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
5578
config.run_lib_path = rustc_lib_path();
5679
config.compile_lib_path = rustc_lib_path();
5780
}
58-
config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings", host_libs().display()));
81+
config.target_rustcflags = Some(format!("-L {0} -L {0}/deps -Dwarnings --sysroot {1}", host_libs().display(), rustc_sysroot_path().display()));
5982

6083
config.mode = cfg_mode;
6184
config.build_base = if rustc_test_suite().is_some() {

0 commit comments

Comments
 (0)