intf_libs = # Interface dependencies.
impl_libs = # Implementation dependencies.
#import xxxx_libs += libhello%lib{hello}

windows = ($c.target.class == 'windows')
apple   = ($c.target.class == 'macos' || $c.target.class == 'ios')
bsd     = ($c.target.class == 'bsd')

# Public header plus one platform TU (ELF, Mach-O, or PE). Keep all three in
# the graph and select with include=.
#
lib{plthook}: libul{plthook}: {h c}{** -plthook_elf -plthook_osx -plthook_win32} \
                              $impl_libs $intf_libs

libul{plthook}: c{plthook_elf}:   include = (!$windows && !$apple)
libul{plthook}: c{plthook_osx}:   include = $apple
libul{plthook}: c{plthook_win32}: include = $windows

libs{plthook}: def{plthook}: include = ($c.target.system == 'win32-msvc')
def{plthook}: libul{plthook}

# Build options.
#
out_pfx = [dir_path] $out_root/libplthook/
src_pfx = [dir_path] $src_root/libplthook/

c.poptions =+ "-I$out_pfx" "-I$src_pfx"

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

# Silence upstream warnings that are benign for this codebase: compiler
# false positives or deprecation noise from Windows/Mach-O binary parsing,
# not actionable bugs. Only one of plthook_elf/osx/win32 is ever compiled
# per platform (see include= above), so these are effectively scoped to
# that single translation unit.
#
if $apple
{
  # sym_name is always set (via BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
  # before any BIND_OPCODE_DO_BIND* opcode uses it; GCC's inliner loses
  # that invariant at -O3.
  #
  if ($c.id == 'gcc')
    c.coptions += -Wno-maybe-uninitialized
}
elif $windows
{
  if ($c.id == 'msvc')
    # C4267: size_t narrowing that never exceeds bounds in practice (import
    # counts, a 512-byte error buffer). C4996: stricmp/sprintf flagged as
    # unsafe/POSIX; usage is bounded and portability requires stricmp.
    #
    c.coptions += /wd4267 /wd4996
  elif ($c.id == 'clang')
    # stricmp is POSIX and deprecated in favor of _stricmp; kept for
    # portability with the non-Windows code paths.
    #
    c.coptions += -Wno-deprecated-declarations
}

# Export options.
#
lib{plthook}:
{
  c.export.poptions = "-I$out_pfx" "-I$src_pfx"
  c.export.libs = $intf_libs
}

# libdl on ELF, libutil on BSD (kinfo_getvmmap), DbgHelp on Windows. Apple
# libSystem already provides dl. Static consumers need these too.
#
if $windows
{
  if ($c.target.system == 'mingw32')
    sys_libs = -ldbghelp
  else
    sys_libs = dbghelp.lib

  lib{plthook}:
  {
    c.libs += $sys_libs
    c.export.libs += $sys_libs
  }
}
elif $bsd
{
  lib{plthook}:
  {
    c.libs += -lutil
    c.export.libs += -lutil
  }
}
elif (!$apple)
{
  lib{plthook}:
  {
    c.libs += -ldl
    c.export.libs += -ldl
  }
}

# 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{plthook}: bin.lib.version = "-$version.project_id"
else
  lib{plthook}: bin.lib.version = "-$version.major.$version.minor"

# Install recreating subdirectories.
#
h{*}:
{
  install         = include/
  install.subdirs = true
}
