Allow compilation with Zig 0.13 as well as 0.14

Fixes #150
This commit is contained in:
Frank Denis 2025-01-16 00:54:22 +01:00
parent c165362385
commit 101e90a668
2 changed files with 29 additions and 11 deletions

View file

@ -1,3 +1,4 @@
const builtin = @import("builtin");
const std = @import("std"); const std = @import("std");
pub fn build(b: *std.Build) !void { pub fn build(b: *std.Build) !void {
@ -15,21 +16,38 @@ pub fn build(b: *std.Build) !void {
}); });
minisign.linkLibC(); minisign.linkLibC();
if (use_libzodium) { if (use_libzodium) {
const libzodium_mod = b.createModule(.{ var libzodium = lib: {
.root_source_file = b.path("src/libzodium/libzodium.zig"), if (builtin.zig_version.major == 0 and builtin.zig_version.minor < 13) {
.target = target, @compileError("Building requires Zig 0.13.0 or later");
.optimize = optimize, }
}); if (builtin.zig_version.major == 0 and builtin.zig_version.minor == 13) {
const libzodium = b.addStaticLibrary(.{ break :lib b.addStaticLibrary(.{
.name = "zodium", .name = "zodium",
.root_module = libzodium_mod, .strip = true,
.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(); libzodium.linkLibC();
b.installArtifact(libzodium); b.installArtifact(libzodium);
minisign.root_module.addCMacro("LIBZODIUM", "1"); minisign.root_module.addCMacro("LIBZODIUM", "1");
minisign.linkLibrary(libzodium); minisign.linkLibrary(libzodium);
} else { } else {
minisign.addLibraryPath(.{ .cwd_relative = "/opt/homebrew/lib" });
minisign.addLibraryPath(.{ .cwd_relative = "/usr/local/lib" });
minisign.root_module.linkSystemLibrary( minisign.root_module.linkSystemLibrary(
"sodium", "sodium",
.{ .{

View file

@ -75,7 +75,7 @@ export fn crypto_generichash_final(
} }
export fn crypto_sign_keypair(pk: [*c]u8, sk: [*c]u8) callconv(.C) c_int { 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(); pk[0..32].* = kp.public_key.toBytes();
sk[0..64].* = kp.secret_key.toBytes(); sk[0..64].* = kp.secret_key.toBytes();
return 0; return 0;