# -*- 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.8.13 tags/Release-
#github.version      ${version}-dist
#github.tarball_from downloads

name             openimageio18
conflicts        openimageio16
conflicts        openimageio17
conflicts        openimageio20
conflicts        openimageio
#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 doesn't work
# 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}

# for some reason, if we compile oiio with gcc 4.9 and boost with gcc 4.2, idiff a.png b.png crashes
if {${configure.cxx_stdlib} eq "libstdc++"} {
    if {[vercmp $xcodeversion 3.0] >= 0} {
        compiler.fallback-append macports-clang-3.4
    } else {
        compiler.fallback-append macports-clang-3.3
    }
}

# GCC < 4.9 does not have proper GCC intrinsics support
compiler.blacklist-append {*gcc-4.8}
compiler.whitelist-append macports-clang-8.0 macports-clang-7.0 macports-clang-6.0 macports-clang-5.0 macports-clang-4.0 macports-gcc-7 macports-gcc-6 macports-gcc-5 {*gcc-4.9}

# The PowerPC build requires GCC >= 4.9, 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*}
#    # 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"
#}

# As of 1.7.17, there is only one debian patch:
# 0001-Fix_documentation_generation.patch
# see https://packages.debian.org/source/sid/openimageio

patch.pre_args	-p1
patchfiles \
    0003-boost-latest.patch \
    oiio-1.5.23-invalidatespec.patch \
    oiio-1.8.7-c99math.patch \
    oiio-1.8.7-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++"} {
    }
}

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
}

#configure.args   --enable-bsdtar=shared --enable-bsdcpio=shared \
#                 --without-lzmadec --without-lzma
#use_autoreconf	yes
