From 1b35f74bfe8a0c44ca8bbe5c334e085d9ce4117e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 25 May 2025 23:47:12 +1200 Subject: [PATCH] Seems to be fully statically built now. --- source/CMakeLists.txt | 31 +++++++++++----------------- source/Dockerfile | 34 ++++++++++++++++++++++++++++++ source/Dockerfile.multiarch | 20 ------------------ source/build.zig | 41 ------------------------------------- source/build_static.sh | 11 ++++++++++ source/multibuild.sh | 24 ---------------------- 6 files changed, 57 insertions(+), 104 deletions(-) create mode 100644 source/Dockerfile delete mode 100644 source/Dockerfile.multiarch delete mode 100644 source/build.zig create mode 100755 source/build_static.sh delete mode 100755 source/multibuild.sh diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 94caf0d..21c9797 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,6 +1,15 @@ cmake_minimum_required(VERSION 3.10) project(dropshell VERSION 1.0.0 LANGUAGES CXX) +# Force static linking globally +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static") +set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") +set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE) +set(CMAKE_POSITION_INDEPENDENT_CODE OFF) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static") +set(ZLIB_USE_STATIC_LIBS "ON") + set(CMAKE_CXX_STANDARD 23) set(CMAKE_C_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -89,7 +98,8 @@ FetchContent_Declare( GIT_TAG v2.1.5 ) FetchContent_MakeAvailable(libassert) -include(FetchContent) + +# Add cpptrace FetchContent_Declare( cpptrace GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git @@ -97,14 +107,6 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(cpptrace) -# Add cpp-httplib -FetchContent_Declare( - cpp-httplib - GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git - GIT_TAG v0.14.1 -) -FetchContent_MakeAvailable(cpp-httplib) - # Add nlohmann/json FetchContent_Declare( nlohmann_json @@ -113,25 +115,16 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(nlohmann_json) -# Find zlib -find_package(ZLIB REQUIRED) - -# Force static linking for zlib -set(ZLIB_USE_STATIC_LIBS TRUE) -set(ZLIB_LIBRARIES z) - # Link libraries target_link_libraries(dropshell PRIVATE libassert::assert cpptrace::cpptrace - httplib::httplib nlohmann_json::nlohmann_json - ${ZLIB_LIBRARIES} ) # Set static linking flags set_target_properties(dropshell PROPERTIES - LINK_FLAGS "-static-libstdc++ -static-libgcc -Wl,-Bstatic -lz -Wl,-Bdynamic" + LINK_FLAGS "-static" ) # Install targets diff --git a/source/Dockerfile b/source/Dockerfile new file mode 100644 index 0000000..2f93893 --- /dev/null +++ b/source/Dockerfile @@ -0,0 +1,34 @@ +FROM alpine:latest + +# Install build dependencies +RUN apk add --no-cache \ + build-base \ + cmake \ + git \ + musl-dev \ + curl \ + bash \ + musl \ + g++ \ + ninja \ + linux-headers + +# Set working directory +WORKDIR /build + +# Copy source files +COPY . . + +# Configure and build +RUN mkdir -p build_static + +RUN cmake -G Ninja -B build_static -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_C_COMPILER=gcc \ + -DCMAKE_CXX_COMPILER=g++ \ + -DCMAKE_EXE_LINKER_FLAGS="-static" \ + -DCMAKE_FIND_LIBRARY_SUFFIXES=".a" \ + -DBUILD_SHARED_LIBS=OFF + +RUN cmake --build build_static + +# The resulting binary will be in build_static/dropshell \ No newline at end of file diff --git a/source/Dockerfile.multiarch b/source/Dockerfile.multiarch deleted file mode 100644 index 1437fe1..0000000 --- a/source/Dockerfile.multiarch +++ /dev/null @@ -1,20 +0,0 @@ -FROM alpine AS compiler - -ARG VERSION=0.13.0 -ARG OPTIONS=-Doptimize=ReleaseSafe - -RUN apk update && apk add curl tar xz - -# zig-linux-aarch64-0.10.1.tar.xz -# ziglang.org/download//zig-linux--.tar.xz - -RUN curl https://ziglang.org/download/$VERSION/zig-linux-$(uname -m)-$VERSION.tar.xz -O && \ - tar -xf *.tar.xz && \ - mv zig-linux-$(uname -m)-$VERSION /compiler - -WORKDIR /build -COPY . /build -RUN /compiler/zig build $OPTIONS - -FROM scratch AS output -COPY --from=compiler /build/zig-out/bin /bin \ No newline at end of file diff --git a/source/build.zig b/source/build.zig deleted file mode 100644 index 73ce95e..0000000 --- a/source/build.zig +++ /dev/null @@ -1,41 +0,0 @@ -const std = @import("std"); - -pub fn build(b: *std.Build) void { - const target = b.standardTargetOptions(.{}); - const optimize = b.standardOptimizeOption(.{}); - - const exe = b.addExecutable(.{ - .name = "dropshell", - .target = target, - .optimize = optimize, - .root_source_file = .{ .cwd_relative = "src/main.cpp" }, - }); - - // Link with C++ standard library - exe.linkLibCpp(); - - // Add include directories if needed - exe.addIncludePath(.{ .cwd_relative = "include" }); - - b.installArtifact(exe); - - const run_cmd = b.addRunArtifact(exe); - run_cmd.step.dependOn(b.getInstallStep()); - - if (b.args) |args| { - run_cmd.addArgs(args); - } - - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); - - const unit_tests = b.addTest(.{ - .root_source_file = .{ .cwd_relative = "src/main.cpp" }, - .target = target, - .optimize = optimize, - }); - - const run_unit_tests = b.addRunArtifact(unit_tests); - const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(&run_unit_tests.step); -} \ No newline at end of file diff --git a/source/build_static.sh b/source/build_static.sh new file mode 100755 index 0000000..6ecafa2 --- /dev/null +++ b/source/build_static.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Build the Docker image +docker build -t dropshell-static-builder . + +# Create a container and copy the binary +docker create --name temp-container dropshell-static-builder +docker cp temp-container:/build/build_static/dropshell ./output/dropshell-static +docker rm temp-container + +echo "Static binary has been created as 'output/dropshell-static'" \ No newline at end of file diff --git a/source/multibuild.sh b/source/multibuild.sh deleted file mode 100755 index 116f414..0000000 --- a/source/multibuild.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash - -# build amd64 and arm64 versions of dropshell, to: -# build/dropshell.amd64 -# build/dropshell.arm64 - -set -e - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -OUTPUT_DIR="$SCRIPT_DIR/output" -DOCKERFILE="$SCRIPT_DIR/Dockerfile.multiarch" - -mkdir -p "$OUTPUT_DIR" - -options="-Doptimize=ReleaseSafe -Dtarget=x86_64-linux-musl" -docker build \ - --platform "linux/amd64" \ - --build-arg "OPTIONS=$options" \ - -f Dockerfile.multiarch \ - -t dropshell-build . - - -echo "Builds complete:" -ls -lh "$OUTPUT_DIR"/dropshell.* \ No newline at end of file