# file      : bdep/buildfile
# license   : MIT; see accompanying LICENSE file

# @@ ODB: these are ODB changelogs that are both generated and stored in the
#         repository (what if src != out?). Will need to think how to handle
#         them properly (always generate in src_base?).
#
define xml: file
xml{*}: extension = xml

import libs  = libbpkg%lib{bpkg}
import libs += libbutl%lib{butl}
import libs += libbutl%lib{butl-odb}

# @@ Why don't we generate these with wildcard patterns (and rules below with
# a for-loop)?
#
options_topics =  \
bdep-options      \
common-options    \
project-options   \
help-options      \
new-options       \
init-options      \
sync-options      \
fetch-options     \
status-options    \
ci-options        \
release-options   \
publish-options   \
deinit-options    \
config-options    \
test-options      \
update-options    \
clean-options

help_topics = projects-configs argument-grouping default-options-files

./: exe{bdep}: {hxx ixx txx cxx}{+bdep} libue{bdep}

libue{bdep}: {hxx ixx txx cxx}{** -bdep                    \
                                  -{$options_topics}       \
                                  -{$help_topics}          \
                                  -*-odb                   \
                                  -version                 \
                                  -**.test...}             \
             {hxx ixx cxx}{$options_topics}                \
             {hxx cxx}{$help_topics}                       \
             {hxx ixx cxx}{project-odb database-views-odb} \
             {hxx}{version}                                \
             $libs                                         \
             xml{*}

hxx{version}: in{version} $src_root/manifest

# Unit tests.
#
exe{*.test}:
{
  test = true
  install = false
}

for t: cxx{**.test...}
{
  d = $directory($t)
  n = $name($t)...

  ./: $d/exe{$n}: $t $d/{hxx ixx txx}{+$n} $d/testscript{+$n}
  $d/exe{$n}: libue{bdep}: bin.whole = false
}

# Build options.
#
obj{utility}: cxx.poptions += -DBDEP_EXE_PREFIX='"'$bin.exe.prefix'"' \
-DBDEP_EXE_SUFFIX='"'$bin.exe.suffix'"'

# Pass the copyright notice extracted from the LICENSE file.
#
copyright = $process.run_regex(                                 \
  cat $src_root/LICENSE,                                        \
  'Copyright \(c\) (.+) \(see the AUTHORS and LEGAL files\)\.', \
  '\1')

obj{bdep}: cxx.poptions += -DBDEP_COPYRIGHT=\"$copyright\"

# For now assume this is a staged toolchain if we are building with the staged
# build system (see build.version.stage for details on the final plan).
#
if $build.version.stage
  cxx.poptions += -DBDEP_STAGE

# Disable "unknown pragma" warnings.
#
switch $cxx.class
{
  case 'msvc'
    cxx.coptions += /wd4068

  case 'gcc'
    cxx.coptions += -Wno-unknown-pragmas
}

# Make sure backtrace includes function names.
#
if ($cxx.target.class == 'linux' && $cc.stdlib == 'glibc')
  cxx.loptions += -rdynamic

# @@ TMP work around what looks like Clang bug #45021 (first triggered with
#    Clang 13.0.0).
#
if ($cxx.id == 'clang' && $cxx.target.system == 'win32-msvc')
{
  if ($regex.find_match($cc.coptions $cxx.coptions, '-O[23]'))
    obj{new}: cxx.coptions += -O1
}

# Generated options parser.
#
if $cli.configured
{
  # General topics and common options.
  #
  cli.cxx{common-options}:         cli{common}
  cli.cxx{project-options}:        cli{project}
  cli.cxx{bdep-options}:           cli{bdep}

  # Command.
  #
  cli.cxx{help-options}: cli{help}

  cli.cxx{new-options}:     cli{new}
  cli.cxx{init-options}:    cli{init}
  cli.cxx{sync-options}:    cli{sync}
  cli.cxx{fetch-options}:   cli{fetch}
  cli.cxx{status-options}:  cli{status}
  cli.cxx{ci-options}:      cli{ci}
  cli.cxx{release-options}: cli{release}
  cli.cxx{publish-options}: cli{publish}
  cli.cxx{deinit-options}:  cli{deinit}
  cli.cxx{config-options}:  cli{config}
  cli.cxx{test-options}:    cli{test}
  cli.cxx{update-options}:  cli{update}
  cli.cxx{clean-options}:   cli{clean}

  # Help topics.
  #
  cli.cxx{projects-configs}:      cli{projects-configs}
  cli.cxx{argument-grouping}:     cli{argument-grouping}
  cli.cxx{default-options-files}: cli{default-options-files}

  # Option length must be the same to get commands/topics/options aligned.
  #
  cli.options += --std c++11 -I $src_root --include-with-brackets \
--include-prefix bdep --guard-prefix BDEP \
--cxx-prologue "#include <bdep/types-parsers.hxx>" --cli-namespace bdep::cli \
--generate-vector-scanner --generate-file-scanner --generate-group-scanner \
--keep-separator --generate-specifier --generate-modifier \
--generate-description --generate-parse --generate-merge \
--page-usage 'bdep::print_$name$_' --ansi-color --ascii-tree \
--include-base-last --suppress-undocumented --option-length 25

  cli.cxx{common-options}: cli.options += --short-usage --long-usage # Both.
  cli.cxx{bdep-options}: cli.options += --short-usage
  cli.options += --long-usage # All other pages -- long usage.

  cli.cxx{new-options}: cli.options += \
--cxx-prologue "#include <bdep/new-parsers.hxx>"

  cli.cxx{ci-options}: cli.options += \
--cxx-prologue "#include <bdep/ci-parsers.hxx>"

  cli.cxx{release-options}: cli.options += \
--cxx-prologue "#include <bdep/release-parsers.hxx>"

  # Avoid generating CLI runtime and empty inline file for help topics.
  #
  cli.cxx{projects-configs}:      cli.options += --suppress-cli --suppress-inline
  cli.cxx{argument-grouping}:     cli.options += --suppress-cli --suppress-inline
  cli.cxx{default-options-files}: cli.options += --suppress-cli --suppress-inline

  # Include the generated cli files into the distribution and don't remove
  # them when cleaning in src (so that clean results in a state identical to
  # distributed).
  #
  cli.cxx{*}:
  {
    dist  = true
    clean = ($src_root != $out_root)
  }
}
