Skip to content

Commit fd4c823

Browse files
committed
chore: Build Wasmer on musl
Closes #1482 Closes #1766
1 parent fd3373a commit fd4c823

File tree

5 files changed

+109
-33
lines changed

5 files changed

+109
-33
lines changed

.github/workflows/main.yaml

+50-10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ jobs:
3838
strategy:
3939
fail-fast: false
4040
matrix:
41-
build: [linux-x64, macos-x64, macos-arm64, windows-x64, linux-aarch64]
4241
include:
4342
- build: linux-x64
4443
os: ubuntu-18.04
@@ -47,6 +46,13 @@ jobs:
4746
artifact_name: 'wasmer-linux-amd64'
4847
cross_compilation_artifact_name: 'cross_compiled_from_linux'
4948
run_integration_tests: true
49+
- build: linux-musl-x64
50+
os: ubuntu-18.04
51+
rust: 1.48
52+
target: x86_64-unknown-linux-musl
53+
artifact_name: 'wasmer-linux-musl-amd64'
54+
cross_compilation_artifact_name: 'cross_compiled_from_linux'
55+
run_integration_tests: false
5056
- build: macos-x64
5157
os: macos-latest
5258
rust: 1.48
@@ -77,14 +83,20 @@ jobs:
7783
CARGO_SCCACHE_VERSION: 0.2.14-alpha.0-parity
7884
SCCACHE_AZURE_BLOB_CONTAINER: wasmerstoragesccacheblob
7985
SCCACHE_AZURE_CONNECTION_STRING: ${{ secrets.SCCACHE_AZURE_CONNECTION_STRING }}
86+
TARGET: ${{ matrix.target }}
8087
steps:
8188
- uses: actions/checkout@v2
8289
- name: Set up libstdc++ on Linux
90+
if: matrix.build == 'linux-x64'
8391
run: |
8492
sudo apt-get update -y
8593
sudo apt-get install -y --allow-downgrades libstdc++6=8.4.0-1ubuntu1~18.04
8694
sudo apt-get install --reinstall g++-8
87-
if: matrix.os == 'ubuntu-18.04'
95+
- name: Set up musl on Linux
96+
if: matrix.build == 'linux-musl-x64'
97+
run: |
98+
sudo apt-get update -y
99+
sudo apt-get install -y musl-dev musl-tools
88100
- name: Install Rust ${{ matrix.rust }}
89101
uses: actions-rs/toolchain@v1
90102
with:
@@ -124,7 +136,6 @@ jobs:
124136
~/.cargo/registry
125137
~/.cargo/git
126138
key: ${{ matrix.build }}-${{ matrix.target }}-cargo-${{ hashFiles('Cargo.lock') }}-v1
127-
# Install sccache
128139
- uses: actions/cache@v2
129140
with:
130141
path: ${{ runner.tool_cache }}/cargo-sccache
@@ -171,26 +182,43 @@ jobs:
171182
- name: Test
172183
run: |
173184
make test
174-
if: matrix.target != 'aarch64-apple-darwin'
185+
if: matrix.build != 'macos-arm64' && matrix.build != 'linux-musl-x64'
186+
- name: Test (musl)
187+
if: matrix.build == 'linux-musl-x64'
188+
run: |
189+
LIBC=musl WASMER_CAPI_USE_SYSTEM_LIBFFI=1 make test
175190
- name: Test C API
176191
run: |
177192
make test-capi
178-
if: matrix.os != 'windows-latest' && matrix.target != 'aarch64-apple-darwin' # we can't test yet on Apple Silicon or Windows
193+
if: matrix.os != 'windows-latest' && matrix.target != 'aarch64-apple-darwin' && matrix.build != 'linux-musl-x64' # we can't test yet on Apple Silicon or Windows
194+
- name: Test C API (musl)
195+
run: |
196+
CC=musl-gcc LIBC=musl make test-capi
197+
if: matrix.build == 'linux-musl-x64'
179198
- name: Build C API
180199
run: |
181200
make build-capi
182-
if: matrix.target != 'aarch64-apple-darwin'
201+
if: matrix.target != 'aarch64-apple-darwin' && matrix.build != 'linux-musl-x64'
183202
- name: Build C API (system libffi)
184203
run: |
185204
make build-capi-cranelift-system-libffi
186205
if: matrix.target == 'aarch64-apple-darwin'
206+
- name: Build C API (musl)
207+
run: |
208+
LIBC=musl WASMER_CAPI_USE_SYSTEM_LIBFFI=1 make build-capi
209+
if: matrix.build == 'linux-musl-x64'
187210
- name: Build Wasmer binary
211+
if: matrix.build != 'linux-musl-x64'
188212
run: |
189213
make build-wasmer
214+
- name: Build Wasmer binary (musl)
215+
if: matrix.build == 'linux-musl-x64'
216+
run: |
217+
LIBC=musl WASMER_CAPI_USE_SYSTEM_LIBFFI=1 make build-wasmer
190218
- name: Build Wapm binary
219+
if: needs.setup.outputs.DOING_RELEASE == '1' && matrix.build != 'linux-musl-x64'
191220
run: |
192221
make build-wapm
193-
if: needs.setup.outputs.DOING_RELEASE == '1'
194222
- name: Install Nightly Rust for Headless
195223
uses: actions-rs/toolchain@v1
196224
with:
@@ -200,6 +228,7 @@ jobs:
200228
components: "rust-src"
201229
if: needs.setup.outputs.DOING_RELEASE == '1'
202230
- name: Build Minimal Wasmer Headless
231+
if: needs.setup.outputs.DOING_RELEASE == '1' && matrix.build != 'linux-musl-x64'
203232
run: |
204233
cargo install xargo
205234
echo "" >> Cargo.toml
@@ -214,12 +243,13 @@ jobs:
214243
echo "codegen-units = 1" >> Cargo.toml
215244
echo "rpath = false" >> Cargo.toml
216245
make build-wasmer-headless-minimal
217-
if: needs.setup.outputs.DOING_RELEASE == '1'
218246
- name: Copy target binaries
219247
run: |
220248
mkdir -p target/release
221249
cp target/${{matrix.target}}/release/wasmer* target/release
222-
cp target/${{matrix.target}}/release/libwasmer* target/release
250+
if find target/${{matrix.target}}/release -type f -name 'libwasmer*' -maxdepth 1 | grep -q "."; then
251+
cp target/${{matrix.target}}/release/libwasmer* target/release
252+
fi
223253
if [ -d "wapm-cli" ]; then
224254
mkdir -p wapm-cli/target/release
225255
cp wapm-cli/target/${{matrix.target}}/release/wapm* wapm-cli/target/release
@@ -241,7 +271,7 @@ jobs:
241271
make test-integration
242272
if: matrix.run_integration_tests && matrix.os != 'windows-latest'
243273
- name: Cross compile from Linux
244-
if: matrix.os == 'ubuntu-18.04'
274+
if: matrix.build == 'linux-x64'
245275
shell: bash
246276
run: |
247277
ls target/release
@@ -376,6 +406,16 @@ jobs:
376406
asset_path: artifacts/wasmer-linux-amd64/wasmer.tar.gz
377407
asset_name: wasmer-linux-amd64.tar.gz
378408
asset_content_type: application/gzip
409+
- name: Upload Release Asset Linux amd64 (musl)
410+
id: upload-release-asset-linux-musl-amd64
411+
uses: actions/upload-release-asset@v1
412+
env:
413+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
414+
with:
415+
upload_url: ${{ steps.create_release.outputs.upload_url }}
416+
asset_path: artifacts/wasmer-linux-musl-amd64/wasmer.tar.gz
417+
asset_name: wasmer-linux-musl-amd64.tar.gz
418+
asset_content_type: application/gzip
379419
- name: Upload Release Asset Mac amd64
380420
id: upload-release-asset-mac-amd64
381421
uses: actions/upload-release-asset@v1

