cccl_get_c2h()

find_package(cudax) # already found, bring in version info.

if (cudax_ENABLE_CUFILE)
  find_package(CUDAToolkit REQUIRED)
endif()

## cudax_add_test
#
# Add a catch2 test executable and register it with ctest.
#
# target_name_var: Variable name to overwrite with the name of the test
#   target. Useful for post-processing target information.
# test_name: A unique name for the executable that will be appended to
#  "<config_prefix>.test.".
# cudax_target: The reference cudax target with configuration information.
#
# Additional arguments will be processed as test sources.
#
function(cudax_add_catch2_test target_name_var test_name cudax_target) # ARGN=test sources
  cudax_get_target_property(config_prefix ${cudax_target} PREFIX)
  cudax_get_target_property(config_dialect ${cudax_target} DIALECT)

  set(test_target ${config_prefix}.test.${test_name})
  set(test_sources ${ARGN})

  add_executable(${test_target} ${test_sources})
  cccl_configure_target(${test_target} DIALECT ${config_dialect})
  cudax_clone_target_properties(${test_target} ${cudax_target})
  target_include_directories(${test_target} PRIVATE "common")
  target_link_libraries(
    ${test_target}
    PRIVATE #
      ${cudax_target}
      cccl.c2h.main
      cudart
  )
  target_compile_options(
    ${test_target}
    PRIVATE
      "-DLIBCUDACXX_ENABLE_EXPERIMENTAL_MEMORY_RESOURCE"
      $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:--extended-lambda>
  )

  set(config_meta_target ${config_prefix}.tests)
  add_dependencies(${config_meta_target} ${test_target})

  add_test(NAME ${test_target} COMMAND "$<TARGET_FILE:${test_target}>")

  set(${target_name_var} ${test_target} PARENT_SCOPE)
endfunction()

# Create tests for each enabled configuration:
foreach (cudax_target IN LISTS cudax_TARGETS)
  cudax_get_target_property(config_prefix ${cudax_target} PREFIX)

  # Metatarget for the current configuration's tests:
  set(config_meta_target ${config_prefix}.tests)
  add_custom_target(${config_meta_target})
  add_dependencies(${config_prefix}.all ${config_meta_target})

  cudax_add_catch2_test(test_target launch ${cudax_target}
    launch/launch_smoke.cu
  )

  cudax_add_catch2_test(test_target launch_configuration ${cudax_target}
    launch/configuration.cu
  )

  cudax_add_catch2_test(test_target launch_config_dynamic_smem ${cudax_target}
    launch/dynamic_shared_memory.cu
  )

  cudax_add_catch2_test(test_target execution ${cudax_target}
    execution/env.cu
    execution/policies/policies.cu
    execution/policies/get_execution_policy.cu
    execution/test_bulk.cu
    execution/test_concepts.cu
    execution/test_completion_signatures.cu
    execution/test_conditional.cu
    execution/test_continues_on.cu
    execution/test_just.cu
    execution/test_let_value.cu
    execution/test_on.cu
    execution/test_sequence.cu
    execution/test_starts_on.cu
    execution/test_stream_context.cu
    execution/test_then.cu
    execution/test_visit.cu
    execution/test_when_all.cu
    execution/test_write_attrs.cu
    execution/test_write_env.cu
  )

  target_compile_options(
    ${test_target}
    PRIVATE $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-allow-unsupported-compiler>
  )

  cudax_add_catch2_test(test_target graph ${cudax_target}
    graph/graph_smoke.cu
  )

  cudax_add_catch2_test(test_target stream ${cudax_target}
    stream/stream_smoke.cu
  )

  cudax_add_catch2_test(test_target misc ${cudax_target}
    utility/ensure_current_device.cu
  )

  cudax_add_catch2_test(test_target containers ${cudax_target}
    containers/uninitialized_buffer.cu
    containers/async_buffer/access.cu
    containers/async_buffer/capacity.cu
    containers/async_buffer/constructor.cu
    containers/async_buffer/conversion.cu
    containers/async_buffer/copy.cu
    containers/async_buffer/iterators.cu
    containers/async_buffer/properties.cu
    containers/async_buffer/swap.cu
    containers/async_buffer/transform.cu
  )

  cudax_add_catch2_test(test_target cuco ${cudax_target}
    cuco/utility/test_hashers.cu
  )

  cudax_add_catch2_test(test_target green_context ${cudax_target}
    green_context/green_ctx_smoke.cu
  )

  cudax_add_catch2_test(test_target kernel ${cudax_target}
    kernel/kernel_ref.cu
  )

  cudax_add_catch2_test(test_target library_ref ${cudax_target}
    library/library_ref.cu
  )

  cudax_add_catch2_test(test_target library ${cudax_target}
    library/library.cu
  )

  cudax_add_catch2_test(test_target algorithm ${cudax_target}
    algorithm/fill.cu
    algorithm/copy.cu
  )

  if (cudax_ENABLE_CUFILE)
    cudax_add_catch2_test(cufile_driver_target cufile_driver ${cudax_target}
      cufile/driver.cu
    )

    set(cufile_test_targets ${cufile_driver_target})
    foreach (cufile_test_target IN LISTS cufile_test_targets)
      target_link_libraries(${cufile_test_target} PRIVATE CUDA::cuFile)
    endforeach()
  endif()
endforeach()

# FIXME: Enable MSVC
if (cudax_ENABLE_CUDASTF AND NOT "MSVC" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
  # STF tests are handled separately:
  add_subdirectory(stf)
endif()
