Skip to content

Commit

Permalink
build: add cross-compile support (#506)
Browse files Browse the repository at this point in the history
* build: add cross-compile support

* fix: switch to musl
  • Loading branch information
bernot-dev authored Dec 5, 2024
1 parent a2da58a commit fa30323
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
8 changes: 7 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[alias]
xtask = "run --package xtask --bin xtask --"
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"
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Rust related build artifacts.
/target
.cargo/
wix/
scripts/
examples/
Expand Down
14 changes: 8 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@

# 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 \
--ingroup 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"]
20 changes: 20 additions & 0 deletions cross-arch-build.sh
Original file line number Diff line number Diff line change
@@ -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" .

0 comments on commit fa30323

Please sign in to comment.