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

name                py-meson
# update version and revision also in the meson port
github.setup        mesonbuild meson 1.11.1
github.tarball_from releases
revision            4

checksums           rmd160  c7bd966adc6b4b9ebd8f63ef988ac768cabec5eb \
                    sha256  6788ae299979643f8d841bcaf64352558436cae45a0355148a3aeeccf7913866 \
                    size    5190321

license             Apache-2
categories-append   devel
maintainers         {soap.za.net:git @SoapZA} {reneeotten @reneeotten} openmaintainer
supported_archs     noarch

homepage            https://mesonbuild.com

description         Meson - A high productivity build system
long_description    Meson is a build system designed to optimize programmer productivity. \
                    It aims to do this by providing simple, out-of-the-box support for modern \
                    software development tools and practices, such as unit tests, coverage reports, \
                    Valgrind, CCache and the like. It is both extremely fast, and, even more importantly, \
                    as user friendly as possible.

python.versions     310 311 312 313 314
github.livecheck.regex {([0-9.]+)}

if {${subport} ne ${name}} {
    depends_run-append  port:py${python.version}-setuptools \
                        port:ninja

    # requires a newer install_name_tool on older systems
    if {${os.platform} eq "darwin" && ${os.major} <= 9} {
        depends_run-append \
                        port:cctools
    }

    if {${os.platform} eq "darwin" && ${os.major} <= 10} {
        patchfiles-append \
                        patch-meson-remove-Wl,-no_weak_imports.diff
    }

    # https://github.com/mesonbuild/meson/issues/6187
    patchfiles-append   patch-meson-32bit-apple.diff

    # https://github.com/mesonbuild/meson/pull/9211
    patchfiles-append   patch-meson-gcc-appleframeworks.diff

    # disable warning not accepted by older clang versions
    # this manifests currently on systems up to 10.9
    # https://github.com/mesonbuild/meson/issues/8307
    if {${os.platform} eq "darwin" && ${os.major} <= 13} {
        patchfiles-append \
                        patch-meson-clang-unknown-optimization-error.diff
    }

    # add a search path for crossfiles in our prefix
    patchfiles-append   patch-meson-search-prefix-for-cross-files.diff

    # see: https://trac.macports.org/ticket/71165
    patchfiles-append   patch-meson-appleframeworks.diff

    # https://github.com/mesonbuild/meson/pull/15721
    patchfiles-append   patch-fix-rpath.diff

    # https://github.com/mesonbuild/meson/pull/15752
    patchfiles-append   patch-fix-whole-rpath.diff

    post-patch {
        reinplace "s|@@PREFIX@@|${prefix}|g" ${worksrcpath}/data/shell-completions/bash/meson \
                                             ${worksrcpath}/mesonbuild/coredata.py
        reinplace "s|@@SUBPORT@@|${subport}|g" ${worksrcpath}/data/shell-completions/bash/meson \
                                             ${worksrcpath}/mesonbuild/coredata.py
    }

    post-destroot {
        # install our MacPorts cross files
        xinstall -m 0755 -d ${destroot}${prefix}/share/${subport}/meson
        copy ${filespath}/cross ${destroot}${prefix}/share/${subport}/meson

        fs-traverse f ${destroot}${prefix}/share/${subport}/meson/cross/ {
            if {![file isdirectory ${f}]} {
                reinplace "s|@@PREFIX@@|${prefix}|g" ${f}
            }
        }

        # install shell completion files
        set bash_completion_dir ${prefix}/share/${subport}/bash-completion/completions
        xinstall -d ${destroot}${bash_completion_dir}
        xinstall -m 0644 ${worksrcpath}/data/shell-completions/bash/meson ${destroot}${bash_completion_dir}

        set zsh_completion_dir ${prefix}/share/${subport}/zsh/site-functions
        xinstall -d ${destroot}${zsh_completion_dir}
        xinstall -m 0644 ${worksrcpath}/data/shell-completions/zsh/_meson ${destroot}${zsh_completion_dir}

        # install editor syntax files
        set vim_dir ${prefix}/share/${subport}/vim/vimfiles
        foreach d {ftdetect ftplugin syntax indent} {
            xinstall -d ${destroot}${vim_dir}/${d}
            xinstall -m 0644 ${worksrcpath}/data/syntax-highlighting/vim/${d}/meson.vim ${destroot}${vim_dir}/${d}
        }
    }

    pre-test {
        reinplace "s|/usr/bin/env python3$|${python.bin}|" \
            {*}[glob ${worksrcpath}/run_*.py]

        set testpath "${worksrcpath}/test\\ cases"
        fs-traverse f ${testpath} {
            if { [string match *.py ${f}] } {
                reinplace -q "s|/usr/bin/env python3$|${python.bin}|" ${f}
            }
        }
    }

    test.run            yes
    python.test_framework
    test.env            CXX=${configure.cxx} \
                        "CXXFLAGS=${configure.cxxflags} [get_canonical_archflags cxx]" \
                        CC=${configure.cc} \
                        "CFLAGS=${configure.cflags} [get_canonical_archflags cc]" \
                        "LDFLAGS=${configure.ldflags} [get_canonical_archflags ld]" \
                        PREFIX=${prefix}
    test.cmd            ./run_tests.py
    test.target
}
