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

epoch                   1

name                    mesa
conflicts               gl-headers
version                 25.2.5
revision                1
checksums               rmd160  cb05a5256e61a8a373cd9aa7a3423fb109357760 \
                        sha256  bb6243e7a6f525febfa1e6ab50827ca4d4bfdad73812377b0ca9b6c50998b03e \
                        size    43723176

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 \
                        patch-fix-zink.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          -Degl=disabled \
                        -Dgles1=disabled \
                        -Dgles2=disabled

set drivers             [list 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            21
    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)
    lappend drivers         llvmpipe
}

# Does not work currently
# https://github.com/KhronosGroup/MoltenVK/issues/2650
variant zink description "enable the zink driver" {
    platform darwin {
        depends_build-append    port:MoltenVK
        configure.args-append   -Dmoltenvk-dir=${prefix}
    }
    depends_run-append      port:vulkan-loader
    lappend drivers         zink
}

# Avoid enabling on macOS as it disables hardware acceleration
if {${os.platform} ne "darwin"} {
    default_variants        +llvm
}

post-patch {
    reinplace "s|\"libvulkan\\.1\\.dylib\"|\"${prefix}/lib/libvulkan.1.dylib\"|g" \
        src/gallium/drivers/zink/zink_screen.c \
        src/gfxstream/guest/vulkan-mapper/GfxStreamVulkanMapper.cpp
}

pre-configure {
    configure.args-append   -Dgallium-drivers=[join $drivers ,]
}

post-destroot {
    # avoid conflict with ANGLE
    move ${destroot}${prefix}/include/KHR/khrplatform.h \
        ${destroot}${prefix}/include/KHR/khrplatform_mesa.h
    reinplace "s|khrplatform\\.h|khrplatform_mesa.h|" \
        ${destroot}${prefix}/include/GL/glcorearb.h \
        ${destroot}${prefix}/include/GL/glext.h
}

variant tests description "build tests" {
    # mesa uses its 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}
