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

github.setup            AcademySoftwareFoundation openvdb 11.0.0 v
github.tarball_from     archive
revision                1
categories              graphics
license                 {MPL-2 LGPL-2.1+}
maintainers             {mcalhoun @MarcusCalhoun-Lopez} \
                        @jasonliu-- openmaintainer

homepage                https://www.${name}.org/
description             sparse volumetric data structure and tools
long_description        OpenVDB is an open source C++ library \
                        comprising a novel hierarchical data structure \
                        and a large suite of tools for the efficient \
                        storage and manipulation of sparse volumetric \
                        data discretized on three-dimensional grids. \
                        It was developed by DreamWorks Animation for \
                        use in volumetric applications typically \
                        encountered in computer-generated graphics and \
                        animation.

checksums               rmd160  3e870caf094446807748dc77744cd1ffc0992f79 \
                        sha256  6314ff1db057ea90050763e7b7d7ed86d8224fcd42a82cdbb9c515e001b96c74 \
                        size    4620858

# it uses constexpr if statement
compiler.cxx_standard   2017
# Blacklist macports-clang-16+ to prevent
#  error: no template named 'unary_function' in namespace 'std'
compiler.blacklist-append {macports-clang-1[6-9]}

boost.version           1.87

depends_build-append    port:pkgconfig

depends_lib-append      port:zlib \
                        port:blosc \
                        port:onetbb \
                        path:lib/pkgconfig/glfw3.pc:glfw

configure.args-append   -DTBB_INCLUDEDIR=${prefix}/libexec/onetbb/include \
                        -DTBB_LIBRARYDIR=${prefix}/libexec/onetbb/lib

# Disable building command-line tools.
# We control whether to build them using the +utils variant below.
configure.args-append   -DOPENVDB_BUILD_BINARIES=OFF

# avoid
#     ccache: error: Failed to create directory
#     ${prefix}/var/macports/build/.ccache/tmp: Operation not permitted
# use latest ABI version (default is 3)
configure.args-append   -DUSE_CCACHE=OFF \
                        -DOPENVDB_ABI_VERSION_NUMBER=[lindex [split ${version} .] 0]

# Enable SIMD compiler flags
if {${configure.build_arch} ni [list ppc ppc64]} {
      configure.args-append \
                        -DOPENVDB_SIMD=AVX
}

if {[string match *gcc* ${configure.compiler}] \
    && ${configure.build_arch} in [list i386 ppc]} {
    # https://github.com/AcademySoftwareFoundation/openvdb/issues/1801
    patchfiles-append   patch-libatomic.diff
}

# Disable CMake uninstall target
configure.args-append   -DOPENVDB_ENABLE_UNINSTALL=OFF

pre-test {
    if {![variant_isset tests]} {
        ui_warn "'tests' variant must be activated to enable full test support"
    }
}

# All of the CMake module files are getting installed, regardless of
# whether the option has been enabled or not. This issue has been
# reported (and a patch has been submitted) upstream:
# https://github.com/AcademySoftwareFoundation/openvdb/pull/1643
# If and when the patch gets accepted and merged into upstream, the
# following code should be deleted from this Portfile when the patch
# gets incorporated into a future upstream release.
post-destroot {
    set lib_cmake_path ${prefix}/lib/cmake/OpenVDB
    if {![variant_isset logging]} {
        delete ${destroot}$lib_cmake_path/FindLog4cplus.cmake
    }
    if {![variant_isset openexr]} {
        delete ${destroot}$lib_cmake_path/FindIlmBase.cmake
        delete ${destroot}$lib_cmake_path/FindOpenEXR.cmake
    }
    if {![variant_isset jemalloc]} {
        delete ${destroot}$lib_cmake_path/FindJemalloc.cmake
    }
    if {![variant_isset utils]} {
        delete ${destroot}$lib_cmake_path/OpenVDBUtils.cmake
    }
    delete ${destroot}$lib_cmake_path/OpenVDBHoudiniSetup.cmake
    delete ${destroot}$lib_cmake_path/OpenVDBMayaSetup.cmake
}

############################ OpenVDB Core ############################

variant tests description {Build unit tests} {
    depends_lib-append      port:gtest
    configure.args-append   -DOPENVDB_BUILD_UNITTESTS=ON
    if {[variant_isset nanovdb]} {
        configure.args-append   -DNANOVDB_BUILD_UNITTESTS=ON
    }
    test.run yes
}

variant docs description {Build documentation} {
    depends_build-append    path:bin/doxygen:doxygen
    configure.args-append   -DOPENVDB_BUILD_DOCS=ON \
                            -DOPENVDB_DOXYGEN_AX=OFF \
                            -DOPENVDB_DOXYGEN_HOUDINI=OFF

    if {![variant_isset nanovdb]} {
        configure.args-append   -DOPENVDB_DOXYGEN_NANOVDB=OFF
    }
}

