-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
303 lines (263 loc) · 11.1 KB
/
build.zig
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
const std = @import("std");
const utils = @import("src/internal.zig");
const EventLoopT = utils.EventLoopT;
const SslT = utils.SslT;
const Compile = std.Build.Step.Compile;
const Dependency = std.Build.Dependency;
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// ssl options
const ssl_type = getSslT(.{
.use_openssl = b.option(bool, "USE_OPENSSL", "indicates whether zSockets will use openssl") orelse false,
.use_wolfssl = b.option(bool, "USE_WOLFSSL", "indicates whether zSockets will use wolfssl") orelse false,
.use_boringssl = b.option(bool, "USE_BORINGSSL", "indicates whether zSockets will use boringssl") orelse false,
});
// event loop options
var event_opts: EventLoopOpts = .{
.use_io_uring = b.option(bool, "USE_IO_URING", "indicates whether zSockets will use io_uring") orelse false,
.use_epoll = b.option(bool, "USE_EPOLL", "indicates whether zSockets will use epoll") orelse false,
.use_libuv = b.option(bool, "USE_LIBUV", "indicates whether zSockets will use libuv") orelse false,
.use_gcd = b.option(bool, "USE_GCD", "indicates whether zSockets will use GCD") orelse false,
.use_kqueue = b.option(bool, "USE_KQUEUE", "indicates whether zSockets will use kqueue") orelse false,
.use_asio = b.option(bool, "USE_ASIO", "indicates whether zSockets will use asio") orelse false,
};
// if no opts are set, default based on target
if (!event_opts.use_io_uring and !event_opts.use_epoll and !event_opts.use_libuv and !event_opts.use_gcd and !event_opts.use_kqueue and !event_opts.use_asio) {
const os_tag = target.result.os.tag;
if (os_tag == .windows) {
event_opts.use_libuv = true;
} else if (os_tag.isDarwin() or os_tag.isBSD()) {
event_opts.use_kqueue = true;
} else {
event_opts.use_epoll = true;
}
}
const event_type = getEventLoopT(event_opts);
const use_quic = b.option(bool, "USE_QUIC", "indicates whether zSockets will use QUIC") orelse false;
var shared_opts = b.addOptions();
shared_opts.addOption(SslT, "ssl_lib", ssl_type);
shared_opts.addOption(EventLoopT, "event_loop_lib", event_type);
shared_opts.addOption(bool, "USE_QUIC", use_quic);
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
// SSL/crypto dependencies
const ssl_deps: SslDeps = .{
.openssl = if (ssl_type == .openssl) b.dependency("openssl", .{ .target = target, .optimize = optimize }) else undefined,
.wolfssl = if (ssl_type == .wolfssl) b.dependency("wolfssl", .{ .target = target, .optimize = optimize }) else undefined,
.boringssl = if (ssl_type == .boringssl) b.dependency("boringssl", .{ .target = target, .optimize = optimize }) else undefined,
};
// Event loop dependencies
const asio: *std.Build.Dependency = if (event_type == .asio) b.dependency("asio", .{ .target = target, .optimize = optimize }) else undefined;
const lsquic: *std.Build.Dependency = if (use_quic) b.dependency("lsquic", .{ .target = target, .optimize = optimize }) else undefined;
const lib = b.addSharedLibrary(.{
.name = "zSockets",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/zsockets.zig"),
.target = target,
.optimize = optimize,
});
lib.linkLibC();
lib.addIncludePath(b.path("include"));
linkSsl(lib, ssl_type, ssl_deps);
if (event_type == .asio) {
const libasio = asio.artifact("asio");
lib.linkLibrary(libasio);
lib.installLibraryHeaders(libasio);
}
if (use_quic) {
const liblsquic = lsquic.artifact("lsquic");
lib.linkLibrary(liblsquic);
lib.installLibraryHeaders(liblsquic);
}
lib.root_module.addOptions("build_opts", shared_opts);
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/zsockets.zig"),
.target = target,
.optimize = optimize,
});
lib_unit_tests.linkLibC();
linkSsl(lib_unit_tests, ssl_type, ssl_deps);
if (event_type == .asio) {
const libasio = asio.artifact("asio");
lib_unit_tests.linkLibrary(libasio);
lib_unit_tests.installLibraryHeaders(libasio);
}
if (use_quic) {
const liblsquic = lsquic.artifact("lsquic");
lib_unit_tests.linkLibrary(liblsquic);
lib_unit_tests.installLibraryHeaders(liblsquic);
}
lib_unit_tests.root_module.addOptions("build_opts", shared_opts);
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
// examples/echo_server.zig
const echo_server_exe = b.addExecutable(.{
.name = "echo_server",
.root_source_file = b.path("examples/echo_server.zig"),
.target = target,
.optimize = optimize,
});
echo_server_exe.linkLibC();
echo_server_exe.root_module.addImport("zSockets", &lib.root_module);
const run_echo_server = b.addRunArtifact(echo_server_exe);
const run_echo_server_step = b.step("echo_server", "Run the echo server demo");
run_echo_server_step.dependOn(&run_echo_server.step);
// examples/hammer_test.zig
const hammer_test_exe = b.addExecutable(.{
.name = "hammer_test",
.root_source_file = b.path("examples/hammer_test.zig"),
.target = target,
.optimize = optimize,
});
hammer_test_exe.linkLibC();
hammer_test_exe.root_module.addImport("zSockets", &lib.root_module);
const run_hammer_test = b.addRunArtifact(hammer_test_exe);
const run_hammer_test_step = b.step("hammer_test", "Hammer test the echo server");
run_hammer_test_step.dependOn(&run_hammer_test.step);
// examples/http_load_test.zig
const http_load_test_exe = b.addExecutable(.{
.name = "http_load_test",
.root_source_file = b.path("examples/http_load_test.zig"),
.target = target,
.optimize = optimize,
});
http_load_test_exe.linkLibC();
http_load_test_exe.root_module.addImport("zSockets", &lib.root_module);
const run_http_load_test = b.addRunArtifact(http_load_test_exe);
if (b.args) |args| {
run_http_load_test.addArgs(args);
}
const run_http_load_test_step = b.step("http_load_test", "Run the http load test demo");
run_http_load_test_step.dependOn(&run_http_load_test.step);
// examples/tcp_load_test.zig
const tcp_load_test_exe = b.addExecutable(.{
.name = "tcp_load_test",
.root_source_file = b.path("examples/tcp_load_test.zig"),
.target = target,
.optimize = optimize,
});
tcp_load_test_exe.linkLibC();
tcp_load_test_exe.root_module.addImport("zSockets", &lib.root_module);
const run_tcp_load_test = b.addRunArtifact(tcp_load_test_exe);
if (b.args) |args| {
run_tcp_load_test.addArgs(args);
}
const run_tcp_load_test_step = b.step("tcp_load_test", "Run the tcp load test demo");
run_tcp_load_test_step.dependOn(&run_tcp_load_test.step);
// examples/http_server.zig
const http_server_exe = b.addExecutable(.{
.name = "http_server",
.root_source_file = b.path("examples/http_server.zig"),
.target = target,
.optimize = optimize,
});
http_server_exe.linkLibC();
http_server_exe.root_module.addImport("zSockets", &lib.root_module);
const run_http_server = b.addRunArtifact(http_server_exe);
if (b.args) |args| {
run_http_server.addArgs(args);
}
const run_http_server_step = b.step("http_server", "Run the HTTP server demo");
run_http_server_step.dependOn(&run_http_server.step);
// examples/tcp_server.zig
const tcp_server_exe = b.addExecutable(.{
.name = "tcp_server",
.root_source_file = b.path("examples/tcp_server.zig"),
.target = target,
.optimize = optimize,
});
tcp_server_exe.linkLibC();
tcp_server_exe.root_module.addImport("zSockets", &lib.root_module);
const run_tcp_server = b.addRunArtifact(tcp_server_exe);
if (b.args) |args| {
run_tcp_server.addArgs(args);
}
const run_tcp_server_step = b.step("tcp_server", "Run the TCP server demo");
run_tcp_server_step.dependOn(&run_tcp_server.step);
}
const SslOpts = struct {
use_openssl: bool,
use_wolfssl: bool,
use_boringssl: bool,
};
pub fn getSslT(opts: SslOpts) SslT {
return if (opts.use_boringssl)
.boringssl
else if (opts.use_openssl)
.openssl
else if (opts.use_wolfssl)
.wolfssl
else
.nossl;
}
const EventLoopOpts = struct {
use_io_uring: bool,
use_epoll: bool,
use_libuv: bool,
use_gcd: bool,
use_kqueue: bool,
use_asio: bool,
};
pub fn getEventLoopT(opts: EventLoopOpts) EventLoopT {
return if (opts.use_io_uring)
.io_uring
else if (opts.use_epoll)
.epoll
else if (opts.use_libuv)
.libuv
else if (opts.use_gcd)
.gcd
else if (opts.use_kqueue)
.kqueue
else
.asio;
}
const SslDeps = struct {
boringssl: *Dependency,
openssl: *Dependency,
wolfssl: *Dependency,
};
pub fn linkSsl(c: *Compile, ssl_type: SslT, deps: SslDeps) void {
switch (ssl_type) {
.boringssl => {
const libssl = deps.boringssl.artifact("ssl");
const libcrypto = deps.boringssl.artifact("crypto");
c.linkLibrary(libssl);
c.linkLibrary(libcrypto);
c.installLibraryHeaders(libssl);
},
.openssl => {
const libssl = deps.openssl.artifact("ssl");
const libcrypto = deps.openssl.artifact("crypto");
c.linkLibrary(libssl);
c.linkLibrary(libcrypto);
c.installLibraryHeaders(libssl);
},
.wolfssl => {
const libssl = deps.wolfssl.artifact("wolfssl");
c.linkLibrary(libssl);
c.installLibraryHeaders(libssl);
},
else => {},
}
}