list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/../cmake)

find_package(Boost)
find_package(MPI)
find_package(OpenMP)
find_package(TBB)

include(CheckLanguage)
check_language(CUDA)
check_language(HIP)

include_directories(..)
link_libraries(trng4::trng4)
link_directories(${PROJECT_BINARY_DIR}/trng)

function(add_executable_and_copy_dlls target)
  add_executable(${target} ${ARGN})
  if(WIN32 AND BUILD_SHARED_LIBS)
    add_custom_command(TARGET ${target} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        $<TARGET_RUNTIME_DLLS:${target}>
        $<TARGET_FILE_DIR:${target}>
        COMMAND_EXPAND_LISTS)
  endif()
endfunction()

add_executable_and_copy_dlls(hello_world hello_world.cc)
add_executable_and_copy_dlls(sample_output sample_output.cc)
add_executable_and_copy_dlls(time time.cc)
if(Boost_FOUND)
  target_compile_definitions(time PUBLIC TRNG_HAVE_BOOST)
  target_link_libraries(time PUBLIC Boost::boost)
endif()
add_executable_and_copy_dlls(pi pi.cc)
if(MPI_FOUND)
  add_executable_and_copy_dlls(pi_block_mpi pi_block_mpi.cc)
  target_link_libraries(pi_block_mpi PUBLIC MPI::MPI_CXX)
  add_executable_and_copy_dlls(pi_leap_mpi pi_leap_mpi.cc)
  target_link_libraries(pi_leap_mpi PUBLIC MPI::MPI_CXX)
endif()
if(OpenMP_FOUND)
  add_executable_and_copy_dlls(pi_block_openmp pi_block_openmp.cc)
  target_link_libraries(pi_block_openmp PUBLIC OpenMP::OpenMP_CXX)
  add_executable_and_copy_dlls(pi_leap_openmp pi_leap_openmp.cc)
  target_link_libraries(pi_leap_openmp PUBLIC OpenMP::OpenMP_CXX)
endif()
if(TBB_FOUND)
  add_executable_and_copy_dlls(pi_block_tbb pi_block_tbb.cc)
  target_link_libraries(pi_block_tbb PUBLIC tbb)
endif()
add_executable_and_copy_dlls(stl_container stl_container.cc)
add_executable_and_copy_dlls(cpp11 cpp11.cc)
add_executable_and_copy_dlls(Ising_model Ising_model.cc)
add_executable_and_copy_dlls(correlated_normal_dist correlated_normal_dist.cc)
add_executable_and_copy_dlls(discrete_dist discrete_dist.cc)
add_executable_and_copy_dlls(discrete_dist_c_style discrete_dist_c_style.cc)
add_executable_and_copy_dlls(bernoulli_dist bernoulli_dist.cc)
if(CMAKE_CUDA_COMPILER)
  enable_language(CUDA)
  add_executable_and_copy_dlls(pi_block_cuda pi_block_cuda.cu)
  set_property(TARGET pi_block_cuda PROPERTY CUDA_STANDARD 11)
  add_executable_and_copy_dlls(pi_leap_cuda pi_leap_cuda.cu)
  set_property(TARGET pi_leap_cuda PROPERTY CUDA_STANDARD 11)
endif()
if(CMAKE_HIP_COMPILER)
  enable_language(HIP)
   add_executable_and_copy_dlls(pi_block_hip pi_block_hip.hip)
   set_source_files_properties(pi_block_hip.hip PROPERTIES LANGUAGE HIP)
endif()
add_executable_and_copy_dlls(dieharder_file dieharder_file.cc)
