# -*- 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           boost 1.0
PortGroup           cmake  1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1

# posix_memalign
legacysupport.newest_darwin_requires_legacy 9

boost.version       1.81
boost.depends_type  build

github.setup        AcademySoftwareFoundation OpenColorIO 2.5.1 v
revision            0
name                [string tolower ${github.project}]
categories          graphics
maintainers         {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
license             BSD
homepage            https://opencolorio.org
description         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
github.tarball_from archive

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    patchfiles-append \
                    0001-Fix-macOS-build.patch
}

patchfiles-append   0002-fix-broken-linking.patch

# https://github.com/macos-powerpc/powerpc-ports/issues/116
patchfiles-append   0003-include-stdlib.patch

depends_build-append \
    path:bin/pkg-config:pkgconfig

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

compiler.cxx_standard   2017

configure.args-append \
    -DCMAKE_CONFIGURATION_TYPES=MacPorts \
    -DOCIO_BUILD_GPU_TESTS=OFF \
    -DOCIO_BUILD_TESTS=OFF \
    -DOCIO_BUILD_TRUELIGHT=OFF \
    -DOCIO_BUILD_NUKE=OFF \
    -DOCIO_METAL_ENABLED=OFF \
    -DUSE_EXTERNAL_YAML=ON

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

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

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

# 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_build-append port:py310-pybind11
    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_build-append port:py311-pybind11
    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_build-append port:py312-pybind11
    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_build-append port:py313-pybind11
    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_build-append port:py314-pybind11
    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 {
    depends_lib-append \
        port:freeglut \
        port:libGLU \
        port:mesa

    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 \
        -DOCIO_USE_APPLE_GL=OFF
}

variant quartz {
    configure.args-append \
        -DGLUT_glut_LIBRARY=/System/Library/Frameworks/GLUT.framework
}

if {[variant_isset x11] || [variant_isset quartz]} {
    depends_lib-append \
        port:glew \
        port:lcms2 \
        port:openimageio

    configure.args-append \
        -DUSE_EXTERNAL_LCMS=ON
} else {
    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
}

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