# -*- 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               python 1.0

# meta-version; bump whenever underlying variants are updated. Needed to
# support portindex (variants can't have different versions.)
version                 14
# Needed for change to meta-versioning
epoch                   1
name                    py-clang
python.versions         310 311 312 313 314
license                 NCSA
maintainers             {eborisch @eborisch} \
                        openmaintainer
description             Python bindings for clang
long_description        {*}${description}
homepage \
    https://github.com/llvm/llvm-project/tree/main/clang/bindings/python

supported_archs         noarch
platforms               {darwin any}

livecheck.url           https://api.github.com/repos/llvm/llvm-project/git/refs/tags
# Update this to the most recent clang supported
livecheck.version       21.1.4
livecheck.regex         {llvmorg-([\d.]+)\"}

if {${name} ne ${subport}} {
    # Share llvm's downloads
    dist_subdir             llvm
    use_xz                  yes

    checksums \
     cfe-3.7.1.src.tar.xz \
      rmd160  185b0f75970bc50682766a21794440578db87b5d \
      sha256  56e2164c7c2a1772d5ed2a3e57485ff73ff06c97dff12edbeea1acc4412b0674 \
     clang-19.1.7.src.tar.xz \
      rmd160  320657b6b78ae206d3a95cebeb4050062f6dc519 \
      sha256  11e5e4ecab5338b9914de3b83a4622cb200de466b7c56ba675afb72fa7d64675 \
     clang-20.1.8.src.tar.xz \
      rmd160  7046dae6f7a51ad9aba5149c9906d0ad4fc49e07 \
      sha256  b7a1b7b0af7b9c7596af6bd46e36d11321926eaa66a7a7dc957ab0a1375ee4b0 \
     clang-21.1.4.src.tar.xz \
      rmd160  81d1303b8699d8f948f9bd0e3c316ea42c06a08d \
      sha256  3e8e25a7478bfb0ef510fff35d1a43bdfb62c7727bf98bb43ad33deef2f9f1a2

    depends_build-append    port:py${python.version}-setuptools

    # Keeping 37 around for old systems; otherwise two latest releases.
    set clanglist       {37    19     20     21}
    set clangvlist      {3.7.1 19.1.7 20.1.8 21.1.4}

    foreach cvnum $clanglist {
        # Explictly use (and depend on) the libclang we select during install
        set cvidx [lsearch $clanglist $cvnum]
        set clang_version [lindex $clangvlist $cvidx]
        set cflist {}
        foreach v [lreplace $clanglist $cvidx $cvidx] {lappend cflist clang$v}
        if {${cvnum} < 90 && ${cvnum} >= 37} {
            set diststub cfe
        } else {
            set diststub clang
        }
        # Once clang reaches version 370, this will need to be revisited...
        if {${cvnum} >= 37} {
            set cvstr [string index $cvnum 0].[string index $cvnum 1]
        } else {
            set cvstr ${cvnum}
        }
        set substr \
          "library_path = '${prefix}/libexec/llvm-${cvstr}/lib'"
        variant clang${cvnum} description {
               Use clang${cvnum}'s libclang
        } conflicts {*}${cflist} "
            master_sites        https://releases.llvm.org/${clang_version} \
                                https://github.com/llvm/llvm-project/releases/download/llvmorg-${clang_version}
            depends_lib-append  port:clang-${cvstr}
            distfiles           ${diststub}-${clang_version}.src.tar.xz
            worksrcdir          ${diststub}-${clang_version}.src/bindings/python
            post-patch {
                reinplace {s^library_path = None^$substr^} clang/cindex.py
                reinplace {s/version=.*/version=\"${clang_version}\",/} setup.py
            }
            livecheck.version   ${clang_version}
            livecheck.regex \
                \"llvmorg-([string range $clang_version 0 1]\\\\.\\\[0-9.\\\]+)\\\"\"
        "
    }

    test.run            yes
    python.test_framework unittest

    if {![variant_isset clang37] &&
        ![variant_isset clang19] &&
        ![variant_isset clang20] &&
        ![variant_isset clang21]} {
        default_variants +clang21
    }

    pre-extract {
        # Will never hit this when installing from packages, which is OK, as
        # they will have the default variant set above.
        if {![variant_isset clang37] &&
            ![variant_isset clang19] &&
            ![variant_isset clang20] &&
            ![variant_isset clang21]} {
            ui_error "At least one of the clangNN variants must be active."
            return -code error "Unsupported (no) variants selected."
        }
    }

    post-extract {
        file copy ${filespath}/setup.py ${worksrcpath}/
    }

    post-destroot {
        set DOCDIR ${destroot}${prefix}/share/doc/${subport}
        xinstall -d ${DOCDIR}
        file copy ${worksrcpath}/README.txt ${DOCDIR}
    }

    notes {
        The path to the selected libclang variant has been recorded in the\
        installed package. It can be overridden with:\
        cindex.set_library_(path|file)().
    }
}
