Skip to content

Commit 2bd7a67

Browse files
committed
feat: enhance cross-compilation support in Dockerfile for ARM64 and AMD64
Signed-off-by: liuhy <[email protected]>
1 parent d8ef7d5 commit 2bd7a67

File tree

1 file changed

+71
-3
lines changed

1 file changed

+71
-3
lines changed

Dockerfile.extproc.cross

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ ENV RUSTFLAGS="-C link-arg=-fuse-ld=lld"
3434
ARG TARGETARCH
3535
ENV RUST_TARGET=${TARGETARCH:-x86_64}-unknown-linux-gnu
3636

37+
# Map Docker TARGETARCH to Rust target
38+
RUN echo "Mapping TARGETARCH=$TARGETARCH to Rust target" && \
39+
if [ "$TARGETARCH" = "arm64" ]; then \
40+
echo "export RUST_TARGET=aarch64-unknown-linux-gnu" >> ~/.bashrc; \
41+
export RUST_TARGET=aarch64-unknown-linux-gnu; \
42+
elif [ "$TARGETARCH" = "amd64" ]; then \
43+
echo "export RUST_TARGET=x86_64-unknown-linux-gnu" >> ~/.bashrc; \
44+
export RUST_TARGET=x86_64-unknown-linux-gnu; \
45+
fi
46+
3747
WORKDIR /app
3848

3949
# Copy dependency files first for better layer caching
@@ -68,6 +78,12 @@ RUN make rust
6878
# Build the Go application
6979
FROM --platform=linux/amd64 golang:1.24 as go-builder
7080

81+
# Install cross-compilation tools for Go
82+
RUN apt-get update && apt-get install -y \
83+
gcc-aarch64-linux-gnu \
84+
g++-aarch64-linux-gnu \
85+
&& rm -rf /var/lib/apt/lists/*
86+
7187
WORKDIR /app
7288

7389
# Copy Go module files first for better layer caching
@@ -83,17 +99,69 @@ COPY src/semantic-router/ src/semantic-router/
8399

84100
# Copy the built Rust library from rust-cross-builder
85101
ARG TARGETARCH
86-
COPY --from=rust-cross-builder /app/candle-binding/target/${TARGETARCH:-x86_64}-unknown-linux-gnu/release/libcandle_semantic_router.so /app/candle-binding/target/release/
102+
103+
# Use a script to copy the correct library
104+
RUN mkdir -p /app/candle-binding/target/release
105+
106+
# Copy all possible library locations and let the script figure it out
107+
COPY --from=rust-cross-builder /app/candle-binding/target/ /tmp/rust-target/
108+
109+
# Copy the appropriate library to the expected location
110+
RUN if [ "$TARGETARCH" = "arm64" ]; then \
111+
if [ -f "/tmp/rust-target/aarch64-unknown-linux-gnu/release/libcandle_semantic_router.so" ]; then \
112+
cp /tmp/rust-target/aarch64-unknown-linux-gnu/release/libcandle_semantic_router.so /app/candle-binding/target/release/; \
113+
echo "Copied ARM64 cross-compiled library"; \
114+
else \
115+
echo "ERROR: ARM64 library not found"; \
116+
ls -la /tmp/rust-target/*/release/ || echo "No target directories found"; \
117+
exit 1; \
118+
fi; \
119+
else \
120+
if [ -f "/tmp/rust-target/x86_64-unknown-linux-gnu/release/libcandle_semantic_router.so" ]; then \
121+
cp /tmp/rust-target/x86_64-unknown-linux-gnu/release/libcandle_semantic_router.so /app/candle-binding/target/release/; \
122+
echo "Copied AMD64 library"; \
123+
elif [ -f "/tmp/rust-target/release/libcandle_semantic_router.so" ]; then \
124+
cp /tmp/rust-target/release/libcandle_semantic_router.so /app/candle-binding/target/release/; \
125+
echo "Copied native library"; \
126+
else \
127+
echo "ERROR: AMD64 library not found"; \
128+
ls -la /tmp/rust-target/*/release/ || echo "No target directories found"; \
129+
exit 1; \
130+
fi; \
131+
fi
87132

88133
# Set environment variables for CGO to find the library
89134
ENV CGO_ENABLED=1
90135
ENV LD_LIBRARY_PATH=/app/candle-binding/target/release
91136
ENV GOOS=linux
92137
ENV GOARCH=${TARGETARCH:-amd64}
93138

94-
# Build the router binary with optimizations
139+
# Set up cross-compilation for Go if needed
140+
RUN if [ "$TARGETARCH" = "arm64" ]; then \
141+
export CC=aarch64-linux-gnu-gcc; \
142+
export CXX=aarch64-linux-gnu-g++; \
143+
export CGO_CFLAGS="-I/app/candle-binding"; \
144+
export CGO_LDFLAGS="-L/app/candle-binding/target/release -lcandle_semantic_router"; \
145+
fi
146+
147+
# Verify the library exists and has the expected symbols
148+
RUN ls -la /app/candle-binding/target/release/libcandle_semantic_router.so && \
149+
file /app/candle-binding/target/release/libcandle_semantic_router.so
150+
151+
# Build the router binary with cross-compilation support
95152
RUN mkdir -p bin && cd src/semantic-router && \
96-
go build -ldflags="-w -s" -o ../../bin/router cmd/main.go
153+
if [ "$TARGETARCH" = "arm64" ]; then \
154+
CC=aarch64-linux-gnu-gcc \
155+
CXX=aarch64-linux-gnu-g++ \
156+
CGO_CFLAGS="-I/app/candle-binding" \
157+
CGO_LDFLAGS="-L/app/candle-binding/target/release -lcandle_semantic_router" \
158+
GOOS=linux GOARCH=arm64 \
159+
go build -ldflags="-w -s" -o ../../bin/router cmd/main.go; \
160+
else \
161+
CGO_CFLAGS="-I/app/candle-binding" \
162+
CGO_LDFLAGS="-L/app/candle-binding/target/release -lcandle_semantic_router" \
163+
go build -ldflags="-w -s" -o ../../bin/router cmd/main.go; \
164+
fi
97165

98166
# Final stage: copy the binary and the shared library
99167
FROM quay.io/centos/centos:stream9

0 commit comments

Comments
 (0)