# -*- 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 9.6.1 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  538597d17859e6cce50e53793466df090ce7fdac \
                    sha256  98cf497c1d6d0a927142d2002a0b6b4816a0998c74fda9ae7b1bdaf6b784e895 \
                    size    13589860
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
}