Skip to content

Commit fa30323

Browse files
authored
build: add cross-compile support (#506)
* build: add cross-compile support * fix: switch to musl
1 parent a2da58a commit fa30323

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

.cargo/config.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
[alias]
2-
xtask = "run --package xtask --bin xtask --"
2+
xtask = "run --package xtask --bin xtask --"
3+
4+
[target.aarch64-unknown-linux-musl]
5+
linker = "aarch64-linux-gnu-gcc"
6+
7+
[target.x86_64-unknown-linux-musl]
8+
linker = "x86_64-linux-gnu-gcc"

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Rust related build artifacts.
22
/target
3-
.cargo/
43
wix/
54
scripts/
65
examples/

Dockerfile

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
21
# The build image
3-
FROM rust:1.83.0-alpine3.20 AS weaver-build
4-
RUN apk add musl-dev
2+
FROM --platform=$BUILDPLATFORM docker.io/rust:1.83.0 AS weaver-build
53
WORKDIR /build
4+
ARG BUILDPLATFORM
5+
ARG TARGETPLATFORM
66

77
# list out directories to avoid pulling local cargo `target/`
88
COPY Cargo.toml /build/Cargo.toml
99
COPY Cargo.lock /build/Cargo.lock
10+
COPY .cargo /build/.cargo
1011
COPY crates /build/crates
1112
COPY data /build/data
1213
COPY src /build/src
1314
COPY tests /build/tests
1415
COPY defaults /build/defaults
16+
COPY cross-arch-build.sh /build/cross-arch-build.sh
1517

1618
# Build weaver
17-
RUN cargo build --release
19+
RUN ./cross-arch-build.sh
1820

1921
# The runtime image
20-
FROM alpine:3.20.3
22+
FROM docker.io/alpine:3.20.3
2123
LABEL maintainer="The OpenTelemetry Authors"
2224
RUN addgroup weaver \
2325
&& adduser \
2426
--ingroup weaver \
2527
--disabled-password \
2628
weaver
2729
WORKDIR /home/weaver
28-
COPY --from=weaver-build /build/target/release/weaver /weaver/weaver
30+
COPY --from=weaver-build --chown=weaver:weaver /build/weaver /weaver/weaver
2931
USER weaver
3032
RUN mkdir /home/weaver/target
3133
ENTRYPOINT ["/weaver/weaver"]

cross-arch-build.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
set -exu
2+
3+
if [ "${TARGETPLATFORM}" = "linux/amd64" ]; then
4+
RUST_TARGET=x86_64-unknown-linux-musl
5+
if [ "${TARGETPLATFORM}" != "${BUILDPLATFORM}" ]; then
6+
apt-get update && apt-get install -y gcc-x86-64-linux-gnu
7+
fi
8+
elif [ "${TARGETPLATFORM}" = "linux/arm64" ]; then
9+
RUST_TARGET=aarch64-unknown-linux-musl
10+
if [ "${TARGETPLATFORM}" != "${BUILDPLATFORM}" ]; then
11+
apt-get update && apt-get install -y gcc-aarch64-linux-gnu
12+
fi
13+
else
14+
echo "Unsupported target platform: ${TARGETPLATFORM}"
15+
exit 1
16+
fi
17+
18+
rustup target add "${RUST_TARGET}"
19+
cargo build --release --target="${RUST_TARGET}"
20+
cp "target/${RUST_TARGET}/release/weaver" .

0 commit comments

Comments
 (0)