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

# timespec_get() and others
legacysupport.newest_darwin_requires_legacy 17

name                    mesa
version                 26.1.1
revision                0
epoch                   1
categories              x11 graphics devel
maintainers             {jeremyhu @jeremyhu} \
                        {@barracuda156 macos-powerpc.org:barracuda} \
                        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.
conflicts               gl-headers
homepage                https://www.mesa3d.org
master_sites            https://archive.mesa3d.org
use_xz                  yes
checksums               rmd160  db17f741b66d2a87f915fbceedc147e16b820736 \
                        sha256  8bd36c031cc6d0edfec04617527609454ee3a09ad53bdf983b45fc2c1e129b2e \
                        size    77544276

# Disable unexpected download of subprojects
meson.wrap_mode         nodownload

# The build system simply checks for poll.h and inotify.h, there is no option to avoid those.
conflicts_build         epoll-shim libinotify-kqueue poll-emulator

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

depends_build-append    port:bison \
                        port:flex \
                        path:bin/pkg-config:pkgconfig \
                        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 \
                        port:xorg-libXrandr

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

patchfiles              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

# https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/39047
# https://trac.macports.org/ticket/73334
# https://trac.macports.org/ticket/73343
patchfiles-append       patch-gallium_vl_csc_c.diff

# https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/40926
patchfiles-append       patch-fix-glx.diff

# https://gitlab.freedesktop.org/mesa/mesa/-/work_items/15528
patchfiles-append       patch-src-meson.diff

# Broken after https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35374
# patchfiles-append       patch-build-with-LLVM-7.diff

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

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}

# osmesa was removed: https://gitlab.freedesktop.org/mesa/mesa/-/issues/13214
configure.args-append   -Dgallium-drivers=softpipe \
                        -Dplatforms=macos,x11 \
                        -Dspirv-tools=disabled

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
}

# https://github.com/macos-powerpc/powerpc-ports/issues/88
platform darwin {
    post-destroot {
        system -W ${destroot} "install_name_tool -change @rpath/libgallium-${version}.dylib ${prefix}/lib/libgallium-${version}.dylib ./${prefix}/lib/libGL.dylib"
    }
}

variant llvm description "Enable the llvmpipe driver" {
    # See: https://github.com/iains/LLVM-7-branch/issues/10
    # Unusable on PowerPC unless fixed.
    if {${configure.build_arch} in [list ppc ppc64]} {
        set llvm_ver        7.1.1
        depends_lib-append  port:llvm-powerpc
    } else {
        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
}

# This was implicit default in MacPorts until https://github.com/macports/macports-ports/commit/9e1b9bfb0af73f2599822d7be36099227d5ef343
# Since angle port is fixed now, and to maintain better compatibility, this is now disabled here as well.
variant egl description "Build with EGL and GLES" {
    configure.args-append \
                    -Degl-native-platform=x11
}

# FIXME: as of v25.1.9, +llvm builds for powerpc, but JIT support is missing, which leads to:
# LLVM ERROR: Scattered relocations not supported for MachO AArch64 (!)
# See: https://github.com/iains/LLVM-7-branch/issues/10
# Later versions require reverting this: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35374
# Mirror: https://github.com/bminor/mesa-mesa/commit/6a28d6707ddaa20da70e07566c1cafa5ef51a020
# Also disabled by default due to https://github.com/macports/macports-ports/commit/7ed77200276ce02c654775a86404a8d78b8f8801
if {${os.platform} ne "darwin"} {
    default_variants        +llvm
}

# Build without EGL and friends:
if {![variant_isset egl]} {
    configure.args-append \
                    -Degl=disabled \
                    -Dgles1=disabled \
                    -Dgles2=disabled

    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
    }
} else {
    conflicts           angle
}

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}
