# Dockerfile for building delta-rs-golang shared library
FROM rust:1.85.1-slim AS rust-builder

# Install necessary system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    pkg-config \
    libssl-dev \
    clang \
    gcc-x86-64-linux-gnu \
    && rm -rf /var/lib/apt/lists/*

# Add x86_64-unknown-linux-gnu target
RUN rustup target add x86_64-unknown-linux-gnu

# Set target architecture to x86_64-unknown-linux-gnu
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc

# Configure Cargo for reduced memory usage during build
ENV CARGO_BUILD_JOBS=1

# Set working directory
WORKDIR /app

# Copy Cargo.toml and Cargo.lock first for better caching
COPY delta-rs-golang/Cargo.toml delta-rs-golang/Cargo.lock ./

# Copy source code
COPY delta-rs-golang/src ./src
COPY delta-rs-golang/include ./include

# Build the shared library for x86_64-unknown-linux-gnu with memory-conservative settings
# Only build dynamic library (cdylib), skip static library to save time and space
RUN cargo build --release --target x86_64-unknown-linux-gnu

# Create final stage to extract the shared library
FROM alpine:3.18 AS extractor
RUN apk add --no-cache file
COPY --from=rust-builder /app/target/x86_64-unknown-linux-gnu/release/libdeltalake_golang.so /tmp/
RUN file /tmp/libdeltalake_golang.so
CMD ["cp", "/tmp/libdeltalake_golang.so", "/output/"]