Skip to content

Commit ac411fd

Browse files
committed
rework TLS/SSL option
1 parent 63e6aed commit ac411fd

File tree

2 files changed

+72
-63
lines changed

2 files changed

+72
-63
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const libgit2_dep = b.dependency("libgit2", .{
1717
.target = target,
1818
.optimize = optimize,
1919
.@"enable-ssh" = true, // optional ssh support via libssh2
20-
.@"enable-openssl" = true, // use openssl instead of mbedtls
20+
.@"tls-backend" = .openssl, // use openssl instead of mbedtls
2121
});
2222
your_compile_step.linkLibrary(libgit_dep.artifact("git2"));
2323
```

build.zig

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ pub fn build(b: *std.Build) !void {
4040
"-fno-sanitize=undefined",
4141
};
4242

43-
const openssl = b.option(bool, "enable-openssl", "Use OpenSSL instead of MbedTLS") orelse false;
43+
// The TLS backend logic is only run for non windows builds.
4444
var tls_dep: ?*std.Build.Dependency = null;
45+
const tls_backend = b.option(
46+
TlsBackend,
47+
"tls-backend",
48+
"Choose Unix TLS/SSL backend (default is mbedtls)",
49+
) orelse .mbedtls;
4550

4651
if (target.result.os.tag == .windows) {
47-
if (openssl) {
48-
std.log.err("OpenSSL option unsupported on Windows", .{});
49-
return;
50-
}
51-
5252
lib.linkSystemLibrary("winhttp");
5353
lib.linkSystemLibrary("rpcrt4");
5454
lib.linkSystemLibrary("crypt32");
@@ -69,42 +69,43 @@ pub fn build(b: *std.Build) !void {
6969
lib.addWin32ResourceFile(.{ .file = libgit_src.path("src/libgit2/git2.rc") });
7070
lib.addCSourceFiles(.{ .root = libgit_root, .files = &util_win32_sources, .flags = &flags });
7171
} else {
72-
if (openssl) {
73-
// OpenSSL backend
74-
tls_dep = b.lazyDependency("openssl", .{
75-
.target = target,
76-
.optimize = optimize,
77-
});
78-
if (tls_dep) |tls| lib.linkLibrary(tls.artifact("openssl"));
79-
features.addValues(.{
80-
.GIT_HTTPS = 1,
81-
.GIT_OPENSSL = 1,
82-
83-
.GIT_SHA1_COLLISIONDETECT = 1,
84-
.GIT_SHA256_OPENSSL = 1,
85-
86-
.GIT_USE_FUTIMENS = 1,
87-
.GIT_IO_POLL = 1,
88-
.GIT_IO_SELECT = 1,
89-
});
90-
} else {
91-
// MbedTLS backend
92-
tls_dep = b.lazyDependency("mbedtls", .{
93-
.target = target,
94-
.optimize = optimize,
95-
});
96-
if (tls_dep) |tls| lib.linkLibrary(tls.artifact("mbedtls"));
97-
features.addValues(.{
98-
.GIT_HTTPS = 1,
99-
.GIT_MBEDTLS = 1,
100-
101-
.GIT_SHA1_COLLISIONDETECT = 1,
102-
.GIT_SHA256_MBEDTLS = 1,
103-
104-
.GIT_USE_FUTIMENS = 1,
105-
.GIT_IO_POLL = 1,
106-
.GIT_IO_SELECT = 1,
107-
});
72+
switch (tls_backend) {
73+
.openssl => {
74+
tls_dep = b.lazyDependency("openssl", .{
75+
.target = target,
76+
.optimize = optimize,
77+
});
78+
if (tls_dep) |tls| lib.linkLibrary(tls.artifact("openssl"));
79+
features.addValues(.{
80+
.GIT_HTTPS = 1,
81+
.GIT_OPENSSL = 1,
82+
83+
.GIT_SHA1_COLLISIONDETECT = 1,
84+
.GIT_SHA256_OPENSSL = 1,
85+
86+
.GIT_USE_FUTIMENS = 1,
87+
.GIT_IO_POLL = 1,
88+
.GIT_IO_SELECT = 1,
89+
});
90+
},
91+
.mbedtls => {
92+
tls_dep = b.lazyDependency("mbedtls", .{
93+
.target = target,
94+
.optimize = optimize,
95+
});
96+
if (tls_dep) |tls| lib.linkLibrary(tls.artifact("mbedtls"));
97+
features.addValues(.{
98+
.GIT_HTTPS = 1,
99+
.GIT_MBEDTLS = 1,
100+
101+
.GIT_SHA1_COLLISIONDETECT = 1,
102+
.GIT_SHA256_MBEDTLS = 1,
103+
104+
.GIT_USE_FUTIMENS = 1,
105+
.GIT_IO_POLL = 1,
106+
.GIT_IO_SELECT = 1,
107+
});
108+
},
108109
}
109110

110111
// ntlmclient
@@ -116,16 +117,16 @@ pub fn build(b: *std.Build) !void {
116117
.link_libc = true,
117118
});
118119
ntlm.addIncludePath(libgit_src.path("deps/ntlmclient"));
119-
addTlsHeaders(ntlm, tls_dep, openssl);
120+
maybeAddTlsIncludes(ntlm, tls_dep, tls_backend);
120121

121122
const ntlm_cflags = .{
122123
"-Wno-implicit-fallthrough",
123124
"-DNTLM_STATIC=1",
124125
"-DUNICODE_BUILTIN=1",
125-
if (openssl)
126-
"-DCRYPT_OPENSSL"
127-
else
128-
"-DCRYPT_MBEDTLS",
126+
switch (tls_backend) {
127+
.openssl => "-DCRYPT_OPENSSL",
128+
.mbedtls => "-DCRYPT_MBEDTLS",
129+
},
129130
};
130131
ntlm.addCSourceFiles(.{
131132
.root = libgit_root,
@@ -134,10 +135,9 @@ pub fn build(b: *std.Build) !void {
134135
});
135136
ntlm.addCSourceFiles(.{
136137
.root = libgit_root,
137-
.files = if (openssl) &.{
138-
"deps/ntlmclient/crypt_openssl.c",
139-
} else &.{
140-
"deps/ntlmclient/crypt_mbedtls.c",
138+
.files = switch (tls_backend) {
139+
.openssl => &.{"deps/ntlmclient/crypt_openssl.c"},
140+
.mbedtls => &.{"deps/ntlmclient/crypt_mbedtls.c"},
141141
},
142142
.flags = &ntlm_cflags,
143143
});
@@ -154,10 +154,9 @@ pub fn build(b: *std.Build) !void {
154154
});
155155
lib.addCSourceFiles(.{
156156
.root = libgit_root,
157-
.files = if (openssl) &.{
158-
"src/util/hash/openssl.c",
159-
} else &.{
160-
"src/util/hash/mbedtls.c",
157+
.files = switch (tls_backend) {
158+
.openssl => &.{"src/util/hash/openssl.c"},
159+
.mbedtls => &.{"src/util/hash/mbedtls.c"},
161160
},
162161
.flags = &flags,
163162
});
@@ -310,7 +309,7 @@ pub fn build(b: *std.Build) !void {
310309
cli.addIncludePath(libgit_src.path("include"));
311310
cli.addIncludePath(libgit_src.path("src/util"));
312311
cli.addIncludePath(libgit_src.path("src/cli"));
313-
addTlsHeaders(cli, tls_dep, openssl);
312+
maybeAddTlsIncludes(cli, tls_dep, tls_backend);
314313

315314
if (target.result.os.tag == .windows)
316315
cli.addCSourceFiles(.{ .root = libgit_root, .files = &cli_win32_sources })
@@ -355,7 +354,7 @@ pub fn build(b: *std.Build) !void {
355354
});
356355

357356
exe.addIncludePath(libgit_src.path("include"));
358-
addTlsHeaders(exe, tls_dep, openssl);
357+
maybeAddTlsIncludes(exe, tls_dep, tls_backend);
359358
exe.linkLibrary(lib);
360359

361360
// independent install step so you can easily access the binary
@@ -385,7 +384,7 @@ pub fn build(b: *std.Build) !void {
385384
tests.addConfigHeader(features);
386385
tests.addIncludePath(libgit_src.path("include"));
387386
tests.addIncludePath(libgit_src.path("src/util"));
388-
addTlsHeaders(tests, tls_dep, openssl);
387+
maybeAddTlsIncludes(tests, tls_dep, tls_backend);
389388

390389
tests.linkLibrary(lib);
391390

@@ -394,10 +393,20 @@ pub fn build(b: *std.Build) !void {
394393
}
395394
}
396395

397-
fn addTlsHeaders(compile: *std.Build.Step.Compile, tls_dep: ?*std.Build.Dependency, openssl_or_mbedtls: bool) void {
398-
if (tls_dep) |tls| compile.addIncludePath(
399-
tls.artifact(if (openssl_or_mbedtls) "openssl" else "mbedtls").getEmittedIncludeTree(),
400-
);
396+
const TlsBackend = enum { openssl, mbedtls };
397+
398+
fn maybeAddTlsIncludes(
399+
compile: *std.Build.Step.Compile,
400+
dep: ?*std.Build.Dependency,
401+
backend: TlsBackend,
402+
) void {
403+
if (dep) |tls| {
404+
const name = switch (backend) {
405+
.openssl => "openssl",
406+
.mbedtls => "mbedtls",
407+
};
408+
compile.addIncludePath(tls.artifact(name).getEmittedIncludeTree());
409+
}
401410
}
402411

403412
const libgit_sources = [_][]const u8{

0 commit comments

Comments
 (0)