# -*- 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.5.1 v
github.tarball_from archive
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  33e754f50c4d281c8571c1e52d0f4e33964405f5 \
                    sha256  08cb6213ea4edee550ab050509d38204004bee6742c658166b1cf825d0a9381b \
                    size    14181252

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

configure.args-append \
    -DCMAKE_CONFIGURATION_TYPES=MacPorts \
    -DOCIO_BUILD_TESTS=OFF

# 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

    depends_lib-append  port:tinyxml

    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

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

    # 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 \
    path:bin/pkg-config:pkgconfig \
    port:sse2neon

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

depends_lib-append  \
    port:expat \
    port:imath \
    port:minizip-ng \
    port:pystring \
    port:yaml-cpp \
    port:zlib

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 python310 description {Build the Python 3.10 bindings} conflicts python311 python312 python313 python314 {
    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 python310 python312 python313 python314 {
    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 python310 python311 python313 python314 {
    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/
    }
}

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

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

if {
    ![variant_isset python310] &&
    ![variant_isset python311] &&
    ![variant_isset python312] &&
    ![variant_isset python313]
} {
    default_variants +python314
}

if {
    ![variant_isset python310] &&
    ![variant_isset python311] &&
    ![variant_isset python312] &&
    ![variant_isset python313] &&
    ![variant_isset python314]
} {
    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 {
    # Use OpenGL framework instead of mesa
    configure.pre_args-delete   -DCMAKE_FIND_FRAMEWORK=LAST

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

    depends_lib-append \
        port:lcms2
}

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-replace \
        -DOCIO_BUILD_TESTS=OFF \
        -DOCIO_BUILD_TESTS=ON

    test.run    yes
}
