From dc50b7ee82dc896b9f981b8f81819adb0379cb70 Mon Sep 17 00:00:00 2001 From: Olivier Lemasle Date: Thu, 3 Jun 2021 21:12:41 +0200 Subject: [PATCH 1/3] Add build/include/wasmtime Following the update to Wasmtime's new C API in #81, the header files are now not only in build/local, but also in build/local/wasmtime. This commit adds the directory "build/include/wasmtime" as a valid empty go package, like it was done in #45 for the other build directories. It also addapts `ci/local.sh` to: - copy the additional header files in build/include/wasmtime - stop this script from removing the "empty.go" files. --- build/include/wasmtime/empty.go | 1 + ci/local.sh | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 build/include/wasmtime/empty.go diff --git a/build/include/wasmtime/empty.go b/build/include/wasmtime/empty.go new file mode 100644 index 0000000..efc8427 --- /dev/null +++ b/build/include/wasmtime/empty.go @@ -0,0 +1 @@ +package wasmtime diff --git a/ci/local.sh b/ci/local.sh index 70eebf4..764edc7 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -6,19 +6,27 @@ if [ "$wasmtime" = "" ]; then exit 1 fi +# 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"; do + mkdir -p "build/$d" + name=$(basename $d) + echo "package ${name/-/_}" > "build/$d/empty.go" +done -build=$wasmtime/target/release -if [ ! -d $build ]; then - build=$wasmtime/target/debug +build="$wasmtime/target/release" +if [ ! -d "$build" ]; then + build="$wasmtime/target/debug" fi +# Use absolute path for symbolic links +build=$(readlink -f "$build") -if [ ! -f $build/libwasmtime.a ]; then +if [ ! -f "$build/libwasmtime.a" ]; then echo 'Missing libwasmtime.a. Did you `cargo build -p wasmtime-c-api`?' fi -mkdir -p build/{include,linux-x86_64,macos-x86_64} -ln -s $build/libwasmtime.a build/linux-x86_64/libwasmtime.a -ln -s $build/libwasmtime.a build/macos-x86_64/libwasmtime.a -cp $wasmtime/crates/c-api/include/*.h build/include -cp $wasmtime/crates/c-api/wasm-c-api/include/*.h build/include +ln -s "$build/libwasmtime.a" build/linux-x86_64/libwasmtime.a +ln -s "$build/libwasmtime.a" build/macos-x86_64/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 From fc3a9b196b8a2f4db97e5384e8b2de40261c31e4 Mon Sep 17 00:00:00 2001 From: Olivier Lemasle Date: Fri, 4 Jun 2021 00:22:28 +0200 Subject: [PATCH 2/3] Import build packages + add CI test for vendoring - Import build packages, to prevent `go mod vendor` from pruning the build directories; - Update download-wasmtime.py to re-create the "empty.go" files and keep the Go packages in "build" - Add a CI test to check that wasmtime-go can be used by Go projects using dependency vendoring --- .github/workflows/main.yml | 4 ++++ .gitignore | 3 ++- ci/download-wasmtime.py | 7 ++++++- ci/local.sh | 5 +++-- ci/test-vendoring.sh | 42 ++++++++++++++++++++++++++++++++++++++ ffi.go | 15 ++++++++++++-- 6 files changed, 70 insertions(+), 6 deletions(-) create mode 100755 ci/test-vendoring.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9e3bf55..ccdc703 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -65,6 +65,10 @@ jobs: bazelisk build --subcommands=pretty_print --verbose_failures --compiler=mingw-gcc :go_default_library bazelisk test --subcommands=pretty_print --verbose_failures --compiler=mingw-gcc :go_default_test if: matrix.os == 'windows-latest' + - name: Test vendoring on *nix + shell: bash + run: ./ci/test-vendoring.sh + if: matrix.os != 'windows-latest' coverage: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 9564f65..d6f1347 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build -/bazel-* \ No newline at end of file +/bazel-* +test-vendoring-project \ No newline at end of file diff --git a/ci/download-wasmtime.py b/ci/download-wasmtime.py index ca3318e..dfa5e09 100644 --- a/ci/download-wasmtime.py +++ b/ci/download-wasmtime.py @@ -5,7 +5,6 @@ import zipfile import tarfile import io -import sys import os import shutil import glob @@ -52,3 +51,9 @@ os.remove(dylib) for dylib in glob.glob("build/**/*.so"): os.remove(dylib) + +for subdir, dirs, files in os.walk("build"): + dir_name = os.path.basename(os.path.normpath(subdir)) + file_path = os.path.join(subdir, "empty.go") + with open(file_path, "w") as empty_file: + empty_file.write("package %s\n" % dir_name.replace("-", "_")) diff --git a/ci/local.sh b/ci/local.sh index 764edc7..61cdaa8 100755 --- a/ci/local.sh +++ b/ci/local.sh @@ -9,9 +9,10 @@ fi # 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"; do - mkdir -p "build/$d" + path="build/$d" + mkdir -p "$path" name=$(basename $d) - echo "package ${name/-/_}" > "build/$d/empty.go" + echo "package ${name/-/_}" > "$path/empty.go" done build="$wasmtime/target/release" diff --git a/ci/test-vendoring.sh b/ci/test-vendoring.sh new file mode 100755 index 0000000..d2b1f40 --- /dev/null +++ b/ci/test-vendoring.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# This script tests the usage of wasmtime-go in a Go project +# that vendors its dependencies. This is used to check if +# all the header files and binaries are well copied in the +# vendor directory. + +# Create a test directory +rm -rf test-vendoring-project +mkdir test-vendoring-project && cd test-vendoring-project || return 1 + +# Initialize a Go project +go mod init "test-vendoring-project" + +# Add dependency to wasmtime-go +cat << E0F >> go.mod +require "github.com/bytecodealliance/wasmtime-go" v0.0.0 +replace "github.com/bytecodealliance/wasmtime-go" v0.0.0 => "../" +E0F + +# Add a basic test which uses wasmtime-go +cat << "E0F" >> main_test.go +package main + +import ( + "testing" + + "github.com/bytecodealliance/wasmtime-go" +) + +func TestMain(t *testing.T) { + wasmtime.NewStore(wasmtime.NewEngine()) +} +E0F + +# Vendor dependency +go mod vendor + +# Then execute the test +go test . + +echo "Success" \ No newline at end of file diff --git a/ffi.go b/ffi.go index 0aa6f4c..0e36256 100644 --- a/ffi.go +++ b/ffi.go @@ -9,8 +9,19 @@ package wasmtime // #cgo windows,amd64 LDFLAGS:-L${SRCDIR}/build/windows-x86_64 // #include import "C" -import "runtime" -import "unsafe" +import ( + "runtime" + "unsafe" + + // Import these build directories in order to have them + // included in vendored dependencies. + // Cf. https://github.com/golang/go/issues/26366 + _ "github.com/bytecodealliance/wasmtime-go/build/include" + _ "github.com/bytecodealliance/wasmtime-go/build/include/wasmtime" + _ "github.com/bytecodealliance/wasmtime-go/build/linux-x86_64" + _ "github.com/bytecodealliance/wasmtime-go/build/macos-x86_64" + _ "github.com/bytecodealliance/wasmtime-go/build/windows-x86_64" +) // # What's up with `ptr()` methods? // From d604db1ade9bcc139cfd842d8a5d95b8eff8bfee Mon Sep 17 00:00:00 2001 From: Olivier Lemasle Date: Fri, 4 Jun 2021 10:34:39 +0200 Subject: [PATCH 3/3] Fix for Bazel --- ffi.go | 15 ++------------- includebuild.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 includebuild.go diff --git a/ffi.go b/ffi.go index 0e36256..0aa6f4c 100644 --- a/ffi.go +++ b/ffi.go @@ -9,19 +9,8 @@ package wasmtime // #cgo windows,amd64 LDFLAGS:-L${SRCDIR}/build/windows-x86_64 // #include import "C" -import ( - "runtime" - "unsafe" - - // Import these build directories in order to have them - // included in vendored dependencies. - // Cf. https://github.com/golang/go/issues/26366 - _ "github.com/bytecodealliance/wasmtime-go/build/include" - _ "github.com/bytecodealliance/wasmtime-go/build/include/wasmtime" - _ "github.com/bytecodealliance/wasmtime-go/build/linux-x86_64" - _ "github.com/bytecodealliance/wasmtime-go/build/macos-x86_64" - _ "github.com/bytecodealliance/wasmtime-go/build/windows-x86_64" -) +import "runtime" +import "unsafe" // # What's up with `ptr()` methods? // diff --git a/includebuild.go b/includebuild.go new file mode 100644 index 0000000..f8eceb5 --- /dev/null +++ b/includebuild.go @@ -0,0 +1,18 @@ +// +build includebuild + +package wasmtime + +// This file is not built and not included in BUILD.bazel; +// it is only used to prevent "go mod vendor" to prune the +// build directory. + +import ( + // Import these build directories in order to have them + // included in vendored dependencies. + // Cf. https://github.com/golang/go/issues/26366 + _ "github.com/bytecodealliance/wasmtime-go/build/include" + _ "github.com/bytecodealliance/wasmtime-go/build/include/wasmtime" + _ "github.com/bytecodealliance/wasmtime-go/build/linux-x86_64" + _ "github.com/bytecodealliance/wasmtime-go/build/macos-x86_64" + _ "github.com/bytecodealliance/wasmtime-go/build/windows-x86_64" +)