-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
137 lines (120 loc) · 4.77 KB
/
build.zig
File metadata and controls
137 lines (120 loc) · 4.77 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Detect target OS — works for both native and cross-compilation.
// target.query.os_tag is null for native builds, so fall back to the
// resolved target's os.tag.
const resolved_os = if (target.query.os_tag) |os| os else target.result.os.tag;
const resolved_abi = if (target.query.abi) |abi| abi else target.result.abi;
const is_android = resolved_os == .linux and resolved_abi == .android;
const is_ios = resolved_os == .ios;
const is_windows = resolved_os == .windows;
const is_macos = resolved_os == .macos;
// ─── FFI module (for mobile embedding — not built on Windows) ───
if (!is_windows) {
const ffi_mod = b.createModule(.{
.root_source_file = b.path("src/meshguard_ffi.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = if (is_android) true else null,
});
const ffi_lib = b.addLibrary(.{
.name = "meshguard-ffi",
.root_module = ffi_mod,
// iOS requires static linking — apps cannot dlopen user dylibs.
.linkage = if (is_ios) .static else .dynamic,
});
// Link libsodium on Linux desktop targets for AVX2-accelerated crypto.
// On Android, macOS, iOS, and Windows, the Zig std.crypto software fallback is used.
if (!is_android and !is_macos and !is_ios) {
ffi_lib.linkSystemLibrary("sodium");
}
b.installArtifact(ffi_lib);
}
// ─── Targets below only build for native (not Android/iOS) ───
if (!is_android and !is_ios) {
// ─── Root modules ───
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const lib_mod = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
// ─── Main executable ───
const exe = b.addExecutable(.{
.name = "meshguard",
.root_module = exe_mod,
});
// Link libsodium on Linux desktop only (AVX2 ChaCha20-Poly1305 assembly)
// macOS and Windows use std.crypto
if (!is_windows and !is_macos) {
exe.linkSystemLibrary("sodium");
}
// On Windows, link ws2_32 for Winsock2 sockets
if (is_windows) {
exe.linkSystemLibrary("ws2_32");
}
b.installArtifact(exe);
// On Windows, bundle wintun.dll alongside meshguard.exe
if (is_windows) {
b.installBinFile("deps/wintun/wintun.dll", "wintun.dll");
}
// ─── WG interop test binary (Linux only — requires kernel WG) ───
if (!is_windows and !is_macos) {
const interop_mod = b.createModule(.{
.root_source_file = b.path("src/wg_interop.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const interop_exe = b.addExecutable(.{
.name = "wg-interop-test",
.root_module = interop_mod,
});
interop_exe.linkSystemLibrary("sodium");
b.installArtifact(interop_exe);
}
// ─── Run step ───
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run meshguard");
run_step.dependOn(&run_cmd.step);
// ─── Static library (for embedding) ───
const lib = b.addLibrary(.{
.name = "meshguard",
.root_module = lib_mod,
.linkage = .static,
});
b.installArtifact(lib);
// ─── Unit tests ───
const test_mod = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const unit_tests = b.addTest(.{
.root_module = test_mod,
});
if (!is_windows and !is_macos) {
unit_tests.linkSystemLibrary("sodium");
}
if (is_windows) {
unit_tests.linkSystemLibrary("ws2_32");
}
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}
}