Skip to content

Commit b278764

Browse files
committed
Dockerfile: extract scripts to separate files
1 parent 656c73f commit b278764

File tree

5 files changed

+123
-50
lines changed

5 files changed

+123
-50
lines changed

Dockerfile

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,25 @@
11
FROM --platform=$BUILDPLATFORM rust:1.90-slim AS builder
2+
23
WORKDIR /usr/src/sqlpage
34
ARG TARGETARCH
45
ARG BUILDARCH
5-
RUN apt-get update && \
6-
mkdir -p /opt/sqlpage-libs && \
7-
if [ "$TARGETARCH" = "$BUILDARCH" ]; then \
8-
rustup target list --installed > TARGET && \
9-
echo gcc > LINKER && \
10-
apt-get install -y gcc libgcc-s1 cmake pkg-config && \
11-
LIBMULTIARCH=$(gcc -print-multiarch); \
12-
LIBDIR="/lib/$LIBMULTIARCH"; \
13-
USRLIBDIR="/usr/lib/$LIBMULTIARCH"; \
14-
HOST_TRIPLE=$(gcc -dumpmachine); \
15-
elif [ "$TARGETARCH" = "arm64" ]; then \
16-
echo aarch64-unknown-linux-gnu > TARGET && \
17-
echo aarch64-linux-gnu-gcc > LINKER && \
18-
dpkg --add-architecture arm64 && apt-get update && \
19-
apt-get install -y gcc-aarch64-linux-gnu libgcc-s1-arm64-cross pkg-config && \
20-
LIBDIR="/lib/aarch64-linux-gnu"; \
21-
USRLIBDIR="/usr/aarch64-linux-gnu/lib"; \
22-
HOST_TRIPLE="aarch64-linux-gnu"; \
23-
elif [ "$TARGETARCH" = "arm" ]; then \
24-
echo armv7-unknown-linux-gnueabihf > TARGET && \
25-
echo arm-linux-gnueabihf-gcc > LINKER && \
26-
dpkg --add-architecture armhf && apt-get update && \
27-
apt-get install -y gcc-arm-linux-gnueabihf libgcc-s1-armhf-cross cmake libclang1 clang pkg-config && \
28-
cargo install --force --locked bindgen-cli && \
29-
SYSROOT=$(arm-linux-gnueabihf-gcc -print-sysroot); \
30-
echo "--sysroot=$SYSROOT -I$SYSROOT/usr/include -I$SYSROOT/usr/include/arm-linux-gnueabihf" > BINDGEN_EXTRA_CLANG_ARGS; \
31-
LIBDIR="/lib/arm-linux-gnueabihf"; \
32-
USRLIBDIR="/usr/arm-linux-gnueabihf/lib"; \
33-
HOST_TRIPLE="arm-linux-gnueabihf"; \
34-
else \
35-
echo "Unsupported cross compilation target: $TARGETARCH"; \
36-
exit 1; \
37-
fi && \
38-
cp $LIBDIR/libgcc_s.so.1 /opt/sqlpage-libs/ && \
39-
rustup target add $(cat TARGET) && \
40-
cargo init .
6+
7+
# Copy build scripts
8+
COPY scripts/ /usr/local/bin/
9+
10+
# Initialize cargo project
11+
RUN cargo init .
12+
13+
# Setup cross-compilation environment
14+
RUN /usr/local/bin/setup-cross-compilation.sh "$TARGETARCH" "$BUILDARCH"
4115

4216
# Build dependencies (creates a layer that avoids recompiling dependencies on every build)
4317
COPY Cargo.toml Cargo.lock ./
44-
RUN BINDGEN_EXTRA_CLANG_ARGS=$(cat BINDGEN_EXTRA_CLANG_ARGS || true) \
45-
cargo build \
46-
--target $(cat TARGET) \
47-
--config target.$(cat TARGET).linker='"'$(cat LINKER)'"' \
48-
--features odbc-static \
49-
--profile superoptimized
18+
RUN /usr/local/bin/build-dependencies.sh
5019

5120
# Build the project
5221
COPY . .
53-
RUN touch src/main.rs && \
54-
cargo build \
55-
--target $(cat TARGET) \
56-
--config target.$(cat TARGET).linker='"'$(cat LINKER)'"' \
57-
--features odbc-static \
58-
--profile superoptimized && \
59-
mv target/$(cat TARGET)/superoptimized/sqlpage sqlpage.bin
22+
RUN /usr/local/bin/build-project.sh
6023

