# -*- 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 80341 2011-07-09 21:35:31Z jeremyhu@macports.org $

PortSystem          1.0

name                mesa

# Yes, it's a dated version. Newer versions of mesa don't work on darwin, so
# we're stuck here for a while If you need a newer version, join the
# xquartz-dev mailing list, and let us know why.

version             7.10.3
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 \
                    MesaGLUT-${version}.tar.bz2

worksrcdir          Mesa-${version}
platforms           macosx darwin
use_bzip2           yes
master_sites        http://www.buraphalinux.org/download/bls2.1/dvd_source/source/xorg/xserver/mesa/ \
                    ftp://ftp.freedesktop.org/pub/mesa/${version}/
checksums           MesaLib-7.10.3.tar.bz2 \
                    rmd160  bd9800a0cf98560a4424742cac635b4c07a39647 \
                    sha256  1e701fc839b872677ddca9ed8784d754c9da1fbeda98173980e06aa7df0e85c0 \
                    MesaGLUT-7.10.3.tar.bz2 \
                    rmd160  b3f166343210abbeabfc70ac3dba289ec5e34e96 \
                    sha256  a18edbdda2d722103417c54008e78e5de84fbbcadd7a218f25d4bc59da8962f5

depends_build \
	bin:makedepend:makedepend \
	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 \
	0001-apple-applegl_destroy_context-Pass-along-the-correct.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}
    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}"

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

post-patch {
	# 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
}

pre-configure {
	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"
	}
}

variant iglx description {Install a libGL that uses your X11 server's indirect GLX path for rendering (the default is off which allows libGL to accelerate rendering using OpenGL.framework)} {
    patchfiles-append mesa-7.10.2-iglx.patch
}

variant python26 conflicts python27 description {Use python 2.6} {
    depends_build-append \
        port:py26-libxml2
    build.args-append \
        PYTHON2="${prefix}/bin/python2.6"
}

variant python27 conflicts python26 description {Use python 2.7} {
    depends_build-append \
        port:py27-libxml2
    build.args-append \
        PYTHON2="${prefix}/bin/python2.7"
}

if {![variant_isset python26] && ![variant_isset python27]} {
    default_variants +python27
}

platform darwin 8 {
    patchfiles-append tiger-exports.patch

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

        # These are being comented out to find out if they're still broken
        # 6/6/2011
        #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.type  regex
livecheck.url   http://www.buraphalinux.org/download/bls1.4/dvd_source/source/x11/xserver/mesa/
livecheck.regex "MesaLib-(\\d+\\.\[02468\](?:\\.\\d+)*)${extract.suffix}"
