intf_libs = # Interface dependencies.
impl_libs = # Implementation dependencies.
import impl_libs += libtommath%lib{tommath}

# Note that upstream keeps its sources and public headers in subdirectories
# of src/, which we symlink as a whole (except for math/; see README-DEV for
# details).
#
# Note also that some sources include lookup tables that are themselves C
# files and that we shouldn't compile on their own.
#
tabs = ciphers/aes/aes_tab         \
       ciphers/safer/safer_tab     \
       ciphers/twofish/twofish_tab \
       hashes/whirl/whirltab       \
       stream/sober128/sober128tab

lib{tomcrypt}: {h c}{** -{$tabs}} $impl_libs $intf_libs
lib{tomcrypt}: c{$tabs}: include = adhoc

# Build options.
#
src_pfx = [dir_path] $src_root/libtomcrypt/

c.poptions =+ "-I$src_pfx/headers"

# Define LTC_SOURCE to expose declarations internal to the library and
# LTM_DESC to enable the libtommath math descriptor (ltm_desc). Note that
# LTM_DESC also adds declarations to the public headers, so we export it as
# well (see below).
#
c.poptions += -DLTC_SOURCE -DLTM_DESC

# Suppress MSVC deprecation warnings for standard C functions, as upstream
# does (see makefile.msvc).
#
if ($c.target.system == 'win32-msvc')
  c.poptions += -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE

# Public headers re-declare the C library functions they use (malloc(),
# etc), which with MSVC conflicts with their declarations as imported from
# the C runtime DLL (C4273). Upstream suppresses these re-declarations with
# LTC_NO_PROTOTYPES in its Visual Studio project and we do the same, both
# for our build and for consumers (see below).
#
if ($c.target.system == 'win32-msvc')
  c.poptions += -DLTC_NO_PROTOTYPES

# Note that upstream compiles aes.c a second time with ENCRYPT_ONLY defined
# to produce the encrypt-only AES implementation (aes_enc_desc), which we
# replicate with a separate object file.
#
lib{tomcrypt}: obj{ciphers/aes/aes_enc}
obj{ciphers/aes/aes_enc}: c{ciphers/aes/aes}
{
  c.poptions += -DENCRYPT_ONLY
}

# Built-in self-tests of some algorithms pass strlen() of their test strings
# as unsigned long, which MSVC warns about for 64-bit targets (C4267). The
# strings are short literals, so we disable this warning for these sources
# only, keeping it for the rest of the library.
#
if ($c.class == 'msvc')
{
  obj{encauth/chachapoly/chacha20poly1305_test \
      hashes/chc/chc                            \
      hashes/rmd128                             \
      hashes/rmd160                             \
      hashes/rmd256                             \
      hashes/rmd320                             \
      mac/poly1305/poly1305_test                \
      misc/adler32                              \
      misc/crc32                                \
      stream/chacha/chacha_test}: c.coptions += /wd4267
}

# GCC can't prove that CCM fills exactly the 16 bytes of its first block and
# warns about writing past it with optimization (-Wstringop-overflow). Upstream
# silences this warning with checks that never fail (see upstream commits
# 97edea36 and 349d56e3) and we disable it for these sources only.
#
# GCC also warns that the PKCS #5 self-test counts the vectors it tests
# without using the count (-Wunused-but-set-variable).
#
if ($c.id == 'gcc')
{
  obj{encauth/ccm/ccm_add_nonce \
      encauth/ccm/ccm_memory}: c.coptions += -Wno-stringop-overflow

  obj{misc/pkcs5/pkcs_5_test}: c.coptions += -Wno-unused-but-set-variable
}

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

# Windows random number source (CryptGenRandom()) is provided by
# advapi32.lib, which upstream links explicitly (see makefile.msvc). MinGW
# links it by default.
#
if ($c.target.system == 'win32-msvc')
  c.libs += advapi32.lib

# Export options.
#
lib{tomcrypt}:
{
  c.export.poptions = "-I$src_pfx/headers" -DLTM_DESC
  c.export.libs = $intf_libs
}

if ($c.target.system == 'win32-msvc')
  lib{tomcrypt}: c.export.poptions += -DLTC_NO_PROTOTYPES

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

# Install public headers directly into, say, /usr/include/, as upstream
# does.
#
h{*}: install = include/
