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

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

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  c81a34ad0839186e7256083963151c09f7e7d3b9 \
                    sha256  1deae9d4917bd069c46c25c6ddbe51a2ef06cc7c12e6a10132af4fa0fb834a14 \
                    size    119467578 \
                    ducklake-${ducklake_commit}${extract.suffix} \
                    rmd160  7192f461de04e1e67b87888a36ee145890dbee01 \
                    sha256  60e9de47996be6464c883165da3e62cd10eb6dbefdf1dac1bdeb70210f4e9244 \
                    size    1211753

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