diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..dc81867 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,34 @@ +name: Publish + +on: + push: + tags: ['*'] + +permissions: + contents: write + +jobs: + build-artifacts: + name: Publish artifacts + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Zig Setup + uses: goto-bus-stop/setup-zig@v2 + with: + version: "0.12.0-dev.2541+894493549" + + - name: Zig Test + run: zig build test + + - name: Zig Dylib + run: zig build dylib -Drelease-safe + + - name: Release + uses: softprops/action-gh-release@v1 + if: startsWith(github.ref, 'refs/tags/') + with: + files: | + zig-out/include/*.h + zig-out/lib/*.so diff --git a/build.zig b/build.zig index 1a7a973..bf1c6c5 100644 --- a/build.zig +++ b/build.zig @@ -30,11 +30,15 @@ pub fn build(b: *std.Build) void { }, }); + const tag = git_tag(b.allocator) catch @panic("No git tag"); + const version = std.SemanticVersion.parse(tag) catch (std.SemanticVersion.parse("0.0.0") catch unreachable); + // Static Library const lib = b.addStaticLibrary(.{ .name = "fastlanez", .target = target, .optimize = optimize, + .version = version, .root_source_file = .{ .path = "src/lib.zig" }, }); lib.bundle_compiler_rt = true; @@ -44,12 +48,26 @@ pub fn build(b: *std.Build) void { // Ideally we would use dlib.getEmittedH(), but https://github.com/ziglang/zig/issues/18497 _ = lib.getEmittedH(); // Needed to trigger header generation const lib_header = b.addInstallFile(.{ .path = "zig-cache/fastlanez.h" }, "include/fastlanez.h"); - lib_header.step.dependOn(&lib_install.step); const lib_step = b.step("lib", "Build static C library"); lib_step.dependOn(&lib_header.step); lib_step.dependOn(&lib_install.step); + // Dynamic Library + const dylib = b.addSharedLibrary(.{ + .name = "fastlanez", + .target = target, + .optimize = optimize, + .version = version, + .root_source_file = .{ .path = "src/lib.zig" }, + }); + dylib.bundle_compiler_rt = true; + const dylib_install = b.addInstallArtifact(dylib, .{}); + + const dylib_step = b.step("dylib", "Build dynamic C library"); + dylib_step.dependOn(&lib_header.step); + dylib_step.dependOn(&dylib_install.step); + // Unit Tests const unit_tests = b.addTest(.{ .root_source_file = .{ .path = "src/fastlanez.zig" }, @@ -75,3 +93,17 @@ pub fn build(b: *std.Build) void { const bench_step = b.step("bench", "Run benchmarks"); bench_step.dependOn(&run_bench.step); } + +fn git_tag(allocator: std.mem.Allocator) ![]const u8 { + const result = try std.process.Child.run(.{ + .allocator = allocator, + .argv = &.{ "git", "describe", "--tags", "--always" }, + }); + if (result.term.Exited != 0) { + std.debug.print("Failed to discover git tag:\n{s}\n", .{result.stderr}); + std.process.exit(1); + } + allocator.free(result.stderr); + // Strip trailing newline + return result.stdout[0 .. result.stdout.len - 1]; +}