Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add static build for macos #115

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: Build docker image
run: cd builders && make docker-image-alpine
run: cd builders && make docker-image-alpine && make docker-image-cross
- name: Build & Test static library
run: make release-build-alpine
- name: Build static library for MacOS
run: make release-build-macos-static
- name: Collect artifacts
run: |
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
- name: Create checksums
working-directory: ./artifacts
run: sha256sum * > checksums.txt && cat checksums.txt
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased](https://github.com/Finschia/wasmvm/compare/v1.1.1-0.11.2...HEAD)

### Features
* feat: Add static build for macos ([#115](https://github.com/Finschia/wasmvm/pull/115))

### Fixes

Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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
Expand All @@ -110,6 +121,7 @@ release-build:
make release-build-linux
make release-build-macos
make release-build-windows
make release-build-macos-static

test-alpine: release-build-alpine
# try running go tests using this lib with muslc
Expand Down
24 changes: 24 additions & 0 deletions builders/guest/build_macos_static.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions internal/api/link_mac.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build darwin && !sys_wasmvm
// +build darwin,!sys_wasmvm
//go:build darwin && !static_wasm && !sys_wasmvm

package api

Expand Down
6 changes: 6 additions & 0 deletions internal/api/link_mac_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build darwin && static_wasm && !sys_wasmvm

package api

// #cgo LDFLAGS: -L${SRCDIR} -lwasmvmstatic_darwin
import "C"
9 changes: 5 additions & 4 deletions libwasmvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -20,9 +20,10 @@ name = "muslc"
path = "src/lib.rs"
crate-type = ["staticlib"]

# # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
# [badges]
# maintenance = { status = "actively-developed" }
[[example]]
name = "wasmvmstatic"
path = "src/examples/wasmvmstatic.rs"
crate-type = ["staticlib"]

[features]
default = []
Expand Down
1 change: 1 addition & 0 deletions libwasmvm/src/examples/wasmvmstatic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use wasmvm::*;