cmake_minimum_required(VERSION 3.21.2)

### Setup project name and language
project(CMT VERSION 1.2
        DESCRIPTION "A CMake project template"
        HOMEPAGE_URL "https://github.com/DavidAce/CMakeTemplate"
        LANGUAGES CUDA C CXX)



### Set up options
option(CMT_ENABLE_OPENMP   "Enable OpenMP flags such as -fopenmp"                 OFF)
option(CMT_ENABLE_MPI      "Enables use of MPI (work in progress)"                OFF)
option(CMT_ENABLE_COVERAGE "Adds the --coverage flag (used with github actions)"  OFF)
option(CMT_BUILD_BENCH     "Builds benchmarks in ./bench"                         OFF)
option(CMT_BUILD_EXAMPLES  "Builds examples in ./examples"                        OFF)
option(CMT_BUILD_TESTS     "Builds unit tests in ./tests"                         OFF)
option(CMT_CMAKE_DEBUG     "Print info during CMake configuration"                OFF)



if(CMT_DEBUG_CMAKE)
    ### Print operating system details
    include(cmake/PrintHostInfo.cmake)
endif()

### Add all source files
file(GLOB_RECURSE CUDA_SOURCES "source/*.cu")
file(GLOB_RECURSE CXX_SOURCES "source/*.cpp")
file(GLOB_RECURSE HEADERS "source/*.h")
add_executable(${PROJECT_NAME} ${CUDA_SOURCES} ${CXX_SOURCES} ${HEADERS})
target_include_directories(${PROJECT_NAME} PRIVATE source)



### Find dependencies
find_package(h5pp       1.11.2  REQUIRED)
find_package(spdlog     1.13.0  REQUIRED)
find_package(pcg-cpp            REQUIRED)
find_package(CLI11      2.3.2   REQUIRED)


### Create a helper target that links dependencies (this target can be used to build tests and benchmarks)
add_library(cmt-libs INTERFACE)

# Link the libraries to the helper target
target_link_libraries(cmt-libs INTERFACE h5pp::h5pp)
target_link_libraries(cmt-libs INTERFACE spdlog::spdlog )
target_link_libraries(cmt-libs INTERFACE pcg-cpp::pcg-cpp )
target_link_libraries(cmt-libs INTERFACE CLI11::CLI11)

if(CMT_ENABLE_OPENMP)
    find_package(OpenMP COMPONENTS CXX REQUIRED)
    target_link_libraries(cmt-libs INTERFACE OpenMP::OpenMP_CXX)
endif()

if(CMT_ENABLE_MPI)
    find_package(MPI COMPONENTS CXX REQUIRED)
    target_link_libraries(cmt-libs INTERFACE MPI::MPI_CXX)
endif()


### Link targets to the main executable
target_link_libraries(CMT PUBLIC cmt-libs)


################################################################
### Get git version number                                   ###
### Generates a header gitversion/gitversion.h               ###
### Include it using #include <gitversion.h>                 ###
### Gives a namespace GIT:: with several git identifiers     ###
################################################################
include(cmake/gitversion.cmake)


if(CMT_CMAKE_DEBUG)
    # Print summary of CMake configuration
    include(cmake/PrintTargetInfo.cmake)
    print_and_write_project_summary(CMT)
endif()
