From 001faa5e1b32c16b05fe82f905ee744142bd4bce Mon Sep 17 00:00:00 2001 From: badumbatish Date: Mon, 9 Sep 2024 23:45:24 -0700 Subject: [PATCH] FEAT: Add --config option for miri flags --- cargo-miri/src/phases.rs | 1 + cargo-miri/src/util.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 3694e715fd..72d28cee81 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -181,6 +181,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator) { 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 diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index a23d7ab80f..84120b01e2 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -88,6 +88,12 @@ pub fn escape_for_toml(s: &str) -> String { format!("\"{s}\"") } +pub fn flagsplit(flags: &str) -> Vec { + // 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 { // 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.) @@ -102,10 +108,33 @@ pub fn get_miriflags() -> Vec { // 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::>(); + 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::>(config_miriflags).unwrap_or_default() } }