Skip to content

Commit

Permalink
Merge pull request #20 from bandprotocol/alpine-build-static
Browse files Browse the repository at this point in the history
Support static libs, osxcross aarch64, GitHub action to build libs automatically
  • Loading branch information
taobun authored Jan 23, 2023
2 parents ebb8800 + b5eb80c commit fbfa815
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 36 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build workflow

on:
push:
branches-ignore:
- 'master'
tags-ignore:
- '**'
paths:
- '.github/workflows/build.yaml'
- 'api/binding.h'
- 'api/libgo_owasm.dylib'
- 'api/libgo_owasm.so'
- 'libgo_owasm/**'
- 'build/**'
- 'Makefile'

jobs:
build_shared_libs:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Build docker images
run: |
make docker-image-linux
make docker-image-osx
- name: Build shared libs
run: |
make release-linux
make release-osx
- name: Check if there is any change
id: check-diff
run: |
git add -A
git diff HEAD --quiet || echo "diff=true" >> $GITHUB_OUTPUT
- name: Commit and Push libs
if: steps.check-diff.outputs.diff == 'true'
run: |
git config --global user.name 'Builder'
git config --global user.email '[email protected]'
git commit -am "Built shared libs"
git push
19 changes: 12 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: go-owasm Workflow
on: push
name: CI workflow
on: [push]

jobs:
go-wasm-test:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
steps:
- name: Code checkout
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Install Rust and rust toolchain
uses: actions-rs/toolchain@v1
Expand All @@ -15,7 +15,7 @@ jobs:
override: true

- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: '^1.18.3'

Expand All @@ -26,22 +26,27 @@ jobs:
sudo cp wabt-1.0.29/bin/wat2wasm /usr/local/bin
- name: Check go mod cache
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-owasmer-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-owasmer-
- name: Check cargo cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-rust-owasmer-${{ hashFiles('**/Cargo.lock') }}

- name: Build libgo_owasm
run: |
(cd libgo_owasm && cargo build --release)
cp libgo_owasm/target/release/libgo_owasm.so api/libgo_owasm.so
- name: Run rust tests
uses: actions-rs/cargo@v1
with:
Expand Down
38 changes: 38 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release workflow

on:
push:
tags:
- v[0-9]+.*

jobs:
build_static_libs:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Build docker images
run: |
make docker-image-alpine
- name: Build static libs
run: |
make release-alpine
- name: Collect artifacts
run: |
mkdir artifacts
cp api/libgo_owasm_muslc.x86_64.a artifacts/libgo_owasm_muslc.x86_64.a
- name: Create checksums
working-directory: artifacts
run: sha256sum * > checksums.txt && cat checksums.txt

- name: Release
uses: softprops/action-gh-release@v1
with:
files: |
artifacts/checksums.txt
artifacts/libgo_owasm_muslc.x86_64.a
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

libgo_owasm/target
.DS_Store

# no static libraries (35MB+)
lib*.a
24 changes: 17 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@ DOCKER_TAG := 0.0.1
USER_ID := $(shell id -u)
USER_GROUP = $(shell id -g)

docker-image-alpine:
DOCKER_BUILDKIT=1 docker build .. -t owasm/go-ext-builder:$(DOCKER_TAG)-alpine -f build/Dockerfile.alpine

docker-image-linux:
DOCKER_BUILDKIT=1 docker build .. -t owasm/go-ext-builder:$(DOCKER_TAG)-linux -f build/Dockerfile.linux

docker-image-osx:
DOCKER_BUILDKIT=1 docker build .. -t owasm/go-ext-builder:$(DOCKER_TAG)-osx -f build/Dockerfile.osx

docker-images: docker-image-linux docker-image-osx
docker-images: docker-image-osx docker-image-linux docker-image-alpine

# Creates a release build in a containerized build environment of the static library for Alpine Linux (.a)
release-alpine:
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-alpine

# Creates a release build in a containerized build environment of the shared library for glibc Linux (.so)
release-linux:
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-linux

