# file      : libpkg-config/buildfile
# license   : ISC; see accompanying COPYING file

lib{pkg-config}: {h c}{* -config -version} {h}{config version}

tclass = $c.target.class

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

msvc_runtime = ($c.target.system == 'win32-msvc')

msvc       = ($c.class == 'msvc')
clang_msvc = ($c.id == 'clang' && $msvc_runtime)

# Assign variables for the PKG_CONFIG_DEFAULT_PATH, SYSTEM_INCLUDEDIR, and
# SYSTEM_LIBDIR macro definitions. The whole idea feels utterly broken (hello
# cross-compilation) so we will just do bare minimum and wait and see.
#
# @@ We should probably allow to configure these macros via configuration
#    variables config.libpkg_config.pkg_config_default_path and alike.
#
if! $windows
{
  if ($install.root != [null])
  {
    inc_dirs = "$install.resolve($install.include)"
    lib_dirs = "$install.resolve($install.lib)"
    def_dirs = "$install.resolve($install.pkgconfig)"
  }
  else
  {
    inc_dirs = "/usr/include"
    lib_dirs = "/usr/lib"
    def_dirs = "/usr/lib/pkconfig"
  }
}
else
{
  inc_dirs = ""
  lib_dirs = ""

  # Note that on Windows PKG_CONFIG_DEFAULT_PATH macros is internally redefined
  # as "../lib/pkgconfig;../share/pkgconfig" and is used as a fallback. Normally
  # the default path is relative to the program's (that link the library)
  # directory and has the following format:
  #
  # <dir>/../lib/pkgconfig;<dir>/../share/pkgconfig
  #
  # So we keep the macro empty.
  #
  def_dirs = ""
}

# The config header is internal (is only included from C files), so we don't
# distribute nor install it (see below).
#
h{config}: in{config}
{
  PROJECT_URL = $project.url

  # Escape backslashes and quotes in the directory paths prior to representing
  # them as C string literals.
  #
  SYSTEM_INCLUDEDIR = $regex.replace($inc_dirs, '(\\|")', '\\\1')
  SYSTEM_LIBDIR     = $regex.replace($lib_dirs, '(\\|")', '\\\1')
  PKG_CONFIG_DEFAULT_PATH  = $regex.replace($def_dirs, '(\\|")', '\\\1')
}

# 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).
#
# NOTE: we may want to keep this going forward in case we want to add other
#       build systems in the future.
#
h{version}: in{version} $src_root/manifest
{
  dist  = true
  clean = ($src_root != $out_root)
}

h{export}@./: c.importable = false

# Build options.
#
# Note that we have to add "-I$src_root" for the headers auto-generating
# machinery to work properly.
#
c.poptions =+ "-I$out_root" "-I$src_root"

# Disable warnings.
#
if $msvc
  c.coptions += /wd4996 /wd4267
elif $clang_msvc
  c.coptions += -Wno-deprecated-declarations -Wno-unused-function

if $windows
  c.libs += ($msvc_runtime ? advapi32.lib : -ladvapi32)

{hbmia obja}{*}: c.poptions += -DLIBPKG_CONFIG_STATIC_BUILD
{hbmis objs}{*}: c.poptions += -DLIBPKG_CONFIG_SHARED_BUILD

# Export options.
#
lib{pkg-config}:  c.export.poptions = "-I$out_root" "-I$src_root"
liba{pkg-config}: c.export.poptions += -DLIBPKG_CONFIG_STATIC
libs{pkg-config}: c.export.poptions += -DLIBPKG_CONFIG_SHARED

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

# Install into the libpkg-config/ subdirectory of, say, /usr/include/.
#
h{*}: install = include/libpkg-config/

# Don't install private headers.
#
h{config} h{stdinc}@./: install = false
