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.
