if(POLICY CMP0079)  
    cmake_policy(SET CMP0079 NEW)
endif()

# Define modules and library
tbp_clear_vars(tbp_modules)
set(tbp_modules
    adapter
    base
    builder
    diag
    materials
    tbpm)
if(WITH_CUDA)
    list(APPEND tbp_modules cuda)
endif()
add_library(tbplas tbplas.cpp)
target_link_libraries(tbplas PUBLIC ${tbp_modules})

# ========================== TBPLAS modules ==========================
message(STATUS "")
message(STATUS "  Modules to build:")

foreach(__module ${tbp_modules})
  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${__module})
  message(STATUS "    " ${__module})
endforeach()

unset(tbp_modules)
#install target tbplas
if(UNIX)
if(BUILD_SHARED_LIBS)
    install(TARGETS tbplas
            EXPORT tbplasTargets
            LIBRARY DESTINATION ${TBPLAS_LIB_INSTALL_PATH}
            DESTINATION ${TBPLAS_LIB_INSTALL_PATH})
else()
    install(TARGETS tbplas
            EXPORT tbplasTargets
            ARCHIVE DESTINATION ${TBPLAS_LIB_INSTALL_PATH}
            DESTINATION ${TBPLAS_LIB_INSTALL_PATH})
endif(BUILD_SHARED_LIBS)
endif(UNIX)
if(WIN32)
    install(TARGETS tbplas
        EXPORT tbplasTargets
        ARCHIVE DESTINATION ${TBPLAS_LIB_ARCHIVE_INSTALL_PATH}
        DESTINATION ${TBPLAS_LIB_ARCHIVE_INSTALL_PATH})
endif(WIN32)
#install files *.cmake
set(INSTALL_CMAKE_DIR ${TBPLAS_LIB_ARCHIVE_INSTALL_PATH}/cmake/tbplas)
if(WITH_OPENMP)
  install(FILES
   ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/TBPLASDetectOpenMP.cmake
    DESTINATION ${INSTALL_CMAKE_DIR}
)
endif(WITH_OPENMP)
if(WITH_MPI)
  install(FILES
  ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/TBPLASDetectMPI.cmake
   DESTINATION ${INSTALL_CMAKE_DIR}
)
endif(WITH_MPI)
if(WITH_CUDA)
  install(FILES
  ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/TBPLASDetectCUDA.cmake
   DESTINATION ${INSTALL_CMAKE_DIR}
)
endif(WITH_CUDA)
if(WITH_HDF5)
  install(FILES
  ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/TBPLASDetectHDF5.cmake
   DESTINATION ${INSTALL_CMAKE_DIR}
)
endif(WITH_HDF5)
if(WITH_FEAST)
  set(FEAST_ROOT "$ENV{FEASTROOT}")
endif(WITH_FEAST)
set(AOCL_ROOT "$ENV{AOCL_ROOT}")

install(
    EXPORT tbplasTargets                
    FILE tbplasTargets.cmake            
    DESTINATION ${INSTALL_CMAKE_DIR}     
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    tbplasConfigVersion.cmake
    VERSION ${TBPLAS_VERSION}
    COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/TBPLASConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/tbplasConfig.cmake
    INSTALL_DESTINATION ${INSTALL_CMAKE_DIR}
)
install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/tbplasConfig.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/tbplasConfigVersion.cmake
    ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/TBPLASMathLib.cmake
    DESTINATION ${INSTALL_CMAKE_DIR}
)
