11use std:: { path:: Path , thread} ;
22
3- use eyre:: { Context , OptionExt } ;
3+ use eyre:: { ensure , Context , OptionExt } ;
44
55fn main ( ) -> eyre:: Result < ( ) > {
6- let root_dir = Path :: new ( ".." ) ;
6+ // Ensure rustup picks up the rust-toolchain.toml file properly and doesn't get confused by this cargo run.
7+ std:: env:: remove_var ( "CARGO" ) ;
8+ std:: env:: remove_var ( "RUSTUP_TOOLCHAIN" ) ;
9+
10+ let root_dir = Path :: new ( ".." )
11+ . canonicalize ( )
12+ . wrap_err ( "canonicalizing .." ) ?;
713 let examples_dir = root_dir. join ( "code" ) . join ( "examples" ) ;
814
15+ // Change the current directory to ensure the correct rustup toolchains are used.
16+ std:: env:: set_current_dir ( & examples_dir)
17+ . wrap_err ( "changing current directory to code/examples" ) ?;
18+
919 let examples = std:: fs:: read_dir ( & examples_dir)
1020 . wrap_err ( "opening ../code/examples, script must be run in ./builder" ) ?;
1121
12- install_toolchain ( & examples_dir) . wrap_err ( "install toolchain" ) ?;
13-
22+ install_toolchain ( ) . wrap_err ( "install toolchain" ) ?;
1423 // Setup miri to avoid race condition in `cargo miri run` below...
15- eprintln ! ( "Setting up miri" ) ;
16- std:: process:: Command :: new ( "cargo" )
17- . arg ( "miri" )
18- . arg ( "setup" )
19- . output ( )
20- . wrap_err ( "setting up miri" ) ?;
24+ setup_miri ( & examples_dir) . wrap_err ( "setting up miri sysroot" ) ?;
2125
2226 thread:: scope ( |scope| {
2327 let mut handles = Vec :: new ( ) ;
@@ -52,6 +56,20 @@ fn main() -> eyre::Result<()> {
5256 Ok ( ( ) )
5357}
5458
59+ fn setup_miri ( dir : & Path ) -> eyre:: Result < ( ) > {
60+ eprintln ! ( "Setting up miri" ) ;
61+ let output = std:: process:: Command :: new ( "cargo" )
62+ . arg ( "miri" )
63+ . arg ( "setup" )
64+ . current_dir ( dir)
65+ . spawn ( )
66+ . wrap_err ( "spawning miri" ) ?
67+ . wait ( )
68+ . wrap_err ( "waiting for miri setup" ) ?;
69+ ensure ! ( output. success( ) ) ;
70+ Ok ( ( ) )
71+ }
72+
5573fn run_example ( examples_dir : & Path , filename : & str ) -> eyre:: Result < ( ) > {
5674 let use_miri = filename. starts_with ( "unsafe_" ) ;
5775
@@ -62,15 +80,12 @@ fn run_example(examples_dir: &Path, filename: &str) -> eyre::Result<()> {
6280 eprintln ! ( "Running {example_name}" ) ;
6381
6482 let mut cmd = std:: process:: Command :: new ( "cargo" ) ;
65- cmd. current_dir ( examples_dir) ;
6683 if use_miri {
6784 cmd. arg ( "miri" ) ;
6885 }
6986 cmd. arg ( "run" ) . arg ( "--quiet" ) . arg ( "--example" ) ;
7087 cmd. arg ( example_name) ;
7188
72- remove_rustup_vars ( & mut cmd) ;
73-
7489 let out = cmd. output ( ) . wrap_err ( "spawning cargo" ) ?;
7590 let stderr = String :: from_utf8 ( out. stderr ) . wrap_err ( "stderr was invalid UTF-8" ) ?;
7691
@@ -82,23 +97,15 @@ fn run_example(examples_dir: &Path, filename: &str) -> eyre::Result<()> {
8297 Ok ( ( ) )
8398}
8499
85- // Ensure there is output for the toolchain and that the installation doesn't pollute stderr.
86- fn install_toolchain ( examples_dir : & Path ) -> eyre:: Result < ( ) > {
100+ /// Ensures there is output for the toolchain and that the installation doesn't pollute stderr.
101+ fn install_toolchain ( ) -> eyre:: Result < ( ) > {
87102 let mut toolchain_install = std:: process:: Command :: new ( "rustc" ) ;
88103 toolchain_install. arg ( "-V" ) ;
89- toolchain_install. current_dir ( examples_dir) ;
90- remove_rustup_vars ( & mut toolchain_install) ;
91- toolchain_install
104+ let output = toolchain_install
92105 . spawn ( )
93106 . wrap_err ( "failed to spawn rustc" ) ?
94107 . wait ( )
95108 . wrap_err ( "failed to wait for rustc" ) ?;
96-
109+ ensure ! ( output . success ( ) ) ;
97110 Ok ( ( ) )
98111}
99-
100- fn remove_rustup_vars ( cmd : & mut std:: process:: Command ) {
101- // Ensure rustup picks up the rust-toolchain.toml file properly and doesn't get confused by this cargo run.
102- cmd. env_remove ( "CARGO" ) ;
103- cmd. env_remove ( "RUSTUP_TOOLCHAIN" ) ;
104- }
0 commit comments