From f45baa8362e9671166316178b72730be1f4da81e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 25 May 2025 22:12:14 +1200 Subject: [PATCH] Craziness with multiarch --- source/CMakeLists.txt | 9 +++++ source/Dockerfile.multiarch | 70 ++++++++++++++++++++++++++++++++++ source/make_createagent.sh | 3 +- source/multibuild.sh | 75 ++++++++++++++++--------------------- 4 files changed, 112 insertions(+), 45 deletions(-) create mode 100644 source/Dockerfile.multiarch diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 2f53e99..69141ea 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -113,12 +113,21 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(nlohmann_json) +# Find zlib +find_package(ZLIB REQUIRED) + # Link libraries target_link_libraries(dropshell PRIVATE libassert::assert cpptrace::cpptrace httplib::httplib nlohmann_json::nlohmann_json + ZLIB::ZLIB +) + +# Set static linking flags +set_target_properties(dropshell PROPERTIES + LINK_FLAGS "-static-libstdc++ -static-libgcc -Wl,-Bstatic -Wl,-Bdynamic" ) # Install targets diff --git a/source/Dockerfile.multiarch b/source/Dockerfile.multiarch new file mode 100644 index 0000000..4ad600f --- /dev/null +++ b/source/Dockerfile.multiarch @@ -0,0 +1,70 @@ +# syntax=docker/dockerfile:1.4 +FROM --platform=$BUILDPLATFORM alpine:3.19 AS deps + +# Install build dependencies +RUN apk add --no-cache \ + build-base \ + cmake \ + git \ + linux-headers \ + musl-dev \ + zlib-dev \ + bzip2-dev \ + xz-dev \ + zstd-dev \ + curl \ + bash \ + gcc \ + g++ \ + libstdc++ \ + libstdc++-dev \ + ccache \ + ninja + +FROM deps AS build + +ARG TARGETARCH +WORKDIR /source +COPY . . + +# Create and run build script +RUN printf '#!/bin/bash\n\ +set -e\n\ +echo "Setting up build directory..."\n\ +mkdir -p /build/ccache\n\ +mkdir -p /build/build_${TARGETARCH}\n\ +\n\ +echo "Setting up ccache..."\n\ +export CCACHE_DIR=/build/ccache\n\ +export CCACHE_MAXSIZE=2G\n\ +export PATH="/usr/lib/ccache/bin:$PATH"\n\ +\n\ +echo "Running CMake configuration..."\n\ +cd /build/build_${TARGETARCH}\n\ +cmake /source -G Ninja -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_FIND_LIBRARY_SUFFIXES=".a" \ + -DBUILD_SHARED_LIBS=OFF \ + -DCMAKE_C_COMPILER_LAUNCHER=ccache \ + -DCMAKE_CXX_COMPILER_LAUNCHER=ccache 2>&1 | tee cmake.log\n\ +\n\ +echo "Starting build..."\n\ +ninja -v -j$(nproc) 2>&1 | tee build.log\n\ +\n\ +echo "Build complete. Checking for binary..."\n\ +if [ ! -f dropshell ]; then\n\ + echo "Build failed. Directory contents:"\n\ + ls -la\n\ + echo "CMake log:"\n\ + cat cmake.log\n\ + echo "Build log:"\n\ + cat build.log\n\ + exit 1\n\ +fi\n\ +\n\ +echo "ccache stats:"\n\ +ccache -s\n' > /source/docker_build.sh && chmod +x /source/docker_build.sh && /source/docker_build.sh + +# Final stage: copy out the binary +FROM scratch AS export-stage +ARG TARGETARCH +COPY --from=build /build/build_${TARGETARCH}/dropshell /dropshell \ No newline at end of file diff --git a/source/make_createagent.sh b/source/make_createagent.sh index 5411a83..c0cf187 100755 --- a/source/make_createagent.sh +++ b/source/make_createagent.sh @@ -6,8 +6,7 @@ set -e # src/utils/createagent.cpp # -SCRIPT_DIR=$(dirname "$0") - +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # check if dehydrate is installed if ! command -v dehydrate &> /dev/null; then echo "dehydrate could not be found - installing" diff --git a/source/multibuild.sh b/source/multibuild.sh index afc3c0a..370dd16 100755 --- a/source/multibuild.sh +++ b/source/multibuild.sh @@ -7,53 +7,42 @@ set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +OUTPUT_DIR="$SCRIPT_DIR/output" +DOCKERFILE="$SCRIPT_DIR/Dockerfile.multiarch" -rm -f $SCRIPT_DIR/build_amd64/dropshell $SCRIPT_DIR/build_arm64/dropshell $SCRIPT_DIR/output/dropshell.amd64 $SCRIPT_DIR/output/dropshell.arm64 +mkdir -p "$OUTPUT_DIR" -# Determine number of CPU cores for parallel build -if command -v nproc >/dev/null 2>&1; then - JOBS=$(nproc) -else - JOBS=4 # fallback default -fi - -PREV_PWD=$PWD -cd $SCRIPT_DIR - -# Build for amd64 (musl) -echo "Building for amd64 (musl)..." -cmake -B build_amd64 -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_C_COMPILER=x86_64-linux-musl-gcc \ - -DCMAKE_CXX_COMPILER=x86_64-linux-musl-g++ \ - -DCMAKE_EXE_LINKER_FLAGS="-static" \ - -DCMAKE_CXX_FLAGS="-march=x86-64" . -cmake --build build_amd64 --target dropshell --config Release -j"$JOBS" -mkdir -p output -cp build_amd64/dropshell output/dropshell.amd64 - -# Build for arm64 (musl) -echo "Building for arm64 (musl)..." -cmake -B build_arm64 -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_C_COMPILER=aarch64-linux-musl-gcc \ - -DCMAKE_CXX_COMPILER=aarch64-linux-musl-g++ \ - -DCMAKE_EXE_LINKER_FLAGS="-static" \ - -DCMAKE_CXX_FLAGS="-march=armv8-a" \ - -DCMAKE_SYSTEM_PROCESSOR=aarch64 . -cmake --build build_arm64 --target dropshell --config Release -j"$JOBS" -mkdir -p output -cp build_arm64/dropshell output/dropshell.arm64 - -if [ ! -f output/dropshell.amd64 ]; then - echo "output/dropshell.amd64 not found!" >&2 +# Ensure buildx is available +if ! docker buildx version &>/dev/null; then + echo "Docker Buildx is required. Please install Docker Buildx." >&2 exit 1 fi -if [ ! -f output/dropshell.arm64 ]; then - echo "output/dropshell.arm64 not found!" >&2 - exit 1 -fi +# Build for x86_64 +DOCKER_BUILDKIT=1 docker buildx build --platform linux/amd64 \ + -f "$DOCKERFILE" \ + --output type=local,dest="$OUTPUT_DIR/amd64_tmp" \ + --cache-from type=local,src=/tmp/dropshell-cache \ + --cache-to type=local,dest=/tmp/dropshell-cache,mode=max \ + "$SCRIPT_DIR" + +mv "$OUTPUT_DIR/amd64_tmp/dropshell" "$OUTPUT_DIR/dropshell.amd64" +rm -rf "$OUTPUT_DIR/amd64_tmp" + +echo "Built output/dropshell.amd64" + +# Build for aarch64 +DOCKER_BUILDKIT=1 docker buildx build --platform linux/arm64 \ + -f "$DOCKERFILE" \ + --output type=local,dest="$OUTPUT_DIR/arm64_tmp" \ + --cache-from type=local,src=/tmp/dropshell-cache \ + --cache-to type=local,dest=/tmp/dropshell-cache,mode=max \ + "$SCRIPT_DIR" + +mv "$OUTPUT_DIR/arm64_tmp/dropshell" "$OUTPUT_DIR/dropshell.arm64" +rm -rf "$OUTPUT_DIR/arm64_tmp" + +echo "Built output/dropshell.arm64" echo "Builds complete:" -ls -lh output/dropshell.* - -cd $PREV_PWD \ No newline at end of file +ls -lh "$OUTPUT_DIR"/dropshell.* \ No newline at end of file