cmake_minimum_required(VERSION 3.27)

project(bayesnet 
    LANGUAGES CXX
    DESCRIPTION "x"
    HOMEPAGE_URL "https://github.com/rmontanana/bayesnet"
    VERSION 1.1.1
)
set(CMAKE_CXX_STANDARD 17)
cmake_policy(SET CMP0135 NEW)

find_package(Torch CONFIG REQUIRED)
find_package(fimdlp CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(folding CONFIG REQUIRED)


# Options
# -------
option(ENABLE_TESTING OFF)
option(ENABLE_SAMPLE  OFF)
option(COVERAGE       OFF)

add_subdirectory(config)

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-elide-constructors")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-default-inline")
endif()

if (ENABLE_TESTING)
    find_package(Catch2 CONFIG REQUIRED)
    find_package(arff-files CONFIG REQUIRED)
    message("Debug mode")
    enable_testing()
    set(CODE_COVERAGE ON)
    set(GCC_COVERAGE_LINK_FLAGS "${GCC_COVERAGE_LINK_FLAGS} -lgcov --coverage")
    add_subdirectory(tests)
else()
    message("Release mode")
endif()

if (ENABLE_SAMPLE)
    message("Building sample")
    add_subdirectory(sample)
endif()


# Add the library
# ---------------
include_directories(
    ${bayesnet_SOURCE_DIR}
    ${CMAKE_BINARY_DIR}/configured_files/include
)

file(GLOB_RECURSE Sources "bayesnet/*.cc")

add_library(bayesnet ${Sources})
target_link_libraries(bayesnet fimdlp::fimdlp folding::folding "${TORCH_LIBRARIES}")


# Installation
# ------------
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/bayesnetConfigVersion.cmake"
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY AnyNewerVersion
)

install(TARGETS bayesnet
        EXPORT bayesnetTargets
        ARCHIVE DESTINATION lib
        LIBRARY DESTINATION lib
        CONFIGURATIONS Release)

install(DIRECTORY bayesnet/ DESTINATION include/bayesnet FILES_MATCHING CONFIGURATIONS Release PATTERN "*.h")
install(FILES ${CMAKE_BINARY_DIR}/configured_files/include/bayesnet/config.h DESTINATION include/bayesnet CONFIGURATIONS Release)


install(EXPORT bayesnetTargets
        FILE bayesnetTargets.cmake
        NAMESPACE bayesnet::
        DESTINATION lib/cmake/bayesnet)

configure_file(bayesnetConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/bayesnetConfig.cmake" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/bayesnetConfig.cmake"
              "${CMAKE_CURRENT_BINARY_DIR}/bayesnetConfigVersion.cmake"
        DESTINATION lib/cmake/bayesnet)

