Skip to content

Commit

Permalink
feat: add static linking of wasmvm (CosmWasm#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongwoo Lee authored Sep 30, 2021
1 parent f727ca4 commit 3015fc4
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ exclude = [".circleci/*", ".gitignore"]
[lib]
crate-type = ["cdylib"]

# the example is to allow us to compile a muslc static lib with the same codebase as we compile the
# the example is to allow us to compile a 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)
[[example]]
name = "muslc"
name = "staticlib"
path = "src/lib.rs"
crate-type = ["staticlib"]

Expand Down
18 changes: 12 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: all build build-rust build-go test

BUILDERS_PREFIX := cosmwasm/go-ext-builder:0006
BUILDERS_PREFIX := line/wasmvm-builder
USER_ID := $(shell id -u)
USER_GROUP = $(shell id -g)

Expand Down Expand Up @@ -59,25 +59,31 @@ test-safety:
release-build-alpine:
rm -rf target/release
# build the muslc *.a file
docker run --rm -v $(shell pwd):/code $(BUILDERS_PREFIX)-alpine
docker run --rm -v $(shell pwd):/code $(BUILDERS_PREFIX):alpine
# try running go tests using this lib with muslc
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code -w /code $(BUILDERS_PREFIX)-alpine go build -tags muslc .
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code -w /code $(BUILDERS_PREFIX)-alpine go test -tags='muslc mocks' ./api ./types
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code -w /code $(BUILDERS_PREFIX):alpine go build -tags muslc .
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/code -w /code $(BUILDERS_PREFIX):alpine go test -tags='muslc mocks' ./api ./types

# Creates a release build in a containerized build environment of the static library for glibc Linux (.a)
release-build-linux-static:
rm -rf target/release
docker run --rm -v $(shell pwd):/code $(BUILDERS_PREFIX):static

# Creates a release build in a containerized build environment of the shared library for glibc Linux (.so)
release-build-linux:
rm -rf target/release
docker run --rm -v $(shell pwd):/code $(BUILDERS_PREFIX)-centos7
docker run --rm -v $(shell pwd):/code $(BUILDERS_PREFIX):centos7

# Creates a release build in a containerized build environment of the shared library for macOS (.dylib)
release-build-macos:
rm -rf target/release
docker run --rm -v $(shell pwd):/code $(BUILDERS_PREFIX)-cross
docker run --rm -v $(shell pwd):/code $(BUILDERS_PREFIX):cross

release-build:
# Write like this because those must not run in parallal
make release-build-alpine
make release-build-linux
make release-build-linux-static
make release-build-macos

test-alpine: release-build-alpine
Expand Down
Binary file modified api/libwasmvm.dylib
Binary file not shown.
Binary file modified api/libwasmvm.so
Binary file not shown.
3 changes: 2 additions & 1 deletion api/link_muslc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build linux,muslc
//go:build linux && muslc && !static
// +build linux,muslc,!static

package api

Expand Down
7 changes: 7 additions & 0 deletions api/link_static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build linux && !muslc && static
// +build linux,!muslc,static

package api

// #cgo LDFLAGS: -Wl,-rpath,${SRCDIR} -L${SRCDIR} -lwasmvm_static -lm -ldl
import "C"
3 changes: 2 additions & 1 deletion api/link_std.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build linux,!muslc darwin
//go:build (linux && !muslc && !static) || darwin
// +build linux,!muslc,!static darwin

package api

Expand Down
1 change: 1 addition & 0 deletions api/mocks.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mocks
// +build mocks

package api
Expand Down
6 changes: 4 additions & 2 deletions builders/Dockerfile.alpine
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# > docker build -t line/wasmvm-builder:alpine -f Dockerfile.alpine .

# we build with go and rust
FROM golang:1.15-alpine3.12

Expand All @@ -15,7 +17,7 @@ RUN set -eux; \

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

# needed for
Expand All @@ -39,4 +41,4 @@ RUN mkdir /.cargo
RUN chmod +rx /.cargo
COPY guest/cargo-config /.cargo/config

CMD ["/opt/build_muslc.sh"]
CMD ["/opt/build_linux_static.sh", "muslc"]
4 changes: 3 additions & 1 deletion builders/Dockerfile.centos7
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# > docker build -t line/wasmvm-builder:centos7 -f Dockerfile.centos7 .

FROM centos:centos7

RUN yum -y update
Expand All @@ -12,7 +14,7 @@ ENV RUSTUP_HOME=/usr/local/rustup \
RUN url="https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init"; \
wget "$url"; \
chmod +x rustup-init; \
./rustup-init -y --no-modify-path --default-toolchain 1.51.0; \
./rustup-init -y --no-modify-path --default-toolchain 1.53.0; \
rm rustup-init; \
chmod -R a+w $RUSTUP_HOME $CARGO_HOME; \
rustup --version; \
Expand Down
4 changes: 3 additions & 1 deletion builders/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM rust:1.51.0-buster
# > docker build -t line/wasmvm-builder:cross -f Dockerfile.cross .

FROM rust:1.53.0-buster

# Install build dependencies
RUN apt-get update
Expand Down
15 changes: 15 additions & 0 deletions builders/Dockerfile.static
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# > docker build -t line/wasmvm-builder:static -f Dockerfile.static .

FROM rust:1.53

COPY . /code
WORKDIR /code

COPY guest/*.sh /opt/
RUN chmod +x /opt/*.sh

RUN mkdir /.cargo
RUN chmod +rx /.cargo
COPY guest/cargo-config /.cargo/config

ENTRYPOINT ["/opt/build_linux_static.sh"]
6 changes: 6 additions & 0 deletions builders/guest/build_linux_static.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

TARGET_NAME="${1:-static}"

cargo build --release --example staticlib
cp /code/target/release/examples/libstaticlib.a "/code/api/libwasmvm_$TARGET_NAME.a"
4 changes: 0 additions & 4 deletions builders/guest/build_muslc.sh

This file was deleted.

1 change: 1 addition & 0 deletions ibc_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mocks
// +build mocks

package cosmwasm
Expand Down
1 change: 1 addition & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build mocks
// +build mocks

package cosmwasm
Expand Down

0 comments on commit 3015fc4

Please sign in to comment.