Skip to content

Commit df5e267

Browse files
committed
add openssl
1 parent ea0e03d commit df5e267

File tree

2 files changed

+80
-23
lines changed

2 files changed

+80
-23
lines changed

build.zig

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

43+
const openssl = switch (target.result.os.tag) {
44+
.linux => b.option(bool, "enable-openssl", "Enable OpenSSL support") orelse false,
45+
else => false,
46+
};
47+
4348
if (target.result.os.tag == .windows) {
4449
lib.linkSystemLibrary("winhttp");
4550
lib.linkSystemLibrary("rpcrt4");
@@ -61,20 +66,37 @@ pub fn build(b: *std.Build) !void {
6166
lib.addWin32ResourceFile(.{ .file = libgit_src.path("src/libgit2/git2.rc") });
6267
lib.addCSourceFiles(.{ .root = libgit_root, .files = &util_win32_sources, .flags = &flags });
6368
} else {
64-
// mbedTLS https and SHA backend
65-
lib.linkSystemLibrary("mbedtls");
66-
lib.linkSystemLibrary("mbedcrypto");
67-
lib.linkSystemLibrary("mbedx509");
68-
features.addValues(.{
69-
.GIT_HTTPS = 1,
70-
.GIT_MBEDTLS = 1,
71-
.GIT_SHA1_MBEDTLS = 1,
72-
.GIT_SHA256_MBEDTLS = 1,
73-
74-
.GIT_USE_FUTIMENS = 1,
75-
.GIT_IO_POLL = 1,
76-
.GIT_IO_SELECT = 1,
77-
});
69+
if (openssl) {
70+
// OpenSSL backend
71+
const openssl_dep = b.dependency("openssl", .{});
72+
const openssl_lib = openssl_dep.artifact("openssl");
73+
lib.linkLibrary(openssl_lib);
74+
features.addValues(.{
75+
.GIT_HTTPS = 1,
76+
.GIT_OPENSSL = 1,
77+
.GIT_SHA1_OPENSSL = 1,
78+
.GIT_SHA256_OPENSSL = 1,
79+
80+
.GIT_USE_FUTIMENS = 1,
81+
.GIT_IO_POLL = 1,
82+
.GIT_IO_SELECT = 1,
83+
});
84+
} else {
85+
// mbedTLS https and SHA backend
86+
lib.linkSystemLibrary("mbedtls");
87+
lib.linkSystemLibrary("mbedcrypto");
88+
lib.linkSystemLibrary("mbedx509");
89+
features.addValues(.{
90+
.GIT_HTTPS = 1,
91+
.GIT_MBEDTLS = 1,
92+
.GIT_SHA1_MBEDTLS = 1,
93+
.GIT_SHA256_MBEDTLS = 1,
94+
95+
.GIT_USE_FUTIMENS = 1,
96+
.GIT_IO_POLL = 1,
97+
.GIT_IO_SELECT = 1,
98+
});
99+
}
78100

79101
// ntlmclient
80102
{
@@ -85,15 +107,30 @@ pub fn build(b: *std.Build) !void {
85107
.link_libc = true,
86108
});
87109
ntlm.addIncludePath(libgit_src.path("deps/ntlmclient"));
110+
if (openssl) addOpenSSLHeaders(ntlm);
111+
112+
const ntlm_cflags = .{
113+
"-Wno-implicit-fallthrough",
114+
"-DNTLM_STATIC=1",
115+
"-DUNICODE_BUILTIN=1",
116+
if (openssl)
117+
"-DCRYPT_OPENSSL"
118+
else
119+
"-DCRYPT_MBEDTLS",
120+
};
88121
ntlm.addCSourceFiles(.{
89122
.root = libgit_root,
90123
.files = &ntlm_sources,
91-
.flags = &.{
92-
"-Wno-implicit-fallthrough",
93-
"-DNTLM_STATIC=1",
94-
"-DUNICODE_BUILTIN=1",
95-
"-DCRYPT_MBEDTLS",
124+
.flags = &ntlm_cflags,
125+
});
126+
ntlm.addCSourceFiles(.{
127+
.root = libgit_root,
128+
.files = if (openssl) &.{
129+
"deps/ntlmclient/crypt_openssl.c",
130+
} else &.{
131+
"deps/ntlmclient/crypt_mbedtls.c",
96132
},
133+
.flags = &ntlm_cflags,
97134
});
98135

99136
lib.linkLibrary(ntlm);
@@ -106,6 +143,15 @@ pub fn build(b: *std.Build) !void {
106143
.files = &util_unix_sources,
107144
.flags = &flags,
108145
});
146+
lib.addCSourceFiles(.{
147+
.root = libgit_root,
148+
.files = if (openssl) &.{
149+
"src/util/hash/openssl.c",
150+
} else &.{
151+
"src/util/hash/mbedtls.c",
152+
},
153+
.flags = &flags,
154+
});
109155
}
110156

111157
if (b.option(bool, "enable-ssh", "Enable SSH support") orelse false) {
@@ -249,6 +295,7 @@ pub fn build(b: *std.Build) !void {
249295
cli.addIncludePath(libgit_src.path("include"));
250296
cli.addIncludePath(libgit_src.path("src/util"));
251297
cli.addIncludePath(libgit_src.path("src/cli"));
298+
if (openssl) addOpenSSLHeaders(cli);
252299

253300
if (target.result.os.tag == .windows)
254301
cli.addCSourceFiles(.{ .root = libgit_root, .files = &cli_win32_sources })
@@ -293,6 +340,7 @@ pub fn build(b: *std.Build) !void {
293340
});
294341

295342
exe.addIncludePath(libgit_src.path("include"));
343+
if (openssl) addOpenSSLHeaders(exe);
296344
exe.linkLibrary(lib);
297345

298346
// independent install step so you can easily access the binary
@@ -322,6 +370,7 @@ pub fn build(b: *std.Build) !void {
322370
tests.addConfigHeader(features);
323371
tests.addIncludePath(libgit_src.path("include"));
324372
tests.addIncludePath(libgit_src.path("src/util"));
373+
if (openssl) addOpenSSLHeaders(tests);
325374

326375
tests.linkLibrary(lib);
327376

@@ -330,6 +379,13 @@ pub fn build(b: *std.Build) !void {
330379
}
331380
}
332381

382+
fn addOpenSSLHeaders(compile: *std.Build.Step.Compile) void {
383+
const b = compile.step.owner;
384+
const openssl_dep = b.dependency("openssl", .{});
385+
const openssl_lib = openssl_dep.artifact("openssl");
386+
compile.addIncludePath(openssl_lib.getEmittedIncludeTree());
387+
}
388+
333389
const libgit_sources = [_][]const u8{
334390
"src/libgit2/annotated_commit.c",
335391
"src/libgit2/apply.c",
@@ -502,8 +558,6 @@ const util_unix_sources = [_][]const u8{
502558
"src/util/unix/map.c",
503559
"src/util/unix/process.c",
504560
"src/util/unix/realpath.c",
505-
506-
"src/util/hash/mbedtls.c",
507561
};
508562

509563
const util_win32_sources = [_][]const u8{
@@ -579,7 +633,6 @@ const xdiff_sources = [_][]const u8{
579633

580634
const ntlm_sources = [_][]const u8{
581635
"deps/ntlmclient/crypt_builtin_md4.c",
582-
"deps/ntlmclient/crypt_mbedtls.c",
583636
"deps/ntlmclient/ntlm.c",
584637
"deps/ntlmclient/unicode_builtin.c",
585638
"deps/ntlmclient/util.c",

build.zig.zon

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
.libgit2 = .{
77
.url = "https://github.com/libgit2/libgit2/archive/refs/tags/v1.8.1.tar.gz",
88
.hash = "12208db692747f305b79ecfb3ac69e691d0b9584383ec4bcb0eb07b62b73de77b1cf",
9-
}
9+
},
10+
.openssl = .{
11+
.url = "https://github.com/allyourcodebase/openssl/archive/refs/tags/3.3.1-1.tar.gz",
12+
.hash = "12207c40cefa38fe90e4230dfba2e5c76b37e1ee36602512cad8ff0501f892002a65",
13+
},
1014
},
1115
.paths = .{
1216
"build.zig",

0 commit comments

Comments
 (0)