# -*- 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           cmake 1.1
PortGroup           compiler_blacklist_versions 1.0
PortGroup           github 1.0
PortGroup           legacysupport 1.1
PortGroup           openssl 1.0

# _getline
legacysupport.newest_darwin_requires_legacy 19

# See discussion in: https://github.com/macports/macports-ports/pull/19686
legacysupport.use_mp_libcxx yes

github.setup        TileDB-Inc TileDB 2.18.2
name                tiledb
revision            0
categories          databases
license             MIT
maintainers         {@barracuda156 gmail.com:vital.had} openmaintainer
description         The universal storage engine
long_description    {*}${description}
homepage            https://tiledb.com
checksums           rmd160  be4ee1364763a61a56f83a54eda31faa63fb9f88 \
                    sha256  9514856314b859d3d10fcdcf9e4999aead5c09dc5f37d10db68df73a5982bd2f \
                    size    4393968
github.tarball_from archive

# Notice that 32-bit platforms are not supported by upstream, and there are reports elsewhere
# that some targets are broken or fail tests. It may or may not work at any given moment,
# but seems to require substantial effort to have it properly fixed.
# ppc64 works much better than ppc32, though some tests fail.
# I do not disable 32-bit archs in order to facilitate testing and possible fix-ups.
# Do not expect it to work as-is. See also: https://github.com/TileDB-Inc/TileDB/issues/4096

set libfmt_ver      10

cmake.module_path-append \
                    ${prefix}/lib/libfmt${libfmt_ver}/cmake

depends_lib-append  port:blosc \
                    port:bzip2 \
                    port:curl \
                    port:libfmt${libfmt_ver} \
                    port:libpng \
                    port:lz4 \
                    port:spdlog \
                    port:zlib \
                    port:zstd

depends_build-append \
                    port:gmake

if {${os.platform} eq "darwin" && ${os.major} < 13} {
    # Lion+ (with Xcode 4.1+) have git; earlier need to bring their own.
    # On 10.8.5 git fetch fails with ssl error.
    depends_build-append    port:git
    git.cmd                 ${prefix}/bin/git
}

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # This trickery is intended. Normal passing of flags to CMake fails to have a desired effect.
    # Non-Apple OS on ppc will need libatomic linking too, but leave it until tested.
    patchfiles-append \
                    patch-linking.diff \
                    0001-Add-a-fix-for-32-bit.patch

    post-patch {
        reinplace "s,@PREFIX@,${prefix},g" ${worksrcpath}/cmake/Modules/FindMagic_EP.cmake
    }
}

compiler.cxx_standard       2020
# https://github.com/TileDB-Inc/TileDB/issues/4566
compiler.blacklist-append   {clang < 1400}

# config modules are defunct.
# https://github.com/TileDB-Inc/TileDB/issues/4070
configure.args-append \
                    -DCATCH_INCLUDE_DIR:STRING=${prefix}/include \
                    -DTILEDB_ABSEIL=OFF \
                    -DTILEDB_ASSERIONS=OFF \
                    -DTILEDB_AZURE=OFF \
                    -DTILEDB_CPP_API=ON \
                    -DTILEDB_EXPERIMENTAL_FEATURES=OFF \
                    -DTILEDB_SERIALIZATION=OFF \
                    -DTILEDB_TESTS=OFF \
                    -DTILEDB_TOOLS=OFF \
                    -DTILEDB_VERBOSE=ON \
                    -DTILEDB_WEBP=OFF \
                    -DTILEDB_WERROR=OFF

configure.cflags-append \
                    -DSPDLOG_FMT_EXTERNAL=1

configure.ldflags-append \
                    -L${prefix}/lib/libfmt${libfmt_ver} -lfmt

platform darwin {
    if {${os.major} < 11 && [string match *gcc* ${configure.compiler}]} {
        configure.args-append \
                    -DCOMPILER_SUPPORTS_AVX2=OFF
    }
}

if {${build_arch} eq "i386" && [string match *clang* ${configure.compiler}]} {
    configure.cxxflags-append \
                    -Wno-c++11-narrowing
}

destroot.target     install-tiledb

variant tests description "Enable testing" {
    if {${configure.build_arch} in [list i386 ppc]} {
        cmake.build_type    Debug
    }

    depends_build-append \
                    port:catch2

    configure.pre_args-replace \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF

    configure.args-replace \
                    -DTILEDB_TESTS=OFF -DTILEDB_TESTS=ON

    test.run        yes
    test.target     check
}
