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

github.setup        PixarAnimationStudios OpenSubdiv 3_4_4 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            2
name                opensubdiv
version             [string map {_ .} ${github.version}]
categories          graphics
license             Apache-2
maintainers         @jasonliu-- openmaintainer
homepage            http://graphics.pixar.com/opensubdiv
description         subdivision surface library
long_description    OpenSubdiv is a set of libraries developed by \
                    Pixar Animation Studios that implement \
                    high-performance subdivision surface (subdiv) \
                    evaluation on massively-parallel CPU and GPU \
                    architectures. This code path is optimized for \
                    drawing deforming surfaces with static topology at \
                    interactive framerates.

checksums           rmd160  63e8288300b940f8c3d66ba1c70387d4d61b957c \
                    sha256  f15f443efcdf1a04668ddbc375f999623cb9766f394558e271a5341c55d2f4e5 \
                    size    39233132

set pythonvers      3.12
set pythonvers_nodot [string map {. ""} $pythonvers]

depends_build-append \
                    path:bin/doxygen:doxygen \
                    port:py${pythonvers_nodot}-docutils \
                    port:py${pythonvers_nodot}-pygments \
                    path:bin/dot:graphviz

depends_lib-append  port:tbb \
                    port:glfw \
                    port:ptex \
                    port:zlib

# Need updated compiler toolkit (ranlib)
# object: ../lib/OpenSubdiv.framework/Versions/A/OpenSubdiv malformed object (unknown load command 0)
depends_build-append \
                    port:cctools \
                    port:ld64

compiler.cxx_standard 2011

post-patch {
    # OpenSubdiv creates a framework bundle,
    # but puts it in the wrong place.
    reinplace "s|@rpath|${frameworks_dir}|g" \
        ${worksrcpath}/opensubdiv/CMakeLists.txt
    reinplace -E "s|\(FRAMEWORK DESTINATION\).*|\\1 \"${frameworks_dir}\"|g" \
        ${worksrcpath}/opensubdiv/CMakeLists.txt

    # Make it so that OpenSubdiv is able to find MacPorts' Docutils.
    reinplace "s|rst2html.py|rst2html-${pythonvers}|" \
        ${worksrcpath}/cmake/FindDocutils.cmake
    # Set a project name, otherwise Doxygen will generate
    # documentation with a project name of "My Project".
    reinplace -E "s|^\(PROJECT_NAME.*\)$|\\1 OpenSubdiv|" \
        ${worksrcpath}/documentation/OpenSubdiv.doxy
}

# By default, OpenSubdiv puts all examples, tutorials, regression
# tests, etc. directly into the ${prefix}/bin directory. We instead
# place them into a location that makes a bit more sense.
#                           vvvvvv
configure.args      -DCMAKE_BINDIR_BASE=${prefix}/share/${name} \
                    -DNO_CUDA=1 \
                    -DNO_CLEW=1 \
                    -DNO_OMP=1 \
                    -DTBB_LOCATION=${prefix}/libexec/tbb \
                    -DCMAKE_AR=${prefix}/bin/ar \
                    -DCMAKE_NM=${prefix}/bin/nm \
                    -DCMAKE_OBJDUMP=${prefix}/bin/objdump \
                    -DCMAKE_RANLIB=${prefix}/bin/ranlib \
                    -DCMAKE_STRIP=${prefix}/bin/strip \
                    -DCMAKE_LINKER=${prefix}/bin/ld

if {${os.platform} eq "darwin" && ${os.major} < 16} {
    # OpenSubdiv uses features of Metal that were added starting
    # in macOS 10.12 (e.g. MTLTessellationPartitionMode)
    configure.args-append   -DNO_METAL=1
}

variant clew description {Enable CLEW wrapper library} {
    depends_lib-append      port:clew
    configure.args-replace  -DNO_CLEW=1 -DNO_CLEW=0
}

variant glew description {Enable use of GLEW} {
    depends_lib-append      port:glew
    # Default is set to NO_GLEW=1 in OpenSubdiv's CMake file (v3_4_3)
    configure.args-append   -DNO_GLEW=0
}

variant openmp description {Enable OpenMP support} {
    depends_lib-append      port:libomp
    configure.args-replace  -DNO_OMP=1 -DNO_OMP=0
    compiler.blacklist-append clang
}

# Exclude pre-release candidates
github.livecheck.regex  {([0-9_]+)}
