# file      : libbutl-odb/buildfile
# license   : MIT; see accompanying LICENSE file

sys_sqlite = $config.libbutl.system_libsqlite3

intf_libs =

if $sys_sqlite
  import intf_libs += libsqlite3%lib{sqlite3}

lib{butl-odb}: odb/{h hxx ixx txx cxx}{**}
lib{butl-odb}: sqlite/{h c}{*}: include = (!$sys_sqlite)
lib{butl-odb}: $intf_libs

# Build options.
#
cxx.poptions =+ "-I$src_base"

if! $sys_sqlite
  cxx.poptions =+ "-I($src_base/sqlite)"

# For now we only need single-threaded ODB and SQLite. Note that if we need to
# change that, then we will most likely need to deal with MinGW stdthread mess
# (see libbutl/buildfile for details). Also see SQLITE_THREADSAFE=0 below.
#
cxx.poptions += -DODB_THREADS_NONE

obja{*}: cxx.poptions += -DLIBODB_STATIC_BUILD -DLIBODB_SQLITE_STATIC_BUILD
objs{*}: cxx.poptions += -DLIBODB_SHARED_BUILD -DLIBODB_SQLITE_SHARED_BUILD

if! $sys_sqlite
{
  # Note: copy of libsqlite3/buildfile except for SQLITE_THREADSAFE=0,
  # -DSQLITE_OMIT_SHARED_CACHE, and removed ENABLE_UNLOCK_NOTIFY=1.
  #
  # We also add -Wno-unused-variable and -Wno-unused-but-set-variable.
  #
  tclass = $c.target.class

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

  gcc        = ($c.class == 'gcc')
  clang      = ($c.id.type == 'clang')
  clang_msvc = ($clang && $c.target.system == 'win32-msvc')

  sqlite/
  {
    # @@ TMP: add -DSQLITE_DQS=0, -DSQLITE_OMIT_DEPRECATED=1
    # @@ TODO: maybe use -DSQLITE_OMIT_AUTOINIT?

    c.poptions += \
      -DSQLITE_ALLOW_ROWID_IN_VIEW=1 \
      -DSQLITE_ENABLE_COLUMN_METADATA=1 \
      -DSQLITE_ENABLE_DBPAGE_VTAB=1 \
      -DSQLITE_ENABLE_DBSTAT_VTAB=1 \
      -DSQLITE_ENABLE_EXPLAIN_COMMENTS=1 \
      -DSQLITE_ENABLE_FTS3=1 \
      -DSQLITE_ENABLE_FTS3_PARENTHESIS=1 \
      -DSQLITE_ENABLE_FTS3_TOKENIZER=1 \
      -DSQLITE_ENABLE_FTS4=1 \
      -DSQLITE_ENABLE_FTS5=1 \
      -DSQLITE_ENABLE_GEOPOLY=1 \
      -DSQLITE_ENABLE_JSON1=1 \
      -DSQLITE_ENABLE_LOAD_EXTENSION=1 \
      -DSQLITE_ENABLE_MATH_FUNCTIONS=1 \
      -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \
      -DSQLITE_ENABLE_RTREE=1 \
      -DSQLITE_ENABLE_SESSION=1 \
      -DSQLITE_ENABLE_STMTVTAB=1 \
      -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1 \
      -DSQLITE_LIKE_DOESNT_MATCH_BLOBS=1 \
      -DSQLITE_MAX_SCHEMA_RETRY=25 \
      -DSQLITE_MAX_VARIABLE_NUMBER=250000 \
      -DSQLITE_OMIT_LOOKASIDE=1 \
      -DSQLITE_SECURE_DELETE=1 \
      -DSQLITE_SOUNDEX=1 \
      -DSQLITE_THREADSAFE=0 \
      -DSQLITE_USE_URI=1 \
      -DSQLITE_OMIT_SHARED_CACHE=1

    c.poptions += -DSQLITE_MAX_ATTACHED=125

    if! $windows
      c.poptions += -DHAVE_USLEEP=1

    if $windows
      objs{*}: c.poptions += '-DSQLITE_API=__declspec(dllexport)'

    if $gcc
      c.coptions += -fno-strict-aliasing

    if $clang_msvc
      c.coptions += -Wno-deprecated-declarations

    if ($clang && $c.version.major >= 15)
      c.coptions += -Wno-unused-but-set-variable

    if $gcc
      c.coptions += -Wno-unused-variable -Wno-extra -Wno-error

    if ($c.id == 'gcc' && $c.version.major >= 14)
      c.coptions += -Wno-stringop-overread
  }

  if! $windows
  {
    cxx.libs += -lm

    if! $bsd
      cxx.libs += -ldl
  }
}

# Export options.
#
lib{butl-odb}:
{
  cxx.export.poptions = "-I$src_base" -DODB_THREADS_NONE
  cxx.export.libs = $intf_libs
}

liba{butl-odb}: cxx.export.poptions += -DLIBODB_STATIC -DLIBODB_SQLITE_STATIC
libs{butl-odb}: cxx.export.poptions += -DLIBODB_SHARED -DLIBODB_SQLITE_SHARED

if! $sys_sqlite
{
  lib{butl-odb}: cc.export.poptions = "-I($src_base/sqlite)"

  if $windows
    libs{butl-odb}: cc.export.poptions += '-DSQLITE_API=__declspec(dllimport)'
}

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

# Install into the libbutl/odb/ subdirectory of, say, /usr/include/. For odb/,
# recreate subdirectories (so we end up with libbutl/odb/odb/...>). For
# sqlite/, install directly into libbutl/odb/. Arrange via pkg-config to be
# able to include ODB headers as <odb/...> and SQLite as just <sqlite3.h>.
#
# The reason we add an extra odb/ subdirectory instead of installing directly
# into libbutl/ is because adding -I for libbutl/ will make all the libbutl
# headers includable without a prefix and thus prone to clashes.
#
odb/{h hxx ixx txx}{*}:
{
  install         = include/libbutl/odb/odb/
  install.subdirs = true
}

sqlite/h{*}: install = include/libbutl/odb/

lib{butl-odb}: cxx.pkgconfig.include = include/libbutl/odb/
