@@ -24,12 +24,22 @@ limitations under the License.
2424
2525use  std:: fs:: OpenOptions ; 
2626use  std:: io:: Write ; 
27+ use  std:: iter:: once; 
2728use  std:: path:: { Path ,  PathBuf } ; 
2829use  std:: { env,  fs} ; 
2930
3031use  anyhow:: Result ; 
3132use  built:: write_built_file; 
3233
34+ fn  append_to_path ( path :  impl  Into < PathBuf > )  { 
35+     let  path = path. into ( ) ; 
36+     let  paths = env:: var_os ( "PATH" ) . unwrap_or_default ( ) ; 
37+     let  paths = env:: split_paths ( & paths) ; 
38+     let  paths = once ( path) . chain ( paths) ; 
39+     let  paths = env:: join_paths ( paths) . unwrap ( ) ; 
40+     unsafe  {  env:: set_var ( "PATH" ,  & paths)  } ; 
41+ } 
42+ 
3343fn  get_wasm_runtime_path ( )  -> PathBuf  { 
3444    let  manifest_dir = env:: var_os ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ; 
3545    let  manifest_dir = PathBuf :: from ( manifest_dir) ; 
@@ -95,6 +105,7 @@ fn build_wasm_runtime() -> PathBuf {
95105    let  out_dir = env:: var_os ( "OUT_DIR" ) . unwrap ( ) ; 
96106
97107    let  target_dir = Path :: new ( "" ) . join ( & out_dir) . join ( "target" ) ; 
108+     let  toolchain_dir = Path :: new ( "" ) . join ( & out_dir) . join ( "toolchain" ) ; 
98109
99110    let  in_repo_dir = get_wasm_runtime_path ( ) ; 
100111
@@ -106,33 +117,14 @@ fn build_wasm_runtime() -> PathBuf {
106117    println ! ( "cargo::rerun-if-env-changed=WIT_WORLD" ) ; 
107118    // the PROFILE env var unfortunately only gives us 1 bit of "dev or release" 
108119    let  cargo_profile = if  profile == "debug"  {  "dev"  }  else  {  "release"  } ; 
109-     let  mut  cargo_cmd = std:: process:: Command :: new ( & cargo_bin) ; 
110120
111121    // Clear the variables that control Cargo's behaviour (as listed 
112122    // at https://doc.rust-lang.org/cargo/reference/environment-variables.html): 
113123    // otherwise the nested build will build the wrong thing 
114124    let  mut  env_vars = env:: vars ( ) . collect :: < Vec < _ > > ( ) ; 
115125    env_vars. retain ( |( key,  _) | !key. starts_with ( "CARGO_" ) ) ; 
116126
117-     // we need to build hyperlight-guest-bin dependency of wasm_runtime, before wasm_runtime 
118-     let  cmd = cargo_cmd
119-         . arg ( "build" ) 
120-         . arg ( "--profile" ) 
121-         . arg ( cargo_profile) 
122-         . arg ( "--package" ) 
123-         . arg ( "hyperlight-guest-bin" ) 
124-         . arg ( "-v" ) 
125-         . arg ( "--target-dir" ) 
126-         . arg ( & target_dir) 
127-         . current_dir ( & in_repo_dir) 
128-         . env_clear ( ) 
129-         . envs ( env_vars. clone ( ) ) ; 
130-     let  status = cmd
131-         . status ( ) 
132-         . unwrap_or_else ( |e| panic ! ( "could not run cargo build hyperlight-guest-bin: {}" ,  e) ) ; 
133-     if  !status. success ( )  { 
134-         panic ! ( "could not compile wasm_runtime" ) ; 
135-     } 
127+     append_to_path ( & toolchain_dir) ; 
136128
137129    let  mut  cargo_cmd = std:: process:: Command :: new ( & cargo_bin) ; 
138130    let  cmd = cargo_cmd
@@ -144,8 +136,8 @@ fn build_wasm_runtime() -> PathBuf {
144136        . arg ( & target_dir) 
145137        . current_dir ( & in_repo_dir) 
146138        . env_clear ( ) 
147-         . envs ( env_vars) ; 
148- 
139+         . envs ( env_vars) 
140+          . env ( "HYPERLIGHT_GUEST_TOOLCHAIN_ROOT" ,   & toolchain_dir ) ; 
149141    let  status = cmd
150142        . status ( ) 
151143        . unwrap_or_else ( |e| panic ! ( "could not run cargo build wasm_runtime: {}" ,  e) ) ; 
0 commit comments