# -*- 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$

PortSystem 1.0
PortGroup        github 1.0
PortGroup        cmake 1.0
PortGroup        conflicts_build 1.0
PortGroup        qt4 1.0
PortGroup       compiler_blacklist_versions 1.0
# gcc-4.8 cannot compile universal
# The internal yaml-cpp is not compiled with multiple archs in universal mode
#PortGroup        muniversal 1.0

epoch        	 1

fetch.type              git
github.setup     OpenImageIO oiio 1.6.18 tags/Release-
#github.version      ${version}-dist
#github.tarball_from downloads

name             openimageio16
conflicts        openimageio17
conflicts        openimageio18
conflicts        openimageio20
conflicts        openimageio
#version          1.4.5.91
#revision         1
categories       graphics
platforms           darwin
license          BSD
maintainers      openmaintainer
description      OpenImageIO is a library for reading and writing images
long_description ${description}
homepage         http://sites.google.com/site/openimageio/


#master_sites     debian:o/openimageio/
#worksrcdir       ${name}-${version}~dfsg0.orig
#distfiles           ${name}_${version}~dfsg0.orig${extract.suffix}
#checksums           rmd160  94c6e2274be01e82cfe8bc4bf11677593d33f299 \
#                    sha256  4cf3b373e56694ce9e3896a356b68a2ed92978e76be653a5b5e45b17937faae8

depends_lib      port:boost path:lib/libjpeg.dylib:jpeg port:tiff port:libpng port:openexr port:glew port:ilmbase port:libraw port:giflib port:webp

# can't compile openimageio when another version is installed, because the include path is wrong
conflicts_build  ${name}

depends_build    port:txt2man
depends_skip_archcheck txt2man
# port:opencolorio

# we need gcc atomics (clang >= 3.5 maybe?, gcc >= 4.8 works)
# tested with openimageio 1.4.6rc1:
# macports-clang-3.3 and later work
# gcc-4.2 doesn't work
# llvm-gcc-4.2 works
# macports-clang < 3.3 untested
# clang < 300 doesn't work
# see also https://github.com/OpenImageIO/oiio/issues/1363
#compiler.blacklist-append {*gcc-3*} {*gcc-4.[0-1]} {macports-gcc-4.[2-7]} {gcc-4.[2-7]} {apple-gcc-4.[2-7]} cc {clang < 300}

# The PowerPC build requires GCC >= 4.8, which doesn't recognize the -arch flag, and requires libatomic
# Unfortunately, the resulting binaries don't work (at least on PPC), so we build without threads
if {${configure.build_arch} eq "ppc" || ${configure.build_arch} eq "ppc64"} {
    # there is no known working clang compiler for ppc
    compiler.blacklist-append {macports-clang*}
#    compiler.blacklist-append macports-gcc-4.3 macports-gcc-4.4 macports-gcc-4.5 macports-gcc-4.6 macports-gcc-4.7
#    # the following removes the flag "-arch ppc" put by cmake-1.0 portgroup in the pre-configure phase
#    pre-configure {
#        configure.args-delete -DCMAKE_OSX_ARCHITECTURES="${configure.build_arch}"
#    }
#    ## -latomic is necessary on ppc for __atomic_fetch_add_8 and __atomic_exchange_8
#    configure.args-append -DCMAKE_SHARED_LINKER_FLAGS=-latomic -DCMAKE_EXE_LINKER_FLAGS=-latomic
    # env PATH=/opt/local/libexec/qt4/bin:$PATH cmake .. -DNOTHREADS=ON -DCMAKE_BUILD_TYPE=Debug -DSTOP_ON_WARNING=OFF
    #configure.args-append -DNOTHREADS=ON -DSTOP_ON_WARNING=OFF
    ## uncomment the following to compile with buggy thread support
    # env PATH=/opt/local/libexec/qt4/bin:$PATH cmake .. -DCMAKE_C_COMPILER=gcc-mp-4.9 -DCMAKE_CXX_COMPILER=g++-mp-4.9 -DCMAKE_SHARED_LINKER_FLAGS=-latomic -DCMAKE_EXE_LINKER_FLAGS=-latomic -DCMAKE_BUILD_TYPE=Debug -DSTOP_ON_WARNING=OFF
    ## -latomic is necessary on ppc for __atomic_fetch_add_8 and __atomic_exchange_8
    #configure.args-append -DCMAKE_SHARED_LINKER_FLAGS=-latomic -DCMAKE_EXE_LINKER_FLAGS=-latomic
}

#post-extract {
#    system "touch ${worksrcpath}/LICENSE  ${workpath}/${worksrcdir}/LICENSE"
#}

# Those are Debian patches, which require -p1
#patch.pre_args	-p1
#patchfiles \
#     build_in_sources.patch
#    0001-Fix_docs_generation.patch \
#    0002-Fix_IlmBase_issue.patch \
#    0003-Fix_multiarch_paths.patch \
#    0004-Fix_FTBFS_on_atomic_operations.patch \

