Skip to content

Commit 8ec3717

Browse files
committed
do not auto-detect the targets in the sysroot, instead specify target manually through env var
1 parent 6eb3274 commit 8ec3717

File tree

3 files changed

+41
-74
lines changed

3 files changed

+41
-74
lines changed

.travis.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ before_script:
2929
else
3030
export MIRI_SYSROOT_BASE=~/.cache/miri/
3131
fi
32+
- |
33+
if [[ "$TRAVIS_OS_NAME" == osx ]]; then
34+
FOREIGN_TARGET=i686-apple-darwin
35+
else
36+
FOREIGN_TARGET=i686-unknown-linux-gnu
37+
fi
3238
# install Rust
3339
- curl https://build.travis-ci.org/files/rustup-init.sh -sSf | sh -s -- -y --default-toolchain "$RUST_TOOLCHAIN"
3440
- export PATH=$HOME/.cargo/bin:$PATH
@@ -43,15 +49,11 @@ script:
4349
- |
4450
# Get ourselves a MIR-full libstd for the host and a foreign architecture
4551
cargo miri setup &&
46-
if [[ "$TRAVIS_OS_NAME" == osx ]]; then
47-
cargo miri setup --target i686-apple-darwin
48-
else
49-
cargo miri setup --target i686-unknown-linux-gnu
50-
fi
52+
cargo miri setup --target "$FOREIGN_TARGET"
5153
- |
5254
# Test miri with full MIR, on the host and other architectures
5355
MIRI_SYSROOT=$MIRI_SYSROOT_BASE/HOST cargo test --release --all-features &&
54-
MIRI_SYSROOT=$MIRI_SYSROOT_BASE cargo test --release --all-features
56+
MIRI_SYSROOT=$MIRI_SYSROOT_BASE MIRI_TARGET=$FOREIGN_TARGET cargo test --release --all-features
5557
- |
5658
# Test cargo integration
5759
(cd test-cargo-miri && MIRI_SYSROOT=$MIRI_SYSROOT_BASE/HOST ./run-test.py)

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ MIRI_LOG=rustc_mir::interpret=debug,miri::stacked_borrows cargo run tests/run-pa
154154
In addition, you can set `MIRI_BACKTRACE=1` to get a backtrace of where an
155155
evaluation error was originally created.
156156

157-
### Miri `-Z` flags
157+
### Miri `-Z` flags and environment variables
158158

159-
Several `-Z` flags are relevant for miri:
159+
Several `-Z` flags are relevant for Miri:
160160

161161
* `-Zmir-opt-level` controls how many MIR optimizations are performed. miri
162162
overrides the default to be `0`; be advised that using any higher level can
@@ -168,6 +168,14 @@ Several `-Z` flags are relevant for miri:
168168
enforcing the validity invariant, which is enforced by default. This is
169169
mostly useful for debugging; it means miri will miss bugs in your program.
170170

171+
Moreover, Miri recognizes some environment variables:
172+
173+
* `MIRI_SYSROOT` (recognized by `miri`, `cargo miri` and the test suite)
174+
indicates the sysroot to use.
175+
* `MIRI_TARGET` (recognized by the test suite) indicates which target
176+
architecture to test against. `miri` and `cargo miri` accept the `--target`
177+
flag for the same purpose.
178+
171179
## Contributing and getting help
172180

173181
Check out the issues on this GitHub repository for some ideas. There's lots that

tests/compiletest.rs

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ fn mk_config(mode: &str) -> compiletest::common::ConfigWithTemp {
3434
config.compile_lib_path = rustc_lib_path();
3535
}
3636
config.filter = env::args().nth(1);
37+
config.host = get_host();
3738
config
3839
}
3940

