## Copyright 2020 Intel Corporation
## SPDX-License-Identifier: Apache-2.0

set(COMPONENT_NAME openvdb)

set(COMPONENT_PATH ${INSTALL_DIR_ABSOLUTE})
if (INSTALL_IN_SEPARATE_DIRECTORIES)
  set(COMPONENT_PATH ${INSTALL_DIR_ABSOLUTE}/${COMPONENT_NAME})
endif()

set(OPENVDB_HASH_ARGS "")
if (NOT "${OPENVDB_HASH}" STREQUAL "")
  set(OPENVDB_HASH_ARGS URL_HASH SHA256=${OPENVDB_HASH})
endif()

# Options that are shared between the OpenVDB and Open VKL builds.
# We need to define these both for the OpenVDB dependency build and
# the main Open VKL build.
set(OPENVKL_EXTRA_OPENVDB_OPTIONS -DCMAKE_NO_SYSTEM_FROM_IMPORTED=ON)
mark_as_advanced(OPENVKL_EXTRA_OPENVDB_OPTIONS)

ExternalProject_Add(${COMPONENT_NAME}
  PREFIX ${COMPONENT_NAME}
  DOWNLOAD_DIR ${COMPONENT_NAME}
  STAMP_DIR ${COMPONENT_NAME}/stamp
  SOURCE_DIR ${COMPONENT_NAME}/src
  BINARY_DIR ${COMPONENT_NAME}/build
  URL ${OPENVDB_URL}
  ${OPENVDB_HASH_ARGS}
  CMAKE_ARGS
    -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
    -DCMAKE_INSTALL_PREFIX=${COMPONENT_PATH}
    -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
    -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
    -DCMAKE_INSTALL_DOCDIR=${CMAKE_INSTALL_DOCDIR}
    -DCMAKE_INSTALL_BINDIR=${CMAKE_INSTALL_BINDIR}
    -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
    -DOPENVDB_BUILD_BINARIES:BOOL=OFF
    -DOPENVDB_BUILD_CORE:BOOL=ON
    -DOPENVDB_BUILD_DOCS:BOOL=OFF
    -DOPENVDB_BUILD_PYTHON_MODULE:BOOL=OFF
    -DOPENVDB_BUILD_UNITTESTS:BOOL=OFF
    -DOPENVDB_CODE_COVERAGE:BOOL=OFF
    -DIlmBase_ROOT=${ILMBASE_PATH}
    -DZLIB_ROOT=${ZLIB_PATH}
    -DBoost_ROOT=${BOOST_PATH}
    -DUSE_BLOSC:BOOL=${BUILD_BLOSC}
    -DCONCURRENT_MALLOC=None 
    -DDISABLE_CMAKE_SEARCH_PATHS:BOOL=ON
    $<$<BOOL:${BUILD_BLOSC}>:-DBlosc_ROOT=${BLOSC_PATH}>
    $<$<BOOL:${BUILD_TBB}>:-DTBB_ROOT=${TBB_PATH}>
    -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
    -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}
    ${OPENVKL_EXTRA_OPENVDB_OPTIONS}
  BUILD_COMMAND ${DEFAULT_BUILD_COMMAND}
  INSTALL_COMMAND ${CMAKE_COMMAND} --build . --target install --config ${CMAKE_BUILD_TYPE}
  BUILD_ALWAYS OFF
)

ExternalProject_Add_StepDependencies(openvdb
  configure
    boost
    ilmbase
    $<$<BOOL:${BUILD_BLOSC}>:c-blosc>
    $<$<BOOL:${BUILD_TBB}>:tbb>
)

set(OPENVDB_PATH ${COMPONENT_PATH})
