From 26a5114226b70eab9f6f4e9af839dc4462a17938 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Mon, 4 Mar 2024 23:46:37 +0000 Subject: [PATCH 1/3] Git Tag --- build.zig | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 1a7a973..3c02573 100644 --- a/build.zig +++ b/build.zig @@ -30,6 +30,9 @@ pub fn build(b: *std.Build) void { }, }); + var shell = Shell.create(b.allocator) catch unreachable; + defer shell.destroy(); + // Static Library const lib = b.addStaticLibrary(.{ .name = "fastlanez", @@ -44,12 +47,25 @@ 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, + .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(&dylib_install.step); + dylib_step.dependOn(lib_step); + // Unit Tests const unit_tests = b.addTest(.{ .root_source_file = .{ .path = "src/fastlanez.zig" }, @@ -75,3 +91,20 @@ 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 execute {s}:\n{s}\n", .{ code, result.stderr }); + std.process.exit(1); + } + allocator.free(result.stderr); + return result.stdout; + + // --always is necessary in cases where we haven't yet `git fetch`-ed. + const stdout = try shell.exec_stdout("git describe --tags --always", .{}); + return stdout; +} From 5fcbe82be307bac3f93c380b98b2f2e173111b81 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Mon, 4 Mar 2024 23:56:52 +0000 Subject: [PATCH 2/3] Git Tag --- build.zig | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/build.zig b/build.zig index 3c02573..bf1c6c5 100644 --- a/build.zig +++ b/build.zig @@ -30,14 +30,15 @@ pub fn build(b: *std.Build) void { }, }); - var shell = Shell.create(b.allocator) catch unreachable; - defer shell.destroy(); + 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; @@ -57,14 +58,15 @@ pub fn build(b: *std.Build) void { .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); - dylib_step.dependOn(lib_step); // Unit Tests const unit_tests = b.addTest(.{ @@ -92,19 +94,16 @@ pub fn build(b: *std.Build) void { bench_step.dependOn(&run_bench.step); } -fn git_tag(allocator: *std.mem.Allocator) ![]const u8 { +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 execute {s}:\n{s}\n", .{ code, result.stderr }); + std.debug.print("Failed to discover git tag:\n{s}\n", .{result.stderr}); std.process.exit(1); } allocator.free(result.stderr); - return result.stdout; - - // --always is necessary in cases where we haven't yet `git fetch`-ed. - const stdout = try shell.exec_stdout("git describe --tags --always", .{}); - return stdout; + // Strip trailing newline + return result.stdout[0 .. result.stdout.len - 1]; } From 1a2584ff1546fa76844d0f606c6934c04fe2d96f Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Tue, 5 Mar 2024 00:01:23 +0000 Subject: [PATCH 3/3] Git Tag --- .github/workflows/publish.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/publish.yml 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