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

github.setup        PixarAnimationStudios USD 20.05 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                usd
revision            4
categories          graphics
platforms           darwin
license             Apache-2
maintainers         @jasonliu--

homepage            http://www.openusd.org
description         universal scene description
long_description    Universal Scene Description (USD) is a software \
                    library, developed by Pixar Animation Studios, \
                    that provides a system for authoring, reading, and \
                    streaming time-sampled scene description for \
                    interchange between computer graphics \
                    applications. It organizes data into hierarchical \
                    namespaces of primitives and their properties. \
                    This provides a set of schemas upon which common \
                    3-D computer graphics concepts such as geometry, \
                    shading, models, and assets can be organized and \
                    manipulated.

checksums           rmd160  8ec6dda307f9804ce4b3a562c3df2dbd14a64390 \
                    sha256  ad167b438401848fdd4133f4470a6288ccf37949b4ccc9abf52b50438380ec59 \
                    size    24800580

set py_ver          3.11
set py_ver_nodot    [string map {. {}} ${py_ver}]
set py_ver_major    [lindex [split $py_ver .] 0]
set py_ver_minor    [lindex [split $py_ver .] 1]

# Both usd and spidermonkey attempt to create a library file called
# ${prefix}/lib/libjs.dylib. Unfortunately, these are not the same
# library, they just happen to be named the same.
# (See: https://trac.macports.org/ticket/63258)
# This issue has been reported upstream:
# https://github.com/PixarAnimationStudios/USD/issues/1615
conflicts           spidermonkey

depends_lib-append  port:tbb

compiler.cxx_standard 2014

# enforce to using the same compiler that is used for MaterialX
set llvm_version    14
# pin the used version for LLVM before 10.13 to
# which is forced by used version of osl
if {${os.platform} eq "darwin" && ${os.major} < 18} {
    set llvm_version    11
}

compiler.blacklist-append clang
compiler.fallback macports-clang-${llvm_version}

# This patch adds modifications that allow Blender to use USD. The patch
# file 'patch-add-blender-mods.diff' was extracted from the file in
# Blender's source code named
# 'build_files/build_environment/patches/usd.diff'. (No further
# modifications to the patch itself were needed, since Blender 2.90 was
# written to be compatible with USD version 20.05.) These modifications
# essentially add a hook for Blender to use, and shouldn't otherwise
# affect the functioning of USD.
patchfiles-append   patch-add-blender-mods.diff

# By default, USD places plugins in a ${prefix}/plugin directory. This
# patch changes it so that the plugins get placed in a location that
# conforms with MacPorts' mtree layout: ${prefix}/share/usd/plugins
patchfiles-append   patch-plugins-output-paths.diff

# A trivial patch to fix support Python 3.11
patchfiles-append   patch-python3.11-compatibility.diff

post-patch {
    # USD puts its CMake package config files in the wrong place
    reinplace {s|cmake/||} ${worksrcpath}/pxr/pxrConfig.cmake.in
    reinplace -E "s|\(set.PXR_INCLUDE_DIRS \).*|\\1\"${prefix}/include\")|" \
        ${worksrcpath}/pxr/pxrConfig.cmake.in
    foreach re [list \
        {s|(PROJECT_BINARY_DIR.*)(pxrConfig)|\1lib/cmake/usd/\2|} \
        {/DESTINATION.*CMAKE_INSTALL_PREFIX/s|(DESTINATION ")(.*)"|\1\2/lib/cmake/usd"|} \
        {/DESTINATION.*cmake/s|(DESTINATION ")cmake|\1lib/cmake/usd|} \
    ] {
        reinplace -E $re ${worksrcpath}/pxr/CMakeLists.txt
    }
}

# Turn off all of USD's default build options. We will allow users to
# directly control these options using port variants.
configure.args      -DPXR_BUILD_TESTS=OFF \
                    -DPXR_BUILD_EXAMPLES=OFF \
                    -DPXR_BUILD_TUTORIALS=OFF \
                    -DPXR_BUILD_IMAGING=OFF \
                    -DPXR_BUILD_USD_IMAGING=OFF \
                    -DPXR_BUILD_USDVIEW=OFF \
                    -DPXR_ENABLE_GL_SUPPORT=OFF \
                    -DPXR_ENABLE_PYTHON_SUPPORT=OFF \
                    -DPXR_ENABLE_HDF5_SUPPORT=OFF \
                    -DPXR_ENABLE_PTEX_SUPPORT=OFF

configure.env-append \
                    TBBROOT=${prefix}/libexec/tbb

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

