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

name                    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.

# Keep this condition the same in the opencolorio, openimageio, and osl ports.
set port_latest [expr {${os.platform} ne "darwin" || ${os.major} >= 20}]

if {${port_latest}} {
    github.setup        AcademySoftwareFoundation OpenImageIO 2.5.10.1 v
    revision            0
    checksums           rmd160  acca3794870850f6d39e41e62e9370bf6ff9565b \
                        sha256  8f6a547f6a5d510737ba436f867043db537def65f0fdb14ec30e5a185b619f93 \
                        size    52061055
} else {
    github.setup        AcademySoftwareFoundation OpenImageIO 2.1.20.0 v
    revision            14
    checksums           rmd160  d10c488b93ab2335d53545d8a1b35ba4c1babb98 \
                        sha256  75222543286d3a12473aa03fdb4e6c9f98760c5ad1ad89d3cf82a5da41385ae0 \
                        size    29115990

    livecheck.type      none
}

github.tarball_from     archive

if {${port_latest}} {
    depends_lib-append  port:imath \
                        port:openexr \
                        port:onetbb
    #configure.args-append       -DUSE_TBB=OFF
    configure.args-append       -DTBB_ROOT=${prefix}/libexec/onetbb
} else {
    patchfiles-append   patch-FindOpenJpeg.cmake.diff
    patchfiles-append   patch-libraw-0.21.0.diff
    patchfiles-append   patch-2.1.20-cmake-disable-wall-werror.diff

    depends_lib-append  port:field3d \
                        port:openexr2 \
                        port:tbb

    configure.pkg_config_path-prepend \
                        ${prefix}/libexec/openexr2/lib/pkgconfig
    #configure.args-append       -DUSE_TBB=OFF
    configure.args-append       -DTBB_ROOT=${prefix}/libexec/tbb
}

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

# OpenVDB header files use C++17 features
compiler.cxx_standard   2017
configure.args-append   -DCMAKE_CXX_STANDARD=17

compiler.thread_local_storage yes

configure.cxxflags-append \
                        -Wno-deprecated-declarations \
                        -Wno-error=unknown-warning-option \
                        -Wno-unknown-warning-option

# NOTE: Ensure we prepend 'libfmt9', to include it before 'openexr2'
set port_libfmt         libfmt9
cmake.module_path-prepend \
                        ${prefix}/lib/${port_libfmt}/cmake
configure.args-append   -DFMT_INCLUDE_DIR=${prefix}/include/${port_libfmt}

# Don't treat clang warnings as errors
configure.args-append   -DSTOP_ON_WARNINGS=OFF

# 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 \
                        port:libpng \
                        port:tiff \
                        port:squish \
                        port:${port_libfmt}

# optional components

#configure.args-append   -DUSE_OPENGL=OFF \
#                        -DUSE_QT=OFF
qt5.min_version         5.6

configure.args-append   -DUSE_PYTHON=OFF

set pythons_suffixes    {38 39 310 311 312}

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 range ${s} 1 end]
    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        +python312
}

#configure.args-append       -DUSE_FFMPEG=OFF
depends_lib-append          path:lib/libavcodec.dylib:ffmpeg

#configure.args-append       -DUSE_JPEGTURBO=OFF
depends_lib-append          path:include/turbojpeg.h:libjpeg-turbo \

#configure.args-append       -DUSE_OPENJPEG=OFF
depends_lib-append          port:openjpeg
configure.args-append       -DOpenJpeg_ROOT=${prefix}

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

#configure.args-append       -DUSE_OPENCV=OFF
set opencv_ver 4
depends_lib-append          path:lib/opencv${opencv_ver}/libopencv_core.dylib:opencv${opencv_ver}
configure.args-append       -DOpenCV_INCLUDE_DIR=${prefix}/include/opencv${opencv_ver} \
                            -DOpenCV_ROOT=${prefix}/lib/opencv${opencv_ver}

#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_OPENVDB=OFF
depends_lib-append          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

github.livecheck.regex      {([0-9.]+)}
