From d3e9650c362eb2661ff4ea8b5bfb122592319c9f Mon Sep 17 00:00:00 2001 From: Adam Bernot Date: Thu, 5 Dec 2024 17:54:07 +0000 Subject: [PATCH 1/2] build: add cross-compile support --- .cargo/config.toml | 8 +++++++- .dockerignore | 1 - Dockerfile | 14 ++++++++------ cross-arch-build.sh | 20 ++++++++++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100755 cross-arch-build.sh diff --git a/.cargo/config.toml b/.cargo/config.toml index 3f72aba1..f0559a4a 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,8 @@ [alias] -xtask = "run --package xtask --bin xtask --" \ No newline at end of file +xtask = "run --package xtask --bin xtask --" + +[target.aarch64-unknown-linux-gnu] +linker = "aarch64-linux-gnu-gcc" + +[target.x86_64-unknown-linux-gnu] +linker = "x86_64-linux-gnu-gcc" diff --git a/.dockerignore b/.dockerignore index 87bc36df..a1abde4b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,5 @@ # Rust related build artifacts. /target -.cargo/ wix/ scripts/ examples/ diff --git a/Dockerfile b/Dockerfile index 0da47441..6152ee4e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,25 @@ - # The build image -FROM rust:1.83.0-alpine3.20 AS weaver-build -RUN apk add musl-dev +FROM --platform=$BUILDPLATFORM docker.io/rust:1.83.0 AS weaver-build WORKDIR /build +ARG BUILDPLATFORM +ARG TARGETPLATFORM # list out directories to avoid pulling local cargo `target/` COPY Cargo.toml /build/Cargo.toml COPY Cargo.lock /build/Cargo.lock +COPY .cargo /build/.cargo COPY crates /build/crates COPY data /build/data COPY src /build/src COPY tests /build/tests COPY defaults /build/defaults +COPY cross-arch-build.sh /build/cross-arch-build.sh # Build weaver -RUN cargo build --release +RUN ./cross-arch-build.sh # The runtime image -FROM alpine:3.20.3 +FROM docker.io/alpine:3.20.3 LABEL maintainer="The OpenTelemetry Authors" RUN addgroup weaver \ && adduser \ @@ -25,7 +27,7 @@ RUN addgroup weaver \ --disabled-password \ weaver WORKDIR /home/weaver -COPY --from=weaver-build /build/target/release/weaver /weaver/weaver +COPY --from=weaver-build /build/weaver /weaver/weaver USER weaver RUN mkdir /home/weaver/target ENTRYPOINT ["/weaver/weaver"] diff --git a/cross-arch-build.sh b/cross-arch-build.sh new file mode 100755 index 00000000..7ec6b84f --- /dev/null +++ b/cross-arch-build.sh @@ -0,0 +1,20 @@ +set -exu + +if [ "${TARGETPLATFORM}" = "linux/amd64" ]; then + RUST_TARGET=x86_64-unknown-linux-gnu + if [ "${TARGETPLATFORM}" != "${BUILDPLATFORM}" ]; then + apt-get update && apt-get install -y gcc-x86-64-linux-gnu + fi +elif [ "${TARGETPLATFORM}" = "linux/arm64" ]; then + RUST_TARGET=aarch64-unknown-linux-gnu + if [ "${TARGETPLATFORM}" != "${BUILDPLATFORM}" ]; then + apt-get update && apt-get install -y gcc-aarch64-linux-gnu + fi +else + echo "Unsupported target platform: ${TARGETPLATFORM}" + exit 1 +fi + +rustup target add "${RUST_TARGET}" +cargo build --release --target="${RUST_TARGET}" +cp "target/${RUST_TARGET}/release/weaver" . From 2f8b02b43b22ed6e3190b2c9d96b6735280c9813 Mon Sep 17 00:00:00 2001 From: Adam Bernot Date: Thu, 5 Dec 2024 20:25:22 +0000 Subject: [PATCH 2/2] fix: switch to musl --- .cargo/config.toml | 4 ++-- Dockerfile | 2 +- cross-arch-build.sh | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index f0559a4a..2bdfee63 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,8 +1,8 @@ [alias] xtask = "run --package xtask --bin xtask --" -[target.aarch64-unknown-linux-gnu] +[target.aarch64-unknown-linux-musl] linker = "aarch64-linux-gnu-gcc" -[target.x86_64-unknown-linux-gnu] +[target.x86_64-unknown-linux-musl] linker = "x86_64-linux-gnu-gcc" diff --git a/Dockerfile b/Dockerfile index 6152ee4e..8b6da071 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,7 @@ RUN addgroup weaver \ --disabled-password \ weaver WORKDIR /home/weaver -COPY --from=weaver-build /build/weaver /weaver/weaver +COPY --from=weaver-build --chown=weaver:weaver /build/weaver /weaver/weaver USER weaver RUN mkdir /home/weaver/target ENTRYPOINT ["/weaver/weaver"] diff --git a/cross-arch-build.sh b/cross-arch-build.sh index 7ec6b84f..65b10797 100755 --- a/cross-arch-build.sh +++ b/cross-arch-build.sh @@ -1,12 +1,12 @@ set -exu if [ "${TARGETPLATFORM}" = "linux/amd64" ]; then - RUST_TARGET=x86_64-unknown-linux-gnu + RUST_TARGET=x86_64-unknown-linux-musl if [ "${TARGETPLATFORM}" != "${BUILDPLATFORM}" ]; then apt-get update && apt-get install -y gcc-x86-64-linux-gnu fi elif [ "${TARGETPLATFORM}" = "linux/arm64" ]; then - RUST_TARGET=aarch64-unknown-linux-gnu + RUST_TARGET=aarch64-unknown-linux-musl if [ "${TARGETPLATFORM}" != "${BUILDPLATFORM}" ]; then apt-get update && apt-get install -y gcc-aarch64-linux-gnu fi