Skip to content

Commit d843f15

Browse files
committed
Allow using wasm_runtime as a vendored tar file
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent c4693ce commit d843f15

7 files changed

Lines changed: 137 additions & 19 deletions

File tree

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Justfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ latest-release:= if os() == "windows" {"$(git tag -l --sort=v:refname | select -
66

77
set windows-shell := ["pwsh.exe", "-NoLogo", "-Command"]
88

9+
make-vendor-tar:
10+
tar cf ./src/hyperlight_wasm/vendor.tar \
11+
--exclude-vcs-ignores \
12+
-C ./src wasm_runtime hyperlight_wasm_macro
13+
914
build-all target=default-target: (build target) (build-wasm-examples target) (build-rust-wasm-examples target) (build-wasm-runtime target)
1015

1116
build target=default-target features="": (build-wasm-runtime target) (fmt-check)

src/hyperlight_wasm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor.tar

src/hyperlight_wasm/Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ name = "hyperlight-wasm"
33
version = "0.1.0"
44
edition = "2024"
55
rust-version = "1.85"
6-
include = ["*"] # Make sure wasm_runtime is included!
6+
description = "Library that enables wasm modules and components to be run inside lightweight Virtual Machine backed Sandbox. It is built on top of Hyperlight."
7+
license = "Apache-2.0"
8+
repository = "https://github.com/hyperlight-dev/hyperlight-wasm"
9+
include = [
10+
"/src",
11+
"/scripts",
12+
"/examples",
13+
"benches",
14+
"/build.rs",
15+
"/vendor.tar", # Make sure vendor.tar is included!
16+
]
717

818
[lib]
919
name = "hyperlight_wasm"
@@ -58,6 +68,7 @@ blake3 = "1.8"
5868
built = { version = "0.8.0", features = ["chrono", "git2"] }
5969
anyhow = { version = "1.0" }
6070
goblin = "0.10.0"
71+
tar = "0.4.44"
6172

6273
[features]
6374
default = ["function_call_metrics", "kvm", "mshv2"]

src/hyperlight_wasm/build.rs

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,78 @@ use std::{env, fs};
3030
use anyhow::Result;
3131
use built::write_built_file;
3232

33+
fn get_wasm_runtime_path() -> PathBuf {
34+
let manifest_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
35+
let manifest_dir = PathBuf::from(manifest_dir);
36+
37+
let tar_path = manifest_dir.join("vendor.tar");
38+
39+
let out_dir = env::var_os("OUT_DIR").unwrap();
40+
let out_dir = PathBuf::from(out_dir);
41+
let vendor_dir = out_dir.join("vendor");
42+
43+
if vendor_dir.exists() {
44+
fs::remove_dir_all(&vendor_dir).unwrap();
45+
}
46+
47+
println!("cargo::rerun-if-changed={}", tar_path.display());
48+
49+
if tar_path.exists() {
50+
let out_dir = env::var_os("OUT_DIR").unwrap();
51+
let out_dir = PathBuf::from(out_dir);
52+
let vendor_dir = out_dir.join("vendor");
53+
54+
let mut tar = tar::Archive::new(fs::File::open(&tar_path).unwrap());
55+
tar.unpack(&vendor_dir).unwrap();
56+
57+
let wasm_runtime_dir = vendor_dir.join("wasm_runtime");
58+
59+
println!(
60+
"cargo::warning=using vendor wasm_runtime from {}",
61+
tar_path.display()
62+
);
63+
return wasm_runtime_dir;
64+
}
65+
66+
let crates_dir = manifest_dir.parent().unwrap();
67+
68+
#[cfg(unix)]
69+
std::os::unix::fs::symlink(crates_dir, &vendor_dir).unwrap();
70+
71+
#[cfg(not(unix))]
72+
std::os::windows::fs::symlink_dir(crates_dir, &vendor_dir).unwrap();
73+
74+
let wasm_runtime_dir = crates_dir.join("wasm_runtime");
75+
if wasm_runtime_dir.exists() {
76+
return wasm_runtime_dir;
77+
}
78+
79+
panic!(
80+
r#"
81+
The wasm_runtime directory not found in the expected locations.
82+
If you are using hyperlight-wasm from a crates.io release, please file an issue: https://github.com/hyperlight-dev/hyperlight-wasm/issues
83+
"#
84+
);
85+
}
86+
3387
fn build_wasm_runtime() -> PathBuf {
34-
let proj_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
88+
let cargo_bin = env::var_os("CARGO").unwrap();
3589
let profile = env::var_os("PROFILE").unwrap();
90+
let out_dir = env::var_os("OUT_DIR").unwrap();
91+
92+
let target_dir = Path::new("").join(&out_dir).join("target");
93+
94+
let in_repo_dir = get_wasm_runtime_path();
3695

37-
let in_repo_dir = PathBuf::from(&proj_dir)
38-
.parent()
39-
.unwrap_or_else(|| panic!("could not find parent of cargo manifest directory"))
40-
.join("wasm_runtime");
4196
if !in_repo_dir.exists() {
42-
panic!("hyperlight_wasm does not yet support being compiled from a release package");
97+
panic!("missing wasm_runtime in-tree dependency");
4398
}
44-
print!("cargo::rerun-if-changed=");
45-
let _ = std::io::stdout()
46-
.write_all(AsRef::<std::ffi::OsStr>::as_ref(&in_repo_dir).as_encoded_bytes());
47-
println!();
99+
100+
println!("cargo::rerun-if-changed={}", in_repo_dir.display());
48101
println!("cargo::rerun-if-env-changed=WIT_WORLD");
49102
// the PROFILE env var unfortunately only gives us 1 bit of "dev or release"
50103
let cargo_profile = if profile == "debug" { "dev" } else { "release" };
51-
let mut cmd = std::process::Command::new("cargo");
104+
let mut cmd = std::process::Command::new(cargo_bin);
52105

53106
// Clear the variables that control Cargo's behaviour (as listed
54107
// at https://doc.rust-lang.org/cargo/reference/environment-variables.html):
@@ -57,7 +110,12 @@ fn build_wasm_runtime() -> PathBuf {
57110
env_vars.retain(|(key, _)| !key.starts_with("CARGO_"));
58111

59112
let cmd = cmd
60-
.args(["build", &format!("--profile={}", cargo_profile), "-v"])
113+
.arg("build")
114+
.arg("--profile")
115+
.arg(cargo_profile)
116+
.arg("-v")
117+
.arg("--target-dir")
118+
.arg(&target_dir)
61119
.current_dir(&in_repo_dir)
62120
.env_clear()
63121
.envs(env_vars);
@@ -68,8 +126,7 @@ fn build_wasm_runtime() -> PathBuf {
68126
if !status.success() {
69127
panic!("could not compile wasm_runtime");
70128
}
71-
let resource = in_repo_dir
72-
.join("target")
129+
let resource = target_dir
73130
.join("x86_64-unknown-none")
74131
.join(profile)
75132
.join("wasm_runtime");

src/hyperlight_wasm/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ mod tests {
7272
fn test_wasmtime_version() {
7373
let wasmtime_version = super::get_wasmtime_version();
7474
// get the wasmtime version from the wasm_runtime binary's Cargo.toml
75-
let proj_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
76-
let cargo_toml_path = Path::new(&proj_dir)
77-
.parent()
78-
.unwrap()
75+
let cargo_toml_path = Path::new(env!("OUT_DIR"))
76+
.join("vendor")
7977
.join("wasm_runtime")
8078
.join("Cargo.toml");
8179
let cargo_toml_content =

src/wasm_runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ cc = "1.2"
2323
cfg-if = { version = "1" }
2424
cargo_metadata = "0.19"
2525
reqwest = {version = "0.12", default-features = false, features = ["blocking","rustls-tls"] }
26+
27+
[workspace] # indicate that this crate is not part of any workspace

0 commit comments

Comments
 (0)