Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Publish dynamic lib (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Mar 5, 2024
1 parent e1bc9e3 commit 3b9fc7b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
34 changes: 34 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -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
34 changes: 33 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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" },
Expand All @@ -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];
}

0 comments on commit 3b9fc7b

Please sign in to comment.