Skip to content

Commit 1e564a5

Browse files
committed
make mbedtls and openssl lazy dependencies
They're both lazy since Windows builds don't require either. Also fixed missing header errors when mbedtls isn't installed on the system.
1 parent 9539cf6 commit 1e564a5

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

build.zig

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ pub fn build(b: *std.Build) !void {
44
const target = b.standardTargetOptions(.{});
55
const optimize = b.standardOptimizeOption(.{});
66

7-
const mbedtls_dep = b.dependency("mbedtls", .{
8-
.target = target,
9-
.optimize = optimize,
10-
});
11-
127
const libgit_src = b.dependency("libgit2", .{});
138
const libgit_root = libgit_src.path(".");
149

@@ -46,6 +41,7 @@ pub fn build(b: *std.Build) !void {
4641
};
4742

4843
const openssl = b.option(bool, "enable-openssl", "Use OpenSSL instead of MbedTLS") orelse false;
44+
var tls_dep: ?*std.Build.Dependency = null;
4945

5046
if (target.result.os.tag == .windows) {
5147
if (openssl) {
@@ -75,9 +71,11 @@ pub fn build(b: *std.Build) !void {
7571
} else {
7672
if (openssl) {
7773
// OpenSSL backend
78-
const openssl_dep = b.dependency("openssl", .{});
79-
const openssl_lib = openssl_dep.artifact("openssl");
80-
lib.linkLibrary(openssl_lib);
74+
tls_dep = b.lazyDependency("openssl", .{
75+
.target = target,
76+
.optimize = optimize,
77+
});
78+
if (tls_dep) |tls| lib.linkLibrary(tls.artifact("openssl"));
8179
features.addValues(.{
8280
.GIT_HTTPS = 1,
8381
.GIT_OPENSSL = 1,
@@ -89,8 +87,12 @@ pub fn build(b: *std.Build) !void {
8987
.GIT_IO_SELECT = 1,
9088
});
9189
} else {
92-
// mbedTLS https and SHA backend
93-
lib.linkLibrary(mbedtls_dep.artifact("mbedtls"));
90+
// mbedTLS backend
91+
tls_dep = b.lazyDependency("mbedtls", .{
92+
.target = target,
93+
.optimize = optimize,
94+
});
95+
if (tls_dep) |tls| lib.linkLibrary(tls.artifact("mbedtls"));
9496
features.addValues(.{
9597
.GIT_HTTPS = 1,
9698
.GIT_MBEDTLS = 1,
@@ -112,11 +114,7 @@ pub fn build(b: *std.Build) !void {
112114
.link_libc = true,
113115
});
114116
ntlm.addIncludePath(libgit_src.path("deps/ntlmclient"));
115-
if (openssl) {
116-
addOpenSSLHeaders(ntlm);
117-
} else {
118-
ntlm.linkLibrary(mbedtls_dep.artifact("mbedtls"));
119-
}
117+
addTlsHeaders(ntlm, tls_dep, openssl);
120118

121119
const ntlm_cflags = .{
122120
"-Wno-implicit-fallthrough",
@@ -304,7 +302,7 @@ pub fn build(b: *std.Build) !void {
304302
cli.addIncludePath(libgit_src.path("include"));
305303
cli.addIncludePath(libgit_src.path("src/util"));
306304
cli.addIncludePath(libgit_src.path("src/cli"));
307-
if (openssl) addOpenSSLHeaders(cli);
305+
addTlsHeaders(cli, tls_dep, openssl);
308306

309307
if (target.result.os.tag == .windows)
310308
cli.addCSourceFiles(.{ .root = libgit_root, .files = &cli_win32_sources })
@@ -349,7 +347,7 @@ pub fn build(b: *std.Build) !void {
349347
});
350348

351349
exe.addIncludePath(libgit_src.path("include"));
352-
if (openssl) addOpenSSLHeaders(exe);
350+
addTlsHeaders(exe, tls_dep, openssl);
353351
exe.linkLibrary(lib);
354352

355353
// independent install step so you can easily access the binary
@@ -379,7 +377,7 @@ pub fn build(b: *std.Build) !void {
379377
tests.addConfigHeader(features);
380378
tests.addIncludePath(libgit_src.path("include"));
381379
tests.addIncludePath(libgit_src.path("src/util"));
382-
if (openssl) addOpenSSLHeaders(tests);
380+
addTlsHeaders(tests, tls_dep, openssl);
383381

384382
tests.linkLibrary(lib);
385383

@@ -388,11 +386,10 @@ pub fn build(b: *std.Build) !void {
388386
}
389387
}
390388

391-
fn addOpenSSLHeaders(compile: *std.Build.Step.Compile) void {
392-
const b = compile.step.owner;
393-
const openssl_dep = b.dependency("openssl", .{});
394-
const openssl_lib = openssl_dep.artifact("openssl");
395-
compile.addIncludePath(openssl_lib.getEmittedIncludeTree());
389+
fn addTlsHeaders(compile: *std.Build.Step.Compile, tls_dep: ?*std.Build.Dependency, openssl_or_mbedtls: bool) void {
390+
if (tls_dep) |tls| compile.addIncludePath(
391+
tls.artifact(if (openssl_or_mbedtls) "openssl" else "mbedtls").getEmittedIncludeTree(),
392+
);
396393
}
397394

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

build.zig.zon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
.openssl = .{
1111
.url = "https://github.com/allyourcodebase/openssl/archive/refs/tags/3.3.1-1.tar.gz",
1212
.hash = "12207c40cefa38fe90e4230dfba2e5c76b37e1ee36602512cad8ff0501f892002a65",
13+
.lazy = true,
1314
},
1415
.mbedtls = .{
1516
.url = "https://github.com/allyourcodebase/mbedtls/archive/40a2c1126b45f87d19b256229620bc2995637e5a.tar.gz",
1617
.hash = "122034bd019496a4e20a775116c53ef05e3247e9a98436b3407d4c0503fa3a16cd42",
18+
.lazy = true,
1719
},
1820
},
1921
.paths = .{

0 commit comments

Comments
 (0)