# file      : xercesc/buildfile
# license   : Apache License 2.0; see accompanying LICENSE file

network = $config.libxerces_c.network
transcoder_icu = $config.libxerces_c.transcoder_icu

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

# Note that the installed util/NetAccessors/Curl/CurlURLInputStream.hpp
# includes the libcurl headers.
#
if $network
  import intf_libs += libcurl%lib{curl}

if $transcoder_icu
{
  import impl_libs += libicuuc%lib{icuuc}
  import impl_libs += libicui18n%lib{icui18n}
}

lib{xerces-c}: {h          }{config}                    \
               {hxx txx cxx}{*/** -util/FileManagers/** \
                                  -util/NetAccessors/** \
                                  -util/Transcoders/**  \
                                  -util/XercesVersion}  \
               {hxx        }{      util/XercesVersion}

tclass = $cxx.target.class
tsys   = $cxx.target.system

macos   = ($tclass == 'macos')
windows = ($tclass == 'windows')

lib{xerces-c}:    {h     c}{stricmp strnicmp}                        \
util/FileManagers/{hxx cxx}{PosixFileMgr    }: include = (!$windows)

lib{xerces-c}: util/FileManagers/{hxx cxx}{WindowsFileMgr}: include = $windows

lib{xerces-c}: util/NetAccessors/Curl/{hxx cxx}{*}: include = $network

lib{xerces-c}: util/Transcoders/ICU/{hxx cxx}{*}: include = $transcoder_icu

lib{xerces-c}: util/Transcoders/MacOSUnicodeConverter/{hxx cxx}{*}: \
include = (!$transcoder_icu && $macos)

lib{xerces-c}: util/Transcoders/Win32/{hxx cxx}{*}: \
include = (!$transcoder_icu && $windows)

lib{xerces-c}: $intf_libs $impl_libs

# Include the generated version header into the distribution (so that we
# don't pick up an installed one) and don't remove it when cleaning in src (so
# that clean results in a state identical to distributed).
#
util/hxx{XercesVersion}: util/in{XercesVersion} $src_root/manifest
{
  dist  = true
  clean = ($src_root != $out_root)

  in.symbol = '@'

  XERCES_VERSION_MAJOR    = "$version.major"
  XERCES_VERSION_MINOR    = "$version.minor"
  XERCES_VERSION_REVISION = "$version.patch"

  XERCES_GRAMMAR_SERIALIZATION_LEVEL = "$grammar_serialization_level"
}

# Build options.
#
if $network
  cc.poptions += -DXERCES_USE_NETACCESSOR_CURL=1

if $transcoder_icu
  cc.poptions += -DXERCES_USE_TRANSCODER_ICU=1
elif $macos
  cc.poptions += -DXERCES_USE_TRANSCODER_MACOSUNICODECONVERTER=1
elif $windows
  cc.poptions += -DXERCES_USE_TRANSCODER_WINDOWS=1

cc.poptions += -DXERCES_BUILDING_LIBRARY -DHAVE_CONFIG_H -D_THREAD_SAFE

if $windows
  cc.poptions += -DWIN32 -D_WINDOWS -D_MBCS

# Note that we need to add "-I$src_root" for the headers auto-generating
# machinery to work properly.
#
cc.poptions =+ "-I$out_root" "-I$src_root" "-I$src_base"

obja{*}: cxx.poptions += -DLIBXERCES_C_STATIC_BUILD
objs{*}: cxx.poptions += -DLIBXERCES_C_SHARED_BUILD

switch $c.class
{
  case 'gcc'
  {
    # Disable warnings that pop up with -Wall -Wextra. Upstream doesn't seem
    # to care about these and it is not easy to disable specific warnings in a
    # way that works across compilers/version (some -Wno-* options are only
    # recognized in newer versions). There are still some warnings left that
    # appear for certain platforms/compilers. We pass them through but disable
    # treating them as errors.
    #
    cc.coptions += -Wno-all -Wno-extra -Wno-error

    # Disable the Clang targeting MSVC warnings.
    #
    if ($c.id == 'clang' && $tsys == 'win32-msvc')
      cc.coptions += -Wno-deprecated-declarations
  }
  case 'msvc'
  {
    # Disable warnings that pop up with /W3.
    #
    cc.coptions += /wd4244 /wd4267 /wd4996 /wd4305
  }
}

switch $tclass, $tsys
{
  case 'windows', 'mingw32'
  {
    # Make sure all symbols are resolvable.
    #
    cxx.loptions += -Wl,--no-undefined

    if $network
      cxx.libs += -lws2_32
  }
  case 'windows'
  {
    # Suppress the 'object file does not define any public symbols' warning.
    #
    cxx.aoptions += /IGNORE:4221

    if! $transcoder_icu
      cxx.libs += advapi32.lib

    if $network
      cxx.libs += ws2_32.lib
  }
  case 'linux'
  {
    # Make sure all symbols are resolvable.
    #
    cxx.loptions += -Wl,--no-undefined

    cxx.libs += -lm -pthread
  }
  case 'macos'
  {
    if! $transcoder_icu
      cxx.libs += -framework CoreServices

    cxx.libs += -pthread
  }
  default
  {
    cxx.libs += -pthread
  }
}

# Export options.
#
lib{xerces-c}:
{
  cxx.export.poptions = "-I$out_root" "-I$src_root"
  cxx.export.libs     = $intf_libs
}

liba{xerces-c}: cxx.export.poptions += -DLIBXERCES_C_STATIC
libs{xerces-c}: cxx.export.poptions += -DLIBXERCES_C_SHARED

# See bootstrap.build for details.
#
if $version.pre_release
  lib{xerces-c}: bin.lib.version = @"-$version.project_id"
else
  lib{xerces-c}: bin.lib.version = @"-$abi_version"

# Don't install the implementation details C headers (config.h and the
# compatibility function declarations).
#
h{*}: install = false

# Install the C++ headers into the xercesc/ subdirectory of, say, /usr/include/
# recreating subdirectories.
#
{hxx txx}{*}:
{
  install         = include/xercesc/
  install.subdirs = true
}
