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

PortSystem          1.0
PortGroup           github 1.0
#PortGroup           qt5 1.0
PortGroup           cmake 1.1
PortGroup           active_variants 1.1

github.setup        OpenImageIO oiio 2.0.13
#revision            1
name                openimageio20
conflicts           openimageio18
conflicts           openimageio16
conflicts           openimageio17
conflicts           openimageio
categories          graphics
license             BSD
maintainers         {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description         a library for reading and writing images
long_description    OpenImageIO is a library for reading and writing images, and a bunch of related classes, utilities, and applications.
platforms           darwin

checksums           rmd160  4112d18405d9916bf352eaf8e809e5ea54f617a8 \
                    sha256  83d9f9b3923a1ceda838fd7f41a136129886dd7973f40953fd7fb376fad949cc \
                    size    29845724

github.master_sites ${github.homepage}/archive
distname            Release-${version}
worksrcdir          oiio-Release-${version}

patch.pre_args  -p1
patchfiles \
    oiio-2.0.8-invalidatespec.patch \
    oiio-2.0.8-atomic-load.patch \
    oiio-2.0.13-exifread.patch

# avoid
#    ccache: error: Failed to create directory ${prefix}/var/macports/build/.ccache/tmp: Operation not permitted
configure.args-append \
    -DUSE_CCACHE=OFF

# do not "Try to download and build any missing dependencies"
configure.args-append \
    -DBUILD_MISSING_DEPS=OFF

depends_lib-append \
    port:robin-map \
    port:zlib \
    port:bzip2 \
    port:boost \
    path:lib/libjpeg.dylib:jpeg \
    port:libpng \
    port:tiff \
    port:ilmbase \
    port:giflib \
    port:libraw \
    port:webp \
    port:openexr

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 \
    -DUSE_OPENGL:BOOL=FALSE \
    -DUSE_WEBP:BOOL=TRUE \
    -DSTOP_ON_WARNING:BOOL=FALSE

compiler.cxx_standard 2011

# 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
}

# optional components

default_variants    +opencolorio +openjpeg +freetype

variant natron conflicts ffmpeg qt5 dicom openvdb ptex dicom requires opencolorio openjpeg 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 python27 description {Build the Python 2.7 bindings} conflicts python36 python37 {
    depends_lib-append \
        port:python27 \
        port:py27-numpy
    configure.args-append \
        -DPYTHON_VERSION=2.7 \
        -DPYTHON_EXECUTABLE=${prefix}/bin/python2.7 \
        -DPYTHON_SITE_DIR=${frameworks_dir}/Python.framework/Versions/2.7/lib/python2.7
}

variant python36 description {Build the Python 3.6 bindings} conflicts python27 python37 {
    depends_lib-append \
        port:python36 \
        port:py36-numpy
    configure.args-append \
        -DPYTHON_VERSION=3.6 \
        -DPYTHON_EXECUTABLE=${prefix}/bin/python3.6 \
        -DPYTHON_SITE_DIR=${frameworks_dir}/Python.framework/Versions/3.6/lib/python3.6
}

variant python37 description {Build the Python 3.7 bindings} conflicts python27 python36 {
    depends_lib-append \
        port:python37 \
        port:py37-numpy
    configure.args-append \
        -DPYTHON_VERSION=3.7 \
        -DPYTHON_EXECUTABLE=${prefix}/bin/python3.7 \
        -DPYTHON_SITE_DIR=${frameworks_dir}/Python.framework/Versions/3.7/lib/python3.7
}

#if {![variant_isset python36] && ![variant_isset python37]} {
#    default_variants +python27
#}

if {![variant_isset python27] && ![variant_isset python36] && ![variant_isset python37]} {
    configure.args-append \
        -DUSE_PYTHON=OFF
} else {
    depends_build-append \
        port:pybind11
}


variant qt5 description {Build the Qt-based image viewer.} {
#    qt5.depends_component qtbase
#    qt5.min_version       5.6
    configure.args-delete -DUSE_QT:BOOL=FALSE
    configure.args-append -DUSE_QT:BOOL=TRUE
    configure.args-delete -DUSE_OPENGL:BOOL=FALSE
    configure.args-append -DUSE_OPENGL: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
}


variant opencv description {Use OpenCV.} {
    configure.args-delete -DUSE_OPENCV:BOOL=FALSE
    configure.args-append -DUSE_OPENCV:BOOL=TRUE
    depends_lib-append port:opencv
}

variant ptex description {Use PTex.} {
    configure.args-delete -DUSE_PTEX:BOOL=FALSE
    configure.args-append -DUSE_PTEX:BOOL=TRUE
    depends_lib-append port:ptex
}

variant openvdb description {Use OpenVDB.} {
    configure.args-delete -DUSE_TBB:BOOL=FALSE
    configure.args-append -DUSE_TBB:BOOL=TRUE
    configure.args-delete -DUSE_OPENVDB:BOOL=FALSE
    configure.args-append -DUSE_OPENVDB:BOOL=TRUE
    depends_lib-append port:tbb \
                       port:openvdb
}

variant dicom description {Use DICOM from dcmtk.} {
    configure.args-delete -DUSE_DICOM:BOOL=FALSE
    configure.args-append -DUSE_DICOM:BOOL=TRUE
    depends_lib-append port:dcmtk
}

livecheck.regex     archive/Release-(\\d+(?:\\.\\d+)*)${extract.suffix}
