# -*- 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 63940 2010-02-19 00:35:42Z jeremyhu@macports.org $

PortSystem      1.0

name            vtk-devel
version         5.4.2
revision        1
set branch      [join [lrange [split ${version} .] 0 1] .]

categories      graphics math science devel

maintainers     dweber openmaintainer

description     3D visualization toolkit (www.vtk.org)

long_description \
An open source, freely available software system for computer graphics, image processing, and visualization used by thousands of researchers and developers around the world. VTK consists of a C++ class library, and several interpreted interface layers including Tcl/Tk, Java, and Python.

homepage        http://www.vtk.org/
master_sites    http://www.vtk.org/files/release/${branch} \
                http://www.vtk.org/doc/release/${branch}

distname        vtk-${branch}
distfiles       vtk-${version}.tar.gz

# Also update checksums in the data and doc variants below.
checksums       vtk-${version}.tar.gz \
                    md5 c2c797091d4b2128d9a1bd32c4b78227 \
                    sha1 508106a15c32326aa9ac737c7f0e7212c150d55f \
                    rmd160 607df8babe7413b92a5bd4d8c264fd69798df83f

eval extract.only   vtk-${version}.tar.gz

platforms       darwin

depends_build   port:cmake  \
                port:readline \
                port:gnutar


# Use gmake and build in a separate directory from the source
build.type      gnu
build.dir       ${workpath}/${distname}-build

use_parallel_build  no

# Global variables (used in variants)
set vtkDataFile     vtkdata-${version}.tar.gz
set vtkDocFile      vtkDocHtml-${version}.tar.gz
set vtkSharePath    ${prefix}/share/${distname}
set vtkDataPath     ${vtkSharePath}/data
set vtkDocPath      ${vtkSharePath}/doc
set vtkExamplePath  ${vtkSharePath}/examples
set vtkTestingPath  ${vtkSharePath}/testing
set vtkIncPath      ${prefix}/include/${distname}
set vtkLibPath      ${prefix}/lib/${distname}
set vtkBuildBinPath ${build.dir}/bin

# Using this dummy stage to inspect MacPorts variables (using 'port -d fetch vtk-devel')
#fetch { system "echo ${configure.args} " }

post-extract {
    move ${workpath}/VTK ${worksrcpath}
}

configure {
    xinstall -d -m 0755 ${build.dir}
    system "cd ${build.dir} && cmake ${configure.args} ${worksrcpath}"
}


# From vtk5 - look into how this works and whether it will replace the need to
# call install_name_tool in many variants.  Disabled until it can be checked.
#post-configure {
#    reinplace "s|c++|c++ -L${prefix}/lib |" \
#    ${worksrcpath}/Infovis/Testing/Cxx/CMakeFiles/InfovisCxxTests.dir/link.txt \
#    ${worksrcpath}/IO/Testing/Cxx/CMakeFiles/IOCxxTests.dir/link.txt \
#    ${worksrcpath}/Views/Testing/Cxx/CMakeFiles/ViewsCxxTests.dir/link.txt
#
#    if {[variant_isset tcltk]} {
#      reinplace "s|c++|c++ -L${prefix}/lib |" \
#      ${worksrcpath}/Wrapping/Tcl/CMakeFiles/vtk.dir/link.txt
#    }
#}


# To double-check all the cmake variable settings after the configure
#cd ${build.dir}
#sudo cmake -LAH ../vtk-${branch} > ~/cmake_vars.txt
# Similarly, for an interactive configuration with ccmake, try:
#cd ${build.dir}
#sudo ccmake ../vtk-${branch}

configure.args \
    -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
    -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
    -DCMAKE_INSTALL_PREFIX:PATH=${prefix} \
    -DCMAKE_INCLUDE_PATH:PATH=${prefix}/include \
    -DCMAKE_LIBRARY_PATH:PATH=${prefix}/lib \
    -DCMAKE_INSTALL_NAME_DIR:STRING=${prefix}/lib/${distname} \
    -DBUILD_DOCUMENTATION:BOOL=OFF \
    -DBUILD_EXAMPLES:BOOL=OFF \
    -DBUILD_TESTING:BOOL=OFF \
    -DBUILD_SHARED_LIBS:BOOL=OFF \
    -DVTK_DEBUG_LEAKS:BOOL=ON \
    -DVTK_USE_COCOA:BOOL=OFF \
    -DVTK_USE_CARBON:BOOL=OFF \
    -DVTK_USE_X:BOOL=OFF \
    -DVTK_USE_HYBRID:BOOL=ON \
    -DVTK_USE_GUISUPPORT:BOOL=ON \
    -DVTK_USE_INFOVIS:BOOL=ON \
    -DVTK_USE_PARALLEL:BOOL=ON \
    -DVTK_USE_RENDERING:BOOL=ON \
    -DVTK_USE_VIEWS:BOOL=ON \
    -DVTK_USE_GL2PS:BOOL=ON \
    -DVTK_USE_N_WAY_ARRAYS:BOOL=ON \
    -DVTK_USE_TK:BOOL=OFF \
    -DVTK_WRAP_JAVA:BOOL=OFF \
    -DVTK_WRAP_PYTHON:BOOL=OFF \
    -DVTK_WRAP_TCL:BOOL=OFF

