# -*- 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               cmake 1.1
PortGroup               active_variants 1.1
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            2
    checksums           rmd160  acca3794870850f6d39e41e62e9370bf6ff9565b \
                        sha256  8f6a547f6a5d510737ba436f867043db537def65f0fdb14ec30e5a185b619f93 \
                        size    52061055
} else {
    github.setup        AcademySoftwareFoundation OpenImageIO 2.1.20.0 v
    revision            16
    checksums           rmd160  7f241baddcc6e731a29fb37090e4582953560f38 \
                        sha256  0ad46bda55d6357a3c474b8c8ccbb41d9732313247cffaf4a65fc50e6aad9e78 \
                        size    29116032

    livecheck.type      none
}

github.tarball_from     archive

if {${port_latest}} {
    depends_lib-append  port:imath \
                        port:openexr \
                        port:openvdb \
                        port:onetbb
    #configure.args-append       -DUSE_TBB=OFF
    configure.args-append       -DTBB_ROOT=${prefix}/libexec/onetbb

    set ffmpeg_ver 6

    #configure.args-append       -DUSE_FFMPEG=OFF
    depends_lib-append          port:ffmpeg${ffmpeg_ver}

    configure.pkg_config_path-prepend \
                        ${prefix}/libexec/ffmpeg${ffmpeg_ver}/lib/pkgconfig
} 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

    # https://trac.macports.org/ticket/69842
    configure.args-append       -DUSE_OPENVDB=OFF

    #configure.args-append       -DUSE_TBB=OFF
    configure.args-append       -DTBB_ROOT=${prefix}/libexec/tbb

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

# 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-unknown-warning-option

if {[string match *clang* ${configure.compiler}]} {
    # This is a clang-specific flag:
    configure.cxxflags-append \
                        -Wno-error=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

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    configure.args-append \
                        -DUSE_QT=OFF
    # configure.args-append -DUSE_OPENGL=OFF
} else {
    PortGroup           qt5 1.0

    qt5.min_version     5.6
}

if {[string match *gcc* ${configure.compiler}] \
    && ${configure.build_arch} in [list i386 ppc]} {
    configure.ldflags-append \
                        -latomic
}

configure.args-append   -DUSE_PYTHON=OFF

set pythons_suffixes    {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_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

set opencv_ver 4
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # opencv4 does not build on 10.6 and earlier.
    configure.args-append   -DUSE_OPENCV=OFF
} else {
    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

# 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.]+)}