# Certain Python script files are not getting their shebang lines
# replaced by the 'cmake/macros/shebang.py' script during the build,
# so we do it for them.
proc shebang dir {
    global prefix py_ver
    if {![file exists $dir] || ![file isdirectory $dir]} {
        return -code error "$dir: No such directory"
    }
    fs-traverse f $dir {
        if {[file isfile $f]} {
            # Look for the string we want to replace inside the contents
            # of the file before we actually do the substitution. This
            # prevents errors being thrown by reinplace in the case
            # where the file in question is a binary file.
            set fp [open $f r]
            set count [regexp -all -- {pxrpythonsubst} [read $fp]]
            close $fp
            if {$count > 0} {
                reinplace -q \
                    "s|/pxrpythonsubst|${prefix}/bin/python$py_ver|" $f
            }
        }
    }
}

post-destroot {
    shebang ${destroot}${prefix}/share/usd
}

############################# Variants ##############################

# Note: The various variant dependencies and package dependencies
# specified in the variants below were obtained by looking at the
# 'cmake/defaults/Packages.cmake' file in the USD source code.

set py_libs_dir ${frameworks_dir}/Python.framework/Versions/$py_ver/lib

variant docs description {Build documentation} {
    depends_build-append    path:bin/doxygen:doxygen \
                            path:bin/dot:graphviz
    post-patch {
        reinplace {s|docs|share/doc/usd|g} \
            ${worksrcpath}/cmake/macros/Public.cmake
        reinplace {/installPath = os/s|docs|share/doc/usd|} \
            ${worksrcpath}/cmake/macros/generateDocs.py
        reinplace {/os.mkdir.installPath/s/mkdir/makedirs/} \
            ${worksrcpath}/cmake/macros/generateDocs.py
        reinplace {/PROJECT_NAME.*=/s/$/"Universal Scene Description"/} \
            ${worksrcpath}/pxr/usd/usd/Doxyfile.in
        reinplace "/PROJECT_NUMBER.*=/s/$/${version}/" \
            ${worksrcpath}/pxr/usd/usd/Doxyfile.in
    }
    configure.args-append   -DPXR_BUILD_DOCUMENTATION=ON
    post-destroot {
        foreach f [list \
            CHANGELOG.md \
            CONTRIBUTING.md \
            LICENSE.txt \
            NOTICE.txt \
            USD_CLA_Corporate.pdf \
            USD_CLA_Individual.pdf \
        ] {
            copy ${worksrcpath}/$f ${destroot}${prefix}/share/doc/usd/
        }
    }
}

variant tests description {Build unit tests} {
    configure.args-replace  -DPXR_BUILD_TESTS=OFF \
                            -DPXR_BUILD_TESTS=ON
    post-build {
        shebang ${worksrcpath}/pxr
    }
    test.run yes
}

variant examples description {Build examples} {
    configure.args-replace  -DPXR_BUILD_EXAMPLES=OFF \
                            -DPXR_BUILD_EXAMPLES=ON
    post-destroot {
        # One of the examples' object file is getting compiled assuming
        # that its associated library is located in ${prefix}/lib,
        # instead of inside the examples directory. I haven't been able
        # to find where this is getting set in the source code, so we
        # fix the path after it gets compiled.
        set usdSchemaExamples_so \
            $py_libs_dir/python${py_ver}/site-packages/pxr/UsdSchemaExamples/_usdSchemaExamples.so
        if {[file exists ${destroot}$usdSchemaExamples_so]} {
            system [join [list \
                "install_name_tool -change" \
                ${prefix}/lib/usdSchemaExamples.dylib \
                ${prefix}/share/usd/examples/share/usd/plugins/usdSchemaExamples.dylib \
                ${destroot}$usdSchemaExamples_so \
            ] " "]
            system [join [list \
                "install_name_tool -id" \
                $usdSchemaExamples_so \
                ${destroot}$usdSchemaExamples_so \
            ] " "]
        }
    }
}

variant tutorials requires python description {Build tutorials} {
    configure.args-replace  -DPXR_BUILD_TUTORIALS=OFF \
                            -DPXR_BUILD_TUTORIALS=ON
}

variant imaging description {Build imaging components} {
    depends_lib-append      port:opensubdiv
    configure.args-replace  -DPXR_BUILD_IMAGING=OFF \
                            -DPXR_BUILD_IMAGING=ON
}

variant usd_imaging requires imaging \
    description {Build USD imaging components} \
{
    configure.args-replace  -DPXR_BUILD_USD_IMAGING=OFF \
                            -DPXR_BUILD_USD_IMAGING=ON
}

variant opengl description {Enable OpenGL-based components} {
    depends_lib-append      port:glew
    configure.args-replace  -DPXR_ENABLE_GL_SUPPORT=OFF \
                            -DPXR_ENABLE_GL_SUPPORT=ON
}

