diff --git a/Dockerfile b/Dockerfile index a22b687..4697816 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,42 +5,48 @@ ######################## 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++" +ARG B_TARGET_ARCH +ARG B_TARGET_TRIPLE +ARG B_CC +ARG B_CXX +ENV TARGET_ARCH=$B_TARGET_ARCH +ENV TARGET_TRIPLE=$B_TARGET_TRIPLE +ENV CC=$B_CC +ENV CXX=$B_CXX # Enable static builds with musl ENV CFLAGS="-static -O2" \ CXXFLAGS="-static -O2" \ - LDFLAGS="-static" + LDFLAGS="-static" + +# Add cross-compiler to PATH +ENV PATH="/opt/cross/bin:${PATH}" # Install build dependencies -RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \ - apk update && \ +RUN apk update && \ apk add --no-cache \ build-base \ + make \ cmake \ ninja \ git \ - wget \ - curl \ openssl-dev \ zlib-static \ - zlib-dev \ - linux-headers \ bash \ + wget \ + curl \ + linux-headers \ ccache \ mold \ musl-dev \ + gcc-cross-embedded \ + g++-cross-embedded \ gcc \ - g++ \ - make \ + g++ + +RUN apk add --no-cache \ + zlib-dev \ python3 \ bison \ flex \ @@ -53,15 +59,16 @@ RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repo libtool \ patch \ gawk \ - xz-dev \ - gcc-cross-embedded + xz-dev + +# Configure mold as the default linker +ENV LD=mold ######################################## # nlohmann/json (compiled static .a) # ######################################## -ARG JSON_VERSION=3.11.3 -RUN git clone --depth 1 --branch v${JSON_VERSION} https://github.com/nlohmann/json.git /tmp/json && \ - cmake -S /tmp/json -B /tmp/json/build \ +RUN git clone --depth 1 https://github.com/nlohmann/json.git /tmp/json +RUN cmake -S /tmp/json -B /tmp/json/build \ -DJSON_BuildTests=OFF \ -DJSON_MultipleHeaders=OFF \ -DBUILD_SHARED_LIBS=OFF && \ @@ -108,11 +115,21 @@ RUN strip --strip-unneeded /usr/local/lib/*.a ############################ FROM alpine:3.19 +ARG B_TARGET_ARCH +ARG B_TARGET_TRIPLE +ARG B_CC +ARG B_CXX + +ENV TARGET_ARCH=$B_TARGET_ARCH +ENV TARGET_TRIPLE=$B_TARGET_TRIPLE +ENV CC=$B_CC +ENV CXX=$B_CXX + # Install core toolchain for building apps -RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories && \ - apk update && \ +RUN apk update && \ apk add --no-cache \ build-base \ + make \ cmake \ ninja \ git \ @@ -124,19 +141,20 @@ RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repo linux-headers \ ccache \ mold \ - gcc-cross-embedded + musl-dev \ + gcc-cross-embedded \ + g++-cross-embedded \ + gcc \ + g++ # Configure mold as the default linker ENV LD=mold -# Copy pre‑built static libs + headers and cross-compiler toolchain +# Copy pre‑built static libs + headers COPY --from=builder /usr/local /usr/local -# Add toolchain to PATH -ENV PATH="/opt/cross/bin:${PATH}" - -ENV CFLAGS="-static -O2" \ - CXXFLAGS="-static -O2" \ +ENV CFLAGS="-static" \ + CXXFLAGS="-static" \ LDFLAGS="-static" WORKDIR /workspace diff --git a/build.sh b/build.sh index 041df84..39328b7 100755 --- a/build.sh +++ b/build.sh @@ -1,11 +1,27 @@ #!/bin/bash -docker build --build-arg TARGET_ARCH=arm64 -t cpp-httplib-builder:arm64 . -docker build --build-arg TARGET_ARCH=amd64 -t cpp-httplib-builder:amd64 . +set -e + +docker build \ + --build-arg B_TARGET_ARCH=arm64 \ + --build-arg B_TARGET_TRIPLE=aarch64-linux-musl \ + --build-arg B_CC=aarch64-none-elf-gcc \ + --build-arg B_CXX=aarch64-none-elf-g++ \ + -t cpp-httplib-builder:arm64 \ + . + +docker build \ + --build-arg B_TARGET_ARCH=amd64 \ + --build-arg B_TARGET_TRIPLE=x86_64-linux-musl \ + --build-arg B_CC=x86_64-alpine-linux-musl-gcc \ + --build-arg B_CXX=x86_64-alpine-linux-musl-g++ \ + -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 +docker push gitea.jde.nz/public/cpp-httplib-builder:amd64 +docker push gitea.jde.nz/public/cpp-httplib-builder:latest diff --git a/test/build.sh b/test/build.sh index 2edbfb4..c3cdf97 100755 --- a/test/build.sh +++ b/test/build.sh @@ -38,14 +38,21 @@ function build() { cp /app/build/$PROJECT /app/src/$OUTDIR/$PROJECT.$ARCH && \ chown -R $(id -u):$(id -g) /app/src/$OUTDIR" + + case $ARCH in + "arm64") export TARGET_TRIPLE="aarch64-linux-musl";; + "amd64") export TARGET_TRIPLE="x86_64-linux-musl";; + *) echo "Unsupported architecture $ARCH"; exit 1;; + esac + export CC="ccache ${TARGET_TRIPLE}-gcc" + export CXX="ccache ${TARGET_TRIPLE}-g++" + # 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] " \ diff --git a/test/crossbuild.sh b/test/crossbuild.sh deleted file mode 100755 index 6020c75..0000000 --- a/test/crossbuild.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/bin/bash - -TARGET_ARCH="aarch64" -OUTDIR="output-${TARGET_ARCH}" - -# Create volume if it doesn't exist -docker volume create cppbuild-cache-${TARGET_ARCH} - -# Build command to run in the container -BUILD_COMMAND="mkdir -p /app/build && \ - cd /app/build && \ - cmake -G Ninja \ - -DCMAKE_TOOLCHAIN_FILE=/app/src/cmake/${TARGET_ARCH}-linux-gnu.cmake \ - /app/src && \ - ninja -j$(nproc) && \ - mkdir -p /app/src/$OUTDIR && \ - cp /app/build/ipdemo /app/src/$OUTDIR/ipdemo && \ - chown -R $(id -u):$(id -g) /app/src/$OUTDIR" - -# Run the build in the container -docker run --rm \ - -v cppbuild-cache-${TARGET_ARCH}:/app/build \ - -v $(pwd):/app/src \ - -w /app/src \ - -e CCACHE_DIR=/app/build/.ccache \ - -e CC="ccache /opt/cross/bin/aarch64-unknown-linux-musl-gcc" \ - -e CXX="ccache /opt/cross/bin/aarch64-unknown-linux-musl-g++" \ - -e LD=mold \ - -e CMAKE_BUILD_PARALLEL_LEVEL=$(nproc) \ - -e NINJA_STATUS="[%f/%t] " \ - cpp-httplib-builder:latest \ - -c "$BUILD_COMMAND" - -echo "Build completed. Output is in $OUTDIR directory" \ No newline at end of file