Skip to content

Commit 54d03f6

Browse files
committed
fixup! let wasm_runtime build its own toolchain
Signed-off-by: Jorge Prendes <[email protected]>
1 parent 96c7a84 commit 54d03f6

File tree

3 files changed

+25
-55
lines changed

3 files changed

+25
-55
lines changed

src/hyperlight_wasm/build.rs

Lines changed: 15 additions & 2 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

@@ -113,6 +124,8 @@ fn build_wasm_runtime() -> PathBuf {
113124
let mut env_vars = env::vars().collect::<Vec<_>>();
114125
env_vars.retain(|(key, _)| !key.starts_with("CARGO_"));
115126

127+
append_to_path(&toolchain_dir);
128+
116129
let mut cargo_cmd = std::process::Command::new(&cargo_bin);
117130
let cmd = cargo_cmd
118131
.arg("build")
@@ -123,8 +136,8 @@ fn build_wasm_runtime() -> PathBuf {
123136
.arg(&target_dir)
124137
.current_dir(&in_repo_dir)
125138
.env_clear()
126-
.envs(env_vars);
127-
139+
.envs(env_vars)
140+
.env("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT", &toolchain_dir);
128141
let status = cmd
129142
.status()
130143
.unwrap_or_else(|e| panic!("could not run cargo build wasm_runtime: {}", e));

src/wasm_runtime/.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ rustflags = [
1010
]
1111
linker = "rust-lld"
1212

13+
[env]
14+
HYPERLIGHT_GUEST_TOOLCHAIN_ROOT = { value = "guest-toolchain", relative = true }
15+
1316
[profile.release]
1417
panic = "abort"
1518

src/wasm_runtime/build.rs

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,16 @@ use std::{env, fs};
1919

2020
use 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-
}
22+
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");
6626

6727
assert!(
68-
toolchain_dir.join("clang").exists(),
69-
"could not find toolchain at {toolchain_dir:?} after building hyperlight-guest-bin",
28+
clang_path.exists(),
29+
"could not find clang at {clang_path:?}"
7030
);
7131

72-
toolchain_dir
73-
}
74-
75-
fn main() {
76-
let toolchain_dir = ensure_toolchain();
77-
7832
println!("cargo:rerun-if-changed=.");
7933
let mut cfg = cc::Build::new();
8034

@@ -109,7 +63,7 @@ fn main() {
10963

11064
cfg.include("src/include");
11165
cfg.file("src/platform.c");
112-
cfg.compiler(toolchain_dir.join("clang"));
66+
cfg.compiler(clang_path);
11367
if cfg!(windows) {
11468
env::set_var("AR_x86_64_unknown_none", "llvm-ar");
11569
}

0 commit comments

Comments
 (0)