Skip to content

Commit

Permalink
FEAT: Add --config option for miri flags
Browse files Browse the repository at this point in the history
  • Loading branch information
badumbatish committed Sep 10, 2024
1 parent a096ff7 commit 001faa5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions cargo-miri/src/phases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
let target_dir = get_target_dir(&metadata);
cmd.arg("--target-dir").arg(target_dir);

cmd.args(get_miriflags());
// Store many-seeds argument.
let mut many_seeds = None;
// *After* we set all the flags that need setting, forward everything else. Make sure to skip
Expand Down
31 changes: 30 additions & 1 deletion cargo-miri/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ pub fn escape_for_toml(s: &str) -> String {
format!("\"{s}\"")
}

pub fn flagsplit(flags: &str) -> Vec<String> {
// This code is taken from `RUSTFLAGS` handling in cargo.
// Taken from miri-script util.rs
flags.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()
}

pub fn get_miriflags() -> Vec<String> {
// TODO: I quite not understand what Carl Jung means by Oh and please add a link to https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags.
// I guess we don't support the target.rustflags part yet? (That's okay but should be mentioned in a comment.)
Expand All @@ -102,10 +108,33 @@ pub fn get_miriflags() -> Vec<String> {
// Respect `MIRIFLAGS` and `miri.flags` setting in cargo config.
// If MIRIFLAGS is present, flags from cargo config are ignored.
// This matches cargo behavior for RUSTFLAGS.
if let Ok(a) = env::var("MIRIFLAGS") {
//
// Strategy: (1) check pseudo var CARGO_ENCODED_MIRIFLAGS first (this is only set after we check for --config
// in the cargo_dash_dash in the if else)
//
// if CARGO_ENCODED_MIRIFLAGS doesn't exist, we check in --config (2)
// if --config doesn't exist, we check offical env var MIRIFLAGS (3)
//
// if MIRIFLAGS is non-existent, we then check for toml (4)
let cargo_dash_dash_config = cargo_extra_flags();
if let Ok(cargo_encoded_miri_flags) = env::var("CARGO_ENCODED_MIRIFLAGS") {
// (1)
flagsplit(cargo_encoded_miri_flags.as_str())
} else if cargo_dash_dash_config.contains(&"miri".to_string()) {
// (2)
let miri_flags_vec = cargo_dash_dash_config
.into_iter()
.filter(|arg| arg.contains(&"miri".to_string()))
.collect::<Vec<String>>();
let miri_flags_string = miri_flags_vec.join(" ");
env::set_var("CARGO_ENCODED_MIRIFLAGS", miri_flags_string);
miri_flags_vec
} else if let Ok(a) = env::var("MIRIFLAGS") {
// (3)
// This code is taken from `RUSTFLAGS` handling in cargo.
a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()
} else {
// (4)
serde_json::from_str::<Vec<String>>(config_miriflags).unwrap_or_default()
}
}
Expand Down

0 comments on commit 001faa5

Please sign in to comment.