Skip to content

Commit 2f4f9ac

Browse files
authored
Merge pull request #4396 from Stypox/build-with-features
Allow building Miri with --features from miri-script
2 parents 0e2dcd8 + eb56874 commit 2f4f9ac

File tree

4 files changed

+153
-65
lines changed

4 files changed

+153
-65
lines changed

miri-script/src/commands.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ impl MiriEnv {
3232
&mut self,
3333
quiet: bool,
3434
target: Option<impl AsRef<OsStr>>,
35+
features: &[String],
3536
) -> Result<PathBuf> {
3637
if let Some(miri_sysroot) = self.sh.var_os("MIRI_SYSROOT") {
3738
// Sysroot already set, use that.
3839
return Ok(miri_sysroot.into());
3940
}
4041

4142
// Make sure everything is built. Also Miri itself.
42-
self.build(".", &[], quiet)?;
43-
self.build("cargo-miri", &[], quiet)?;
43+
self.build(".", features, &[], quiet)?;
44+
self.build("cargo-miri", &[], &[], quiet)?;
4445

4546
let target_flag = if let Some(target) = &target {
4647
vec![OsStr::new("--target"), target.as_ref()]
@@ -58,7 +59,7 @@ impl MiriEnv {
5859
}
5960

6061
let mut cmd = self
61-
.cargo_cmd("cargo-miri", "run")
62+
.cargo_cmd("cargo-miri", "run", &[])
6263
.arg("--quiet")
6364
.arg("--")
6465
.args(&["miri", "setup", "--print-sysroot"])
@@ -90,7 +91,9 @@ impl Command {
9091
Self::fmt(vec![])?;
9192
}
9293
if auto_clippy {
93-
Self::clippy(vec![])?;
94+
// no features for auto actions, see
95+
// https://github.com/rust-lang/miri/pull/4396#discussion_r2149654845
96+
Self::clippy(vec![], vec![])?;
9497
}
9598

9699
Ok(())
@@ -175,16 +178,16 @@ impl Command {
175178
}
176179
// Then run the actual command.
177180
match self {
178-
Command::Install { flags } => Self::install(flags),
179-
Command::Build { flags } => Self::build(flags),
180-
Command::Check { flags } => Self::check(flags),
181-
Command::Test { bless, flags, target, coverage } =>
182-
Self::test(bless, flags, target, coverage),
183-
Command::Run { dep, verbose, target, edition, flags } =>
184-
Self::run(dep, verbose, target, edition, flags),
185-
Command::Doc { flags } => Self::doc(flags),
181+
Command::Install { features, flags } => Self::install(features, flags),
182+
Command::Build { features, flags } => Self::build(features, flags),
183+
Command::Check { features, flags } => Self::check(features, flags),
184+
Command::Test { bless, target, coverage, features, flags } =>
185+
Self::test(bless, target, coverage, features, flags),
186+
Command::Run { dep, verbose, target, edition, features, flags } =>
187+
Self::run(dep, verbose, target, edition, features, flags),
188+
Command::Doc { features, flags } => Self::doc(features, flags),
186189
Command::Fmt { flags } => Self::fmt(flags),
187-
Command::Clippy { flags } => Self::clippy(flags),
190+
Command::Clippy { features, flags } => Self::clippy(features, flags),
188191
Command::Bench { target, no_install, save_baseline, load_baseline, benches } =>
189192
Self::bench(target, no_install, save_baseline, load_baseline, benches),
190193
Command::Toolchain { flags } => Self::toolchain(flags),
@@ -494,7 +497,7 @@ impl Command {
494497

495498
if !no_install {
496499
// Make sure we have an up-to-date Miri installed and selected the right toolchain.
497-
Self::install(vec![])?;
500+
Self::install(vec![], vec![])?;
498501
}
499502
let results_json_dir = if save_baseline.is_some() || load_baseline.is_some() {
500503
Some(TempDir::new()?)
@@ -601,47 +604,48 @@ impl Command {
601604
Ok(())
602605
}
603606

604-
fn install(flags: Vec<String>) -> Result<()> {
607+
fn install(features: Vec<String>, flags: Vec<String>) -> Result<()> {
605608
let e = MiriEnv::new()?;
606-
e.install_to_sysroot(e.miri_dir.clone(), &flags)?;
607-
e.install_to_sysroot(path!(e.miri_dir / "cargo-miri"), &flags)?;
609+
e.install_to_sysroot(".", &features, &flags)?;
610+
e.install_to_sysroot("cargo-miri", &[], &flags)?;
608611
Ok(())
609612
}
610613

611-
fn build(flags: Vec<String>) -> Result<()> {
614+
fn build(features: Vec<String>, flags: Vec<String>) -> Result<()> {
612615
let e = MiriEnv::new()?;
613-
e.build(".", &flags, /* quiet */ false)?;
614-
e.build("cargo-miri", &flags, /* quiet */ false)?;
616+
e.build(".", &features, &flags, /* quiet */ false)?;
617+
e.build("cargo-miri", &[], &flags, /* quiet */ false)?;
615618
Ok(())
616619
}
617620

618-
fn check(flags: Vec<String>) -> Result<()> {
621+
fn check(features: Vec<String>, flags: Vec<String>) -> Result<()> {
619622
let e = MiriEnv::new()?;
620-
e.check(".", &flags)?;
621-
e.check("cargo-miri", &flags)?;
623+
e.check(".", &features, &flags)?;
624+
e.check("cargo-miri", &[], &flags)?;
622625
Ok(())
623626
}
624627

625-
fn doc(flags: Vec<String>) -> Result<()> {
628+
fn doc(features: Vec<String>, flags: Vec<String>) -> Result<()> {
626629
let e = MiriEnv::new()?;
627-
e.doc(".", &flags)?;
628-
e.doc("cargo-miri", &flags)?;
630+
e.doc(".", &features, &flags)?;
631+
e.doc("cargo-miri", &[], &flags)?;
629632
Ok(())
630633
}
631634

632-
fn clippy(flags: Vec<String>) -> Result<()> {
635+
fn clippy(features: Vec<String>, flags: Vec<String>) -> Result<()> {
633636
let e = MiriEnv::new()?;
634-
e.clippy(".", &flags)?;
635-
e.clippy("cargo-miri", &flags)?;
636-
e.clippy("miri-script", &flags)?;
637+
e.clippy(".", &features, &flags)?;
638+
e.clippy("cargo-miri", &[], &flags)?;
639+
e.clippy("miri-script", &[], &flags)?;
637640
Ok(())
638641
}
639642

640643
fn test(
641644
bless: bool,
642-
mut flags: Vec<String>,
643645
target: Option<String>,
644646
coverage: bool,
647+
features: Vec<String>,
648+
mut flags: Vec<String>,
645649
) -> Result<()> {
646650
let mut e = MiriEnv::new()?;
647651

@@ -652,7 +656,7 @@ impl Command {
652656
}
653657

654658
// Prepare a sysroot. (Also builds cargo-miri, which we need.)
655-
e.build_miri_sysroot(/* quiet */ false, target.as_deref())?;
659+
e.build_miri_sysroot(/* quiet */ false, target.as_deref(), &features)?;
656660

657661
// Forward information to test harness.
658662
if bless {
@@ -672,10 +676,10 @@ impl Command {
672676

673677
// Then test, and let caller control flags.
674678
// Only in root project as `cargo-miri` has no tests.
675-
e.test(".", &flags)?;
679+
e.test(".", &features, &flags)?;
676680

677681
if let Some(coverage) = &coverage {
678-
coverage.show_coverage_report(&e)?;
682+
coverage.show_coverage_report(&e, &features)?;
679683
}
680684

681685
Ok(())
@@ -686,14 +690,17 @@ impl Command {
686690
verbose: bool,
687691
target: Option<String>,
688692
edition: Option<String>,
693+
features: Vec<String>,
689694
flags: Vec<String>,
690695
) -> Result<()> {
691696
let mut e = MiriEnv::new()?;
692697

693698
// Preparation: get a sysroot, and get the miri binary.
694-
let miri_sysroot = e.build_miri_sysroot(/* quiet */ !verbose, target.as_deref())?;
695-
let miri_bin =
696-
e.build_get_binary(".").context("failed to get filename of miri executable")?;
699+
let miri_sysroot =
700+
e.build_miri_sysroot(/* quiet */ !verbose, target.as_deref(), &features)?;
701+
let miri_bin = e
702+
.build_get_binary(".", &features)
703+
.context("failed to get filename of miri executable")?;
697704

698705
// More flags that we will pass before `flags`
699706
// (because `flags` may contain `--`).
@@ -718,7 +725,7 @@ impl Command {
718725
// The basic command that executes the Miri driver.
719726
let mut cmd = if dep {
720727
// We invoke the test suite as that has all the logic for running with dependencies.
721-
e.cargo_cmd(".", "test")
728+
e.cargo_cmd(".", "test", &features)
722729
.args(&["--test", "ui"])
723730
.args(quiet_flag)
724731
.arg("--")

miri-script/src/coverage.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl CoverageReport {
4949

5050
/// show_coverage_report will print coverage information using the artifact
5151
/// files in `self.path`.
52-
pub fn show_coverage_report(&self, e: &MiriEnv) -> Result<()> {
52+
pub fn show_coverage_report(&self, e: &MiriEnv, features: &[String]) -> Result<()> {
5353
let profraw_files = self.profraw_files()?;
5454

5555
let profdata_bin = path!(e.libdir / ".." / "bin" / "llvm-profdata");
@@ -63,8 +63,9 @@ impl CoverageReport {
6363

6464
// Create the coverage report.
6565
let cov_bin = path!(e.libdir / ".." / "bin" / "llvm-cov");
66-
let miri_bin =
67-
e.build_get_binary(".").context("failed to get filename of miri executable")?;
66+
let miri_bin = e
67+
.build_get_binary(".", features)
68+
.context("failed to get filename of miri executable")?;
6869
cmd!(
6970
e.sh,
7071
"{cov_bin} report --instr-profile={merged_file} --object {miri_bin} --sources src/"

miri-script/src/main.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,40 @@ pub enum Command {
1414
/// Sets up the rpath such that the installed binary should work in any
1515
/// working directory.
1616
Install {
17+
/// Pass features to cargo invocations on the "miri" crate in the root. This option does
18+
/// **not** apply to other crates, so e.g. these features won't be used on "cargo-miri".
19+
#[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
20+
features: Vec<String>,
1721
/// Flags that are passed through to `cargo install`.
1822
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
1923
flags: Vec<String>,
2024
},
2125
/// Build Miri.
2226
Build {
27+
/// Pass features to cargo invocations on the "miri" crate in the root. This option does
28+
/// **not** apply to other crates, so e.g. these features won't be used on "cargo-miri".
29+
#[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
30+
features: Vec<String>,
2331
/// Flags that are passed through to `cargo build`.
2432
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
2533
flags: Vec<String>,
2634
},
2735
/// Check Miri.
2836
Check {
37+
/// Pass features to cargo invocations on the "miri" crate in the root. This option does
38+
/// **not** apply to other crates, so e.g. these features won't be used on "cargo-miri".
39+
#[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
40+
features: Vec<String>,
2941
/// Flags that are passed through to `cargo check`.
3042
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
3143
flags: Vec<String>,
3244
},
3345
/// Check Miri with Clippy.
3446
Clippy {
47+
/// Pass features to cargo invocations on the "miri" crate in the root. This option does
48+
/// **not** apply to other crates, so e.g. these features won't be used on "cargo-miri".
49+
#[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
50+
features: Vec<String>,
3551
/// Flags that are passed through to `cargo clippy`.
3652
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
3753
flags: Vec<String>,
@@ -47,6 +63,10 @@ pub enum Command {
4763
/// Produce coverage report.
4864
#[arg(long)]
4965
coverage: bool,
66+
/// Pass features to cargo invocations on the "miri" crate in the root. This option does
67+
/// **not** apply to other crates, so e.g. these features won't be used on "cargo-miri".
68+
#[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
69+
features: Vec<String>,
5070
/// Flags that are passed through to the test harness.
5171
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
5272
flags: Vec<String>,
@@ -67,6 +87,10 @@ pub enum Command {
6787
/// The Rust edition.
6888
#[arg(long)]
6989
edition: Option<String>,
90+
/// Pass features to cargo invocations on the "miri" crate in the root. This option does
91+
/// **not** apply to other crates, so e.g. these features won't be used on "cargo-miri".
92+
#[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
93+
features: Vec<String>,
7094
/// Flags that are passed through to `miri`.
7195
///
7296
/// The flags set in `MIRIFLAGS` are added in front of these flags.
@@ -75,6 +99,10 @@ pub enum Command {
7599
},
76100
/// Build documentation.
77101
Doc {
102+
/// Pass features to cargo invocations on the "miri" crate in the root. This option does
103+
/// **not** apply to other crates, so e.g. these features won't be used on "cargo-miri".
104+
#[arg(long, value_delimiter = ',', action = clap::ArgAction::Append)]
105+
features: Vec<String>,
78106
/// Flags that are passed through to `cargo doc`.
79107
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
80108
flags: Vec<String>,
@@ -144,13 +172,13 @@ impl Command {
144172
}
145173

146174
match self {
147-
Self::Install { flags }
148-
| Self::Build { flags }
149-
| Self::Check { flags }
150-
| Self::Doc { flags }
175+
Self::Install { flags, .. }
176+
| Self::Build { flags, .. }
177+
| Self::Check { flags, .. }
178+
| Self::Doc { flags, .. }
151179
| Self::Fmt { flags }
152180
| Self::Toolchain { flags }
153-
| Self::Clippy { flags }
181+
| Self::Clippy { flags, .. }
154182
| Self::Run { flags, .. }
155183
| Self::Test { flags, .. } => {
156184
flags.extend(remainder);

0 commit comments

Comments
 (0)