diff --git a/build.zig b/build.zig index ac7d3af..a8b4065 100644 --- a/build.zig +++ b/build.zig @@ -228,7 +228,31 @@ 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 { + 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" = crypto_backend, + .@"link-system-crypto-backend" = false, + .zlib = true, + })) |libssh2| { + mod.linkLibrary(libssh2.artifact("ssh2")); + } + } features.addValues(.{ .GIT_SSH = 1, .GIT_SSH_LIBSSH2 = 1, @@ -298,36 +322,16 @@ pub fn build(b: *std.Build) !void { })); features.addValues(.{ .GIT_REGEX_BUILTIN = 1 }); } - { - // @Todo: support using system zlib? - const zlib = b.createModule(.{ + const zlib_includes = blk: { + const zlib_dep = 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, - })); + 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 @@ -483,7 +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(libgit_src.path("deps/zlib")); + runner.addIncludePath(zlib_includes); runner.addIncludePath(libgit_src.path("deps/xdiff")); runner.addIncludePath(libgit_src.path("deps/pcre")); maybeAddTlsIncludes(runner, tls_dep, tls_backend); @@ -892,18 +896,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", diff --git a/build.zig.zon b/build.zig.zon index 965db13..2cfcb15 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -18,6 +18,16 @@ .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#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",