intf_libs = # Interface dependencies.
impl_libs = # Implementation dependencies.

# Public headers.
#
pub = [dir_path] ../include/

include $pub

pub_hdrs = $($pub/ pub_hdrs)

lib{box3d}: $pub/{$pub_hdrs}

# Private headers and sources as well as dependencies.
#
lib{box3d}: {h c}{**} $impl_libs $intf_libs

# Make .inl private headers available to the compilation (included from .c).
#
lib{box3d}: file{**.inl **.natvis}: include = adhoc

# Build options.
#
out_pfx_inc = [dir_path] $out_root/include/
src_pfx_inc = [dir_path] $src_root/include/
out_pfx_src = [dir_path] $out_root/src/
src_pfx_src = [dir_path] $src_root/src/

c.poptions =+ "-I$out_pfx_src" "-I$src_pfx_src" \
              "-I$out_pfx_inc" "-I$src_pfx_inc"

# Map config.libbox3d.* to BOX3D_* macros.
#
if $config.libbox3d.disable_simd
  c.poptions += -DBOX3D_DISABLE_SIMD

if $config.libbox3d.double_precision
  c.poptions += -DBOX3D_DOUBLE_PRECISION

if $config.libbox3d.enable_assert
  c.poptions += -DB3_ENABLE_ASSERT

if $config.libbox3d.validate
  c.poptions += -DBOX3D_VALIDATE

# Deterministic math (match upstream CMake: -ffp-contract=off).
# Hidden visibility (match upstream CMake: CMAKE_C_VISIBILITY_PRESET hidden).
#
if ($c.id != 'msvc')
{
  c.coptions += -ffp-contract=off
  objs{*}: c.coptions += -fvisibility=hidden
}

# Emscripten requires explicit flags to enable WebAssembly SIMD and map SSE2
# intrinsics onto it (match upstream CMake: -msimd128 -msse2).
#
if (!$config.libbox3d.disable_simd && $c.target.system == 'emscripten')
  c.coptions += -msimd128 -msse2

# Shared library export: upstream uses box3d_EXPORTS when building the shared
# library and BOX3D_DLL when consuming the Windows DLL.
#
objs{*}: c.poptions += -Dbox3d_EXPORTS

# System libraries.
#
if ($c.target.system != 'win32-msvc')
{
  # libm for sqrtf and friends on Unix-like systems.
  #
  # pthread for the worker-thread implementation in timer.c & scheduler.c.
  #
  c.libs += -lm -pthread
}

# Export options.
#
lib{box3d}:
{
  c.export.poptions = "-I$out_pfx_inc" "-I$src_pfx_inc"
  c.export.libs = $intf_libs

  # Export configuration as target metadata so consumers and the tests
  # subproject can query compile-time options.
  #
  export.metadata = 1 libbox3d

  libbox3d.disable_simd     = [bool] $config.libbox3d.disable_simd
  libbox3d.double_precision = [bool] $config.libbox3d.double_precision
  libbox3d.enable_assert    = [bool] $config.libbox3d.enable_assert
  libbox3d.validate         = [bool] $config.libbox3d.validate
}

# ABI: consumers must compile with the same precision mode as the library.
#
if $config.libbox3d.double_precision
  lib{box3d}: c.export.poptions += -DBOX3D_DOUBLE_PRECISION

# Upstream declares B3_ENABLE_ASSERT PUBLIC so consumers see active asserts too.
#
if $config.libbox3d.enable_assert
  lib{box3d}: c.export.poptions += -DB3_ENABLE_ASSERT

# Consumers of the Windows shared library need BOX3D_DLL for dllimport.
#
if ($c.target.class == 'windows')
  libs{box3d}: c.export.poptions += -DBOX3D_DLL

# Re-export system libs that are part of the public link interface.
#
if ($c.target.system != 'win32-msvc')
  lib{box3d}: c.export.libs += -lm

# For pre-releases use the complete version to make sure they cannot
# be used in place of another pre-release or the final version. See
# the version module for details on the version.* variable values.
#
if $version.pre_release
  lib{box3d}: bin.lib.version = "-$version.project_id"
else
  lib{box3d}: bin.lib.version = "-$version.major.$version.minor"

# Don't install private headers.
#
h{*}: install = false

file{**.natvis}: install = share/box3d/
