From 87ec7bc464e840f91e3495ac5ca60af67f6555b7 Mon Sep 17 00:00:00 2001 From: Dustin Persek Date: Fri, 3 Jul 2026 18:51:47 -0400 Subject: [PATCH 1/2] fix(build): gate libgit2 allocator support Signed-off-by: Dustin Persek --- .github/workflows/_test.yml | 11 ++++++++--- Makefile.cbm | 21 +++++++++++++++++---- internal/cbm/cbm.c | 16 ++++++++++++++-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/.github/workflows/_test.yml b/.github/workflows/_test.yml index c2aa6e90f..200586d50 100644 --- a/.github/workflows/_test.yml +++ b/.github/workflows/_test.yml @@ -33,7 +33,7 @@ jobs: BROAD: ${{ inputs.broad_platforms }} run: | CORE_UNIX='[ - {"os":"ubuntu-latest","cc":"gcc","cxx":"g++"}, + {"os":"ubuntu-latest","cc":"gcc","cxx":"g++","libgit2":true}, {"os":"ubuntu-24.04-arm","cc":"gcc","cxx":"g++"}, {"os":"macos-14","cc":"cc","cxx":"c++"}, {"os":"macos-15-intel","cc":"cc","cxx":"c++"} @@ -79,10 +79,15 @@ jobs: - name: Install deps (Ubuntu) if: startsWith(matrix.os, 'ubuntu') - run: sudo apt-get update && sudo apt-get install -y zlib1g-dev + run: sudo apt-get update && sudo apt-get install -y zlib1g-dev ${{ matrix.libgit2 == true && 'libgit2-dev pkg-config' || '' }} - name: Test - run: scripts/test.sh CC=${{ matrix.cc }} CXX=${{ matrix.cxx }} + run: | + if [ "${{ matrix.libgit2 == true }}" = "true" ]; then + echo "REQUIRE_LIBGIT2=1" + pkg-config --modversion libgit2 + fi + scripts/test.sh CC=${{ matrix.cc }} CXX=${{ matrix.cxx }} ${{ matrix.libgit2 == true && 'REQUIRE_LIBGIT2=1' || '' }} env: CBM_SKIP_PERF: ${{ inputs.skip_perf && '1' || '' }} diff --git a/Makefile.cbm b/Makefile.cbm index de6ad40ff..7c072d027 100644 --- a/Makefile.cbm +++ b/Makefile.cbm @@ -35,12 +35,25 @@ TS_INCLUDE = $(CBM_DIR)/vendored/ts_runtime/include TS_SRC = $(CBM_DIR)/vendored/ts_runtime/src # ── Optional libgit2 (faster git history parsing) ──────────────── -# Auto-detected via pkg-config. Falls back to popen("git log ...") if absent. -LIBGIT2_CFLAGS := $(shell pkg-config --cflags libgit2 2>/dev/null) -LIBGIT2_LIBS := $(shell pkg-config --libs libgit2 2>/dev/null) -ifneq ($(LIBGIT2_LIBS),) +# Auto-detected via pkg-config. Falls back to popen("git log ...") if absent +# or too old for the git_allocator ABI used by the production allocator bind. +PKG_CONFIG ?= pkg-config +LIBGIT2_MIN_VERSION := 1.7.0 +LIBGIT2_AVAILABLE := $(shell $(PKG_CONFIG) --atleast-version=$(LIBGIT2_MIN_VERSION) libgit2 >/dev/null 2>&1 && echo yes || echo no) + +ifeq ($(REQUIRE_LIBGIT2),1) +ifneq ($(LIBGIT2_AVAILABLE),yes) +LIBGIT2_VERSION := $(shell $(PKG_CONFIG) --modversion libgit2 2>/dev/null) +$(error libgit2 >= $(LIBGIT2_MIN_VERSION) is required when REQUIRE_LIBGIT2=1; pkg-config found $(if $(LIBGIT2_VERSION),$(LIBGIT2_VERSION),none)) +endif +endif + +ifeq ($(LIBGIT2_AVAILABLE),yes) +LIBGIT2_CFLAGS := $(shell $(PKG_CONFIG) --cflags libgit2 2>/dev/null) +LIBGIT2_LIBS := $(shell $(PKG_CONFIG) --libs libgit2 2>/dev/null) LIBGIT2_FLAGS = -DHAVE_LIBGIT2 $(LIBGIT2_CFLAGS) else +LIBGIT2_CFLAGS = LIBGIT2_FLAGS = LIBGIT2_LIBS = endif diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.c index 1bbce2e3b..bf91eb859 100644 --- a/internal/cbm/cbm.c +++ b/internal/cbm/cbm.c @@ -20,6 +20,18 @@ #if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR #include "sqlite3.h" // sqlite3_mem_methods, sqlite3_config, SQLITE_CONFIG_MALLOC — bind sqlite to mimalloc #if defined(HAVE_LIBGIT2) +#include +#if defined(LIBGIT2_VERSION_CHECK) +#if !LIBGIT2_VERSION_CHECK(1, 7, 0) +#error "HAVE_LIBGIT2 requires libgit2 >= 1.7.0 for git_allocator" +#endif +#elif defined(LIBGIT2_VER_MAJOR) && defined(LIBGIT2_VER_MINOR) && defined(LIBGIT2_VER_REVISION) +#if ((LIBGIT2_VER_MAJOR * 1000000) + (LIBGIT2_VER_MINOR * 10000) + (LIBGIT2_VER_REVISION * 100)) < 1070000 +#error "HAVE_LIBGIT2 requires libgit2 >= 1.7.0 for git_allocator" +#endif +#else +#error "HAVE_LIBGIT2 requires known libgit2 version macros for the >= 1.7.0 git_allocator guard" +#endif #include // git_libgit2_opts, GIT_OPT_SET_ALLOCATOR — bind libgit2 to mimalloc #include // git_allocator — not pulled in by (it's a sys/ header) #endif @@ -287,8 +299,8 @@ static void cbm_sqlite_memshutdown(void *appdata) { } #if defined(HAVE_LIBGIT2) -/* libgit2 git_allocator backed by mimalloc. The struct (current libgit2) has - * exactly three members: gmalloc(size_t,file,line), grealloc(ptr,size,file,line), +/* libgit2 >= 1.7 git_allocator backed by mimalloc. The struct has exactly + * three members: gmalloc(size_t,file,line), grealloc(ptr,size,file,line), * gfree(ptr). The file/line args are ignored. */ static void *cbm_git_malloc(size_t n, const char *file, int line) { (void)file; From 8205237159db36f76649c605b8ab5d7a2c1df5ef Mon Sep 17 00:00:00 2001 From: Dustin Persek Date: Fri, 3 Jul 2026 22:21:39 -0400 Subject: [PATCH 2/2] fix(build): format libgit2 version guard Signed-off-by: Dustin Persek --- internal/cbm/cbm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.c index bf91eb859..2368eb777 100644 --- a/internal/cbm/cbm.c +++ b/internal/cbm/cbm.c @@ -26,7 +26,8 @@ #error "HAVE_LIBGIT2 requires libgit2 >= 1.7.0 for git_allocator" #endif #elif defined(LIBGIT2_VER_MAJOR) && defined(LIBGIT2_VER_MINOR) && defined(LIBGIT2_VER_REVISION) -#if ((LIBGIT2_VER_MAJOR * 1000000) + (LIBGIT2_VER_MINOR * 10000) + (LIBGIT2_VER_REVISION * 100)) < 1070000 +#if ((LIBGIT2_VER_MAJOR * 1000000) + (LIBGIT2_VER_MINOR * 10000) + (LIBGIT2_VER_REVISION * 100)) < \ + 1070000 #error "HAVE_LIBGIT2 requires libgit2 >= 1.7.0 for git_allocator" #endif #else