Makefile

+38-21
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ ifneq ($(OS), Windows_NT)
33
ARCH := $(shell uname -m)
44
UNAME_S := $(shell uname -s)
55
LIBC ?= $(shell ldd 2>&1 | grep -o musl | head -n1)
6+
TARGET ?= $(CARGO_BUILD_TARGET)
67
else
78
# We can assume, if in windows it will likely be in x86_64
89
ARCH := x86_64
910
UNAME_S :=
1011
LIBC ?=
12+
TARGET ?= $(CARGO_BUILD_TARGET)
1113
endif
1214

1315
# Which compilers we build. These have dependencies that may not be on the system.
@@ -103,11 +105,18 @@ ifneq (, $(LIBC))
103105
$(info C standard library: $(bold)$(green)$(LIBC)$(reset))
104106
endif
105107

108+
ifneq (, $(TARGET))
109+
$(info Target: $(bold)$(green)$(TARGET)$(reset))
110+
endif
111+
106112
$(info Host target: $(bold)$(green)$(HOST_TARGET)$(reset))
107113
$(info Available compilers: $(bold)$(green)${compilers}$(reset))
108114
$(info Compilers features: $(bold)$(green)${compiler_features}$(reset))
109115
$(info Available compilers + engines for test: $(bold)$(green)${test_compilers_engines}$(reset))
110116

