diff --git a/.circleci/config.yml b/.circleci/config.yml index 8ab6a580..49b48494 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -338,6 +338,9 @@ jobs: - run: name: Build static library for Alpine command: make release-build-alpine + - run: + name: Build static library for MacOS + command: make release-build-macos-static - run: name: Debug build results command: ls -l ./internal/api @@ -347,6 +350,7 @@ jobs: mkdir artifacts cp ./internal/api/libwasmvm_muslc.a artifacts/libwasmvm_muslc.x86_64.a cp ./internal/api/libwasmvm_muslc.aarch64.a artifacts/libwasmvm_muslc.aarch64.a + cp ./internal/api/libwasmvmstatic_darwin.a artifacts/libwasmvmstatic_darwin.a - run: name: Create checksums working_directory: artifacts diff --git a/Makefile b/Makefile index 9c1c1438..8164fce9 100644 --- a/Makefile +++ b/Makefile @@ -88,6 +88,17 @@ release-build-macos: cp libwasmvm/artifacts/libwasmvm.dylib internal/api make update-bindings +# Creates a release build in a containerized build environment of the static library for macOS (.a) +release-build-macos-static: + rm -rf libwasmvm/target/x86_64-apple-darwin/release + rm -rf libwasmvm/target/aarch64-apple-darwin/release + docker run --rm -u $(USER_ID):$(USER_GROUP) \ + -v $(shell pwd)/libwasmvm:/code \ + -v $(shell pwd)/builders/guest/build_macos_static.sh:/usr/local/bin/build_macos_static.sh \ + $(BUILDERS_PREFIX)-cross build_macos_static.sh + cp libwasmvm/artifacts/libwasmvmstatic_darwin.a internal/api/libwasmvmstatic_darwin.a + make update-bindings + # Creates a release build in a containerized build environment of the shared library for Windows (.dll) release-build-windows: rm -rf libwasmvm/target/release diff --git a/builders/guest/build_macos_static.sh b/builders/guest/build_macos_static.sh new file mode 100755 index 00000000..45086a5d --- /dev/null +++ b/builders/guest/build_macos_static.sh @@ -0,0 +1,24 @@ +#!/bin/bash +set -o errexit -o nounset -o pipefail + +# ref: https://wapl.es/rust/2019/02/17/rust-cross-compile-linux-to-macos.html +export PATH="/opt/osxcross/target/bin:$PATH" +export LIBZ_SYS_STATIC=1 + +# See https://github.com/CosmWasm/wasmvm/issues/222#issuecomment-880616953 for two approaches to +# enable stripping through cargo (if that is desired). + +echo "Starting aarch64-apple-darwin build" +export CC=aarch64-apple-darwin20.4-clang +export CXX=aarch64-apple-darwin20.4-clang++ +cargo build --release --target aarch64-apple-darwin --example wasmvmstatic + +echo "Starting x86_64-apple-darwin build" +export CC=o64-clang +export CXX=o64-clang++ +cargo build --release --target x86_64-apple-darwin --example wasmvmstatic + +# Create a universal library with both archs +lipo -output artifacts/libwasmvmstatic_darwin.a -create \ + target/x86_64-apple-darwin/release/examples/libwasmvmstatic.a \ + target/aarch64-apple-darwin/release/examples/libwasmvmstatic.a diff --git a/internal/api/link_mac.go b/internal/api/link_mac.go index aea58734..e6d841ea 100644 --- a/internal/api/link_mac.go +++ b/internal/api/link_mac.go @@ -1,4 +1,4 @@ -//go:build darwin && !sys_wasmvm +//go:build darwin && !static_wasm && !sys_wasmvm package api diff --git a/internal/api/link_mac_static.go b/internal/api/link_mac_static.go new file mode 100644 index 00000000..d9132e51 --- /dev/null +++ b/internal/api/link_mac_static.go @@ -0,0 +1,6 @@ +//go:build darwin && static_wasm && !sys_wasmvm + +package api + +// #cgo LDFLAGS: -L${SRCDIR} -lwasmvmstatic_darwin +import "C" diff --git a/libwasmvm/Cargo.toml b/libwasmvm/Cargo.toml index b483b161..0b618570 100644 --- a/libwasmvm/Cargo.toml +++ b/libwasmvm/Cargo.toml @@ -11,7 +11,7 @@ readme = "README.md" exclude = [".circleci/*", ".gitignore"] [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "rlib"] # the example is to allow us to compile a muslc static lib with the same codebase as we compile the # normal dynamic libs (best workaround I could find to override crate-type on the command line) @@ -20,6 +20,11 @@ name = "muslc" path = "src/lib.rs" crate-type = ["staticlib"] +[[example]] +name = "wasmvmstatic" +path = "src/examples/wasmvmstatic.rs" +crate-type = ["staticlib"] + [features] default = [] # This feature requires Rust nightly because it depends on the unstable backtrace feature. diff --git a/libwasmvm/src/examples/wasmvmstatic.rs b/libwasmvm/src/examples/wasmvmstatic.rs new file mode 100644 index 00000000..42db554f --- /dev/null +++ b/libwasmvm/src/examples/wasmvmstatic.rs @@ -0,0 +1 @@ +pub use wasmvm::*;