FROM --platform=$BUILDPLATFORM alpine:latest AS builder ARG PROJECT # Install ccache along with other dependencies RUN apk add --no-cache \ build-base \ cmake \ git \ musl-dev \ curl \ bash \ musl \ g++ \ ninja \ linux-headers \ mold \ zlib-static \ ccache \ pkgconf \ pkgconfig \ libunwind \ libunwind-dev \ libunwind-static \ zlib-dev \ zlib-static \ xz \ xz-dev \ xz-static \ xz-libs \ libdwarf \ libdwarf-dev \ elfutils \ elfutils-dev \ elfutils-libelf \ binutils \ binutils-dev \ pipx \ openssl-dev \ sqlite-dev \ libpq-dev \ mariadb-connector-c-dev \ c-ares-dev \ jsoncpp-dev \ ninja \ util-linux-dev \ util-linux-static \ perl RUN apk add --no-cahce \ c-ares-dev \ c-ares-static #RUN apk add --no-cache --update --repository=https://dl-cdn.alpinelinux.org/alpine/v3.16/main/ libexecinfo-dev SHELL ["/bin/bash", "-c"] # Build OpenSSL statically with musl ARG OPENSSL_VERSION=3.3.0 WORKDIR /tmp RUN curl -LO https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz && \ tar xzf openssl-${OPENSSL_VERSION}.tar.gz && \ cd openssl-${OPENSSL_VERSION} && \ ./Configure no-shared no-dso no-tests no-async linux-x86_64 \ --prefix=/usr/local/openssl-musl \ -static -fPIC && \ make -j$(nproc) && \ make install_sw && \ cd / && rm -rf /tmp/openssl-${OPENSSL_VERSION} /tmp/openssl-${OPENSSL_VERSION}.tar.gz #ARG DROGON_VERSION=1.9.5 RUN git clone --recurse-submodules https://github.com/drogonframework/drogon.git /tmp/drogon && \ #RUN git clone --branch v${DROGON_VERSION} --depth 1 https://github.com/drogonframework/drogon.git /tmp/drogon && \ mkdir -p /tmp/drogon/build && \ cd /tmp/drogon/build && \ cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=OFF \ -DCMAKE_POSITION_INDEPENDENT_CODE=ON \ -DCMAKE_CXX_FLAGS="-static -static-libgcc -static-libstdc++" \ -DCMAKE_EXE_LINKER_FLAGS="-static" \ -DCMAKE_INSTALL_PREFIX=/usr/local \ -DCMAKE_FIND_LIBRARY_SUFFIXES=".a" \ -DUSE_MONGODB=OFF \ -DUSE_POSTGRESQL=ON \ -DUSE_MYSQL=ON \ -DUSE_SQLITE3=ON \ -DBUILD_EXAMPLES=OFF \ -DBUILD_TESTING=OFF \ -DUUID_LIBRARY=/usr/lib/libuuid.a \ -DOPENSSL_ROOT_DIR=/usr/local/openssl-musl \ -DOPENSSL_USE_STATIC_LIBS=TRUE \ &&\ make -j$(nproc) && \ make install && \ cd / && rm -rf /tmp/drogon # Set working directory WORKDIR /app COPY . . # Configure and build with ccache RUN --mount=type=cache,target=/build \ mkdir -p /build && \ cmake -G Ninja -S /app -B /build \ -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \ -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \ -DCMAKE_C_COMPILER_LAUNCHER=ccache \ -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold -static" \ -DCMAKE_FIND_LIBRARY_SUFFIXES=".a" \ -DZLIB_BUILD_SHARED=OFF \ -DZLIB_BUILD_STATIC=ON \ -DBUILD_SHARED_LIBS=OFF \ -DPROJECT_NAME="${PROJECT}" \ -DCMAKE_STRIP=OFF \ -DIGNORE_DYNAMIC_LOADING=ON \ ${CMAKE_TOOLCHAIN_FILE:+-DCMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE} # Explicitly build dependencies first (cached separately) RUN --mount=type=cache,target=/build cmake --build /build --target run_prebuild_script RUN --mount=type=cache,target=/build cmake --build /build # Copy the built executable to a regular directory for the final stage RUN --mount=type=cache,target=/build mkdir -p /output && \ find /build -type f -executable -name "*${PROJECT}*" -exec cp {} /output/${PROJECT} \; || \ find /build -type f -executable -exec cp {} /output/${PROJECT} \; # Final stage that only contains the binary FROM scratch AS project ARG PROJECT # Copy the actual binary from the regular directory COPY --from=builder /output/${PROJECT} /${PROJECT}