cmake_minimum_required(VERSION 3.25)
project(jgap VERSION 7.2.12 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED OFF)

set(PACKAGE_VERSION ${PROJECT_VERSION})

list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")

# 1. External & Fetched Dependencies
include(Dependencies)

# Suppress GCC 15 template-body checking on uninstantiated Eigen LAPACKE templates
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-Wno-template-body" COMPILER_HAS_WNO_TEMPLATE_BODY)
if (COMPILER_HAS_WNO_TEMPLATE_BODY)
    add_compile_options("-Wno-template-body")
endif ()
# Auto-detect active Python virtualenv or Conda environment if default preset prefix is used
if (CMAKE_INSTALL_PREFIX STREQUAL "$ENV{HOME}/.local" OR CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    if (DEFINED ENV{VIRTUAL_ENV} AND NOT "$ENV{VIRTUAL_ENV}" STREQUAL "")
        set(CMAKE_INSTALL_PREFIX "$ENV{VIRTUAL_ENV}" CACHE PATH "Installation prefix" FORCE)
        message(STATUS "Active Python virtualenv detected: default install prefix set to ${CMAKE_INSTALL_PREFIX}")
    elseif (DEFINED ENV{CONDA_PREFIX} AND NOT "$ENV{CONDA_PREFIX}" STREQUAL "")
        set(CMAKE_INSTALL_PREFIX "$ENV{CONDA_PREFIX}" CACHE PATH "Installation prefix" FORCE)
        message(STATUS "Active Conda environment detected: default install prefix set to ${CMAKE_INSTALL_PREFIX}")
    endif ()
endif ()

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)

include_directories(${PROJECT_SOURCE_DIR}/src)

configure_file(
        ${CMAKE_SOURCE_DIR}/src/Version.hpp.in
        ${CMAKE_BINARY_DIR}/generated/Version.hpp
        @ONLY
)
include_directories(${CMAKE_BINARY_DIR}/generated)

# 3. Source Collection & Target Definitions
file(GLOB_RECURSE ALL_SRC_CPP "${PROJECT_SOURCE_DIR}/src/*.cpp")
file(GLOB_RECURSE ALL_SRC_C "${PROJECT_SOURCE_DIR}/src/*.c")

# Exclude standalone executables and python modules from the main shared library
list(FILTER ALL_SRC_CPP EXCLUDE REGEX "${PROJECT_SOURCE_DIR}/src/app/.*")
list(FILTER ALL_SRC_CPP EXCLUDE REGEX "${PROJECT_SOURCE_DIR}/src/pybind/.*")

set(LIBRARY_SOURCE_FILES ${ALL_SRC_CPP} ${ALL_SRC_C})

if (NOT SKBUILD)
    add_library(jgap_lib SHARED ${LIBRARY_SOURCE_FILES})
    add_library(jgap::jgap ALIAS jgap_lib)

    set_target_properties(jgap_lib PROPERTIES
            OUTPUT_NAME jgap
            EXPORT_NAME jgap
            CXX_VISIBILITY_PRESET default
            VISIBILITY_INLINES_HIDDEN OFF
    )

    # Hard guard: jgap MUST be a shared library.
    get_target_property(_jgap_lib_type jgap_lib TYPE)
    if (NOT _jgap_lib_type STREQUAL "SHARED_LIBRARY")
        message(FATAL_ERROR
                "jgap_lib must be built as a SHARED library (it is currently ${_jgap_lib_type}). The "
                "serialization registry and cross-boundary dynamic_cast depend on shared-library symbol "
                "semantics; a static build breaks them silently.")
    endif ()

    if (JGAP_LAPACK_LIBS)
        target_link_libraries(jgap_lib PRIVATE ${JGAP_LAPACK_LIBS})
    endif ()
    if (JGAP_BLAS_LIBS)
        target_link_libraries(jgap_lib PRIVATE ${JGAP_BLAS_LIBS})
    endif ()

    target_link_libraries(jgap_lib PRIVATE
            Eigen3::Eigen
            HighFive
            pugixml::pugixml
            HDF5::HDF5
    )
    target_include_directories(jgap_lib PRIVATE ${HDF5_INCLUDE_DIRS})

    if (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        # Resolve libc++ symbols required by pre-compiled static dependencies when building with GCC on macOS
        target_link_libraries(jgap_lib PRIVATE c++)
    endif ()

    if (APPLE)
        target_compile_definitions(jgap_lib PUBLIC HAS_GCD)
    elseif (OpenMP_CXX_FOUND)
        target_compile_definitions(jgap_lib PUBLIC HAS_OPENMP)
        target_link_libraries(jgap_lib PUBLIC OpenMP::OpenMP_CXX)
    endif ()

    # Public include dirs so consumers of the target get jgap's headers for free.
    target_include_directories(jgap_lib PUBLIC
            "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
            "$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/generated>"
            "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
    )

    target_compile_definitions(jgap_lib PUBLIC EIGEN_FAST_MATH EIGEN_NO_DEBUG "JGAP_VERSION=\"${PROJECT_VERSION}\"")
    if (JGAP_BLAS_LIBS)
        target_compile_definitions(jgap_lib PUBLIC EIGEN_USE_BLAS)
    endif ()
    if (LAPACKE_FOUND)
        target_include_directories(jgap_lib PUBLIC ${LAPACKE_INCLUDE_DIR})
        target_compile_definitions(jgap_lib PUBLIC EIGEN_USE_LAPACKE "lapack_complex_float=std::complex<float>" "lapack_complex_double=std::complex<double>")
    endif ()

    # Standalone CLI tools (skipped when building Python wheels)
    add_executable(jgap
            ${PROJECT_SOURCE_DIR}/src/app/MainApp.cpp
    )
    target_link_libraries(jgap PRIVATE jgap_lib)

    # QUIP XML -> .h5 converter app
    add_executable(jgap_convert ${PROJECT_SOURCE_DIR}/src/app/ConvertApp.cpp)
    target_link_libraries(jgap_convert PRIVATE jgap_lib pugixml::pugixml)

    add_custom_command(TARGET jgap_lib POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_SOURCE_DIR}/resources
            ${CMAKE_CURRENT_BINARY_DIR}/resources
    )

    add_custom_command(TARGET jgap_lib POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_directory
            ${CMAKE_CURRENT_SOURCE_DIR}/test/resources
            ${CMAKE_CURRENT_BINARY_DIR}/test/resources
    )
endif ()

# 4. Python Bindings
include(PythonBindings)

# 5. Installation Rules
include(Installation)

# 6. Unit Tests (Debug Mode)
include(Testing)

# 7. Automated Validation Suite
include(Validation)
