Skip to content

Commit f91d0e2

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

File tree

3 files changed

+61
-33
lines changed

3 files changed

+61
-33
lines changed

src/hyperlight_wasm/build.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,34 +106,13 @@ fn build_wasm_runtime() -> PathBuf {
106106
println!("cargo::rerun-if-env-changed=WIT_WORLD");
107107
// the PROFILE env var unfortunately only gives us 1 bit of "dev or release"
108108
let cargo_profile = if profile == "debug" { "dev" } else { "release" };
109-
let mut cargo_cmd = std::process::Command::new(&cargo_bin);
110109

111110
// Clear the variables that control Cargo's behaviour (as listed
112111
// at https://doc.rust-lang.org/cargo/reference/environment-variables.html):
113112
// otherwise the nested build will build the wrong thing
114113
let mut env_vars = env::vars().collect::<Vec<_>>();
115114
env_vars.retain(|(key, _)| !key.starts_with("CARGO_"));
116115

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-
}
136-
137116
let mut cargo_cmd = std::process::Command::new(&cargo_bin);
138117
let cmd = cargo_cmd
139118
.arg("build")

src/wasm_runtime/.cargo/config.toml

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

13-
[env]
14-
HYPERLIGHT_GUEST_TOOLCHAIN_ROOT = { value = "guest-toolchain", relative = true }
15-
1613
[profile.release]
1714
panic = "abort"
1815

src/wasm_runtime/build.rs

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,73 @@ 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

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+
2275
fn 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

Comments
 (0)