build.zig (11877B)
1 const std = @import("std"); 2 const fmt = std.fmt; 3 const fs = std.fs; 4 const heap = std.heap; 5 const mem = std.mem; 6 const Compile = std.Build.Step.Compile; 7 const Target = std.Target; 8 9 fn initLibConfig(b: *std.Build, target: std.Build.ResolvedTarget, lib: *Compile) void { 10 lib.linkLibC(); 11 lib.addIncludePath(b.path("src/libsodium/include/sodium")); 12 lib.root_module.addCMacro("_GNU_SOURCE", "1"); 13 lib.root_module.addCMacro("CONFIGURED", "1"); 14 lib.root_module.addCMacro("DEV_MODE", "1"); 15 lib.root_module.addCMacro("HAVE_ATOMIC_OPS", "1"); 16 lib.root_module.addCMacro("HAVE_C11_MEMORY_FENCES", "1"); 17 lib.root_module.addCMacro("HAVE_CET_H", "1"); 18 lib.root_module.addCMacro("HAVE_GCC_MEMORY_FENCES", "1"); 19 lib.root_module.addCMacro("HAVE_INLINE_ASM", "1"); 20 lib.root_module.addCMacro("HAVE_INTTYPES_H", "1"); 21 lib.root_module.addCMacro("HAVE_STDINT_H", "1"); 22 lib.root_module.addCMacro("HAVE_TI_MODE", "1"); 23 lib.want_lto = false; 24 25 const endian = target.result.cpu.arch.endian(); 26 switch (endian) { 27 .big => lib.root_module.addCMacro("NATIVE_BIG_ENDIAN", "1"), 28 .little => lib.root_module.addCMacro("NATIVE_LITTLE_ENDIAN", "1"), 29 } 30 31 switch (target.result.os.tag) { 32 .linux => { 33 lib.root_module.addCMacro("ASM_HIDE_SYMBOL", ".hidden"); 34 lib.root_module.addCMacro("TLS", "_Thread_local"); 35 36 lib.root_module.addCMacro("HAVE_CATCHABLE_ABRT", "1"); 37 lib.root_module.addCMacro("HAVE_CATCHABLE_SEGV", "1"); 38 lib.root_module.addCMacro("HAVE_CLOCK_GETTIME", "1"); 39 lib.root_module.addCMacro("HAVE_GETPID", "1"); 40 lib.root_module.addCMacro("HAVE_MADVISE", "1"); 41 lib.root_module.addCMacro("HAVE_MLOCK", "1"); 42 lib.root_module.addCMacro("HAVE_MMAP", "1"); 43 lib.root_module.addCMacro("HAVE_MPROTECT", "1"); 44 lib.root_module.addCMacro("HAVE_NANOSLEEP", "1"); 45 lib.root_module.addCMacro("HAVE_POSIX_MEMALIGN", "1"); 46 lib.root_module.addCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1"); 47 lib.root_module.addCMacro("HAVE_PTHREAD", "1"); 48 lib.root_module.addCMacro("HAVE_RAISE", "1"); 49 lib.root_module.addCMacro("HAVE_SYSCONF", "1"); 50 lib.root_module.addCMacro("HAVE_SYS_AUXV_H", "1"); 51 lib.root_module.addCMacro("HAVE_SYS_MMAN_H", "1"); 52 lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1"); 53 lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1"); 54 lib.root_module.addCMacro("HAVE_WEAK_SYMBOLS", "1"); 55 }, 56 .windows => { 57 lib.root_module.addCMacro("HAVE_RAISE", "1"); 58 lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1"); 59 if (lib.isStaticLibrary()) { 60 lib.root_module.addCMacro("SODIUM_STATIC", "1"); 61 } 62 }, 63 .macos => { 64 lib.root_module.addCMacro("ASM_HIDE_SYMBOL", ".private_extern"); 65 lib.root_module.addCMacro("TLS", "_Thread_local"); 66 67 lib.root_module.addCMacro("HAVE_ARC4RANDOM", "1"); 68 lib.root_module.addCMacro("HAVE_ARC4RANDOM_BUF", "1"); 69 lib.root_module.addCMacro("HAVE_CATCHABLE_ABRT", "1"); 70 lib.root_module.addCMacro("HAVE_CATCHABLE_SEGV", "1"); 71 lib.root_module.addCMacro("HAVE_CLOCK_GETTIME", "1"); 72 lib.root_module.addCMacro("HAVE_GETENTROPY", "1"); 73 lib.root_module.addCMacro("HAVE_GETPID", "1"); 74 lib.root_module.addCMacro("HAVE_MADVISE", "1"); 75 lib.root_module.addCMacro("HAVE_MEMSET_S", "1"); 76 lib.root_module.addCMacro("HAVE_MLOCK", "1"); 77 lib.root_module.addCMacro("HAVE_MMAP", "1"); 78 lib.root_module.addCMacro("HAVE_MPROTECT", "1"); 79 lib.root_module.addCMacro("HAVE_NANOSLEEP", "1"); 80 lib.root_module.addCMacro("HAVE_POSIX_MEMALIGN", "1"); 81 lib.root_module.addCMacro("HAVE_PTHREAD", "1"); 82 lib.root_module.addCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1"); 83 lib.root_module.addCMacro("HAVE_RAISE", "1"); 84 lib.root_module.addCMacro("HAVE_SYSCONF", "1"); 85 lib.root_module.addCMacro("HAVE_SYS_MMAN_H", "1"); 86 lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1"); 87 lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1"); 88 lib.root_module.addCMacro("HAVE_WEAK_SYMBOLS", "1"); 89 }, 90 .wasi => { 91 lib.root_module.addCMacro("HAVE_ARC4RANDOM", "1"); 92 lib.root_module.addCMacro("HAVE_ARC4RANDOM_BUF", "1"); 93 lib.root_module.addCMacro("HAVE_CLOCK_GETTIME", "1"); 94 lib.root_module.addCMacro("HAVE_GETENTROPY", "1"); 95 lib.root_module.addCMacro("HAVE_NANOSLEEP", "1"); 96 lib.root_module.addCMacro("HAVE_POSIX_MEMALIGN", "1"); 97 lib.root_module.addCMacro("HAVE_SYS_AUXV_H", "1"); 98 lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1"); 99 lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1"); 100 }, 101 else => {}, 102 } 103 104 switch (target.result.cpu.arch) { 105 .x86_64 => { 106 switch (target.result.os.tag) { 107 .windows => {}, 108 else => { 109 lib.root_module.addCMacro("HAVE_AMD64_ASM", "1"); 110 lib.root_module.addCMacro("HAVE_AVX_ASM", "1"); 111 }, 112 } 113 lib.root_module.addCMacro("HAVE_CPUID", "1"); 114 lib.root_module.addCMacro("HAVE_MMINTRIN_H", "1"); 115 lib.root_module.addCMacro("HAVE_EMMINTRIN_H", "1"); 116 lib.root_module.addCMacro("HAVE_PMMINTRIN_H", "1"); 117 lib.root_module.addCMacro("HAVE_TMMINTRIN_H", "1"); 118 lib.root_module.addCMacro("HAVE_SMMINTRIN_H", "1"); 119 lib.root_module.addCMacro("HAVE_AVXINTRIN_H", "1"); 120 lib.root_module.addCMacro("HAVE_AVX2INTRIN_H", "1"); 121 lib.root_module.addCMacro("HAVE_AVX512FINTRIN_H", "1"); 122 lib.root_module.addCMacro("HAVE_WMMINTRIN_H", "1"); 123 lib.root_module.addCMacro("HAVE_RDRAND", "1"); 124 }, 125 .aarch64, .aarch64_be => { 126 lib.root_module.addCMacro("HAVE_ARMCRYPTO", "1"); 127 }, 128 .wasm32, .wasm64 => { 129 lib.root_module.addCMacro("__wasm__", "1"); 130 }, 131 else => {}, 132 } 133 134 switch (target.result.os.tag) { 135 .wasi => { 136 lib.root_module.addCMacro("__wasi__", "1"); 137 }, 138 else => {}, 139 } 140 } 141 142 pub fn build(b: *std.Build) !void { 143 const root_path = b.pathFromRoot("."); 144 var cwd = try fs.openDirAbsolute(root_path, .{}); 145 defer cwd.close(); 146 147 const src_path = "src/libsodium"; 148 const src_dir = try fs.Dir.openDir(cwd, src_path, .{ .iterate = true, .no_follow = true }); 149 150 var target = b.standardTargetOptions(.{}); 151 const optimize = b.standardOptimizeOption(.{}); 152 153 const enable_benchmarks = b.option(bool, "enable_benchmarks", "Whether tests should be benchmarks.") orelse false; 154 const benchmarks_iterations = b.option(u32, "iterations", "Number of iterations for benchmarks.") orelse 200; 155 var build_static = b.option(bool, "static", "Build libsodium as a static library.") orelse true; 156 var build_shared = b.option(bool, "shared", "Build libsodium as a shared library.") orelse true; 157 158 const build_tests = b.option(bool, "test", "Build the tests (implies -Dstatic=true)") orelse true; 159 160 if (target.result.cpu.arch.isWasm()) { 161 build_shared = false; 162 } 163 if (build_tests) { 164 build_static = true; 165 } 166 167 switch (target.result.cpu.arch) { 168 .aarch64, .aarch64_be => { 169 // ARM CPUs supported by Windows are assumed to have NEON support 170 if (target.result.isMinGW()) { 171 target.query.cpu_features_add.addFeature(@intFromEnum(Target.aarch64.Feature.neon)); 172 } 173 }, 174 else => {}, 175 } 176 177 const static_lib = b.addStaticLibrary(.{ 178 .name = if (target.result.isMinGW()) "libsodium-static" else "sodium", 179 .target = target, 180 .optimize = optimize, 181 }); 182 const shared_lib = b.addSharedLibrary(.{ 183 .name = if (target.result.isMinGW()) "libsodium" else "sodium", 184 .target = target, 185 .optimize = optimize, 186 .strip = optimize != .Debug and !target.result.isMinGW(), 187 }); 188 189 // work out which libraries we are building 190 var libs = std.ArrayList(*Compile).init(b.allocator); 191 defer libs.deinit(); 192 if (build_static) { 193 try libs.append(static_lib); 194 } 195 if (build_shared) { 196 try libs.append(shared_lib); 197 } 198 199 const prebuilt_version_file_path = "builds/msvc/version.h"; 200 const version_file_path = "include/sodium/version.h"; 201 202 if (src_dir.access(version_file_path, .{ .mode = .read_only })) {} else |_| { 203 try cwd.copyFile(prebuilt_version_file_path, src_dir, version_file_path, .{}); 204 } 205 206 for (libs.items) |lib| { 207 b.installArtifact(lib); 208 lib.installHeader(b.path(src_path ++ "/include/sodium.h"), "sodium.h"); 209 lib.installHeadersDirectory(b.path(src_path ++ "/include/sodium"), "sodium", .{}); 210 211 initLibConfig(b, target, lib); 212 213 const flags = &.{ 214 "-fvisibility=hidden", 215 "-fno-strict-aliasing", 216 "-fno-strict-overflow", 217 "-fwrapv", 218 "-flax-vector-conversions", 219 "-Werror=vla", 220 }; 221 222 const allocator = heap.page_allocator; 223 224 var walker = try src_dir.walk(allocator); 225 while (try walker.next()) |entry| { 226 const name = entry.basename; 227 if (mem.endsWith(u8, name, ".c")) { 228 const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path }); 229 230 lib.addCSourceFiles(.{ 231 .files = &.{full_path}, 232 .flags = flags, 233 }); 234 } else if (mem.endsWith(u8, name, ".S")) { 235 const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path }); 236 lib.addAssemblyFile(b.path(full_path)); 237 } 238 } 239 } 240 241 const test_path = "test/default"; 242 const out_bin_path = "zig-out/bin"; 243 const test_dir = try fs.Dir.openDir(cwd, test_path, .{ .iterate = true, .no_follow = true }); 244 fs.Dir.makePath(cwd, out_bin_path) catch {}; 245 const out_bin_dir = try fs.Dir.openDir(cwd, out_bin_path, .{}); 246 try test_dir.copyFile("run.sh", out_bin_dir, "run.sh", .{}); 247 const allocator = heap.page_allocator; 248 var walker = try test_dir.walk(allocator); 249 if (build_tests) { 250 while (try walker.next()) |entry| { 251 const name = entry.basename; 252 if (mem.endsWith(u8, name, ".exp")) { 253 try test_dir.copyFile(name, out_bin_dir, name, .{}); 254 continue; 255 } 256 if (!mem.endsWith(u8, name, ".c")) { 257 continue; 258 } 259 const exe_name = name[0 .. name.len - 2]; 260 var exe = b.addExecutable(.{ 261 .name = exe_name, 262 .target = target, 263 .optimize = optimize, 264 .strip = true, 265 }); 266 exe.linkLibC(); 267 exe.linkLibrary(static_lib); 268 exe.addIncludePath(b.path("src/libsodium/include")); 269 exe.addIncludePath(b.path("test/quirks")); 270 const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ test_path, entry.path }); 271 exe.addCSourceFiles(.{ .files = &.{full_path} }); 272 if (enable_benchmarks) { 273 exe.root_module.addCMacro("BENCHMARKS", "1"); 274 var buf: [16]u8 = undefined; 275 exe.root_module.addCMacro("ITERATIONS", std.fmt.bufPrintIntToSlice(&buf, benchmarks_iterations, 10, .lower, .{})); 276 } 277 278 b.installArtifact(exe); 279 } 280 } 281 }