# -*- 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 52618 2009-06-19 23:36:26Z jeremyhu@macports.org $

PortSystem          1.0

name                mesa
version             7.4.3
set ASGLX_version   57
categories          x11 graphics
maintainers         jeremyhu and.damore 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 \
                    AppleSGLX-${ASGLX_version}.tar.bz2:xq

# The Mesa glut versions of glxgears and glxinfo aren't Tiger friendly
#                    MesaDemos-${version}.tar.bz2:mesa \

worksrcdir          Mesa-${version}
platforms           macosx darwin
use_bzip2           yes
master_sites        sourceforge:mesa3d:mesa \
                    http://xquartz.macosforge.org/downloads/src/:xq
checksums           MesaLib-7.4.3.tar.bz2 \
                    md5     70a983ba3deaa8bd63b18bbab283f698 \
                    sha1    e8f6ea3ca7784ff7d8e413eb8ca402c90579cc81 \
                    rmd160  10ddaa327e2acadeae4bbe4221964e29e3ecfab6 \
                    MesaGLUT-7.4.3.tar.bz2 \
                    md5     03a4beeef74fc5ef0b1d6d04710e5a8a \
                    sha1    466e5247b288b13123bf5b6ca412888be3787154 \
                    rmd160  b99e3e8af99719c8cc2009f51fe624d39b169870 \
                    AppleSGLX-57.tar.bz2 \
                    md5     7e81c6952030660874511090dd5b31da \
                    sha1    80e5eb5cc6e9b1352bef86b67a14e3f6f61ef522 \
                    rmd160  9bb84bd53ca8172f794464704de6ba4c789cdea9

# 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
# PROGS="glxgears glxinfo"
build.args-append INSTALL_DIR=${prefix}
destroot.args-append INSTALL_DIR=${prefix}

# Ensure correct compilers are used in Makefiles.
build.args-append \
    CC=${configure.cc} \
    CXX=${configure.cxx}

variant universal {
	build.args-append RC_CFLAGS="${configure.universal_cflags}"
	eval configure.cflags-append ${configure.universal_cflags}
	eval configure.ldflags-append ${configure.universal_ldflags}

	# Ensure test programs glxgears and glxinfo are universal.
	post-patch {
		reinplace "s|(CC) tests|(CC) ${configure.universal_cflags} ${configure.universal_ldflags} tests|" \
			${worksrcpath}/../AppleSGLX-${ASGLX_version}/Makefile
	}
}

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

default_variants +hw_render

post-patch {
	reinplace "s:-fno-strict-aliasing:-fno-strict-aliasing -fno-common:g" ${worksrcpath}/configs/darwin
	reinplace "s:osmesa::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-extract {
	if {! [file exists "${worksrcpath}/configs/current"]} {
		ln -s darwin ${worksrcpath}/configs/current
	}
}

# Do this if you want to install the mesa-provided glxinfo and glxgears... doesn't work on Tiger
#post-destroot {
#	xinstall -m 755 -W "${worksrcpath}/progs/xdemos" glxgears glxinfo "${destroot}${prefix}/bin"
#}

variant hw_render description {Install a libGL.dylib that uses OpenGL.framework to allow rendering on your graphics hardware} {
#	post-patch {
#		system "cd ${worksrcpath}/../AppleSGLX-${ASGLX_version} && cat ${filespath}/asglx-*.patch | patch -p0"
#	}

	post-build {
		system "cd ${worksrcpath}/../AppleSGLX-${ASGLX_version} && ${build.cmd} builds/libGL.1.2.dylib ${build.args} LDFLAGS='${configure.ldflags}' CFLAGS='${configure.cppflags} ${configure.cflags}' INSTALL_DIR='${prefix}'"
		system "cd ${worksrcpath}/../AppleSGLX-${ASGLX_version} && ${build.cmd} programs ${build.args} LDFLAGS='${configure.ldflags}' CFLAGS='${configure.cppflags} ${configure.cflags}' INSTALL_DIR='${prefix}'"
	}

	post-destroot {
		system "cd ${worksrcpath}/../AppleSGLX-${ASGLX_version} && ${destroot.cmd} ${destroot.target} ${destroot.destdir} ${destroot.args} INSTALL_DIR='${prefix}'"
		system "cd ${worksrcpath}/../AppleSGLX-${ASGLX_version} && ${destroot.cmd} install_programs ${destroot.destdir} ${destroot.args} INSTALL_DIR='${prefix}'"
	}
}

platform darwin 8 {
	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.regex "<title>MesaLib (.*) released.*</title>"