variant nanovdb description {Build the NanoVDB library} {
    configure.args-append   -DOPENVDB_BUILD_NANOVDB=ON \
                            -DNANOVDB_BUILD_TOOLS=OFF \
                            -DNANOVDB_USE_INTRINSICS=ON \
                            -DNANOVDB_USE_OPENVDB=ON
}

variant logging description {Build and enable logging support} {
    depends_lib-append      port:log4cplus
    configure.args-append   -DUSE_LOG4CPLUS=ON
}

variant openexr description {Build with OpenEXR support} {
    depends_lib-append      port:imath \
                            port:openexr
    configure.args-append   -DUSE_EXR=ON
}

variant png description {Build with PNG support} {
    depends_lib-append      port:libpng
    configure.args-append   -DUSE_PNG=ON
}

variant jemalloc \
    description {Use jemalloc as the concurrent memory allocator} \
{
    depends_lib-append      path:lib/pkgconfig/jemalloc.pc:jemalloc
    configure.args-append   -DCONCURRENT_MALLOC=Jemalloc
}

############### OpenVDB Binaries (command-line tools) ################

variant utils description {Build OpenVDB command-line tools} {
    configure.args-replace  -DOPENVDB_BUILD_BINARIES=OFF \
                            -DOPENVDB_BUILD_BINARIES=ON

    if {[variant_isset nanovdb]} {
        configure.args-replace  -DNANOVDB_BUILD_TOOLS=OFF \
                                -DNANOVDB_BUILD_TOOLS=ON
    }

    configure.args-append   -DOPENVDB_BUILD_VDB_LOD=ON \
                            -DOPENVDB_BUILD_VDB_RENDER=ON \
                            -DOPENVDB_BUILD_VDB_VIEW=ON \
                            -DOPENVDB_BUILD_VDB_TOOL=ON

    # OpenVDB tool (vdb_tool) options
    if {[variant_isset tests]} {
        configure.args-append   -DBUILD_TEST=ON
    }
    if {[variant_isset nanovdb]} {
        configure.args-append   -DOPENVDB_TOOL_USE_NANO=ON
    }
    if {[variant_isset png]} {
        configure.args-append   -DOPENVDB_TOOL_USE_PNG=ON
    }
    # There is currently a configure error when trying to build vdb_tool
    # using OpenEXR. This issue has already been reported upstream:
    # https://github.com/AcademySoftwareFoundation/openvdb/issues/1541
#   if {[variant_isset openexr]} {
#       configure.args-append   -DOPENVDB_TOOL_USE_EXR=ON
#   }
    if {[variant_isset jpg]} {
        configure.args-append   -DOPENVDB_TOOL_USE_JPG=ON
    }
    if {[variant_isset alembic]} {
        configure.args-append   -DOPENVDB_TOOL_USE_ABC=ON
    }
}

variant jpg requires utils \
    description {Build OpenVDB tool (vdb_tool) with JPG support} \
{
    depends_lib-append      port:libjpeg-turbo
}

variant alembic requires utils \
    description {Build OpenVDB tool (vdb_tool) with Alembic support} \
{
    depends_lib-append      port:alembic
}

############################## NanoVDB ###############################

variant examples requires nanovdb description {Build NanoVDB examples} {
    configure.args-append   -DNANOVDB_BUILD_EXAMPLES=ON
}

variant benchmark requires nanovdb examples \
    description {Build NanoVDB benchmark in examples} \
{
    depends_lib-append      port:gtest
    configure.args-append   -DNANOVDB_BUILD_BENCHMARK=ON
}

############################# PyOpenVDB ##############################

set python_suffixes     {39 310 311 312}
set python_ports        {}
foreach s $python_suffixes {
    lappend python_ports python$s
}

foreach s $python_suffixes {
    set p python$s
    set v [string index $s 0].[string range $s 1 end]
    set i [lsearch -exact $python_ports $p]
    set c [lreplace $python_ports $i $i]
    variant $p conflicts {*}${c} \
        description "Build the PyOpenVDB Python ${v} module" \
    "
        depends_lib-append      port:${p} \
                                port:py${s}-numpy
        boost.require_numpy     yes
        require_active_variants [boost::depends_portname]       ${p}
        require_active_variants [boost::depends_portname]-numpy ${p}

        # Find correct Python version
        patchfiles-append       patch-python_version.diff

        post-patch {
            reinplace s|__MACPORTS_PYTHON_VERSION__|${v}|g \
                ${worksrcpath}/${name}/${name}/python/CMakeLists.txt
        }

        configure.args-append   -DOPENVDB_BUILD_PYTHON_MODULE=ON \
                                -DUSE_NUMPY=ON
    "
}

default_variants        +docs +nanovdb +examples +openexr +jemalloc \
                        +utils +png +jpg

# While alembic builds, it does not work correctly on BE platforms:
# https://github.com/alembic/alembic/issues/449
if {${configure.build_arch} ni [list ppc ppc64]} {
    default_variants-append \
                        +alembic
}
