Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions src/hyperlight_wasm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>) -> 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);
Expand Down Expand Up @@ -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();

Expand All @@ -106,34 +117,13 @@ 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):
// otherwise the nested build will build the wrong thing
let mut env_vars = env::vars().collect::<Vec<_>>();
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")
Expand All @@ -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));
Expand Down
24 changes: 12 additions & 12 deletions src/wasm_runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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");
}
Expand Down