# -*- 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        duckdb duckdb 2.0.0 v
github.tarball_from archive
revision            0

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

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  1a354a9ed5d435bf1f35bcc9336ad2ce682039ae \
                    sha256  7e89053255046bfb26afbd7f524a2fb84df7dbef496aa911ea35e4a1182fdc00 \
                    size    106477075 \
                    ducklake-${ducklake_commit}${extract.suffix} \
                    rmd160  0044daa0d9ae6b5e0c698493b8d472567de27c43 \
                    sha256  264fa356b06f62d1601358a6a931ee522b41813e05566ae46010e2cbe9a832d7 \
                    size    1099005

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' \
                    -DLINK_CORE_EXTENSIONS=ON \
                    -DOVERRIDE_GIT_DESCRIBE=v${version} \
                    -DPython3_EXECUTABLE=${prefix}/bin/python${python_branch}

# DuckDB 2.0 requires C++17
compiler.cxx_standard 2017

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"
}
