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

github.setup        cwida duckdb 1.5.6 v
github.tarball_from archive
revision            1

# Keep in sync with GIT_TAG in .github/config/extensions/ducklake.cmake
set ducklake_commit ac7595b0a1305bea3d4cfaca763b0ce964c763a2

homepage            https://www.duckdb.org

description         \
    DuckDB is an embeddable SQL OLAP Database Management System

long_description    \
    DuckDB is an embedded database designed to execute analytical SQL queries \
    fast while embedded in another process. It is designed to be easy to \
    install and easy to use. DuckDB has no external dependencies. DuckDB has \
    bindings for C/C++, Python and R. DuckDB is developed by the Database \
    Architectures group of the CWI. The DuckLake lakehouse format extension \
    is built in.

categories          databases
license             MIT
maintainers         {gmail.com:herby.gillot @herbygillot} \
                    openmaintainer

master_sites-append https://github.com/duckdb/ducklake/archive/${ducklake_commit}${extract.suffix}?dummy=:ducklake
distfiles-append    ducklake-${ducklake_commit}${extract.suffix}:ducklake

checksums           ${distname}${extract.suffix} \
                    rmd160  c993ebc1d1c149e26707abf913b059f7cd6b2f4a \
                    sha256  1fadcbe9e69e1470f9093b6bcde08daf477d729c449e59a807f45c346622099b \
                    size    100988480 \
                    ducklake-${ducklake_commit}${extract.suffix} \
                    rmd160  248e05715848807faf3b156285d096c0597ca816 \
                    sha256  04829416605ccee7464069b74941bd97188e509d132d45e0f4dbfecf2a1a2e13 \
                    size    704575

patchfiles-append   no-ccache.patch \
                    no-clangd-cache.patch \
                    macports-smoke-test.patch

set python_version  314
set python_branch   \
    [string index ${python_version} 0].[string range ${python_version} 1 end]

configure.args-append \
                    -DBUILD_EXTENSIONS='autocomplete\;icu\;parquet\;json\;ducklake' \
                    -DENABLE_EXTENSION_AUTOLOADING=1 \
                    -DENABLE_EXTENSION_AUTOINSTALL=1 \
                    -DOVERRIDE_GIT_DESCRIBE=v${version} \
                    -DCMAKE_CXX_STANDARD=14 \
                    -DPython3_EXECUTABLE=${prefix}/bin/python${python_branch}

# C++14 for std::hash on enum class keys; older libc++ lacks it in C++11 mode
compiler.cxx_standard 2014

configure.env-append \
                    DUCKDB_DUCKLAKE_DIRECTORY=${workpath}/ducklake-${ducklake_commit}

post-patch {
    set ducklake_cmake ${worksrcpath}/.github/config/extensions/ducklake.cmake
    set fd [open ${ducklake_cmake} r]
    set pinned [regexp -inline {GIT_TAG\s+([0-9a-f]+)} [read ${fd}]]
    close ${fd}
    if {[lindex ${pinned} 1] ne ${ducklake_commit}} {
        return -code error "ducklake_commit ${ducklake_commit} does not match the GIT_TAG in ${ducklake_cmake}: [lindex ${pinned} 1]"
    }
    reinplace "s|GIT_TAG ${ducklake_commit}|& EXTENSION_VERSION [string range ${ducklake_commit} 0 6]|" ${ducklake_cmake}
}

depends_build-append \
                    port:pkgconfig \
                    port:python${python_version} \
                    port:zstd

depends_lib-append  port:croaring

if {[string match *gcc* ${configure.compiler}]} {
    # Without the following setting, the build fails miserably at linking with every possible symbol undefined.
    # Undefined symbols: __ZNK6duckdb5Value14GetValueUnsafeItEET_v, __ZN6duckdb11LogicalType3MAPES0_S0_ etc.
    configure.args-append \
                    -DEXTENSION_STATIC_BUILD=TRUE
    configure.ldflags-append \
                    -latomic
}

test.run            yes
test {
    set scratch ${workpath}/test
    file delete -force ${scratch}
    file mkdir ${scratch}
    system -W ${scratch} "${cmake.build_dir}/duckdb \
        -cmd \"SET VARIABLE expected_version = 'v${version}'\" \
        -f ${worksrcpath}/test/macports-smoke.sql"
}
