Skip to content

Commit

Permalink
scm: Initial Zig project scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
mochalins committed Sep 13, 2024
1 parent a271fb2 commit 4ece02d
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/**
58 changes: 58 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
13 changes: 13 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -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",
},
}
13 changes: 13 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -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);
}
Empty file added src/serialport.zig
Empty file.

0 comments on commit 4ece02d

Please sign in to comment.