# -*- 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.9.0
github.tarball_from releases
revision            0

checksums           rmd160  228f17de34f05b3159a24593c9e558edb1f4b1b0 \
                    sha256  cd27277649b5ed50d19875031de516e270b22e890d9db65ed9af57d18ebc498d \
                    size    2366082

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     39 310 311 312 313
github.livecheck.regex {([0-9.]+)}

if {${subport} ne ${name}} {
    patchfiles      patch-wheel-pyproject.toml.diff

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

    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

    # Compiled typelib files need their dylibs' full install path
    patchfiles-append   patch-meson-gnome.diff

    # don't set CC for g-ir-scanner. MacPorts does that, often with modifications
    patchfiles-append   patch-meson-gnome-dont-set-cc-for-gir-scanner.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

    platform darwin 8 {

        github.setup        mesonbuild meson 1.6.1
        github.tarball_from releases
        revision            0

        checksums           rmd160  b7c38c2626e32a40c1989a54f7bffca1a4a01a28 \
                            sha256  1eca49eb6c26d58bbee67fd3337d8ef557c0804e30a6d16bfdf269db997464de \
                            size    2276144

        patchfiles          patch-meson161-remove-Wl,-no_weak_imports.diff \
                            patch-meson161-32bit-apple.diff \
                            patch-meson161-gcc-appleframeworks.diff \
                            patch-meson161-gnome.diff \
                            patch-meson161-gnome-dont-set-cc-for-gir-scanner.diff \
                            patch-meson161-clang-unknown-optimization-error.diff \
                            patch-meson161-search-prefix-for-cross-files.diff \
                            patch-meson161-tiger-no-rpath-fix.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}
        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}
        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}
            xinstall -m 0644 ${worksrcpath}/data/syntax-highlighting/vim/${d}/meson.vim ${destroot}${vim_dir}/${d}
        }
    }

    pre-activate {
        # the original meson port new pyXY-meson ports conflict
        if {![catch {lindex [registry_active meson] 0} installed]} {
            set _version [lindex $installed 1]
            if {[vercmp $_version 1.2.3] < 0} {
                registry_deactivate_composite meson "" [list ports_nodepcheck 1]
            }
        }
    }

    pre-test {
        reinplace "s|/usr/bin/env python3$|${python.bin}|" \
            ${worksrcpath}/run_tests.py \
            ${worksrcpath}/run_cross_test.py \
            ${worksrcpath}/run_meson_command_tests.py \
            ${worksrcpath}/run_project_tests.py \
            ${worksrcpath}/run_unittests.py

        set testpath "${worksrcpath}/test\\ cases"
        fs-traverse f ${testpath} {
            if { [string match *.py ${f}] } {
                reinplace "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
}