# Creates a release build in a containerized build environment of the shared library for macOS (.dylib)
release-osx:
rm -rf libgo_owasm/target/release
rm -rf libgo_owasm/target/x86_64-apple-darwin/release
rm -rf libgo_owasm/target/aarch64-apple-darwin/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-osx

# and use them to compile release builds
release:
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-linux
rm -rf libgo_owasm/target/release
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code/go-owasm owasm/go-ext-builder:$(DOCKER_TAG)-osx
releases:
make release-alpine
make release-linux
make release-osx
1 change: 0 additions & 1 deletion api/lib.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package api

// #cgo LDFLAGS: -Wl,-rpath,${SRCDIR} -L${SRCDIR} -lgo_owasm
// #include "bindings.h"
//
// typedef int64_t (*get_span_size_fn)(env_t*);
Expand Down
Binary file modified api/libgo_owasm.dylib
Binary file not shown.
6 changes: 6 additions & 0 deletions api/link_muslc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build linux && muslc

package api

// #cgo LDFLAGS: -Wl,-rpath,${SRCDIR} -L${SRCDIR} -lgo_owasm_muslc -lgmp
import "C"
6 changes: 6 additions & 0 deletions api/link_normal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !linux || !muslc

package api

// #cgo LDFLAGS: -Wl,-rpath,${SRCDIR} -L${SRCDIR} -lgo_owasm
import "C"
44 changes: 44 additions & 0 deletions build/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FROM golang:1.18.8-alpine

ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH

RUN set -eux \
&& apk add --no-cache ca-certificates build-base linux-headers gmp-dev gmp git

RUN wget "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-musl/rustup-init"; \
chmod +x rustup-init; \
./rustup-init -y --no-modify-path --default-toolchain 1.60.0; \
rm rustup-init; \
chmod -R a+w $RUSTUP_HOME $CARGO_HOME;

# prepare go cache dirs
RUN mkdir -p /.cache/go-build
RUN chmod -R 777 /.cache

# pre-fetch many deps
WORKDIR /scratch
COPY go-owasm /scratch/go-owasm
RUN cd go-owasm/libgo_owasm && cargo fetch

# allow non-root user to download more deps later
RUN chmod -R 777 /usr/local/cargo

# cleanup
WORKDIR /code
RUN rm -rf /scratch

# add musl Rust targets
RUN rustup target add x86_64-unknown-linux-musl

# copy build scripts
COPY go-owasm/build/build_alpine.sh /opt
RUN chmod +x /opt/build*

# add config cargo
RUN mkdir /.cargo
RUN chmod +rx /.cargo
COPY go-owasm/build/cargo-config /.cargo/config

CMD ["/opt/build_alpine.sh"]
11 changes: 6 additions & 5 deletions build/Dockerfile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ FROM centos:centos7
RUN yum -y update
RUN yum -y install clang gcc gcc-c++ make wget gmp-devel

# GET FROM https://github.com/rust-lang/docker-rust-nightly
# reference from https://github.com/rust-lang/docker-rust-nightly
ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH


RUN url="https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init"; \
wget "$url"; \
chmod +x rustup-init; \
Expand All @@ -19,21 +18,23 @@ RUN url="https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustu
cargo --version; \
rustc --version;

# PRE-FETCH MANY DEPS
# pre-fetch many deps
WORKDIR /scratch
COPY go-owasm /scratch/go-owasm
RUN cd go-owasm/libgo_owasm && cargo fetch

# allow non-root user to download more deps later
RUN chmod -R 777 /usr/local/cargo

## COPY BUILD SCRIPTS

# cleanup
WORKDIR /code
RUN rm -rf /scratch

# copy build scripts
COPY go-owasm/build/build_linux.sh /opt
RUN chmod +x /opt/build*

# add config cargo
RUN mkdir /.cargo
RUN chmod +rx /.cargo
COPY go-owasm/build/cargo-config /.cargo/config
Expand Down
25 changes: 14 additions & 11 deletions build/Dockerfile.osx
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,47 @@ RUN apt-get update
RUN apt install -y clang gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev
RUN apt install -y build-essential wget cmake

