From 101e90a668ce68365d4a0b292c06525d6d8919c0 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 16 Jan 2025 00:54:22 +0100 Subject: [PATCH] Allow compilation with Zig 0.13 as well as 0.14 Fixes #150 --- build.zig | 38 +++++++++++++++++++++++++++---------- src/libzodium/libzodium.zig | 2 +- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/build.zig b/build.zig index bc20591..5f27ab0 100644 --- a/build.zig +++ b/build.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("std"); pub fn build(b: *std.Build) !void { @@ -15,21 +16,38 @@ pub fn build(b: *std.Build) !void { }); minisign.linkLibC(); if (use_libzodium) { - const libzodium_mod = b.createModule(.{ - .root_source_file = b.path("src/libzodium/libzodium.zig"), - .target = target, - .optimize = optimize, - }); - const libzodium = b.addStaticLibrary(.{ - .name = "zodium", - .root_module = libzodium_mod, - .strip = true, - }); + var libzodium = lib: { + if (builtin.zig_version.major == 0 and builtin.zig_version.minor < 13) { + @compileError("Building requires Zig 0.13.0 or later"); + } + if (builtin.zig_version.major == 0 and builtin.zig_version.minor == 13) { + break :lib b.addStaticLibrary(.{ + .name = "zodium", + .strip = true, + .root_source_file = b.path("src/libzodium/libzodium.zig"), + .target = target, + .optimize = optimize, + }); + } else { + const libzodium_mod = b.createModule(.{ + .root_source_file = b.path("src/libzodium/libzodium.zig"), + .target = target, + .optimize = optimize, + }); + break :lib b.addStaticLibrary(.{ + .name = "zodium", + .root_module = libzodium_mod, + .strip = true, + }); + } + }; libzodium.linkLibC(); b.installArtifact(libzodium); minisign.root_module.addCMacro("LIBZODIUM", "1"); minisign.linkLibrary(libzodium); } else { + minisign.addLibraryPath(.{ .cwd_relative = "/opt/homebrew/lib" }); + minisign.addLibraryPath(.{ .cwd_relative = "/usr/local/lib" }); minisign.root_module.linkSystemLibrary( "sodium", .{ diff --git a/src/libzodium/libzodium.zig b/src/libzodium/libzodium.zig index c5c8ec9..72f1912 100644 --- a/src/libzodium/libzodium.zig +++ b/src/libzodium/libzodium.zig @@ -75,7 +75,7 @@ export fn crypto_generichash_final( } export fn crypto_sign_keypair(pk: [*c]u8, sk: [*c]u8) callconv(.C) c_int { - const kp = Ed25519.KeyPair.generate(); + const kp = if (std.meta.hasFn(Ed25519.KeyPair, "generate")) Ed25519.KeyPair.generate() else (Ed25519.KeyPair.create(null) catch return -1); pk[0..32].* = kp.public_key.toBytes(); sk[0..64].* = kp.secret_key.toBytes(); return 0;