From 4ece02dfe74218c30de8f5b18da9f36c1bce334b Mon Sep 17 00:00:00 2001 From: mochalins <117967760+mochalins@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:28:02 +0900 Subject: [PATCH] scm: Initial Zig project scaffold --- .github/workflows/main.yml | 33 ++++++++++++++++++++++ .gitignore | 19 +++++++++++++ build.zig | 58 ++++++++++++++++++++++++++++++++++++++ build.zig.zon | 13 +++++++++ src/lib.zig | 13 +++++++++ src/serialport.zig | 0 6 files changed, 136 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 .gitignore create mode 100644 build.zig create mode 100644 build.zig.zon create mode 100644 src/lib.zig create mode 100644 src/serialport.zig diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..ecc23e1 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,33 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + branches: + - main + workflow_dispatch: + +jobs: + build: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Log masks + run: echo "::add-mask::$NAME_MASK" + + - uses: actions/checkout@v4 + - uses: mlugg/setup-zig@v1 + with: + version: master + + - name: Run zig fmt + if: matrix.os == 'self-hosted' + run: zig fmt --check . + + - name: Run module tests + run: zig build test --summary all diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a450c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# This sets the `.gitignore` to be a *whitelist* by default. +**/** +!*/ + + +# Whitelist +!*.md +!LICENSE-MIT +!.gitignore +!build.zig +!build.zig.zon +!.github/**/*.yml + +!*.zig + +# Blacklist (overrides Whitelist) +**/zig-out/** +**/zig-cache/** +**/.zig-cache/** diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..ae8aa96 --- /dev/null +++ b/build.zig @@ -0,0 +1,58 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const lib = b.option( + std.builtin.LinkMode, + "lib", + "Build an exportable C library.", + ); + + const mod = b.addModule("serialport", .{ + .root_source_file = b.path("src/serialport.zig"), + .target = target, + .optimize = optimize, + }); + + const test_step = b.step("test", "Run unit tests"); + + const mod_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/serialport.zig"), + .target = target, + .optimize = optimize, + }); + const run_mod_unit_tests = b.addRunArtifact(mod_unit_tests); + test_step.dependOn(&run_mod_unit_tests.step); + + if (lib) |l| { + // Library Artifact + { + const library = if (l == .static) b.addStaticLibrary(.{ + .name = "serialport", + .root_source_file = b.path("src/lib.zig"), + .target = target, + .optimize = optimize, + }) else b.addSharedLibrary(.{ + .name = "serialport", + .root_source_file = b.path("src/lib.zig"), + .target = target, + .optimize = optimize, + }); + library.root_module.addImport("serialport", mod); + b.installArtifact(library); + } + // Library Tests + { + const lib_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/lib.zig"), + .target = target, + .optimize = optimize, + }); + lib_unit_tests.root_module.addImport("serialport", mod); + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + test_step.dependOn(&run_lib_unit_tests.step); + } + } +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..dd44ea2 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,13 @@ +.{ + .name = "serialport", + .version = "0.0.0", + .minimum_zig_version = "0.11.0", + .dependencies = .{}, + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + "LICENSE-MIT", + "README.md", + }, +} diff --git a/src/lib.zig b/src/lib.zig new file mode 100644 index 0000000..f034ca3 --- /dev/null +++ b/src/lib.zig @@ -0,0 +1,13 @@ +//! By convention, root.zig is the root source file when making a library. If +//! you are making an executable, the convention is to delete this file and +//! start with main.zig instead. +const std = @import("std"); +const testing = std.testing; + +export fn add(a: i32, b: i32) i32 { + return a + b; +} + +test "basic add functionality" { + try testing.expect(add(3, 7) == 10); +} diff --git a/src/serialport.zig b/src/serialport.zig new file mode 100644 index 0000000..e69de29