diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 212f3f35..3e905314 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,8 +42,6 @@ jobs: - name: test run: make ci shell: bash - - name: adapter - run: cd wasm_abi/adapter && cargo build --release -p viceroy-component-adapter --target wasm32-unknown-unknown # Run the trap test in an isolated job. It needs different cargo features than the usual build, so # it entails rebuilding the whole workspace if we combine them in a single job. This way, we diff --git a/.gitignore b/.gitignore index ce194acb..a889e63f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ publish vendor/ verify-publishable/ .vscode +# Auto-generated adapter wasm files (built by build.rs) +wasm_abi/data/*.wasm diff --git a/Cargo.toml b/Cargo.toml index 7409bcdc..e2b9ace8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,12 +16,13 @@ categories = [ "wasm" ] include = [ + "build.rs", "CHANGELOG.md", "SECURITY.md", "src/**/*", + "wasm_abi/adapter/**/*", "wasm_abi/wit/**/*", "wasm_abi/compute-at-edge-abi/**/*.witx", - "wasm_abi/data/*.wasm", ] rust-version = "1.90" diff --git a/Makefile b/Makefile index 9e01e3af..fccf15b9 100644 --- a/Makefile +++ b/Makefile @@ -62,58 +62,3 @@ generate-lockfile: $(VICEROY_CARGO) generate-lockfile $(VICEROY_CARGO) generate-lockfile --manifest-path=test-fixtures/Cargo.toml $(VICEROY_CARGO) generate-lockfile --manifest-path=cli/tests/trap-test/Cargo.toml - -# Regenerate the adapter, and move it into `wasm_abi/data` -.PHONY: build-adapter -build-adapter: - # Build the component adapter for adapting the host-call abi to the - # component model. This version uses `--no-default-features` to disable - # the default "exports" feature, to build the imports-only "library" - # version of the adapter. - ( \ - cd wasm_abi/adapter && \ - cargo build \ - --package viceroy-component-adapter \ - --target wasm32-unknown-unknown \ - --no-default-features \ - --profile release-library \ - ) - # Build the non-shift "library" version of the adapter. - ( \ - cd wasm_abi/adapter && \ - cargo build \ - --package viceroy-component-adapter \ - --target wasm32-unknown-unknown \ - --no-default-features \ - --profile release-library-noshift \ - --features noshift \ - ) - - # Build the component adapter for adapting the host-call abi to the - # component model. This is the normal version that includes the exports. - ( \ - cd wasm_abi/adapter && \ - cargo build \ - --package viceroy-component-adapter \ - --target wasm32-unknown-unknown \ - --release \ - ) - - # Build the non-shift normal version of the adapter. - ( \ - cd wasm_abi/adapter && \ - cargo build \ - --package viceroy-component-adapter \ - --target wasm32-unknown-unknown \ - --profile release-noshift \ - --features noshift \ - ) - - cp wasm_abi/adapter/target/wasm32-unknown-unknown/release/viceroy_component_adapter.wasm \ - wasm_abi/data/viceroy-component-adapter.wasm - cp wasm_abi/adapter/target/wasm32-unknown-unknown/release-noshift/viceroy_component_adapter.wasm \ - wasm_abi/data/viceroy-component-adapter.noshift.wasm - cp wasm_abi/adapter/target/wasm32-unknown-unknown/release-library/viceroy_component_adapter.wasm \ - wasm_abi/data/viceroy-component-adapter.library.wasm - cp wasm_abi/adapter/target/wasm32-unknown-unknown/release-library-noshift/viceroy_component_adapter.wasm \ - wasm_abi/data/viceroy-component-adapter.library.noshift.wasm \ No newline at end of file diff --git a/build.rs b/build.rs new file mode 100644 index 00000000..8e1d9705 --- /dev/null +++ b/build.rs @@ -0,0 +1,115 @@ +use std::env; +use std::path::{Path, PathBuf}; +use std::process::Command; + +fn main() { + // Only rebuild if the adapter source changes + println!("cargo:rerun-if-changed=wasm_abi/adapter"); + + let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let adapter_dir = manifest_dir.join("wasm_abi/adapter"); + let data_dir = manifest_dir.join("wasm_abi/data"); + + // Ensure the data directory exists + std::fs::create_dir_all(&data_dir).expect("Failed to create wasm_abi/data directory"); + + // Get the cargo command to use (allows for override like in Makefile) + let cargo = env::var("CARGO").unwrap_or_else(|_| "cargo".to_string()); + + // Build the component adapter for adapting the host-call abi to the + // component model. This version uses `--no-default-features` to disable + // the default "exports" feature, to build the imports-only "library" + // version of the adapter. + build_adapter( + &cargo, + &adapter_dir, + &data_dir, + "release-library", + &["--no-default-features"], + "viceroy-component-adapter.library.wasm", + ); + + // Build the non-shift "library" version of the adapter. + build_adapter( + &cargo, + &adapter_dir, + &data_dir, + "release-library-noshift", + &["--no-default-features", "--features", "noshift"], + "viceroy-component-adapter.library.noshift.wasm", + ); + + // Build the component adapter for adapting the host-call abi to the + // component model. This is the normal version that includes the exports. + build_adapter( + &cargo, + &adapter_dir, + &data_dir, + "release", + &[], + "viceroy-component-adapter.wasm", + ); + + // Build the non-shift normal version of the adapter. + build_adapter( + &cargo, + &adapter_dir, + &data_dir, + "release-noshift", + &["--features", "noshift"], + "viceroy-component-adapter.noshift.wasm", + ); +} + +fn build_adapter( + cargo: &str, + adapter_dir: &Path, + data_dir: &Path, + profile: &str, + extra_args: &[&str], + output_name: &str, +) { + eprintln!("Building adapter variant: {}", output_name); + + let mut cmd = Command::new(cargo); + cmd.current_dir(adapter_dir) + .arg("build") + .arg("--package") + .arg("viceroy-component-adapter") + .arg("--target") + .arg("wasm32-unknown-unknown") + .arg("--profile") + .arg(profile); + + // Add any extra arguments (like --no-default-features or --features) + for arg in extra_args { + cmd.arg(arg); + } + + let status = cmd + .status() + .unwrap_or_else(|e| panic!("Failed to execute cargo build for {}: {}", output_name, e)); + + if !status.success() { + panic!("Failed to build adapter variant: {}", output_name); + } + + // Copy the built wasm file to the data directory + let source = adapter_dir + .join("target/wasm32-unknown-unknown") + .join(profile) + .join("viceroy_component_adapter.wasm"); + + let dest = data_dir.join(output_name); + + std::fs::copy(&source, &dest).unwrap_or_else(|e| { + panic!( + "Failed to copy {} to {}: {}", + source.display(), + dest.display(), + e + ) + }); + + eprintln!("Successfully built and copied {}", output_name); +} diff --git a/wasm_abi/data/viceroy-component-adapter.library.noshift.wasm b/wasm_abi/data/viceroy-component-adapter.library.noshift.wasm deleted file mode 100755 index bd8df663..00000000 Binary files a/wasm_abi/data/viceroy-component-adapter.library.noshift.wasm and /dev/null differ diff --git a/wasm_abi/data/viceroy-component-adapter.library.wasm b/wasm_abi/data/viceroy-component-adapter.library.wasm deleted file mode 100755 index 25048d7f..00000000 Binary files a/wasm_abi/data/viceroy-component-adapter.library.wasm and /dev/null differ diff --git a/wasm_abi/data/viceroy-component-adapter.noshift.wasm b/wasm_abi/data/viceroy-component-adapter.noshift.wasm deleted file mode 100755 index 75f26c1b..00000000 Binary files a/wasm_abi/data/viceroy-component-adapter.noshift.wasm and /dev/null differ diff --git a/wasm_abi/data/viceroy-component-adapter.wasm b/wasm_abi/data/viceroy-component-adapter.wasm deleted file mode 100755 index 77e20296..00000000 Binary files a/wasm_abi/data/viceroy-component-adapter.wasm and /dev/null differ