variant python description {Enable Python-based components} {
    require_active_variants [boost::depends_portname] python${py_ver_nodot}
    depends_lib-append      port:python${py_ver_nodot} \
                            port:py${py_ver_nodot}-jinja2
    post-patch {
        if {![active_variants [boost::depends_portname] cmake_scripts]} {
            reinplace "/set.boost_version_string/a\\
\\    set(Boost_NO_BOOST_CMAKE TRUE)\\
\\    if(NOT PYTHON_VERSION_MAJOR)\\
\\        set(PYTHON_VERSION_MAJOR $py_ver_major)\\
\\    endif()\\
\\    if(NOT PYTHON_VERSION_MINOR)\\
\\        set(PYTHON_VERSION_MINOR $py_ver_minor)\\
\\    endif()\\
" \
                ${worksrcpath}/cmake/defaults/Packages.cmake
        } else {
            reinplace "/find_package(Boost/,/)/s|python$|python$py_ver_nodot|" \
                ${worksrcpath}/cmake/defaults/Packages.cmake
        }

        set pypath_regex \
            "s|\(lib/python\)|Library/Frameworks/Python.framework/Versions/$py_ver/\\1$py_ver/site-packages|"
        foreach f [list \
            ${worksrcpath}/cmake/macros/Private.cmake \
            ${worksrcpath}/pxr/usdImaging/usdviewq/CMakeLists.txt \
        ] {
            reinplace -E $pypath_regex $f
        }
        foreach search_regex [list \
            "/_get_install_dir/" \
            "/set._testPythonPath/" \
        ] {
            reinplace -E [join [list $search_regex $pypath_regex] ""] \
                ${worksrcpath}/cmake/macros/Public.cmake
        }
    }
    configure.args-replace  -DPXR_ENABLE_PYTHON_SUPPORT=OFF \
                            -DPXR_ENABLE_PYTHON_SUPPORT=ON
    configure.args-append   -DPXR_USE_PYTHON_3=ON \
                            -DPYTHON_EXECUTABLE=${prefix}/bin/python$py_ver \
                            -DPythonLibs_DIR=$py_libs_dir
}

variant viewer requires usd_imaging python opengl \
    description {Build USD viewer} \
{
    depends_lib-append      port:py${py_ver_nodot}-pyside-tools \
                            port:py${py_ver_nodot}-opengl
    post-patch {
        reinplace -E "s/\(pyside-uic-\)2.7/\\1${py_ver}/" \
            ${worksrcpath}/cmake/modules/FindPySide.cmake
    }
    configure.args-replace  -DPXR_BUILD_USDVIEW=OFF \
                            -DPXR_BUILD_USDVIEW=ON
}

variant ptex description {Enable Ptex support} {
    depends_lib-append      port:ptex
    configure.args-replace  -DPXR_ENABLE_PTEX_SUPPORT=OFF \
                            -DPXR_ENABLE_PTEX_SUPPORT=ON
}

variant openvdb description {Enable OpenVDB support} {
    depends_lib-append      port:openvdb
    configure.args-append   -DPXR_ENABLE_OPENVDB_SUPPORT=ON
}

variant monolithic \
    description {Build the USD libraries as a single archive library, instead of modular individual libraries} \
{
    configure.args-append   -DPXR_BUILD_MONOLITHIC=ON
}

### Imaging Plugins

variant oiio description {Build OpenImageIO plugin} {
    depends_lib-append      port:openimageio
    configure.args-append   -DPXR_BUILD_OPENIMAGEIO_PLUGIN=ON
}

variant ocio description {Build OpenColorIO plugin} {
    depends_lib-append      port:opencolorio
    configure.args-append   -DPXR_BUILD_OPENCOLORIO_PLUGIN=ON
}

variant alembic description {Build the Alembic plugin for USD} {
    depends_lib-append      port:alembic
    configure.args-append   -DPXR_BUILD_ALEMBIC_PLUGIN=ON
}

variant hdf5 requires alembic \
    description {Enable HDF5 support in the Alembic plugin} \
{
    require_active_variants alembic hdf5
    configure.args-replace  -DPXR_ENABLE_HDF5_SUPPORT=OFF \
                            -DPXR_ENABLE_HDF5_SUPPORT_ON
}

variant materialx description {Build MaterialX plugin} {
    depends_lib-append      port:materialx
    post-patch {
        foreach re [list \
            "/find_package.MaterialX/i\\
\\    set(CMAKE_MODULE_PATH_ORIG \${CMAKE_MODULE_PATH})\\
\\    list(PREPEND CMAKE_MODULE_PATH \"\${CMAKE_SOURCE_DIR}/cmake/modules\")\\
" \
            "/find_package.MaterialX/a\\
\\    set(CMAKE_MODULE_PATH \${CMAKE_MODULE_PATH_ORIG})\\
" \
        ] {
            reinplace $re ${worksrcpath}/cmake/defaults/Packages.cmake
        }
        reinplace -E {s|(stdlib_defs.mtlx)|share/MaterialX/libraries/stdlib/\1|} \
            ${worksrcpath}/cmake/modules/FindMaterialX.cmake
    }
    configure.args-append   -DPXR_BUILD_MATERIALX_PLUGIN=ON
}

default_variants    +docs +examples +tutorials +ptex
