# file      : tools/buildfile
# license   : Unicode License; see accompanying LICENSE file

# The bootstrap library is used to break the circular dependency chain icuuc
# -> icudata -> tools -> icuuc. See its buildfile for more details.
#
include ../icu-bootstrap/

tools = escapesrc       \
        genbrk          \
        genccode        \
        gencfu          \
        gencmn          \
        gencnval        \
        gendict         \
        gennorm2        \
        genrb           \
        gensprep        \
        icuexportdata   \
        icuinfo         \
        icupkg          \
        makeconv        \
        pkgdata

# Dependencies for the tools, all of which have the same form except for
# genrb/{genrb,derb} (see below) and genccode which has a generated source
# file.
#
for t: $regex.filter_out_match($tools, 'genrb|genccode')
{
  ./: $t/exe{$t}: $t/{hxx cxx c}{*} \
                  ../icu-bootstrap/libue{icu-bootstrap}

  # Dependencies on the installed- and development-specific bootstrap
  # libraries.
  #
  $t/exe{$t}: ../icu-bootstrap/libue{icu-bootstrap-install}: for_install = true
  $t/exe{$t}: ../icu-bootstrap/libue{icu-bootstrap-develop}: for_install = false

  # All these tools are installed but some of the tools are also used during
  # the build (see data/buildfile). While this doesn't necessarily work in
  # general (incompatible builds, missing rpath, etc), we know that these
  # tools function correctly for our purposes even when built for install. So
  # we force them to be always built for install during update-for-install.
  # See build2 issue #472 for background.
  #
  $t/exe{$t}: for_install = true
}

# Note: genccode.c is a generated source file.
#
./: genccode/exe{genccode}: genccode/c{genccode} \
                            ../icu-bootstrap/libue{icu-bootstrap}

# As above.
#
genccode/exe{genccode}: for_install = true

# Dependencies for the genrb and derb tools (which share the genrb/
# subdirectory).
#
# Note that derb is the only tool that requires libicuio (and derb is not used
# to produce the data library).
#
./: genrb/exe{genrb}: genrb/{hxx cxx c}{* -derb} \
                      ../icu-bootstrap/libue{icu-bootstrap}

./: genrb/exe{derb}: genrb/cxx{derb} \
                     ../icu-bootstrap/libue{icu-bootstrap}

# Dependencies on the installed- and development-specific bootstrap libraries.
#
genrb/exe{genrb derb} genccode/exe{genccode} : \
  ../icu-bootstrap/libue{icu-bootstrap-install}: for_install = true
genrb/exe{genrb derb} genccode/exe{genccode}: \
  ../icu-bootstrap/libue{icu-bootstrap-develop}: for_install = false

# As above.
#
genrb/exe{genrb derb}: for_install = true

# Export metadata from the tool executables.
#

# Environment variables likely to affect tool output.
#
#   ICU_DATA: ICU data directory (location of the default ICU data).
#
env = [strings] ICU_DATA

for t: $tools
{
  $t/exe{$t}:
  {
    export.metadata = 1 $t
    $(t).name = [string] $t
    $(t).version  = [string] $version
    $(t).checksum = [string] $version
    $(t).environment = $env
  }
}

genrb/exe{derb}:
{
  export.metadata = 1 derb
  derb.name = [string] derb
  derb.version  = [string] $version
  derb.checksum = [string] $version
  derb.environment = $env
}

# The libicuuc package uses the genccode tool to embed the data archive into
# the ICU data library. Unfortunately there would be little point in exporting
# a file{} target because there is no rule-specific search to find it in the
# installed case. Therefore the best we can do is to export the path as
# metadata from the genccode executable.
#
# Include data/ to make the data archive target available for consuming
# development builds to depend on. Note: causes an include cycle (see
# ../data/buildfile for details).
#
include [allow_cycle] ../data/

data_basename = $(../data/ data_basename)
data_name = $(../data/ data_name)

genccode/exe{genccode}:
{
  # Path to the ICU data directory.
  #
  genccode.data_dir = $data_dir_develop

  # Paths to the ICU data archives. Make them null in the develop case to
  # signal to consumers that they can import the target (i.e., as opposed to
  # creating their own local file{} target).
  #
  genccode.datfile_l_path = [path, null] # Little-endian.
  genccode.datfile_b_path = [path, null] # Big-endian.
}

# Export the same metadata as above from the main function.
#
[rule_hint=in] genccode/c{genccode}: genccode/in{genccode}
{
  data_dir = $data_dir_install

  datfile_l_path = [path] $data_dir_install/$(data_basename)l.dat
  datfile_b_path = [path] $data_dir_install/$(data_basename)b.dat

  # Escape backslashes on Windows (C string literals). Wrap the printed
  # Buildfile values in single quotes to obviate the need to escape yet
  # another time.
  #
  data_dir       = [dir_path] "'$regex.replace($data_dir, '\\', '\\\\')'"
  datfile_l_path = [path]     "'$regex.replace($datfile_l_path, '\\', '\\\\')'"
  datfile_b_path = [path]     "'$regex.replace($datfile_b_path, '\\', '\\\\')'"
}

# Arrange for the data archive to be updated after any tool executable is
# updated.
#
for n: $tools
  $n/exe{$n}: ../data/file{$(data_name).dat}: include = posthoc

# Build options that apply to all targets (i.e., the tools and the bootstrap
# library).
#
# Note that we build without plugins support (UCONFIG_ENABLE_PLUGINS macro is
# undefined) and thus we don't define the DEFAULT_ICU_PLUGINS macro either.
#
c.coptions += $common_cc_coptions
cxx.coptions += $common_cc_coptions $common_cxx_coptions

# Build options and libraries for the tool executables.
#
for t: $tools
{
  $t/
  {
    cc.poptions =+ "-I$src_root/icu-bootstrap/uc"       \
                   "-I$src_root/icu-bootstrap/i18n"     \
                   "-I$src_root/icu-bootstrap/toolutil"

    cc.poptions += -DU_STATIC_IMPLEMENTATION            \
                   -DUNISTR_FROM_CHAR_EXPLICIT=explicit \
                   -DUNISTR_FROM_STRING_EXPLICIT=explicit

    if ($cxx.class == 'msvc')
    {
      # Disable warnings that pop up with /W3.
      #
      # 4267: conversion from 'size_t' to 'int32_t', possible loss of data
      # 4805: '==': unsafe mix of type 'UBool' and type 'bool' in operation
      #
      cc.coptions += /wd4244 /wd4267 /wd4805
    }

    cxx.libs += $common_cxx_libs
  }
}

genrb/obj{derb}: cc.poptions =+ "-I$src_root/icu-bootstrap/io"

# Note that for some unknown reason both Debian and Fedora install these into
# sbin/.
#
exe{escapesrc/escapesrc \
    genccode/genccode   \
    gencmn/gencmn       \
    gennorm2/gennorm2   \
    gensprep/gensprep   \
    icupkg/icupkg}:     \
  install = bin/
