@@ -23,7 +23,9 @@ const JOSH_PORT: &str = "42042";
2323
2424impl MiriEnv {
2525 /// Returns the location of the sysroot.
26- fn build_miri_sysroot ( & mut self , quiet : bool ) -> Result < PathBuf > {
26+ ///
27+ /// If the target is None the sysroot will be built for the host machine.
28+ fn build_miri_sysroot ( & mut self , quiet : bool , target : Option < & str > ) -> Result < PathBuf > {
2729 if let Some ( miri_sysroot) = self . sh . var_os ( "MIRI_SYSROOT" ) {
2830 // Sysroot already set, use that.
2931 return Ok ( miri_sysroot. into ( ) ) ;
@@ -35,26 +37,27 @@ impl MiriEnv {
3537 self . build ( path ! ( self . miri_dir / "Cargo.toml" ) , & [ ] , quiet) ?;
3638 self . build ( & manifest_path, & [ ] , quiet) ?;
3739
38- let target = & match self . sh . var ( "MIRI_TEST_TARGET" ) {
39- Ok ( target) => vec ! [ "--target" . into( ) , target] ,
40- Err ( _) => vec ! [ ] ,
41- } ;
40+ let target_flag =
41+ & if let Some ( target) = target { vec ! [ "--target" , target] } else { vec ! [ ] } ;
42+
4243 if !quiet {
43- match self . sh . var ( "MIRI_TEST_TARGET" ) {
44- Ok ( target) => eprintln ! ( "$ (building Miri sysroot for {target})" ) ,
45- Err ( _) => eprintln ! ( "$ (building Miri sysroot)" ) ,
44+ if let Some ( target) = target {
45+ eprintln ! ( "$ (building Miri sysroot for {target})" ) ;
46+ } else {
47+ eprintln ! ( "$ (building Miri sysroot)" ) ;
4648 }
4749 }
50+
4851 let output = cmd ! ( self . sh,
4952 "cargo +{toolchain} --quiet run {cargo_extra_flags...} --manifest-path {manifest_path} --
50- miri setup --print-sysroot {target ...}"
53+ miri setup --print-sysroot {target_flag ...}"
5154 ) . read ( ) ;
5255 let Ok ( output) = output else {
5356 // Run it again (without `--print-sysroot` or `--quiet`) so the user can see the error.
5457 cmd ! (
5558 self . sh,
5659 "cargo +{toolchain} run {cargo_extra_flags...} --manifest-path {manifest_path} --
57- miri setup {target ...}"
60+ miri setup {target_flag ...}"
5861 )
5962 . run ( )
6063 . with_context ( || "`cargo miri setup` failed" ) ?;
@@ -161,7 +164,7 @@ impl Command {
161164 Command :: Install { flags } => Self :: install ( flags) ,
162165 Command :: Build { flags } => Self :: build ( flags) ,
163166 Command :: Check { flags } => Self :: check ( flags) ,
164- Command :: Test { bless, flags } => Self :: test ( bless, flags) ,
167+ Command :: Test { bless, flags, target } => Self :: test ( bless, flags, target ) ,
165168 Command :: Run { dep, verbose, many_seeds, flags } =>
166169 Self :: run ( dep, verbose, many_seeds, flags) ,
167170 Command :: Fmt { flags } => Self :: fmt ( flags) ,
@@ -446,16 +449,23 @@ impl Command {
446449 Ok ( ( ) )
447450 }
448451
449- fn test ( bless : bool , flags : Vec < OsString > ) -> Result < ( ) > {
452+ fn test ( bless : bool , flags : Vec < OsString > , target : Option < String > ) -> Result < ( ) > {
450453 let mut e = MiriEnv :: new ( ) ?;
454+
455+ if let Some ( target) = target. as_deref ( ) {
456+ // Tell the sysroot which target to test.
457+ e. sh . set_var ( "MIRI_TEST_TARGET" , target) ;
458+ }
459+
451460 // Prepare a sysroot.
452- e. build_miri_sysroot ( /* quiet */ false ) ?;
461+ e. build_miri_sysroot ( /* quiet */ false , target . as_deref ( ) ) ?;
453462
454463 // Then test, and let caller control flags.
455464 // Only in root project as `cargo-miri` has no tests.
456465 if bless {
457466 e. sh . set_var ( "RUSTC_BLESS" , "Gesundheit" ) ;
458467 }
468+
459469 e. test ( path ! ( e. miri_dir / "Cargo.toml" ) , & flags) ?;
460470 Ok ( ( ) )
461471 }
@@ -476,14 +486,24 @@ impl Command {
476486 . take_while ( |arg| * arg != "--" )
477487 . tuple_windows ( )
478488 . find ( |( first, _) | * first == "--target" ) ;
479- if let Some ( ( _, target) ) = target {
489+
490+ let target_triple = if let Some ( ( _, target) ) = target {
480491 // Found it!
481492 e. sh . set_var ( "MIRI_TEST_TARGET" , target) ;
493+
494+ let triple =
495+ target. clone ( ) . into_string ( ) . map_err ( |_| anyhow ! ( "target triple is not UTF-8" ) ) ?;
496+ Some ( triple)
482497 } else if let Ok ( target) = std:: env:: var ( "MIRI_TEST_TARGET" ) {
483498 // Convert `MIRI_TEST_TARGET` into `--target`.
484499 flags. push ( "--target" . into ( ) ) ;
485- flags. push ( target. into ( ) ) ;
486- }
500+ flags. push ( target. clone ( ) . into ( ) ) ;
501+
502+ Some ( target)
503+ } else {
504+ None
505+ } ;
506+
487507 // Scan for "--edition", set one ourselves if that flag is not present.
488508 let have_edition =
489509 flags. iter ( ) . take_while ( |arg| * arg != "--" ) . any ( |arg| * arg == "--edition" ) ;
@@ -492,7 +512,8 @@ impl Command {
492512 }
493513
494514 // Prepare a sysroot, and add it to the flags.
495- let miri_sysroot = e. build_miri_sysroot ( /* quiet */ !verbose) ?;
515+ let miri_sysroot =
516+ e. build_miri_sysroot ( /* quiet */ !verbose, target_triple. as_deref ( ) ) ?;
496517 flags. push ( "--sysroot" . into ( ) ) ;
497518 flags. push ( miri_sysroot. into ( ) ) ;
498519
0 commit comments