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

boost.depends_type  build

github.setup        AcademySoftwareFoundation OpenColorIO 2.3.2 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0
name                opencolorio
categories          graphics
maintainers         {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
license             BSD
homepage            https://opencolorio.org
description         a color management framework for visual effects and animation
long_description    OpenColorIO (OCIO) is a complete color management solution \
                    geared towards motion picture production with an emphasis on \
                    visual effects and computer animation.

checksums           rmd160  297c061816895bd4d02a9450c0415d8769e188ab \
                    sha256  6e437aa8c09f5d870212847af7788d92c3a5c34c739b212a9947a4ef3e8542b8 \
                    size    11444813

# Exclude pre-releases from livecheck
github.livecheck.regex  {([0-9.]+)}

configure.args-append \
    -DCMAKE_CONFIGURATION_TYPES=MacPorts

# 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 OpenColorIO 1.1.1 v
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    revision            4

    checksums           rmd160  81534822cb8350e1b7ba171c91226de996494d02 \
                        sha256  b7def3b5383c9b35dc9c0bdd26da74e2199c2b283d52bb84847aa307f70a0bc4 \
                        size    13830493

    # workaround for stealth update
    dist_subdir         ${name}/${version}_1

    # see https://github.com/imageworks/OpenColorIO/commit/c43cc918c3e79e324f11ca47e95bfe36e9e0dd15
    patchfiles-append   patch-upstream.diff

    # see https://gitweb.gentoo.org/repo/gentoo.git/tree/media-libs/opencolorio/files/opencolorio-1.1.0-yaml-cpp-0.6.patch
    patchfiles-append   patch-yaml-cpp.diff

    patchfiles-append   patch-yaml-cpp-0.7.diff

    patchfiles-append   patch-fix-endianness.diff

    patchfiles-append   patch-CMakeLists.txt.diff

    livecheck.type      none

    # see https://github.com/imageworks/OpenColorIO/issues/563
    # https://trac.macports.org/ticket/67904
    if {[string match *clang* ${configure.compiler}]} {
        configure.cxxflags-append \
            -Wno-error=self-assign-field
    }
}

# source assumes C++11 compiler
compiler.cxx_standard 2011
configure.cxxflags-append \
    -std=c++11

# src/core/ExponentOps.cpp: error: 'OpenColorIO::v1::{anonymous}::ExponentOp::~ExponentOp()'
# defined but not used [-Werror=unused-function]
# pyglue/PyAllocationTransform.cpp: error: cast between incompatible function types from 'PyObject* (*)(PyObject*)' \
# {aka '_object* (*)(_object*)'} to 'PyCFunction' {aka '_object* (*)(_object*, _object*)'} [-Werror=cast-function-type]
if {[string match *gcc* ${configure.compiler}]} {
    configure.cxxflags-append \
        -Wno-unused-function -Wno-cast-function-type
}

configure.cxxflags-append \
    -Wno-deprecated-declarations

depends_build-append \
    port:pkgconfig

# not in MacPorts
configure.args-append \
    -DOCIO_BUILD_TRUELIGHT=OFF \
    -DOCIO_BUILD_NUKE=OFF

depends_lib-append  \
    port:yaml-cpp \
    port:tinyxml

configure.args-append \
    -DUSE_EXTERNAL_YAML=ON \
    -DUSE_EXTERNAL_TINYXML=ON \
    -DUSE_EXTERNAL_LCMS=ON

if {${configure.build_arch} in [list ppc ppc64]} {
    # This option is ON by default:
    configure.args-append \
        -DOCIO_USE_SSE=OFF
}

# OpenColorIO intentionally installs Python module in lib
# see https://github.com/imageworks/OpenColorIO/blob/15e96c1f579d3640947a5fcb5ec831383cc3956e/src/pyglue/CMakeLists.txt#L85

variant python38 description {Build the Python 3.8 bindings} conflicts python39 python310 python311 python312 {
    depends_lib-append port:python38
    configure.args-append \
        -DPYTHON=${prefix}/bin/python3.8
    post-destroot {
        xinstall -d -m 0755 \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.8/lib/python3.8/site-packages
        ln -s \
            ${prefix}/lib/python3.8/site-packages/PyOpenColorIO.so \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.8/lib/python3.8/site-packages/
    }
}

