Skip to content

Commit 2b82c30

Browse files
authored
feat: linux/darwin arm64 cross-compilation support for x86_64 docker 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
1 parent 9ea2f74 commit 2b82c30

File tree

2 files changed

+84
-51
lines changed

2 files changed

+84
-51
lines changed

Makefile

+40-12
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,57 @@ buildx:
2626

2727
test:
2828
@docker run --rm -it \
29-
-v $(PWD):/drone/src \
30-
-w /drone/src \
29+
-v $(PWD):/root/src \
30+
-w /root/src \
3131
$(REPOSITORY)/rust-linux-darwin-builder:$(TAG) \
32-
make test-ci
32+
bash -c 'set -eu; make test-ci'
3333
.PHONY: test
3434

3535
test-ci:
3636
@echo "Testing cross-compiling application..."
3737
@rustc -vV
3838
@echo
3939
@cd tests/hello-world \
40-
&& echo "Compiling application (linux-musl $$(uname -m))..." \
41-
&& cargo build --release --target "$$(uname -m)-unknown-linux-musl" \
42-
&& du -sh target/$$(uname -m)-unknown-linux-musl/release/helloworld \
43-
&& ./target/$$(uname -m)-unknown-linux-musl/release/helloworld \
44-
&& echo \
45-
&& echo "Compiling application (apple-darwin x86_64)..." \
40+
\
41+
&& if [ "$$(uname -m)" = "x86_64" ]; then \
42+
echo "Compiling application (linux-gnu x86_64)..."; \
43+
cargo build --release --target x86_64-unknown-linux-gnu; \
44+
du -sh target/x86_64-unknown-linux-gnu/release/helloworld; \
45+
target/x86_64-unknown-linux-gnu/release/helloworld; \
46+
echo; \
47+
\
48+
echo "Compiling application (linux-musl x86_64)..."; \
49+
cargo build --release --target x86_64-unknown-linux-musl; \
50+
du -sh target/x86_64-unknown-linux-musl/release/helloworld; \
51+
target/x86_64-unknown-linux-musl/release/helloworld; \
52+
echo; \
53+
fi \
54+
\
55+
&& echo "Cross-compiling application (apple-darwin x86_64)..." \
4656
&& cargo build --release --target x86_64-apple-darwin \
4757
&& du -sh target/x86_64-apple-darwin/release/helloworld \
4858
&& echo \
49-
&& echo "Compiling application (apple-darwin aarch64)..." \
50-
&& cargo build --release --target aarch64-apple-darwin \
51-
&& du -sh target/aarch64-apple-darwin/release/helloworld
59+
\
60+
\
61+
&& echo "Cross-compiling application (linux-gnu aarch64)..." \
62+
&& cargo build --release --target aarch64-unknown-linux-gnu \
63+
&& du -sh target/aarch64-unknown-linux-gnu/release/helloworld \
64+
&& if [ "$$(uname -m)" = "aarch64" ]; then \
65+
target/aarch64-unknown-linux-gnu/release/helloworld; \
66+
fi \
67+
&& echo \
68+
\
69+
&& echo "Cross-compiling application (linux-musl aarch64)..." \
70+
&& cargo build --release --target aarch64-unknown-linux-musl \
71+
&& du -sh target/aarch64-unknown-linux-musl/release/helloworld \
72+
&& if [ "$$(uname -m)" = "aarch64" ]; then \
73+
target/aarch64-unknown-linux-musl/release/helloworld; \
74+
fi \
75+
&& echo \
76+
\
77+
&& echo "Cross-compiling application (apple-darwin aarch64)..." \
78+
&& cargo build --release --target aarch64-apple-darwin \
79+
&& du -sh target/aarch64-apple-darwin/release/helloworld
5280

5381
.ONESHELL: test-ci
5482

docker/Dockerfile

+44-39
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,6 @@ LABEL version="${VERSION}" \
99
description="Use same Docker image for compiling Rust programs for Linux (musl libc) & macOS (osxcross)." \
1010
maintainer="Jose Quintana <joseluisq.net>"
1111

12-
# Rust stable toolchain
13-
ARG TOOLCHAIN=stable
14-
15-
# Dependencies
16-
17-
# OpenSSL 1.1.1 - https://www.openssl.org/source/old/1.1.1/
18-
ARG OPENSSL_VERSION=1.1.1p
19-
20-
# zlib - http://zlib.net/
21-
ARG ZLIB_VERSION=1.2.12
22-
23-
# libpq - https://ftp.postgresql.org/pub/source/
24-
ARG POSTGRESQL_VERSION=14.5
25-
26-
# Mac OS X SDK version - https://github.com/joseluisq/macosx-sdks
27-
ARG OSX_SDK_VERSION=12.3
28-
ARG OSX_SDK_SUM=3abd261ceb483c44295a6623fdffe5d44fc4ac2c872526576ec5ab5ad0f6e26c
29-
ARG OSX_VERSION_MIN=10.14
30-
31-
# OS X Cross - https://github.com/tpoechtrager/osxcross
32-
ARG OSX_CROSS_COMMIT=50e86ebca7d14372febd0af8cd098705049161b9
33-
3412
# Make sure we have basic dev tools for building C libraries. Our goal
3513
# here is to support the musl-libc builds and Cargo builds needed for a
3614
# large selection of the most popular crates.
@@ -45,7 +23,9 @@ RUN set -eux \
4523
cmake \
4624
curl \
4725
file \
26+
gcc-aarch64-linux-gnu \
4827
gcc-arm-linux-gnueabihf \
28+
g++-aarch64-linux-gnu \
4929
git \
5030
libbz2-dev \
5131
libgmp-dev \
@@ -85,29 +65,19 @@ RUN set -eux \
8565
# musl-gcc toolchain and for our Rust toolchain.
8666
ENV PATH=/root/.cargo/bin:/usr/local/musl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
8767

