Skip to content

Commit 9770803

Browse files
authored
Merge pull request DeusData#829 from SyntaxSawdust/codex/issue-801-libgit2-floor
fix(build): gate libgit2 allocator support
2 parents 9871e7e + 8205237 commit 9770803

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

.github/workflows/_test.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
BROAD: ${{ inputs.broad_platforms }}
3434
run: |
3535
CORE_UNIX='[
36-
{"os":"ubuntu-latest","cc":"gcc","cxx":"g++"},
36+
{"os":"ubuntu-latest","cc":"gcc","cxx":"g++","libgit2":true},
3737
{"os":"ubuntu-24.04-arm","cc":"gcc","cxx":"g++"},
3838
{"os":"macos-14","cc":"cc","cxx":"c++"},
3939
{"os":"macos-15-intel","cc":"cc","cxx":"c++"}
@@ -79,10 +79,15 @@ jobs:
7979

8080
- name: Install deps (Ubuntu)
8181
if: startsWith(matrix.os, 'ubuntu')
82-
run: sudo apt-get update && sudo apt-get install -y zlib1g-dev
82+
run: sudo apt-get update && sudo apt-get install -y zlib1g-dev ${{ matrix.libgit2 == true && 'libgit2-dev pkg-config' || '' }}
8383

8484
- name: Test
85-
run: scripts/test.sh CC=${{ matrix.cc }} CXX=${{ matrix.cxx }}
85+
run: |
86+
if [ "${{ matrix.libgit2 == true }}" = "true" ]; then
87+
echo "REQUIRE_LIBGIT2=1"
88+
pkg-config --modversion libgit2
89+
fi
90+
scripts/test.sh CC=${{ matrix.cc }} CXX=${{ matrix.cxx }} ${{ matrix.libgit2 == true && 'REQUIRE_LIBGIT2=1' || '' }}
8691
env:
8792
CBM_SKIP_PERF: ${{ inputs.skip_perf && '1' || '' }}
8893

Makefile.cbm

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,25 @@ TS_INCLUDE = $(CBM_DIR)/vendored/ts_runtime/include
3535
TS_SRC = $(CBM_DIR)/vendored/ts_runtime/src
3636

3737
# ── Optional libgit2 (faster git history parsing) ────────────────
38-
# Auto-detected via pkg-config. Falls back to popen("git log ...") if absent.
39-
LIBGIT2_CFLAGS := $(shell pkg-config --cflags libgit2 2>/dev/null)
40-
LIBGIT2_LIBS := $(shell pkg-config --libs libgit2 2>/dev/null)
41-
ifneq ($(LIBGIT2_LIBS),)
38+
# Auto-detected via pkg-config. Falls back to popen("git log ...") if absent
39+
# or too old for the git_allocator ABI used by the production allocator bind.
40+
PKG_CONFIG ?= pkg-config
41+
LIBGIT2_MIN_VERSION := 1.7.0
42+
LIBGIT2_AVAILABLE := $(shell $(PKG_CONFIG) --atleast-version=$(LIBGIT2_MIN_VERSION) libgit2 >/dev/null 2>&1 && echo yes || echo no)
43+
44+
ifeq ($(REQUIRE_LIBGIT2),1)
45+
ifneq ($(LIBGIT2_AVAILABLE),yes)
46+
LIBGIT2_VERSION := $(shell $(PKG_CONFIG) --modversion libgit2 2>/dev/null)
47+
$(error libgit2 >= $(LIBGIT2_MIN_VERSION) is required when REQUIRE_LIBGIT2=1; pkg-config found $(if $(LIBGIT2_VERSION),$(LIBGIT2_VERSION),none))
48+
endif
49+
endif
50+
51+
ifeq ($(LIBGIT2_AVAILABLE),yes)
52+
LIBGIT2_CFLAGS := $(shell $(PKG_CONFIG) --cflags libgit2 2>/dev/null)
53+
LIBGIT2_LIBS := $(shell $(PKG_CONFIG) --libs libgit2 2>/dev/null)
4254
LIBGIT2_FLAGS = -DHAVE_LIBGIT2 $(LIBGIT2_CFLAGS)
4355
else
56+
LIBGIT2_CFLAGS =
4457
LIBGIT2_FLAGS =
4558
LIBGIT2_LIBS =
4659
endif

internal/cbm/cbm.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222
#if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR
2323
#include "sqlite3.h" // sqlite3_mem_methods, sqlite3_config, SQLITE_CONFIG_MALLOC — bind sqlite to mimalloc
2424
#if defined(HAVE_LIBGIT2)
25+
#include <git2/version.h>
26+
#if defined(LIBGIT2_VERSION_CHECK)
27+
#if !LIBGIT2_VERSION_CHECK(1, 7, 0)
28+
#error "HAVE_LIBGIT2 requires libgit2 >= 1.7.0 for git_allocator"
29+
#endif
30+
#elif defined(LIBGIT2_VER_MAJOR) && defined(LIBGIT2_VER_MINOR) && defined(LIBGIT2_VER_REVISION)
31+
#if ((LIBGIT2_VER_MAJOR * 1000000) + (LIBGIT2_VER_MINOR * 10000) + (LIBGIT2_VER_REVISION * 100)) < \
32+
1070000
33+
#error "HAVE_LIBGIT2 requires libgit2 >= 1.7.0 for git_allocator"
34+
#endif
35+
#else
36+
#error "HAVE_LIBGIT2 requires known libgit2 version macros for the >= 1.7.0 git_allocator guard"
37+
#endif
2538
#include <git2.h> // git_libgit2_opts, GIT_OPT_SET_ALLOCATOR — bind libgit2 to mimalloc
2639
#include <git2/sys/alloc.h> // git_allocator — not pulled in by <git2.h> (it's a sys/ header)
2740
#endif
@@ -289,8 +302,8 @@ static void cbm_sqlite_memshutdown(void *appdata) {
289302
}
290303

291304
#if defined(HAVE_LIBGIT2)
292-
/* libgit2 git_allocator backed by mimalloc. The struct (current libgit2) has
293-
* exactly three members: gmalloc(size_t,file,line), grealloc(ptr,size,file,line),
305+
/* libgit2 >= 1.7 git_allocator backed by mimalloc. The struct has exactly
306+
* three members: gmalloc(size_t,file,line), grealloc(ptr,size,file,line),
294307
* gfree(ptr). The file/line args are ignored. */
295308
static void *cbm_git_malloc(size_t n, const char *file, int line) {
296309
(void)file;

0 commit comments

Comments
 (0)