From 37564fa5583a721606a913d4ef42410360ac60c5 Mon Sep 17 00:00:00 2001 From: Cayman Date: Mon, 10 Aug 2026 15:46:48 -0400 Subject: [PATCH] export translated C headers as Zig module --- .github/workflows/ci.yml | 3 +++ README.md | 11 ++++++++++- build.zig | 24 ++++++++++++++++++++++++ src/test.zig | 7 +++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/test.zig diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 068c240..9d03b2f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,3 +40,6 @@ jobs: - name: zig build (Release Safe) run: zig build --release=safe + + - name: zig build test (Release Safe) + run: zig build test --release=safe diff --git a/README.md b/README.md index 6f2f868..c8986c8 100644 --- a/README.md +++ b/README.md @@ -12,5 +12,14 @@ const blst_dep = b.dependency("blst", .{ .optimize = optimize, }); -your_exe.linkLibrary(blst_dep.artifact("blst")); +your_exe.root_module.addImport("blst", blst_dep.module("blst")); +``` + +The module contains the translated declarations from `blst.h` and +`blst_aux.h`, and links the native library automatically: + +```zig +const blst = @import("blst"); + +const generator = blst.blst_p1_generator(); ``` diff --git a/build.zig b/build.zig index 60f6588..2f52d3d 100644 --- a/build.zig +++ b/build.zig @@ -67,5 +67,29 @@ pub fn build(b: *std.Build) !void { .flags = c_flags.items, }); + const translated_headers = b.addTranslateC(.{ + .root_source_file = upstream.path("bindings/blst.h"), + .target = target, + .optimize = optimize, + }); + translated_headers.addIncludePath(upstream.path("bindings")); + + const module = translated_headers.addModule("blst"); + module.linkLibrary(lib); + b.installArtifact(lib); + + const module_tests = b.addTest(.{ + .name = "blst-module-tests", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/test.zig"), + .target = target, + .optimize = optimize, + }), + }); + module_tests.root_module.addImport("blst", module); + + const run_module_tests = b.addRunArtifact(module_tests); + const test_step = b.step("test", "Test the public blst module"); + test_step.dependOn(&run_module_tests.step); } diff --git a/src/test.zig b/src/test.zig new file mode 100644 index 0000000..f3bf1a2 --- /dev/null +++ b/src/test.zig @@ -0,0 +1,7 @@ +const std = @import("std"); +const blst = @import("blst"); + +test "translated core and auxiliary APIs are linked" { + try std.testing.expect(blst.blst_p1_generator() != null); + try std.testing.expectEqual(@sizeOf(blst.blst_p1), blst.blst_p1_sizeof()); +}