11// SPDX-License-Identifier: MPL-2.0
22// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
33//
4- // Minimum Zig version: 0.11.0
4+ // Minimum Zig version: 0.15.0 (Build.addLibrary / root_module API; for the
5+ // old 0.11 addStaticLibrary/addSharedLibrary form see git history)
56// Required for: std.crypto.hash.Blake3, C ABI export, @memcpy builtin
67//
78const std = @import ("std" );
@@ -10,44 +11,58 @@ pub fn build(b: *std.Build) void {
1011 const target = b .standardTargetOptions (.{});
1112 const optimize = b .standardOptimizeOption (.{});
1213
13- // Build libochrance.so shared library
14- const lib = b .addStaticLibrary (.{
15- .name = "ochrance" ,
16- .root_source_file = .{ .cwd_relative = "src/main.zig" },
14+ // One module definition shared by every artifact. libc is required by
15+ // std.heap.c_allocator (used by the handle lifecycle) and is the natural
16+ // choice for a C-ABI FFI library.
17+ const mod = b .createModule (.{
18+ .root_source_file = b .path ("src/main.zig" ),
1719 .target = target ,
1820 .optimize = optimize ,
21+ .link_libc = true ,
1922 });
2023
21- // libc is required by std.heap.c_allocator (used by the handle lifecycle)
22- // and is the natural choice for a C-ABI FFI library.
23- lib .linkLibC ();
24- b .installArtifact (lib );
24+ // Static libochrance.a
25+ const static_lib = b .addLibrary (.{
26+ .name = "ochrance" ,
27+ .linkage = .static ,
28+ .root_module = mod ,
29+ });
30+ b .installArtifact (static_lib );
2531
26- // Also create a shared library version ( libochrance.so) — this is what the
27- // Idris2 FFI loads at runtime. The name MUST be "ochrance" so the artifact is
28- // libochrance.so: the Idris bindings declare `%foreign "C:blake3_hash,
29- // libochrance"` (ochrance-core/Ochrance/FFI/Crypto.idr), so the runtime loader
30- // resolves the soname libochrance.so. Any other shared-library name would never
31- // be found. A static libochrance.a and a shared libochrance.so share the base
32+ // Shared libochrance.so — this is what the Idris2 FFI loads at runtime.
33+ // The name MUST be "ochrance" so the artifact is libochrance.so: the Idris
34+ // bindings declare `%foreign "C:blake3_hash, libochrance"`
35+ // (ochrance-core/Ochrance/FFI/Crypto.idr), so the runtime loader resolves
36+ // the soname libochrance.so. Any other shared-library name would never be
37+ // found. A static libochrance.a and a shared libochrance.so share the base
3238 // name without clashing (distinct extensions).
33- const shared_lib = b .addSharedLibrary (.{
39+ const shared_lib = b .addLibrary (.{
3440 .name = "ochrance" ,
35- .root_source_file = .{ .cwd_relative = "src/main.zig" },
36- .target = target ,
37- .optimize = optimize ,
41+ .linkage = .dynamic ,
42+ .root_module = mod ,
3843 });
39- shared_lib .linkLibC ();
4044 b .installArtifact (shared_lib );
4145
42- // Tests
46+ // Unit tests (in-module test blocks: KAT vectors, Ed25519 round-trip)
4347 const tests = b .addTest (.{
44- .root_source_file = .{ .cwd_relative = "src/main.zig" },
45- .target = target ,
46- .optimize = optimize ,
48+ .root_module = mod ,
49+ });
50+
51+ // Integration tests: extern declarations linked against libochrance.a,
52+ // exercising the C ABI exactly as an external consumer (Idris2) does.
53+ const integration_tests = b .addTest (.{
54+ .root_module = b .createModule (.{
55+ .root_source_file = b .path ("test/integration_test.zig" ),
56+ .target = target ,
57+ .optimize = optimize ,
58+ .link_libc = true ,
59+ }),
4760 });
48- tests . linkLibC ( );
61+ integration_tests . linkLibrary ( static_lib );
4962
5063 const run_tests = b .addRunArtifact (tests );
51- const test_step = b .step ("test" , "Run unit tests" );
64+ const run_integration_tests = b .addRunArtifact (integration_tests );
65+ const test_step = b .step ("test" , "Run unit + integration tests" );
5266 test_step .dependOn (& run_tests .step );
67+ test_step .dependOn (& run_integration_tests .step );
5368}
0 commit comments