2022-04-28 21:21:39 -04:00
|
|
|
const std = @import("std");
|
|
|
|
const fmt = std.fmt;
|
|
|
|
const fs = std.fs;
|
|
|
|
const heap = std.heap;
|
|
|
|
const mem = std.mem;
|
|
|
|
const LibExeObjStep = std.build.LibExeObjStep;
|
2022-11-27 16:42:04 -05:00
|
|
|
const Target = std.Target;
|
2022-04-28 21:21:39 -04:00
|
|
|
|
|
|
|
pub fn build(b: *std.build.Builder) !void {
|
|
|
|
const src_path = "src/libsodium";
|
2022-10-20 08:42:14 -04:00
|
|
|
const src_dir = try fs.Dir.openIterableDir(fs.cwd(), src_path, .{ .no_follow = true });
|
2022-04-28 21:21:39 -04:00
|
|
|
|
2022-12-17 15:36:15 -05:00
|
|
|
const target = b.standardTargetOptions(.{});
|
|
|
|
const mode = b.standardReleaseOptions();
|
|
|
|
|
|
|
|
const enable_benchmarks = b.option(bool, "enable_benchmarks", "Whether tests should be benchmarks.") orelse false;
|
|
|
|
const benchmarks_iterations = b.option(u32, "iterations", "Number of iterations for benchmarks.") orelse 200;
|
2022-04-28 21:21:39 -04:00
|
|
|
|
2023-01-18 15:00:48 -05:00
|
|
|
const shared = b.addSharedLibrary(
|
|
|
|
if (target.isWindows()) "sodium_shared" else "sodium",
|
|
|
|
null,
|
|
|
|
.unversioned,
|
|
|
|
);
|
2022-04-28 21:21:39 -04:00
|
|
|
const static = b.addStaticLibrary("sodium", null);
|
2022-12-17 14:17:50 -05:00
|
|
|
shared.strip = true;
|
2023-01-18 15:00:48 -05:00
|
|
|
static.strip = true;
|
2022-06-07 11:35:41 -04:00
|
|
|
|
2023-01-18 15:00:48 -05:00
|
|
|
const libs_ = [_]*LibExeObjStep{ shared, static };
|
|
|
|
const libs = if (target.getOsTag() == .wasi) libs_[1..] else libs_[0..];
|
2022-04-28 21:21:39 -04:00
|
|
|
|
|
|
|
const prebuilt_version_file_path = "builds/msvc/version.h";
|
|
|
|
const version_file_path = "include/sodium/version.h";
|
|
|
|
|
2022-10-20 08:42:14 -04:00
|
|
|
if (src_dir.dir.access(version_file_path, .{ .mode = .read_only })) {} else |_| {
|
|
|
|
try fs.cwd().copyFile(prebuilt_version_file_path, src_dir.dir, version_file_path, .{});
|
2022-04-28 21:21:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (libs) |lib| {
|
|
|
|
lib.setTarget(target);
|
|
|
|
lib.setBuildMode(mode);
|
|
|
|
lib.install();
|
|
|
|
if (mode != .Debug) {
|
|
|
|
lib.strip = true;
|
|
|
|
}
|
|
|
|
lib.linkLibC();
|
|
|
|
|
2022-10-20 08:42:14 -04:00
|
|
|
lib.addIncludePath("src/libsodium/include/sodium");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("_GNU_SOURCE", "1");
|
2022-04-28 21:21:39 -04:00
|
|
|
lib.defineCMacro("CONFIGURED", "1");
|
|
|
|
lib.defineCMacro("DEV_MODE", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("HAVE_ATOMIC_OPS", "1");
|
|
|
|
lib.defineCMacro("HAVE_C11_MEMORY_FENCES", "1");
|
|
|
|
lib.defineCMacro("HAVE_GCC_MEMORY_FENCES", "1");
|
2022-04-28 21:21:39 -04:00
|
|
|
lib.defineCMacro("HAVE_INLINE_ASM", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("HAVE_INTTYPES_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_STDINT_H", "1");
|
2022-04-28 21:21:39 -04:00
|
|
|
lib.defineCMacro("HAVE_TI_MODE", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
|
|
|
|
if (target.cpu_arch) |arch| {
|
|
|
|
switch (arch.endian()) {
|
|
|
|
.Big => lib.defineCMacro("NATIVE_BIG_ENDIAN", "1"),
|
|
|
|
.Little => lib.defineCMacro("NATIVE_LITTLE_ENDIAN", "1"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (target.getOsTag()) {
|
|
|
|
.linux => {
|
|
|
|
lib.defineCMacro("ASM_HIDE_SYMBOL", ".hidden");
|
|
|
|
lib.defineCMacro("TLS", "_Thread_local");
|
|
|
|
|
|
|
|
lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1");
|
|
|
|
lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1");
|
2023-02-15 13:42:08 -05:00
|
|
|
lib.defineCMacro("HAVE_CLOCK_GETTIME", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("HAVE_GETPID", "1");
|
|
|
|
lib.defineCMacro("HAVE_INLINE_ASM", "1");
|
|
|
|
lib.defineCMacro("HAVE_MADVISE", "1");
|
|
|
|
lib.defineCMacro("HAVE_MLOCK", "1");
|
|
|
|
lib.defineCMacro("HAVE_MMAP", "1");
|
|
|
|
lib.defineCMacro("HAVE_MPROTECT", "1");
|
|
|
|
lib.defineCMacro("HAVE_NANOSLEEP", "1");
|
|
|
|
lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1");
|
|
|
|
lib.defineCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1");
|
|
|
|
lib.defineCMacro("HAVE_PTHREAD", "1");
|
|
|
|
lib.defineCMacro("HAVE_RAISE", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYSCONF", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_AUXV_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_MMAN_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_PARAM_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_RANDOM_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_WEAK_SYMBOLS", "1");
|
|
|
|
},
|
|
|
|
.windows => {
|
|
|
|
lib.defineCMacro("HAVE_RAISE", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_PARAM_H", "1");
|
|
|
|
},
|
|
|
|
.macos => {
|
|
|
|
lib.defineCMacro("ASM_HIDE_SYMBOL", ".private_extern");
|
|
|
|
lib.defineCMacro("TLS", "_Thread_local");
|
|
|
|
|
|
|
|
lib.defineCMacro("HAVE_ARC4RANDOM", "1");
|
|
|
|
lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1");
|
|
|
|
lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1");
|
|
|
|
lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1");
|
2023-02-15 13:42:08 -05:00
|
|
|
lib.defineCMacro("HAVE_CLOCK_GETTIME", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("HAVE_GETENTROPY", "1");
|
|
|
|
lib.defineCMacro("HAVE_GETPID", "1");
|
|
|
|
lib.defineCMacro("HAVE_MADVISE", "1");
|
|
|
|
lib.defineCMacro("HAVE_MEMSET_S", "1");
|
|
|
|
lib.defineCMacro("HAVE_MLOCK", "1");
|
|
|
|
lib.defineCMacro("HAVE_MMAP", "1");
|
|
|
|
lib.defineCMacro("HAVE_MPROTECT", "1");
|
|
|
|
lib.defineCMacro("HAVE_NANOSLEEP", "1");
|
|
|
|
lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1");
|
|
|
|
lib.defineCMacro("HAVE_PTHREAD", "1");
|
|
|
|
lib.defineCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1");
|
|
|
|
lib.defineCMacro("HAVE_RAISE", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYSCONF", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_MMAN_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_PARAM_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_RANDOM_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_WEAK_SYMBOLS", "1");
|
|
|
|
},
|
|
|
|
.wasi => {
|
|
|
|
lib.defineCMacro("HAVE_ARC4RANDOM", "1");
|
|
|
|
lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1");
|
2023-02-15 13:42:08 -05:00
|
|
|
lib.defineCMacro("HAVE_CLOCK_GETTIME", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("HAVE_GETENTROPY", "1");
|
|
|
|
lib.defineCMacro("HAVE_NANOSLEEP", "1");
|
|
|
|
lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_AUXV_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_PARAM_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_SYS_RANDOM_H", "1");
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
2022-04-28 21:21:39 -04:00
|
|
|
|
|
|
|
switch (target.getCpuArch()) {
|
|
|
|
.x86_64 => {
|
|
|
|
lib.defineCMacro("HAVE_AMD64_ASM", "1");
|
|
|
|
lib.defineCMacro("HAVE_AVX_ASM", "1");
|
|
|
|
lib.defineCMacro("HAVE_CPUID", "1");
|
|
|
|
lib.defineCMacro("HAVE_MMINTRIN_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_EMMINTRIN_H", "1");
|
|
|
|
lib.defineCMacro("HAVE_PMMINTRIN_H", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("HAVE_SMMINTRIN_H", "1");
|
2022-11-27 16:42:04 -05:00
|
|
|
lib.defineCMacro("HAVE_TMMINTRIN_H", "1");
|
2022-11-27 15:10:10 -05:00
|
|
|
lib.defineCMacro("HAVE_WMMINTRIN_H", "1");
|
2022-04-28 21:21:39 -04:00
|
|
|
},
|
|
|
|
.aarch64, .aarch64_be => {
|
2022-11-13 14:50:27 -05:00
|
|
|
const cpu_features = target.getCpuFeatures();
|
2022-11-27 16:42:04 -05:00
|
|
|
const has_neon = cpu_features.isEnabled(@enumToInt(Target.aarch64.Feature.neon));
|
|
|
|
const has_crypto = cpu_features.isEnabled(@enumToInt(Target.aarch64.Feature.crypto));
|
2022-11-13 14:50:27 -05:00
|
|
|
if (has_neon and has_crypto) {
|
|
|
|
lib.defineCMacro("HAVE_ARMCRYPTO", "1");
|
|
|
|
}
|
2022-04-28 21:21:39 -04:00
|
|
|
},
|
|
|
|
.wasm32, .wasm64 => {
|
|
|
|
lib.defineCMacro("__wasm__", "1");
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (target.getOsTag()) {
|
|
|
|
.wasi => {
|
|
|
|
lib.defineCMacro("__wasi__", "1");
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
2022-11-27 16:42:04 -05:00
|
|
|
switch (target.getCpuArch()) {
|
|
|
|
.x86_64 => {
|
|
|
|
lib.target.cpu_features_add.addFeature(@enumToInt(Target.x86.Feature.sse4_1));
|
|
|
|
lib.target.cpu_features_add.addFeature(@enumToInt(Target.x86.Feature.aes));
|
|
|
|
lib.target.cpu_features_add.addFeature(@enumToInt(Target.x86.Feature.pclmul));
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
2022-04-28 21:21:39 -04:00
|
|
|
var allocator = heap.page_allocator;
|
|
|
|
var walker = try src_dir.walk(allocator);
|
|
|
|
while (try walker.next()) |entry| {
|
|
|
|
const name = entry.basename;
|
|
|
|
if (mem.endsWith(u8, name, ".c")) {
|
|
|
|
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
|
|
|
|
lib.addCSourceFiles(&.{full_path}, &.{
|
|
|
|
"-fvisibility=hidden",
|
|
|
|
"-fno-strict-aliasing",
|
|
|
|
"-fno-strict-overflow",
|
|
|
|
"-fwrapv",
|
|
|
|
"-flax-vector-conversions",
|
|
|
|
});
|
|
|
|
} else if (mem.endsWith(u8, name, ".S")) {
|
|
|
|
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
|
|
|
|
lib.addAssemblyFile(full_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-23 15:07:55 -05:00
|
|
|
|
|
|
|
const test_path = "test/default";
|
|
|
|
const out_bin_path = "zig-out/bin";
|
|
|
|
const test_dir = try fs.Dir.openIterableDir(fs.cwd(), test_path, .{ .no_follow = true });
|
|
|
|
fs.Dir.makePath(fs.cwd(), out_bin_path) catch {};
|
|
|
|
const out_bin_dir = try fs.Dir.openDir(fs.cwd(), out_bin_path, .{});
|
2022-11-23 15:32:10 -05:00
|
|
|
try test_dir.dir.copyFile("run.sh", out_bin_dir, "run.sh", .{});
|
2022-11-23 15:07:55 -05:00
|
|
|
var allocator = heap.page_allocator;
|
|
|
|
var walker = try test_dir.walk(allocator);
|
|
|
|
while (try walker.next()) |entry| {
|
|
|
|
const name = entry.basename;
|
|
|
|
if (mem.endsWith(u8, name, ".exp")) {
|
|
|
|
try test_dir.dir.copyFile(name, out_bin_dir, name, .{});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!mem.endsWith(u8, name, ".c")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const exe_name = name[0 .. name.len - 2];
|
|
|
|
var exe = b.addExecutable(exe_name, null);
|
|
|
|
exe.setTarget(target);
|
|
|
|
exe.setBuildMode(mode);
|
|
|
|
exe.linkLibC();
|
2022-11-27 15:49:13 -05:00
|
|
|
exe.want_lto = false;
|
|
|
|
exe.strip = true;
|
2022-11-23 15:07:55 -05:00
|
|
|
exe.linkLibrary(static);
|
|
|
|
exe.addIncludePath("src/libsodium/include");
|
|
|
|
exe.addIncludePath("test/quirks");
|
|
|
|
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ test_path, entry.path });
|
|
|
|
exe.addCSourceFiles(&.{full_path}, &.{});
|
2022-12-17 15:36:15 -05:00
|
|
|
|
|
|
|
if (enable_benchmarks) {
|
|
|
|
exe.defineCMacro("BENCHMARKS", "1");
|
|
|
|
var buf: [16]u8 = undefined;
|
|
|
|
exe.defineCMacro("ITERATIONS", std.fmt.bufPrintIntToSlice(&buf, benchmarks_iterations, 10, .lower, .{}));
|
|
|
|
}
|
|
|
|
|
2022-11-23 15:07:55 -05:00
|
|
|
exe.install();
|
|
|
|
}
|
2022-04-28 21:21:39 -04:00
|
|
|
}
|