@@ -30,25 +30,83 @@ use std::{env, fs};
3030use anyhow:: Result ;
3131use built:: write_built_file;
3232
33+ fn get_wasm_runtime_path ( ) -> PathBuf {
34+ let manifest_dir = env:: var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ;
35+ let manifest_dir = PathBuf :: from ( manifest_dir) ;
36+
37+ let tar_path = manifest_dir. join ( "vendor.tar" ) ;
38+
39+ let out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
40+ let out_dir = PathBuf :: from ( out_dir) ;
41+ let vendor_dir = out_dir. join ( "vendor" ) ;
42+
43+ if vendor_dir. exists ( ) {
44+ fs:: remove_dir_all ( & vendor_dir) . unwrap ( ) ;
45+ }
46+
47+ println ! ( "cargo::rerun-if-changed={}" , tar_path. display( ) ) ;
48+
49+ // If the vendor.tar file exists, extract it to the OUT_DIR/vendor directory
50+ // and return the wasm_runtime directory inside it.
51+ // This is useful for vendoring the wasm_runtime crate in a release build, since crates.io
52+ // does not allow vendoring folders with Cargo.toml files (i.e., other crates).
53+ // The vendor.tar file is expected to be in the same directory as this build script.
54+ if tar_path. exists ( ) {
55+ let out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
56+ let out_dir = PathBuf :: from ( out_dir) ;
57+ let vendor_dir = out_dir. join ( "vendor" ) ;
58+
59+ let mut tar = tar:: Archive :: new ( fs:: File :: open ( & tar_path) . unwrap ( ) ) ;
60+ tar. unpack ( & vendor_dir) . unwrap ( ) ;
61+
62+ let wasm_runtime_dir = vendor_dir. join ( "wasm_runtime" ) ;
63+
64+ println ! (
65+ "cargo::warning=using vendor wasm_runtime from {}" ,
66+ tar_path. display( )
67+ ) ;
68+ return wasm_runtime_dir;
69+ }
70+
71+ let crates_dir = manifest_dir. parent ( ) . unwrap ( ) ;
72+
73+ #[ cfg( unix) ]
74+ std:: os:: unix:: fs:: symlink ( crates_dir, & vendor_dir) . unwrap ( ) ;
75+
76+ #[ cfg( not( unix) ) ]
77+ std:: os:: windows:: fs:: symlink_dir ( crates_dir, & vendor_dir) . unwrap ( ) ;
78+
79+ let wasm_runtime_dir = crates_dir. join ( "wasm_runtime" ) ;
80+ if wasm_runtime_dir. exists ( ) {
81+ return wasm_runtime_dir;
82+ }
83+
84+ panic ! (
85+ r#"
86+ The wasm_runtime directory not found in the expected locations.
87+ If you are using hyperlight-wasm from a crates.io release, please file an issue: https://github.com/hyperlight-dev/hyperlight-wasm/issues
88+ "#
89+ ) ;
90+ }
91+
3392fn build_wasm_runtime ( ) -> PathBuf {
34- let proj_dir = env:: var_os ( "CARGO_MANIFEST_DIR " ) . unwrap ( ) ;
93+ let cargo_bin = env:: var_os ( "CARGO " ) . unwrap ( ) ;
3594 let profile = env:: var_os ( "PROFILE" ) . unwrap ( ) ;
95+ let out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ;
96+
97+ let target_dir = Path :: new ( "" ) . join ( & out_dir) . join ( "target" ) ;
98+
99+ let in_repo_dir = get_wasm_runtime_path ( ) ;
36100
37- let in_repo_dir = PathBuf :: from ( & proj_dir)
38- . parent ( )
39- . unwrap_or_else ( || panic ! ( "could not find parent of cargo manifest directory" ) )
40- . join ( "wasm_runtime" ) ;
41101 if !in_repo_dir. exists ( ) {
42- panic ! ( "hyperlight_wasm does not yet support being compiled from a release package " ) ;
102+ panic ! ( "missing wasm_runtime in-tree dependency " ) ;
43103 }
44- print ! ( "cargo::rerun-if-changed=" ) ;
45- let _ = std:: io:: stdout ( )
46- . write_all ( AsRef :: < std:: ffi:: OsStr > :: as_ref ( & in_repo_dir) . as_encoded_bytes ( ) ) ;
47- println ! ( ) ;
104+
105+ println ! ( "cargo::rerun-if-changed={}" , in_repo_dir. display( ) ) ;
48106 println ! ( "cargo::rerun-if-env-changed=WIT_WORLD" ) ;
49107 // the PROFILE env var unfortunately only gives us 1 bit of "dev or release"
50108 let cargo_profile = if profile == "debug" { "dev" } else { "release" } ;
51- let mut cmd = std:: process:: Command :: new ( "cargo" ) ;
109+ let mut cmd = std:: process:: Command :: new ( cargo_bin ) ;
52110
53111 // Clear the variables that control Cargo's behaviour (as listed
54112 // at https://doc.rust-lang.org/cargo/reference/environment-variables.html):
@@ -57,7 +115,12 @@ fn build_wasm_runtime() -> PathBuf {
57115 env_vars. retain ( |( key, _) | !key. starts_with ( "CARGO_" ) ) ;
58116
59117 let cmd = cmd
60- . args ( [ "build" , & format ! ( "--profile={}" , cargo_profile) , "-v" ] )
118+ . arg ( "build" )
119+ . arg ( "--profile" )
120+ . arg ( cargo_profile)
121+ . arg ( "-v" )
122+ . arg ( "--target-dir" )
123+ . arg ( & target_dir)
61124 . current_dir ( & in_repo_dir)
62125 . env_clear ( )
63126 . envs ( env_vars) ;
@@ -68,8 +131,7 @@ fn build_wasm_runtime() -> PathBuf {
68131 if !status. success ( ) {
69132 panic ! ( "could not compile wasm_runtime" ) ;
70133 }
71- let resource = in_repo_dir
72- . join ( "target" )
134+ let resource = target_dir
73135 . join ( "x86_64-unknown-none" )
74136 . join ( profile)
75137 . join ( "wasm_runtime" ) ;
0 commit comments