variant python39 description {Build the Python 3.9 bindings} conflicts python38 python310 python311 python312 {
    depends_lib-append port:python39
    configure.args-append \
        -DPYTHON=${prefix}/bin/python3.9
    post-destroot {
        xinstall -d -m 0755 \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.9/lib/python3.9/site-packages
        ln -s \
            ${prefix}/lib/python3.9/site-packages/PyOpenColorIO.so \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.9/lib/python3.9/site-packages/
    }
}

variant python310 description {Build the Python 3.10 bindings} conflicts python38 python39 python311 python312 {
    depends_lib-append port:python310
    configure.args-append \
        -DPYTHON=${prefix}/bin/python3.10
    post-destroot {
        xinstall -d -m 0755 \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.10/lib/python3.10/site-packages
        ln -s \
            ${prefix}/lib/python3.10/site-packages/PyOpenColorIO.so \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.10/lib/python3.10/site-packages/
    }
}


variant python311 description {Build the Python 3.11 bindings} conflicts python38 python39 python310 python312 {
    depends_lib-append port:python311
    configure.args-append \
        -DPYTHON=${prefix}/bin/python3.11
    post-destroot {
        xinstall -d -m 0755 \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.11/lib/python3.11/site-packages
        ln -s \
            ${prefix}/lib/python3.11/site-packages/PyOpenColorIO.so \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.11/lib/python3.11/site-packages/
    }
}

variant python312 description {Build the Python 3.12 bindings} conflicts python38 python39 python310 python311 {
    depends_lib-append port:python312
    configure.args-append \
        -DPYTHON=${prefix}/bin/python3.12
    post-destroot {
        xinstall -d -m 0755 \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.12/lib/python3.12/site-packages
        ln -s \
            ${prefix}/lib/python3.12/site-packages/PyOpenColorIO.so \
            ${destroot}${frameworks_dir}/Python.framework/Versions/3.12/lib/python3.12/site-packages/
    }
}

if {![variant_isset python38] && ![variant_isset python39] && ![variant_isset python310] && ![variant_isset python311] && ![variant_isset python312]} {
    default_variants +python312
}

if {![variant_isset python38] && ![variant_isset python39] && ![variant_isset python310] && ![variant_isset python311] && ![variant_isset python312]} {
    configure.args-append \
        -DOCIO_BUILD_PYGLUE=OFF
}

# make neither x11 nor quartz default since openimageio depends on opencolorio

variant x11 {
    if {${port_latest}} {
        depends_lib-append  port:openexr
    } else {
        depends_lib-append  port:openimageio
    }

    depends_lib-append \
        port:lcms2 \
        port:mesa \
        port:libGLU \
        port:freeglut \
        port:glew

    configure.args-append \
        -DOPENGL_gl_LIBRARY=${prefix}/lib/libGL.dylib \
        -DOPENGL_glu_LIBRARY=${prefix}/lib/libGLU.dylib \
        -DGLUT_glut_LIBRARY=${prefix}/lib/libglut.dylib \
        -DGLUT_INCLUDE_DIR=${prefix}/include \
        -DOPENGL_INCLUDE_DIR=${prefix}/include \
        -DGLUT_cocoa_LIBRARY="" \
        -DGLEW_INCLUDES=${prefix}/include \
        -DGLEW_LIBRARIES=${prefix}/lib/libGLEW.dylib
}

variant quartz {
    if {${port_latest}} {
        depends_lib-append  port:openexr
    } else {
        depends_lib-append  port:openimageio
    }

    depends_lib-append \
        port:lcms2 \
        port:glew
    configure.args-append \
        -DGLUT_glut_LIBRARY=/System/Library/Frameworks/GLUT.framework
}

if {![variant_isset x11] && ![variant_isset quartz]} {
    configure.args-append \
        -DOCIO_BUILD_APPS=OFF
}

# Notice that on PowerPC tests will report a failure, however if you look into the actual output,
# almost all tests pass (with the fix for endianness above). What still fails is a result of OpenColorIO
# being incapable of properly handling rounding: FAILED: osvec[i] == PROFILE_OUTvec[i]
# values were 'luma: [0.212599993, 0.715200007, 0.0722000003]' and 'luma: [0.2126, 0.7152, 0.0722]'
variant tests description "Enable testing" {
    configure.pre_args-replace \
        -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
        -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF
    configure.args-append \
        -DOCIO_BUILD_TESTS=ON

    test.run    yes
}
