Skip to content

Commit

Permalink
feat: linux/darwin arm64 cross-compilation support for x86_64 doc…
Browse files Browse the repository at this point in the history
…ker image (#14)

* refactor: improve dockerfile layers cache

* feat: linux arm64 support (for both images)

- linux gcc: gcc-aarch64-linux-gnu
- rust toolchain: aarch64-unknown-linux-gnu

* feat: linux arm64 musl (both images)

- rust toolchain: aarch64-unknown-linux-musl

* chore: reoder rust toolchains

* refactor: cross-compiling tests

* fix: tests for aarch64-unknown-linux-gnu
  • Loading branch information
joseluisq authored Oct 4, 2022
1 parent 9ea2f74 commit 2b82c30
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 51 deletions.
52 changes: 40 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,57 @@ buildx:

test:
@docker run --rm -it \
-v $(PWD):/drone/src \
-w /drone/src \
-v $(PWD):/root/src \
-w /root/src \
$(REPOSITORY)/rust-linux-darwin-builder:$(TAG) \
make test-ci
bash -c 'set -eu; make test-ci'
.PHONY: test

test-ci:
@echo "Testing cross-compiling application..."
@rustc -vV
@echo
@cd tests/hello-world \
&& echo "Compiling application (linux-musl $$(uname -m))..." \
&& cargo build --release --target "$$(uname -m)-unknown-linux-musl" \
&& du -sh target/$$(uname -m)-unknown-linux-musl/release/helloworld \
&& ./target/$$(uname -m)-unknown-linux-musl/release/helloworld \
&& echo \
&& echo "Compiling application (apple-darwin x86_64)..." \
\
&& if [ "$$(uname -m)" = "x86_64" ]; then \
echo "Compiling application (linux-gnu x86_64)..."; \
cargo build --release --target x86_64-unknown-linux-gnu; \
du -sh target/x86_64-unknown-linux-gnu/release/helloworld; \
target/x86_64-unknown-linux-gnu/release/helloworld; \
echo; \
\
echo "Compiling application (linux-musl x86_64)..."; \
cargo build --release --target x86_64-unknown-linux-musl; \
du -sh target/x86_64-unknown-linux-musl/release/helloworld; \
target/x86_64-unknown-linux-musl/release/helloworld; \
echo; \
fi \
\
&& echo "Cross-compiling application (apple-darwin x86_64)..." \
&& cargo build --release --target x86_64-apple-darwin \
&& du -sh target/x86_64-apple-darwin/release/helloworld \
&& echo \
&& echo "Compiling application (apple-darwin aarch64)..." \
&& cargo build --release --target aarch64-apple-darwin \
&& du -sh target/aarch64-apple-darwin/release/helloworld
\
\
&& echo "Cross-compiling application (linux-gnu aarch64)..." \
&& cargo build --release --target aarch64-unknown-linux-gnu \
&& du -sh target/aarch64-unknown-linux-gnu/release/helloworld \
&& if [ "$$(uname -m)" = "aarch64" ]; then \
target/aarch64-unknown-linux-gnu/release/helloworld; \
fi \
&& echo \
\
&& echo "Cross-compiling application (linux-musl aarch64)..." \
&& cargo build --release --target aarch64-unknown-linux-musl \
&& du -sh target/aarch64-unknown-linux-musl/release/helloworld \
&& if [ "$$(uname -m)" = "aarch64" ]; then \
target/aarch64-unknown-linux-musl/release/helloworld; \
fi \
&& echo \
\
&& echo "Cross-compiling application (apple-darwin aarch64)..." \
&& cargo build --release --target aarch64-apple-darwin \
&& du -sh target/aarch64-apple-darwin/release/helloworld

.ONESHELL: test-ci

Expand Down
83 changes: 44 additions & 39 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,6 @@ LABEL version="${VERSION}" \
description="Use same Docker image for compiling Rust programs for Linux (musl libc) & macOS (osxcross)." \
maintainer="Jose Quintana <joseluisq.net>"

# Rust stable toolchain
ARG TOOLCHAIN=stable

# Dependencies

# OpenSSL 1.1.1 - https://www.openssl.org/source/old/1.1.1/
ARG OPENSSL_VERSION=1.1.1p

# zlib - http://zlib.net/
ARG ZLIB_VERSION=1.2.12

# libpq - https://ftp.postgresql.org/pub/source/
ARG POSTGRESQL_VERSION=14.5

# Mac OS X SDK version - https://github.com/joseluisq/macosx-sdks
ARG OSX_SDK_VERSION=12.3
ARG OSX_SDK_SUM=3abd261ceb483c44295a6623fdffe5d44fc4ac2c872526576ec5ab5ad0f6e26c
ARG OSX_VERSION_MIN=10.14

# OS X Cross - https://github.com/tpoechtrager/osxcross
ARG OSX_CROSS_COMMIT=50e86ebca7d14372febd0af8cd098705049161b9

# Make sure we have basic dev tools for building C libraries. Our goal
# here is to support the musl-libc builds and Cargo builds needed for a
# large selection of the most popular crates.
Expand All @@ -45,7 +23,9 @@ RUN set -eux \
cmake \
curl \
file \
gcc-aarch64-linux-gnu \
gcc-arm-linux-gnueabihf \
g++-aarch64-linux-gnu \
git \
libbz2-dev \
libgmp-dev \
Expand Down Expand Up @@ -85,29 +65,19 @@ RUN set -eux \
# musl-gcc toolchain and for our Rust toolchain.
ENV PATH=/root/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# Install our Rust toolchain and the `musl` target. We patch the
# command-line we pass to the installer so that it won't attempt to
# interact with the user or fool around with TTYs. We also set the default
# `--target` to musl so that our users don't need to keep overriding it manually.
RUN set -eux \
&& curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $TOOLCHAIN \
&& rustup target add $(uname -m)-unknown-linux-musl \
&& rustup target add armv7-unknown-linux-musleabihf \
&& rustup target add x86_64-apple-darwin \
&& rustup target add aarch64-apple-darwin \
&& true
ADD docker/cargo-config.toml /root/.cargo/config

# Set up a `git credentials` helper for using GH_USER and GH_TOKEN to access
# private repositories if desired.
ADD docker/git-credential-ghtoken /usr/local/bin
COPY docker/git-credential-ghtoken /usr/local/bin
RUN set -eux \
&& git config --global credential.https://git.luolix.top.helper ghtoken \
&& true

# Build a static library version of OpenSSL using musl-libc. This is needed by
# the popular Rust `hyper` crate.
#

# OpenSSL 1.1.1 - https://www.openssl.org/source/old/1.1.1/
ARG OPENSSL_VERSION=1.1.1p

# We point /usr/local/musl/include/linux at some Linux kernel headers (not
# necessarily the right ones) in an effort to compile OpenSSL 1.1's "engine"
# component. It's possible that this will cause bizarre and terrible things to
Expand Down Expand Up @@ -139,6 +109,10 @@ RUN set -eux \
&& rm -r /tmp/* \
&& true


# zlib - http://zlib.net/
ARG ZLIB_VERSION=1.2.12

RUN set -eux \
&& echo "Building zlib ${ZLIB_VERSION}..." \
&& cd /tmp \
Expand All @@ -151,6 +125,10 @@ RUN set -eux \
&& rm -r /tmp/* \
&& true


# libpq - https://ftp.postgresql.org/pub/source/
ARG POSTGRESQL_VERSION=14.5

RUN set -eux \
&& echo "Building libpq ${POSTGRESQL_VERSION}..." \
&& cd /tmp \
Expand Down Expand Up @@ -185,9 +163,16 @@ ENV X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR=/usr/local/musl/ \
# everybody needing to build them manually.)


# Mac OS X SDK version - https://github.com/joseluisq/macosx-sdks
ARG OSX_SDK_VERSION=12.3
ARG OSX_SDK_SUM=3abd261ceb483c44295a6623fdffe5d44fc4ac2c872526576ec5ab5ad0f6e26c
ARG OSX_VERSION_MIN=10.14

# OS X Cross - https://github.com/tpoechtrager/osxcross
ARG OSX_CROSS_COMMIT=50e86ebca7d14372febd0af8cd098705049161b9

# Install OS X Cross
# A Mac OS X cross toolchain for Linux, FreeBSD, OpenBSD and Android

RUN set -eux \
&& echo "Cloning osxcross..." \
&& git clone https://github.com/tpoechtrager/osxcross.git /usr/local/osxcross \
Expand Down Expand Up @@ -221,8 +206,28 @@ RUN set -eux \
&& echo "compiler-rt installed and working successfully!" \
&& true


# Rust stable toolchain
ARG TOOLCHAIN=stable

# Install our Rust toolchain and the `musl` target. We patch the
# command-line we pass to the installer so that it won't attempt to
# interact with the user or fool around with TTYs. We also set the default
# `--target` to musl so that our users don't need to keep overriding it manually.
RUN set -eux \
&& curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $TOOLCHAIN \
&& rustup target add \
aarch64-apple-darwin \
aarch64-unknown-linux-gnu \
aarch64-unknown-linux-musl \
armv7-unknown-linux-musleabihf \
x86_64-apple-darwin \
x86_64-unknown-linux-musl \
&& true
COPY docker/cargo-config.toml /root/.cargo/config

RUN set -eux \
&& echo "Removing osxcross temp files..." \
&& echo "Removing temp files..." \
&& rm -rf *~ taballs *.tar.xz \
&& rm -rf /tmp/* \
&& true
Expand Down

0 comments on commit 2b82c30

Please sign in to comment.