# This is a GridTools example.
# The important steps for compiling a GridTools application/library are numbered

cmake_minimum_required(VERSION 3.14.5)

# 1) GridTools needs the language CXX
project(GridTools-examples LANGUAGES CXX)

# enable CUDA if it is found on the system
include(../workaround_check_language.cmake) # see https://gitlab.kitware.com/cmake/cmake/issues/19013
_workaround_check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
    # 2) Enable the CUDA language if you want to run your code on a CUDA-capable GPU.
    #    This needs to be done before find_package(GridTools) to properly setup the GridTools targets for CUDA.
    enable_language(CUDA)
endif()

# 3) Find the GridTools installation
find_package(GridTools 2.0.0 REQUIRED
    HINTS /usr/lib/cmake/GridTools)

enable_testing()

set(EXAMPLES_SRCFILES boundaries boundaries_provided)
foreach(srcfile IN LISTS EXAMPLES_SRCFILES)
    # one executable for each example in the list
    add_executable(${srcfile}_cpu_ifirst ${srcfile}.cpp)
    # 4) An executable/library using GridTools stencil needs to link to backend, here GridTools::stencil_cpu_ifirst
    target_link_libraries(${srcfile}_cpu_ifirst GridTools::stencil_cpu_ifirst)
    add_test(NAME ${srcfile}_cpu_ifirst COMMAND $<TARGET_FILE:${srcfile}_cpu_ifirst> 33 44 55)
endforeach()

if(TARGET GridTools::stencil_gpu)
    set(EXAMPLE_CUDA_ARCH "" CACHE STRING "CUDA compute capability to be used for this example.")
    foreach(srcfile IN LISTS EXAMPLES_SRCFILES)
        add_executable(${srcfile}_gpu ${srcfile}.cpp)
        # The executable links to the target GridTools::stencil_gpu
        target_link_libraries(${srcfile}_gpu GridTools::stencil_gpu)
        gridtools_setup_target(${srcfile}_gpu CUDA_ARCH ${EXAMPLE_CUDA_ARCH})
        add_test(NAME ${srcfile}_gpu COMMAND $<TARGET_FILE:${srcfile}_gpu> 33 44 55)
    endforeach()
else()
    message("A CUDA-capable compiler was found, but GridTools was installed without CUDA support. Skipping the CUDA example...")
endif()