88-
# Install our Rust toolchain and the `musl` target. We patch the
89-
# command-line we pass to the installer so that it won't attempt to
90-
# interact with the user or fool around with TTYs. We also set the default
91-
# `--target` to musl so that our users don't need to keep overriding it manually.
92-
RUN set -eux \
93-
&& curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $TOOLCHAIN \
94-
&& rustup target add $(uname -m)-unknown-linux-musl \
95-
&& rustup target add armv7-unknown-linux-musleabihf \
96-
&& rustup target add x86_64-apple-darwin \
97-
&& rustup target add aarch64-apple-darwin \
98-
&& true
99-
ADD docker/cargo-config.toml /root/.cargo/config
100-
10168
# Set up a `git credentials` helper for using GH_USER and GH_TOKEN to access
10269
# private repositories if desired.
103-
ADD docker/git-credential-ghtoken /usr/local/bin
70+
COPY docker/git-credential-ghtoken /usr/local/bin
10471
RUN set -eux \
10572
&& git config --global credential.https://github.com.helper ghtoken \
10673
&& true
10774

10875
# Build a static library version of OpenSSL using musl-libc. This is needed by
10976
# the popular Rust `hyper` crate.
110-
#
77+
78+
# OpenSSL 1.1.1 - https://www.openssl.org/source/old/1.1.1/
79+
ARG OPENSSL_VERSION=1.1.1p
80+
11181
# We point /usr/local/musl/include/linux at some Linux kernel headers (not
11282
# necessarily the right ones) in an effort to compile OpenSSL 1.1's "engine"
11383
# component. It's possible that this will cause bizarre and terrible things to
@@ -139,6 +109,10 @@ RUN set -eux \
139109
&& rm -r /tmp/* \
140110
&& true
141111

112+
113+
# zlib - http://zlib.net/
114+
ARG ZLIB_VERSION=1.2.12
115+
142116
RUN set -eux \
143117
&& echo "Building zlib ${ZLIB_VERSION}..." \
144118
&& cd /tmp \
@@ -151,6 +125,10 @@ RUN set -eux \
151125
&& rm -r /tmp/* \
152126
&& true
153127

128+
129+
# libpq - https://ftp.postgresql.org/pub/source/
130+
ARG POSTGRESQL_VERSION=14.5
131+
154132
RUN set -eux \
155133
&& echo "Building libpq ${POSTGRESQL_VERSION}..." \
156134
&& cd /tmp \
@@ -185,9 +163,16 @@ ENV X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR=/usr/local/musl/ \
185163
# everybody needing to build them manually.)
186164

187165

166+
# Mac OS X SDK version - https://github.com/joseluisq/macosx-sdks
167+
ARG OSX_SDK_VERSION=12.3
168+
ARG OSX_SDK_SUM=3abd261ceb483c44295a6623fdffe5d44fc4ac2c872526576ec5ab5ad0f6e26c
169+
ARG OSX_VERSION_MIN=10.14
170+
171+
# OS X Cross - https://github.com/tpoechtrager/osxcross
172+
ARG OSX_CROSS_COMMIT=50e86ebca7d14372febd0af8cd098705049161b9
173+
188174
# Install OS X Cross
189175
# A Mac OS X cross toolchain for Linux, FreeBSD, OpenBSD and Android
190-
191176
RUN set -eux \
192177
&& echo "Cloning osxcross..." \
193178
&& git clone https://github.com/tpoechtrager/osxcross.git /usr/local/osxcross \
@@ -221,8 +206,28 @@ RUN set -eux \
221206
&& echo "compiler-rt installed and working successfully!" \
222207
&& true
223208

209+
210+
# Rust stable toolchain
211+
ARG TOOLCHAIN=stable
212+
213+
# Install our Rust toolchain and the `musl` target. We patch the
214+
# command-line we pass to the installer so that it won't attempt to
215+
# interact with the user or fool around with TTYs. We also set the default
216+
# `--target` to musl so that our users don't need to keep overriding it manually.
217+
RUN set -eux \
218+
&& curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $TOOLCHAIN \
219+
&& rustup target add \
220+
aarch64-apple-darwin \
221+
aarch64-unknown-linux-gnu \
222+
aarch64-unknown-linux-musl \
223+
armv7-unknown-linux-musleabihf \
224+
x86_64-apple-darwin \
225+
x86_64-unknown-linux-musl \
226+
&& true
227+
COPY docker/cargo-config.toml /root/.cargo/config
228+
224229
RUN set -eux \
225-
&& echo "Removing osxcross temp files..." \
230+
&& echo "Removing temp files..." \
226231
&& rm -rf *~ taballs *.tar.xz \
227232
&& rm -rf /tmp/* \
228233
&& true

0 commit comments

Comments
 (0)