Skip to content

Commit

Permalink
use cross-rs (#332)
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Prendes <[email protected]>
  • Loading branch information
jprendes authored Oct 3, 2023
1 parent 89040e0 commit ec65b3f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ rustflags = [

[target.'cfg(unix)']
runner = 'scripts/test-runner.sh'

[profile.release]
strip = "symbols"
14 changes: 14 additions & 0 deletions Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[build]
default-target = "x86_64-unknown-linux-musl"

[target.aarch64-unknown-linux-musl]
dockerfile = "cross/Dockerfile.musl"

[target.x86_64-unknown-linux-musl]
dockerfile = "cross/Dockerfile.musl"

[target.aarch64-unknown-linux-gnu]
dockerfile = "cross/Dockerfile.gnu"

[target.x86_64-unknown-linux-gnu]
dockerfile = "cross/Dockerfile.gnu"
57 changes: 32 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
PREFIX ?= /usr/local
INSTALL ?= install
CARGO ?= cargo
LN ?= ln -sf
TEST_IMG_NAME ?= wasmtest:latest
RUNTIMES ?= wasmedge wasmtime wasmer
export CONTAINERD_NAMESPACE ?= default
CONTAINERD_NAMESPACE ?= default

TARGET ?= debug
TARGET ?=
OPT_PROFILE ?= debug
RELEASE_FLAG :=
ifeq ($(TARGET),release)
ifeq ($(OPT_PROFILE),release)
RELEASE_FLAG = --release
endif
ifneq ($(TARGET),)
TARGET_FLAG = --target=$(TARGET)
endif

FEATURES_wasmedge =
WARNINGS = -D warnings
Expand All @@ -24,92 +29,94 @@ DOCKER_BUILD ?= docker buildx build

KIND_CLUSTER_NAME ?= containerd-wasm

export

.PHONY: build build-common build-wasm build-%
build: build-wasm $(RUNTIMES:%=build-%);

build-common: build-wasm;
build-wasm:
cargo build -p containerd-shim-wasm --no-default-features --features generate_bindings $(RELEASE_FLAG)
cargo build -p containerd-shim-wasm $(FEATURES_wasm) $(RELEASE_FLAG)
$(CARGO) build $(TARGET_FLAG) -p containerd-shim-wasm --no-default-features --features generate_bindings $(RELEASE_FLAG)
$(CARGO) build $(TARGET_FLAG) -p containerd-shim-wasm $(FEATURES_wasm) $(RELEASE_FLAG)

build-%:
cargo build -p containerd-shim-$* $(FEATURES_$*) $(RELEASE_FLAG)
$(CARGO) build $(TARGET_FLAG) -p containerd-shim-$* $(FEATURES_$*) $(RELEASE_FLAG)

.PHONY: check check-common check-wasm check-%
check: check-wasm $(RUNTIMES:%=check-%);

check-common: check-wasm;
check-wasm:
cargo +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- --check
cargo clippy $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)

check-%:
cargo +nightly fmt -p containerd-shim-$* -- --check
cargo clippy $(FEATURES_$*) -p containerd-shim-$* -- $(WARNINGS)
$(CARGO) +nightly fmt $(TARGET_FLAG) -p containerd-shim-$* -- --check
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_$*) -p containerd-shim-$* -- $(WARNINGS)

.PHONY: fix fix-common fix-wasm fix-%
fix: fix-wasm $(RUNTIMES:%=fix-%);

fix-common: fix-wasm;
fix-wasm:
cargo +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test
cargo clippy $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)
$(CARGO) +nightly fmt $(TARGET_FLAG) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)

fix-%:
cargo +nightly fmt -p containerd-shim-$*
cargo clippy $(FEATURES_$*) --fix -p containerd-shim-$* -- $(WARNINGS)
$(CARGO) +nightly fmt $(TARGET_FLAG) -p containerd-shim-$*
$(CARGO) clippy $(TARGET_FLAG) $(FEATURES_$*) --fix -p containerd-shim-$* -- $(WARNINGS)

.PHONY: test test-common test-wasm test-wasmedge test-%
test: test-wasm $(RUNTIMES:%=test-%);

test-common: test-wasm;
test-wasm:
# oci-tar-builder and wasi-demo-app have no tests
RUST_LOG=trace cargo test --package containerd-shim-wasm $(FEATURES_wasm) --verbose -- --nocapture
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasm $(FEATURES_wasm) --verbose -- --nocapture

test-wasmedge:
# run tests in one thread to prevent paralellism
RUST_LOG=trace cargo test --package containerd-shim-wasmedge $(FEATURES_wasmedge) --lib --verbose -- --nocapture --test-threads=1
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge $(FEATURES_wasmedge) --lib --verbose -- --nocapture --test-threads=1
ifneq ($(OS), Windows_NT)
# run wasmedge test without the default `static` feature
RUST_LOG=trace cargo test --package containerd-shim-wasmedge --no-default-features --features standalone --lib --verbose -- --nocapture --test-threads=1
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-wasmedge --no-default-features --features standalone --lib --verbose -- --nocapture --test-threads=1
endif

