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

github.setup        mesonbuild meson 1.2.2
revision            0

checksums           rmd160  b059e5c44efb0671373c78659f5ed36956d7dc1b \
                    sha256  4a0f04de331fbc7af3b802a844fc8838f4ccd1ded1e792ba4f8f2faf8c5fe4d6 \
                    size    2190455

github.tarball_from releases
license             Apache-2
categories          devel python
maintainers         {soap.za.net:git @SoapZA} openmaintainer
supported_archs     noarch
installs_libs       no
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.

# As of verison 0.62.0, requires python 3.7 or later

python.default_version  311
python.link_binaries    no

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

# 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

platform darwin 8 {

    # this meson is modified for systems without @rpath support
    # and is therefore fragile. Keep pegged, and update occasionally

    github.setup        mesonbuild meson 0.64.1
    github.tarball_from releases
    revision            0
    checksums           rmd160  6ed7a0c8b1ac2a0de91d25aa253ad2efa132c6dc \
                        sha256  3a8e030c2334f782085f81627062cc6d4a6771edf31e055ffe374f9e6b089ab9 \
                        size    2089752

    patchfiles          patch-meson64-tiger-no-rpath-fix.diff \
                        patch-meson64-remove-Wl,-no_weak_imports.diff \
                        patch-meson64-32bit-apple.diff \
                        patch-meson64-gcc-appleframeworks.diff \
                        patch-meson64-gnome.diff \
                        patch-meson64-clang-unknown-optimization-error.diff \
                        patch-meson64-search-prefix-for-cross-files.diff
}

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

post-destroot {
    set python_prefix ${frameworks_dir}/Python.framework/Versions/${python.branch}
    ln -s  ${python_prefix}/bin/meson ${destroot}${prefix}/bin/meson
    ln -s  ${python_prefix}/share/man/man1/meson.1 ${destroot}${prefix}/share/man/man1

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

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

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

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

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

# the following block avoids requiring users to 'sudo port select python3 python37'
# doing a file test for ${prefix}/bin/python3 and requiring this
# to be honest would have been much simpler, but not the "MacPorts way"
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

# restrict livecheck to release versions only, no pre-releases
github.livecheck.regex  {(\d+(?:\.\d+)*)}