40-
fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, opt: bool) {
41+
fn compile_fail(path: &str, target: &str, opt: bool) {
4142
let opt_str = if opt { " with optimizations" } else { "" };
4243
eprintln!("{}", format!(
4344
"## Running compile-fail tests in {} against miri for target {}{}",
@@ -47,7 +48,6 @@ fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, opt: bool)
4748
).green().bold());
4849

4950
let mut flags = Vec::new();
50-
flags.push(format!("--sysroot {}", sysroot.display()));
5151
flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
5252
flags.push("--edition 2018".to_owned());
5353
if opt {
@@ -60,12 +60,11 @@ fn compile_fail(sysroot: &Path, path: &str, target: &str, host: &str, opt: bool)
6060
let mut config = mk_config("compile-fail");
6161
config.src_base = PathBuf::from(path);
6262
config.target = target.to_owned();
63-
config.host = host.to_owned();
6463
config.target_rustcflags = Some(flags.join(" "));
6564
compiletest::run_tests(&config);
6665
}
6766

68-
fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, opt: bool) {
67+
fn miri_pass(path: &str, target: &str, opt: bool) {
6968
let opt_str = if opt { " with optimizations" } else { "" };
7069
eprintln!("{}", format!(
7170
"## Running run-pass tests in {} against miri for target {}{}",
@@ -75,7 +74,6 @@ fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, opt: bool) {
7574
).green().bold());
7675

7776
let mut flags = Vec::new();
78-
flags.push(format!("--sysroot {}", sysroot.display()));
7977
flags.push("-Dwarnings -Dunused".to_owned()); // overwrite the -Aunused in compiletest-rs
8078
flags.push("--edition 2018".to_owned());
8179
if opt {
@@ -87,57 +85,24 @@ fn miri_pass(sysroot: &Path, path: &str, target: &str, host: &str, opt: bool) {
8785
let mut config = mk_config("ui");
8886
config.src_base = PathBuf::from(path);
8987
config.target = target.to_owned();
90-
config.host = host.to_owned();
9188
config.target_rustcflags = Some(flags.join(" "));
9289
compiletest::run_tests(&config);
9390
}
9491

95-
fn is_target_dir<P: Into<PathBuf>>(path: P) -> bool {
96-
let mut path = path.into();
97-
path.push("lib");
98-
path.metadata().map(|m| m.is_dir()).unwrap_or(false)
99-
}
100-
101-
fn target_has_std<P: Into<PathBuf>>(path: P) -> bool {
102-
let mut path = path.into();
103-
path.push("lib");
104-
std::fs::read_dir(path)
105-
.expect("invalid target")
106-
.map(|entry| entry.unwrap())
107-
.filter(|entry| entry.file_type().unwrap().is_file())
108-
.filter_map(|entry| entry.file_name().into_string().ok())
109-
.any(|file_name| file_name == "libstd.rlib")
110-
}
111-
112-
113-
fn for_all_targets<F: FnMut(String)>(sysroot: &Path, f: F) {
114-
let target_dir = sysroot.join("lib").join("rustlib");
115-
let mut targets = std::fs::read_dir(target_dir)
116-
.expect("invalid sysroot")
117-
.map(|entry| entry.unwrap())
118-
.filter(|entry| is_target_dir(entry.path()))
119-
.filter(|entry| target_has_std(entry.path()))
120-
.map(|entry| entry.file_name().into_string().unwrap())
121-
.peekable();
122-
123-
if targets.peek().is_none() {
124-
panic!("No valid targets found");
92+
/// Make sure the MIRI_SYSROOT env var is set
93+
fn set_sysroot() {
94+
if std::env::var("MIRI_SYSROOT").is_ok() {
95+
// Nothing to do
96+
return;
12597
}
126-
127-
targets.for_each(f);
128-
}
129-
130-
fn get_sysroot() -> PathBuf {
131-
let sysroot = std::env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
132-
let sysroot = std::process::Command::new("rustc")
133-
.arg("--print")
134-
.arg("sysroot")
135-
.output()
136-
.expect("rustc not found")
137-
.stdout;
138-
String::from_utf8(sysroot).expect("sysroot is not utf8")
139-
});
140-
PathBuf::from(sysroot.trim())
98+
let sysroot = std::process::Command::new("rustc")
99+
.arg("--print")
100+
.arg("sysroot")
101+
.output()
102+
.expect("rustc not found")
103+
.stdout;
104+
let sysroot = String::from_utf8(sysroot).expect("sysroot is not utf8");
105+
std::env::set_var("MIRI_SYSROOT", sysroot.trim());
141106
}
142107

143108
fn get_host() -> String {
@@ -153,28 +118,20 @@ fn get_host() -> String {
153118
version_meta.host
154119
}
155120

156-
fn run_pass_miri(opt: bool) {
157-
let sysroot = get_sysroot();
158-
let host = get_host();
121+
fn get_target() -> String {
122+
std::env::var("MIRI_TARGET").unwrap_or_else(|_| get_host())
123+
}
159124

160-
for_all_targets(&sysroot, |target| {
161-
miri_pass(&sysroot, "tests/run-pass", &target, &host, opt);
162-
});
125+
fn run_pass_miri(opt: bool) {
126+
miri_pass("tests/run-pass", &get_target(), opt);
163127
}
164128

165129
fn compile_fail_miri(opt: bool) {
166-
let sysroot = get_sysroot();
167-
let host = get_host();
168-
169-
for_all_targets(&sysroot, |target| {
170-
compile_fail(&sysroot, "tests/compile-fail", &target, &host, opt);
171-
});
130+
compile_fail("tests/compile-fail", &get_target(), opt);
172131
}
173132

174133
fn test_runner(_tests: &[&()]) {
175-
// We put everything into a single test to avoid the parallelism `cargo test`
176-
// introduces. We still get parallelism within our tests because `compiletest`
177-
// uses `libtest` which runs jobs in parallel.
134+
set_sysroot();
178135

179136
run_pass_miri(false);
180137
run_pass_miri(true);

0 commit comments

Comments
 (0)