@@ -167,75 +167,88 @@ impl CompilerTest {
167167 /// Set the Rust source code to compile a library Cargo project to Wasm module
168168 pub fn rust_source_cargo_lib (
169169 cargo_project_folder : PathBuf ,
170+ artifact_name : & str ,
170171 is_build_std : bool ,
171172 entry_func_name : Option < String > ,
172173 ) -> Self {
173- let manifest_path = cargo_project_folder. join ( "Cargo.toml" ) ;
174- let mut cargo_build_cmd = Command :: new ( "cargo" ) ;
175- let compiler_workspace_dir = get_workspace_dir ( ) ;
176- // Enable Wasm bulk-memory proposal (uses Wasm `memory.copy` op instead of `memcpy` import)
177- // Remap the compiler workspace directory to `~` to have a reproducible build that does not
178- // have the absolute local path baked into the Wasm binary
179- cargo_build_cmd. env (
180- "RUSTFLAGS" ,
181- format ! (
182- "-C target-feature=+bulk-memory --remap-path-prefix {compiler_workspace_dir}=~"
183- ) ,
184- ) ;
185- cargo_build_cmd
186- . arg ( "build" )
187- . arg ( "--manifest-path" )
188- . arg ( manifest_path)
189- . arg ( "--release" )
190- . arg ( "--target=wasm32-wasi" ) ;
191- if is_build_std {
192- // compile std as part of crate graph compilation
193- // https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
194- cargo_build_cmd. arg ( "-Z" )
174+ let expected_wasm_artifact_path = wasm_artifact_path ( & cargo_project_folder, artifact_name) ;
175+ // dbg!(&wasm_artifact_path);
176+ let wasm_artifact_path = if !skip_rust_compilation ( & cargo_project_folder, artifact_name)
177+ || !expected_wasm_artifact_path. exists ( )
178+ {
179+ let manifest_path = cargo_project_folder. join ( "Cargo.toml" ) ;
180+ let mut cargo_build_cmd = Command :: new ( "cargo" ) ;
181+ let compiler_workspace_dir = get_workspace_dir ( ) ;
182+ // Enable Wasm bulk-memory proposal (uses Wasm `memory.copy` op instead of `memcpy`
183+ // import) Remap the compiler workspace directory to `~` to have a
184+ // reproducible build that does not have the absolute local path baked into
185+ // the Wasm binary
186+ cargo_build_cmd. env (
187+ "RUSTFLAGS" ,
188+ format ! (
189+ "-C target-feature=+bulk-memory --remap-path-prefix {compiler_workspace_dir}=~"
190+ ) ,
191+ ) ;
192+ cargo_build_cmd
193+ . arg ( "build" )
194+ . arg ( "--manifest-path" )
195+ . arg ( manifest_path)
196+ . arg ( "--release" )
197+ . arg ( "--target=wasm32-wasi" ) ;
198+ if is_build_std {
199+ // compile std as part of crate graph compilation
200+ // https://doc.rust-lang.org/cargo/reference/unstable.html#build-std
201+ cargo_build_cmd. arg ( "-Z" )
195202 . arg ( "build-std=std,core,alloc,panic_abort" )
196203 . arg ( "-Z" )
197204 // abort on panic without message formatting (core::fmt uses call_indirect)
198205 . arg ( "build-std-features=panic_immediate_abort" ) ;
199- }
200- let mut child = cargo_build_cmd
201- . arg ( "--message-format=json-render-diagnostics" )
202- . stdout ( Stdio :: piped ( ) )
203- . spawn ( )
204- . unwrap_or_else ( |_| {
205- panic ! (
206- "Failed to execute cargo build {}." ,
207- cargo_build_cmd
208- . get_args( )
209- . map( |arg| format!( "'{}'" , arg. to_str( ) . unwrap( ) ) )
210- . collect:: <Vec <_>>( )
211- . join( " " )
212- )
206+ }
207+ let mut child = cargo_build_cmd
208+ . arg ( "--message-format=json-render-diagnostics" )
209+ . stdout ( Stdio :: piped ( ) )
210+ . spawn ( )
211+ . unwrap_or_else ( |_| {
212+ panic ! (
213+ "Failed to execute cargo build {}." ,
214+ cargo_build_cmd
215+ . get_args( )
216+ . map( |arg| format!( "'{}'" , arg. to_str( ) . unwrap( ) ) )
217+ . collect:: <Vec <_>>( )
218+ . join( " " )
219+ )
220+ } ) ;
221+
222+ // Find the Wasm artifacts from the cargo build output for debugging purposes
223+ let mut wasm_artifacts = find_wasm_artifacts ( & mut child) ;
224+ let output = child. wait ( ) . expect ( "Couldn't get cargo's exit status" ) ;
225+ if !output. success ( ) {
226+ report_cargo_error ( child) ;
227+ }
228+ assert ! ( output. success( ) ) ;
229+ // filter out dependencies
230+ wasm_artifacts. retain ( |path| {
231+ let path_str = path. to_str ( ) . unwrap ( ) ;
232+ !path_str. contains ( "release/deps" )
213233 } ) ;
214- let mut wasm_artifacts = find_wasm_artifacts ( & mut child) ;
215- let output = child. wait ( ) . expect ( "Couldn't get cargo's exit status" ) ;
216- if !output. success ( ) {
217- report_cargo_error ( child) ;
218- }
219- assert ! ( output. success( ) ) ;
220- // filter out dependencies
221- wasm_artifacts. retain ( |path| {
222- let path_str = path. to_str ( ) . unwrap ( ) ;
223- !path_str. contains ( "release/deps" )
224- } ) ;
225- // dbg!(&wasm_artifacts);
226- assert_eq ! ( wasm_artifacts. len( ) , 1 , "Expected one Wasm artifact" ) ;
227- let wasm_comp_path = & wasm_artifacts. first ( ) . unwrap ( ) ;
228- let artifact_name = wasm_comp_path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
229- dbg ! ( & artifact_name) ;
234+ dbg ! ( & wasm_artifacts) ;
235+ assert_eq ! ( wasm_artifacts. len( ) , 1 , "Expected one Wasm artifact" ) ;
236+ wasm_artifacts. first ( ) . unwrap ( ) . to_path_buf ( )
237+ } else {
238+ expected_wasm_artifact_path
239+ } ;
240+
230241 let entrypoint = entry_func_name. map ( |func_name| FunctionIdent {
231- module : Ident :: new ( Symbol :: intern ( artifact_name. clone ( ) ) , SourceSpan :: default ( ) ) ,
242+ module : Ident :: new ( Symbol :: intern ( artifact_name) , SourceSpan :: default ( ) ) ,
232243 function : Ident :: new ( Symbol :: intern ( func_name. to_string ( ) ) , SourceSpan :: default ( ) ) ,
233244 } ) ;
234- let input_file = InputFile :: from_path ( wasm_artifacts . first ( ) . unwrap ( ) ) . unwrap ( ) ;
245+ let input_file = InputFile :: from_path ( wasm_artifact_path ) . unwrap ( ) ;
235246 Self {
236247 config : WasmTranslationConfig :: default ( ) ,
237248 session : default_session ( input_file) ,
238- source : CompilerTestSource :: RustCargoLib { artifact_name } ,
249+ source : CompilerTestSource :: RustCargoLib {
250+ artifact_name : artifact_name. to_string ( ) ,
251+ } ,
239252 entrypoint,
240253 hir : None ,
241254 masm_program : None ,
@@ -416,7 +429,7 @@ impl CompilerTest {
416429 . as_str ( ) ,
417430 )
418431 . build ( ) ;
419- Self :: rust_source_cargo_lib ( proj. root ( ) , is_build_std, Some ( "entrypoint" . to_string ( ) ) )
432+ Self :: rust_source_cargo_lib ( proj. root ( ) , name , is_build_std, Some ( "entrypoint" . to_string ( ) ) )
420433 }
421434
422435 /// Set the Rust source code to compile with `miden-stdlib-sys` (stdlib + intrinsics)
@@ -481,7 +494,7 @@ impl CompilerTest {
481494 )
482495 . build ( ) ;
483496
484- Self :: rust_source_cargo_lib ( proj. root ( ) , is_build_std, Some ( "entrypoint" . to_string ( ) ) )
497+ Self :: rust_source_cargo_lib ( proj. root ( ) , name , is_build_std, Some ( "entrypoint" . to_string ( ) ) )
485498 }
486499
487500 /// Compare the compiled Wasm against the expected output
@@ -564,7 +577,8 @@ impl CompilerTest {
564577 /// The compiled Wasm component/module
565578 fn wasm_bytes ( & self ) -> Vec < u8 > {
566579 match & self . session . input . file {
567- InputType :: Real ( file_path) => fs:: read ( file_path) . unwrap ( ) ,
580+ InputType :: Real ( file_path) => fs:: read ( file_path)
581+ . unwrap_or_else ( |_| panic ! ( "Failed to read Wasm file: {}" , file_path. display( ) ) ) ,
568582 InputType :: Stdin { name : _, input } => input. clone ( ) ,
569583 }
570584 }
@@ -585,6 +599,26 @@ impl CompilerTest {
585599 }
586600}
587601
602+ fn wasm_artifact_path ( cargo_project_folder : & Path , artifact_name : & str ) -> PathBuf {
603+ cargo_project_folder
604+ . to_path_buf ( )
605+ . join ( "target" )
606+ . join ( "wasm32-wasi" )
607+ . join ( "release" )
608+ . join ( artifact_name)
609+ . with_extension ( "wasm" )
610+ }
611+
612+ /// Directs if we should do the Rust compilation step or not
613+ pub fn skip_rust_compilation ( cargo_project_folder : & Path , artifact_name : & str ) -> bool {
614+ let expected_wasm_artifact_path = wasm_artifact_path ( cargo_project_folder, artifact_name) ;
615+ let skip_rust = std:: env:: var ( "SKIP_RUST" ) . is_ok ( ) && expected_wasm_artifact_path. exists ( ) ;
616+ if skip_rust {
617+ eprintln ! ( "Skipping Rust compilation" ) ;
618+ } ;
619+ skip_rust
620+ }
621+
588622// Assemble the VM MASM program from the compiled IR MASM modules
589623fn masm_prog_from_modules (
590624 user_ns_name : & str ,
0 commit comments