build: add libsodium support
Some checks are pending
CI / build (macos-latest, 0.13.0) (push) Waiting to run
CI / build (true, ubuntu-latest, master) (push) Waiting to run
CI / build (ubuntu-latest, 0.12.1) (push) Waiting to run
CI / build (ubuntu-latest, 0.13.0) (push) Waiting to run
CI / build (windows-latest, 0.13.0) (push) Waiting to run

This commit is contained in:
torque 2024-09-15 20:06:51 -07:00
parent 7bd3a8ef51
commit b898e4ec8b
Signed by: torque
SSH Key Fingerprint: SHA256:nCrXefBNo6EbjNSQhv0nXmEg/VuNq3sMF5b8zETw3Tk
3 changed files with 25 additions and 1 deletions

View File

@ -4,6 +4,7 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const enable_libsodium = b.option(bool, "enable-libsodium", "Build with libsodium for higher-performance signing (default: true)") orelse true;
const enable_tls = b.option(bool, "enable-tls", "Build TLS support (default: true)") orelse true;
const tls_verify = b.option(bool, "force-host-verify", "Force hostname verification for TLS connections (default: true)") orelse true;
const enable_streaming = b.option(bool, "enable-streaming", "Build with streaming support (default: true)") orelse true;
@ -17,6 +18,15 @@ pub fn build(b: *std.Build) void {
"protobuf_c",
.{ .target = target, .optimize = optimize },
) else null;
const libsodium_dep = if (enable_libsodium) b.lazyDependency(
"libsodium",
.{
.target = target,
.optimize = optimize,
.static = true,
.shared = false,
},
) else null;
const lib = b.addStaticLibrary(.{
.name = "nats",
@ -100,6 +110,12 @@ pub fn build(b: *std.Build) void {
lib.linkLibrary(dep.artifact("protobuf_c"));
}
if (libsodium_dep) |dep| {
lib.defineCMacro("NATS_USE_LIBSODIUM", null);
// yep
lib.linkLibrary(dep.artifact(if (tinfo.isMinGW()) "libsodium-static" else "sodium"));
}
b.installArtifact(lib);
}

View File

@ -17,6 +17,11 @@
.hash = "1220f454bf9412333c5d21b8c21b323fc7e4e37b02341bf2fa49f110b8bc5d97c972",
.lazy = true,
},
.libsodium = .{
.url = "git+https://github.com/jedisct1/libsodium.git?ref=1.0.20-RELEASE#9511c982fb1d046470a8b42aa36556cdb7da15de",
.hash = "1220d265dc673167ffe4a3cefe2840893d2910cfd773cfb1893ff768d5f1351d2a1f",
.lazy = true,
},
},
.paths = .{
"build.zig",

View File

@ -29,6 +29,7 @@ You can then import `nats_c` in your `build.zig` with:
const nats_c_dep = b.dependency("nats_c", .{
.target = target,
.optimize = optimize,
.@"enable-libsodium" = true, // Use libsodium for optimized implementations of some signing routines
.@"enable-tls" = true, // enable SSL/TLS support
.@"force-host-verify" = true, // force hostname verification for TLS connections
.@"enable-streaming" = true, // build with support for NATS streaming extensions
@ -38,10 +39,11 @@ your_exe.linkLibrary(nats_c_dep.artifact("nats_c"));
## Dependencies
The NATS.c library has two optional dependencies:
The NATS.c library has optional dependencies:
- [`libressl`][libressl] when building with `enable-tls`
- [`protobuf-c`][protobuf-c] when building with `enable-streaming`
- [`libsodium`][libsodium] when building with `enable-libsodium`
These dependencies are currently automatically retrieved and compiled as static libraries by the Zig build system.
@ -54,4 +56,5 @@ These dependencies are currently automatically retrieved and compiled as static
[nats.c]: https://github.com/nats-io/nats.c
[libressl]: https://github.com/allyourcodebase/libressl
[protobuf-c]: https://github.com/allyourcodebase/protobuf-c
[libsodium]: https://github.com/jedisct1/libsodium
[epicyclic-dev-bindings]: https://github.com/epicyclic-dev/nats-client