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

name                mesa
epoch               1
version             17.1.6
revision            2
categories          x11 graphics
maintainers         {jeremyhu @jeremyhu} openmaintainer
license             MIT
license_noconflict  py27-libxml2
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
platforms           macosx darwin
use_xz              yes

checksums           sha1    2acc201e24ea67c5231074d6746a42a747228ed6 \
                    rmd160  43a9b758462316ad344ccdccf72d6d0ce295620e \
                    sha256  0686deadde1f126b20aa67e47e8c50502043eee4ecdf60d5009ffda3cebfee50 \
                    size    9868932

depends_build       port:pkgconfig \
                    port:flex \
                    port:bison \
                    port:gindent

depends_lib         port:expat \
                    port:xorg-xorgproto \
                    port:xorg-libxcb \
                    port:xorg-libX11 \
                    port:xorg-libXext \
                    port:xorg-libXdamage \
                    port:xorg-libXfixes \
                    port:xorg-libXi \
                    port:xorg-libXmu \
                    port:xorg-libXxf86vm

patch.pre_args      -p1
patchfiles \
    0001-glxcmds-Fix-a-typo-in-the-__APPLE__-codepath.patch \
    0002-glext.h-Add-missing-include-of-stddef.h-for-ptrdiff_.patch \
    0003-applegl-Provide-requirements-of-_SET_DrawBuffers.patch \
    0004-mesa-Deal-with-size-differences-between-GLuint-and-G.patch

if {[string match *gcc* ${configure.compiler}]} {
    # Older gcc fail to do -Werror=missing-prototypes correctly
    # https://trac.macports.org/ticket/46827
    patchfiles-append no-missing-prototypes-error.patch
}

use_autoreconf      yes
autoreconf.args     -fvi

configure.args \
    --disable-silent-rules \
    --with-platforms=x11 \
    --disable-egl \
    --disable-gbm \
    --disable-osmesa \
    --disable-llvm-shared-libs \
    --with-gallium-drivers=

configure.env-append \
    INDENT=${prefix}/bin/gindent

# This project is affected by a bug in Apple's gcc driver driver that I fixed in the apple-gcc42 port.
# Use that or clang.
# clang-700.1.81 (Xcode 7.2.1) fails at:
#     disk_cache.c:637:7: error: cannot compile this atomic library call yet
#           p_atomic_add(cache->size, - (uint64_t)sb.st_blocks * 512);
#           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compiler.blacklist gcc-3.3 gcc-4.0 gcc-4.2 llvm-gcc-4.2 {clang < 800}

platform darwin {
    if {${os.major} < 11} {
        # https://bugs.freedesktop.org/show_bug.cgi?id=89088
        configure.env-append INDENT=cat

        patchfiles-append static-strndup.patch
    }
    
    if {${os.major} < 10} {
        # https://trac.macports.org/ticket/52811
        patchfiles-append patch-include-GL-mesa_glinterop_h.diff
    }

    if {${os.major} < 11} {
        # See https://trac.macports.org/ticket/54643
        configure.args-append --disable-glx-tls
    }

    if {${os.major} < 14} {
        # See https://trac.macports.org/ticket/54638
        # See https://trac.macports.org/ticket/54643
        patchfiles-append disable_shader_cache.patch
    }

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

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

configure.cppflags-delete -I${prefix}/include

variant python27 description {Use python 2.7} {
    depends_build-append \
        port:py27-libxml2
    configure.env-append \
        PYTHON2=${prefix}/bin/python2.7
}

if {![variant_isset python27]} {
    default_variants-append +python27
}

variant osmesa description {enable OSMesa library} {
    configure.args-delete --disable-osmesa
    configure.args-append --enable-osmesa
}
default_variants-append +osmesa

#variant openvg description {enable support for OpenVG API} {
#    configure.args-delete --disable-egl --disable-openvg
#    configure.args-append --enable-egl --enable-openvg
#}
#default_variants-append +openvg

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