From 08794e64802b19d99c9389de2f43a781870eaf77 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 25 May 2025 23:04:39 +1200 Subject: [PATCH] LOL Zig --- source/Dockerfile.multiarch | 92 ++++++------------------------------- source/build.zig | 41 +++++++++++++++++ source/multibuild.sh | 36 +++------------ 3 files changed, 62 insertions(+), 107 deletions(-) create mode 100644 source/build.zig diff --git a/source/Dockerfile.multiarch b/source/Dockerfile.multiarch index be1a64a..1437fe1 100644 --- a/source/Dockerfile.multiarch +++ b/source/Dockerfile.multiarch @@ -1,82 +1,20 @@ -# syntax=docker/dockerfile:1.4 -FROM --platform=$BUILDPLATFORM alpine:3.19 AS deps +FROM alpine AS compiler -# Install build dependencies -RUN apk add --no-cache \ - build-base \ - cmake \ - git \ - linux-headers \ - musl-dev \ - zlib \ - zlib-dev \ - zlib-static \ - bzip2-static \ - xz-static \ - zstd-static \ - curl \ - bash \ - gcc \ - g++ \ - libstdc++ \ - ccache \ - ninja \ - pkgconfig +ARG VERSION=0.13.0 +ARG OPTIONS=-Doptimize=ReleaseSafe -FROM deps AS build +RUN apk update && apk add curl tar xz -ARG TARGETARCH -WORKDIR /source -COPY . . +# zig-linux-aarch64-0.10.1.tar.xz +# ziglang.org/download//zig-linux--.tar.xz -# 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 "Checking static libraries..."\n\ -ls -la /usr/lib/libz.a /usr/lib/libstdc++.a /usr/lib/libc.a || true\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 \ - -DCMAKE_POLICY_DEFAULT_CMP0074=NEW \ - -DZLIB_INCLUDE_DIR=/usr/include \ - -DZLIB_USE_STATIC_LIBS=TRUE \ - -DCMAKE_EXE_LINKER_FLAGS="-Wl,-Bstatic -lz -lstdc++ -static-libgcc" \ - -DCMAKE_C_FLAGS="-fPIC" \ - -DCMAKE_CXX_FLAGS="-fPIC" \ - -DCMAKE_FIND_LIBRARY_CUSTOM_LIB_SUFFIX=".a" 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 +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 -# 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 +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 new file mode 100644 index 0000000..73ce95e --- /dev/null +++ b/source/build.zig @@ -0,0 +1,41 @@ +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/multibuild.sh b/source/multibuild.sh index 370dd16..116f414 100755 --- a/source/multibuild.sh +++ b/source/multibuild.sh @@ -12,37 +12,13 @@ DOCKERFILE="$SCRIPT_DIR/Dockerfile.multiarch" mkdir -p "$OUTPUT_DIR" -# Ensure buildx is available -if ! docker buildx version &>/dev/null; then - echo "Docker Buildx is required. Please install Docker Buildx." >&2 - exit 1 -fi +options="-Doptimize=ReleaseSafe -Dtarget=x86_64-linux-musl" +docker build \ + --platform "linux/amd64" \ + --build-arg "OPTIONS=$options" \ + -f Dockerfile.multiarch \ + -t dropshell-build . -# 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_DIR"/dropshell.* \ No newline at end of file