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

build: add cross-compile support #506

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
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
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]
bernot-dev marked this conversation as resolved.
Show resolved Hide resolved
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
bernot-dev marked this conversation as resolved.
Show resolved Hide resolved
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" .
Loading