6124
FROM busybox:glibc
6225
RUN addgroup --gid 1000 --system sqlpage && \
@@ -69,7 +32,7 @@ ENV SQLPAGE_CONFIGURATION_DIRECTORY=/etc/sqlpage
6932
WORKDIR /var/www
7033
COPY --from=builder /usr/src/sqlpage/sqlpage.bin /usr/local/bin/sqlpage
7134
# Provide runtime helper libs in system lib directory for the glibc busybox base
72-
COPY --from=builder /opt/sqlpage-libs/* /lib/
35+
COPY --from=builder /tmp/sqlpage-libs/* /lib/
7336
USER sqlpage
7437
COPY --from=builder --chown=sqlpage:sqlpage /usr/src/sqlpage/sqlpage/sqlpage.db sqlpage/sqlpage.db
7538
EXPOSE 8080

scripts/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Docker Build Scripts
2+
3+
This directory contains scripts used by the Dockerfile to build SQLPage with cross-compilation support.
4+
5+
## Scripts
6+
7+
- **`setup-cross-compilation.sh`**: Sets up the cross-compilation environment based on target and build architectures. Handles system dependencies, cross-compiler installation, and libgcc extraction for runtime.
8+
- **`build-dependencies.sh`**: Builds only the project dependencies for Docker layer caching
9+
- **`build-project.sh`**: Builds the final SQLPage binary
10+
11+
## Usage
12+
13+
These scripts are automatically copied and executed by the Dockerfile during the build process. They handle:
14+
15+
- Cross-compilation setup for different architectures (amd64, arm64, arm)
16+
- System dependencies installation
17+
- Cargo build configuration with appropriate linkers
18+
- Library extraction for runtime
19+
20+
The scripts use temporary files in `/tmp/` to pass configuration between stages and export environment variables for use in subsequent build steps.

scripts/build-dependencies.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Source the environment variables set by setup-cross-compilation.sh
5+
TARGET="$(cat /tmp/TARGET)"
6+
LINKER="$(cat /tmp/LINKER)"
7+
BINDGEN_EXTRA_CLANG_ARGS="$(cat /tmp/BINDGEN_EXTRA_CLANG_ARGS || true)"
8+
9+
echo "Building dependencies for target: $TARGET"
10+
11+
# Build dependencies only (for Docker layer caching)
12+
cargo build \
13+
--target "$TARGET" \
14+
--config "target.$TARGET.linker=\"$LINKER\"" \
15+
--features odbc-static \
16+
--profile superoptimized

scripts/build-project.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Source the environment variables set by setup-cross-compilation.sh
5+
TARGET="$(cat /tmp/TARGET)"
6+
LINKER="$(cat /tmp/LINKER)"
7+
8+
echo "Building project for target: $TARGET"
9+
10+
# Build the project
11+
touch src/main.rs
12+
cargo build \
13+
--target "$TARGET" \
14+
--config "target.$TARGET.linker=\"$LINKER\"" \
15+
--features odbc-static \
16+
--profile superoptimized
17+
18+
# Move the binary to the expected location
19+
mv "target/$TARGET/superoptimized/sqlpage" sqlpage.bin

scripts/setup-cross-compilation.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
TARGETARCH="$1"
5+
BUILDARCH="$2"
6+
7+
if [ "$TARGETARCH" = "$BUILDARCH" ]; then
8+
# Native build
9+
rustup target list --installed > /tmp/TARGET
10+
echo "gcc" > /tmp/LINKER
11+
apt-get install -y gcc libgcc-s1 cmake pkg-config
12+
13+
LIBDIR="/lib/$(gcc -print-multiarch)"
14+
15+
elif [ "$TARGETARCH" = "arm64" ]; then
16+
echo "aarch64-unknown-linux-gnu" > /tmp/TARGET
17+
echo "aarch64-linux-gnu-gcc" > /tmp/LINKER
18+
19+
apt-get update
20+
apt-get install -y gcc-aarch64-linux-gnu libgcc-s1-arm64-cross pkg-config
21+
22+
LIBDIR="/usr/aarch64-linux-gnu/lib"
23+
24+
# Also install gcc for aarch64 to get libgcc
25+
apt-get install -y gcc-aarch64-linux-gnu
26+
27+
elif [ "$TARGETARCH" = "arm" ]; then
28+
echo "armv7-unknown-linux-gnueabihf" > /tmp/TARGET
29+
echo "arm-linux-gnueabihf-gcc" > /tmp/LINKER
30+
31+
apt-get update
32+
apt-get install -y gcc-arm-linux-gnueabihf libgcc-s1-armhf-cross cmake libclang1 clang pkg-config
33+
34+
# Ensure gcc-arm-linux-gnueabihf is available
35+
apt-get install -y gcc-arm-linux-gnueabihf
36+
37+
cargo install --force --locked bindgen-cli
38+
39+
SYSROOT=$(arm-linux-gnueabihf-gcc -print-sysroot)
40+
echo "--sysroot=$SYSROOT -I$SYSROOT/usr/include -I$SYSROOT/usr/include/arm-linux-gnueabihf" > /tmp/BINDGEN_EXTRA_CLANG_ARGS
41+
42+
LIBDIR="/usr/arm-linux-gnueabihf/lib"
43+
else
44+
echo "Unsupported cross compilation target: $TARGETARCH"
45+
exit 1
46+
fi
47+
48+
# Copy libgcc_s.so.1 for runtime
49+
mkdir -p /tmp/sqlpage-libs
50+
51+
cp "$LIBDIR/libgcc_s.so.1" /tmp/sqlpage-libs/
52+
53+
# Add the target
54+
rustup target add "$(cat /tmp/TARGET)"
55+

0 commit comments

Comments
 (0)