From b898e4ec8bcbb486a36997754d461597dd560c2b Mon Sep 17 00:00:00 2001 From: torque Date: Sun, 15 Sep 2024 20:06:51 -0700 Subject: [PATCH] build: add libsodium support --- build.zig | 16 ++++++++++++++++ build.zig.zon | 5 +++++ readme.md | 5 ++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 370ac0d..252d930 100644 --- a/build.zig +++ b/build.zig @@ -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); } diff --git a/build.zig.zon b/build.zig.zon index 69d066f..649350d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -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", diff --git a/readme.md b/readme.md index 7445471..e0f0e52 100644 --- a/readme.md +++ b/readme.md @@ -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