# -*- 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
PortGroup               compiler_blacklist_versions 1.0
PortGroup               boost 1.0

github.setup            OpenImageIO oiio 2.2.18.0
revision                100
name                openimageio22
conflicts           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  ff8376f038694bf7e37a81de1351a46162780a10 \
                    sha256  b8dd189fbc97f1b172528e324daa904f74a166bad62c32e7166ca6c866734a29 \
                    size    29246567

compiler.cxx_standard   2011
# error: multiple overloads of 'address' instantiate to the same signature 'const_pointer (const_reference) const noexcept'
# http://lists.llvm.org/pipermail/llvm-bugs/2013-November/031552.html
# Seen on OSX 10.9 and older.
compiler.blacklist-append {clang < 700}

compiler.thread_local_storage yes

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

# 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 \
                        -DBUILD_MISSING_FMT=OFF

depends_lib-append      port:robin-map \
                        port:zlib \
                        port:bzip2 \
                        path:include/turbojpeg.h:libjpeg-turbo \
                        port:libpng \
                        port:tiff \
                        port:ilmbase \
                        port:openexr \
                        port:squish

# optional components

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

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

# 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 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.} {
    # OpenVDB header files use C++14 features
    configure.args-append   -DCMAKE_CXX_STANDARD=14
    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
}

configure.args-append   -DUSE_PYTHON=OFF

set pythons_suffixes    {27 36 37 38}

set pythons_ports       {}
foreach s ${pythons_suffixes} {
    lappend pythons_ports python${s}
}

foreach s ${pythons_suffixes} {
    set p python${s}
    set v [string index ${s} 0].[string index ${s} 1]
    set i [lsearch -exact ${pythons_ports} ${p}]
    set c [lreplace ${pythons_ports} ${i} ${i}]
    variant ${p} description "Build the Python ${v} bindings" conflicts {*}${c} "
        depends_build-append    port:pybind11
        depends_lib-append      port:${p} \
                                port:py${s}-numpy

        configure.args-delete   -DUSE_PYTHON=OFF

        configure.args-append   -DPYTHON_VERSION=${v} \
                                -DPYTHON_EXECUTABLE=${prefix}/bin/python${v} \
                                -DPYTHON_SITE_DIR=${frameworks_dir}/Python.framework/Versions/${v}/lib/python${v}
    "
}

set set_python_default  yes
foreach s ${pythons_suffixes} {
    if {[variant_isset python${s}]} {
        set set_python_default  no
    }
}
# this default version should stay synchronized with python_get_default_version
#    in the python PortGroup
#if {${set_python_default}} {
#    default_variants        +python38
#}

#configure.args-append       -DUSE_FIELD3D=OFF
#depends_lib-append          port:field3d

#configure.args-append       -DUSE_FFMPEG=OFF
#depends_lib-append          port:ffmpeg

# conflicts with jpeg
#configure.args-append       -DUSE_JPEGTURBO=OFF
#depends_lib-append          port:libjpeg-turbo

#configure.args-append       -DUSE_OPENJPEG=OFF
#depends_lib-append          port:openjpeg

#configure.args-append       -DUSE_OCIO=OFF
#depends_lib-append          port:opencolorio

#configure.args-append       -DUSE_OPENCV=OFF
#depends_lib-append          port:opencv

#configure.args-append       -DUSE_FREETYPE=OFF
#depends_lib-append          port:freetype

#configure.args-append       -DUSE_GIF=OFF
depends_lib-append          port:giflib

#configure.args-append       -DUSE_HEIF=OFF
depends_lib-append          port:libheif

#configure.args-append       -DUSE_PTEX=OFF
#depends_lib-append          port:ptex

#configure.args-append       -DUSE_LIBRAW=OFF
depends_lib-append          port:libraw

#configure.args-append       -DUSE_TBB=OFF \
#                            -DUSE_OPENVDB=OFF
#depends_lib-append          port:tbb \
#                            port:openvdb

# not in MacPorts
configure.args-append       -DUSE_NUKE=OFF

#configure.args-append       -DUSE_DICOM=OFF
#depends_lib-append          port:dcmtk

#configure.args-append       -DUSE_WEBP=OFF
depends_lib-append          port:webp

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