Skip to content

Commit 8d0e2fd

Browse files
committed
let wasm_runtime build its own toolchain
Signed-off-by: Jorge Prendes <[email protected]>
1 parent 0805a79 commit 8d0e2fd

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

src/hyperlight_wasm/build.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,22 @@ limitations under the License.
2424

2525
use std::fs::OpenOptions;
2626
use std::io::Write;
27+
use std::iter::once;
2728
use std::path::{Path, PathBuf};
2829
use std::{env, fs};
2930

3031
use anyhow::Result;
3132
use 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+
3343
fn 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));

src/wasm_runtime/build.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ See the License for the specific language governing permissions and
1414
limitations 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

2020
use cargo_metadata::{MetadataCommand, Package};
2121

2222
fn main() {
23+
let toolchain_dir = env::var_os("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT").unwrap();
24+
let toolchain_dir = PathBuf::from(toolchain_dir);
25+
let clang_path = toolchain_dir.join("clang");
26+
27+
assert!(
28+
clang_path.exists(),
29+
"could not find clang at {clang_path:?}"
30+
);
31+
2332
println!("cargo:rerun-if-changed=.");
2433
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-
}
32-
33-
// Get the wasmtime_platform.h file from the appropriate wasm release
3434

3535
// get the version of the wasmtime crate
3636

@@ -63,7 +63,7 @@ fn main() {
6363

6464
cfg.include("src/include");
6565
cfg.file("src/platform.c");
66-
cfg.compiler("clang");
66+
cfg.compiler(clang_path);
6767
if cfg!(windows) {
6868
env::set_var("AR_x86_64_unknown_none", "llvm-ar");
6969
}

0 commit comments

Comments
 (0)