From 39e5de6858456e0794513731d521ec38e4737ed3 Mon Sep 17 00:00:00 2001 From: Tobias Simetsreiter Date: Fri, 23 Oct 2026 18:09:22 +0200 Subject: [PATCH 1/5] add a dependency on libssh2 and use a systemIntegrationOption --- build.zig | 13 ++++++++++++- build.zig.zon | 6 ++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index ac7d3af..8856cda 100644 --- a/build.zig +++ b/build.zig @@ -228,7 +228,18 @@ pub fn build(b: *std.Build) !void { }); if (b.option(bool, "enable-ssh", "Enable SSH support") orelse false) { - mod.linkSystemLibrary("ssh2", .{}); + if (b.systemIntegrationOption("ssh2", .{})) { + mod.linkSystemLibrary("ssh2", .{}); + } else { + if (b.lazyDependency("libssh2", .{ + .target = target, + .optimize = optimize, + .@"crypto-backend" = .mbedtls, + .@"link-system-crypto-backend" = false, + })) |libssh2| { + mod.linkLibrary(libssh2.artifact("ssh2")); + } + } features.addValues(.{ .GIT_SSH = 1, .GIT_SSH_LIBSSH2 = 1, diff --git a/build.zig.zon b/build.zig.zon index 965db13..7c81fe6 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -18,6 +18,12 @@ .hash = "mbedtls-3.6.4-E4NUR24VAADBF5pt-No-Z90DjozkzySYAgq6eDFy9Xdy", .lazy = true, }, + .libssh2 = .{ + // TODO: merge https://github.com/allyourcodebase/libssh2/pull/11 + .url = "git+https://github.com/dasimmet/libssh2?ref=feature/mbedtls-dependency#a593b032a2ac50f60527d55094ea2d8a2b8fda56", + .hash = "libssh2-1.11.1-JgdeR1YYAAA0tzOdJeQFzntxhxXv42Hx7M_o7wlJ5rJm", + .lazy = true, + }, }, .paths = .{ "build.zig", From 8442612f6ce03362da110f4de5df1a9107a26f7f Mon Sep 17 00:00:00 2001 From: Tobias Simetsreiter Date: Sat, 24 Oct 2026 12:15:35 +0200 Subject: [PATCH 2/5] add zlib 1.3.2 dependency to libssh2 and libgit2 --- build.zig | 27 +++------------------------ build.zig.zon | 8 ++++++-- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/build.zig b/build.zig index 8856cda..4691af1 100644 --- a/build.zig +++ b/build.zig @@ -236,6 +236,7 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, .@"crypto-backend" = .mbedtls, .@"link-system-crypto-backend" = false, + .zlib = true, })) |libssh2| { mod.linkLibrary(libssh2.artifact("ssh2")); } @@ -310,33 +311,11 @@ pub fn build(b: *std.Build) !void { features.addValues(.{ .GIT_REGEX_BUILTIN = 1 }); } { - // @Todo: support using system zlib? - const zlib = b.createModule(.{ + const zlib = b.dependency("zlib", .{ .target = target, .optimize = optimize, - .link_libc = true, - }); - zlib.addIncludePath(libgit_src.path("deps/zlib")); - zlib.addCSourceFiles(.{ - .root = libgit_root, - .files = &zlib_sources, - .flags = &.{ - "-Wno-implicit-fallthrough", - "-DNO_VIZ", - "-DSTDC", - "-DNO_GZIP", - "-DHAVE_SYS_TYPES_H", - "-DHAVE_STDINT_H", - "-DHAVE_STDDEF_H", - }, }); - - mod.addIncludePath(libgit_src.path("deps/zlib")); - mod.linkLibrary(b.addLibrary(.{ - .name = "z", - .linkage = .static, - .root_module = zlib, - })); + mod.linkLibrary(zlib.artifact("z")); features.addValues(.{ .GIT_COMPRESSION_ZLIB = 1 }); } // xdiff diff --git a/build.zig.zon b/build.zig.zon index 7c81fe6..2cfcb15 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -20,10 +20,14 @@ }, .libssh2 = .{ // TODO: merge https://github.com/allyourcodebase/libssh2/pull/11 - .url = "git+https://github.com/dasimmet/libssh2?ref=feature/mbedtls-dependency#a593b032a2ac50f60527d55094ea2d8a2b8fda56", - .hash = "libssh2-1.11.1-JgdeR1YYAAA0tzOdJeQFzntxhxXv42Hx7M_o7wlJ5rJm", + .url = "git+https://github.com/dasimmet/libssh2?ref=feature/mbedtls-dependency#5e39e4fdde45d031ca417ab7f6a20004264f3acf", + .hash = "libssh2-1.11.1-JgdeR2AYAAAI5JWiLCEquyMH25Q0m3IXODXotOojEsUS", .lazy = true, }, + .zlib = .{ + .url = "git+https://github.com/allyourcodebase/zlib.git?ref=1.3.2#d3b5519d3b73616a6bfece0712f080029d2a9244", + .hash = "zlib-1.3.2-ZZQ7lc8NAAAHm9MDfplvwoesXvk4tVm6VCsiI8KnIbT0", + }, }, .paths = .{ "build.zig", From f0e09d95b768c33b0ad8c426b36af9f88abb96e0 Mon Sep 17 00:00:00 2001 From: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com> Date: Sun, 25 Oct 2026 04:02:36 -0400 Subject: [PATCH 3/5] choose appropriate crypto backend for libssh based on build config Fallback to mbedtls if we cannot provide a good alternative. --- build.zig | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 4691af1..a967efa 100644 --- a/build.zig +++ b/build.zig @@ -231,10 +231,22 @@ pub fn build(b: *std.Build) !void { if (b.systemIntegrationOption("ssh2", .{})) { mod.linkSystemLibrary("ssh2", .{}); } else { + const crypto_backend: enum { + openssl, + mbedtls, + wincng, + } = if (target.result.os.tag == .windows) + .wincng + else switch (tls_backend) { + .openssl => .openssl, + .mbedtls => .mbedtls, + // fallback to mbedtls + else => .mbedtls, + }; if (b.lazyDependency("libssh2", .{ .target = target, .optimize = optimize, - .@"crypto-backend" = .mbedtls, + .@"crypto-backend" = crypto_backend, .@"link-system-crypto-backend" = false, .zlib = true, })) |libssh2| { From 8c1d2b6cc917c244d8da80a310582ece0092aefa Mon Sep 17 00:00:00 2001 From: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com> Date: Sun, 25 Oct 2026 04:14:11 -0400 Subject: [PATCH 4/5] remove unecessary zlib sources The runner builds fine on my machine without zlib headers. We'll see if it passes CI... --- build.zig | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/build.zig b/build.zig index a967efa..3d7dba0 100644 --- a/build.zig +++ b/build.zig @@ -485,7 +485,6 @@ pub fn build(b: *std.Build) !void { runner.addIncludePath(libgit_src.path("src/util")); runner.addIncludePath(libgit_src.path("src/libgit2")); - runner.addIncludePath(libgit_src.path("deps/zlib")); runner.addIncludePath(libgit_src.path("deps/xdiff")); runner.addIncludePath(libgit_src.path("deps/pcre")); maybeAddTlsIncludes(runner, tls_dep, tls_backend); @@ -894,18 +893,6 @@ const pcre_sources = [_][]const u8{ "deps/pcre/pcreposix.c", }; -const zlib_sources = [_][]const u8{ - "deps/zlib/adler32.c", - "deps/zlib/crc32.c", - "deps/zlib/deflate.c", - "deps/zlib/infback.c", - "deps/zlib/inffast.c", - "deps/zlib/inflate.c", - "deps/zlib/inftrees.c", - "deps/zlib/trees.c", - "deps/zlib/zutil.c", -}; - const xdiff_sources = [_][]const u8{ "deps/xdiff/xdiffi.c", "deps/xdiff/xemit.c", From b21bfc9f48712de6b611e781a132dc78f86103dc Mon Sep 17 00:00:00 2001 From: Parzival-3141 <29632054+Parzival-3141@users.noreply.github.com> Date: Sun, 25 Oct 2026 04:26:45 -0400 Subject: [PATCH 5/5] include zlib headers in test runner The zlib headers are required for testing windows builds. --- build.zig | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 3d7dba0..a8b4065 100644 --- a/build.zig +++ b/build.zig @@ -322,14 +322,16 @@ pub fn build(b: *std.Build) !void { })); features.addValues(.{ .GIT_REGEX_BUILTIN = 1 }); } - { - const zlib = b.dependency("zlib", .{ + const zlib_includes = blk: { + const zlib_dep = b.dependency("zlib", .{ .target = target, .optimize = optimize, }); - mod.linkLibrary(zlib.artifact("z")); + const zlib = zlib_dep.artifact("z"); + mod.linkLibrary(zlib); features.addValues(.{ .GIT_COMPRESSION_ZLIB = 1 }); - } + break :blk zlib.getEmittedIncludeTree(); + }; // xdiff { // Bundled xdiff dependency relies on libgit2 headers & utils, so we @@ -485,6 +487,7 @@ pub fn build(b: *std.Build) !void { runner.addIncludePath(libgit_src.path("src/util")); runner.addIncludePath(libgit_src.path("src/libgit2")); + runner.addIncludePath(zlib_includes); runner.addIncludePath(libgit_src.path("deps/xdiff")); runner.addIncludePath(libgit_src.path("deps/pcre")); maybeAddTlsIncludes(runner, tls_dep, tls_backend);