# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0
PortGroup           cmake 1.1
PortGroup           legacysupport 1.1

# strnlen
legacysupport.newest_darwin_requires_legacy 10

github.setup        facebook rocksdb 10.1.3 v
revision            0

categories          databases devel
license             BSD

maintainers         {stromnov @stromnov} openmaintainer

description         Embedded key-value store for fast storage
long_description    {*}${description}

homepage            https://rocksdb.org

checksums           rmd160  288efe2fccc68412b02d96000dce677ff9b31ed1 \
                    sha256  df44cbca43d2002726ebbdd5caeae1701dcdf0500d4c2065d6fca261b4706a37 \
                    size    13766405
github.tarball_from archive

depends_lib-append  port:gflags \
                    port:snappy \
                    port:zlib \
                    port:bzip2 \
                    port:lz4 \
                    port:zstd

patch.pre_args-replace  -p0 -p1
patchfiles          0001-rocksdb-fixes-for-macOS-PPC.patch \
                    0002-MAP_ANON.patch

compiler.cxx_standard 2017
compiler.thread_local_storage yes

# DCMAKE_INCLUDE_DIRECTORIES_BEFORE=ON fixes the build while an older version is installed, see #69079
configure.args-append \
                    -DWITH_JEMALLOC=OFF \
                    -DWITH_LIBURING=OFF \
                    -DWITH_SNAPPY=ON \
                    -DWITH_LZ4=ON \
                    -DWITH_ZLIB=ON \
                    -DWITH_ZSTD=ON \
                    -DUSE_FOLLY=OFF \
                    -DROCKSDB_BUILD_SHARED=ON \
                    -DWITH_BZ2=ON \
                    -DWITH_MD_LIBRARY=ON \
                    -DWITH_TBB=OFF \
                    -DCMAKE_INCLUDE_DIRECTORIES_BEFORE=ON

if {[string match *gcc* ${configure.compiler}]} {
    # version_set.cc: error: 'rocksdb::{anonymous}::ManifestPicker::ManifestPicker' defined but not used
    # port_posix.cc: error: 'm' may be used uninitialized
    # db_bench_tool.cc: error: narrowing conversion of 'thread->rocksdb::ThreadState::rand.rocksdb::Random64::Next()' from 'uint64_t' to 'size_t'
    configure.cxxflags-append \
                    -Wno-unused-function \
                    -Wno-maybe-uninitialized \
                    -Wno-narrowing
}

# Build auto-detects ccache if it is installed and attempts to write
# to CCACHE_DIR, which is not allowed if configure.ccache=off.
# Set CCACHE_DIR to the build directory instead.
if {![option configure.ccache]} {
    configure.env-append   CCACHE_DIR=${workpath}/.ccache
    build.env-append       CCACHE_DIR=${workpath}/.ccache
    destroot.env-append    CCACHE_DIR=${workpath}/.ccache
}

variant jemalloc description "Use jemalloc" {
    depends_lib-append \
                    path:lib/pkgconfig/jemalloc.pc:jemalloc
    configure.args-replace \
                    -DWITH_JEMALLOC=OFF -DWITH_JEMALLOC=ON
}

variant tbb description "Use TBB" {
    depends_lib-append \
                    port:onetbb
    configure.args-replace \
                    -DWITH_TBB=OFF -DWITH_TBB=ON
    configure.args-append \
                    -DTBB_ROOT_DIR=${prefix}/libexec/onetbb
}
