diff --git a/.github/workflows/dep_build_wasm_examples.yml b/.github/workflows/dep_build_wasm_examples.yml index ee2fe4cf..a6cfd980 100644 --- a/.github/workflows/dep_build_wasm_examples.yml +++ b/.github/workflows/dep_build_wasm_examples.yml @@ -54,7 +54,9 @@ jobs: build/*/pulley/*.aot src/tests/rust_guests/component_sample/wit/component-world.wasm src/tests/c_guests/wasmsamples/components/runcomponent-world.wasm - key: wasm-modules-${{ matrix.arch.name }}-${{ matrix.wasmtime_version.name }}-${{ hashFiles('src/tests/**', 'src/hyperlight_wasm_aot/**', 'Cargo.lock', '.cargo/config.toml', 'rust-toolchain.toml', 'Justfile', 'c.just', '.github/workflows/dep_build_wasm_examples.yml') }} + build/*/component-sample-runtime + src/tests/rust_guests/component_sample/target/wasm32-unknown-unknown/*/component_sample.wasm + key: wasm-modules-${{ matrix.arch.name }}-${{ matrix.wasmtime_version.name }}-${{ hashFiles('src/tests/**', 'src/hyperlight_wasm_aot/**', 'src/hyperlight_wasm_runtime/**', 'src/hyperlight_wasm_macro/**', 'Cargo.lock', '.cargo/config.toml', 'rust-toolchain.toml', 'Justfile', 'c.just', '.github/workflows/dep_build_wasm_examples.yml') }} - name: Remove default clang if: steps.modules-cache.outputs.cache-hit != 'true' run: sudo rm /usr/bin/clang @@ -106,6 +108,8 @@ jobs: build/*/pulley/*.aot src/tests/rust_guests/component_sample/wit/component-world.wasm src/tests/c_guests/wasmsamples/components/runcomponent-world.wasm + build/*/component-sample-runtime + src/tests/rust_guests/component_sample/target/wasm32-unknown-unknown/*/component_sample.wasm key: ${{ steps.modules-cache.outputs.cache-primary-key }} # Build jobs compile examples against the WIT worlds, so they ship with the modules. - name: Upload Wasm Modules (wasmtime ${{ matrix.wasmtime_version.name }}) @@ -118,3 +122,5 @@ jobs: build/*/pulley/*.aot src/tests/rust_guests/component_sample/wit/component-world.wasm src/tests/c_guests/wasmsamples/components/runcomponent-world.wasm + build/*/component-sample-runtime + src/tests/rust_guests/component_sample/target/wasm32-unknown-unknown/*/component_sample.wasm diff --git a/.github/workflows/dep_rust.yml b/.github/workflows/dep_rust.yml index 2c90a43b..d52e5835 100644 --- a/.github/workflows/dep_rust.yml +++ b/.github/workflows/dep_rust.yml @@ -177,8 +177,10 @@ jobs: with: dotnet-version: 8.0.x + # One leg builds the runtime and component through Hyperlight.Wasm.Build. + # The others use the prebuilt ones. - name: Test .NET packages - run: just dotnet-test-prebuilt ${{ matrix.config }} ${{ env.FEATURES }} + run: just ${{ matrix.hypervisor == 'kvm' && matrix.cpu == 'amd' && matrix.config == 'debug' && 'dotnet-test-build' || 'dotnet-test-prebuilt' }} ${{ matrix.config }} ${{ env.FEATURES }} - name: Install github-cli (Windows) if: runner.os == 'Windows' diff --git a/CHANGELOG.md b/CHANGELOG.md index e5b108b6..a52f4172 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). * Experimental: add the `experimental_runtime_file` feature, with `SandboxBuilder::with_runtime_file` to load the guest runtime from a file. * Experimental: add the `hyperlight-wasm-capi` crate, a C API that calls component functions with arguments and results in Hyperlight's component wire format. It is not published. * Experimental: add the `Hyperlight.Wasm` and `Hyperlight.Wasm.Build` .NET packages, which run components from .NET with typed C# bindings generated from WIT at build time. See `dotnet/README.md`. They are not published. +* Experimental: add `hyperlight-wasm-aot experimental-build-runtime`, which builds a guest runtime for a WIT world. `Hyperlight.Wasm.Build` runs it for `HyperlightWit` items with `Runtime` set, and precompiles `HyperlightComponent` items. ### Changed diff --git a/Cargo.lock b/Cargo.lock index e0f47fbc..254574dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2090,10 +2090,13 @@ dependencies = [ name = "hyperlight-wasm-aot" version = "0.16.0" dependencies = [ + "cargo-hyperlight", "cargo-util-schemas", "cargo_metadata", "clap", + "hyperlight-wasm-runtime", "object 0.39.1", + "tar", "wasmtime 36.0.18", "wasmtime 47.0.4", ] diff --git a/Justfile b/Justfile index e557e558..9ab5aa17 100644 --- a/Justfile +++ b/Justfile @@ -152,8 +152,8 @@ examples-ci target=default-target features="": (examples-modules target features # Runs the examples-ci examples on the modules build-ci-examples builds. examples-ci-prebuilt target=default-target features="": (run-examples-modules target features) (run-examples-components target features) (use-pulley-examples target) (run-examples-pulley target features) -# Builds every module the CI build jobs load, for the given profiles. C modules build -# in release only. Pulley builds go to build//pulley. +# Builds every module the CI build jobs load, and the .NET tests' guest runtime, for the +# given profiles. C modules build in release only. Pulley builds go to build//pulley. build-ci-examples features="" profiles="debug release": (build-wasm-examples "release" features) #!/usr/bin/env bash set -euo pipefail @@ -168,6 +168,7 @@ build-ci-examples features="" profiles="debug release": (build-wasm-examples "re mv build/$target/rust_wasm_samples.aot build/$target/component_sample.aot build/$target/pulley/ just build-rust-wasm-examples $target {{ features }} just build-rust-component-examples $target {{ features }} + just build-component-sample-runtime $target {{ features }} done [private] @@ -183,17 +184,26 @@ run-examples-components target=default-target features="": {{ wit-world }} {{ wit-world-name-greeter }} cargo run {{ if features =="" {''} else {"--no-default-features -F kvm -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} --example component_greeter_example {{ wit-world }} cargo test -p hyperlight-wasm-capi {{ if features =="" {''} else {"--no-default-features -F kvm -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} -- --include-ignored -# Builds the native libraries and a guest runtime for the component_sample example world, for the .NET tests -dotnet-test-assets target=default-target features="": ensure-cargo-hyperlight +# Builds the native libraries for the .NET tests +dotnet-test-assets target=default-target features="": cargo build -p hyperlight-wasm-capi -p hyperlight-wasm-csharp-bindgen {{ if features =="" {''} else {"--no-default-features -F kvm -F " + features } }} --profile={{ if target == "debug" {"dev"} else { target } }} - {{ wit-world }} cargo hyperlight build {{ if features =~ "wasmtime_latest" {"--no-default-features -F wasmtime_latest"} else {""} }} --profile={{ if target == "debug" {"dev"} else { target } }} --manifest-path src/hyperlight_wasm_runtime/Cargo.toml --target-dir target/component-sample-runtime --locked + +# Builds a guest runtime for the component_sample example world into build//component-sample-runtime +build-component-sample-runtime target=default-target features="": + cargo run -p hyperlight-wasm-aot -- experimental-build-runtime src/tests/rust_guests/component_sample/wit/example.wit build/{{ target }}/component-sample-runtime --world example --target-dir target/component-sample-runtime {{ if target == "debug" {"--debug"} else {""} }} {{ if features =~ "wasmtime_latest" {"--wasmtime-version latest"} else {""} }} # Runs the .NET tests. Needs the .NET 8 SDK or later. -dotnet-test target=default-target features="": (build-rust-component-examples target features) (dotnet-test-prebuilt target features) +dotnet-test target=default-target features="": (build-rust-component-examples target features) (build-component-sample-runtime target features) (dotnet-test-prebuilt target features) -# Runs the .NET tests on the component_sample component that build-ci-examples builds. +# Runs the .NET tests on the component and runtime that build-ci-examples builds. dotnet-test-prebuilt target=default-target features="": (dotnet-test-assets target features) (dotnet-run-tests target) +# Runs the .NET tests with the build creating the runtime and component through hyperlight-wasm-aot. +# Needs the component_sample component that build-rust-component-examples builds. +dotnet-test-build target=default-target features="": (dotnet-test-assets target features) + cargo build -p hyperlight-wasm-aot + just dotnet-run-tests {{ target }} -p:HyperlightWasmAot={{ justfile_directory() }}/target/debug/hyperlight-wasm-aot{{ if os() == "windows" {".exe"} else {""} }} + dotnet-tests := "dotnet/tests/Hyperlight.Wasm.Tests" # A hung test fails after 5 minutes and is named in the output. diff --git a/dotnet/README.md b/dotnet/README.md index 3d744e28..a8ba1797 100644 --- a/dotnet/README.md +++ b/dotnet/README.md @@ -6,7 +6,7 @@ Run WebAssembly components in Hyperlight micro VMs from .NET, with typed bindings generated from WIT. * `Hyperlight.Wasm` is the runtime library. It wraps the native `hyperlight-wasm-capi` library. -* `Hyperlight.Wasm.Build` generates C# from WIT at build time. It needs no Rust toolchain. Visual Studio builds need Visual Studio 2026 or later. +* `Hyperlight.Wasm.Build` generates C# from WIT at build time, which needs no Rust toolchain. It can also build the guest runtime and components. Visual Studio builds need Visual Studio 2026 or later. Both target .NET 8 or later. The packages hold native libraries for the runtime identifier they were packed on. @@ -42,15 +42,29 @@ uint sum = component.Adder.Add(1, 2); ## Guest runtime and components -A component needs a guest runtime built for its WIT world, because the runtime's guest bindings are generated when it is compiled. Building a runtime needs Rust and a hyperlight-wasm checkout of the same version as the packages: +A component needs a guest runtime built for its WIT world, because the runtime's guest bindings are generated when it is compiled. Components must be precompiled. The runtime, the precompiled component and the packages must come from the same hyperlight-wasm version. + +Both steps use `hyperlight-wasm-aot`, which needs rustup and clang. Its runtime build is experimental, and uses Rust 1.94.1 unless `RUSTUP_TOOLCHAIN` is set. Install the packages' version without the `-preview` suffix: ```sh -wasm-tools component wit example.wit -w -o example-world.wasm -WIT_WORLD=example-world.wasm cargo hyperlight build --release \ - --manifest-path src/hyperlight_wasm_runtime/Cargo.toml --target-dir target/example-runtime +cargo install --locked hyperlight-wasm-aot --version ``` -Precompile the component with `hyperlight-wasm-aot compile --component`. The runtime, the AOT tool and the packages must come from the same hyperlight-wasm version. +### Building them with the project + +Set `Runtime` on a `HyperlightWit` item and list components as `HyperlightComponent` items. The build then creates the runtime and `.aot` in the output directory. The first runtime build takes several minutes. `HyperlightWasmAot` sets the tool's path. + +```xml + + +``` + +### Building them by hand + +```sh +hyperlight-wasm-aot experimental-build-runtime example.wit example-runtime --world example +hyperlight-wasm-aot compile --component example.wasm example.aot +``` ## Sandboxes @@ -78,8 +92,9 @@ Hyperlight runs a helper process, `hyperlight_surrogate_.exe`, for each sa ## Development ```sh -just dotnet-test # builds the native libraries, a test runtime and component, then runs the tests -just dotnet-pack # packs both packages into dotnet/artifacts +just dotnet-test # builds the native libraries, a test runtime and component, then runs the tests +just dotnet-test-build # runs the tests with the build creating the runtime and component +just dotnet-pack # packs both packages into dotnet/artifacts ``` `HyperlightNativeDir` overrides where the projects find the native libraries. diff --git a/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.props b/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.props index 8072c94d..0b07ff45 100644 --- a/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.props +++ b/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.props @@ -3,15 +3,22 @@ + diff --git a/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.targets b/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.targets index 72c66cc8..2ade8769 100644 --- a/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.targets +++ b/dotnet/src/Hyperlight.Wasm.Build/build/Hyperlight.Wasm.Build.targets @@ -34,4 +34,62 @@ + + hyperlight-wasm-aot + $(BaseIntermediateOutputPath)hyperlight-wasm-runtime/ + + + + + + <_HyperlightRuntime Include="@(HyperlightWit)" Condition="'%(HyperlightWit.Runtime)' != ''"> + --world "%(HyperlightWit.World)" + + + + + + + + + + + + + + <_HyperlightWasmAotTime Condition="Exists('$(HyperlightWasmAot)')">$([System.IO.File]::GetLastWriteTime('$(HyperlightWasmAot)').Ticks) + + + + + + + + + + + + + + + + + diff --git a/dotnet/tests/Hyperlight.Wasm.Tests/ComponentTests.cs b/dotnet/tests/Hyperlight.Wasm.Tests/ComponentTests.cs index 83bb8a4e..1366e737 100644 --- a/dotnet/tests/Hyperlight.Wasm.Tests/ComponentTests.cs +++ b/dotnet/tests/Hyperlight.Wasm.Tests/ComponentTests.cs @@ -10,7 +10,7 @@ namespace Hyperlight.Wasm.Tests; public sealed class ComponentTests { - private static readonly string Assets = Path.Combine(AppContext.BaseDirectory, "assets"); + private static readonly string Assets = AppContext.BaseDirectory; private static readonly string Runtime = Path.Combine(Assets, "component-sample-runtime"); private static readonly string Component = Path.Combine(Assets, "component_sample.aot"); @@ -334,7 +334,7 @@ public sealed class SignalTests [Fact] public async Task GcSuspendsSpinningThreadAfterSandboxLoads() { - var assets = Path.Combine(AppContext.BaseDirectory, "assets"); + var assets = AppContext.BaseDirectory; using var sandbox = new WasmSandbox( Path.Combine(assets, "component-sample-runtime"), new SandboxOptions { ScratchSize = 100 << 20 }); diff --git a/dotnet/tests/Hyperlight.Wasm.Tests/Hyperlight.Wasm.Tests.csproj b/dotnet/tests/Hyperlight.Wasm.Tests/Hyperlight.Wasm.Tests.csproj index 04467e18..0c77a842 100644 --- a/dotnet/tests/Hyperlight.Wasm.Tests/Hyperlight.Wasm.Tests.csproj +++ b/dotnet/tests/Hyperlight.Wasm.Tests/Hyperlight.Wasm.Tests.csproj @@ -27,18 +27,23 @@ + + + <_BuildSampleAssets>true + <_SampleRuntime>component-sample-runtime + - + - - - aarch64 - x86_64 - - - - + + + + + + + diff --git a/src/hyperlight_wasm_aot/Cargo.toml b/src/hyperlight_wasm_aot/Cargo.toml index 000b2749..a1a93887 100644 --- a/src/hyperlight_wasm_aot/Cargo.toml +++ b/src/hyperlight_wasm_aot/Cargo.toml @@ -21,6 +21,14 @@ clap = { version = "4.6", features = ["derive"] } cargo_metadata = "0.23" cargo-util-schemas = "=0.14.1" object = { version = "0.39.1", default-features = false, features = ["read_core", "elf"] } +cargo-hyperlight = "0.1.14" +tar = "0.4.46" +# Makes Cargo fetch the runtime source that `experimental-build-runtime` builds. +hyperlight-wasm-runtime.workspace = true + +[build-dependencies] +cargo_metadata = "0.23" +tar = "0.4.46" [features] gdb = ["wasmtime/debug-builtins", "wasmtime_lts/debug-builtins"] diff --git a/src/hyperlight_wasm_aot/build.rs b/src/hyperlight_wasm_aot/build.rs new file mode 100644 index 00000000..2409858f --- /dev/null +++ b/src/hyperlight_wasm_aot/build.rs @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2026 The Hyperlight Authors. + +// Records where `experimental-build-runtime` finds the hyperlight-wasm-runtime +// source, in OUT_DIR/runtime_source.rs. + +use std::path::PathBuf; +use std::{env, fs}; + +use cargo_metadata::MetadataCommand; + +fn main() { + println!("cargo::rerun-if-changed=build.rs"); + + let metadata = MetadataCommand::new() + .exec() + .expect("could not run cargo metadata"); + let runtime = metadata + .packages + .iter() + .find(|p| *p.name == "hyperlight-wasm-runtime") + .expect("hyperlight-wasm-runtime not found in cargo metadata"); + let manifest = runtime.manifest_path.as_std_path(); + let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + + let registry = runtime + .source + .as_ref() + .is_some_and(|s| s.repr.starts_with("registry+") || s.repr.starts_with("sparse+")); + let source = if !registry { + // A checkout, including a git one, builds the runtime in place, against + // its workspace's other crates. A packaged copy would use their released + // versions. + format!("RuntimeSource::Path({manifest:?})") + } else { + // Cargo can garbage collect registry sources, so embed them. + let prefix = format!("hyperlight-wasm-runtime-{}", runtime.version); + let archive = out_dir.join("runtime-source.tar"); + let mut builder = tar::Builder::new(fs::File::create(&archive).unwrap()); + builder + .append_dir_all(&prefix, manifest.parent().unwrap()) + .unwrap(); + builder.finish().unwrap(); + format!("RuntimeSource::Archive({prefix:?}, include_bytes!({archive:?}))") + }; + fs::write( + out_dir.join("runtime_source.rs"), + format!("const RUNTIME_SOURCE: RuntimeSource = {source};\n"), + ) + .unwrap(); +} diff --git a/src/hyperlight_wasm_aot/src/main.rs b/src/hyperlight_wasm_aot/src/main.rs index 0b39d404..bbb77f30 100644 --- a/src/hyperlight_wasm_aot/src/main.rs +++ b/src/hyperlight_wasm_aot/src/main.rs @@ -15,7 +15,8 @@ limitations under the License. */ use std::fmt::Display; -use std::path::Path; +use std::fs; +use std::path::{Path, PathBuf}; use cargo_metadata::{MetadataCommand, Package}; use cargo_util_schemas::manifest::PackageName; @@ -100,6 +101,180 @@ enum Commands { #[arg(long, value_enum, default_value = "lts")] wasmtime_version: WasmtimeVersion, }, + + /// Experimental: build a guest runtime for a WIT world + /// + /// Needs rustup and clang. Builds with Rust 1.94.1 unless RUSTUP_TOOLCHAIN is + /// set. Use a runtime with hosts and precompiled components from this version + /// of hyperlight-wasm. + ExperimentalBuildRuntime { + /// The `.wit` file, directory of `.wit` files or wasm-encoded WIT package that defines the world + wit: PathBuf, + + /// The output file path + output: PathBuf, + + /// The world to build for. Defaults to the last world in the package. + #[arg(long)] + world: Option, + + /// Build with debug info and optimizations disabled + #[arg(long)] + debug: bool, + + /// Wasmtime version the runtime uses. Precompile components with the same version. + #[arg(long, value_enum, default_value = "lts")] + wasmtime_version: WasmtimeVersion, + + /// Directory for build artifacts, reused by later builds + #[arg(long, default_value = "target/hyperlight-wasm-runtime")] + target_dir: PathBuf, + }, +} + +// Only one variant is built, chosen by build.rs. +#[allow(dead_code)] +enum RuntimeSource { + /// The runtime's manifest in a checkout. + Path(&'static str), + /// The top-level directory name and a tar of the runtime crate. + Archive(&'static str, &'static [u8]), +} + +include!(concat!(env!("OUT_DIR"), "/runtime_source.rs")); + +type BoxError = Box; + +/// The toolchain that builds the runtime when `RUSTUP_TOOLCHAIN` is unset. +/// Keep it in sync with rust-toolchain.toml. +const RUNTIME_TOOLCHAIN: &str = "1.94.1"; + +/// Returns the runtime's manifest, extracting an embedded source into `target_dir`. +fn runtime_manifest(target_dir: &Path) -> Result { + match RUNTIME_SOURCE { + RuntimeSource::Path(manifest) => Ok(PathBuf::from(manifest)), + RuntimeSource::Archive(prefix, archive) => { + const MARKER: &str = ".hyperlight-wasm-aot-extracted"; + let parent = target_dir.join("source"); + let source = parent.join(prefix); + // The marker shows that an earlier extraction finished. + let marker = source.join(MARKER); + if !marker.exists() { + // Concurrent builds each extract privately, then rename the + // result into place, so none sees a partial source. + let staging = parent.join(format!(".staging-{}", std::process::id())); + if staging.exists() { + fs::remove_dir_all(&staging)?; + } + tar::Archive::new(archive).unpack(&staging)?; + let staged = staging.join(prefix); + // Keep Cargo from treating an enclosing project as the workspace. + let manifest = staged.join("Cargo.toml"); + let mut contents = fs::read_to_string(&manifest)?; + contents.push_str("\n[workspace]\n"); + fs::write(&manifest, contents)?; + fs::write(staged.join(MARKER), "")?; + if source.exists() && !marker.exists() { + fs::remove_dir_all(&source)?; + } + let renamed = fs::rename(&staged, &source); + fs::remove_dir_all(&staging)?; + // A concurrent build may have renamed its copy first. + match renamed { + Err(e) if !marker.exists() => return Err(e.into()), + _ => {} + } + } + Ok(source.join("Cargo.toml")) + } + } +} + +fn build_runtime( + wit: &Path, + output: &Path, + world: Option<&str>, + debug: bool, + wasmtime_version: WasmtimeVersion, + target_dir: &Path, +) -> Result<(), BoxError> { + // The runtime resolves a relative WIT_WORLD against its own manifest. + let wit = wit + .canonicalize() + .map_err(|e| format!("could not read {}: {e}", wit.display()))?; + let manifest = runtime_manifest(target_dir)?; + let target = format!("{}-hyperlight-none", std::env::consts::ARCH); + let (profile, profile_dir) = if debug { + ("dev", "debug") + } else { + ("release", "release") + }; + + // Stable Rust 1.95 and later reject the guest's custom target. cargo-hyperlight + // also adds rust-src only when RUSTUP_TOOLCHAIN is set in this process. + if std::env::var_os("RUSTUP_TOOLCHAIN").is_none() { + // SAFETY: the process has not started other threads. + unsafe { std::env::set_var("RUSTUP_TOOLCHAIN", RUNTIME_TOOLCHAIN) }; + } + + let mut cmd = cargo_hyperlight::cargo()?; + cmd.env_clear_cargo() + .arg("build") + .arg("--profile") + .arg(profile) + .arg("--target") + .arg(&target) + .arg("--target-dir") + .arg(target_dir) + .arg("--manifest-path") + .arg(&manifest) + .arg("--locked") + .env("WIT_WORLD", &wit); + match world { + Some(world) => cmd.env("WIT_WORLD_NAME", world), + None => cmd.env_remove("WIT_WORLD_NAME"), + }; + if wasmtime_version == WasmtimeVersion::Latest { + cmd.args(["--no-default-features", "--features", "wasmtime_latest"]); + } + cmd.status()?; + + let built = target_dir + .join(&target) + .join(profile_dir) + .join("hyperlight-wasm-runtime"); + let copy_error = |e: std::io::Error| { + format!( + "could not copy {} to {}: {e}", + built.display(), + output.display() + ) + }; + let built_time = fs::metadata(&built) + .and_then(|m| m.modified()) + .map_err(copy_error)?; + // The output takes the build's time, so an unchanged build keeps its + // timestamp and later build steps can skip it. + let current = fs::metadata(output) + .and_then(|m| m.modified()) + .is_ok_and(|t| t == built_time); + if !current { + if let Some(parent) = output.parent().filter(|p| !p.as_os_str().is_empty()) { + fs::create_dir_all(parent)?; + } + fs::copy(&built, output).map_err(copy_error)?; + fs::File::options() + .write(true) + .open(output) + .and_then(|f| f.set_modified(built_time)) + .map_err(copy_error)?; + } + println!( + "Built guest runtime for {}: {}", + wit.display(), + output.display() + ); + Ok(()) } /// Precompile bytes using the LTS wasmtime version @@ -356,6 +531,26 @@ fn main() { } } } + Commands::ExperimentalBuildRuntime { + wit, + output, + world, + debug, + wasmtime_version, + target_dir, + } => { + if let Err(e) = build_runtime( + &wit, + &output, + world.as_deref(), + debug, + wasmtime_version, + &target_dir, + ) { + eprintln!("Error - {e}"); + std::process::exit(1); + } + } } } diff --git a/src/hyperlight_wasm_macro/src/lib.rs b/src/hyperlight_wasm_macro/src/lib.rs index 3b4c0a37..66dede19 100644 --- a/src/hyperlight_wasm_macro/src/lib.rs +++ b/src/hyperlight_wasm_macro/src/lib.rs @@ -27,14 +27,26 @@ mod wasmguest; /// wasmtime linker for component imports (which are implemented by /// calling to the Hyperlight host). /// +/// `WIT_WORLD` is a `.wit` file, a directory of `.wit` files or a +/// wasm-encoded WIT package. +/// /// If the WIT file contains multiple worlds, set the `WIT_WORLD_NAME` /// environment variable to select a specific world by name. If not set, /// the last world in the file will be used. #[proc_macro] pub fn wasm_guest_bindgen(_: proc_macro::TokenStream) -> proc_macro::TokenStream { - let path = std::env::var_os("WIT_WORLD").unwrap(); + let path = std::path::PathBuf::from(std::env::var_os("WIT_WORLD").unwrap()); let world_name = std::env::var("WIT_WORLD_NAME").ok(); - util::read_wit_type_from_file(path, world_name, |kebab_name, ct| { + let source = if path.extension().is_some_and(|e| e == "wit") + || std::path::Path::new(&std::env::var_os("CARGO_MANIFEST_DIR").unwrap()) + .join(&path) + .is_dir() + { + util::WitSource::Wit(path) + } else { + util::WitSource::Wasm(path) + }; + util::read_wit_type(source, world_name, |kebab_name, ct| { let decls = emit::run_state(true, true, |s| { // Emit type/trait definitions for all instances in the world rtypes::emit_toplevel(s, &kebab_name, ct); diff --git a/src/hyperlight_wasm_runtime/build.rs b/src/hyperlight_wasm_runtime/build.rs index 2ac2086f..3d209924 100644 --- a/src/hyperlight_wasm_runtime/build.rs +++ b/src/hyperlight_wasm_runtime/build.rs @@ -126,11 +126,11 @@ fn main() { } /// Identifies the `WIT_WORLD` world, so hosts can check that a snapshot -/// matches their bindings. Hashes the file and `WIT_WORLD_NAME`. +/// matches their bindings. Hashes the file, or the names and contents of +/// every file under a directory, and `WIT_WORLD_NAME`. fn component_interface(path: &Path) -> String { - let bytes = fs::read(path).expect("could not read WIT_WORLD"); let mut hash = blake3::Hasher::new(); - hash.update(blake3::hash(&bytes).as_bytes()); + hash_wit(&mut hash, path); // One file can hold several worlds. Unset selects the last one. if let Ok(world) = env::var("WIT_WORLD_NAME") { hash.update(b"\x01"); @@ -138,3 +138,22 @@ fn component_interface(path: &Path) -> String { } hash.finalize().to_string() } + +fn hash_wit(hash: &mut blake3::Hasher, path: &Path) { + if path.is_dir() { + let mut entries = fs::read_dir(path) + .and_then(|d| { + d.map(|e| e.map(|e| e.path())) + .collect::, _>>() + }) + .expect("could not read WIT_WORLD"); + entries.sort(); + for entry in entries { + hash.update(entry.file_name().unwrap().as_encoded_bytes()); + hash_wit(hash, &entry); + } + } else { + let bytes = fs::read(path).expect("could not read WIT_WORLD"); + hash.update(blake3::hash(&bytes).as_bytes()); + } +}