patch.pre_args	-p1
patchfiles \
    oiio-1.5.23-checkmaxmem.patch \
    oiio-1.5.23-invalidatespec.patch \
    oiio-1.6.18-fix-safe_tiffgetfield.patch \
    oiio-1.6.18-fix_read_tiles_prefix_bytes.patch \
    oiio-1.6.18-libraw.patch \
    oiio-1.6.18-tiffposition.patch \
    oiio-1.6.18-ppcatomics.patch \
    oiio-nothreads.patch

cmake.out_of_source yes

configure.args-append \
    -DOIIO_BUILD_TESTS:BOOL=FALSE \
    -DBOOST_ROOT=${prefix} \
    -DUSE_QT:BOOL=FALSE \
    -DUSE_PYTHON:BOOL=FALSE \
    -DUSE_PYTHON3:BOOL=FALSE \
    -DUSE_FIELD3D:BOOL=FALSE \
    -DUSE_FFMPEG:BOOL=FALSE \
    -DUSE_OPENJPEG:BOOL=FALSE \
    -DUSE_OCIO:BOOL=FALSE \
    -DUSE_OPENCV:BOOL=FALSE \
    -DUSE_OPENSSL:BOOL=FALSE \
    -DUSE_FREETYPE:BOOL=FALSE \
    -DUSE_GIF:BOOL=TRUE \
    -DUSE_PTEX:BOOL=FALSE \
    -DUSE_LIBRAW:BOOL=TRUE \
    -DUSE_NUKE:BOOL=FALSE \
    -DSTOP_ON_WARNING:BOOL=FALSE

default_variants    +opencolorio +openjpeg +qt4 +freetype

variant natron conflicts ffmpeg requires opencolorio openjpeg qt4 freetype description {Natron version} {
    #if {${configure.cxx_stdlib} eq "libstdc++"} {
    #    # configure with GCC 4.9 for proper GCC intrinsics support
    #    configure.compiler macports-gcc-4.9
    #}
}

variant opencolorio description {Add OpenColorIO support.} {
    depends_lib-append  port:opencolorio
    configure.args-delete       -DUSE_OCIO:BOOL=FALSE
    configure.args-append       -DUSE_OCIO:BOOL=TRUE
}

variant ffmpeg description {Add FFmpeg support.} {
    depends_lib-append  port:ffmpeg
    configure.args-delete       -DUSE_FFMPEG:BOOL=FALSE
    configure.args-append       -DUSE_FFMPEG:BOOL=TRUE
}

variant openjpeg description {Add OpenJPEG support.} {
    depends_lib-append  port:openjpeg
    configure.args-delete       -DUSE_OPENJPEG:BOOL=FALSE
    configure.args-append       -DUSE_OPENJPEG:BOOL=TRUE -DOPENJPEG_HOME=${prefix} -DOPENJPEG_INCLUDE_DIR=`pkg-config --variable=includedir libopenjp2`
}

variant python26 conflicts python27 description {Add Python 2.6 bindings.} {
    depends_lib-append          port:python26
    configure.args-delete       -DUSE_PYTHON:BOOL=FALSE
    configure.args-append       -DUSE_PYTHON:BOOL=TRUE \
                                -DPYTHON_VERSION=2.6 \
                                -DPYTHON_EXECUTABLE=${prefix}/bin/python2.6 \
                                -DPYTHON_LIBRARY=${prefix}/lib/libpython2.6.dylib \
                                -DPYTHON_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/2.6/Headers \
                                -DPYTHON_PACKAGES_PATH=${frameworks_dir}/Python.framework/Versions/2.6/lib/python2.6/site-packages
}

variant python27 conflicts python26 description {Add Python 2.7 bindings.} {
    depends_lib-append          port:python27
    configure.args-delete       -DUSE_PYTHON:BOOL=FALSE
    configure.args-append       -DUSE_PYTHON:BOOL=TRUE \
                                -DPYTHON_VERSION=2.7 \
                                -DPYTHON_EXECUTABLE=${prefix}/bin/python2.7 \
                                -DPYTHON_LIBRARY=${prefix}/lib/libpython2.7.dylib \
                                -DPYTHON_INCLUDE_DIR=${frameworks_dir}/Python.framework/Versions/2.7/Headers \
                                -DPYTHON_PACKAGES_PATH=${frameworks_dir}/Python.framework/Versions/2.7/lib/python2.7/site-packages
}

variant qt4 description {Build the Qt-based image viewer.} {
    configure.args-delete -DUSE_QT:BOOL=FALSE
    configure.args-append -DUSE_QT:BOOL=TRUE
    depends_lib-append port:qt4-mac
}

variant field3d description {Use Field3D.} {
    configure.args-delete -DUSE_FIELD3D:BOOL=FALSE
    configure.args-append -DUSE_FIELD3D:BOOL=TRUE
    depends_lib-append port:field3d
}

variant freetype description {Use FreeType.} {
    configure.args-delete -DUSE_FREETYPE:BOOL=FALSE
    configure.args-append -DUSE_FREETYPE:BOOL=TRUE -DFREETYPE_INCLUDE_DIRS=${prefix}/include/freetype2
    depends_lib-append port:freetype
}

