cmake_minimum_required(VERSION 3.20)
project(mod_authn_totp VERSION 1.0.0 LANGUAGES C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# clangd: emit build/compile_commands.json on every configure (re-runs when
# CMakeLists.txt changes, i.e. when a source file is added).
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

find_package(APR REQUIRED)
find_package(APACHE REQUIRED)

# Pure core compiled ONCE, linked into both the module DSO and the unit binary.
add_library(totp_core OBJECT totp_core.c)
target_include_directories(totp_core PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR} ${APR_INCLUDE_DIR})
target_compile_definitions(totp_core PUBLIC ${APR_DEFINITIONS})

# Module DSO -> build/mod_authn_totp.so (PREFIX "" gives unversioned .so)
add_library(mod_authn_totp MODULE mod_authn_totp.c)
set_target_properties(mod_authn_totp PROPERTIES PREFIX "")
target_sources(mod_authn_totp PRIVATE $<TARGET_OBJECTS:totp_core>)
target_include_directories(mod_authn_totp PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR} ${APACHE_INCLUDE_DIR} ${APR_INCLUDE_DIR})
target_compile_definitions(mod_authn_totp PRIVATE
    ${APR_DEFINITIONS} ${APACHE_DEFINITIONS})
target_compile_definitions(mod_authn_totp PRIVATE LINUX _REENTRANT _GNU_SOURCE)
target_link_libraries(mod_authn_totp PRIVATE ${APACHE_CORE_LINK} ${APR_LINK_LIBS})

# Unit harness: ABTS main (test_units -> units) + run_units wrapper in SAME dir.
add_executable(test_units test/test_units.c test/abts/abts.c test/stubs.c)
set_target_properties(test_units PROPERTIES OUTPUT_NAME units)
target_sources(test_units PRIVATE $<TARGET_OBJECTS:totp_core>)
target_include_directories(test_units PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/test/abts ${APR_INCLUDE_DIR})
target_compile_definitions(test_units PRIVATE
    ${APR_DEFINITIONS} LINUX _REENTRANT _GNU_SOURCE)
target_link_libraries(test_units PRIVATE ${APR_LINK_LIBS} ${APRUTIL_LINK_LIBS})

add_executable(run_units test/run_units.c)
target_link_libraries(run_units PRIVATE ${APR_LINK_LIBS})
set_target_properties(test_units run_units PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

# install-module: copy the built DSO into the httpd module dir (manual/sudo).
install(PROGRAMS $<TARGET_FILE:mod_authn_totp>
    DESTINATION ${APACHE_MODULE_DIR})
add_custom_target(install-module
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:mod_authn_totp> ${APACHE_MODULE_DIR}/mod_authn_totp.so
    DEPENDS mod_authn_totp)

# CTest: unit runs run_units; integration runs t/TEST (deploy/env-gated).
include(CTest)
# Default ON (dev workflow runs BOTH suites); the Debian build passes
# -DTOTP_ENABLE_INTEGRATION_TESTS=OFF so integration (live-httpd) is never
# registered in the chroot where a live httpd cannot run. unit stays registered
# and dh_auto_test still FAILS the build on unit failure.
option(TOTP_ENABLE_INTEGRATION_TESTS "Build the live-httpd integration suite" ON)
if(BUILD_TESTING)
  add_test(NAME unit COMMAND run_units)
  if(TOTP_ENABLE_INTEGRATION_TESTS)
    add_test(NAME integration
        COMMAND perl ${CMAKE_CURRENT_SOURCE_DIR}/t/TEST
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/t)
    set_tests_properties(integration PROPERTIES DEPENDS unit)
  endif()
endif()
