# -*- 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               meson 1.0
PortGroup               conflicts_build 1.0
PortGroup               muniversal 1.1
PortGroup               legacysupport 1.0

# timespec_get() and others
legacysupport.newest_darwin_requires_legacy 17

name                    mesa
conflicts               gl-headers
epoch                   1
version                 25.0.2
revision                0
checksums               rmd160  9cc4dc7b4e0324857dd35a3cf7818059ae5aefd5 \
                        sha256  adf904d083b308df95898600ffed435f4b5c600d95fb6ec6d4c45638627fdc97 \
                        size    46705776

categories              x11 graphics
maintainers             {jeremyhu @jeremyhu} openmaintainer
license                 MIT
description             Mesa 3D Graphics Library
long_description        Mesa is an open-source implementation of the OpenGL specification, \
                        a system for rendering interactive 3D graphics.

homepage                https://www.mesa3d.org
master_sites            https://archive.mesa3d.org
use_xz                  yes

# Disable unexpected download of subprojects
meson.wrap_mode         nodownload

set py_ver              3.13
set py_ver_nodot        [string map {. {}} ${py_ver}]

depends_build-append    path:bin/pkg-config:pkgconfig \
                        port:flex \
                        port:bison \
                        port:python${py_ver_nodot} \
                        port:py${py_ver_nodot}-mako \
                        port:py${py_ver_nodot}-packaging \
                        port:py${py_ver_nodot}-yaml \
                        port:xorg-xorgproto

depends_lib-append      port:expat \
                        port:xorg-libxcb \
                        port:xorg-libX11 \
                        port:xorg-libXext \
                        port:xorg-libXdamage \
                        port:xorg-libXrandr \
                        port:zlib \
                        port:zstd

patchfiles              patch-meson-spec-python.diff \
                        patch-pre-10.8-scandir.diff \
                        patch-fix-linking.diff

# https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/32658
patchfiles-append       patch-fix-32-bit.diff

# https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/34718
patchfiles-append       patch-missing-mach-init-include.patch

# MP ticket 66269
# the sizes of int and GLint are different on 10.4 PPC, at least
# not sure how many systems this might affect, but all systems on buildbot were OK
# restrict to Tiger for now, therefore
platform darwin 8 {
    patchfiles-append   patch-mesa-apple-cgi-int-differences.diff
}

post-patch {
    reinplace "s|@@python3@@|${prefix}/bin/python${py_ver}|g" meson.build
}

compiler.c_standard     2011
compiler.cxx_standard   2017
# Build issues on mac OS 10.10
# ../mesa-22.1.7/src/util/compiler.h:90:7: error: builtin feature check macro requires a parenthesized identifier
# #elif HAS_CLANG_FALLTHROUGH
compiler.blacklist-append  {clang < 800}

configure.args          -Dosmesa=true \
                        -Dgallium-drivers=softpipe

platform darwin {
    if {${os.major} < 9} {
          # Xplugin.h is missing on Tiger
          configure.cppflags-append -I${filespath}/include
    }

    if {${os.major} < 10} {
          # avoid use of libunwind, which is not in the System on < darwin 10
          # see https://trac.macports.org/ticket/65934
          configure.args-append  -Dlibunwind=disabled
    }

    pre-configure {
        if {${os.major} < 11} {
            # https://trac.macports.org/ticket/25677
            if { ![file exists /usr/lib/libXplugin.dylib] } {
                ui_error "Detected a problem with your development environment.  Please work around it by executing:"
                ui_error "sudo ln -s libXplugin.1.dylib /usr/lib/libXplugin.dylib"
                return -code error "missing libXplugin.dylib"
            }
        }
    }
}

if {[string match *gcc* ${configure.compiler}]} {
    configure.ldflags-append -latomic

    # https://github.com/macports/macports-ports/pull/16260#issuecomment-1268741658
    configure.cxxflags-append -fpermissive
}

variant llvm description "enable the llvmpipe driver" {
    set llvm_ver            19
    depends_lib-append      port:llvm-${llvm_ver}
    # for llvm-config
    configure.env-append    PATH=${prefix}/libexec/llvm-${llvm_ver}/bin:$env(PATH)
    # for llvm-ar
    build.env-append        PATH=${prefix}/libexec/llvm-${llvm_ver}/bin:$env(PATH)
    configure.args-replace  -Dgallium-drivers=softpipe \
                            -Dgallium-drivers=softpipe,llvmpipe
}

if {${os.platform} ne "darwin" || ${os.major} > 10} {
    default_variants        +llvm
}

variant tests description "build tests" {
    # mesa uses it's own internal gtest version and fails with newer
    conflicts_build-append gtest
    configure.args-replace -Dbuild-tests=false -Dbuild-tests=true
}

test.run                yes
test.cmd                meson
test.target             test

livecheck.type          regex
livecheck.url           ${homepage}
livecheck.regex         {relnotes/([0-9.]+)\.html}
