Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to build without QEMU #420

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
#
# docker buildx build --platform linux/arm64,linux/amd64 .
#
# Adding support for additional architectures requires editing the
# `case "$TARGETPLATFORM" in` in the build stage (and likely quite a
# bit of googling).
#
# 1: https://www.docker.com/blog/how-to-rapidly-build-multi-architecture-images-with-buildx
# 2: https://docs.docker.com/build/install-buildx
# ==============================================================================


# ------------------------------------------------------------------------------
# Cargo Build Stage
#
# Runs on native host architecture
# Cross compiles for target architecture
# ------------------------------------------------------------------------------
FROM rust:alpine3.17 AS cargo-build
ARG FEATURES
FROM --platform=$BUILDPLATFORM rust:alpine3.17 AS cargo-build
RUN apk add --no-cache --update \
clang15-libclang \
cmake \
Expand All @@ -34,17 +40,33 @@ RUN apk add --no-cache --update \
WORKDIR /tmp/helium_gateway
COPY . .

ENV CC=gcc CXX=g++ CFLAGS="-U__sun__" RUSTFLAGS="-C target-feature=-crt-static"
ARG BUILDPLATFORM
ARG TARGETPLATFORM

RUN \
case "$BUILDPLATFORM $TARGETPLATFORM" in \
"linux/amd64 linux/arm64") \
rustup target add aarch64-unknown-linux-musl ; \
echo "aarch64-unknown-linux-musl" > rust_target.txt ; \
echo "--target=aarch64-unknown-linux-musl" > cargo_flags.txt ; \
;; \
"linux/amd64 linux/amd64") \
echo > rust_target.txt ; \
echo "--no-default-features --features=tpm" > cargo_flags.txt ; \
;; \
*) \
exit 1 \
;; \
esac

# TMP build fail when cross compiling, so we need to use QEMU when
# building for not-host architectures. But QUEMU builds fail in CI due
# to OOMing on cargo registry updating. Therefore, we will need to
# compile with nightly until cargo's sparse registry stabilizes.
RUN rustup toolchain install nightly
ENV CARGO_UNSTABLE_SPARSE_REGISTRY=true
ENV CC_aarch64_unknown_linux_musl=clang
ENV AR_aarch64_unknown_linux_musl=llvm-ar
ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Clink-self-contained=yes -Clinker=rust-lld"

RUN cargo +nightly build --release --features=tpm
RUN mv target/release/helium_gateway .
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUSTFLAGS="-Ctarget-feature=-crt-static"

RUN cargo build --release $(cat cargo_flags.txt)
RUN mv target/$(cat rust_target.txt)/release/helium_gateway .


# ------------------------------------------------------------------------------
Expand All @@ -56,13 +78,19 @@ RUN mv target/release/helium_gateway .
FROM alpine:3.17.3
ENV RUST_BACKTRACE=1
ENV GW_LISTEN="0.0.0.0:1680"
RUN apk add --no-cache --update \
ARG TARGETPLATFORM

# We will never enable TPM on anything other than x86
RUN \
if [ "$TARGETPLATFORM" = "linux/amd64" ]; \
then apk add --no-cache --update \
libstdc++ \
tpm2-tss-esys \
tpm2-tss-fapi \
tpm2-tss-mu \
tpm2-tss-rc \
tpm2-tss-tcti-device
tpm2-tss-tcti-device ; \
fi

COPY --from=cargo-build /tmp/helium_gateway/helium_gateway /usr/local/bin/helium_gateway
RUN mkdir /etc/helium_gateway
Expand Down