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

Publish dynamic lib #9

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}
Loading