From 73e6206e574484f4d65a474d4a5969c3254b0aa2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 4 Oct 2023 22:11:15 +0200 Subject: [PATCH] auto-detect no_std where possible --- CONTRIBUTING.md | 17 +++++------------ README.md | 6 ++++-- cargo-miri/src/setup.rs | 12 +++++++++++- ci.sh | 2 +- tests/compiletest.rs | 4 +--- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4e50b8fae8..40a4332cdb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -71,19 +71,12 @@ and you can (cross-)run the entire test suite using: MIRI_TEST_TARGET=i686-unknown-linux-gnu ./miri test ``` -If your target doesn't support libstd, you can run miri with +If your target doesn't support libstd that should usually just work. However, if you are using a +custom target file, you might have to set `MIRI_NO_STD=1`. -``` -MIRI_NO_STD=1 MIRI_TEST_TARGET=thumbv7em-none-eabihf ./miri test tests/fail/alloc/no_global_allocator.rs -MIRI_NO_STD=1 ./miri run tests/pass/no_std.rs --target thumbv7em-none-eabihf -``` - -to avoid attempting (and failing) to build libstd. Note that almost no tests will pass -this way, but you can run individual tests. - -`./miri test FILTER` only runs those tests that contain `FILTER` in their -filename (including the base directory, e.g. `./miri test fail` will run all -compile-fail tests). +`./miri test FILTER` only runs those tests that contain `FILTER` in their filename (including the +base directory, e.g. `./miri test fail` will run all compile-fail tests). These filters are passed +to `cargo test`, so for multiple filers you need to use `./miri test -- FILTER1 FILTER2`. You can get a trace of which MIR statements are being executed by setting the `MIRI_LOG` environment variable. For example: diff --git a/README.md b/README.md index 6127eada22..ca2bb80bd2 100644 --- a/README.md +++ b/README.md @@ -480,8 +480,10 @@ Moreover, Miri recognizes some environment variables: purpose. * `MIRI_TEST_THREADS` (recognized by the test suite): set the number of threads to use for running tests. By default the number of cores is used. -* `MIRI_NO_STD` (recognized by `cargo miri` and the test suite) makes sure that the target's - sysroot is built without libstd. This allows testing and running no_std programs. +* `MIRI_NO_STD` (recognized by `cargo miri`) makes sure that the target's sysroot is built without + libstd. This allows testing and running no_std programs. + (Miri has a heuristic to detect no-std targets based on the target name; this environment variable + is only needed when that heuristic fails.) * `RUSTC_BLESS` (recognized by the test suite and `cargo-miri-test/run-test.py`): overwrite all `stderr` and `stdout` files instead of checking whether the output matches. * `MIRI_SKIP_UI_CHECKS` (recognized by the test suite): don't check whether the diff --git a/cargo-miri/src/setup.rs b/cargo-miri/src/setup.rs index 77cecddcb8..d921741d5d 100644 --- a/cargo-miri/src/setup.rs +++ b/cargo-miri/src/setup.rs @@ -74,7 +74,17 @@ pub fn setup( } }; // Sysroot configuration and build details. - let sysroot_config = if std::env::var_os("MIRI_NO_STD").is_some() { + let no_std = match std::env::var_os("MIRI_NO_STD") { + None => + // No-std heuristic taken from rust/src/bootstrap/config.rs + // (https://github.com/rust-lang/rust/blob/25b5af1b3a0b9e2c0c57b223b2d0e3e203869b2c/src/bootstrap/config.rs#L549-L555). + target.contains("-none") + || target.contains("nvptx") + || target.contains("switch") + || target.contains("-uefi"), + Some(val) => val != "0", + }; + let sysroot_config = if no_std { SysrootConfig::NoStd } else { SysrootConfig::WithStd { diff --git a/ci.sh b/ci.sh index 9e7779e351..1b3ed796c6 100755 --- a/ci.sh +++ b/ci.sh @@ -112,7 +112,7 @@ case $HOST_TARGET in MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings - MIRI_TEST_TARGET=thumbv7em-none-eabihf MIRI_NO_STD=1 run_tests_minimal no_std # no_std embedded architecture + MIRI_TEST_TARGET=thumbv7em-none-eabihf run_tests_minimal no_std # no_std embedded architecture MIRI_TEST_TARGET=tests/avr.json MIRI_NO_STD=1 run_tests_minimal no_std # JSON target file ;; x86_64-apple-darwin) diff --git a/tests/compiletest.rs b/tests/compiletest.rs index 035846f0ef..c2dccf8137 100644 --- a/tests/compiletest.rs +++ b/tests/compiletest.rs @@ -93,9 +93,7 @@ fn test_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) -> ..Config::rustc(path) }; - let use_std = env::var_os("MIRI_NO_STD").is_none(); - - if with_dependencies && use_std { + if with_dependencies { config.dependencies_crate_manifest_path = Some(Path::new("test_dependencies").join("Cargo.toml")); let mut builder_args = vec!["run".into()];