From fa303233723add3f797492f2dd0fba4c28f4a427 Mon Sep 17 00:00:00 2001 From: Adam <34751694+bernot-dev@users.noreply.github.com> Date: Thu, 5 Dec 2024 14:13:50 -0700 Subject: [PATCH] build: add cross-compile support (#506) * build: add cross-compile support * fix: switch to musl --- .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..2bdfee63 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-musl] +linker = "aarch64-linux-gnu-gcc" + +[target.x86_64-unknown-linux-musl] +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..8b6da071 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 --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 new file mode 100755 index 00000000..65b10797 --- /dev/null +++ b/cross-arch-build.sh @@ -0,0 +1,20 @@ +set -exu + +if [ "${TARGETPLATFORM}" = "linux/amd64" ]; then + 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-musl + 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" .