import intf_libs = \
  cuda-nvcc%lib{nvcc} \
  cuda-nvrtc%libs{nvrtc}

name = $regex.replace($project, 'cuda-', '')

source redist.build
define [see_through] import: group

./: lib{$name}: import{legal} import{$name} $intf_libs

redist{cuda_$name}: $src_root/manifest
archive{$cc.target.class-$cc.target.cpu-$version-archive.tar}: redist{cuda_$name}
extract{archive}: archive{$cc.target.class-$cc.target.cpu-$version-archive.tar}
import{$name}: extract{archive}: update = match
import{legal}: extract{archive}: update = match

import{legal}:
{{
  o = $directory($>[0])
  d = $o/$name($>[0]).files

  legal = [path] "$out_base/LICENSE"

  depdb dyndep \
    --dyn-target \
    --target-cwd "$o" \
    --target-what 'imported files' \
    --target-default-type 'legal' \
    --format lines \
    --file $d \
    -- \
      echo $legal >$d

  diag import $>

  for t : $filter_out($>, group)
    touch $path($t)
  end
}}
import{$name}:
{{
  o = $directory($>[0])
  d = $o/$(name).files

  find "$out_base/include/" -type f -name "*" \
    | set hdrs [paths]

  depdb dyndep \
    --dyn-target \
    --target-cwd "$o" \
    --target-what 'imported files' \
    --target-default-type 'hxx' \
    --target-extension-type '=h' \
    --target-extension-type 'h=hxx' \
    --target-extension-type 'hpp=hxx' \
    --target-extension-type 'inl=hxx' \
    --target-extension-type 'cuh=hxx' \
    --target-extension-type 'a=liba' \
    --target-extension-type 'so=libs' \
    --target-extension-type 'dylib=libs' \
    --target-extension-type 'dll=libs' \
    --target-extension-type 'lib=liba' \
    --format lines \
    --file $d \
    -- \
      echo $hdrs >$d &!$d

  diag import $>

  for t : $filter_out($>, group)
    touch $path($t)
  end
}}

# Export options.

lib{$name}:
{
  cc.type = cxx
  cc.export.poptions = "-I$out_base/include" "-I$out_base/include/cccl"
  cc.export.libs = $intf_libs
}

lib/liba{**}: install = false

exe{*}:
{
  install         = bin/
}

include/hxx{*}:
{
  install         = include/$project/
  install.subdirs = true
}
