Files
sporel-lib-core.git-lib/CMakeLists.txt
Axel Meyer b161f2a059 lib-core.git v0.1.0: native libgit2 wrapper
Initial release. Statically vendors libgit2 v1.7.2 (HTTPS-only via
WinHTTP on Windows, no SSH dependency); exposes synchronous clone,
checkout, fetch, describe, has_ref, is_repo, tags plus an async
start_ls_remote_tags / is_done / take_result triple routed through
the engine async-pool.

The native-lib loader resolves engine services through the
sporel_engine_api struct passed to sporel_lib_init, so this lib does
not statically depend on internal engine symbols beyond the Lua C-API
re-exports.

Smoke-tested against a public Gitea repo: clone over HTTPS succeeds,
async ls-remote returns the tag list, all C-allocations clean up via
git_*_free / git_buf_dispose. First clean build of libgit2 takes
~5-15 min on a typical machine; cached afterwards.
2026-05-31 01:15:30 +02:00

111 lines
4.5 KiB
CMake

cmake_minimum_required(VERSION 3.20)
project(sporel_lib_core_git LANGUAGES C)
# -----------------------------------------------------------------------------
# Vendor libgit2 v1.7.2 — static, HTTPS-only, no SSH, no CLI, no tests.
# HTTPS backend: WinHTTP on Windows; libgit2 picks it automatically when
# USE_HTTPS=ON and BUILD_SHARED_LIBS=OFF. See ADR-0047.
# First fetch + build takes ~5-15 min; subsequent builds are fast.
# -----------------------------------------------------------------------------
include(FetchContent)
set(BUILD_CLI OFF CACHE BOOL "" FORCE)
set(BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(USE_SSH OFF CACHE BOOL "" FORCE) # HTTPS-only per spec
set(USE_HTTPS ON CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) # static
set(REGEX_BACKEND builtin CACHE STRING "" FORCE)
FetchContent_Declare(
libgit2
GIT_REPOSITORY https://github.com/libgit2/libgit2.git
GIT_TAG v1.7.2)
FetchContent_MakeAvailable(libgit2)
# -----------------------------------------------------------------------------
# Lua headers: reused from the engine's FetchContent cache (the same Lua
# VM Sporel.exe boots into; no separate Lua build for the lib). At runtime,
# Lua C-API symbols (lua_*, luaL_*) are resolved against the re-exports
# in sporel_engine.dll (per ADR-0023).
#
# At link time on MinGW we need an import library that resolves those
# symbols; we use libsporel_engine.dll.a from the engine's build tree.
# Caller MUST set both -DLUA_INCLUDE_DIR_HINT and -DSPOREL_ENGINE_BUILD_HINT.
# -----------------------------------------------------------------------------
if(NOT DEFINED LUA_INCLUDE_DIR_HINT)
set(LUA_INCLUDE_DIR_HINT
"$ENV{HOME}/Projects/Sporel/sporel-engine/build/_deps/lua-src/src")
endif()
if(NOT DEFINED SPOREL_ENGINE_BUILD_HINT)
set(SPOREL_ENGINE_BUILD_HINT
"$ENV{HOME}/Projects/Sporel/sporel-engine/build")
endif()
if(NOT DEFINED SPOREL_ENGINE_INCLUDE_HINT)
set(SPOREL_ENGINE_INCLUDE_HINT
"$ENV{HOME}/Projects/Sporel/sporel-engine/include")
endif()
if(NOT DEFINED SPOREL_ENGINE_SRC_HINT)
# engine_async.h lives in src/ (private engine headers). Native libs
# that use the async-pool need this for the sporel_async_handle and
# sporel_async_work_fn typedefs — though engine_api.h re-declares them.
set(SPOREL_ENGINE_SRC_HINT
"$ENV{HOME}/Projects/Sporel/sporel-engine/src")
endif()
find_path(LUA_INCLUDE_DIR lua.h HINTS ${LUA_INCLUDE_DIR_HINT})
if(NOT LUA_INCLUDE_DIR)
message(FATAL_ERROR
"sporel_git: could not find lua.h. Pass "
"-DLUA_INCLUDE_DIR_HINT=/path/to/sporel-engine/build/_deps/lua-src/src "
"(default = ~/Projects/Sporel/sporel-engine/build/_deps/lua-src/src).")
endif()
find_library(SPOREL_ENGINE_IMPLIB
NAMES sporel_engine libsporel_engine
HINTS ${SPOREL_ENGINE_BUILD_HINT}
NO_DEFAULT_PATH)
if(NOT SPOREL_ENGINE_IMPLIB)
message(FATAL_ERROR
"sporel_git: could not find libsporel_engine.dll.a in "
"${SPOREL_ENGINE_BUILD_HINT}. Build the engine first.")
endif()
# -----------------------------------------------------------------------------
# sporel_git shared module
# -----------------------------------------------------------------------------
add_library(sporel_git MODULE src/sporel_git.c)
target_include_directories(sporel_git PRIVATE
src
${LUA_INCLUDE_DIR}
${SPOREL_ENGINE_INCLUDE_HINT} # for sporel/engine_api.h
${libgit2_SOURCE_DIR}/include)
# libgit2's main target is named "libgit2package" (the on-disk artifact
# is libgit2.a / git2.dll; CMake target name lives independently per
# libgit2's CMakeLists.txt at src/libgit2/CMakeLists.txt:59).
# Linking against the CMake target propagates transitive system libs
# (WinHTTP, ws2_32, crypt32, etc.) automatically.
target_link_libraries(sporel_git PRIVATE
libgit2package
${SPOREL_ENGINE_IMPLIB})
# When linking into sporel_engine.dll at runtime the Lua symbols come from
# its re-exports, so we treat the engine import lib as the Lua import lib
# too (no separate lua54 target). This avoids needing two Lua copies.
set_target_properties(sporel_git PROPERTIES
PREFIX ""
OUTPUT_NAME "sporel_git")
# Silence libgit2's compile warnings — they leak through FetchContent.
if(MSVC)
target_compile_options(sporel_git PRIVATE /W2)
else()
target_compile_options(sporel_git PRIVATE -Wall -Wno-unused-parameter)
endif()
# On Linux: --unresolved-symbols=ignore-in-shared-libs would let us drop
# the engine import lib, but the import-lib path is uniform across both
# platforms — keep it.