Skip to content

macOS support #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
zig-version: ["0.14.0"]
os:
- ubuntu-latest
# - macos-latest # Does not build yet
- macos-latest
# - windows-latest # Does not pass tests
runs-on: ${{ matrix.os }}
steps:
Expand Down
63 changes: 47 additions & 16 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,29 @@ pub fn build(b: *std.Build) !void {
.{ .style = .{ .cmake = libgit_src.path("src/util/git2_features.h.in") } },
.{
.GIT_THREADS = 1,

// @Todo: add per target conditionals for these
.GIT_USE_NSEC = 1,
.GIT_USE_STAT_MTIM = 1,
.GIT_RAND_GETENTROPY = 1,
.GIT_RAND_GETLOADAVG = 1,
},
);

// TODO: are there more subtleties for other platforms?
// TODO: do we need iconv on other platforms? original cmake enabled it by
// default on APPLE targets
if (target.result.os.tag == .macos) {
lib.linkSystemLibrary("iconv");
features.addValues(.{
.GIT_USE_ICONV = 1,
.GIT_USE_STAT_MTIMESPEC = 1,
.GIT_REGEX_REGCOMP_L = 1,
.GIT_QSORT_BSD = 1,
});
} else {
features.addValues(.{
.GIT_USE_STAT_MTIM = 1,
.GIT_RAND_GETENTROPY = 1,
});
}

const flags = [_][]const u8{
// @Todo: for some reason on linux, trying to use c90 as specified in the cmake
// files causes compile errors relating to pthreads. Using gnu90 or the
Expand All @@ -45,11 +59,11 @@ pub fn build(b: *std.Build) !void {

// The TLS backend logic is only run for non windows builds.
var tls_dep: ?*std.Build.Dependency = null;
const tls_backend = b.option(
const tls_backend: TlsBackend = b.option(
TlsBackend,
"tls-backend",
"Choose Unix TLS/SSL backend (default is mbedtls)",
) orelse .mbedtls;
) orelse if (target.result.os.tag == .macos) .securetransport else .mbedtls;

if (target.result.os.tag == .windows) {
lib.linkSystemLibrary("winhttp");
Expand All @@ -73,6 +87,21 @@ pub fn build(b: *std.Build) !void {
lib.addCSourceFiles(.{ .root = libgit_root, .files = &util_win32_sources, .flags = &flags });
} else {
switch (tls_backend) {
.securetransport => {
lib.linkFramework("Security");
lib.linkFramework("CoreFoundation");
features.addValues(.{
.GIT_HTTPS = 1,
.GIT_SECURE_TRANSPORT = 1,

.GIT_SHA1_COLLISIONDETECT = 1,
.GIT_SHA256_COMMON_CRYPTO = 1,

.GIT_USE_FUTIMENS = 1,
.GIT_IO_POLL = 1,
.GIT_IO_SELECT = 1,
});
},
.openssl => {
tls_dep = b.lazyDependency("openssl", .{
.target = target,
Expand Down Expand Up @@ -132,21 +161,23 @@ pub fn build(b: *std.Build) !void {
switch (tls_backend) {
.openssl => "-DCRYPT_OPENSSL",
.mbedtls => "-DCRYPT_MBEDTLS",
.securetransport => "-DCRYPT_COMMONCRYPTO",
},
};
ntlm.addCSourceFiles(.{
.root = libgit_root,
.files = &ntlm_sources,
.flags = &ntlm_cflags,
});
ntlm.addCSourceFiles(.{
.root = libgit_root,
.files = switch (tls_backend) {
.openssl => &.{"deps/ntlmclient/crypt_openssl.c"},
.mbedtls => &.{"deps/ntlmclient/crypt_mbedtls.c"},
.securetransport => &.{"deps/ntlmclient/crypt_commoncrypto.c"},
},
.flags = &ntlm_cflags,
});
ntlm.addCSourceFiles(.{
.root = libgit_root,
.files = &ntlm_sources,
.flags = &(ntlm_cflags ++ .{"-Wno-deprecated"}),
});

lib.linkLibrary(ntlm);
lib.addAfterIncludePath(libgit_src.path("deps/ntlmclient")); // avoid aliasing ntlmclient/util.h and src/util/util.h
Expand All @@ -163,6 +194,7 @@ pub fn build(b: *std.Build) !void {
.files = switch (tls_backend) {
.openssl => &.{"src/util/hash/openssl.c"},
.mbedtls => &.{"src/util/hash/mbedtls.c"},
.securetransport => &.{"src/util/hash/common_crypto.c"},
},
.flags = &flags,
});
Expand Down Expand Up @@ -210,7 +242,7 @@ pub fn build(b: *std.Build) !void {
lib.linkLibrary(llhttp);
features.addValues(.{ .GIT_HTTPPARSER_BUILTIN = 1 });
}
{
if (target.result.os.tag != .macos) {
const pcre = b.addLibrary(.{
.name = "pcre",
.linkage = .static,
Expand Down Expand Up @@ -298,8 +330,6 @@ pub fn build(b: *std.Build) !void {
else => |size| std.debug.panic("Unsupported architecture ({d}bit)", .{size}),
}

// @Todo: ICONV?

lib.addConfigHeader(features);

lib.addIncludePath(libgit_src.path("src/libgit2"));
Expand Down Expand Up @@ -409,7 +439,7 @@ pub fn build(b: *std.Build) !void {
try file.setPermissions(.{ .inner = .{ .mode = 0o755 } });
}

const gen_cmd = b.addSystemCommand(&.{"python"});
const gen_cmd = b.addSystemCommand(&.{"python3"});
gen_cmd.addFileArg(libgit_src.path("tests/clar/generate.py"));
const clar_suite = gen_cmd.addPrefixedOutputDirectoryArg("-o", "clar_suite");
gen_cmd.addArgs(&.{ "-f", "-xonline", "-xstress", "-xperf" });
Expand Down Expand Up @@ -509,7 +539,7 @@ pub fn build(b: *std.Build) !void {
}
}

const TlsBackend = enum { openssl, mbedtls };
pub const TlsBackend = enum { openssl, mbedtls, securetransport };

fn maybeAddTlsIncludes(
compile: *std.Build.Step.Compile,
Expand All @@ -518,6 +548,7 @@ fn maybeAddTlsIncludes(
) void {
if (dep) |tls| {
const name = switch (backend) {
.securetransport => unreachable,
.openssl => "openssl",
.mbedtls => "mbedtls",
};
Expand Down