Skip to content

Commit 001faa5

Browse files
committed
FEAT: Add --config option for miri flags
1 parent a096ff7 commit 001faa5

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

cargo-miri/src/phases.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
181181
let target_dir = get_target_dir(&metadata);
182182
cmd.arg("--target-dir").arg(target_dir);
183183

184+
cmd.args(get_miriflags());
184185
// Store many-seeds argument.
185186
let mut many_seeds = None;
186187
// *After* we set all the flags that need setting, forward everything else. Make sure to skip

cargo-miri/src/util.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ pub fn escape_for_toml(s: &str) -> String {
8888
format!("\"{s}\"")
8989
}
9090

91+
pub fn flagsplit(flags: &str) -> Vec<String> {
92+
// This code is taken from `RUSTFLAGS` handling in cargo.
93+
// Taken from miri-script util.rs
94+
flags.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()
95+
}
96+
9197
pub fn get_miriflags() -> Vec<String> {
9298
// 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.
9399
// 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<String> {
102108
// Respect `MIRIFLAGS` and `miri.flags` setting in cargo config.
103109
// If MIRIFLAGS is present, flags from cargo config are ignored.
104110
// This matches cargo behavior for RUSTFLAGS.
105-
if let Ok(a) = env::var("MIRIFLAGS") {
111+
//
112+
// Strategy: (1) check pseudo var CARGO_ENCODED_MIRIFLAGS first (this is only set after we check for --config
113+
// in the cargo_dash_dash in the if else)
114+
//
115+
// if CARGO_ENCODED_MIRIFLAGS doesn't exist, we check in --config (2)
116+
// if --config doesn't exist, we check offical env var MIRIFLAGS (3)
117+
//
118+
// if MIRIFLAGS is non-existent, we then check for toml (4)
119+
let cargo_dash_dash_config = cargo_extra_flags();
120+
if let Ok(cargo_encoded_miri_flags) = env::var("CARGO_ENCODED_MIRIFLAGS") {
121+
// (1)
122+
flagsplit(cargo_encoded_miri_flags.as_str())
123+
} else if cargo_dash_dash_config.contains(&"miri".to_string()) {
124+
// (2)
125+
let miri_flags_vec = cargo_dash_dash_config
126+
.into_iter()
127+
.filter(|arg| arg.contains(&"miri".to_string()))
128+
.collect::<Vec<String>>();
129+
let miri_flags_string = miri_flags_vec.join(" ");
130+
env::set_var("CARGO_ENCODED_MIRIFLAGS", miri_flags_string);
131+
miri_flags_vec
132+
} else if let Ok(a) = env::var("MIRIFLAGS") {
133+
// (3)
106134
// This code is taken from `RUSTFLAGS` handling in cargo.
107135
a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()
108136
} else {
137+
// (4)
109138
serde_json::from_str::<Vec<String>>(config_miriflags).unwrap_or_default()
110139
}
111140
}

0 commit comments

Comments
 (0)