if {[info exists universal_sysroot]} {
    configure.args-append \
        -DCMAKE_OSX_SYSROOT=${universal_sysroot}
}


# General post-destroot, see also variant specific sections below
post-destroot {
    # Create symlinks for generic lib, include, and share paths to this version
    ln -sf ${distname} ${destroot}${prefix}/lib/vtk
    ln -sf ${distname} ${destroot}${prefix}/include/vtk
    ln -sf ${distname} ${destroot}${prefix}/share/vtk
    # create version specific binaries and symlinks
    if [file exists ${destroot}${prefix}/bin/vtkEncodeString] {
        move ${destroot}${prefix}/bin/vtkEncodeString ${destroot}${prefix}/bin/vtk-${branch}-EncodeString
        ln -f -s vtk-${branch}-EncodeString ${destroot}${prefix}/bin/vtkEncodeString
    }
    if [file exists ${destroot}${prefix}/bin/lproj] {
        move ${destroot}${prefix}/bin/lproj ${destroot}${prefix}/bin/vtk-${branch}-lproj
        ln -f -s vtk-${branch}-lproj ${destroot}${prefix}/bin/lproj
    }
}


# -----------------------------------------------------------------------------
# PLATFORMS: platform darwin <version>
# Can be used to handle different tasks depending
# on the version of Mac OS X.  The version can be:
# 6 for 10.2 Jaguar,
# 7 for 10.3 Panther,
# 8 for 10.4 Tiger, or
# 9 for 10.5 Leopard.

# Nothing done here, but the darwin_8 platform needs to be initialized
# for use inside the post-destroot phase of the data variant
platform darwin 8 {}

# When building VTK with Cocoa, in Mac OS X >= 10.5.x, Cocoa supports two memory
# models: reference counting and garbage collection. This compiler flag lets
# it work with both.
platform darwin 9 {
    configure.args-append \
        -DVTK_REQUIRED_OBJCXX_FLAGS="-fobjc-gc"
}


# -----------------------------------------------------------------------------
# VARIANTS
# variant name [requires variant] [conflicts variant] [description description]

default_variants \
    +data \
    +doc \
    +examples \
    +shared \
    +testing

if { ![variant_isset carbon] && ![variant_isset x11] } {
    default_variants-append +cocoa
}


# --- Convenience variants

variant huge \
    requires core cocoa shared wrap database mpi boost \
    description "provide core cocoa shared wrap database mpi boost" {}

variant core \
    requires data doc examples testing \
    description "provide data doc examples testing" {}

variant wrap \
    requires java python26 tcl \
    description "provide java, python26, & tcl" {}

variant database \
    requires mysql5 pgsql84 odbc \
    description "provide all database variants" {}


# --- Build variants, alternatives to RelWithDebInfo

variant debug conflicts release description "debug build" {
    configure.args-delete \
        -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo
    configure.args-append \
        -DCMAKE_BUILD_TYPE:STRING=Debug
}

variant release conflicts debug description "release build" {
    configure.args-delete \
        -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo
    configure.args-append \
        -DCMAKE_BUILD_TYPE:STRING=Release
}

# Notes on RPATH settings for the shared dylib build and install:
#
# CMake book, Appendix A, p 234: "CMAKE_SKIP_BUILD_RPATH: Do not include RPATHs
# in the build tree.  Normally CMake uses the build tree for the RPATH when
# building executables etc. on systems that use RPATH.  When the software is
# installed the executables etc.  are relinked by CMake to have the install
# RPATH.  If this variable is set to true then the software is always built with
# no RPATH."
#
# CMake book, Appendix B, p. 301: "... SKIP_BUILD_RPATH is a boolean specifying
# whether to skip automatic generation of an rpath allowing the target to run
# from the build tree.  BUILD_WITH_INSTALL_RPATH is a boolean specifying whether
# to link the target in the build tree with the INSTALL_RPATH.  This takes
# precedence over SKIP_BUILD_RPATH and avoids the need for relinking before
# installation."
#
# Using CMAKE_SKIP_BUILD_RPATH:BOOL=OFF, we get all the executables and dylibs
# built with the $build.dir in the rpath.  For this to work, we must also have
# CMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF.  After the build, at the destroot
# phase, all the installation candidates (which excludes the examples and
# testing binaries) have their rpath settings reset to the INSTALL_RPATH (which
# should point to $prefix..., not the $destroot location).  VTK_USE_RPATH
# is required to ensure that rpath is set for all the installed executables and
# dylibs.

