-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.zig
More file actions
365 lines (331 loc) · 15.3 KB
/
build.zig
File metadata and controls
365 lines (331 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
const std = @import("std");
const fmt = std.fmt;
const heap = std.heap;
const mem = std.mem;
const Compile = std.Build.Step.Compile;
const Target = std.Target;
// Zig 0.16+ uses std.Io.Dir, 0.15 uses std.fs
const is_zig_16 = @hasDecl(std, "Io") and @hasDecl(std.Io, "Dir");
const Dir = if (is_zig_16) std.Io.Dir else std.fs.Dir;
const Io = if (is_zig_16) std.Io else void;
fn initLibConfig(b: *std.Build, target: std.Build.ResolvedTarget, lib: *Compile) void {
lib.root_module.link_libc = true;
lib.lto = null;
lib.root_module.addIncludePath(b.path("src/libsodium/include/sodium"));
lib.root_module.addCMacro("_GNU_SOURCE", "1");
lib.root_module.addCMacro("CONFIGURED", "1");
lib.root_module.addCMacro("DEV_MODE", "1");
lib.root_module.addCMacro("HAVE_ATOMIC_OPS", "1");
lib.root_module.addCMacro("HAVE_C11_MEMORY_FENCES", "1");
lib.root_module.addCMacro("HAVE_CET_H", "1");
lib.root_module.addCMacro("HAVE_GCC_MEMORY_FENCES", "1");
lib.root_module.addCMacro("HAVE_INLINE_ASM", "1");
lib.root_module.addCMacro("HAVE_INTTYPES_H", "1");
lib.root_module.addCMacro("HAVE_STDINT_H", "1");
lib.root_module.addCMacro("HAVE_TI_MODE", "1");
const endian = target.result.cpu.arch.endian();
switch (endian) {
.big => lib.root_module.addCMacro("NATIVE_BIG_ENDIAN", "1"),
.little => lib.root_module.addCMacro("NATIVE_LITTLE_ENDIAN", "1"),
}
switch (target.result.os.tag) {
.linux => {
lib.root_module.addCMacro("ASM_HIDE_SYMBOL", ".hidden");
lib.root_module.addCMacro("TLS", "_Thread_local");
lib.root_module.addCMacro("HAVE_CATCHABLE_ABRT", "1");
lib.root_module.addCMacro("HAVE_CATCHABLE_SEGV", "1");
lib.root_module.addCMacro("HAVE_CLOCK_GETTIME", "1");
lib.root_module.addCMacro("HAVE_GETPID", "1");
lib.root_module.addCMacro("HAVE_MADVISE", "1");
lib.root_module.addCMacro("HAVE_MLOCK", "1");
lib.root_module.addCMacro("HAVE_MMAP", "1");
lib.root_module.addCMacro("HAVE_MPROTECT", "1");
lib.root_module.addCMacro("HAVE_NANOSLEEP", "1");
lib.root_module.addCMacro("HAVE_POSIX_MEMALIGN", "1");
lib.root_module.addCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1");
lib.root_module.addCMacro("HAVE_PTHREAD", "1");
lib.root_module.addCMacro("HAVE_RAISE", "1");
lib.root_module.addCMacro("HAVE_SYSCONF", "1");
lib.root_module.addCMacro("HAVE_SYS_AUXV_H", "1");
lib.root_module.addCMacro("HAVE_SYS_MMAN_H", "1");
lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1");
lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1");
lib.root_module.addCMacro("HAVE_WEAK_SYMBOLS", "1");
},
.windows => {
lib.root_module.addCMacro("HAVE_RAISE", "1");
lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1");
if (lib.isStaticLibrary()) {
lib.root_module.addCMacro("SODIUM_STATIC", "1");
}
},
.macos => {
lib.root_module.addCMacro("ASM_HIDE_SYMBOL", ".private_extern");
lib.root_module.addCMacro("TLS", "_Thread_local");
lib.root_module.addCMacro("HAVE_ARC4RANDOM", "1");
lib.root_module.addCMacro("HAVE_ARC4RANDOM_BUF", "1");
lib.root_module.addCMacro("HAVE_CATCHABLE_ABRT", "1");
lib.root_module.addCMacro("HAVE_CATCHABLE_SEGV", "1");
lib.root_module.addCMacro("HAVE_CLOCK_GETTIME", "1");
lib.root_module.addCMacro("HAVE_GETENTROPY", "1");
lib.root_module.addCMacro("HAVE_GETPID", "1");
lib.root_module.addCMacro("HAVE_MADVISE", "1");
lib.root_module.addCMacro("HAVE_MEMSET_S", "1");
lib.root_module.addCMacro("HAVE_MLOCK", "1");
lib.root_module.addCMacro("HAVE_MMAP", "1");
lib.root_module.addCMacro("HAVE_MPROTECT", "1");
lib.root_module.addCMacro("HAVE_NANOSLEEP", "1");
lib.root_module.addCMacro("HAVE_POSIX_MEMALIGN", "1");
lib.root_module.addCMacro("HAVE_PTHREAD", "1");
lib.root_module.addCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1");
lib.root_module.addCMacro("HAVE_RAISE", "1");
lib.root_module.addCMacro("HAVE_SYSCONF", "1");
lib.root_module.addCMacro("HAVE_SYS_MMAN_H", "1");
lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1");
lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1");
lib.root_module.addCMacro("HAVE_WEAK_SYMBOLS", "1");
},
.wasi => {
lib.root_module.addCMacro("HAVE_ARC4RANDOM", "1");
lib.root_module.addCMacro("HAVE_ARC4RANDOM_BUF", "1");
lib.root_module.addCMacro("HAVE_CLOCK_GETTIME", "1");
lib.root_module.addCMacro("HAVE_GETENTROPY", "1");
lib.root_module.addCMacro("HAVE_NANOSLEEP", "1");
lib.root_module.addCMacro("HAVE_POSIX_MEMALIGN", "1");
lib.root_module.addCMacro("HAVE_SYS_AUXV_H", "1");
lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1");
lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1");
},
.freebsd => {
lib.root_module.addCMacro("ASM_HIDE_SYMBOL", ".hidden");
lib.root_module.addCMacro("TLS", "_Thread_local");
lib.root_module.addCMacro("HAVE_ARC4RANDOM", "1");
lib.root_module.addCMacro("HAVE_ARC4RANDOM_BUF", "1");
lib.root_module.addCMacro("HAVE_CATCHABLE_ABRT", "1");
lib.root_module.addCMacro("HAVE_CATCHABLE_SEGV", "1");
lib.root_module.addCMacro("HAVE_CLOCK_GETTIME", "1");
lib.root_module.addCMacro("HAVE_GETPID", "1");
lib.root_module.addCMacro("HAVE_MADVISE", "1");
lib.root_module.addCMacro("HAVE_MLOCK", "1");
lib.root_module.addCMacro("HAVE_MMAP", "1");
lib.root_module.addCMacro("HAVE_MPROTECT", "1");
lib.root_module.addCMacro("HAVE_NANOSLEEP", "1");
lib.root_module.addCMacro("HAVE_POSIX_MEMALIGN", "1");
lib.root_module.addCMacro("HAVE_PTHREAD_PRIO_INHERIT", "1");
lib.root_module.addCMacro("HAVE_PTHREAD", "1");
lib.root_module.addCMacro("HAVE_RAISE", "1");
lib.root_module.addCMacro("HAVE_SYSCONF", "1");
lib.root_module.addCMacro("HAVE_SYS_MMAN_H", "1");
lib.root_module.addCMacro("HAVE_SYS_PARAM_H", "1");
lib.root_module.addCMacro("HAVE_SYS_RANDOM_H", "1");
lib.root_module.addCMacro("HAVE_WEAK_SYMBOLS", "1");
},
else => {},
}
switch (target.result.cpu.arch) {
.x86_64 => {
switch (target.result.os.tag) {
.windows => {},
else => {
lib.root_module.addCMacro("HAVE_AMD64_ASM", "1");
lib.root_module.addCMacro("HAVE_AVX_ASM", "1");
},
}
lib.root_module.addCMacro("HAVE_CPUID", "1");
lib.root_module.addCMacro("HAVE_MMINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_EMMINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_PMMINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_TMMINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_SMMINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_AVXINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_AVX2INTRIN_H", "1");
lib.root_module.addCMacro("HAVE_AVX512FINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_WMMINTRIN_H", "1");
lib.root_module.addCMacro("HAVE_RDRAND", "1");
},
.aarch64, .aarch64_be => {
lib.root_module.addCMacro("HAVE_ARMCRYPTO", "1");
},
.wasm32, .wasm64 => {
lib.root_module.addCMacro("__wasm__", "1");
},
else => {},
}
switch (target.result.os.tag) {
.wasi => {
lib.root_module.addCMacro("__wasi__", "1");
},
else => {},
}
}
pub fn build(b: *std.Build) !void {
const io: Io = if (is_zig_16) b.graph.io else {};
const cwd = if (is_zig_16) Dir.cwd() else std.fs.cwd();
const src_path = "src/libsodium";
const src_dir = if (is_zig_16)
try cwd.openDir(io, src_path, .{ .iterate = true })
else if (@hasField(Dir.OpenOptions, "follow_symlinks"))
try cwd.openDir(src_path, .{ .iterate = true, .follow_symlinks = false })
else
try cwd.openDir(src_path, .{ .iterate = true, .no_follow = true });
var target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
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;
var build_static = b.option(bool, "static", "Build libsodium as a static library.") orelse true;
var build_shared = b.option(bool, "shared", "Build libsodium as a shared library.") orelse true;
const build_tests = b.option(bool, "test", "Build the tests (implies -Dstatic=true)") orelse true;
if (target.result.cpu.arch.isWasm()) {
build_shared = false;
}
if (build_tests) {
build_static = true;
}
switch (target.result.cpu.arch) {
.aarch64, .aarch64_be => {
// ARM CPUs supported by Windows are assumed to have NEON support
if (target.result.isMinGW()) {
target.query.cpu_features_add.addFeature(@intFromEnum(Target.aarch64.Feature.neon));
}
},
else => {},
}
const static_lib = b.addLibrary(.{
.name = if (target.result.isMinGW()) "libsodium-static" else "sodium",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}),
});
static_lib.pie = true;
const shared_lib = b.addLibrary(.{
.name = if (target.result.isMinGW()) "libsodium" else "sodium",
.linkage = .dynamic,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.strip = optimize != .Debug and !target.result.isMinGW(),
}),
});
// work out which libraries we are building
var libs = std.ArrayList(*Compile){};
defer libs.deinit(heap.page_allocator);
if (build_static) {
try libs.append(heap.page_allocator, static_lib);
}
if (build_shared) {
try libs.append(heap.page_allocator, shared_lib);
}
const prebuilt_version_file_path = "builds/msvc/version.h";
const version_file_path = "include/sodium/version.h";
if (is_zig_16) {
src_dir.access(io, version_file_path, .{}) catch {
try Dir.copyFile(cwd, prebuilt_version_file_path, src_dir, version_file_path, io, .{});
};
} else if (@hasField(Dir.OpenOptions, "follow_symlinks")) {
src_dir.access(version_file_path, .{ .read = true }) catch {
try cwd.copyFile(prebuilt_version_file_path, src_dir, version_file_path, .{});
};
} else {
src_dir.access(version_file_path, .{ .mode = .read_only }) catch {
try cwd.copyFile(prebuilt_version_file_path, src_dir, version_file_path, .{});
};
}
for (libs.items) |lib| {
b.installArtifact(lib);
lib.installHeader(b.path(src_path ++ "/include/sodium.h"), "sodium.h");
lib.installHeadersDirectory(b.path(src_path ++ "/include/sodium"), "sodium", .{});
initLibConfig(b, target, lib);
const flags = &.{
"-fvisibility=hidden",
"-fno-strict-aliasing",
"-fno-strict-overflow",
"-fwrapv",
"-flax-vector-conversions",
"-Werror=vla",
};
const allocator = heap.page_allocator;
var walker = try src_dir.walk(allocator);
while (if (is_zig_16) try walker.next(io) else 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.root_module.addCSourceFiles(.{
.files = &.{full_path},
.flags = flags,
});
} else if (mem.endsWith(u8, name, ".S")) {
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
lib.root_module.addAssemblyFile(b.path(full_path));
}
}
}
const test_path = "test/default";
const out_bin_path = "zig-out/bin";
const test_dir = if (is_zig_16)
try cwd.openDir(io, test_path, .{ .iterate = true })
else if (@hasField(Dir.OpenOptions, "follow_symlinks"))
try cwd.openDir(test_path, .{ .iterate = true, .follow_symlinks = false })
else
try cwd.openDir(test_path, .{ .iterate = true, .no_follow = true });
if (is_zig_16) {
cwd.createDirPath(io, out_bin_path) catch {};
} else {
cwd.makePath(out_bin_path) catch {};
}
const out_bin_dir = if (is_zig_16)
try cwd.openDir(io, out_bin_path, .{})
else
try cwd.openDir(out_bin_path, .{});
if (is_zig_16) {
try Dir.copyFile(test_dir, "run.sh", out_bin_dir, "run.sh", io, .{});
} else {
try test_dir.copyFile("run.sh", out_bin_dir, "run.sh", .{});
}
const allocator = heap.page_allocator;
var walker = try test_dir.walk(allocator);
const test_step = b.step("test", "Run all libsodium tests");
if (build_tests) {
while (if (is_zig_16) try walker.next(io) else try walker.next()) |entry| {
const name = entry.basename;
if (mem.endsWith(u8, name, ".exp")) {
if (is_zig_16) {
try Dir.copyFile(test_dir, name, out_bin_dir, name, io, .{});
} else {
try test_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(.{
.name = exe_name,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.strip = true,
.link_libc = true,
}),
});
exe.root_module.linkLibrary(static_lib);
exe.root_module.addIncludePath(b.path("src/libsodium/include"));
exe.root_module.addIncludePath(b.path("test/quirks"));
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ test_path, entry.path });
exe.root_module.addCSourceFiles(.{ .files = &.{full_path} });
if (enable_benchmarks) {
exe.root_module.addCMacro("BENCHMARKS", "1");
var buf: [16]u8 = undefined;
const n = std.fmt.bufPrint(&buf, "{d}", .{benchmarks_iterations}) catch unreachable;
exe.root_module.addCMacro("ITERATIONS", n);
}
b.installArtifact(exe);
// Add a run step for this test
const run_test = b.addRunArtifact(exe);
run_test.setCwd(b.path(test_path));
test_step.dependOn(&run_test.step);
}
}
}