Seems to be fully statically built now.
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 1m15s

This commit is contained in:
Your Name 2025-05-25 23:47:12 +12:00
parent 08794e6480
commit 1b35f74bfe
6 changed files with 57 additions and 104 deletions

View File

@ -1,6 +1,15 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(dropshell VERSION 1.0.0 LANGUAGES CXX) 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_CXX_STANDARD 23)
set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
@ -89,7 +98,8 @@ FetchContent_Declare(
GIT_TAG v2.1.5 GIT_TAG v2.1.5
) )
FetchContent_MakeAvailable(libassert) FetchContent_MakeAvailable(libassert)
include(FetchContent)
# Add cpptrace
FetchContent_Declare( FetchContent_Declare(
cpptrace cpptrace
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
@ -97,14 +107,6 @@ FetchContent_Declare(
) )
FetchContent_MakeAvailable(cpptrace) 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 # Add nlohmann/json
FetchContent_Declare( FetchContent_Declare(
nlohmann_json nlohmann_json
@ -113,25 +115,16 @@ FetchContent_Declare(
) )
FetchContent_MakeAvailable(nlohmann_json) 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 # Link libraries
target_link_libraries(dropshell PRIVATE target_link_libraries(dropshell PRIVATE
libassert::assert libassert::assert
cpptrace::cpptrace cpptrace::cpptrace
httplib::httplib
nlohmann_json::nlohmann_json nlohmann_json::nlohmann_json
${ZLIB_LIBRARIES}
) )
# Set static linking flags # Set static linking flags
set_target_properties(dropshell PROPERTIES set_target_properties(dropshell PROPERTIES
LINK_FLAGS "-static-libstdc++ -static-libgcc -Wl,-Bstatic -lz -Wl,-Bdynamic" LINK_FLAGS "-static"
) )
# Install targets # Install targets

34
source/Dockerfile Normal file
View File

@ -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

View File

@ -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/<ver>/zig-linux-<architecture>-<ver>.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

View File

@ -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);
}

11
source/build_static.sh Executable file
View File

@ -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'"

View File

@ -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.*