import libs  = libangelscript-addon%lib{angelscript-addon}

# NOTE: Don't name executables the same as the upstream dir symlinks.

for d : dir{test_* -test_build_performance}
{
  n = $name($d)
  ./: exe{$(n)_exe}: libul{$(n)_exe}: \
    add_on/hxx{**} \
    $n/{hxx cxx}{source/** -source/teststdstring -source/test_addon_scriptsocket} \
    $libs
}

# Only test_feature is run as a test, the performance tests are benchmarks.
#
exe{*}: test = false
exe{test_feature_exe}: test = true
exe{test_feature_exe}: testscript

# Scripts that test_feature opens by relative path (see testscript).
#
exe{test_feature_exe}: file{test_feature/bin/scripts/**}: include = adhoc

# Upstream tests register test suites via static initializers; keep whole-archive
# so all objects are linked even if unreferenced.
#
libul{*}: bin.whole = true

# Patched copies of test_feature/source/ files (see the .patch files), with
# utils.h and scriptstring.h shims so that their #include "utils.h" resolves
# next to them.
#
exe{test_feature_exe}: cxx{teststdstring test_addon_scriptsocket} \
                       hxx{utils scriptstring}

# test_build_performance is Windows-only.
#
./: exe{test_build_performance_exe}: include = ($cxx.target.class == 'windows')
exe{test_build_performance_exe}: libul{test_build_performance_exe}: \
  add_on/hxx{**} test_build_performance/{hxx cxx}{source/**} $libs
obj{test_build_performance/**}: \
  cxx.poptions += "-I$src_base/test_build_performance/source"

# Build options.
#

# Benign warnings in upstream test code: unused entities, deprecated or unsafe
# functions, ISO-8859-1 sources, as well as sloppy but intentional code (sign
# comparisons, member initialization order, format strings, offsetof on
# non-standard-layout types, and the like). Keep this before any
# target-specific cxx.coptions append below, since those copy the value at
# that point.
#
if ($cxx.class == 'gcc')
{
  cxx.coptions += -Wno-unused -Wno-unused-parameter -Wno-deprecated-declarations \
                  -Wno-sign-compare -Wno-reorder -Wno-format -Wno-parentheses \
                  -Wno-attributes -Wno-address -Wno-invalid-offsetof \
                  -Wno-delete-non-virtual-dtor -Wno-deprecated-copy \
                  -Wno-ignored-qualifiers -Wno-cast-function-type \
                  -Wno-type-limits

  if ($cxx.id.type == 'clang')
    cxx.coptions += -Wno-invalid-source-encoding -Wno-self-assign \
                    -Wno-tautological-undefined-compare
  else
  {
    cxx.coptions += -Wno-maybe-uninitialized

    # -Walloc-size is only known to GCC 14 or later.
    #
    if ($cxx.version.major >= 14)
      cxx.coptions += -Wno-alloc-size
  }

  # -Winvalid-utf8 is only known to GCC 13 and Clang 15 or later.
  #
  if (($cxx.id == 'gcc'        && $cxx.version.major >= 13) || \
      ($cxx.id.type == 'clang' && $cxx.version.major >= 15))
    cxx.coptions += -Wno-invalid-utf8
}
else
  cxx.coptions += -wd4996 -wd4100 -wd4101 -wd4189 -wd4244

if ($cxx.target.system == 'win32-msvc')
{
  # Explicitly define WIN32, it is used by upstream for conditional includes.
  cxx.poptions += -DWIN32

  if ($cxx.id == 'msvc')
  {
    # ISO-8859-1 sources: suppress UTF-8 charset warning on MSVC.
    #
    cxx.coptions += -wd4828
  }
  elif ($cxx.id == 'clang')
  {
    # test_registertype.cpp has an intentionally ill-formed array in an
    # unused template under _MSC_VER, which only MSVC tolerates. Delay
    # template parsing so that Clang skips it as well.
    #
    obj{test_feature/source/test_registertype}: \
      cxx.coptions += -fdelayed-template-parsing \
                      -Wno-delayed-template-parsing-in-cxx20
  }
}

# For __imp_timeGetTime
#
if ($cxx.target.system == 'mingw32')
  cxx.libs += -lwinmm
elif ($cxx.target.class == 'windows')
  cxx.libs += winmm.lib

# Upstream relies on relative includes to 'add_on/', so we use dummy include
# directories to satisfy them.
#
./: file{1/2/3/leave.me}
cxx.poptions =+ "-I$src_base/1/2" "-I$src_base/1/2/3"

if ($cxx.target.class == 'macos')
{
  # Upstream test_build_performance.cpp unconditionally include e.g. <malloc.h>
  # which doesn't exist on macos, but <malloc/malloc.h> usually does.
  #
  malloc_hdr = [path] $cxx.find_system_header('malloc/malloc.h')
  if(!$null($malloc_hdr))
    obj{test_feature/source/test_registertype}: \
      cxx.poptions += "-I$directory($malloc_hdr)"

  # memalign does not exist on macos, but posix_memalign does. Hack it in place.
  # 
  obj{test_feature/source/test_registertype}: \
    cxx.poptions += '-Dmemalign(a,s)=({void*p=nullptr;posix_memalign(&p,a,s)==0?p:nullptr;})'
}
if ($cxx.class == 'gcc')
{
  # test_registertype.cpp uses std:: math functions without including <cmath>.
  #
  if ($cxx.target.class != 'windows')
    obj{test_feature/source/test_registertype}: \
      cxx.coptions += -include 'cmath'

  # test_saveload.cpp reads an asQWORD through int* (type punning).
  #
  obj{test_feature/source/test_saveload}: cxx.coptions += -fno-strict-aliasing
}
