From 3a4ef0ee196bfb497ef1ad8d25a68ce10aac43ea Mon Sep 17 00:00:00 2001 From: booniepepper Date: Fri, 13 Oct 2023 01:22:41 -0700 Subject: [PATCH] Use Zig 0.11.0, add github CI --- .github/workflows/compile.yml | 37 ++++++++++++ .github/workflows/release.yml | 35 +++++++++++ .tool-versions | 2 +- build.zig | 108 +++++++++++++++++++++++++--------- src/command.zig | 5 +- 5 files changed, 158 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/compile.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/compile.yml b/.github/workflows/compile.yml new file mode 100644 index 0000000..2b6d24d --- /dev/null +++ b/.github/workflows/compile.yml @@ -0,0 +1,37 @@ +name: compile + +on: + push: + branches: + - core + pull_request: + branches: + - core + +jobs: + compile: + runs-on: ubuntu-latest + + steps: + - name: Setup Zig + uses: goto-bus-stop/setup-zig@v2.1.0 + with: + version: 0.11.0 + + - name: Checkout project + uses: actions/checkout@v3 + + - name: zig build + run: | + zig env + zig build + + - name: zig build test + run: | + zig env + zig build test + + - name: zig build cross + run: | + zig env + zig build cross diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..9779743 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,35 @@ +on: + release: + types: [created] + +name: Add artifacts to release + +jobs: + generate: + name: Create release artifacts + runs-on: ubuntu-latest + steps: + - name: Setup Zig + uses: goto-bus-stop/setup-zig@v2.1.0 + with: + version: 0.11.0 + + - name: Checkout project + uses: actions/checkout@v3 + + - name: zig build test + run: | + zig env + zig build test + + - name: zig build cross -Doptimize=ReleaseSmall + run: | + zig env + zig build cross -Doptimize=ReleaseSmall + + - name: Upload the artifacts + uses: skx/github-action-publish-binaries@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: 'zig-out/cross/*/yn-*.tgz' diff --git a/.tool-versions b/.tool-versions index b152fe5..0721394 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ -zig 0.10.1 +zig 0.11.0 diff --git a/build.zig b/build.zig index 8d2dd90..700d196 100644 --- a/build.zig +++ b/build.zig @@ -1,40 +1,94 @@ const std = @import("std"); -pub fn build(b: *std.build.Builder) void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. +pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); + // Compile + const compile_step = b.step("yn", "Install Yn and yN executables"); + const cross_step = b.step("cross-compile", "Install cross-compiled executables"); + const cross_tar_step = b.step("cross", "Install and archive cross-compiled executables"); - const exe_Yn = b.addExecutable("Yn", "src/Yn.zig"); - exe_Yn.setTarget(target); - exe_Yn.setBuildMode(mode); - exe_Yn.install(); + const executables: [2][]const u8 = .{ "Yn", "yN" }; + inline for (executables) |name| { + const source = std.Build.FileSource.relative("src/" ++ name ++ ".zig"); + const exe = b.addExecutable(.{ + .name = name, + .root_source_file = source, + .optimize = optimize, + .target = target, + }); + const install_exe = b.addInstallArtifact(exe, .{}); + compile_step.dependOn(&install_exe.step); - const exe_yN = b.addExecutable("yN", "src/yN.zig"); - exe_yN.setTarget(target); - exe_yN.setBuildMode(mode); - exe_yN.install(); + inline for (TRIPLES) |TRIPLE| { + const cross = b.addExecutable(.{ + .name = name, + .root_source_file = source, + .optimize = optimize, + .target = try std.zig.CrossTarget.parse(.{ .arch_os_abi = TRIPLE }), + }); - const run_cmd = exe_Yn.run(); - run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| { - run_cmd.addArgs(args); + const cross_install = b.addInstallArtifact(cross, .{ + .dest_dir = .{ .override = .{ .custom = "cross/" ++ TRIPLE } }, + }); + + cross_step.dependOn(&cross_install.step); + } + } + + b.default_step.dependOn(compile_step); + + inline for (TRIPLES) |TRIPLE| { + const cross_tar = b.addSystemCommand(&.{ "sh", "-c", "tar -czvf yn-" ++ TRIPLE ++ ".tgz Yn* yN*" }); + cross_tar.cwd = "./zig-out/cross/" ++ TRIPLE; + + cross_tar.step.dependOn(cross_step); + cross_tar_step.dependOn(&cross_tar.step); } - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); + // Tests + const testSource = std.Build.FileSource.relative("test/test.zig"); + + const test_exe = b.addTest(.{ + .root_source_file = testSource, + .optimize = optimize, + .target = target, + }); - const exe_tests = b.addTest("test/test.zig"); - exe_tests.setTarget(target); - exe_tests.setBuildMode(mode); + const test_run = b.addRunArtifact(test_exe); const test_step = b.step("test", "Run unit tests"); - test_step.dependOn(b.getInstallStep()); - test_step.dependOn(&exe_tests.step); + test_step.dependOn(compile_step); + test_step.dependOn(&test_run.step); + + b.default_step.dependOn(test_step); } + +const TRIPLES = .{ + "aarch64-linux-gnu", + "aarch64-linux-musleabi", + "aarch64-macos-none", + "arm-linux-musleabi", + "arm-linux-musleabihf", + "mips-linux-gnu", + "mips-linux-musl", + "mips64-linux-gnuabi64", + "mips64-linux-musl", + "mips64el-linux-gnuabi64", + "mips64el-linux-musl", + "mipsel-linux-gnu", + "mipsel-linux-musl", + "powerpc-linux-gnu", + "powerpc-linux-musl", + "powerpc64le-linux-gnu", + "powerpc64le-linux-musl", + "riscv64-linux-gnu", + "riscv64-linux-musl", + "wasm32-wasi-musl", + "x86_64-linux-gnu", + "x86_64-linux-musl", + "x86_64-macos-none", + "x86-linux-gnu", + "x86-linux-musl", +}; diff --git a/src/command.zig b/src/command.zig index e86c313..9ec9ea4 100644 --- a/src/command.zig +++ b/src/command.zig @@ -52,7 +52,10 @@ pub const Response = enum { }; pub fn run(default: Response) !void { - var args = std.process.args(); + var arena = std.heap.ArenaAllocator{ .child_allocator = std.heap.page_allocator, .state = .{} }; + defer arena.deinit(); + + var args = try std.process.argsWithAllocator(arena.allocator()); const programPath = args.next().?; const program = std.fs.path.basename(programPath);