Skip to content

Commit 703228c

Browse files
authored
Merge pull request #305 from CosmWasm/musl-cross-builds
Add build system for musl Linux static libraries for aarch64
2 parents 56b6c8a + 0da262b commit 703228c

File tree

7 files changed

+52
-19
lines changed

7 files changed

+52
-19
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ jobs:
242242
command: |
243243
mkdir artifacts
244244
cp ./api/libwasmvm_muslc.a artifacts
245+
cp ./api/libwasmvm_muslc.aarch64.a artifacts
245246
- run:
246247
name: Create checksums
247248
working_directory: artifacts

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.PHONY: all build build-rust build-go test
22

33
# Builds the Rust library libwasmvm
4-
BUILDERS_PREFIX := cosmwasm/go-ext-builder:0009
4+
BUILDERS_PREFIX := cosmwasm/go-ext-builder:0010
55
# Contains a full Go dev environment in order to run Go tests on the built library
6-
ALPINE_TESTER := cosmwasm/go-ext-builder:0009-alpine
6+
ALPINE_TESTER := cosmwasm/go-ext-builder:0010-alpine
77

88
USER_ID := $(shell id -u)
99
USER_GROUP = $(shell id -g)
@@ -60,7 +60,8 @@ release-build-alpine:
6060
rm -rf libwasmvm/target/release
6161
# build the muslc *.a file
6262
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd)/libwasmvm:/code $(BUILDERS_PREFIX)-alpine
63-
cp libwasmvm/target/release/examples/libmuslc.a api/libwasmvm_muslc.a
63+
cp libwasmvm/artifacts/libwasmvm_muslc.a api
64+
cp libwasmvm/artifacts/libwasmvm_muslc.aarch64.a api
6465
make update-bindings
6566
# try running go tests using this lib with muslc
6667
docker run --rm -u $(USER_ID):$(USER_GROUP) -v $(shell pwd):/mnt/testrun -w /mnt/testrun $(ALPINE_TESTER) go build -tags muslc ./...

builders/Dockerfile.alpine

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@ ENV RUSTUP_HOME=/usr/local/rustup \
1111
# this comes from standard alpine nightly file
1212
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
1313
# with some changes to support our toolchain, etc
14-
RUN set -eux; \
15-
apk add --no-cache \
16-
ca-certificates \
17-
build-base;
18-
19-
RUN wget "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-musl/rustup-init"
20-
RUN chmod +x rustup-init
21-
RUN ./rustup-init -y --no-modify-path --default-toolchain 1.55.0; rm rustup-init
22-
RUN chmod -R a+w $RUSTUP_HOME $CARGO_HOME
23-
24-
# needed for
25-
# /usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find crti.o: No such file or directory
26-
ENV LIBRARY_PATH=/usr/local/rustup/toolchains/1.55.0-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib:$LIBRARY_PATH
14+
RUN set -eux \
15+
&& apk add --no-cache ca-certificates build-base
16+
17+
RUN wget "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-musl/rustup-init" \
18+
&& chmod +x rustup-init \
19+
&& ./rustup-init -y --no-modify-path --profile minimal --default-toolchain 1.55.0 \
20+
&& rm rustup-init \
21+
&& chmod -R a+w $RUSTUP_HOME $CARGO_HOME
22+
23+
# Install C compiler for cross-compilation. This is required by
24+
# Wasmer in https://github.com/wasmerio/wasmer/blob/2.2.1/lib/vm/build.rs.
25+
# For newer versions this might not be needed anymore since build.rs is removed
26+
# in https://github.com/wasmerio/wasmer/pull/2807.
27+
#
28+
# https://unix.stackexchange.com/questions/620205/aarch64-linux-musl-cross-has-a-broken-link-for-ld-musl-aarch64-so-1
29+
RUN wget https://musl.cc/aarch64-linux-musl-cross.tgz \
30+
&& tar -xf aarch64-linux-musl-cross.tgz \
31+
&& mv ./aarch64-linux-musl-cross /opt \
32+
&& /opt/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc --version \
33+
&& rm aarch64-linux-musl-cross.tgz
2734

2835
# prepare go cache dirs
2936
RUN mkdir -p /.cache/go-build
@@ -35,6 +42,9 @@ RUN chmod -R 777 /usr/local/cargo
3542
## COPY BUILD SCRIPTS
3643
WORKDIR /code
3744

45+
# Add musl Rust targets
46+
RUN rustup target add aarch64-unknown-linux-musl x86_64-unknown-linux-musl
47+
3848
COPY guest/*.sh /opt/
3949
RUN chmod +x /opt/*.sh
4050

builders/Dockerfile.centos7

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ENV RUSTUP_HOME=/usr/local/rustup \
1010

1111
RUN wget "https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init" \
1212
&& chmod +x rustup-init \
13-
&& ./rustup-init -y --no-modify-path --default-toolchain 1.55.0 \
13+
&& ./rustup-init -y --no-modify-path --profile minimal --default-toolchain 1.55.0 \
1414
&& rm rustup-init \
1515
&& chmod -R a+w $RUSTUP_HOME $CARGO_HOME \
1616
&& rustup --version \

builders/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Versioned by a simple counter that is not bound to a specific CosmWasm version
22
# See builders/README.md
3-
BUILDERS_PREFIX := cosmwasm/go-ext-builder:0009
3+
BUILDERS_PREFIX := cosmwasm/go-ext-builder:0010
44

55
.PHONY: docker-image-centos7
66
docker-image-centos7:
@@ -22,3 +22,9 @@ docker-publish: docker-images
2222
docker push $(BUILDERS_PREFIX)-cross
2323
docker push $(BUILDERS_PREFIX)-centos7
2424
docker push $(BUILDERS_PREFIX)-alpine
25+
26+
.PHONY: pull-base-images
27+
pull-base-images:
28+
docker pull golang:1.17.7-alpine
29+
docker pull centos:centos7
30+
docker pull rust:1.55.0-bullseye

builders/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ can do the cross-compilation.
1414

1515
## Changelog
1616

17+
**Version 0010:**
18+
19+
- Add cross-compilation setup to build `libwasmvm_muslc.a` and `libwasmvm_muslc.aarch64.a`
20+
from the alpine builder image.
21+
1722
**Version 0009:**
1823

1924
- Let macOS build dylib files with both aarch64 and x86_64 code.

builders/guest/build_muslc.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
#!/bin/sh
2+
set -e # Note we are not using bash here but the Alpine default shell
23

34
# See https://github.com/CosmWasm/wasmvm/issues/222#issuecomment-880616953 for two approaches to
45
# enable stripping through cargo (if that is desired).
56

6-
cargo build --release --example muslc
7+
echo "Starting aarch64-unknown-linux-musl build"
8+
export CC=/opt/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc
9+
cargo build --release --target aarch64-unknown-linux-musl --example muslc
10+
unset CC
11+
12+
echo "Starting x86_64-unknown-linux-musl build"
13+
cargo build --release --target x86_64-unknown-linux-musl --example muslc
14+
15+
cp target/aarch64-unknown-linux-musl/release/examples/libmuslc.a artifacts/libwasmvm_muslc.aarch64.a
16+
cp target/x86_64-unknown-linux-musl/release/examples/libmuslc.a artifacts/libwasmvm_muslc.a

0 commit comments

Comments
 (0)