diff --git a/src/hyperlight_wasm/build.rs b/src/hyperlight_wasm/build.rs index 5767a94..75ac7f9 100644 --- a/src/hyperlight_wasm/build.rs +++ b/src/hyperlight_wasm/build.rs @@ -22,14 +22,24 @@ limitations under the License. // this file is included in lib.rs. // The wasm_runtime binary is expected to be in the x64/{config} directory. +use std::ffi::OsString; use std::fs::OpenOptions; use std::io::Write; +use std::iter::once; use std::path::{Path, PathBuf}; use std::{env, fs}; use anyhow::Result; use built::write_built_file; +fn path_with(path: impl Into) -> OsString { + let path = path.into(); + let paths = env::var_os("PATH").unwrap_or_default(); + let paths = env::split_paths(&paths); + let paths = once(path).chain(paths); + env::join_paths(paths).unwrap() +} + fn get_wasm_runtime_path() -> PathBuf { let manifest_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap(); let manifest_dir = PathBuf::from(manifest_dir); @@ -95,6 +105,7 @@ fn build_wasm_runtime() -> PathBuf { let out_dir = env::var_os("OUT_DIR").unwrap(); let target_dir = Path::new("").join(&out_dir).join("target"); + let toolchain_dir = Path::new("").join(&out_dir).join("toolchain"); let in_repo_dir = get_wasm_runtime_path(); @@ -106,7 +117,6 @@ fn build_wasm_runtime() -> PathBuf { println!("cargo::rerun-if-env-changed=WIT_WORLD"); // the PROFILE env var unfortunately only gives us 1 bit of "dev or release" let cargo_profile = if profile == "debug" { "dev" } else { "release" }; - let mut cargo_cmd = std::process::Command::new(&cargo_bin); // Clear the variables that control Cargo's behaviour (as listed // at https://doc.rust-lang.org/cargo/reference/environment-variables.html): @@ -114,26 +124,6 @@ fn build_wasm_runtime() -> PathBuf { let mut env_vars = env::vars().collect::>(); env_vars.retain(|(key, _)| !key.starts_with("CARGO_")); - // we need to build hyperlight-guest-bin dependency of wasm_runtime, before wasm_runtime - let cmd = cargo_cmd - .arg("build") - .arg("--profile") - .arg(cargo_profile) - .arg("--package") - .arg("hyperlight-guest-bin") - .arg("-v") - .arg("--target-dir") - .arg(&target_dir) - .current_dir(&in_repo_dir) - .env_clear() - .envs(env_vars.clone()); - let status = cmd - .status() - .unwrap_or_else(|e| panic!("could not run cargo build hyperlight-guest-bin: {}", e)); - if !status.success() { - panic!("could not compile wasm_runtime"); - } - let mut cargo_cmd = std::process::Command::new(&cargo_bin); let cmd = cargo_cmd .arg("build") @@ -144,8 +134,9 @@ fn build_wasm_runtime() -> PathBuf { .arg(&target_dir) .current_dir(&in_repo_dir) .env_clear() - .envs(env_vars); - + .envs(env_vars) + .env("PATH", path_with(&toolchain_dir)) + .env("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT", &toolchain_dir); let status = cmd .status() .unwrap_or_else(|e| panic!("could not run cargo build wasm_runtime: {}", e)); diff --git a/src/wasm_runtime/build.rs b/src/wasm_runtime/build.rs index 59ccd2e..2244ce8 100644 --- a/src/wasm_runtime/build.rs +++ b/src/wasm_runtime/build.rs @@ -14,23 +14,23 @@ See the License for the specific language governing permissions and limitations under the License. */ -use std::path::Path; -use std::{env, fs, path}; +use std::path::{Path, PathBuf}; +use std::{env, fs}; use cargo_metadata::{MetadataCommand, Package}; fn main() { + let toolchain_dir = env::var_os("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT").unwrap(); + let toolchain_dir = PathBuf::from(toolchain_dir); + let clang_path = toolchain_dir.join("clang"); + + assert!( + clang_path.exists(), + "could not find clang at {clang_path:?}" + ); + println!("cargo:rerun-if-changed=."); let mut cfg = cc::Build::new(); - if let Some(path) = env::var_os("PATH") { - let paths: Vec<_> = env::split_paths(&path).collect(); - let toolchain_path = - path::PathBuf::from(env::var_os("HYPERLIGHT_GUEST_TOOLCHAIN_ROOT").unwrap()); - let joined = env::join_paths(std::iter::once(toolchain_path).chain(paths)).unwrap(); - env::set_var("PATH", &joined); - } - - // Get the wasmtime_platform.h file from the appropriate wasm release // get the version of the wasmtime crate @@ -63,7 +63,7 @@ fn main() { cfg.include("src/include"); cfg.file("src/platform.c"); - cfg.compiler("clang"); + cfg.compiler(clang_path); if cfg!(windows) { env::set_var("AR_x86_64_unknown_none", "llvm-ar"); }