import intf_libs = \
  cuda-crt%lib{crt}
import nvvm = libnvvm%dir{nvvm}

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

source redist.build
define [see_through] import: group

./: lib{$name}: import{legal} import{$name-headers} $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{legal}: extract{archive}: update = match
import{$name-headers}: 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-headers}: fsdir{include}
{{
  o = $directory($>[0])
  d = $o/$name($>[0]).files

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

  depdb dyndep \
    --dyn-target \
    --target-cwd "$o" \
    --target-what 'headers' \
    --target-default-type 'h' \
    --target-extension-type '=h' \
    --format lines \
    -- echo $regex.merge($f, '(.+)', '\1\n')

  diag import $>

  for f : $filter($>, h)
    touch $path($f)
  end
}}

## @NOTE: CUDA Toolkit v12 -> v13 removed 'nvvm/' from the cuda-nvcc
#         archive, but nvcc.exe (and friends) still require dir 'nvvm'
#         to exist relative to 'dir(nvcc.exe)/../nvvm/'. It's all
#         impressively hacky & hard-coded, but hopefully it'll be fixed
#         (eg. allow overriding envar CICC_PATH).
#         For now, we import & symlink.
#
fsdir{nvvm}: $nvvm
% update
{{
  ln --symbolic $path($<) $directory($>)/nvvm
}}
% clean
{{
  rm $path($>)
}}

## import exe{<name>} targets
#
./: bin/exe{*}
< \
  bin/exe{bin2c.exe} \
  bin/exe{cudafe++.exe} \
  bin/exe{fatbinary.exe} \
  bin/exe{nvcc.exe} \
  bin/exe{nvlink.exe} \
  bin/exe{ptxas.exe} \
> \
: extract{archive} fsdir{bin} fsdir{nvvm}
{{
  o = $directory($>[0])
  d = $o/$name($>[0]).files
  e = $filter($<, exe)

  find "$out_base/bin" -type f -name "*\.exe" \
    | set e [path]

  depdb dyndep \
    --dyn-target \
    --target-cwd "$o" \
    --target-what 'imported executables' \
    --target-default-type 'exe' \
    --target-extension-type '=exe' \
    --target-extension-type 'exe=exe' \
    --format lines \
    -- \
      echo $regex.merge($e, '(.+)', '\1\n')

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

if ($cc.target.system == 'mingw32')
  c.loptions += -Wl,--export-all-symbols

# Export options.

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

lib/liba{**}: install = false

exe{*}:
{
  install         = bin/
  install.subdirs = true
}

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