test-%:
# run tests in one thread to prevent paralellism
RUST_LOG=trace cargo test --package containerd-shim-$* $(FEATURES_$*) --lib --verbose -- --nocapture --test-threads=1
RUST_LOG=trace $(CARGO) test $(TARGET_FLAG) --package containerd-shim-$* $(FEATURES_$*) --lib --verbose -- --nocapture --test-threads=1

.PHONY: install install-%
install: $(RUNTIMES:%=install-%);

install-%: build-%
mkdir -p $(PREFIX)/bin
$(INSTALL) target/$(TARGET)/containerd-shim-$*-v1 $(PREFIX)/bin/
$(INSTALL) target/$(OPT_PROFILE)/containerd-shim-$*-v1 $(PREFIX)/bin/
$(LN) ./containerd-shim-$*-v1 $(PREFIX)/bin/containerd-shim-$*d-v1
$(LN) ./containerd-shim-$*-v1 $(PREFIX)/bin/containerd-$*d

.PHONY: dist dist-%
dist: $(RUNTIMES:%=dist-%);

dist-%:
[ -f $(PWD)/dist/bin/containerd-shim-$*-v1 ] || $(MAKE) install-$* PREFIX="$(PWD)/dist" TARGET="$(TARGET)"
[ -f $(PWD)/dist/bin/containerd-shim-$*-v1 ] || $(MAKE) install-$* PREFIX="$(PWD)/dist" OPT_PROFILE="$(OPT_PROFILE)"

.PHONY: test-image
test-image: dist/img.tar

.PHONY: test-image
test-image/clean:
rm -rf target/wasm32-wasi/$(TARGET)/
rm -rf target/wasm32-wasi/$(OPT_PROFILE)/

.PHONY: target/wasm32-wasi/$(TARGET)/wasi-demo-app.wasm
target/wasm32-wasi/$(TARGET)/wasi-demo-app.wasm:
.PHONY: target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm
target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm:
rustup target add wasm32-wasi
cd crates/wasi-demo-app && cargo build $(RELEASE_FLAG)

target/wasm32-wasi/$(TARGET)/img.tar: target/wasm32-wasi/$(TARGET)/wasi-demo-app.wasm
target/wasm32-wasi/$(OPT_PROFILE)/img.tar: target/wasm32-wasi/$(OPT_PROFILE)/wasi-demo-app.wasm
cd crates/wasi-demo-app && cargo build $(RELEASE_FLAG) --features oci-v1-tar

dist/img.tar: target/wasm32-wasi/$(TARGET)/img.tar
dist/img.tar: target/wasm32-wasi/$(OPT_PROFILE)/img.tar
@mkdir -p "dist/"
cp "$<" "$@"

Expand Down
8 changes: 8 additions & 0 deletions cross/Dockerfile.gnu
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ARG CROSS_BASE_IMAGE
ARG CROSS_DEB_ARCH
FROM $CROSS_BASE_IMAGE

ARG CROSS_DEB_ARCH
RUN dpkg --add-architecture ${CROSS_DEB_ARCH} && \
apt-get -y update && \
apt-get install -y pkg-config libseccomp-dev:${CROSS_DEB_ARCH}
25 changes: 25 additions & 0 deletions cross/Dockerfile.musl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ARG CROSS_BASE_IMAGE
FROM $CROSS_BASE_IMAGE

COPY --from=jorgeprendes420/apk-anywhere / /
ENV MARCH=${CROSS_CMAKE_SYSTEM_PROCESSOR}
RUN apk-init ${MARCH} ${CROSS_SYSROOT}

# configure libsecccomp-rs to use static linking
RUN apk-${MARCH} add libseccomp-static libseccomp-dev
ENV LIBSECCOMP_LINK_TYPE="static"
ENV LIBSECCOMP_LIB_PATH="${CROSS_SYSROOT}/lib"

# configure wasmedge to link stdc++ statically
RUN apk-${MARCH} add g++
ENV WASMEDGE_DEP_STDCXX_LINK_TYPE="static"
ENV WASMEDGE_DEP_STDCXX_LIB_PATH="${CROSS_SYSROOT}/lib"

# wasmedge (through llvm) needs some symbols defined in libgcc
RUN mkdir /.cargo && cat <<'EOF' > /.cargo/config.toml
[target.'cfg(target_env = "musl")']
rustflags = ["-Clink-arg=-lgcc"]
EOF

RUN apt-get -y update && \
apt-get install -y pkg-config

0 comments on commit ec65b3f

Please sign in to comment.