variant shared description "build shared libraries" {
    configure.args-delete \
        -DBUILD_SHARED_LIBS:BOOL=OFF
    configure.args-append \
        -DBUILD_SHARED_LIBS:BOOL=ON \
        -DCMAKE_SKIP_BUILD_RPATH:BOOL=OFF \
        -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF \
        -DCMAKE_INSTALL_RPATH:STRING=${prefix}/lib/${distname} \
        -DVTK_USE_RPATH:BOOL=ON
    # Additional options not required here:
    #-DCMAKE_EXE_LINKER_FLAGS:STRING=-rpath ${prefix}/lib \
    #-DVTK_REQUIRED_EXE_LINKER_FLAGS:STRING=-rpath ${prefix}/lib
}


#variant system_libs description "build with various system libs" {
#    configure.args-append \
#        -DVTK_USE_SYSTEM_EXPAT:BOOL=ON \
#        -DVTK_USE_SYSTEM_FREETYPE:BOOL=ON \
#        -DVTK_USE_SYSTEM_JPEG:BOOL=ON \
#        -DVTK_USE_SYSTEM_LIBPROJ4:BOOL=ON \
#        -DVTK_USE_SYSTEM_LIBXML2:BOOL=ON \
#        -DVTK_USE_SYSTEM_PNG:BOOL=ON \
#        -DVTK_USE_SYSTEM_TIFF:BOOL=ON \
#        -DVTK_USE_SYSTEM_ZLIB:BOOL=ON
#}


# --- Graphics build variants: carbon, cocoa, x11


variant carbon conflicts cocoa x11 description "build with Carbon" {
    configure.args-delete   \
        -DVTK_USE_CARBON:BOOL=OFF
    configure.args-append   \
        -DVTK_USE_CARBON:BOOL=ON
}


variant cocoa conflicts carbon x11 description "build with Cocoa" {
    configure.args-delete   \
        -DVTK_USE_COCOA:BOOL=OFF
    configure.args-append   \
        -DVTK_USE_COCOA:BOOL=ON
}


variant x11 conflicts cocoa carbon description "build with X11" {
    depends_lib-append    port:mesa port:Xft2 port:xorg-libXScrnSaver port:xorg-libXt
    configure.args-delete   \
        -DVTK_USE_X:BOOL=OFF
    configure.args-append   \
        -DVTK_USE_X:BOOL=ON \
        -DOPENGL_gl_LIBRARY:FILEPATH=${prefix}/lib/libGL.dylib \
        -DOPENGL_glu_LIBRARY:FILEPATH=${prefix}/lib/libGLU.dylib \
        -DOPENGL_INCLUDE_DIR:PATH=${prefix}/include \
        -DVTK_GLEXT_FILE:FILEPATH=${prefix}/include/GL/glext.h \
        -DVTK_GLXEXT_FILE:FILEPATH=${prefix}/include/GL/glxext.h \
        -DVTK_OPENGL_HAS_OSMESA:BOOL=ON
        #-DVTK_USE_MANGLED_MESA:BOOL=OFF
}


# --- "Core" variants for VTK: data, doc, examples, testing


variant data description "provide example data in: ${vtkDataPath}" {
    distfiles-append    ${vtkDataFile}
    checksums-append    ${vtkDataFile} \
                        md5 3b05eadd4446bbf3f863e744b7bee14a \
                        sha1 5030523c0a9e17c47bf94fe9005e5c91069a571a \
                        rmd160 0cc24fc7a6a9bb80c619a0555eb288cc97554001
    configure.args-append \
        -DVTK_DATA_ROOT:PATH=${vtkDataPath}
    post-destroot {
        xinstall -d -o root -g admin -m 0755 ${destroot}/${vtkDataPath}
        system "gnutar --strip-components 1 -C ${destroot}/${vtkDataPath} -zxvf ${distpath}/${vtkDataFile}"
    }
}