117+
ifneq (,$(TARGET))
118+
TARGET := $(TARGET)/
119+
endif
111120

112121
############
113122
# Building #
@@ -335,13 +344,13 @@ package-wapm:
335344
mkdir -p "package/bin"
336345
ifneq ($(OS), Windows_NT)
337346
if [ -d "wapm-cli" ]; then \
338-
cp wapm-cli/target/release/wapm package/bin/ ;\
347+
cp wapm-cli/target/$(TARGET)release/wapm package/bin/ ;\
339348
echo "#!/bin/bash\nwapm execute \"\$$@\"" > package/bin/wax ;\
340349
chmod +x package/bin/wax ;\
341350
fi
342351
else
343352
if [ -d "wapm-cli" ]; then \
344-
cp wapm-cli/target/release/wapm package/bin/ ;\
353+
cp wapm-cli/target/$(TARGET)release/wapm package/bin/ ;\
345354
fi
346355
ifeq ($(UNAME_S), Darwin)
347356
codesign -s - package/bin/wapm
@@ -362,9 +371,9 @@ endif
362371
package-wasmer:
363372
mkdir -p "package/bin"
364373
ifeq ($(OS), Windows_NT)
365-
cp target/release/wasmer.exe package/bin/
374+
cp target/$(TARGET)release/wasmer.exe package/bin/
366375
else
367-
cp target/release/wasmer package/bin/
376+
cp target/$(TARGET)release/wasmer package/bin/
368377
ifeq ($(UNAME_S), Darwin)
369378
codesign -s - package/bin/wasmer
370379
endif
@@ -377,25 +386,33 @@ package-capi:
377386
cp lib/c-api/wasmer_wasm.h* package/include
378387
cp lib/c-api/wasm.h* package/include
379388
cp lib/c-api/README.md package/include/README.md
380-
ifeq ($(OS), Windows_NT)
381-
cp target/release/wasmer_c_api.dll package/lib/wasmer.dll
382-
cp target/release/wasmer_c_api.lib package/lib/wasmer.lib
383-
else
384-
ifeq ($(UNAME_S), Darwin)
389+
390+
# Windows
391+
if [ -f target/$(TARGET)release/wasmer_c_api.dll ]; then \
392+
cp target/$(TARGET)release/wasmer_c_api.dll package/lib/wasmer.dll ;\
393+
fi
394+
if [ -f target/$(TARGET)release/wasmer_c_api.lib ]; then \
395+
cp target/$(TARGET)release/wasmer_c_api.lib package/lib/wasmer.lib ;\
396+
fi
397+
398+
# Darwin
385399
# For some reason in macOS arm64 there are issues if we copy constantly in the install_name_tool util
386400
rm -f package/lib/libwasmer.dylib
387-
cp target/release/libwasmer_c_api.dylib package/lib/libwasmer.dylib
388-
cp target/release/libwasmer_c_api.a package/lib/libwasmer.a
389-
# Fix the rpath for the dylib
390-
install_name_tool -id "@rpath/libwasmer.dylib" package/lib/libwasmer.dylib
391-
else
392-
# In some cases the .so may not be available, for example when building against musl (static linking)
393-
if [ -f target/release/libwasmer_c_api.so ]; then \
394-
cp target/release/libwasmer_c_api.so package/lib/libwasmer.so ;\
395-
fi;
396-
cp target/release/libwasmer_c_api.a package/lib/libwasmer.a
397-
endif
398-
endif
401+
if [ -f target/$(TARGET)release/libwasmer_c_api.dylib ]; then \
402+
cp target/$(TARGET)release/libwasmer_c_api.dylib package/lib/libwasmer.dylib ;\
403+
install_name_tool -id "@rpath/libwasmer.dylib" package/lib/libwasmer.dylib ;\
404+
fi
405+
406+
if [ -f target/$(TARGET)release/libwasmer_c_api.so ]; then \
407+
cp target/$(TARGET)release/libwasmer_c_api.so package/lib/libwasmer.so ;\
408+
fi
409+
if [ -f target/$(TARGET)release/libwasmer_c_api.a ]; then \
410+
cp target/$(TARGET)release/libwasmer_c_api.a package/lib/libwasmer.a ;\
411+
fi
412+
413+
# Inspect
414+
ls -lhaR target/$(TARGET)release
415+
ls -lhaR package
399416

