diff --git a/Dockerfile b/Dockerfile index f3ea20f1..15fe9841 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,20 +4,14 @@ WORKDIR /usr/src/sqlpage ARG TARGETARCH ARG BUILDARCH -# Copy build scripts COPY scripts/ /usr/local/bin/ - -# Initialize cargo project RUN cargo init . -# Setup cross-compilation environment RUN /usr/local/bin/setup-cross-compilation.sh "$TARGETARCH" "$BUILDARCH" -# Build dependencies (creates a layer that avoids recompiling dependencies on every build) COPY Cargo.toml Cargo.lock ./ RUN /usr/local/bin/build-dependencies.sh -# Build the project COPY . . RUN /usr/local/bin/build-project.sh diff --git a/scripts/build-dependencies.sh b/scripts/build-dependencies.sh index e732125f..5a55754a 100755 --- a/scripts/build-dependencies.sh +++ b/scripts/build-dependencies.sh @@ -1,14 +1,10 @@ #!/bin/bash set -euo pipefail -# Source the environment variables set by setup-cross-compilation.sh -TARGET="$(cat /tmp/TARGET)" -LINKER="$(cat /tmp/LINKER)" -BINDGEN_EXTRA_CLANG_ARGS="$(cat /tmp/BINDGEN_EXTRA_CLANG_ARGS || true)" +source /tmp/build-env.sh echo "Building dependencies for target: $TARGET" -# Build dependencies only (for Docker layer caching) cargo build \ --target "$TARGET" \ --config "target.$TARGET.linker=\"$LINKER\"" \ diff --git a/scripts/build-project.sh b/scripts/build-project.sh index 6723fc17..ac595b61 100755 --- a/scripts/build-project.sh +++ b/scripts/build-project.sh @@ -1,19 +1,14 @@ #!/bin/bash set -euo pipefail -# Source the environment variables set by setup-cross-compilation.sh -TARGET="$(cat /tmp/TARGET)" -LINKER="$(cat /tmp/LINKER)" +source /tmp/build-env.sh echo "Building project for target: $TARGET" -# Build the project -touch src/main.rs cargo build \ --target "$TARGET" \ --config "target.$TARGET.linker=\"$LINKER\"" \ --features odbc-static \ --profile superoptimized -# Move the binary to the expected location mv "target/$TARGET/superoptimized/sqlpage" sqlpage.bin diff --git a/scripts/setup-cross-compilation.sh b/scripts/setup-cross-compilation.sh index 941c99b9..83531abb 100755 --- a/scripts/setup-cross-compilation.sh +++ b/scripts/setup-cross-compilation.sh @@ -1,48 +1,43 @@ #!/bin/bash -set -euox pipefail +set -euo pipefail TARGETARCH="$1" BUILDARCH="$2" +BINDGEN_EXTRA_CLANG_ARGS="" apt-get update if [ "$TARGETARCH" = "$BUILDARCH" ]; then - # Native build - rustup target list --installed > /tmp/TARGET - echo "gcc" > /tmp/LINKER + TARGET="$(rustup target list --installed | head -n1)" + LINKER="gcc" apt-get install -y gcc libgcc-s1 make - LIBDIR="/lib/$(gcc -print-multiarch)" - elif [ "$TARGETARCH" = "arm64" ]; then - echo "aarch64-unknown-linux-gnu" > /tmp/TARGET - echo "aarch64-linux-gnu-gcc" > /tmp/LINKER - + TARGET="aarch64-unknown-linux-gnu" + LINKER="aarch64-linux-gnu-gcc" apt-get install -y gcc-aarch64-linux-gnu libgcc-s1-arm64-cross make - LIBDIR="/usr/aarch64-linux-gnu/lib" elif [ "$TARGETARCH" = "arm" ]; then - echo "armv7-unknown-linux-gnueabihf" > /tmp/TARGET - echo "arm-linux-gnueabihf-gcc" > /tmp/LINKER - + TARGET="armv7-unknown-linux-gnueabihf" + LINKER="arm-linux-gnueabihf-gcc" apt-get install -y gcc-arm-linux-gnueabihf libgcc-s1-armhf-cross make cmake libclang-dev - cargo install --force --locked bindgen-cli - SYSROOT=$(arm-linux-gnueabihf-gcc -print-sysroot) - echo "--sysroot=$SYSROOT -I$SYSROOT/usr/include -I$SYSROOT/usr/include/arm-linux-gnueabihf" > /tmp/BINDGEN_EXTRA_CLANG_ARGS - + BINDGEN_EXTRA_CLANG_ARGS="--sysroot=$SYSROOT -I$SYSROOT/usr/include -I$SYSROOT/usr/include/arm-linux-gnueabihf" LIBDIR="/usr/arm-linux-gnueabihf/lib" else echo "Unsupported cross compilation target: $TARGETARCH" exit 1 fi -# Copy libgcc_s.so.1 for runtime mkdir -p /tmp/sqlpage-libs - cp "$LIBDIR/libgcc_s.so.1" /tmp/sqlpage-libs/ - -# Add the target -rustup target add "$(cat /tmp/TARGET)" - +rustup target add "$TARGET" + +{ + echo "export TARGET='$TARGET'" + echo "export LINKER='$LINKER'" + if [ -n "$BINDGEN_EXTRA_CLANG_ARGS" ]; then + printf "export BINDGEN_EXTRA_CLANG_ARGS=%q\n" "$BINDGEN_EXTRA_CLANG_ARGS" + fi +} > /tmp/build-env.sh