libs =
import taskflow = libtaskflow%lib{taskflow}
import! nvcc = cuda-nvcc%exe{nvcc}
import libs += \
  $taskflow \
  doctest%lib{doctest}

define cu: file
cu{*}: extension = cu

exe{*}: test.arguments = --minimal

# See upstream CMakeLists.txt for upstream-disabled tests.
#
for t: cxx{test_* \
            -test_serializer.cpp \
            -test_priorities.cpp \
            -test_deferred_pipelines.cpp \
            -test_deferred_scalable_pipelines.cpp \
          }
{
  d = $directory($t)
  n = $name($t)...

  ./: exe{$n}: $t $d/{hxx ixx txx}{+$n} $d/testscript
  exe{$n}: hxx{doctest.h} $libs
}

exception_handling_enabled = (!$($taskflow: libtaskflow.disable_exception_handling))

# Requires exception handling
#
exe{test_basics}:     test = $exception_handling_enabled
exe{test_exceptions}: test = $exception_handling_enabled

## See upstream cuda/CMakeLists.txt for upstream-disabled tests.
#
for t: cuda/cu{test_* \
                -test_cuda_reduce \
                -test_cuda_scan \
                -test_cuda_find \
                -test_cuda_min_max_element \
                -test_cuda_merge \
                -test_cuda_capturer_optimizer \
                -test_cuda_capture \
              } \
        cuda/cu{cuda_* \
                -cuda_algorithms \
                -cuda_algorithm_updates \
              }
{
  d = $directory($t)
  n = $name($t)
  ./: exe{$n}: include = $($taskflow: libtaskflow.cuda)
  exe{$n}: obje{$(n).o}: $t
  exe{$n}: $d/{hxx ixx txx}{+$n} hxx{doctest.h} $libs
}

## Compile all .cu files with nvcc (Nvidias compiler)
#
libue{meta}: $libs
obje{~'/(test_cuda_.*)/'}: cu{~'/\1/'} libue{meta} $nvcc
{{
  cu = $filter($<, cu)
  lib_poptions = $cxx.lib_poptions(libue{meta}, obje)
  depdb hash $lib_poptions

  diag nvcc $cu $>

  poptions = $regex.apply($cc.poptions $cxx.poptions $lib_poptions $cc.coptions $cxx.coptions, '\\', '/')
  std = $regex.apply($cxx.mode, '[-/]std[:=](.+)', '-std=\1', format_no_copy)
  mode = $regex.merge($cxx.mode, ' ', '.*', ',')
  $path($nvcc) -rdc=false --expt-extended-lambda -x cu -Xcompiler=$mode -allow-unsupported-compiler \
               $poptions $std -arch=native \
               -o $path($>) -c $path($<[0])
}}

cxx.poptions =+ "-I$out_root" "-I$src_base"
