Skip to content

Commit 04a9be5

Browse files
committed
Add Component model example and CI
Signed-off-by: James Sturtevant <[email protected]>
1 parent 5acda75 commit 04a9be5

File tree

13 files changed

+608
-6
lines changed

13 files changed

+608
-6
lines changed

.github/workflows/dep_rust.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ jobs:
7676
with:
7777
name: guest-modules
7878
path: ./x64/${{ matrix.config }}
79+
80+
- name: Build Rust component model examples
81+
run: |
82+
# this must be build before the formatting and other jobs run
83+
# because the component model example depends on the wasm component built here
84+
just ensure-tools
85+
just build-rust-component-examples ${{ matrix.config }}
7986
8087
- name: Fmt
8188
run: just fmt-check
@@ -124,6 +131,10 @@ jobs:
124131
# required for gh cli when downloading
125132
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
126133

134+
- name: Test Component Model Examples
135+
run: just examples-components ${{ matrix.config }} ${{ matrix.hypervisor == 'mshv3' && 'mshv3' || ''}}
136+
working-directory: ./src/hyperlight_wasm
137+
127138
### Benchmarks ###
128139

129140
- name: Download benchmarks from "latest"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,4 @@ target/
478478

479479
# MSVC Windows builds of rustc generate these, which store debugging information
480480
*.pdb
481+
src/component_sample/**/*.wasm

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
2-
members = ["src/hyperlight_wasm", "src/examples_common", "src/hyperlight_wasm_aot" ]
3-
exclude = [ "src/wasm_runtime", "src/rust_wasm_samples", "src/hyperlight_wasm_macro" ]
2+
members = [ "src/hyperlight_wasm", "src/examples_common", "src/hyperlight_wasm_aot" ]
3+
exclude = [ "src/wasm_runtime", "src/rust_wasm_samples", "src/hyperlight_wasm_macro", "src/component_sample" ]
44
resolver = "2"
55

66
[workspace.dependencies]

Justfile

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@ default-tag:= "latest"
33
build-wasm-examples-command := if os() == "windows" { "./src/hyperlight_wasm/scripts/build-wasm-examples.bat" } else { "./src/hyperlight_wasm/scripts/build-wasm-examples.sh" }
44
mkdir-arg := if os() == "windows" { "-Force" } else { "-p" }
55
latest-release:= if os() == "windows" {"$(git tag -l --sort=v:refname | select -last 2 | select -first 1)"} else {`git tag -l --sort=v:refname | tail -n 2 | head -n 1`}
6+
wit-world := if os() == "windows" { "$env:WIT_WORLD=\"" + justfile_directory() + "\\src\\component_sample\\wit\\component-world.wasm" + "\";" } else { "WIT_WORLD=" + justfile_directory() + "/src/component_sample/wit/component-world.wasm" }
67

78
set windows-shell := ["pwsh.exe", "-NoLogo", "-Command"]
89

9-
build-all target=default-target: (build target) (build-wasm-examples target) (build-rust-wasm-examples target) (build-wasm-runtime target)
10+
ensure-tools:
11+
cargo install --locked wasm-tools --version 1.235.0
12+
cargo install cargo-component --locked --version 0.21.1
13+
14+
build-all target=default-target: (build target) (build-wasm-examples target) (build-rust-wasm-examples target) (build-wasm-runtime target) (build-rust-component-examples target)
1015