variant doc description "provide doxygen documentation in: ${vtkDocPath}" {
    distfiles-append    ${vtkDocFile}
    checksums-append    ${vtkDocFile} \
                        md5 a12be55a8787a6346efa9e251bce3a10 \
                        sha1 13da1373bb95f34b1f868b307f0e84fef6626020 \
                        rmd160 d6372984b74ede33713501219fecd6768edcfad3
    post-destroot {
        xinstall -d -o root -g admin -m 0755 ${destroot}/${vtkDocPath}
        xinstall -d -o root -g admin -m 0755 ${destroot}/${prefix}/share/doc
        # Add the doxygen documentation
        system "gnutar -C ${destroot}/${vtkDocPath} -zxf ${distpath}/${vtkDocFile}"
        # Add basic documentation
        file copy ${worksrcpath}/README.html   ${destroot}/${vtkDocPath}
        file copy ${worksrcpath}/Copyright.txt ${destroot}/${vtkDocPath}
        file copy ${worksrcpath}/Testing.txt   ${destroot}/${vtkDocPath}
        # Create a symlink to the $prefix path (not the destroot path); use the
        # system to do this because the $prefix path doesn't exist yet and the
        # tcl ln command fails, whereas this system command will allow it.
        system "ln -s ${vtkDocPath} ${destroot}/${prefix}/share/doc/${distname}"
    }
    # A doxygen dependency would add a lot of complexity to vtk, so
    # just use a download instead of building the docs. Otherwise:
    #depends_build-append   port:doxygen
    #configure.args-append  -DBUILD_DOCUMENTATION:BOOL=ON
}


proc resetLibLinks { inputPath } {
    # The example and testing binaries are built, but they are not
    # "installed" with the correct rpath settings.  All their rpath settings
    # point to the build.dir, so they must be reset using install_name_tool for
    # the destroot install.
    global vtkBuildBinPath vtkLibPath
    foreach f [glob ${inputPath}/*] {
        if [string equal [file extension ${f}] ".app"] {
            set exeName [file rootname [lindex [file split ${f}] end]]
            set f [format "%s/Contents/MacOS/%s" ${f} ${exeName}]
        }
        if [expr [file isfile ${f}] && [file executable ${f}]] {
            foreach dep [exec otool -L ${f}] {
                if [string match "*/libvtk*.dylib" ${dep}] {
                    set newdep [strsed ${dep} #${vtkBuildBinPath}#${vtkLibPath}#]
                    system "install_name_tool -change ${dep} ${newdep} ${f}"
                }
            }
        }
    }
}


