@@ -14,21 +14,73 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- use std:: path:: Path ;
18- use std:: { env, fs, path } ;
17+ use std:: path:: { Path , PathBuf } ;
18+ use std:: { env, fs} ;
1919
2020use cargo_metadata:: { MetadataCommand , Package } ;
2121
22+ fn ensure_toolchain ( ) -> PathBuf {
23+ let out_dir = env:: var ( "OUT_DIR" ) . unwrap ( ) ;
24+ let out_dir = PathBuf :: from ( out_dir) ;
25+ let toolchain_dir = env:: var ( "HYPERLIGHT_GUEST_TOOLCHAIN_ROOT" )
26+ . map ( PathBuf :: from)
27+ . unwrap_or_else ( |_| out_dir. join ( "toolchain" ) ) ;
28+
29+ if toolchain_dir. join ( "clang" ) . exists ( ) {
30+ return toolchain_dir;
31+ }
32+
33+ let cargo_bin = env:: var_os ( "CARGO" ) . unwrap ( ) ;
34+ let mut cargo_cmd = std:: process:: Command :: new ( & cargo_bin) ;
35+
36+ let profile = env:: var_os ( "PROFILE" ) . unwrap ( ) ;
37+ let profile = if profile == "debug" { "dev" } else { "release" } ;
38+
39+ // Clear the variables that control Cargo's behaviour (as listed
40+ // at https://doc.rust-lang.org/cargo/reference/environment-variables.html):
41+ // otherwise the nested build will build the wrong thing
42+ let mut env_vars = env:: vars ( ) . collect :: < Vec < _ > > ( ) ;
43+ env_vars. retain ( |( key, _) | !key. starts_with ( "CARGO_" ) ) ;
44+
45+ // we need to build hyperlight-guest-bin dependency of wasm_runtime, before wasm_runtime
46+ let cmd = cargo_cmd
47+ . arg ( "build" )
48+ . arg ( "--profile" )
49+ . arg ( profile)
50+ . arg ( "--package" )
51+ . arg ( "hyperlight-guest-bin" )
52+ . arg ( "-v" )
53+ . arg ( "--target-dir" )
54+ . arg ( out_dir. join ( "toolchain-build" ) )
55+ . env_clear ( )
56+ . envs ( env_vars)
57+ . env ( "HYPERLIGHT_GUEST_TOOLCHAIN_ROOT" , & toolchain_dir) ;
58+
59+ let status = cmd
60+ . status ( )
61+ . unwrap_or_else ( |e| panic ! ( "could not run build hyperlight toolchain: {}" , e) ) ;
62+
63+ if !status. success ( ) {
64+ panic ! ( "could not compile wasm_runtime" ) ;
65+ }
66+
67+ assert ! (
68+ toolchain_dir. join( "clang" ) . exists( ) ,
69+ "could not find toolchain at {toolchain_dir:?} after building hyperlight-guest-bin" ,
70+ ) ;
71+
72+ toolchain_dir
73+ }
74+
2275fn main ( ) {
76+ let toolchain_dir = ensure_toolchain ( ) ;
77+
2378 println ! ( "cargo:rerun-if-changed=." ) ;
2479 let mut cfg = cc:: Build :: new ( ) ;
25- if let Some ( path) = env:: var_os ( "PATH" ) {
26- let paths: Vec < _ > = env:: split_paths ( & path) . collect ( ) ;
27- let toolchain_path =
28- path:: PathBuf :: from ( env:: var_os ( "HYPERLIGHT_GUEST_TOOLCHAIN_ROOT" ) . unwrap ( ) ) ;
29- let joined = env:: join_paths ( std:: iter:: once ( toolchain_path) . chain ( paths) ) . unwrap ( ) ;
30- env:: set_var ( "PATH" , & joined) ;
31- }
80+ let path = env:: var_os ( "PATH" ) . unwrap_or_default ( ) ;
81+ let paths = env:: split_paths ( & path) ;
82+ let joined = env:: join_paths ( paths. chain ( std:: iter:: once ( toolchain_dir) ) ) . unwrap ( ) ;
83+ env:: set_var ( "PATH" , & joined) ;
3284
3385 // Get the wasmtime_platform.h file from the appropriate wasm release
3486
0 commit comments