From 12086ef98027a97ee5543c4a3a06dd8de4406fb6 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 12:41:23 +0300 Subject: [PATCH 01/25] build(ci): add riscv64 cross-compile support Signed-off-by: Rodney Osodo --- README.md | 32 ++++++++++++++++---- ci/local.sh | 86 +++++++++++++++++++++++++++++++++++++++++------------ ffi.go | 11 +++---- 3 files changed, 99 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 3e28801a..765afa99 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,9 @@ This Go library uses CGO to consume the C API of the [Wasmtime project][wasmtime] which is written in Rust. Precompiled binaries of Wasmtime are checked into this repository on tagged releases so you won't have to install Wasmtime locally, but it means that this project only works on Linux x86\_64, -macOS x86\_64 , and Windows x86\_64 currently. Building on other platforms will -need to arrange to build Wasmtime and use `CGO_*` env vars to compile correctly. +Linux aarch64, macOS x86\_64, macOS aarch64, and Windows x86\_64 currently. +Building on other platforms will need to arrange to build Wasmtime and use +`CGO_*` env vars to compile correctly. This project has been tested with Go 1.13 or later. @@ -130,11 +131,11 @@ $ git clone https://github.com/bytecodealliance/wasmtime-go Next up you'll want to have a [local Wasmtime build available](https://bytecodealliance.github.io/wasmtime/contributing-building.html). -You'll need to build at least the `wasmtime-c-api` crate, which, at the time of -this writing, would be: +You'll need to build the `wasmtime-c-api` artifact crate to produce the static +library (`libwasmtime.a`) and shared library (`libwasmtime.so`): ```sh -$ cargo build -p wasmtime-c-api +$ cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml ``` Once you've got that you can set up the environment of this library with: @@ -152,6 +153,25 @@ $ go test And after that you should be good to go! +### Cross-compiling for Linux riscv64 + +Pass `riscv64` as the second argument to `ci/local.sh`. This requires +[zig](https://ziglang.org/) as the C cross-compiler: + +```sh +$ ./ci/local.sh /path/to/wasmtime riscv64 +``` + +This will automatically install the Rust target, cross-compile the Wasmtime C +API artifact, and set up the `build/linux-riscv64` directory. Then cross-compile +your Go program: + +```sh +$ GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 \ + CC="zig cc -target riscv64-linux-gnu" \ + go build -ldflags "-s -w" ./main.go +``` + ### Release Checklist First run: @@ -166,7 +186,7 @@ Make sure everything passes at the current version. Next run: ``` -$ git ls-files | xargs sed -i 's/v16/v17/g' +$ git ls-files | xargs sed -i 's/v44/v45/g' $ python3 ci/download-wasmtime.py $ go test ``` diff --git a/ci/local.sh b/ci/local.sh index d211ebe1..1fcaf1a3 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -2,34 +2,82 @@ wasmtime=$1 if [ "$wasmtime" = "" ]; then - echo "must pass path to wasmtime" + echo "usage: $0 [target]" + echo " target: native (default), riscv64" exit 1 fi +target=${2:-native} + # Clean and re-create "build" directory hierarchy rm -rf build -for d in "include" "include/wasmtime" "linux-x86_64" "macos-x86_64" "windows-x86_64" "linux-aarch64" "macos-aarch64"; do +for d in "include" "include/wasmtime" "include/wasmtime/component" "include/wasmtime/component/types" "include/wasmtime/types" "linux-x86_64" "macos-x86_64" "windows-x86_64" "linux-aarch64" "macos-aarch64"; do path="build/$d" mkdir -p "$path" - name=$(basename $d) - echo "package ${name/-/_}" > "$path/empty.go" + name=$(basename $d | tr - _) + echo "package $name" > "$path/empty.go" done -build="$wasmtime/target/release" -if [ ! -d "$build" ]; then - build="$wasmtime/target/debug" -fi -# Use absolute path for symbolic links -build=$(cd "$build" && pwd) +if [ "$target" = "riscv64" ]; then + rust_target="riscv64gc-unknown-linux-gnu" + subdir="$rust_target" -if [ ! -f "$build/libwasmtime.a" ]; then - echo 'Missing libwasmtime.a. Did you `cargo build -p wasmtime-c-api`?' -fi + if ! rustup target list --installed | grep -q "$rust_target"; then + echo "Installing Rust target $rust_target ..." + rustup target add "$rust_target" + fi -for d in "linux-x86_64" "macos-x86_64" "linux-aarch64" "macos-aarch64"; do - ln -s "$build/libwasmtime.a" "build/$d/libwasmtime.a" -done + mkdir -p "build/linux-riscv64" + echo "package linux_riscv64" > "build/linux-riscv64/empty.go" + + build="$wasmtime/target/$subdir/release" + if [ ! -d "$build" ]; then + build="$wasmtime/target/$subdir/debug" + fi + + if [ ! -f "$build/libwasmtime.a" ]; then + echo "Building wasmtime-c-api for $rust_target ..." + CC_riscv64gc_unknown_linux_gnu="zig cc -target riscv64-linux-gnu" \ + cargo build --release --target "$rust_target" \ + -p wasmtime-c-api --manifest-path "$wasmtime/crates/c-api/artifact/Cargo.toml" + build="$wasmtime/target/$subdir/release" + fi + + build=$(cd "$build" && pwd) + ln -s "$build/libwasmtime.a" "build/linux-riscv64/libwasmtime.a" -cp "$wasmtime"/crates/c-api/include/*.h build/include -cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include -cp "$wasmtime"/crates/c-api/wasm-c-api/include/*.h build/include + # Copy headers + cp "$wasmtime"/crates/c-api/include/*.h build/include + cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include + + conf=$(find "$build"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) + if [ -n "$conf" ]; then + cp "$conf" build/include/wasmtime/conf.h + fi + + echo "riscv64 build ready. Cross-compile with:" + echo " GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 CC=\"zig cc -target riscv64-linux-gnu\" go build -ldflags \"-s -w\" ./main.go" +else + build="$wasmtime/target/release" + if [ ! -d "$build" ]; then + build="$wasmtime/target/debug" + fi + build=$(cd "$build" && pwd) + + if [ ! -f "$build/libwasmtime.a" ]; then + echo 'Missing libwasmtime.a. Build with:' + echo ' cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml' + fi + + for d in "linux-x86_64" "macos-x86_64" "linux-aarch64" "macos-aarch64"; do + ln -s "$build/libwasmtime.a" "build/$d/libwasmtime.a" + done + + cp "$wasmtime"/crates/c-api/include/*.h build/include + cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include + + conf=$(find "$build"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) + if [ -n "$conf" ]; then + cp "$conf" build/include/wasmtime/conf.h + fi +fi diff --git a/ffi.go b/ffi.go index 7d47d7b5..6817d082 100644 --- a/ffi.go +++ b/ffi.go @@ -1,13 +1,14 @@ package wasmtime // #cgo CFLAGS:-I${SRCDIR}/build/include -// #cgo !windows LDFLAGS:-lwasmtime -lm -ldl -pthread +// #cgo !windows LDFLAGS:-lm -ldl -pthread // #cgo windows CFLAGS:-DWASM_API_EXTERN= -DWASI_API_EXTERN= // #cgo windows LDFLAGS:-lwasmtime -luserenv -lole32 -lntdll -lws2_32 -lkernel32 -lbcrypt -// #cgo linux,amd64 LDFLAGS:-L${SRCDIR}/build/linux-x86_64 -// #cgo linux,arm64 LDFLAGS:-L${SRCDIR}/build/linux-aarch64 -// #cgo darwin,amd64 LDFLAGS:-L${SRCDIR}/build/macos-x86_64 -// #cgo darwin,arm64 LDFLAGS:-L${SRCDIR}/build/macos-aarch64 +// #cgo linux,amd64 LDFLAGS:-L${SRCDIR}/build/linux-x86_64 -lwasmtime +// #cgo linux,arm64 LDFLAGS:-L${SRCDIR}/build/linux-aarch64 -lwasmtime +// #cgo linux,riscv64 LDFLAGS:-L${SRCDIR}/build/linux-riscv64 ${SRCDIR}/build/linux-riscv64/libwasmtime.a -lgcc -lgcc_s +// #cgo darwin,amd64 LDFLAGS:-L${SRCDIR}/build/macos-x86_64 -lwasmtime +// #cgo darwin,arm64 LDFLAGS:-L${SRCDIR}/build/macos-aarch64 -lwasmtime // #cgo windows,amd64 LDFLAGS:-L${SRCDIR}/build/windows-x86_64 // #include import "C" From 50af7a2a746c0088a819e8d6d96dbf62a08562f9 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 12:41:42 +0300 Subject: [PATCH 02/25] feat(example): add Go Wasmtime example Signed-off-by: Rodney Osodo --- example/go.mod | 7 +++++++ example/go.sum | 8 ++++++++ example/main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 example/go.mod create mode 100644 example/go.sum create mode 100644 example/main.go diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 00000000..1debf675 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,7 @@ +module rodneyosodo.com/propeller-example + +go 1.26.2 + +require github.com/bytecodealliance/wasmtime-go/v44 v44.0.0 + +replace github.com/bytecodealliance/wasmtime-go/v44 => ../ diff --git a/example/go.sum b/example/go.sum new file mode 100644 index 00000000..e9e74282 --- /dev/null +++ b/example/go.sum @@ -0,0 +1,8 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/example/main.go b/example/main.go new file mode 100644 index 00000000..747ee0a3 --- /dev/null +++ b/example/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + + "github.com/bytecodealliance/wasmtime-go/v44" +) + +func main() { + store := wasmtime.NewStore(wasmtime.NewEngine()) + + wasm, err := wasmtime.Wat2Wasm(` + (module + (import "" "hello" (func $hello)) + (func (export "run") + (call $hello)) + ) + `) + check(err) + + module, err := wasmtime.NewModule(store.Engine, wasm) + check(err) + + item := wasmtime.WrapFunc(store, func() { + fmt.Println("Hello from Go!") + }) + + instance, err := wasmtime.NewInstance(store, module, []wasmtime.AsExtern{item}) + check(err) + + run := instance.GetFunc(store, "run") + if run == nil { + panic("not a function") + } + _, err = run.Call(store) + check(err) +} + +func check(e error) { + if e != nil { + panic(e) + } +} From f62c81d21d967257dd50ea0ffece3dd6bc5bd65a Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 12:41:59 +0300 Subject: [PATCH 03/25] ci(workflows): add example-build GitHub Actions workflow Signed-off-by: Rodney Osodo --- .github/workflows/example-build.yml | 123 ++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 .github/workflows/example-build.yml diff --git a/.github/workflows/example-build.yml b/.github/workflows/example-build.yml new file mode 100644 index 00000000..33e2bb02 --- /dev/null +++ b/.github/workflows/example-build.yml @@ -0,0 +1,123 @@ +name: Example Build + +on: + pull_request: + branches: [main] + push: + branches: [main] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + build-example-linux: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + goarch: [amd64, arm64, riscv64] + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.x" + + - run: python ci/download-wasmtime.py + + - uses: actions/setup-go@v5 + with: + go-version: "1.24.x" + + - name: Install cross-compiler (arm64) + if: matrix.goarch == 'arm64' + run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu + + - name: Install zig (riscv64) + if: matrix.goarch == 'riscv64' + uses: mlugg/setup-zig@v2 + with: + version: 0.14.0 + + - name: Build example (amd64) + if: matrix.goarch == 'amd64' + working-directory: example + run: CGO_ENABLED=1 go build -ldflags "-s -w" ./main.go + + - name: Build example (arm64) + if: matrix.goarch == 'arm64' + working-directory: example + env: + GOOS: linux + GOARCH: arm64 + CGO_ENABLED: 1 + CC: aarch64-linux-gnu-gcc + run: go build -ldflags "-s -w" ./main.go + + - name: Build example (riscv64) + if: matrix.goarch == 'riscv64' + working-directory: example + env: + GOOS: linux + GOARCH: riscv64 + CGO_ENABLED: 1 + CC: zig cc -target riscv64-linux-gnu + run: go build -ldflags "-s -w" ./main.go + + - name: Verify binary + working-directory: example + run: file main + + build-example-macos: + runs-on: macos-latest + strategy: + fail-fast: false + matrix: + goarch: [amd64, arm64] + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.x" + - run: python ci/download-wasmtime.py + + - uses: actions/setup-go@v5 + with: + go-version: "1.24.x" + + - name: Build example + working-directory: example + env: + GOARCH: ${{ matrix.goarch }} + CGO_ENABLED: 1 + run: go build -ldflags "-s -w" ./main.go + + - name: Verify binary + working-directory: example + run: file main + + build-example-windows: + runs-on: windows-2025 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.x" + - run: python ci/download-wasmtime.py + + - uses: actions/setup-go@v5 + with: + go-version: "1.24.x" + + - name: Build example + working-directory: example + env: + CGO_ENABLED: 1 + run: go build -ldflags "-s -w" ./main.go + + - name: Verify binary + working-directory: example + run: file main.exe From e956dc291dda6b0459a3807d7b809f2e40f511de Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 13:51:37 +0300 Subject: [PATCH 04/25] build(cgo): Move -lm from global LDFLAGS to linux targets Signed-off-by: Rodney Osodo --- ffi.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ffi.go b/ffi.go index 6817d082..b54e20a6 100644 --- a/ffi.go +++ b/ffi.go @@ -1,12 +1,12 @@ package wasmtime // #cgo CFLAGS:-I${SRCDIR}/build/include -// #cgo !windows LDFLAGS:-lm -ldl -pthread +// #cgo !windows LDFLAGS:-ldl -pthread // #cgo windows CFLAGS:-DWASM_API_EXTERN= -DWASI_API_EXTERN= // #cgo windows LDFLAGS:-lwasmtime -luserenv -lole32 -lntdll -lws2_32 -lkernel32 -lbcrypt -// #cgo linux,amd64 LDFLAGS:-L${SRCDIR}/build/linux-x86_64 -lwasmtime -// #cgo linux,arm64 LDFLAGS:-L${SRCDIR}/build/linux-aarch64 -lwasmtime -// #cgo linux,riscv64 LDFLAGS:-L${SRCDIR}/build/linux-riscv64 ${SRCDIR}/build/linux-riscv64/libwasmtime.a -lgcc -lgcc_s +// #cgo linux,amd64 LDFLAGS:-L${SRCDIR}/build/linux-x86_64 -lwasmtime -lm +// #cgo linux,arm64 LDFLAGS:-L${SRCDIR}/build/linux-aarch64 -lwasmtime -lm +// #cgo linux,riscv64 LDFLAGS:-L${SRCDIR}/build/linux-riscv64 ${SRCDIR}/build/linux-riscv64/libwasmtime.a -lm -lgcc -lgcc_s // #cgo darwin,amd64 LDFLAGS:-L${SRCDIR}/build/macos-x86_64 -lwasmtime // #cgo darwin,arm64 LDFLAGS:-L${SRCDIR}/build/macos-aarch64 -lwasmtime // #cgo windows,amd64 LDFLAGS:-L${SRCDIR}/build/windows-x86_64 From d378ef01875f72131034e1fa3e45f3404df9f1e5 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 13:59:32 +0300 Subject: [PATCH 05/25] ci(workflow): add Rust and wasmtime build for riscv64 Signed-off-by: Rodney Osodo --- .github/workflows/example-build.yml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/example-build.yml b/.github/workflows/example-build.yml index 33e2bb02..5e44068d 100644 --- a/.github/workflows/example-build.yml +++ b/.github/workflows/example-build.yml @@ -34,12 +34,25 @@ jobs: if: matrix.goarch == 'arm64' run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu - - name: Install zig (riscv64) + - name: Install zig and Rust (riscv64) if: matrix.goarch == 'riscv64' uses: mlugg/setup-zig@v2 with: version: 0.14.0 + - name: Install Rust toolchain (riscv64) + if: matrix.goarch == 'riscv64' + uses: dtolnay/rust-toolchain@stable + + - name: Build wasmtime and set up riscv64 + if: matrix.goarch == 'riscv64' + run: | + git clone --depth 1 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime + cd /tmp/wasmtime && git submodule update --init + cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml + cd - + ./ci/local.sh /tmp/wasmtime riscv64 + - name: Build example (amd64) if: matrix.goarch == 'amd64' working-directory: example From 354351c1fe49b83898e344ea59e2d92f540b291d Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 14:04:48 +0300 Subject: [PATCH 06/25] ci(riscv): add riscv64 build and update local.sh Signed-off-by: Rodney Osodo --- .github/workflows/example-build.yml | 10 ++-- ci/local.sh | 77 ++++++++++++++--------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/.github/workflows/example-build.yml b/.github/workflows/example-build.yml index 5e44068d..48d22d2f 100644 --- a/.github/workflows/example-build.yml +++ b/.github/workflows/example-build.yml @@ -34,7 +34,7 @@ jobs: if: matrix.goarch == 'arm64' run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu - - name: Install zig and Rust (riscv64) + - name: Install zig (riscv64) if: matrix.goarch == 'riscv64' uses: mlugg/setup-zig@v2 with: @@ -44,13 +44,13 @@ jobs: if: matrix.goarch == 'riscv64' uses: dtolnay/rust-toolchain@stable - - name: Build wasmtime and set up riscv64 + - name: Build wasmtime for riscv64 if: matrix.goarch == 'riscv64' run: | + rustup target add riscv64gc-unknown-linux-gnu git clone --depth 1 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime - cd /tmp/wasmtime && git submodule update --init - cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml - cd - + git -C /tmp/wasmtime submodule update --init + cargo build --release -p wasmtime-c-api --manifest-path /tmp/wasmtime/crates/c-api/artifact/Cargo.toml ./ci/local.sh /tmp/wasmtime riscv64 - name: Build example (amd64) diff --git a/ci/local.sh b/ci/local.sh index 1fcaf1a3..4dbed547 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -9,30 +9,21 @@ fi target=${2:-native} -# Clean and re-create "build" directory hierarchy -rm -rf build -for d in "include" "include/wasmtime" "include/wasmtime/component" "include/wasmtime/component/types" "include/wasmtime/types" "linux-x86_64" "macos-x86_64" "windows-x86_64" "linux-aarch64" "macos-aarch64"; do - path="build/$d" - mkdir -p "$path" - name=$(basename $d | tr - _) - echo "package $name" > "$path/empty.go" -done - if [ "$target" = "riscv64" ]; then + # Add riscv64 to existing build directory (does not remove other platforms) + mkdir -p "build/linux-riscv64" "build/include" "build/include/wasmtime" + echo "package linux_riscv64" > "build/linux-riscv64/empty.go" + rust_target="riscv64gc-unknown-linux-gnu" - subdir="$rust_target" - if ! rustup target list --installed | grep -q "$rust_target"; then + if command -v rustup &>/dev/null && ! rustup target list --installed 2>/dev/null | grep -q "$rust_target"; then echo "Installing Rust target $rust_target ..." rustup target add "$rust_target" fi - mkdir -p "build/linux-riscv64" - echo "package linux_riscv64" > "build/linux-riscv64/empty.go" - - build="$wasmtime/target/$subdir/release" + build="$wasmtime/target/$rust_target/release" if [ ! -d "$build" ]; then - build="$wasmtime/target/$subdir/debug" + build="$wasmtime/target/$rust_target/debug" fi if [ ! -f "$build/libwasmtime.a" ]; then @@ -40,13 +31,11 @@ if [ "$target" = "riscv64" ]; then CC_riscv64gc_unknown_linux_gnu="zig cc -target riscv64-linux-gnu" \ cargo build --release --target "$rust_target" \ -p wasmtime-c-api --manifest-path "$wasmtime/crates/c-api/artifact/Cargo.toml" - build="$wasmtime/target/$subdir/release" + build="$wasmtime/target/$rust_target/release" fi - build=$(cd "$build" && pwd) - ln -s "$build/libwasmtime.a" "build/linux-riscv64/libwasmtime.a" + cp "$build/libwasmtime.a" "build/linux-riscv64/libwasmtime.a" - # Copy headers cp "$wasmtime"/crates/c-api/include/*.h build/include cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include @@ -57,27 +46,37 @@ if [ "$target" = "riscv64" ]; then echo "riscv64 build ready. Cross-compile with:" echo " GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 CC=\"zig cc -target riscv64-linux-gnu\" go build -ldflags \"-s -w\" ./main.go" -else - build="$wasmtime/target/release" - if [ ! -d "$build" ]; then - build="$wasmtime/target/debug" - fi - build=$(cd "$build" && pwd) + exit 0 +fi - if [ ! -f "$build/libwasmtime.a" ]; then - echo 'Missing libwasmtime.a. Build with:' - echo ' cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml' - fi +# Clean and re-create "build" directory hierarchy +rm -rf build +for d in "include" "include/wasmtime" "include/wasmtime/component" "include/wasmtime/component/types" "include/wasmtime/types" "linux-x86_64" "macos-x86_64" "windows-x86_64" "linux-aarch64" "macos-aarch64"; do + path="build/$d" + mkdir -p "$path" + name=$(basename $d | tr - _) + echo "package $name" > "$path/empty.go" +done - for d in "linux-x86_64" "macos-x86_64" "linux-aarch64" "macos-aarch64"; do - ln -s "$build/libwasmtime.a" "build/$d/libwasmtime.a" - done +build="$wasmtime/target/release" +if [ ! -d "$build" ]; then + build="$wasmtime/target/debug" +fi +build=$(cd "$build" && pwd) - cp "$wasmtime"/crates/c-api/include/*.h build/include - cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include +if [ ! -f "$build/libwasmtime.a" ]; then + echo 'Missing libwasmtime.a. Build with:' + echo ' cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml' +fi - conf=$(find "$build"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) - if [ -n "$conf" ]; then - cp "$conf" build/include/wasmtime/conf.h - fi +for d in "linux-x86_64" "macos-x86_64" "linux-aarch64" "macos-aarch64"; do + ln -s "$build/libwasmtime.a" "build/$d/libwasmtime.a" +done + +cp "$wasmtime"/crates/c-api/include/*.h build/include +cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include + +conf=$(find "$build"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) +if [ -n "$conf" ]; then + cp "$conf" build/include/wasmtime/conf.h fi From a043a8f58efcf8cdb6dfa8674b7762bed71e5042 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 15:00:50 +0300 Subject: [PATCH 07/25] ci(test-minimal-runtime): back up riscv64 lib and download v44 libs Signed-off-by: Rodney Osodo --- ci/test-minimal-runtime/test.sh | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/ci/test-minimal-runtime/test.sh b/ci/test-minimal-runtime/test.sh index aa242955..af675972 100755 --- a/ci/test-minimal-runtime/test.sh +++ b/ci/test-minimal-runtime/test.sh @@ -1,17 +1,36 @@ #!/usr/bin/env bash set -euo pipefail -cd "$(dirname "$0")" +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +cd "$SCRIPT_DIR" trap 'rm -rf vendor module.cwasm' EXIT -# Step 1: Create a pre-compiled module using the full Wasmtime library. +WASMTIME_GO="$(cd "$SCRIPT_DIR/../.." && pwd)" +DOWNLOAD_SCRIPT="$WASMTIME_GO/ci/download-wasmtime.py" + +# Step 1: Ensure the local wasmtime-go build directory has the v44 full libraries. +# The local build may contain libraries built from a different wasmtime version +# (e.g., v46 from local Rust source), which causes serialization format mismatches. +# We temporarily replace them with the official v44 release binaries. +# Backup linux-riscv64 FIRST since download-wasmtime.py clears all files in build/. +if [ -f "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" ]; then + cp "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" /tmp/linux-riscv64-libwasmtime.a +fi +( + cd "$WASMTIME_GO" + python3 "$DOWNLOAD_SCRIPT" +) +if [ -f /tmp/linux-riscv64-libwasmtime.a ]; then + mv /tmp/linux-riscv64-libwasmtime.a "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" +fi + +# Step 2: Create a pre-compiled module using the full Wasmtime library. go run create_cwasm.go -# Step 2: Vendor the module, then adjust the vendored copy so it compiles +# Step 3: Vendor the module, then adjust the vendored copy so it compiles # against the minimal Wasmtime library: # a) Remove Go source files that call C functions absent from the min binary. # b) Download the min static libraries over the full ones so CGO links the right lib. -DOWNLOAD_SCRIPT="$(pwd)/../download-wasmtime.py" go mod vendor ( cd vendor/github.com/bytecodealliance/wasmtime-go/v45 @@ -19,5 +38,5 @@ go mod vendor python3 "$DOWNLOAD_SCRIPT" --min ) -# Step 3: Test that the minimal Wasmtime binary can deserialize and run a module. +# Step 4: Test that the minimal Wasmtime binary can deserialize and run a module. go test -count=1 . From 2a779d5f031bfbe54ae154f13e58a795cfe2626b Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 7 May 2026 15:24:20 +0300 Subject: [PATCH 08/25] ci(download-wasmtime): ensure build/linux-riscv64 exists Signed-off-by: Rodney Osodo --- ci/download-wasmtime.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ci/download-wasmtime.py b/ci/download-wasmtime.py index a36020a0..ae9b91d2 100644 --- a/ci/download-wasmtime.py +++ b/ci/download-wasmtime.py @@ -33,6 +33,9 @@ except FileNotFoundError: os.makedirs('build') +# Ensure linux-riscv64 directory exists (not in official releases, but imported by includebuild.go) +os.makedirs('build/linux-riscv64', exist_ok=True) + for i, arr in enumerate(urls): filename, dirname = arr filename = filename.format(version) From 953920e9474b2f789b1244ea19a2bab82f960f6d Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Fri, 8 May 2026 12:43:57 +0300 Subject: [PATCH 09/25] ci: download prebuilt riscv64 wasmtime C API artifact --- ci/download-wasmtime.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ci/download-wasmtime.py b/ci/download-wasmtime.py index ae9b91d2..dd462f2a 100644 --- a/ci/download-wasmtime.py +++ b/ci/download-wasmtime.py @@ -22,6 +22,7 @@ ['wasmtime-{}-x86_64-macos-c-api.tar.xz', 'macos-x86_64'], ['wasmtime-{}-aarch64-linux-c-api.tar.xz', 'linux-aarch64'], ['wasmtime-{}-aarch64-macos-c-api.tar.xz', 'macos-aarch64'], + ['wasmtime-{}-riscv64gc-unknown-linux-gnu-c-api.tar.xz', 'linux-riscv64'], ] try: @@ -33,9 +34,6 @@ except FileNotFoundError: os.makedirs('build') -# Ensure linux-riscv64 directory exists (not in official releases, but imported by includebuild.go) -os.makedirs('build/linux-riscv64', exist_ok=True) - for i, arr in enumerate(urls): filename, dirname = arr filename = filename.format(version) From afe5a99da81b953e7f8ded90e8647e6e1a1f9de0 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Fri, 8 May 2026 15:48:09 +0300 Subject: [PATCH 10/25] fix: review corrections for riscv64 support - Fix riscv64 tarball filename (riscv64gc-linux not riscv64gc-unknown-linux-gnu) - Skip riscv64 entry when --min flag is set (no minimal variant published) - Add tarfile path-traversal guard (filter=data on Python 3.12+) - Use mktemp for riscv64 backup in test.sh; restore on EXIT via trap - Fix stale version example in test.sh comment - Extract duplicate copy_c_api_headers into a function in local.sh - Add exit 1 after missing libwasmtime.a warning in local.sh - Pin wasmtime clone to v44.0.0 tag in CI; remove redundant rustup target add and dead-work cargo build (local.sh handles both) - Downgrade example/go.mod from go 1.26.2 to go 1.18 to match CI toolchain - Fix example module path to bytecodealliance namespace - Add Linux riscv64 to supported platforms in README --- .github/workflows/example-build.yml | 4 +--- README.md | 2 +- ci/download-wasmtime.py | 10 ++++++++-- ci/local.sh | 28 ++++++++++++++-------------- ci/test-minimal-runtime/test.sh | 12 +++++++----- example/go.mod | 4 ++-- 6 files changed, 33 insertions(+), 27 deletions(-) diff --git a/.github/workflows/example-build.yml b/.github/workflows/example-build.yml index 48d22d2f..c6b66e39 100644 --- a/.github/workflows/example-build.yml +++ b/.github/workflows/example-build.yml @@ -47,10 +47,8 @@ jobs: - name: Build wasmtime for riscv64 if: matrix.goarch == 'riscv64' run: | - rustup target add riscv64gc-unknown-linux-gnu - git clone --depth 1 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime + git clone --depth 1 --branch v44.0.0 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime git -C /tmp/wasmtime submodule update --init - cargo build --release -p wasmtime-c-api --manifest-path /tmp/wasmtime/crates/c-api/artifact/Cargo.toml ./ci/local.sh /tmp/wasmtime riscv64 - name: Build example (amd64) diff --git a/README.md b/README.md index 765afa99..345c1a53 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ This Go library uses CGO to consume the C API of the [Wasmtime project][wasmtime] which is written in Rust. Precompiled binaries of Wasmtime are checked into this repository on tagged releases so you won't have to install Wasmtime locally, but it means that this project only works on Linux x86\_64, -Linux aarch64, macOS x86\_64, macOS aarch64, and Windows x86\_64 currently. +Linux aarch64, Linux riscv64, macOS x86\_64, macOS aarch64, and Windows x86\_64 currently. Building on other platforms will need to arrange to build Wasmtime and use `CGO_*` env vars to compile correctly. diff --git a/ci/download-wasmtime.py b/ci/download-wasmtime.py index dd462f2a..bb69a2bf 100644 --- a/ci/download-wasmtime.py +++ b/ci/download-wasmtime.py @@ -22,7 +22,7 @@ ['wasmtime-{}-x86_64-macos-c-api.tar.xz', 'macos-x86_64'], ['wasmtime-{}-aarch64-linux-c-api.tar.xz', 'linux-aarch64'], ['wasmtime-{}-aarch64-macos-c-api.tar.xz', 'macos-aarch64'], - ['wasmtime-{}-riscv64gc-unknown-linux-gnu-c-api.tar.xz', 'linux-riscv64'], + ['wasmtime-{}-riscv64gc-linux-c-api.tar.xz', 'linux-riscv64'], ] try: @@ -50,13 +50,19 @@ z.extractall() else: t = tarfile.open(fileobj=io.BytesIO(contents)) - t.extractall() + if hasattr(tarfile, 'data_filter'): + t.extractall(filter='data') + else: + t.extractall() src = filename.replace('.zip', '').replace('.tar.xz', '') include_src = src + '/min/include' if args.min else src + '/include' if i == 0: shutil.copytree(include_src, 'build/include', dirs_exist_ok=True) + if args.min and dirname == 'linux-riscv64': + shutil.rmtree(src) + continue lib_src = src + '/min/lib' if args.min else src + '/lib' shutil.copytree(lib_src, 'build/' + dirname, dirs_exist_ok=True) shutil.rmtree(src) diff --git a/ci/local.sh b/ci/local.sh index 4dbed547..72e4890d 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -1,5 +1,16 @@ #!/bin/bash +copy_c_api_headers() { + local wasmtime_dir=$1 build_dir=$2 + cp "$wasmtime_dir"/crates/c-api/include/*.h build/include + cp -r "$wasmtime_dir"/crates/c-api/include/wasmtime build/include + local conf + conf=$(find "$build_dir"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) + if [ -n "$conf" ]; then + cp "$conf" build/include/wasmtime/conf.h + fi +} + wasmtime=$1 if [ "$wasmtime" = "" ]; then echo "usage: $0 [target]" @@ -36,13 +47,7 @@ if [ "$target" = "riscv64" ]; then cp "$build/libwasmtime.a" "build/linux-riscv64/libwasmtime.a" - cp "$wasmtime"/crates/c-api/include/*.h build/include - cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include - - conf=$(find "$build"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) - if [ -n "$conf" ]; then - cp "$conf" build/include/wasmtime/conf.h - fi + copy_c_api_headers "$wasmtime" "$build" echo "riscv64 build ready. Cross-compile with:" echo " GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 CC=\"zig cc -target riscv64-linux-gnu\" go build -ldflags \"-s -w\" ./main.go" @@ -67,16 +72,11 @@ build=$(cd "$build" && pwd) if [ ! -f "$build/libwasmtime.a" ]; then echo 'Missing libwasmtime.a. Build with:' echo ' cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml' + exit 1 fi for d in "linux-x86_64" "macos-x86_64" "linux-aarch64" "macos-aarch64"; do ln -s "$build/libwasmtime.a" "build/$d/libwasmtime.a" done -cp "$wasmtime"/crates/c-api/include/*.h build/include -cp -r "$wasmtime"/crates/c-api/include/wasmtime build/include - -conf=$(find "$build"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) -if [ -n "$conf" ]; then - cp "$conf" build/include/wasmtime/conf.h -fi +copy_c_api_headers "$wasmtime" "$build" diff --git a/ci/test-minimal-runtime/test.sh b/ci/test-minimal-runtime/test.sh index af675972..f191f20f 100755 --- a/ci/test-minimal-runtime/test.sh +++ b/ci/test-minimal-runtime/test.sh @@ -3,25 +3,27 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" cd "$SCRIPT_DIR" -trap 'rm -rf vendor module.cwasm' EXIT WASMTIME_GO="$(cd "$SCRIPT_DIR/../.." && pwd)" DOWNLOAD_SCRIPT="$WASMTIME_GO/ci/download-wasmtime.py" # Step 1: Ensure the local wasmtime-go build directory has the v44 full libraries. # The local build may contain libraries built from a different wasmtime version -# (e.g., v46 from local Rust source), which causes serialization format mismatches. +# (e.g., a newer version built from local Rust source), which causes serialization format mismatches. # We temporarily replace them with the official v44 release binaries. # Backup linux-riscv64 FIRST since download-wasmtime.py clears all files in build/. +_riscv64_backup="" if [ -f "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" ]; then - cp "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" /tmp/linux-riscv64-libwasmtime.a + _riscv64_backup=$(mktemp /tmp/linux-riscv64-libwasmtime-XXXXXX.a) + cp "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" "$_riscv64_backup" fi +trap 'rm -rf vendor module.cwasm; [ -n "$_riscv64_backup" ] && [ -f "$_riscv64_backup" ] && mv "$_riscv64_backup" "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" || true' EXIT ( cd "$WASMTIME_GO" python3 "$DOWNLOAD_SCRIPT" ) -if [ -f /tmp/linux-riscv64-libwasmtime.a ]; then - mv /tmp/linux-riscv64-libwasmtime.a "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" +if [ -n "$_riscv64_backup" ] && [ -f "$_riscv64_backup" ]; then + mv "$_riscv64_backup" "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" fi # Step 2: Create a pre-compiled module using the full Wasmtime library. diff --git a/example/go.mod b/example/go.mod index 1debf675..8e6eb232 100644 --- a/example/go.mod +++ b/example/go.mod @@ -1,6 +1,6 @@ -module rodneyosodo.com/propeller-example +module github.com/bytecodealliance/wasmtime-go/v44/example -go 1.26.2 +go 1.18 require github.com/bytecodealliance/wasmtime-go/v44 v44.0.0 From bf1cd10b6ef57bba8ba448660fcad98e70c709be Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Fri, 8 May 2026 16:56:05 +0300 Subject: [PATCH 11/25] fix: address remaining PR review comments - Add linux-riscv64 to native local.sh build loop so go mod vendor works without cross-compiling (includebuild.go imports it unconditionally) - Add cd example/ to README riscv64 cross-compile snippet --- README.md | 1 + ci/local.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 345c1a53..184adcff 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ API artifact, and set up the `build/linux-riscv64` directory. Then cross-compile your Go program: ```sh +$ cd example $ GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 \ CC="zig cc -target riscv64-linux-gnu" \ go build -ldflags "-s -w" ./main.go diff --git a/ci/local.sh b/ci/local.sh index 72e4890d..ff361dfe 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -56,7 +56,7 @@ fi # Clean and re-create "build" directory hierarchy rm -rf build -for d in "include" "include/wasmtime" "include/wasmtime/component" "include/wasmtime/component/types" "include/wasmtime/types" "linux-x86_64" "macos-x86_64" "windows-x86_64" "linux-aarch64" "macos-aarch64"; do +for d in "include" "include/wasmtime" "include/wasmtime/component" "include/wasmtime/component/types" "include/wasmtime/types" "linux-x86_64" "macos-x86_64" "windows-x86_64" "linux-aarch64" "macos-aarch64" "linux-riscv64"; do path="build/$d" mkdir -p "$path" name=$(basename $d | tr - _) From 24664d2a6c364f2a1edbe1866c171bc20c2ff181 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Fri, 8 May 2026 17:01:01 +0300 Subject: [PATCH 12/25] fix: respect TMPDIR in mktemp call for riscv64 backup --- ci/test-minimal-runtime/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/test-minimal-runtime/test.sh b/ci/test-minimal-runtime/test.sh index f191f20f..7730fb8c 100755 --- a/ci/test-minimal-runtime/test.sh +++ b/ci/test-minimal-runtime/test.sh @@ -14,7 +14,7 @@ DOWNLOAD_SCRIPT="$WASMTIME_GO/ci/download-wasmtime.py" # Backup linux-riscv64 FIRST since download-wasmtime.py clears all files in build/. _riscv64_backup="" if [ -f "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" ]; then - _riscv64_backup=$(mktemp /tmp/linux-riscv64-libwasmtime-XXXXXX.a) + _riscv64_backup=$(mktemp "${TMPDIR:-/tmp}/linux-riscv64-libwasmtime-XXXXXX.a") cp "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" "$_riscv64_backup" fi trap 'rm -rf vendor module.cwasm; [ -n "$_riscv64_backup" ] && [ -f "$_riscv64_backup" ] && mv "$_riscv64_backup" "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" || true' EXIT From 1a1df1b5b586906fc613b97e8c5dca284ed9d884 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 4 Jun 2026 11:34:19 +0300 Subject: [PATCH 13/25] build(example): bump wasmtime-go to v45 Signed-off-by: Rodney Osodo --- .github/workflows/example-build.yml | 2 +- README.md | 2 +- ci/test-minimal-runtime/test.sh | 4 ++-- example/go.mod | 6 +++--- example/main.go | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/example-build.yml b/.github/workflows/example-build.yml index c6b66e39..7bd9dee6 100644 --- a/.github/workflows/example-build.yml +++ b/.github/workflows/example-build.yml @@ -47,7 +47,7 @@ jobs: - name: Build wasmtime for riscv64 if: matrix.goarch == 'riscv64' run: | - git clone --depth 1 --branch v44.0.0 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime + git clone --depth 1 --branch v45.0.0 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime git -C /tmp/wasmtime submodule update --init ./ci/local.sh /tmp/wasmtime riscv64 diff --git a/README.md b/README.md index 184adcff..4e79c7c0 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ Make sure everything passes at the current version. Next run: ``` -$ git ls-files | xargs sed -i 's/v44/v45/g' +$ git ls-files | xargs sed -i 's/v45/v46/g' $ python3 ci/download-wasmtime.py $ go test ``` diff --git a/ci/test-minimal-runtime/test.sh b/ci/test-minimal-runtime/test.sh index 7730fb8c..5522e731 100755 --- a/ci/test-minimal-runtime/test.sh +++ b/ci/test-minimal-runtime/test.sh @@ -7,10 +7,10 @@ cd "$SCRIPT_DIR" WASMTIME_GO="$(cd "$SCRIPT_DIR/../.." && pwd)" DOWNLOAD_SCRIPT="$WASMTIME_GO/ci/download-wasmtime.py" -# Step 1: Ensure the local wasmtime-go build directory has the v44 full libraries. +# Step 1: Ensure the local wasmtime-go build directory has the v45 full libraries. # The local build may contain libraries built from a different wasmtime version # (e.g., a newer version built from local Rust source), which causes serialization format mismatches. -# We temporarily replace them with the official v44 release binaries. +# We temporarily replace them with the official v45 release binaries. # Backup linux-riscv64 FIRST since download-wasmtime.py clears all files in build/. _riscv64_backup="" if [ -f "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" ]; then diff --git a/example/go.mod b/example/go.mod index 8e6eb232..28ae9f70 100644 --- a/example/go.mod +++ b/example/go.mod @@ -1,7 +1,7 @@ -module github.com/bytecodealliance/wasmtime-go/v44/example +module github.com/bytecodealliance/wasmtime-go/v45/example go 1.18 -require github.com/bytecodealliance/wasmtime-go/v44 v44.0.0 +require github.com/bytecodealliance/wasmtime-go/v45 v45.0.0 -replace github.com/bytecodealliance/wasmtime-go/v44 => ../ +replace github.com/bytecodealliance/wasmtime-go/v45 => ../ diff --git a/example/main.go b/example/main.go index 747ee0a3..bafdc760 100644 --- a/example/main.go +++ b/example/main.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "github.com/bytecodealliance/wasmtime-go/v44" + "github.com/bytecodealliance/wasmtime-go/v45" ) func main() { From 6362760fd905f76e97fea7b6f338f0175b0aabed Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 4 Jun 2026 11:36:46 +0300 Subject: [PATCH 14/25] ci(workflow): pin Python version to 3.8 Signed-off-by: Rodney Osodo --- .github/workflows/example-build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/example-build.yml b/.github/workflows/example-build.yml index 7bd9dee6..f510aa60 100644 --- a/.github/workflows/example-build.yml +++ b/.github/workflows/example-build.yml @@ -17,12 +17,13 @@ jobs: fail-fast: false matrix: goarch: [amd64, arm64, riscv64] + steps: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: - python-version: "3.x" + python-version: 3.8 - run: python ci/download-wasmtime.py From d2655d090850a129aa70400dcd2b4af0c5c035c6 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 4 Jun 2026 11:39:08 +0300 Subject: [PATCH 15/25] feat(build): add linux-riscv64 support Signed-off-by: Rodney Osodo --- BUILD.bazel | 1 + README.md | 2 +- ci/download-wasmtime.py | 3 +++ includebuild.go | 1 + 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/BUILD.bazel b/BUILD.bazel index 3532d60c..f2be686e 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -8,6 +8,7 @@ cc_library( "@io_bazel_rules_go//go/platform:windows_amd64": ["build/windows-x86_64/libwasmtime.a"], "@io_bazel_rules_go//go/platform:darwin_arm64": ["build/macos-aarch64/libwasmtime.a"], "@io_bazel_rules_go//go/platform:linux_arm64": ["build/linux-aarch64/libwasmtime.a"], + "@io_bazel_rules_go//go/platform:linux_riscv64": ["build/linux-riscv64/libwasmtime.a"], }), hdrs = glob(["build/include/**/*.h"]), includes = ["build/include"], diff --git a/README.md b/README.md index 4e79c7c0..fa277546 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ ## Installation ```sh -go get -u github.com/bytecodealliance/wasmtime-go/v45@v45.0.1 +go get -u github.com/bytecodealliance/wasmtime-go/v45@v45.0.0 ``` Be sure to check out the [API documentation][api]! diff --git a/ci/download-wasmtime.py b/ci/download-wasmtime.py index bb69a2bf..e34657cc 100644 --- a/ci/download-wasmtime.py +++ b/ci/download-wasmtime.py @@ -60,6 +60,9 @@ if i == 0: shutil.copytree(include_src, 'build/include', dirs_exist_ok=True) + # The minimal runtime is not published for riscv64; skip it so we + # do not overwrite a locally-built full library with a missing min + # artifact. if args.min and dirname == 'linux-riscv64': shutil.rmtree(src) continue diff --git a/includebuild.go b/includebuild.go index e20726e3..c342cb9d 100644 --- a/includebuild.go +++ b/includebuild.go @@ -18,6 +18,7 @@ import ( _ "github.com/bytecodealliance/wasmtime-go/v45/build/include/wasmtime/component/types" _ "github.com/bytecodealliance/wasmtime-go/v45/build/include/wasmtime/types" _ "github.com/bytecodealliance/wasmtime-go/v45/build/linux-aarch64" + _ "github.com/bytecodealliance/wasmtime-go/v45/build/linux-riscv64" _ "github.com/bytecodealliance/wasmtime-go/v45/build/linux-x86_64" _ "github.com/bytecodealliance/wasmtime-go/v45/build/macos-aarch64" _ "github.com/bytecodealliance/wasmtime-go/v45/build/macos-x86_64" From c73b7ab068825f4b6cda4de7d63f62271fd14416 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 4 Jun 2026 11:48:36 +0300 Subject: [PATCH 16/25] ci(local): add Zig wrapper to strip --target for riscv Signed-off-by: Rodney Osodo --- ci/local.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ci/local.sh b/ci/local.sh index ff361dfe..142e4d40 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail copy_c_api_headers() { local wasmtime_dir=$1 build_dir=$2 @@ -39,9 +40,24 @@ if [ "$target" = "riscv64" ]; then if [ ! -f "$build/libwasmtime.a" ]; then echo "Building wasmtime-c-api for $rust_target ..." - CC_riscv64gc_unknown_linux_gnu="zig cc -target riscv64-linux-gnu" \ + # Zig 0.14+ interprets --target as its own native target flag, but the cc + # crate passes an LLVM triple (e.g. --target=riscv64-unknown-linux-gnu) + # when cross-compiling. Use a wrapper that strips the conflicting flag. + zig_wrapper=$(mktemp) + cat > "$zig_wrapper" <<'EOF' +#!/bin/bash +args=() +for arg in "$@"; do + [[ "$arg" == --target=* ]] && continue + args+=("$arg") +done +exec zig cc -target riscv64-linux-gnu "${args[@]}" +EOF + chmod +x "$zig_wrapper" + CC_riscv64gc_unknown_linux_gnu="$zig_wrapper" \ cargo build --release --target "$rust_target" \ -p wasmtime-c-api --manifest-path "$wasmtime/crates/c-api/artifact/Cargo.toml" + rm -f "$zig_wrapper" build="$wasmtime/target/$rust_target/release" fi From 8fce0cb887e3d8c64bfc019d846ca79a37ac18f4 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 4 Jun 2026 11:55:20 +0300 Subject: [PATCH 17/25] ci(local): set Cargo RISC-V linker env for local build Signed-off-by: Rodney Osodo --- ci/local.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/local.sh b/ci/local.sh index 142e4d40..6b14afa9 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -55,6 +55,7 @@ exec zig cc -target riscv64-linux-gnu "${args[@]}" EOF chmod +x "$zig_wrapper" CC_riscv64gc_unknown_linux_gnu="$zig_wrapper" \ + CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER="zig cc -target riscv64-linux-gnu" \ cargo build --release --target "$rust_target" \ -p wasmtime-c-api --manifest-path "$wasmtime/crates/c-api/artifact/Cargo.toml" rm -f "$zig_wrapper" From d9b7e9e7362c66566eb767f873d2bda1a2c596af Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Thu, 4 Jun 2026 12:02:08 +0300 Subject: [PATCH 18/25] ci(local): Create separate Zig compile and link wrappers Signed-off-by: Rodney Osodo --- ci/local.sh | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ci/local.sh b/ci/local.sh index 6b14afa9..c65294f0 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -43,8 +43,12 @@ if [ "$target" = "riscv64" ]; then # Zig 0.14+ interprets --target as its own native target flag, but the cc # crate passes an LLVM triple (e.g. --target=riscv64-unknown-linux-gnu) # when cross-compiling. Use a wrapper that strips the conflicting flag. - zig_wrapper=$(mktemp) - cat > "$zig_wrapper" <<'EOF' + # Cargo's CC env var can accept a command string, but the LINKER env var + # expects a single executable path. Create two wrapper scripts: one for + # compiling (strips --target to avoid Zig flag collision) and one for + # linking (invokes zig cc directly). + zig_cc_wrapper=$(mktemp) + cat > "$zig_cc_wrapper" <<'EOF' #!/bin/bash args=() for arg in "$@"; do @@ -53,12 +57,20 @@ for arg in "$@"; do done exec zig cc -target riscv64-linux-gnu "${args[@]}" EOF - chmod +x "$zig_wrapper" - CC_riscv64gc_unknown_linux_gnu="$zig_wrapper" \ - CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER="zig cc -target riscv64-linux-gnu" \ + chmod +x "$zig_cc_wrapper" + + zig_ld_wrapper=$(mktemp) + cat > "$zig_ld_wrapper" <<'EOF' +#!/bin/bash +exec zig cc -target riscv64-linux-gnu "$@" +EOF + chmod +x "$zig_ld_wrapper" + + CC_riscv64gc_unknown_linux_gnu="$zig_cc_wrapper" \ + CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER="$zig_ld_wrapper" \ cargo build --release --target "$rust_target" \ -p wasmtime-c-api --manifest-path "$wasmtime/crates/c-api/artifact/Cargo.toml" - rm -f "$zig_wrapper" + rm -f "$zig_cc_wrapper" "$zig_ld_wrapper" build="$wasmtime/target/$rust_target/release" fi From 24d23442c08cf4f328823de298432d795fbe35c4 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Tue, 9 Jun 2026 13:55:37 +0300 Subject: [PATCH 19/25] ci(workflows): consolidate example build into main workflow Signed-off-by: Rodney Osodo --- .github/workflows/example-build.yml | 135 ---------------------------- .github/workflows/main.yml | 97 ++++++++++++++++++++ 2 files changed, 97 insertions(+), 135 deletions(-) delete mode 100644 .github/workflows/example-build.yml diff --git a/.github/workflows/example-build.yml b/.github/workflows/example-build.yml deleted file mode 100644 index f510aa60..00000000 --- a/.github/workflows/example-build.yml +++ /dev/null @@ -1,135 +0,0 @@ -name: Example Build - -on: - pull_request: - branches: [main] - push: - branches: [main] - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - build-example-linux: - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - goarch: [amd64, arm64, riscv64] - - steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-python@v5 - with: - python-version: 3.8 - - - run: python ci/download-wasmtime.py - - - uses: actions/setup-go@v5 - with: - go-version: "1.24.x" - - - name: Install cross-compiler (arm64) - if: matrix.goarch == 'arm64' - run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu - - - name: Install zig (riscv64) - if: matrix.goarch == 'riscv64' - uses: mlugg/setup-zig@v2 - with: - version: 0.14.0 - - - name: Install Rust toolchain (riscv64) - if: matrix.goarch == 'riscv64' - uses: dtolnay/rust-toolchain@stable - - - name: Build wasmtime for riscv64 - if: matrix.goarch == 'riscv64' - run: | - git clone --depth 1 --branch v45.0.0 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime - git -C /tmp/wasmtime submodule update --init - ./ci/local.sh /tmp/wasmtime riscv64 - - - name: Build example (amd64) - if: matrix.goarch == 'amd64' - working-directory: example - run: CGO_ENABLED=1 go build -ldflags "-s -w" ./main.go - - - name: Build example (arm64) - if: matrix.goarch == 'arm64' - working-directory: example - env: - GOOS: linux - GOARCH: arm64 - CGO_ENABLED: 1 - CC: aarch64-linux-gnu-gcc - run: go build -ldflags "-s -w" ./main.go - - - name: Build example (riscv64) - if: matrix.goarch == 'riscv64' - working-directory: example - env: - GOOS: linux - GOARCH: riscv64 - CGO_ENABLED: 1 - CC: zig cc -target riscv64-linux-gnu - run: go build -ldflags "-s -w" ./main.go - - - name: Verify binary - working-directory: example - run: file main - - build-example-macos: - runs-on: macos-latest - strategy: - fail-fast: false - matrix: - goarch: [amd64, arm64] - steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-python@v5 - with: - python-version: "3.x" - - run: python ci/download-wasmtime.py - - - uses: actions/setup-go@v5 - with: - go-version: "1.24.x" - - - name: Build example - working-directory: example - env: - GOARCH: ${{ matrix.goarch }} - CGO_ENABLED: 1 - run: go build -ldflags "-s -w" ./main.go - - - name: Verify binary - working-directory: example - run: file main - - build-example-windows: - runs-on: windows-2025 - steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-python@v5 - with: - python-version: "3.x" - - run: python ci/download-wasmtime.py - - - uses: actions/setup-go@v5 - with: - go-version: "1.24.x" - - - name: Build example - working-directory: example - env: - CGO_ENABLED: 1 - run: go build -ldflags "-s -w" ./main.go - - - name: Verify binary - working-directory: example - run: file main.exe diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f9beaeb3..c0b9c19d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -77,6 +77,103 @@ jobs: run: ./ci/test-minimal-runtime/test.sh if: runner.os != 'Windows' + build-example: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-2025] + goarch: [amd64, arm64, riscv64] + exclude: + - os: macos-latest + goarch: riscv64 + - os: windows-2025 + goarch: arm64 + - os: windows-2025 + goarch: riscv64 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v2 + with: + python-version: 3.8 + - run: python ci/download-wasmtime.py + + - uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go || '1.24.x' }} + + - name: Install cross-compiler (arm64) + if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'arm64' + run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu + + - name: Install zig (riscv64) + if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' + uses: mlugg/setup-zig@v2 + with: + version: 0.14.0 + + - name: Install Rust toolchain (riscv64) + if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' + uses: dtolnay/rust-toolchain@stable + + - name: Build wasmtime for riscv64 + if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' + run: | + git clone --depth 1 --branch v45.0.0 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime + git -C /tmp/wasmtime submodule update --init + ./ci/local.sh /tmp/wasmtime riscv64 + + - name: Build example (linux amd64) + if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64' + working-directory: example + run: CGO_ENABLED=1 go build -ldflags "-s -w" ./main.go + + - name: Build example (linux arm64) + if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'arm64' + working-directory: example + env: + GOOS: linux + GOARCH: arm64 + CGO_ENABLED: 1 + CC: aarch64-linux-gnu-gcc + run: go build -ldflags "-s -w" ./main.go + + - name: Build example (linux riscv64) + if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' + working-directory: example + env: + GOOS: linux + GOARCH: riscv64 + CGO_ENABLED: 1 + CC: zig cc -target riscv64-linux-gnu + run: go build -ldflags "-s -w" ./main.go + + - name: Build example (macOS) + if: matrix.os == 'macos-latest' + working-directory: example + env: + GOARCH: ${{ matrix.goarch }} + CGO_ENABLED: 1 + run: go build -ldflags "-s -w" ./main.go + + - name: Build example (Windows) + if: matrix.os == 'windows-2025' + working-directory: example + env: + CGO_ENABLED: 1 + run: go build -ldflags "-s -w" ./main.go + + - name: Verify binary + working-directory: example + run: file main + if: runner.os != 'Windows' + + - name: Verify binary (Windows) + working-directory: example + run: file main.exe + if: runner.os == 'Windows' + coverage: runs-on: ubuntu-latest steps: From d23a88b525a2933093583f31259507231d4118ea Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Tue, 9 Jun 2026 14:01:31 +0300 Subject: [PATCH 20/25] ci(download-wasmtime): remove riscv64 min skip Signed-off-by: Rodney Osodo --- ci/download-wasmtime.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ci/download-wasmtime.py b/ci/download-wasmtime.py index e34657cc..6fe720cd 100644 --- a/ci/download-wasmtime.py +++ b/ci/download-wasmtime.py @@ -60,12 +60,6 @@ if i == 0: shutil.copytree(include_src, 'build/include', dirs_exist_ok=True) - # The minimal runtime is not published for riscv64; skip it so we - # do not overwrite a locally-built full library with a missing min - # artifact. - if args.min and dirname == 'linux-riscv64': - shutil.rmtree(src) - continue lib_src = src + '/min/lib' if args.min else src + '/lib' shutil.copytree(lib_src, 'build/' + dirname, dirs_exist_ok=True) shutil.rmtree(src) From 9905d7d8d69d964f9390ece2b67b706025b6e806 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Tue, 9 Jun 2026 15:07:00 +0300 Subject: [PATCH 21/25] ci(build): remove riscv64 cross-compile steps and use CMake Signed-off-by: Rodney Osodo --- .github/workflows/main.yml | 11 ---- README.md | 20 ------ ci/local.sh | 128 ++++++++++--------------------------- 3 files changed, 35 insertions(+), 124 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c0b9c19d..4eacabb7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -113,17 +113,6 @@ jobs: with: version: 0.14.0 - - name: Install Rust toolchain (riscv64) - if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' - uses: dtolnay/rust-toolchain@stable - - - name: Build wasmtime for riscv64 - if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' - run: | - git clone --depth 1 --branch v45.0.0 https://github.com/bytecodealliance/wasmtime.git /tmp/wasmtime - git -C /tmp/wasmtime submodule update --init - ./ci/local.sh /tmp/wasmtime riscv64 - - name: Build example (linux amd64) if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64' working-directory: example diff --git a/README.md b/README.md index fa277546..eb01e5ca 100644 --- a/README.md +++ b/README.md @@ -153,26 +153,6 @@ $ go test And after that you should be good to go! -### Cross-compiling for Linux riscv64 - -Pass `riscv64` as the second argument to `ci/local.sh`. This requires -[zig](https://ziglang.org/) as the C cross-compiler: - -```sh -$ ./ci/local.sh /path/to/wasmtime riscv64 -``` - -This will automatically install the Rust target, cross-compile the Wasmtime C -API artifact, and set up the `build/linux-riscv64` directory. Then cross-compile -your Go program: - -```sh -$ cd example -$ GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 \ - CC="zig cc -target riscv64-linux-gnu" \ - go build -ldflags "-s -w" ./main.go -``` - ### Release Checklist First run: diff --git a/ci/local.sh b/ci/local.sh index c65294f0..98e49e4d 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -1,111 +1,53 @@ #!/bin/bash set -euo pipefail -copy_c_api_headers() { - local wasmtime_dir=$1 build_dir=$2 - cp "$wasmtime_dir"/crates/c-api/include/*.h build/include - cp -r "$wasmtime_dir"/crates/c-api/include/wasmtime build/include - local conf - conf=$(find "$build_dir"/build/wasmtime-c-api-impl-*/out/include/wasmtime/conf.h 2>/dev/null | head -1) - if [ -n "$conf" ]; then - cp "$conf" build/include/wasmtime/conf.h - fi -} - wasmtime=$1 if [ "$wasmtime" = "" ]; then - echo "usage: $0 [target]" - echo " target: native (default), riscv64" + echo "usage: $0 " exit 1 fi -target=${2:-native} - -if [ "$target" = "riscv64" ]; then - # Add riscv64 to existing build directory (does not remove other platforms) - mkdir -p "build/linux-riscv64" "build/include" "build/include/wasmtime" - echo "package linux_riscv64" > "build/linux-riscv64/empty.go" - - rust_target="riscv64gc-unknown-linux-gnu" - - if command -v rustup &>/dev/null && ! rustup target list --installed 2>/dev/null | grep -q "$rust_target"; then - echo "Installing Rust target $rust_target ..." - rustup target add "$rust_target" - fi - - build="$wasmtime/target/$rust_target/release" - if [ ! -d "$build" ]; then - build="$wasmtime/target/$rust_target/debug" - fi - - if [ ! -f "$build/libwasmtime.a" ]; then - echo "Building wasmtime-c-api for $rust_target ..." - # Zig 0.14+ interprets --target as its own native target flag, but the cc - # crate passes an LLVM triple (e.g. --target=riscv64-unknown-linux-gnu) - # when cross-compiling. Use a wrapper that strips the conflicting flag. - # Cargo's CC env var can accept a command string, but the LINKER env var - # expects a single executable path. Create two wrapper scripts: one for - # compiling (strips --target to avoid Zig flag collision) and one for - # linking (invokes zig cc directly). - zig_cc_wrapper=$(mktemp) - cat > "$zig_cc_wrapper" <<'EOF' -#!/bin/bash -args=() -for arg in "$@"; do - [[ "$arg" == --target=* ]] && continue - args+=("$arg") -done -exec zig cc -target riscv64-linux-gnu "${args[@]}" -EOF - chmod +x "$zig_cc_wrapper" - - zig_ld_wrapper=$(mktemp) - cat > "$zig_ld_wrapper" <<'EOF' -#!/bin/bash -exec zig cc -target riscv64-linux-gnu "$@" -EOF - chmod +x "$zig_ld_wrapper" - - CC_riscv64gc_unknown_linux_gnu="$zig_cc_wrapper" \ - CARGO_TARGET_RISCV64GC_UNKNOWN_LINUX_GNU_LINKER="$zig_ld_wrapper" \ - cargo build --release --target "$rust_target" \ - -p wasmtime-c-api --manifest-path "$wasmtime/crates/c-api/artifact/Cargo.toml" - rm -f "$zig_cc_wrapper" "$zig_ld_wrapper" - build="$wasmtime/target/$rust_target/release" - fi - - cp "$build/libwasmtime.a" "build/linux-riscv64/libwasmtime.a" +rm -rf build - copy_c_api_headers "$wasmtime" "$build" +# Build the C API with CMake (invokes cargo under the hood) +cmake -S "$wasmtime/crates/c-api" -B "$wasmtime/build-cmake" -DCMAKE_BUILD_TYPE=Release +cmake --build "$wasmtime/build-cmake" +rm -rf "$wasmtime/build-cmake" - echo "riscv64 build ready. Cross-compile with:" - echo " GOOS=linux GOARCH=riscv64 CGO_ENABLED=1 CC=\"zig cc -target riscv64-linux-gnu\" go build -ldflags \"-s -w\" ./main.go" - exit 0 -fi - -# Clean and re-create "build" directory hierarchy -rm -rf build +# Create the expected directory structure with empty.go files for d in "include" "include/wasmtime" "include/wasmtime/component" "include/wasmtime/component/types" "include/wasmtime/types" "linux-x86_64" "macos-x86_64" "windows-x86_64" "linux-aarch64" "macos-aarch64" "linux-riscv64"; do - path="build/$d" - mkdir -p "$path" - name=$(basename $d | tr - _) - echo "package $name" > "$path/empty.go" + mkdir -p "build/$d" + name=$(basename "$d" | tr - _) + echo "package $name" > "build/$d/empty.go" done -build="$wasmtime/target/release" -if [ ! -d "$build" ]; then - build="$wasmtime/target/debug" +# Determine host platform +host_os=$(uname -s) +host_arch=$(uname -m) +case "$host_os-$host_arch" in + Linux-x86_64) platform=linux-x86_64; rust_target=x86_64-unknown-linux-gnu ;; + Linux-aarch64) platform=linux-aarch64; rust_target=aarch64-unknown-linux-gnu ;; + Linux-riscv64) platform=linux-riscv64; rust_target=riscv64gc-unknown-linux-gnu ;; + Darwin-x86_64) platform=macos-x86_64; rust_target=x86_64-apple-darwin ;; + Darwin-arm64) platform=macos-aarch64; rust_target=aarch64-apple-darwin ;; + *) echo "Unsupported platform: $host_os-$host_arch"; exit 1 ;; +esac + +# The CMake/cargo build puts artifacts in the wasmtime target directory. +# Native builds go to target/release/, cross builds to target/$rust_target/release/. +wasmtime_target_dir="$wasmtime/target/$rust_target/release" +if [ ! -d "$wasmtime_target_dir" ]; then + wasmtime_target_dir="$wasmtime/target/release" fi -build=$(cd "$build" && pwd) -if [ ! -f "$build/libwasmtime.a" ]; then - echo 'Missing libwasmtime.a. Build with:' - echo ' cargo build --release -p wasmtime-c-api --manifest-path crates/c-api/artifact/Cargo.toml' +# Find the built library (may be libwasmtime.a or wasmtime.lib) +built_lib=$(find "$wasmtime_target_dir" -maxdepth 1 -name "libwasmtime*.a" -o -name "wasmtime.lib" | head -1) +if [ -z "$built_lib" ]; then + echo "Failed to find built wasmtime library in $wasmtime_target_dir" exit 1 fi +ln -s "$built_lib" "build/$platform/libwasmtime.a" -for d in "linux-x86_64" "macos-x86_64" "linux-aarch64" "macos-aarch64"; do - ln -s "$build/libwasmtime.a" "build/$d/libwasmtime.a" -done - -copy_c_api_headers "$wasmtime" "$build" +# Copy headers +cp "$wasmtime/crates/c-api/include/"*.h build/include/ +cp -r "$wasmtime/crates/c-api/include/wasmtime" build/include/ From e128722561f72be4e11747dd976329afe76bd558 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Tue, 16 Jun 2026 15:22:02 +0300 Subject: [PATCH 22/25] ci(test-minimal-runtime): remove riscv64 backup/restore logic Signed-off-by: Rodney Osodo --- ci/test-minimal-runtime/test.sh | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/ci/test-minimal-runtime/test.sh b/ci/test-minimal-runtime/test.sh index 5522e731..41be8e92 100755 --- a/ci/test-minimal-runtime/test.sh +++ b/ci/test-minimal-runtime/test.sh @@ -11,20 +11,11 @@ DOWNLOAD_SCRIPT="$WASMTIME_GO/ci/download-wasmtime.py" # The local build may contain libraries built from a different wasmtime version # (e.g., a newer version built from local Rust source), which causes serialization format mismatches. # We temporarily replace them with the official v45 release binaries. -# Backup linux-riscv64 FIRST since download-wasmtime.py clears all files in build/. -_riscv64_backup="" -if [ -f "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" ]; then - _riscv64_backup=$(mktemp "${TMPDIR:-/tmp}/linux-riscv64-libwasmtime-XXXXXX.a") - cp "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" "$_riscv64_backup" -fi -trap 'rm -rf vendor module.cwasm; [ -n "$_riscv64_backup" ] && [ -f "$_riscv64_backup" ] && mv "$_riscv64_backup" "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" || true' EXIT +trap 'rm -rf vendor module.cwasm' EXIT ( cd "$WASMTIME_GO" python3 "$DOWNLOAD_SCRIPT" ) -if [ -n "$_riscv64_backup" ] && [ -f "$_riscv64_backup" ]; then - mv "$_riscv64_backup" "$WASMTIME_GO/build/linux-riscv64/libwasmtime.a" -fi # Step 2: Create a pre-compiled module using the full Wasmtime library. go run create_cwasm.go From 8ee07517ddd9fd5bb631e5423c9d388f861ef91d Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Tue, 16 Jun 2026 15:42:16 +0300 Subject: [PATCH 23/25] ci: remove example directory - Removed example/ directory as requested by reviewer - Cross-compilation verification handled by build-cross job in main.yml --- example/go.mod | 7 ------- example/go.sum | 8 -------- example/main.go | 43 ------------------------------------------- 3 files changed, 58 deletions(-) delete mode 100644 example/go.mod delete mode 100644 example/go.sum delete mode 100644 example/main.go diff --git a/example/go.mod b/example/go.mod deleted file mode 100644 index 28ae9f70..00000000 --- a/example/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module github.com/bytecodealliance/wasmtime-go/v45/example - -go 1.18 - -require github.com/bytecodealliance/wasmtime-go/v45 v45.0.0 - -replace github.com/bytecodealliance/wasmtime-go/v45 => ../ diff --git a/example/go.sum b/example/go.sum deleted file mode 100644 index e9e74282..00000000 --- a/example/go.sum +++ /dev/null @@ -1,8 +0,0 @@ -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= -github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/example/main.go b/example/main.go deleted file mode 100644 index bafdc760..00000000 --- a/example/main.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/bytecodealliance/wasmtime-go/v45" -) - -func main() { - store := wasmtime.NewStore(wasmtime.NewEngine()) - - wasm, err := wasmtime.Wat2Wasm(` - (module - (import "" "hello" (func $hello)) - (func (export "run") - (call $hello)) - ) - `) - check(err) - - module, err := wasmtime.NewModule(store.Engine, wasm) - check(err) - - item := wasmtime.WrapFunc(store, func() { - fmt.Println("Hello from Go!") - }) - - instance, err := wasmtime.NewInstance(store, module, []wasmtime.AsExtern{item}) - check(err) - - run := instance.GetFunc(store, "run") - if run == nil { - panic("not a function") - } - _, err = run.Call(store) - check(err) -} - -func check(e error) { - if e != nil { - panic(e) - } -} From 51dc0d7f64350de8e727d8505f9b903e4a511bde Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Tue, 16 Jun 2026 15:45:56 +0300 Subject: [PATCH 24/25] ci: simplify cross-compilation verification - Rename build-example to build-cross - Remove example/ directory references - Simplify to just build test files for verification - Only run on ubuntu-latest for amd64, arm64, riscv64 --- .github/workflows/main.yml | 69 ++++++++++---------------------------- 1 file changed, 17 insertions(+), 52 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4eacabb7..5a6bb74e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -77,20 +77,12 @@ jobs: run: ./ci/test-minimal-runtime/test.sh if: runner.os != 'Windows' - build-example: - runs-on: ${{ matrix.os }} + build-cross: + runs-on: ubuntu-latest strategy: fail-fast: false matrix: - os: [ubuntu-latest, macos-latest, windows-2025] goarch: [amd64, arm64, riscv64] - exclude: - - os: macos-latest - goarch: riscv64 - - os: windows-2025 - goarch: arm64 - - os: windows-2025 - goarch: riscv64 steps: - uses: actions/checkout@v4 @@ -101,67 +93,40 @@ jobs: - uses: actions/setup-go@v2 with: - go-version: ${{ matrix.go || '1.24.x' }} + go-version: '1.24.x' - name: Install cross-compiler (arm64) - if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'arm64' + if: matrix.goarch == 'arm64' run: sudo apt-get update && sudo apt-get install -y gcc-aarch64-linux-gnu - name: Install zig (riscv64) - if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' + if: matrix.goarch == 'riscv64' uses: mlugg/setup-zig@v2 with: version: 0.14.0 - - name: Build example (linux amd64) - if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'amd64' - working-directory: example - run: CGO_ENABLED=1 go build -ldflags "-s -w" ./main.go + - name: Build test files for amd64 + if: matrix.goarch == 'amd64' + env: + GOARCH: amd64 + CGO_ENABLED: 1 + run: go build -o /dev/null ./... 2>&1 | head -20 - - name: Build example (linux arm64) - if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'arm64' - working-directory: example + - name: Build test files for arm64 + if: matrix.goarch == 'arm64' env: - GOOS: linux GOARCH: arm64 CGO_ENABLED: 1 CC: aarch64-linux-gnu-gcc - run: go build -ldflags "-s -w" ./main.go + run: go build -o /dev/null ./... 2>&1 | head -20 - - name: Build example (linux riscv64) - if: matrix.os == 'ubuntu-latest' && matrix.goarch == 'riscv64' - working-directory: example + - name: Build test files for riscv64 + if: matrix.goarch == 'riscv64' env: - GOOS: linux GOARCH: riscv64 CGO_ENABLED: 1 CC: zig cc -target riscv64-linux-gnu - run: go build -ldflags "-s -w" ./main.go - - - name: Build example (macOS) - if: matrix.os == 'macos-latest' - working-directory: example - env: - GOARCH: ${{ matrix.goarch }} - CGO_ENABLED: 1 - run: go build -ldflags "-s -w" ./main.go - - - name: Build example (Windows) - if: matrix.os == 'windows-2025' - working-directory: example - env: - CGO_ENABLED: 1 - run: go build -ldflags "-s -w" ./main.go - - - name: Verify binary - working-directory: example - run: file main - if: runner.os != 'Windows' - - - name: Verify binary (Windows) - working-directory: example - run: file main.exe - if: runner.os == 'Windows' + run: go build -o /dev/null ./... 2>&1 | head -20 coverage: runs-on: ubuntu-latest From cd2753b4a1b769c359740359d47408d4891196d4 Mon Sep 17 00:00:00 2001 From: Rodney Osodo Date: Tue, 16 Jun 2026 15:57:09 +0300 Subject: [PATCH 25/25] cgo: simplify riscv64 LDFLAGS - Remove direct static library path and -lgcc/-lgcc_s flags - Use -lwasmtime like other architectures for consistency - No special handling needed since only static library exists in target directory --- ffi.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ffi.go b/ffi.go index b54e20a6..f021b9c0 100644 --- a/ffi.go +++ b/ffi.go @@ -6,7 +6,7 @@ package wasmtime // #cgo windows LDFLAGS:-lwasmtime -luserenv -lole32 -lntdll -lws2_32 -lkernel32 -lbcrypt // #cgo linux,amd64 LDFLAGS:-L${SRCDIR}/build/linux-x86_64 -lwasmtime -lm // #cgo linux,arm64 LDFLAGS:-L${SRCDIR}/build/linux-aarch64 -lwasmtime -lm -// #cgo linux,riscv64 LDFLAGS:-L${SRCDIR}/build/linux-riscv64 ${SRCDIR}/build/linux-riscv64/libwasmtime.a -lm -lgcc -lgcc_s +// #cgo linux,riscv64 LDFLAGS:-L${SRCDIR}/build/linux-riscv64 -lwasmtime -lm // #cgo darwin,amd64 LDFLAGS:-L${SRCDIR}/build/macos-x86_64 -lwasmtime // #cgo darwin,arm64 LDFLAGS:-L${SRCDIR}/build/macos-aarch64 -lwasmtime // #cgo windows,amd64 LDFLAGS:-L${SRCDIR}/build/windows-x86_64