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

lib{rpmalloc}: libul{rpmalloc}: {h c}{** -malloc} $impl_libs $intf_libs

# Included by rpmalloc.c.
#
libul{rpmalloc}: c{malloc}: include = adhoc

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

# Build options.
#
c.poptions =+ "-I$out_root" "-I$src_root"

# Initialize and finalize the allocator from DllMain() on Windows (see
# malloc.c).
#
objs{*}: c.poptions += -DENABLE_DYNAMIC_LINK=1

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

# rpmalloc.c uses <stdatomic.h>, which requires C11, but build2 does not
# translate c.std into an option for MSVC-class compilers. Older cl.exe
# versions also gate C11 atomics behind /experimental:c11atomics (which
# upstream's build/msvs/rpmalloc.vcxproj passes as well) while clang-cl
# does not recognize it.
#
if ($c.class == 'msvc')
{
  c.coptions += /std:c11

  if ($c.id == 'msvc')
    c.coptions += /experimental:c11atomics
}

# As upstream, expose the madvise() flags and other extensions used by
# rpmalloc.c regardless of the C dialect.
#
if ($c.target.class == 'linux' || $c.target.class == 'bsd')
  c.poptions += -D_GNU_SOURCE=1

if ($c.target.class != 'windows')
  c.libs += -pthread
else
{
  # For the large page privilege functions.
  #
  if ($c.target.system == 'mingw32')
    c.libs += -ladvapi32
  else
    c.libs += advapi32.lib
}

# Feature switches (see build/root.build).
#
c.poptions += -DENABLE_OVERRIDE=($config.librpmalloc.override ? 1 : 0)                 \
              -DENABLE_STATISTICS=($config.librpmalloc.statistics ? 1 : 0)             \
              -DENABLE_LEAK_DETECTION=($config.librpmalloc.leak_detection ? 1 : 0)     \
              -DENABLE_VALIDATE_ARGS=($config.librpmalloc.validate_args ? 1 : 0)       \
              -DENABLE_ASSERTS=($config.librpmalloc.asserts ? 1 : 0)                   \
              -DRPMALLOC_HEAP_STATISTICS=($config.librpmalloc.heap_statistics ? 1 : 0)

# Also gates the rpmalloc_heap_*() declarations in rpmalloc.h and so must be
# exported (see below).
#
first_class_heaps = ($config.librpmalloc.first_class_heaps ? 1 : 0)

c.poptions += "-DRPMALLOC_FIRST_CLASS_HEAPS=$first_class_heaps"

# Export options.
#
# Besides the qualified <rpmalloc/rpmalloc.h>, also support the unqualified
# <rpmalloc.h> and <rpnew.h> inclusion that upstream documents.
#
# Also export the config.librpmalloc.override value as metadata (used by the
# tests).
#
lib{rpmalloc}:
{
  export.metadata = 1 librpmalloc

  c.export.poptions = "-I$out_root" "-I$src_root" "-I$out_base" "-I$src_base"
  c.export.poptions += "-DRPMALLOC_FIRST_CLASS_HEAPS=$first_class_heaps"
  c.export.libs = $intf_libs

  librpmalloc.override = [bool] $config.librpmalloc.override
}

lib{rpmalloc}: c.pkgconfig.include = include/ include/rpmalloc/

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

# Install into the rpmalloc/ subdirectory of, say, /usr/include/
# recreating subdirectories.
#
h{*}:
{
  install         = include/rpmalloc/
  install.subdirs = true
}
