Skip to content

Commit 2170d7a

Browse files
committed
Auto merge of #1949 - RalfJung:miri-lib-src, r=oli-obk
add and document MIRI_LIB_SRC env var to set the source from which Miri builds the standard library This is just an alias of `XARGO_RUST_SRC`, but avoids exposing how exactly we use xargo.
2 parents 6f3061b + e51810d commit 2170d7a

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ There's a test for the cargo wrapper in the `test-cargo-miri` directory; run
107107
`./run-test.py` in there to execute it. Like `./miri test`, this respects the
108108
`MIRI_TEST_TARGET` environment variable to execute the test for another target.
109109

110+
### Using a modified standard library
111+
112+
Miri re-builds the standard library into a custom sysroot, so it is fairly easy
113+
to test Miri against a modified standard library -- you do not even have to
114+
build Miri yourself, the Miri shipped by `rustup` will work. All you have to do
115+
is set the `MIRI_LIB_SRC` environment variable to the `library` folder of a
116+
`rust-lang/rust` repository checkout. Note that changing files in that directory
117+
does not automatically trigger a re-build of the standard library; you have to
118+
clear the Miri build cache manually (on Linux, `rm -rf ~/.cache/miri`).
119+
110120
## Configuring `rust-analyzer`
111121

112122
To configure `rust-analyzer` and VS Code for working on Miri, save the following

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ Moreover, Miri recognizes some environment variables:
306306
Miri executions, also [see "Testing the Miri driver" in `CONTRIBUTING.md`][testing-miri].
307307
* `MIRIFLAGS` (recognized by `cargo miri` and the test suite) defines extra
308308
flags to be passed to Miri.
309+
* `MIRI_LIB_SRC` defines the directory where Miri expects the sources of the
310+
standard library that it will build and use for interpretation. This directory
311+
must point to the `library` subdirectory of a `rust-lang/rust` repository
312+
checkout. Note that changing files in that directory does not automatically
313+
trigger a re-build of the standard library; you have to clear the Miri build
314+
cache manually (on Linux, `rm -rf ~/.cache/miri`).
309315
* `MIRI_SYSROOT` (recognized by `cargo miri` and the test suite)
310316
indicates the sysroot to use. To do the same thing with `miri`
311317
directly, use the `--sysroot` flag.

cargo-miri/bin.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::env;
2-
use std::ffi::OsString;
2+
use std::ffi::{OsStr, OsString};
33
use std::fmt::Write as _;
44
use std::fs::{self, File};
55
use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
@@ -341,8 +341,11 @@ fn setup(subcommand: MiriCommand) {
341341
ask_to_run(cmd, ask_user, "install a recent enough xargo");
342342
}
343343

344-
// Determine where the rust sources are located. `XARGO_RUST_SRC` env var trumps everything.
345-
let rust_src = match std::env::var_os("XARGO_RUST_SRC") {
344+
// Determine where the rust sources are located. The env vars manually setting the source
345+
// (`MIRI_LIB_SRC`, `XARGO_RUST_SRC`) trump auto-detection.
346+
let rust_src_env_var =
347+
std::env::var_os("MIRI_LIB_SRC").or_else(|| std::env::var_os("XARGO_RUST_SRC"));
348+
let rust_src = match rust_src_env_var {
346349
Some(path) => {
347350
let path = PathBuf::from(path);
348351
// Make path absolute if possible.
@@ -376,6 +379,13 @@ fn setup(subcommand: MiriCommand) {
376379
if !rust_src.exists() {
377380
show_error(format!("given Rust source directory `{}` does not exist.", rust_src.display()));
378381
}
382+
if rust_src.file_name().and_then(OsStr::to_str) != Some("library") {
383+
show_error(format!(
384+
"given Rust source directory `{}` does not seem to be the `library` subdirectory of \
385+
a Rust source checkout.",
386+
rust_src.display()
387+
));
388+
}
379389

380390
// Next, we need our own libstd. Prepare a xargo project for that purpose.
381391
// We will do this work in whatever is a good cache dir for this platform.

0 commit comments

Comments
 (0)