1116
build target=default-target features="": (build-wasm-runtime target) (fmt-check)
1217
cargo build {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F " + features } }} --verbose --profile={{ if target == "debug" {"dev"} else { target } }}
@@ -31,23 +36,37 @@ build-rust-wasm-examples target=default-target: (mkdir-redist target)
3136
cargo run -p hyperlight-wasm-aot compile ./src/rust_wasm_samples/target/wasm32-unknown-unknown/{{ target }}/rust_wasm_samples.wasm ./x64/{{ target }}/rust_wasm_samples.aot
3237
cp ./x64/{{ target }}/rust_wasm_samples.aot ./x64/{{ target }}/rust_wasm_samples.wasm
3338

39+
build-rust-component-examples target=default-target:
40+
wasm-tools component wit ./src/component_sample/wit/example.wit -w -o ./src/component_sample/wit/component-world.wasm
41+
# use cargo component so we don't get all the wasi imports https://github.com/bytecodealliance/cargo-component?tab=readme-ov-file#relationship-with-wasm32-wasip2
42+
# we also explicitly target wasm32-unknown-unknown since cargo component might try to pull in wasi imports https://github.com/bytecodealliance/cargo-component/issues/290
43+
rustup target add wasm32-unknown-unknown
44+
cd ./src/component_sample && cargo component build --target wasm32-unknown-unknown --profile={{ if target == "debug" {"dev"} else { target } }}
45+
cargo run -p hyperlight-wasm-aot compile --component ./src/component_sample/target/wasm32-unknown-unknown/{{ target }}/component_sample.wasm ./x64/{{ target }}/component_sample.aot
46+
cp ./x64/{{ target }}/component_sample.aot ./x64/{{ target }}/component_sample.wasm
47+
3448
check target=default-target:
3549
cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
3650
cd src/rust_wasm_samples && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
51+
cd src/component_sample && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
3752
cd src/wasm_runtime && cargo check --profile={{ if target == "debug" {"dev"} else { target } }}
3853

3954
fmt-check:
4055
rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
4156
cd src/rust_wasm_samples && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
57+
cd src/component_sample && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
4258
cd src/wasm_runtime && rustup toolchain install nightly -c rustfmt && cargo +nightly fmt -v --all -- --check
43-
fmt:
59+
fmt:
60+
rustup toolchain install nightly -c rustfmt
4461
cargo +nightly fmt --all
45-
cd src/rust_wasm_samples && cargo +nightly fmt
46-
cd src/wasm_runtime && cargo +nightly fmt
62+
cd src/rust_wasm_samples && cargo +nightly fmt -v --all
63+
cd src/component_sample && cargo +nightly fmt -v --all
64+
cd src/wasm_runtime && cargo +nightly fmt -v --all
4765

4866
clippy target=default-target: (check target)
4967
cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
5068
cd src/rust_wasm_samples && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
69+
cd src/component_sample && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
5170
cd src/wasm_runtime && cargo clippy --profile={{ if target == "debug" {"dev"} else { target } }} --all-targets --all-features -- -D warnings
5271

5372
# TESTING
@@ -71,6 +90,9 @@ examples-ci target=default-target features="": (build-rust-wasm-examples target)
7190
cargo run {{ if features =="" {''} else {"--no-default-features -F function_call_metrics," + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example metrics
7291
cargo run {{ if features =="" {"--no-default-features --features kvm,mshv2"} else {"--no-default-features -F function_call_metrics," + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example metrics
7392

93+
examples-components target=default-target features="": (build-rust-component-examples target)
94+
{{ wit-world }} cargo run {{ if features =="" {''} else {"--no-default-features -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example component_example
95+
7496
# warning, compares to and then OVERWRITES the given baseline
7597
bench-ci baseline target=default-target features="":
7698
cd src/hyperlight_wasm && cargo bench --profile={{ if target == "debug" {"dev"} else { target } }} {{ if features =="" {''} else { "--features " + features } }} -- --verbose --save-baseline {{baseline}}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Make sure the following are installed:
2121
1. [pwsh](https://github.com/PowerShell/PowerShell)
2222
1. [git](https://gitforwindows.org/)
2323
1. [GitHub CLI](https://github.com/cli/cli#installation)
24+
1. [wasm-tools](https://github.com/bytecodealliance/wasm-tools?tab=readme-ov-file#installation)
25+
1. [cargo component](https://github.com/bytecodealliance/cargo-component?tab=readme-ov-file#installation)
2426

2527
Ensure that Windows Hypervisor Platform is enabled:
2628

@@ -38,6 +40,8 @@ Make sure the following are installed:
3840
1. [Rust](https://www.rust-lang.org/tools/install) `curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh`
3941
1. [just](https://github.com/casey/just). is used as a command runner `cargo install just`. Do not install `just` through a package manager as it may install an older incompatible version, make sure to use at least version 1.5.0 if not installed through cargo.
4042
1. [GitHub CLI](https://github.com/cli/cli#installation)
43+
1. [wasm-tools](https://github.com/bytecodealliance/wasm-tools?tab=readme-ov-file#installation)
44+
1. [cargo component](https://github.com/bytecodealliance/cargo-component?tab=readme-ov-file#installation)
4145

4246
Ensure that KVM is enabled: On an Azure VM using a size that supports nested virtualisation:
4347

@@ -94,6 +98,17 @@ generate bindings from the same component type in the host. For a
9498
complete (albeit small) example of this, see [this
9599
example](https://aka.ms/hyperlight-wasm-sockets-sample).
96100

101+
### Debugging the macro
102+
103+
You can get more detailed error messages by expanding the Macro locally:
104+
105+
```
106+
cargo clean -p hyperlight-wasm
107+
WIT_WORLD=</path/to/output.wasm> HYPERLIGHT_COMPONENT_MACRO_DEBUG=/tmp/guest.rs cargo build -p hyperlight-wasm
108+
HYPERLIGHT_COMPONENT_MACRO_DEBUG=/tmp/host.rs cargo build
109+
```
110+
111+
97112
## Code of Conduct
98113

99114
This project has adopted the [Microsoft Open Source Code of

src/component_sample/Cargo.lock

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

src/component_sample/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "component_sample"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
wit-bindgen-rt = { version = "0.41.0", features = ["bitflags"] }
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[package.metadata.component]
13+
package = "component-sample:example"
14+
15+
[package.metadata.component.target]
16+
path = "wit/example.wit"
17+
world = "example"
18+
19+
[package.metadata.component.target.dependencies]
20+

0 commit comments

Comments
 (0)