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

boost.version           1.88

github.setup            AcademySoftwareFoundation openvdb 12.1.1 v
revision                0
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  c71f992ff425ef1fe0669ed73458819293c07ba2 \
                        sha256  ccd0ea1669a53c7c13087a08ac5a1351041c4cdd308f6d6f591074a106fcb565 \
                        size    4876713
github.tarball_from     archive

patchfiles-append       d68d0914fc6ed41cadd363bd4330c39a7fb5b1f1.patch

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

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

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

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 ABI version 11, later ones are broken on 32-bit.
# FIXME: unfortunately, this is not sufficient to fix the build...
configure.args-append   -DUSE_CCACHE=OFF \
                        -DOPENVDB_ABI_VERSION_NUMBER=11 \
                        -DOPENVDB_USE_DEPRECATED_ABI_11=ON

# Enable SIMD compiler flags
if {${configure.build_arch} eq "x86_64"} {
      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 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 jemalloc \
    description "Use jemalloc as the concurrent memory allocator" \
{
    depends_lib-append      path:lib/pkgconfig/jemalloc.pc:jemalloc
    configure.args-append   -DCONCURRENT_MALLOC=Jemalloc
}

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

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

############### 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 alembic requires utils \
    description "Build OpenVDB tool (vdb_tool) with Alembic support" \
{
    depends_lib-append      port:alembic
}

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

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

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

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

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

set python_suffixes     {310 311 312 313 314}
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
    "
}

# FIXME: 12.x+ is largely broken, so we disable extra components. See:
# https://github.com/AcademySoftwareFoundation/openvdb/issues/2172
# default_variants        +docs +examples +jemalloc +jpg +nanovdb +openexr +png +utils
default_variants        +docs +jemalloc +openexr +png
