-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.zig
395 lines (377 loc) · 15.9 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
const std = @import("std");
const builtin = @import("builtin");
const Builder = std.build.Builder;
const Module = std.build.Module;
const GitRepoStep = @import("GitRepoStep.zig");
const loggyrunstep = @import("loggyrunstep.zig");
pub fn build(b: *Builder) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const is_ci = if (b.option(bool, "is_ci", "is the CI")) |o| o else false;
const build_all_step = b.step("all", "Build ziget with all the 'enabled' backends");
const nossl_exe = addExe(b, target, optimize, null, build_all_step, is_ci);
var ssl_exes: [ssl_backends.len]*std.build.CompileStep = undefined;
inline for (ssl_backends, 0..) |field, i| {
const enum_value = @field(SslBackend, field.name);
ssl_exes[i] = addExe(b, target, optimize, enum_value, build_all_step, is_ci);
}
const test_all_step = b.step("test", "Run all the 'Enabled' tests");
addTest(b, test_all_step, "nossl", nossl_exe, null, is_ci);
inline for (ssl_backends, 0..) |field, i| {
const enum_value = @field(SslBackend, field.name);
addTest(b, test_all_step, field.name, ssl_exes[i], enum_value, is_ci);
}
// by default, install the std ssl backend
const default_exe = ssl_exes[@intFromEnum(SslBackend.iguana)];
b.installArtifact(default_exe);
const run_cmd = b.addRunArtifact(default_exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run ziget with the iguana backend");
run_step.dependOn(&run_cmd.step);
}
fn getEnabledByDefault(optional_ssl_backend: ?SslBackend, is_ci: bool) bool {
return if (optional_ssl_backend) |backend| switch (backend) {
.std => true,
.iguana => true,
.schannel => false, // schannel not supported yet
// TODO: also enable for macos
.opensslstatic => (builtin.os.tag == .linux),
// TODO: also enable for macos
.openssl => (builtin.os.tag == .linux and !is_ci), // zig is having trouble with the openssh headers on the CI
} else true;
}
fn addExe(
b: *Builder,
target: std.zig.CrossTarget,
optimize: std.builtin.Mode,
comptime optional_ssl_backend: ?SslBackend,
build_all_step: *std.build.Step,
is_ci: bool,
) *std.build.CompileStep {
const info: struct { name: []const u8, exe_suffix: []const u8 } = comptime if (optional_ssl_backend) |backend| .{
.name = @tagName(backend),
.exe_suffix = if (backend == .iguana) "" else ("-" ++ @tagName(backend)),
} else .{
.name = "nossl",
.exe_suffix = "-nossl",
};
const exe = b.addExecutable(.{
.name = "ziget" ++ info.exe_suffix,
.root_source_file = .{ .path = "ziget-cmdline.zig" },
.target = target,
.optimize = optimize,
});
exe.single_threaded = true;
addZigetModule(exe, optional_ssl_backend, ".");
const install = b.addInstallArtifact(exe, .{});
const enabled_by_default = getEnabledByDefault(optional_ssl_backend, is_ci);
if (enabled_by_default) {
build_all_step.dependOn(&install.step);
}
const abled_suffix: []const u8 = if (enabled_by_default) "" else " (DISABLED BY DEFAULT)";
b.step(info.name, b.fmt("Build ziget with the {s} backend{s}", .{
info.name,
abled_suffix,
})).dependOn(&install.step);
return exe;
}
fn addTest(
b: *Builder,
test_all_step: *std.build.Step,
comptime backend_name: []const u8,
exe: *std.build.CompileStep,
optional_ssl_backend: ?SslBackend,
is_ci: bool,
) void {
const enabled_by_default = getEnabledByDefault(optional_ssl_backend, is_ci);
const abled_suffix: []const u8 = if (enabled_by_default) "" else " (DISABLED BY DEFAULT)";
const test_backend_step = b.step(
"test-" ++ backend_name,
b.fmt("Test the {s} backend{s}", .{ backend_name, abled_suffix }),
);
{
const run = b.addRunArtifact(exe);
run.addArg("http://google.com");
loggyrunstep.enable(run);
test_backend_step.dependOn(&run.step);
}
if (optional_ssl_backend) |_| {
{
const run = b.addRunArtifact(exe);
run.addArg("http://ziglang.org"); // NOTE: ziglang.org will redirect to HTTPS
loggyrunstep.enable(run);
test_backend_step.dependOn(&run.step);
}
{
const run = b.addRunArtifact(exe);
run.addArg("https://ziglang.org");
loggyrunstep.enable(run);
test_backend_step.dependOn(&run.step);
}
} else {
const run = b.addRunArtifact(exe);
run.addArg("google.com");
loggyrunstep.enable(run);
test_backend_step.dependOn(&run.step);
}
if (getEnabledByDefault(optional_ssl_backend, is_ci)) {
test_all_step.dependOn(test_backend_step);
}
}
pub const SslBackend = enum {
std,
openssl,
opensslstatic,
iguana,
schannel,
};
pub const ssl_backends = @typeInfo(SslBackend).Enum.fields;
///! Adds the ziget package to the given compile step.
///! This function will add the necessary include directories, libraries, etc to be able to
///! include ziget and it's SSL backend dependencies into the given compile.
pub fn addZigetModule(
compile: *std.build.CompileStep,
optional_ssl_backend: ?SslBackend,
ziget_repo: []const u8,
) void {
const b = compile.step.owner;
const ziget_index = b.pathJoin(&.{ ziget_repo, "ziget.zig" });
const ssl_module = if (optional_ssl_backend) |backend|
addSslBackend(compile, backend, ziget_repo)
else
b.createModule(.{ .source_file = .{ .path = "nossl/ssl.zig" } });
compile.addAnonymousModule("ziget", .{
.source_file = .{ .path = ziget_index },
.dependencies = &[_]std.Build.ModuleDependency{
.{ .name = "ssl", .module = ssl_module },
},
});
}
fn addSslBackend(compile: *std.build.CompileStep, backend: SslBackend, ziget_repo: []const u8) *Module {
const b = compile.step.owner;
switch (backend) {
.std => return b.createModule(.{
.source_file = .{ .path = b.pathJoin(&.{ ziget_repo, "stdssl.zig" }) },
}),
.openssl => {
compile.linkSystemLibrary("c");
if (builtin.os.tag == .windows) {
compile.linkSystemLibrary("libcrypto");
compile.linkSystemLibrary("libssl");
setupOpensslWindows(compile);
} else {
compile.linkSystemLibrary("crypto");
compile.linkSystemLibrary("ssl");
}
return b.createModule(.{ .source_file = .{ .path = b.pathJoin(&.{ ziget_repo, "openssl", "ssl.zig" }) } });
},
.opensslstatic => {
const openssl_repo = GitRepoStep.create(b, .{
.url = "https://github.com/openssl/openssl",
.branch = "OpenSSL_1_1_1j",
.sha = "52c587d60be67c337364b830dd3fdc15404a2f04",
});
// TODO: should we implement something to cache the configuration?
// can the configure output be in a different directory?
{
const configure_openssl = std.build.RunStep.create(b, "configure openssl");
configure_openssl.step.dependOn(&openssl_repo.step);
configure_openssl.cwd = openssl_repo.getPath(&configure_openssl.step);
configure_openssl.addArgs(&[_][]const u8{
"./config",
// just a temporary path for now
//"--openssl",
//"/tmp/ziget-openssl-static-dir1",
"-static",
// just disable everything for now
"no-threads",
"no-shared",
"no-asm",
"no-sse2",
"no-aria",
"no-bf",
"no-camellia",
"no-cast",
"no-des",
"no-dh",
"no-dsa",
"no-ec",
"no-idea",
"no-md2",
"no-mdc2",
"no-rc2",
"no-rc4",
"no-rc5",
"no-seed",
"no-sm2",
"no-sm3",
"no-sm4",
});
configure_openssl.addCheck(.{
.expect_stdout_match = "OpenSSL has been successfully configured",
});
const make_openssl = std.build.RunStep.create(b, "configure openssl");
make_openssl.cwd = configure_openssl.cwd;
make_openssl.addArgs(&[_][]const u8{
"make",
"include/openssl/opensslconf.h",
"include/crypto/bn_conf.h",
"include/crypto/dso_conf.h",
});
make_openssl.step.dependOn(&configure_openssl.step);
compile.step.dependOn(&make_openssl.step);
}
const openssl_repo_path_for_step = openssl_repo.getPath(&compile.step);
compile.addIncludePath(.{ .path = openssl_repo_path_for_step });
compile.addIncludePath(.{ .path = b.pathJoin(&.{ openssl_repo_path_for_step, "include" }) });
compile.addIncludePath(.{ .path = b.pathJoin(&.{ openssl_repo_path_for_step, "crypto", "modes" }) });
const cflags = &[_][]const u8{
"-Wall",
// TODO: is this the right way to do this? is it a config option?
"-DOPENSSL_NO_ENGINE",
// TODO: --openssldir doesn't seem to be setting this?
"-DOPENSSLDIR=\"/tmp/ziget-openssl-static-dir2\"",
};
{
const sources = @embedFile("openssl/sources");
var source_lines = std.mem.split(u8, sources, "\n");
while (source_lines.next()) |src| {
if (src.len == 0 or src[0] == '#') continue;
compile.addCSourceFile(.{
.file = .{ .path = b.pathJoin(&.{ openssl_repo_path_for_step, src }) },
.flags = cflags,
});
}
}
compile.linkLibC();
return b.createModule(.{
.source_file = .{ .path = b.pathJoin(&.{ ziget_repo, "openssl", "ssl.zig" }) },
});
},
.iguana => {
const iguana_repo = GitRepoStep.create(b, .{
.url = "https://github.com/marler8997/iguanaTLS",
.branch = null,
.sha = "91d22df192d1df1022352df49f41c1a90ca8327d",
.fetch_enabled = true,
});
compile.step.dependOn(&iguana_repo.step);
const iguana_repo_path = iguana_repo.getPath(&compile.step);
const iguana_mod = b.addModule("iguanaTLS", .{
.source_file = .{
.path = b.pathJoin(&.{ iguana_repo_path, "src", "main.zig" }),
},
});
return b.createModule(.{
.source_file = .{
.path = b.pathJoin(&.{ ziget_repo, "iguana", "ssl.zig" }),
},
.dependencies = &[_]std.Build.ModuleDependency{
.{ .name = "iguana", .module = iguana_mod },
},
});
},
.schannel => {
{
// NOTE: for now I'm using msspi from https://github.com/deemru/msspi
// I'll probably port this to Zig at some point
// Once I do remove this build config
// NOTE: I tested using this commit: 7338760a4a2c6fb80c47b24a2abba32d5fc40635 tagged at version 0.1.42
const msspi_repo = GitRepoStep.create(b, .{
.url = "https://github.com/deemru/msspi",
.branch = "0.1.42",
.sha = "7338760a4a2c6fb80c47b24a2abba32d5fc40635",
});
compile.step.dependOn(&msspi_repo.step);
const msspi_repo_path = msspi_repo.getPath(&compile.step);
const msspi_src_dir = b.pathJoin(&.{ msspi_repo_path, "src" });
const msspi_main_cpp = b.pathJoin(&.{ msspi_src_dir, "msspi.cpp" });
const msspi_third_party_include = b.pathJoin(&.{ msspi_repo_path, "third_party", "cprocsp", "include" });
compile.addCSourceFile(.{
.file = .{ .path = msspi_main_cpp },
.flags = &[_][]const u8{},
});
compile.addIncludePath(.{ .path = msspi_src_dir });
compile.addIncludePath(.{ .path = msspi_third_party_include });
compile.linkLibC();
compile.linkSystemLibrary("ws2_32");
compile.linkSystemLibrary("crypt32");
compile.linkSystemLibrary("advapi32");
}
// TODO: this will be needed if/when msspi is ported to Zig
//const zigwin32_index_file = try getGitRepoFile(b.allocator,
// "https://github.com/marlersoft/zigwin32",
// "src" ++ std.fs.path.sep_str ++ "win32.zig");
return b.createModule(.{
.source_file = .{ .path = b.pathJoin(&.{ ziget_repo, "schannel", "ssl.zig" }) },
//.dependencies = &[_]Module {
// .{ .name = "win32", .source = .{ .path = zigwin32_index_file } },
//},
});
},
}
}
const OpensslPathOption = struct {
// NOTE: I can't use ??[]const u8 because it exposes a bug in the compiler
is_cached: bool = false,
cached: ?[]const u8 = undefined,
fn get(self: *OpensslPathOption, b: *std.build.Builder) ?[]const u8 {
if (!self.is_cached) {
self.cached = b.option(
[]const u8,
"openssl-path",
"path to openssl (for Windows)",
);
self.is_cached = true;
}
std.debug.assert(self.is_cached);
return self.cached;
}
};
var global_openssl_path_option = OpensslPathOption{};
pub fn setupOpensslWindows(compile: *std.build.CompileStep) void {
const b = compile.step.owner;
const openssl_path = global_openssl_path_option.get(b) orelse {
compile.step.dependOn(&FailStep.create(b, "missing openssl-path", "-Dopenssl on windows requires -Dopenssl-path=DIR to be specified").step);
return;
};
// NOTE: right now these files are hardcoded to the files expected when installing SSL via
// this web page: https://slproweb.com/products/Win32OpenSSL.html and installed using
// this exe installer: https://slproweb.com/download/Win64OpenSSL-1_1_1g.exe
compile.addIncludePath(.{ .path = b.pathJoin(&.{ openssl_path, "include" }) });
compile.addLibraryPath(.{ .path = b.pathJoin(&.{ openssl_path, "lib" }) });
// install dlls to the same directory as executable
for ([_][]const u8{ "libcrypto-1_1-x64.dll", "libssl-1_1-x64.dll" }) |dll| {
compile.step.dependOn(&b.addInstallFileWithDir(
.{ .path = b.pathJoin(&.{ openssl_path, dll }) },
.bin,
dll,
).step);
}
}
const FailStep = struct {
step: std.build.Step,
fail_msg: []const u8,
pub fn create(b: *Builder, name: []const u8, fail_msg: []const u8) *FailStep {
var result = b.allocator.create(FailStep) catch unreachable;
result.* = .{
.step = std.build.Step.init(.{
.id = .custom,
.name = name,
.owner = b,
.makeFn = make,
}),
.fail_msg = fail_msg,
};
return result;
}
fn make(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
_ = prog_node;
const self = @fieldParentPtr(FailStep, "step", step);
std.log.err("{s}", .{self.fail_msg});
std.os.exit(0xff);
}
};