# -*- 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
# $Id: Portfile 67787 2010-05-18 14:14:23Z jmr@macports.org $

PortSystem          1.0

name                mesa
version             7.8.1
revision            1
categories          x11 graphics
maintainers         jeremyhu openmaintainer
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            http://mesa3d.sourceforge.net/
distfiles           MesaLib-${version}.tar.bz2:mesa \
                    MesaGLUT-${version}.tar.bz2:mesa \
                    MesaDemos-${version}.tar.bz2:mesa

worksrcdir          Mesa-${version}
platforms           macosx darwin
use_bzip2           yes
master_sites        http://www.buraphalinux.org/download/bls1.4/dvd/source/x11/xserver/mesa/:mesa \
                    ftp://ftp.freedesktop.org/pub/mesa/${version}/:mesa
checksums           MesaLib-7.8.1.tar.bz2 \
                    md5     25ec15f8e41fde6d206118cc786dbac4 \
                    sha1    1c15c62f845ef9b648366aa804410f68f7f79172 \
                    rmd160  b7b989342dc80a2a12456c17882e652db7bd86cd \
                    MesaGLUT-7.8.1.tar.bz2 \
                    md5     6bae516a44c6d26ff3152c960ab648e7 \
                    sha1    192a4c33cb5841b3ffaa282fc2fb258b064488ad \
                    rmd160  0592c2b0a7cd3eb9abacf8d0a3a8314abac2ff44 \
                    MesaDemos-7.8.1.tar.bz2 \
                    md5     9ef47f911869657c6bf2f43ebce86b61 \
                    sha1    42618c17fb7be6d96ef73ff007e41aac5d3ded65 \
                    rmd160  f22b6619a30e581e1d7388fec62cba5ed0ca9578

# glut port is here to "clean out" the glut port if it's installed to avoid conflict
depends_build \
	bin:makedepend:makedepend \
	port:glut \
	bin:tclsh8.5:tcl

depends_lib \
	port:xorg-glproto \
	port:xorg-dri2proto \
	port:xorg-libXfixes \
	port:xorg-libXi \
	port:xorg-libXmu

use_configure  no
use_parallel_build yes

patch.pre_args -p1
patchfiles \
	mesa-7.2-drm_headers.patch

build.target default
build.args-append INSTALL_DIR=${prefix}
destroot.args-append INSTALL_DIR=${prefix}

# Ensure correct compilers are used in Makefiles.
if {${os.platform} == "darwin" && ${os.major} == 9} {
    # http://trac.macports.org/ticket/24345
    configure.cc /usr/bin/gcc-4.2
    configure.cxx /usr/bin/g++-4.2
}

variant universal {}
if {[variant_isset universal]} {
    set extra_cflags ${configure.universal_cflags}
    set extra_ldflags ${configure.universal_ldflags}
} else {
    set extra_cflags ${configure.cc_archflags}
    if {![info exists configure.ld_archflags]} {
        eval configure.ldflags-append ${configure.cc_archflags}
        set extra_ldflags ${configure.cc_archflags}
    } else {
        set extra_ldflags ${configure.ld_archflags}
    }
}

if {$extra_cflags != ""} {
    append build.args " " RC_CFLAGS="${extra_cflags}"
}

if { ![file exists /usr/include/Xplugin.h] } {
        # Xplugin.h is missing on Tiger
        configure.cppflags-append -I${filespath}/include
}

configure.cppflags-delete -I${prefix}/include
build.args-append \
    CC="${configure.cc} ${configure.cppflags}" \
    CXX="${configure.cxx} ${configure.cppflags}"

default_variants +hw_render

post-extract {
	if {! [file exists "${worksrcpath}/configs/current"]} {
		ln -s darwin ${worksrcpath}/configs/current
	}
}

post-patch {
	# http://trac.macports.org/ticket/24337
	reinplace "/SRC_DIRS/ s/glew//g" ${worksrcpath}/configs/darwin

	# Ensure correct compilers are used in mklib.
	reinplace "s:LINK=\"g++\":LINK=\"${configure.cxx}\":"  ${worksrcpath}/bin/mklib
	reinplace "s:LINK=\"cc\":LINK=\"${configure.cc}\":"    ${worksrcpath}/bin/mklib
}

post-destroot {
	xinstall -m 755 -W "${worksrcpath}/progs/xdemos" glxgears glxinfo "${destroot}${prefix}/bin"

	# http://trac.macports.org/ticket/24337
	# http://trac.macports.org/ticket/24349
	# mesa just installs all headers from GL/*.h
	delete "${destroot}${prefix}/include/GL/glew.h"
	delete "${destroot}${prefix}/include/GL/glxew.h"
	delete "${destroot}${prefix}/include/GL/wglew.h"
}

variant hw_render description {Install a libGL.dylib that uses OpenGL.framework to allow rendering on your graphics hardware} {
    patchfiles-append \
        0001-apple-Initial-import-of-libGL-for-OSX-from-AppleSGLX.patch \
        0002-apple-Remove-duplicate-headers-that-already-exist-in.patch \
        0003-apple-Change-ifdefs-for-DRI-to-be-DRI-APPLE.patch \
        0004-darwin-Use-clientattrib.patch \
        0005-apple-Integrate-our-libGL-into-the-existing-build-sy.patch \
        0006-apple-Use-mesa-gl.h-rather-than-generating-one.patch \
        0007-Use-X_LIBS-from-pkg-config-instead-of-libdir-for-loc.patch
}

platform darwin 8 {
    patchfiles-append patch-Mesa_7.6-src-glu-sgi-glu.exports.darwin.diff

    post-patch {
        # http://trac.macports.org/ticket/24345
        # http://trac.macports.org/ticket/24393
        reinplace "/SRC_DIRS/ s/gallium//" ${worksrcpath}/configs/darwin
        reinplace "/SRC_DIRS/ s/mesa//" ${worksrcpath}/configs/darwin

        # libOSMesa fails on Tiger, too...
        reinplace "/DRIVER_DIRS/ s/osmesa//" ${worksrcpath}/configs/darwin

	# http://trac.macports.org/ticket/24366
	reinplace "s:-fno-strict-aliasing:-fno-strict-aliasing -fno-common:g" ${worksrcpath}/configs/darwin
    }

    post-activate {
        if {[variant_isset hw_render]} {
            ui_msg "In order to use OpenGL on Tiger, you need to use MacPorts' X11 server (xorg-server) rather than Apple's."
        }
    }
}

livecheck.name  Mesa
