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

cmake_minimum_required(VERSION @CMAKE_MINIMUM_REQUIRED_VERSION@)

# 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 @GridTools_VERSION@ REQUIRED
    HINTS @CMAKE_INSTALL_PREFIX@/lib/cmake)

enable_testing()

set(EXAMPLES_SRCFILES @EXAMPLES_SRCFILES@)
foreach(srcfile IN LISTS EXAMPLES_SRCFILES)
    # one executable for each example in the list
    add_executable(${srcfile}_mc ${srcfile}.cpp)
    # 4) An executable/library using GridTools needs to link to the target GridTools::gridtools
    #    if it is using header-only modules.
    target_link_libraries(${srcfile}_mc GridTools::gridtools)
    add_test(NAME ${srcfile}_mc COMMAND $<TARGET_FILE:${srcfile}_mc> 33 44 55)
endforeach()

if(CMAKE_CUDA_COMPILER)
    if(GRIDTOOLS_HAS_BACKEND_CUDA)
        # If a CUDA compiler was found AND the GridTools installation was installed with CUDA enabled we can compile the CUDA example
        foreach(srcfile IN LISTS EXAMPLES_SRCFILES)
            add_executable(${srcfile}_cuda ${srcfile}.cu)
            # The executable links to the same target GridTools::gridtools as in the case of the "mc" backend
            target_link_libraries(${srcfile}_cuda GridTools::gridtools)
            add_test(NAME ${srcfile}_cuda COMMAND $<TARGET_FILE:${srcfile}_cuda> 33 44 55)
        endforeach()
    else()
        message("A CUDA-capable compiler was found, but GridTools was installed without CUDA support. Skipping the CUDA example...")
    endif()
endif()