variant examples description "provide VTK examples in: ${vtkExamplePath}" {
    configure.args-delete \
        -DBUILD_EXAMPLES:BOOL=OFF
    configure.args-append \
        -DBUILD_EXAMPLES:BOOL=ON
    post-destroot {
        xinstall -d -o root -g admin -m 0755 ${destroot}/${vtkExamplePath}/bin
        foreach f [glob ${worksrcpath}/Examples/*] {
            file copy ${f} ${destroot}/${vtkExamplePath}/
        }
        # Installations for the example binaries are in ${build.dir}/bin/.
        foreach f [glob ${vtkBuildBinPath}/*] {
            if {![string match {*.dylib} ${f}]} {
              file copy ${f} ${destroot}/${vtkExamplePath}/bin/
            }
        }
        if [variant_isset shared] {
            resetLibLinks ${destroot}/${vtkExamplePath}/bin
        }
    }
}


variant testing requires data description "provide VTK tests in: ${vtkTestingPath}" {
    configure.args-delete \
        -DBUILD_TESTING:BOOL=OFF
    configure.args-append \
        -DBUILD_TESTING:BOOL=ON
    post-destroot {
        xinstall -d -o root -g admin -m 0755 ${destroot}/${vtkTestingPath}/bin
        # Copy the testing source tree
        foreach d [exec find ${worksrcpath} -type d -name Testing] {
            regsub ${worksrcpath} ${d} ${destroot}${vtkTestingPath} testpath;
            regsub Testing ${testpath} "" testpath;
            if {![string match *Examples* ${d}]} {
                xinstall -d -o root -g admin -m 0755 ${testpath}
                file copy ${d} ${testpath}
            }
        }
        # Installations for the test binaries are in ${build.dir}/bin/.
        foreach f [glob ${vtkBuildBinPath}/*Test*] {
            if {![string match {*.dylib} ${f}]} {
              file copy ${f} ${destroot}/${vtkTestingPath}/bin/
            }
        }
        if [variant_isset shared] {
            resetLibLinks ${destroot}/${vtkTestingPath}/bin
        }
    }
}


# --- Language wrappers: java, python, tcl
#
# VTK has its own language parser, it doesn't depend on swig or cableswig (as
# of May 2009).  Since the VTK CVS repository and source distributions include
# the output of byacc and flex, there is no need for a dependency on these
# to build VTK.  If that were to change at some point, use byacc and not bison,
# i.e.: depends_build-append port:byacc port:flex


variant java description "java wrapper" {
    configure.args-delete \
        -DVTK_WRAP_JAVA:BOOL=OFF
    configure.args-append \
        -DVTK_WRAP_JAVA:BOOL=ON
    #
    # All of the following were detected automatically by cmake.  They use the
    # system java framework.  To replace them with a MacPorts java, reset
    # these variables and add the required dependencies.
    #
    #JAVA_ARCHIVE:FILEPATH=/usr/bin/jar
    #JAVA_AWT_INCLUDE_PATH:FILEPATH=/System/Library/Frameworks/JavaVM.framework/Headers
    #JAVA_AWT_LIBRARY:FILEPATH=-framework JavaVM
    #JAVA_COMPILE:FILEPATH=/usr/bin/javac
    #JAVA_INCLUDE_PATH:PATH=/System/Library/Frameworks/JavaVM.framework/Headers
    #JAVA_INCLUDE_PATH2:PATH=/System/Library/Frameworks/JavaVM.framework/Headers
    #JAVA_JVM_LIBRARY:FILEPATH=-framework JavaVM
    #JAVA_RUNTIME:FILEPATH=/usr/bin/java
    post-destroot {
        if [file exists ${destroot}${prefix}/bin/vtkWrapJava] {
            move ${destroot}${prefix}/bin/vtkWrapJava ${destroot}${prefix}/bin/vtk-${branch}-WrapJava
            ln -f -s vtk-${branch}-WrapJava ${destroot}${prefix}/bin/vtkWrapJava
        }
        if [file exists ${destroot}${prefix}/bin/vtkParseJava] {
            move ${destroot}${prefix}/bin/vtkParseJava ${destroot}${prefix}/bin/vtk-${branch}-ParseJava
            ln -f -s vtk-${branch}-ParseJava ${destroot}${prefix}/bin/vtkParseJava
        }
    }
}


# It might be possible to install vtk for both python25 and python26 (etc.), but
# that may require separate ports because the build and install process for vtk
# can only work with one at a time.  The build is a lengthy process and there
# may be, as yet, no easy way to run a post-build wrapper with a dependency on a
# prior install of vtk.  At present, if there are separate ports of vtk for
# different versions of python, each port will incur a lengthy build process.
# Moreover, it's not clear that the install process can be specifically selected
# for python only, without also installing all of vtk.  For that reason,
# it may be best to avoid separate ports of vtk for each python version.  The
# preference should be to install vtk for the latest compatible version of
# python.
#
# Hence, these variants for python25 and python26 must conflict.  At some point, the
# python25 variant could disappear.  Likewise python26 could disappear if vtk works in
# python 3.x

# Note: currently vtkpython depends on libutil and finds it in:
# // Utility library needed for vtkpython
# PYTHON_UTIL_LIBRARY:FILEPATH=/usr/lib/libutil.dylib
# I don't see a port for libutil.  If one arises, it should become a
# dependency for any python variant.
#
#// Extra libraries to link when linking to python (such as "z" for zlib).
#// Separate multiple libraries with semicolons.
#PYTHON_EXTRA_LIBS:STRING=
#

# A procedure to set or reset python variables (defaults to python26).
# This is called from within pyXY variants like this: setPython X Y
proc setPython { {major 2} {minor 6} } {
    global pyVer python pyPort pyBin pyLib pyInc pyFrame pySite
    set pyVer       ${major}.${minor}
    set python      python${pyVer}
    set pyPort      python${major}${minor}
    set pyFrame     Library/Frameworks/Python.framework/Versions/${pyVer}
    set pyLib       ${pyFrame}/Python
    #set pyLib       lib/${python}
    set pyBin       bin/${python}
    set pyInc       include/${python}
    set pySite      lib/${python}/site-packages
    if [string match "2.6" ${pyVer}] {
        # python2.6 is a framework installation, whereas installs for 2.4
        # and 2.5 contain symlinks in the framework path to the prefix path;
        # those symlinks can break the file_map stage of port activation.
        set pyBin   ${pyFrame}/bin/${python}
        set pyInc   ${pyFrame}/include/${python}
        set pySite  ${pyFrame}/lib/${python}/site-packages
    }
}
# Initialize the global variables for python26
setPython

proc pyPostDestroot { {major 2} {minor 6} } {
    global pyVer python pyPort pyBin pyLib pyInc pyFrame pySite
    global branch destroot prefix
    global vtkBuildBinPath vtkLibPath
    setPython $major $minor
    # Reset the name of the vtkpython binaries; create symlinks to the
    # version specific binaries, in case other apps look for them.
    if [file exists ${destroot}${prefix}/bin/vtkpython] {
        move ${destroot}${prefix}/bin/vtkpython ${destroot}${prefix}/bin/vtk-${branch}-${pyPort}
        ln -f -s vtk-${branch}-${pyPort} ${destroot}${prefix}/bin/vtkpython
    }
    if [file exists ${destroot}${prefix}/bin/pvtkpython] {
        move ${destroot}${prefix}/bin/pvtkpython ${destroot}${prefix}/bin/pvtk-${branch}-${pyPort}
        ln -f -s pvtk-${branch}-${pyPort} ${destroot}${prefix}/bin/pvtkpython
    }
    if [file exists ${destroot}${prefix}/bin/vtkWrapPython] {
        move ${destroot}${prefix}/bin/vtkWrapPython ${destroot}${prefix}/bin/vtk-${branch}-WrapPython${pyVer}
        ln -f -s vtk-${branch}-WrapPython${pyVer} ${destroot}${prefix}/bin/vtkWrapPython
    }
    if [file exists ${destroot}${prefix}/bin/vtkWrapPythonInit] {
        move ${destroot}${prefix}/bin/vtkWrapPythonInit ${destroot}${prefix}/bin/vtk-${branch}-WrapPython${pyVer}Init
        ln -f -s vtk-${branch}-WrapPython${pyVer}Init ${destroot}${prefix}/bin/vtkWrapPythonInit
    }
    ################################################################################
    # Is it possible to change the python site-package name and have it all work OK?
    # from:  .../lib/python2.5/site-packages/vtk
    # to:    .../lib/python2.5/site-packages/vtk-5.4
    ################################################################################
    # Reset the RPATH for all the python .so files
    set vtkSitePackage ${destroot}${prefix}/${pySite}/vtk
    foreach f [glob ${vtkSitePackage}/*.so] {
        foreach dep [exec otool -L ${f}] {
            if [string match "*libvtk*.dylib" ${dep}] {
                set newdep [strsed ${dep} #${vtkBuildBinPath}#${vtkLibPath}#]
                system "install_name_tool -change ${dep} ${newdep} ${f}"
            }
        }
    }
}

variant python25 conflicts python26 requires shared description "python 2.5 wrapper" {
    # Note that python25 is NOT installed as a framework (May 2009)
    setPython 2 5
    depends_lib-append \
        port:${pyPort}
    configure.args-delete \
        -DVTK_WRAP_PYTHON:BOOL=OFF
    configure.args-append \
        -DVTK_WRAP_PYTHON:BOOL=ON \
        -DVTK_NO_PYTHON_THREADS:BOOL=OFF \
        -DPYTHON_INCLUDE_PATH:FILEPATH=${prefix}/${pyInc} \
        -DPYTHON_LIBRARY:FILEPATH=${prefix}/${pyLib} \
        -DPYTHON_DEBUG_LIBRARY:FILEPATH=${prefix}/${pyLib} \
        -DPYTHON_EXECUTABLE:FILEPATH=${prefix}/${pyBin} \
        -DVTK_PYTHON_SETUP_ARGS:STRING='--prefix=${prefix} --root=${destroot}'
        # The VTK_PYTHON_SETUP_ARGS *MUST* be in single quotes
    post-destroot {
        pyPostDestroot 2 5
    }
}

variant python26 conflicts python25 requires shared description "python 2.6 wrapper" {
    # Note that python26 is installed as a framework (May 2009)
    setPython 2 6
    depends_lib-append \
        port:${pyPort}
    configure.args-delete \
        -DVTK_WRAP_PYTHON:BOOL=OFF
    configure.args-append \
        -DVTK_WRAP_PYTHON:BOOL=ON \
        -DVTK_NO_PYTHON_THREADS:BOOL=OFF \
        -DPYTHON_INCLUDE_PATH:FILEPATH=${prefix}/${pyInc} \
        -DPYTHON_LIBRARY:FILEPATH=${prefix}/${pyLib} \
        -DPYTHON_DEBUG_LIBRARY:FILEPATH=${prefix}/${pyLib} \
        -DPYTHON_EXECUTABLE:FILEPATH=${prefix}/${pyBin} \
        -DVTK_PYTHON_SETUP_ARGS:STRING='--prefix=${prefix}/${pyFrame} --root=${destroot}'
    #configure.args-append \
        -DPYTHON_INCLUDE_PATH:FILEPATH=${prefix}/${pyframe}/Headers \
        -DPYTHON_LIBRARY:FILEPATH=${prefix}/lib/lib${python}.dylib \
        -DPYTHON_DEBUG_LIBRARY:FILEPATH=${prefix}/lib/lib${python}.dylib \
        -DPYTHON_EXECUTABLE:FILEPATH=${prefix}/bin/${python} \
        -DVTK_PYTHON_SETUP_ARGS:STRING='--prefix=${prefix}/${pyframe} --root=${destroot}'
        # For 2.6, it needs to be installed into ${pyframe}/lib/${python}/site-packages
        # The VTK_PYTHON_SETUP_ARGS *MUST* be in single quotes
    post-destroot {
        pyPostDestroot 2 6
    }
}


proc tclPostDestroot {} {
    global branch destroot prefix
    # Reset the name of the vtk tcl binaries; create symlinks to the
    # version specific installation, in case other apps look for the binaries by
    # a default name; these symlinks might conflict across vtk versions, may
    # require a vtk-select port to set them if multiple vtk versions are
    # installed.
    if [file exists ${destroot}${prefix}/bin/vtk] {
        move ${destroot}${prefix}/bin/vtk ${destroot}${prefix}/bin/vtk-${branch}-tcl
        ln -f -s vtk-${branch}-tcl ${destroot}${prefix}/bin/vtk
    }
    if [file exists ${destroot}${prefix}/bin/pvtk] {
        move ${destroot}${prefix}/bin/pvtk ${destroot}${prefix}/bin/pvtk-${branch}-tcl
        ln -f -s pvtk-${branch}-tcl ${destroot}${prefix}/bin/pvtk
    }
    if [file exists ${destroot}${prefix}/bin/vtkWrapTcl] {
        move ${destroot}${prefix}/bin/vtkWrapTcl ${destroot}${prefix}/bin/vtk-${branch}-WrapTcl
        ln -f -s vtk-${branch}-WrapTcl ${destroot}${prefix}/bin/vtkWrapTcl
    }
    if [file exists ${destroot}${prefix}/bin/vtkWrapTclInit] {
        move ${destroot}${prefix}/bin/vtkWrapTclInit ${destroot}${prefix}/bin/vtk-${branch}-WrapTclInit
        ln -f -s vtk-${branch}-WrapTclInit ${destroot}${prefix}/bin/vtkWrapTclInit
    }
}

variant tcl conflicts tcl_apple description "tcl wrapper (MacPorts tcl/tk)" {
    depends_lib-append \
        port:tcl \
        port:tk
    configure.args-delete \
        -DVTK_USE_TK:BOOL=OFF \
        -DVTK_WRAP_TCL:BOOL=OFF
    configure.args-append \
        -DVTK_USE_TK:BOOL=ON \
        -DVTK_WRAP_TCL:BOOL=ON \
        -DTCL_TCLSH:FILEPATH=${prefix}/bin/tclsh \
        -DTCL_INCLUDE_PATH:PATH=${prefix}/include \
        -DTCL_LIBRARY:FILEPATH=${prefix}/lib/libtcl.dylib \
        -DTK_INCLUDE_PATH:PATH=${prefix}/include \
        -DTK_LIBRARY:FILEPATH=${prefix}/lib/libtk.dylib
        # VTK_TCL_TK_STATIC is not specified here, it's OFF by default.
    post-destroot {
        tclPostDestroot
    }
}

variant tcl_apple conflicts tcl description "tcl wrapper (Apple tcl/tk framework)" {
    configure.args-delete \
        -DVTK_USE_TK:BOOL=OFF \
        -DVTK_WRAP_TCL:BOOL=OFF
    configure.args-append \
        -DVTK_USE_TK:BOOL=ON \
        -DVTK_WRAP_TCL:BOOL=ON \
        -DTCL_TCLSH:FILEPATH=/usr/bin/tclsh \
        -DTCL_INCLUDE_PATH:PATH=/System/Library/Frameworks/Tcl.framework/Headers \
        -DTCL_LIBRARY:FILEPATH=/System/Library/Frameworks/Tcl.framework \
        -DTK_INCLUDE_PATH:PATH=/System/Library/Frameworks/Tk.framework/Headers \
        -DTK_LIBRARY:FILEPATH=/System/Library/Frameworks/Tk.framework
    post-destroot {
        tclPostDestroot
    }
}


# --- Database variants


variant mysql5 description "build the MySQL driver for vtkSQLDatabase" {
    depends_lib-append \
        path:bin/mysql_config5:mysql5 \
        port:zlib
    pre-configure {
        # Find the directory containing mysql.h
        set includePath [exec ${prefix}/bin/mysql_config5 --include]
        set includePath [string trim ${includePath} -I ]
        #if [ exec find $includePath -name "mysql.h" ]
        set libs [exec ${prefix}/bin/mysql_config5 --libs]
        set libIdx [lsearch -regex $libs "${prefix}/lib/mysql"]
        set libPath [lindex $libs $libIdx]
        set libPath [string trim ${libPath} -L ]
        set libFile [exec find ${libPath} -name "libmysqlclient.dylib"]
        configure.args-append \
            -DVTK_USE_MYSQL:BOOL=ON \
            -DMYSQL_INCLUDE_DIRECTORIES:PATH=${includePath} \
            -DMYSQL_LIBRARY:FILEPATH=${libFile} \
            -DMYSQL_EXTRA_LIBRARIES:FILEPATH=${prefix}/lib/libz.dylib
            # A URL for a MySQL server of the form
            # mysql://[[username[:password]@]hostname[:port]]/[dbname]
            #VTK_MYSQL_TEST_URL:STRING=
    }
}


variant pgsql83 conflicts pgsql84 description "build the PostgreSQL 8.3 driver for vtkSQLDatabase" {
    depends_lib-append \
        path:lib/postgresql83/bin/pg_config:postgresql83
    pre-configure {
        set pgconf [exec ${prefix}/lib/postgresql83/bin/pg_config ]
        set includeIdx [lsearch -regex ${pgconf} "^INCLUDEDIR"]
        set includePath [lindex ${pgconf} [expr ${includeIdx} + 2]]
        set libIdx [lsearch -regex $pgconf "^LIBDIR"]
        set libPath [lindex $pgconf [expr $libIdx + 2]]
        set libFile [exec find ${libPath} -name "libpq.dylib"]
        configure.args-append \
            -DVTK_USE_POSTGRES:BOOL=ON \
            -DPOSTGRES_INCLUDE_DIRECTORIES:PATH=${includePath} \
            -DPOSTGRES_LIBRARIES:FILEPATH=${libFile}
            #// A URL for a PostgreSQL server of the form
            #// psql://[[username[:password]@]hostname[:port]]/[dbname]
            #VTK_PSQL_TEST_URL:STRING=
    }
}


variant pgsql84 conflicts pgsql83 description "build the PostgreSQL 8.4 driver for vtkSQLDatabase" {
    depends_lib-append \
        path:lib/postgresql84/bin/pg_config:postgresql84
    pre-configure {
        set pgconf [exec ${prefix}/lib/postgresql84/bin/pg_config ]
        set includeIdx [lsearch -regex ${pgconf} "^INCLUDEDIR"]
        set includePath [lindex ${pgconf} [expr ${includeIdx} + 2]]
        set libIdx [lsearch -regex $pgconf "^LIBDIR"]
        set libPath [lindex $pgconf [expr $libIdx + 2]]
        set libFile [exec find ${libPath} -name "libpq.dylib"]
        configure.args-append \
            -DVTK_USE_POSTGRES:BOOL=ON \
            -DPOSTGRES_INCLUDE_DIRECTORIES:PATH=${includePath} \
            -DPOSTGRES_LIBRARIES:FILEPATH=${libFile}
            #// A URL for a PostgreSQL server of the form
            #// psql://[[username[:password]@]hostname[:port]]/[dbname]
            #VTK_PSQL_TEST_URL:STRING=
    }
}


variant odbc description "build the ODBC database interface" {
    depends_lib-append \
        port:unixODBC
    configure.args-append \
        -DVTK_USE_ODBC:BOOL=ON \
        -DODBC_INCLUDE_DIRECTORIES:PATH=${prefix}/include \
        -DODBC_LIBRARY:FILEPATH=${prefix}/lib/libodbc.dylib
        #// A data source name (DSN) for an ODBC database connection to use for testing.
        #-DVTK_ODBC_TEST_DSN:STRING=
}


# --- Miscellaneous variants


variant mpi description "use message passing interface (mpich2) for parallel support" {
    set nCPU [expr [exec sysctl -n hw.logicalcpu_max]]
    depends_lib-append \
        port:mpich2
    configure.args-append \
        -DVTK_USE_MPI:BOOL=ON \
        -DMPI_COMPILER:FILEPATH=${prefix}/bin/mpicxx \
        -DMPIEXEC:FILEPATH=${prefix}/bin/mpiexec \
        -DMPIEXEC_MAX_NUMPROCS:STRING=${nCPU}
        # If the MPI_LIBRARY and-or MPI_INCLUDE_PATH are set, then the
        # FindMPI module will not determine all the cmake variables
        # automatically (don't set them). It should be sufficient to set the
        # MPI_COMPILER and leave the rest to the FindMPI module, see:
        # ${prefix}/share/cmake-2.6/Modules/FindMPI.cmake
}


variant boost description "use Boost libraries - www.boost.org" {
    depends_lib-append \
        port:boost
    configure.args-append \
        -DVTK_USE_BOOST:BOOL=ON
        # All determined automatically:
        #Boost_INCLUDE_DIR:PATH=${prefix}/include
        #Boost_LIBRARY_DIRS:FILEPATH=${prefix}/lib
        #Boost_USE_MULTITHREADED:BOOL=ON
}




#variant qvtk "build QVTK widget and plugin for Qt" {
#    # Dependencies?
#    configure.args-append \
#        -DVTK_USE_QVTK:BOOL=ON
#}


#variant cg description "build pixel and vertex shader support for Cg" {
#    # Require a new port for cg_toolkit, see downloads at
#    # http://developer.download.nvidia.com/cg/Cg_2.2/Cg-2.2_April2009.dmg
#    depends_build-append \
#        port:cg_toolkit
#    configure.args-append \
#        -DVTK_USE_CG_SHADERS:BOOL=ON
#}