## ADD MACOS SUPPORT
# add macos support
WORKDIR /opt

# Pin to proper nightly and add macOS Rust target
RUN rustup target add x86_64-apple-darwin
# pin to proper nightly and add macOS Rust target
RUN rustup target add x86_64-apple-darwin aarch64-apple-darwin

# Build osxcross
# build osxcross
RUN git clone https://github.com/tpoechtrager/osxcross \
&& cd osxcross \
# Don't change file name when downloading because osxcross auto-detects the version from the name
# don't change file name when downloading because osxcross auto-detects the version from the name
&& wget -nc https://github.com/phracker/MacOSX-SDKs/releases/download/11.3/MacOSX11.3.sdk.tar.xz \
&& mv MacOSX11.3.sdk.tar.xz tarballs/ \
&& UNATTENDED=yes OSX_VERSION_MIN=10.10 ./build.sh \
# Install gmp
&& echo "1" | MACOSX_DEPLOYMENT_TARGET=10.10 ./tools/osxcross-macports install gmp \
# Cleanups before Docker layer is finalized
# install gmp
&& echo "1" | MACOSX_DEPLOYMENT_TARGET=11.3 ./tools/osxcross-macports install gmp \
# cleanups before Docker layer is finalized
&& rm -r tarballs/
RUN chmod +rx /opt/osxcross
RUN chmod +rx /opt/osxcross/target
RUN chmod -R +rx /opt/osxcross/target/bin
RUN /opt/osxcross/target/bin/x86_64-apple-darwin20.4-clang --version
RUN /opt/osxcross/target/bin/aarch64-apple-darwin20.4-clang --version

# PRE-FETCH MANY DEPS
# pre-fetch many deps
WORKDIR /scratch
COPY go-owasm /scratch/go-owasm
RUN cd go-owasm/libgo_owasm && cargo fetch

# allow non-root user to download more deps later
RUN chmod -R 777 /usr/local/cargo
RUN chmod -R 777 /usr/local/rustup

## COPY BUILD SCRIPTS

# cleanup
WORKDIR /code
RUN rm -rf /scratch

# copy build scripts
COPY go-owasm/build/*.sh /opt/
RUN chmod +x /opt/*.sh

# add config cargo
RUN mkdir /.cargo
RUN chmod +rx /.cargo
COPY go-owasm/build/cargo-config /.cargo/config
Expand Down
5 changes: 5 additions & 0 deletions build/build_alpine.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

cd go-owasm/libgo_owasm
cargo build --release --target x86_64-unknown-linux-musl --example muslc
cp target/x86_64-unknown-linux-musl/release/examples/libmuslc.a ./../api/libgo_owasm_muslc.x86_64.a
22 changes: 17 additions & 5 deletions build/build_osx.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
#!/bin/bash

# ref: https://wapl.es/rust/2019/02/17/rust-cross-compile-linux-to-macos.html
# ref from
## https://wapl.es/rust/2019/02/17/rust-cross-compile-linux-to-macos.html
## https://github.com/CosmWasm/wasmvm/blob/v1.1.1/builders/Dockerfile.cross
export OSXCROSS_MP_INC=1
export LIBZ_SYS_STATIC=1
export CC=o64-clang
export CXX=o64-clang++

cd go-owasm/libgo_owasm
rustup target add x86_64-apple-darwin

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

echo "Starting x86_64-apple-darwin build"
export CC=o64-clang
export CXX=o64-clang++
cargo build --release --target x86_64-apple-darwin
cp target/x86_64-apple-darwin/release/deps/libgo_owasm.dylib ./../api

# Create a universal library with both archs
lipo -output ./../api/libgo_owasm.dylib -create \
target/x86_64-apple-darwin/release/deps/libgo_owasm.dylib \
target/aarch64-apple-darwin/release/deps/libgo_owasm.dylib
Loading

0 comments on commit fbfa815

Please sign in to comment.