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

github.setup            AcademySoftwareFoundation OpenShadingLanguage 1.12.7.0 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
name                    osl
revision                1
categories              graphics
license                 BSD
maintainers             @jasonliu-- openmaintainer

homepage                http://opensource.imageworks.com
description             advanced shading language for computer \
                        graphics rendering
long_description        Open Shading Language is a language developed \
                        by Sony Pictures Imageworks, used for \
                        programming shader effects in \
                        computer-generated imagery. It can be used to \
                        define how surfaces or volumes scatter light, \
                        and can be used for describing materials, \
                        lights, displacement, and pattern generation.

checksums               rmd160  e80e2e0543691dfbe5c08f14d0f1527f5da021e1 \
                        sha256  0387104bafe309cac76fbf071d55eabccc3ece1a7108bea9d0649ca0934e8ce8 \
                        size    20249105

compiler.cxx_standard   2014

# Keep this value synchronized with the
# newest LLVM that is compatible with OSL
# and be sure that partio is built by the same compiler
set llvm_version        14

# 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 OpenShadingLanguage 1.11.13.0 Release-
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball
    name                osl
    revision            3

    checksums           rmd160  87b4fc115da9574735671fb56d3ae34a17171084 \
                        sha256  7ec1e9a460bd22211b14d926287c946d5ffba753d3ee48d08cf6149db2f23e12 \
                        size    14141564


    set llvm_version    11

    # https://github.com/AcademySoftwareFoundation/OpenShadingLanguage/pull/1319
    # Remove when in next release.
    patchfiles-append   fix-test-disable-cmake.patch
}

# MacPorts Clang is used "to generate bitcode"
# See variable LLVM_BC_GENERATOR
depends_build-append    port:flex \
                        port:bison \
                        port:clang-$llvm_version
depends_lib-append      port:llvm-$llvm_version \
                        port:imath \
                        port:pugixml \
                        port:pybind11 \
                        port:openimageio

# As MP clang is available at build time, use for consistency and
# to avoid potentially a second MP clang being installed unnecessarily.
compiler.blacklist-append clang
compiler.fallback macports-clang-${llvm_version}

post-patch {
    reinplace {s|llvm-config|llvm-config-mp|g} \
        ${worksrcpath}/src/cmake/modules/FindLLVM.cmake
    reinplace -E {s|(/shaders)|/share/OSL\1|g} \
        ${worksrcpath}/src/liboslcomp/oslcomp.cpp
}

configure.args-append   -DCMAKE_CXX_STANDARD=14 \
                        -DLLVM_VERSION=$llvm_version \
                        -DVERBOSE=ON \
                        -DUSE_PARTIO=OFF \
                        -DUSE_PYTHON=OFF \
                        -DOSL_BUILD_TESTS=OFF \
                        -DINSTALL_DOCS=OFF

pre-test {
    if {![variant_isset tests]} {
        ui_error "'tests' variant must be activated to enable test support"
        error "Please enable the 'tests' variant and try again"
    }
}

variant partio description {Enable Partio point cloud support} {
    depends_lib-append      port:partio
    configure.args-replace  -DUSE_PARTIO=OFF -DUSE_PARTIO=ON
}

variant tests description {Build unit tests} {
    configure.args-replace  -DOSL_BUILD_TESTS=OFF -DOSL_BUILD_TESTS=ON
    test.run    yes
}

variant docs description {Build documentation} {
    configure.args-replace  -DINSTALL_DOCS=OFF -DINSTALL_DOCS=ON
}

default_variants    +partio +docs

set pythons_suffixes    {38 39 310 311}

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} "
         require_active_variants [boost::depends_portname] ${p}
         depends_lib-append      port:${p}
         configure.args-append   -DPYTHON_VERSION=${v}
         configure.args-replace  -DUSE_PYTHON=OFF -DUSE_PYTHON=ON
     "
}

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-append \
        +python311
}

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