@@ -34,6 +34,16 @@ ENV RUSTFLAGS="-C link-arg=-fuse-ld=lld"
3434ARG TARGETARCH
3535ENV 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+
3747WORKDIR /app
3848
3949# Copy dependency files first for better layer caching
@@ -68,6 +78,12 @@ RUN make rust
6878# Build the Go application
6979FROM --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+
7187WORKDIR /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
85101ARG 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
89134ENV CGO_ENABLED=1
90135ENV LD_LIBRARY_PATH=/app/candle-binding/target/release
91136ENV GOOS=linux
92137ENV 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
95152RUN 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
99167FROM quay.io/centos/centos:stream9
0 commit comments