cmake_minimum_required(VERSION 3.16)
project(Aurora)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(Aurora_USE_CUDA ON)

if (Aurora_USE_CUDA)
set(CMAKE_CUDA_COMPILER /usr/local/cuda/bin/nvcc)
enable_language(CUDA)
find_package(CUDAToolkit REQUIRED)
add_definitions(-DUSE_CUDA)
endif(Aurora_USE_CUDA)

find_package (OpenMP REQUIRED)
set(CMAKE_C_FLAGS  "${CMAKE_C_FLAGS}  -march=native")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -march=native")
if(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()

set(MKL_INTERFACE_FULL intel_lp64)
find_package(MKL CONFIG REQUIRED)
include_directories(
./ ./src /usr/local/include/eigen3 ./thirdparty/include)
file(GLOB_RECURSE  cpp_files ./src/*.cpp)
file(GLOB_RECURSE  cxx_files ./src/*.cxx)

if (Aurora_USE_CUDA)
file(GLOB_RECURSE  cu_files ./src/*.cu)
file(GLOB_RECURSE  cuh_files ./src/*.cuh)
endif(Aurora_USE_CUDA)

add_executable(Aurora ${cpp_files} ${cxx_files} ${cu_files} ${cuh_files})
target_compile_options(Aurora PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
target_include_directories(Aurora PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
target_link_libraries(Aurora PUBLIC $<LINK_ONLY:MKL::MKL>)
target_link_libraries(Aurora PUBLIC OpenMP::OpenMP_CXX)
target_link_libraries(Aurora PUBLIC matio)
if (Aurora_USE_CUDA)
target_include_directories(Aurora PRIVATE ./src /usr/local/cuda/include) 
set_target_properties(Aurora PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_compile_options(Aurora PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:
    -arch=sm_75
>)
target_link_libraries(Aurora PRIVATE ${CUDA_RUNTIME_LIBRARY} CUDA::cufft CUDA::cudart)
endif(Aurora_USE_CUDA)

find_package(GTest  REQUIRED)
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS})
include_directories(./src/util)

file(GLOB_RECURSE  test_cpp ./test/*.cpp)

enable_testing()

add_executable(Aurora_Test  ${cpp_files} ${test_cpp} )
target_include_directories(Aurora_Test PUBLIC ./test/)
target_compile_options(Aurora_Test PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_COMPILE_OPTIONS>)
target_include_directories(Aurora_Test PUBLIC $<TARGET_PROPERTY:MKL::MKL,INTERFACE_INCLUDE_DIRECTORIES>)
target_link_libraries(Aurora_Test PUBLIC $<LINK_ONLY:MKL::MKL>)
target_link_libraries(Aurora_Test PUBLIC OpenMP::OpenMP_CXX)
target_link_libraries(Aurora_Test PUBLIC matio)
target_link_libraries(Aurora_Test PUBLIC ${GTEST_BOTH_LIBRARIES}  )

if (Aurora_USE_CUDA)
target_include_directories(Aurora_Test PRIVATE ./src /usr/local/cuda/include) 
set_target_properties(Aurora_Test PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_compile_options(Aurora_Test PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:
    -arch=sm_75
>)
target_link_libraries(Aurora_Test PRIVATE ${CUDA_RUNTIME_LIBRARY} CUDA::cufft CUDA::cudart)
endif(Aurora_USE_CUDA)
gtest_discover_tests(Aurora_Test )
#target_link_libraries(CreateMatchedFilter PRIVATE TBB::tbb)