400417
package-docs: build-docs build-docs-capi
401418
mkdir -p "package/docs"

bors.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ status = [
22
"Audit",
33
"Code lint",
44
"Test on linux-x64",
5+
"Test on linux-musl-x64",
56
"Test on linux-aarch64",
67
"Test on macos-x64",
78
"Test on windows-x64",

lib/c-api/build.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
//! * setting `inline-c` up.
55
66
use cbindgen::{Builder, Language};
7-
use std::{env, fs, path::PathBuf};
7+
use std::{
8+
env, fs,
9+
path::{Path, PathBuf},
10+
};
811

912
const PRE_HEADER: &'static str = r#"
1013
// Define the `ARCH_X86_X64` constant.
@@ -549,7 +552,17 @@ fn build_inline_c_env_vars() {
549552
} else if cfg!(target_os = "macos") {
550553
"libwasmer_c_api.dylib".to_string()
551554
} else {
552-
"libwasmer_c_api.so".to_string()
555+
let path = format!(
556+
"{shared_object_dir}/{lib}",
557+
shared_object_dir = shared_object_dir,
558+
lib = "libwasmer_c_api.so"
559+
);
560+
561+
if Path::new(path.as_str()).exists() {
562+
"libwasmer_c_api.so".to_string()
563+
} else {
564+
"libwasmer_c_api.a".to_string()
565+
}
553566
}
554567
);
555568
}

lib/c-api/tests/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# ignore wasm-c-api binaries
22
wasm-c-api-*
33
test-*
4+
wasm-c-api/example/*
45

56
# Unignore files ending with `.c` (i.e. `wasm-c-api-wasi.c`)
67
!*.c
8+
!wasm-c-api/example/*.c
9+
!wasm-c-api/example/*.cc
10+
!wasm-c-api/example/*.wasm
11+
!wasm-c-api/example/*.wat

0 commit comments

Comments
 (0)