Skip to content

Commit 027ad06

Browse files
authored
Merge pull request #92 from autonomys/refactor
docker cross compile
2 parents a995ce1 + 3e18447 commit 027ad06

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

.github/workflows/docker.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ jobs:
2727
packages: write
2828

2929
steps:
30+
- name: Set up QEMU
31+
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0
32+
3033
- name: Set up Docker Buildx
3134
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1
3235
with:
@@ -53,13 +56,15 @@ jobs:
5356
type=ref,event=branch
5457
type=sha,format=long
5558
flavor: |
56-
latest=${{ github.ref_name == 'main' || github.ref_type == 'tag' }}
59+
latest=false
5760
suffix=
5861
5962
- name: Build and push chain-alerter image
6063
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
6164
with:
6265
file: docker/alerter.Dockerfile
66+
# TODO: Add linux/amd64/v4 when runner supports it
67+
platforms: linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/arm64
6368
pull: true
6469
push: true
6570
tags: ${{ steps.meta.outputs.tags }}

docker/alerter.Dockerfile

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
# This Dockerfile supports both native building and cross-compilation to x86-64, aarch64 and riscv64
12
FROM --platform=$BUILDPLATFORM ubuntu:24.04
23

34
ARG PROFILE=production
45
ARG RUSTFLAGS
56
# Incremental compilation here isn't helpful
67
ENV CARGO_INCREMENTAL=0
78

9+
ENV PKG_CONFIG_ALLOW_CROSS=true
10+
ARG BUILDARCH
11+
ARG TARGETARCH
12+
813
WORKDIR /code
914

1015
RUN \
@@ -15,18 +20,68 @@ RUN \
1520
llvm \
1621
clang
1722

23+
RUN \
24+
if [ $BUILDARCH != "arm64" ] && [ $TARGETARCH = "arm64" ]; then \
25+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
26+
g++-aarch64-linux-gnu \
27+
gcc-aarch64-linux-gnu \
28+
libc6-dev-arm64-cross \
29+
; fi
30+
31+
RUN \
32+
if [ $BUILDARCH != "riscv64" ] && [ $TARGETARCH = "riscv64" ]; then \
33+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
34+
g++-riscv64-linux-gnu \
35+
gcc-riscv64-linux-gnu \
36+
libc6-dev-riscv64-cross \
37+
; fi
38+
39+
RUN \
40+
if [ $BUILDARCH != "amd64" ] && [ $TARGETARCH = "amd64" ]; then \
41+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
42+
g++-x86-64-linux-gnu \
43+
gcc-x86-64-linux-gnu \
44+
libc6-dev-amd64-cross \
45+
; fi
46+
1847
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain none
1948

2049
# Up until this line all Rust images in this repo should be the same to share the same layers
2150

2251
COPY . /code
52+
ARG TARGETVARIANT
2353

24-
25-
RUN /root/.cargo/bin/cargo build \
54+
RUN \
55+
if [ $BUILDARCH != "arm64" ] && [ $TARGETARCH = "arm64" ]; then \
56+
export RUSTFLAGS="$RUSTFLAGS -C linker=aarch64-linux-gnu-gcc" \
57+
; fi && \
58+
if [ $BUILDARCH != "riscv64" ] && [ $TARGETARCH = "riscv64" ]; then \
59+
export RUSTFLAGS="$RUSTFLAGS -C linker=riscv64-linux-gnu-gcc" \
60+
; fi && \
61+
if [ $TARGETARCH = "amd64" ] && [ "$RUSTFLAGS" = "" ]; then \
62+
case "$TARGETVARIANT" in \
63+
# x86-64-v2 with AES-NI
64+
"v2") export RUSTFLAGS="-C target-cpu=x86-64-v2" ;; \
65+
# x86-64-v3 with AES-NI
66+
"v3") export RUSTFLAGS="-C target-cpu=x86-64-v3 -C target-feature=+aes" ;; \
67+
# v4 is compiled for Zen 4+
68+
"v4") export RUSTFLAGS="-C target-cpu=znver4" ;; \
69+
# Default build is for Skylake
70+
*) export RUSTFLAGS="-C target-cpu=skylake" ;; \
71+
esac \
72+
; fi && \
73+
if [ $BUILDARCH != "amd64" ] && [ $TARGETARCH = "amd64" ]; then \
74+
export RUSTFLAGS="$RUSTFLAGS -C linker=x86_64-linux-gnu-gcc" \
75+
; fi && \
76+
sed -i 's/deny(unused_crate_dependencies)/allow(unused_crate_dependencies)/' alerter/src/main.rs && \
77+
RUSTC_TARGET_ARCH=$(echo $TARGETARCH | sed "s/amd64/x86_64/g" | sed "s/arm64/aarch64/g" | sed "s/riscv64/riscv64gc/g") && \
78+
/root/.cargo/bin/cargo -Zgitoxide -Zgit build \
2679
--locked \
80+
-Z build-std \
2781
--profile $PROFILE \
28-
--bin alerter && \
29-
mv target/*/alerter /alerter && \
82+
--bin alerter \
83+
--target $RUSTC_TARGET_ARCH-unknown-linux-gnu && \
84+
mv target/*/*/alerter /alerter && \
3085
rm -rf target
3186

3287
FROM ubuntu:24.04

rust-toolchain.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[toolchain]
22
channel = "nightly-2025-05-31"
3-
profile = "minimal"
4-
components = ["clippy", "rustfmt"]
3+
components = ["rust-src", "clippy", "rustfmt"]
4+
profile = "default"

0 commit comments

Comments
 (0)