diff --git a/Dockerfile b/Dockerfile index 13d150b..a22b687 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,17 @@ ######################## # Stage 1: build deps # ######################## -FROM alpine:3.19 AS builder +FROM alpine:latest AS builder + +ARG TARGET_ARCH +RUN case $TARGET_ARCH in \ + "arm64") export TARGET_TRIPLE="aarch64-linux-musl";; \ + "amd64") export TARGET_TRIPLE="x86_64-linux-musl";; \ + *) echo "Unsupported architecture"; exit 1;; \ + esac && \ + export CC="${TARGET_TRIPLE}-gcc" && \ + export CXX="${TARGET_TRIPLE}-g++" + # Enable static builds with musl ENV CFLAGS="-static -O2" \ @@ -44,8 +54,7 @@ RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repo patch \ gawk \ xz-dev \ - crosstool-ng - + gcc-cross-embedded ######################################## # nlohmann/json (compiled static .a) # @@ -115,7 +124,7 @@ RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repo linux-headers \ ccache \ mold \ - crosstool-ng + gcc-cross-embedded # Configure mold as the default linker ENV LD=mold diff --git a/build.sh b/build.sh index b88c62b..041df84 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,11 @@ #!/bin/bash -docker build -t cpp-httplib-builder . \ No newline at end of file +docker build --build-arg TARGET_ARCH=arm64 -t cpp-httplib-builder:arm64 . +docker build --build-arg TARGET_ARCH=amd64 -t cpp-httplib-builder:amd64 . + +docker tag cpp-httplib-builder:arm64 gitea.jde.nz/public/cpp-httplib-builder:arm64 +docker tag cpp-httplib-builder:amd64 gitea.jde.nz/public/cpp-httplib-builder:amd64 +docker tag cpp-httplib-builder:amd64 gitea.jde.nz/public/cpp-httplib-builder:latest + +docker push gitea.jde.nz/public/cpp-httplib-builder:arm64 +docker push gitea.jde.nz/public/cpp-httplib-builder:amd64 \ No newline at end of file diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/build.sh b/test/build.sh index d8c118f..2edbfb4 100755 --- a/test/build.sh +++ b/test/build.sh @@ -1,35 +1,74 @@ #!/bin/bash -# Create volume if it doesn't exist -docker volume create cppbuild-cache +set -e -# Directory to copy the executable to -OUTDIR="output" +PROJECT=ipdemo -# Build command to run in the container -BUILD_COMMAND="mkdir -p /app/build && \ - cd /app/build && \ - cmake -G Ninja /app/src && \ - ninja -j$(nproc) && \ - mkdir /app/src/$OUTDIR && \ - cp /app/build/ipdemo /app/src/$OUTDIR/ipdemo && \ - chown -R $(id -u):$(id -g) /app/src/$OUTDIR" +function title() { + # print a title with a line of dashes + echo "--------------------------------" + echo "$1" + echo "--------------------------------" +} -# Run the build in the container -docker run --rm \ - -v cppbuild-cache:/app/build \ - -v $(pwd):/app/src \ - -w /app/src \ - -e CCACHE_DIR=/app/build/.ccache \ - -e CC="ccache gcc" \ - -e CXX="ccache g++" \ - -e LD=mold \ - -e CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) \ - -e NINJA_STATUS="[%f/%t] " \ - cpp-httplib-builder:latest \ - -c "$BUILD_COMMAND" +function build() { + ARCH="$1" + + title "Building $PROJECT.$ARCH" + + if [ "$ARCH" != "arm64" ] && [ "$ARCH" != "amd64" ]; then + echo "Unsupported architecture: $ARCH" + exit 1 + fi + + # Create volume if it doesn't exist + docker volume create cppbuild-cache-$ARCH + + # Directory to copy the executable to + OUTDIR="output" + + BUILD_IMAGE="gitea.jde.nz/public/cpp-httplib-builder:$ARCH" + + # Build command to run in the container + BUILD_COMMAND="mkdir -p /app/build && \ + cd /app/build && \ + cmake -G Ninja /app/src && \ + ninja -j$(nproc) && \ + mkdir -p /app/src/$OUTDIR && \ + cp /app/build/$PROJECT /app/src/$OUTDIR/$PROJECT.$ARCH && \ + chown -R $(id -u):$(id -g) /app/src/$OUTDIR" + + # Run the build in the container + docker run --rm \ + -v cppbuild-cache:/app/build \ + -v $(pwd):/app/src \ + -w /app/src \ + -e CCACHE_DIR=/app/build/.ccache \ + -e CC="ccache gcc" \ + -e CXX="ccache g++" \ + -e LD=mold \ + -e CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) \ + -e NINJA_STATUS="[%f/%t] " \ + $BUILD_IMAGE \ + -c "$BUILD_COMMAND" + + # Run the executable if it exists + if [ ! -f $OUTDIR/$PROJECT.$ARCH ]; then + echo "Executable not found: $OUTDIR/$PROJECT.$ARCH" + exit 1 + fi + +# Check if the executable is dynamically linked using ldd + if ldd "$OUTDIR/$PROJECT.$ARCH"; then + echo "Error: Dynamic dependencies found for $OUTDIR/$PROJECT.$ARCH" + exit 1 # Exit the script with an error + else + echo "Successfully verified no dynamic dependencies for $OUTDIR/$PROJECT.$ARCH" + fi + + file $OUTDIR/$PROJECT.$ARCH +} + +build arm64 +build amd64 -# Run the executable if it exists -if [ -f $OUTDIR/ipdemo ]; then - $OUTDIR/ipdemo -fi diff --git a/test/output/ipdemo b/test/output/ipdemo deleted file mode 100755 index 87fe073..0000000 Binary files a/test/output/ipdemo and /dev/null differ