Skip to content

Commit

Permalink
fetchcontent
Browse files Browse the repository at this point in the history
Open3D viewer, libopen3d, pybind, tf_ops and torch_ops work
  • Loading branch information
ssheorey committed Jul 27, 2024
1 parent e52a208 commit 8e20d19
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 122 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/find_dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ else(OPEN3D_USE_ONEAPI_PACKAGES)
endif()
if(NOT USE_SYSTEM_TBB)
include(${Open3D_3RDPARTY_DIR}/mkl/tbb.cmake)
list(APPEND Open3D_3RDPARTY_PRIVATE_TARGETS_FROM_CUSTOM Open3D::3rdparty_tbb)
list(APPEND Open3D_3RDPARTY_PRIVATE_TARGETS_FROM_CUSTOM TBB::tbb)
else()
list(APPEND Open3D_3RDPARTY_PRIVATE_TARGETS_FROM_SYSTEM Open3D::3rdparty_tbb)
endif()
Expand Down
56 changes: 19 additions & 37 deletions 3rdparty/mkl/tbb.cmake
Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
# TBB build scripts.

include(ExternalProject)
include(FetchContent)
cmake_policy(SET CMP0077 NEW)

# Where MKL and TBB headers and libs will be installed.
# This needs to be consistent with mkl.cmake.
set(MKL_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/mkl_install)
set(STATIC_MKL_INCLUDE_DIR "${MKL_INSTALL_PREFIX}/${Open3D_INSTALL_INCLUDE_DIR}/")
set(STATIC_MKL_LIB_DIR "${MKL_INSTALL_PREFIX}/${Open3D_INSTALL_LIB_DIR}")

# TBB variables exported for PyTorch Ops and TensorFlow Ops
set(TBB_INCLUDE_DIR "${STATIC_MKL_INCLUDE_DIR}")
set(TBB_LIB_DIR "${STATIC_MKL_LIB_DIR}")
set(TBB_RUNTIME_DIR "${MKL_INSTALL_PREFIX}/${Open3D_INSTALL_BIN_DIR}")
set(TBB_LIBRARIES tbb tbbmalloc)

ExternalProject_Add(
# Save and restore BUILD_SHARED_LIBS since TBB must be built as a shared library
set(__build_shared_libs ${BUILD_SHARED_LIBS})
set(BUILD_SHARED_LIBS ON)
FetchContent_Declare(
ext_tbb
PREFIX tbb
URL https://github.com/oneapi-src/oneTBB/archive/refs/tags/v2021.12.0.tar.gz # April 2024
URL_HASH SHA256=c7bb7aa69c254d91b8f0041a71c5bcc3936acb64408a1719aec0b2b7639dd84f
DOWNLOAD_DIR "${OPEN3D_THIRD_PARTY_DOWNLOAD_DIR}/tbb"
UPDATE_COMMAND ""
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${MKL_INSTALL_PREFIX}
-DSTATIC_WINDOWS_RUNTIME=${STATIC_WINDOWS_RUNTIME}
-DTBBMALLOC_BUILD=ON
-DTBBMALLOC_PROXY_BUILD=OFF
-DTBB_TEST=OFF
-DCMAKE_INSTALL_LIBDIR=${Open3D_INSTALL_LIB_DIR}
-DCMAKE_INSTALL_BINDIR=${Open3D_INSTALL_BIN_DIR}
${ExternalProject_CMAKE_ARGS}
BUILD_BYPRODUCTS
${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbb${CMAKE_SHARED_LIBRARY_SUFFIX}
${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbbmalloc${CMAKE_SHARED_LIBRARY_SUFFIX}
)
set(__TBB_BINARY_VERSION 12) # from tbb/version.h
set(TBBMALLOC_BUILD OFF CACHE BOOL "Enable tbbmalloc build.")
set(TBBMALLOC_PROXY_BUILD OFF CACHE BOOL "Enable tbbmalloc_proxy build.")
set(TBB_TEST OFF CACHE BOOL "Build TBB tests.")
set(TBB_INSTALL OFF CACHE BOOL "Enable installation")
set(TBB_STRICT OFF CACHE BOOL "Treat compiler warnings as errors")
FetchContent_MakeAvailable(ext_tbb)
set(BUILD_SHARED_LIBS ${__build_shared_libs})

# TBB is built and linked as a shared library - this is different from all other Open3D dependencies.
add_library(3rdparty_tbb INTERFACE)
target_include_directories(3rdparty_tbb SYSTEM INTERFACE $<BUILD_INTERFACE:${TBB_INCLUDE_DIR}>)
add_dependencies(3rdparty_tbb ext_tbb)
if (WIN32)
set(TBB_LIBRARIES_PATH "${TBB_LIB_DIR}/tbb${__TBB_BINARY_VERSION}.lib;${TBB_LIB_DIR}/tbbmalloc.lib")
set(TBB_RUNTIMES_PATH "${TBB_RUNTIME_DIR}/tbb${__TBB_BINARY_VERSION}.dll;${TBB_RUNTIME_DIR}/tbbmalloc.dll")
install(FILES ${TBB_RUNTIMES_PATH} DESTINATION ${Open3D_INSTALL_BIN_DIR})
else()
set(TBB_LIBRARIES_PATH "${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbb${CMAKE_SHARED_LIBRARY_SUFFIX};${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbbmalloc${CMAKE_SHARED_LIBRARY_SUFFIX}")
endif()
target_link_libraries(3rdparty_tbb INTERFACE ${TBB_LIBRARIES_PATH})
install(TARGETS 3rdparty_tbb EXPORT ${PROJECT_NAME}Targets)
install(FILES ${TBB_LIBRARIES_PATH} DESTINATION ${Open3D_INSTALL_LIB_DIR})
add_library(${PROJECT_NAME}::3rdparty_tbb ALIAS 3rdparty_tbb)
install(TARGETS tbb EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${Open3D_INSTALL_LIB_DIR}
COMPONENT tbb
RUNTIME DESTINATION ${Open3D_INSTALL_BIN_DIR}
COMPONENT tbb
)
53 changes: 53 additions & 0 deletions 3rdparty/mkl/tbb.externalcontent.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# TBB build scripts.

include(ExternalProject)

# Where MKL and TBB headers and libs will be installed.
# This needs to be consistent with mkl.cmake.
set(MKL_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/mkl_install)
set(STATIC_MKL_INCLUDE_DIR "${MKL_INSTALL_PREFIX}/${Open3D_INSTALL_INCLUDE_DIR}/")
set(STATIC_MKL_LIB_DIR "${MKL_INSTALL_PREFIX}/${Open3D_INSTALL_LIB_DIR}")

# TBB variables exported for PyTorch Ops and TensorFlow Ops
set(TBB_INCLUDE_DIR "${STATIC_MKL_INCLUDE_DIR}")
set(TBB_LIB_DIR "${STATIC_MKL_LIB_DIR}")
set(TBB_RUNTIME_DIR "${MKL_INSTALL_PREFIX}/${Open3D_INSTALL_BIN_DIR}")
set(TBB_LIBRARIES tbb tbbmalloc)

ExternalProject_Add(
ext_tbb
PREFIX tbb
URL https://github.com/oneapi-src/oneTBB/archive/refs/tags/v2021.12.0.tar.gz # April 2024
URL_HASH SHA256=c7bb7aa69c254d91b8f0041a71c5bcc3936acb64408a1719aec0b2b7639dd84f
DOWNLOAD_DIR "${OPEN3D_THIRD_PARTY_DOWNLOAD_DIR}/tbb"
UPDATE_COMMAND ""
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${MKL_INSTALL_PREFIX}
-DSTATIC_WINDOWS_RUNTIME=${STATIC_WINDOWS_RUNTIME}
-DTBBMALLOC_BUILD=ON
-DTBBMALLOC_PROXY_BUILD=OFF
-DTBB_TEST=OFF
-DCMAKE_INSTALL_LIBDIR=${Open3D_INSTALL_LIB_DIR}
-DCMAKE_INSTALL_BINDIR=${Open3D_INSTALL_BIN_DIR}
${ExternalProject_CMAKE_ARGS}
BUILD_BYPRODUCTS
${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbb${CMAKE_SHARED_LIBRARY_SUFFIX}
${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbbmalloc${CMAKE_SHARED_LIBRARY_SUFFIX}
)
set(__TBB_BINARY_VERSION 12) # from tbb/version.h

# TBB is built and linked as a shared library - this is different from all other Open3D dependencies.
add_library(3rdparty_tbb INTERFACE)
target_include_directories(3rdparty_tbb SYSTEM INTERFACE $<BUILD_INTERFACE:${TBB_INCLUDE_DIR}>)
add_dependencies(3rdparty_tbb ext_tbb)
if (WIN32)
set(TBB_LIBRARIES_PATH "${TBB_LIB_DIR}/tbb${__TBB_BINARY_VERSION}.lib;${TBB_LIB_DIR}/tbbmalloc.lib")
set(TBB_RUNTIMES_PATH "${TBB_RUNTIME_DIR}/tbb${__TBB_BINARY_VERSION}.dll;${TBB_RUNTIME_DIR}/tbbmalloc.dll")
install(FILES ${TBB_RUNTIMES_PATH} DESTINATION ${Open3D_INSTALL_BIN_DIR})
else()
set(TBB_LIBRARIES_PATH "${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbb${CMAKE_SHARED_LIBRARY_SUFFIX};${TBB_LIB_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}tbbmalloc${CMAKE_SHARED_LIBRARY_SUFFIX}")
endif()
target_link_libraries(3rdparty_tbb INTERFACE ${TBB_LIBRARIES_PATH})
install(TARGETS 3rdparty_tbb EXPORT ${PROJECT_NAME}Targets)
install(FILES ${TBB_LIBRARIES_PATH} DESTINATION ${Open3D_INSTALL_LIB_DIR})
add_library(${PROJECT_NAME}::3rdparty_tbb ALIAS 3rdparty_tbb)
33 changes: 0 additions & 33 deletions 3rdparty/mkl/tbb.fetccontent.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion cpp/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ macro(open3d_add_app_common SRC_DIR APP_NAME TARGET_NAME)
INSTALL_RPATH "$ORIGIN;$ORIGIN/../lib/")
endif()

target_link_libraries(${TARGET_NAME} PRIVATE Open3D::Open3D ${ARGN})
target_link_libraries(${TARGET_NAME} PRIVATE Open3D::Open3D TBB::tbb ${ARGN})
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "apps")

open3d_link_3rdparty_libraries(${TARGET_NAME})
Expand Down
69 changes: 34 additions & 35 deletions cpp/apps/fixup_macosx_bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@
# within the bundle.

if [[ $(uname) != "Darwin" ]]; then
echo "This script is only useful for macOS"
exit 1
echo "This script is only useful for macOS"
exit 1
fi

if [[ $# != 1 ]]; then
echo "Usage: $0 path/to/name.app"
exit 1
echo "Usage: $0 path/to/name.app"
exit 1
fi

# Find the path to the actual executable in the app bundle
appBundle=$1
exeDir="$appBundle/Contents/MacOS"
exe=$(find "$exeDir" -type f -perm +111 | grep -v dylib)
if [[ ! -f $exe ]]; then
echo "No executable file in app bundle ($appBundle/Contents/MacOS)"
exit 1
echo "No executable file in app bundle ($appBundle/Contents/MacOS)"
exit 1
fi

# Find the rpath paths
rpaths=$(otool -l "$exe" | grep "path " | awk '{print $2}')
if [[ $rpath != "" ]]; then
echo "@rpath:"
for rp in $rpaths; do
echo " $rp"
done
echo "@rpath:"
for rp in $rpaths; do
echo " $rp"
done
fi

# Set IFS so that newlines don't become spaces; helps parsing the otool -L output
Expand All @@ -41,35 +41,34 @@ IFS='
# Copy any external libraries and change the library paths to @executable_path
libs=$(otool -L "$exe" | grep -v "$exe" | grep -v /usr/lib | grep -v /System | awk '{ print $1; }')
for lib in $libs; do
if [[ ${lib:0:1} != "@" ]]; then # external library with a regular path
# copy the external library
cp -aL "$lib" "$exeDir"
if [[ ${lib:0:1} != "@" ]]; then # external library with a regular path
libname=$(basename $lib)
# copy the external library, resolve symlink chain
cp -aRL "$lib" "$exeDir/$lib"
# change its path in the executable
newpath="@executable_path/$libname"
echo "$lib -> $newpath"
install_name_tool -change "$lib" "$newpath" "$exe"

# change its path in the executable
libname=$(basename $lib)
newpath="@executable_path/$libname"
echo "$lib -> $newpath"
install_name_tool -change "$lib" "$newpath" "$exe"
elif [[ $lib == @rpath/* ]]; then # external library with @rpath
libname=${lib:7}
# copy the external library. Since it uses an rpath, we need to
# prepend each rpath to see which one gives a valid path
for rp in $rpaths; do
if [[ -f "$rp/$libname" ]]; then
cp -aRL "$rp/$libname" "$exeDir/$libname"
break
fi
done

elif [[ $lib == @rpath/* ]]; then # external library with @rpath
libname=${lib:7}
# copy the external library. Since it uses an rpath, we need to
# prepend each rpath to see which one gives a valid path
for rp in $rpaths; do
if [[ -f "$rp/$libname" ]]; then
cp -a "$rp/$libname" "$exeDir"
break
fi
done

# change its path in the executable
newpath="@executable_path/$libname"
echo "$lib -> $newpath"
install_name_tool -change "$lib" "$newpath" $exe
fi
# change its path in the executable
newpath="@executable_path/$libname"
echo "$lib -> $newpath"
install_name_tool -change "$lib" "$newpath" $exe
fi
done

# Remove rpaths
for rp in $rpaths; do
install_name_tool -delete_rpath "$rp" "$exe"
install_name_tool -delete_rpath "$rp" "$exe"
done
11 changes: 8 additions & 3 deletions cpp/open3d/ml/pytorch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ set_target_properties(open3d_torch_ops PROPERTIES
# Do not add "lib" prefix
set_target_properties(open3d_torch_ops PROPERTIES PREFIX "")
set_target_properties(open3d_torch_ops PROPERTIES DEBUG_POSTFIX "_debug")
# Set BUILD_RPATH to find tbb. We don't install through cmake.
if (APPLE)
set_target_properties(open3d_torch_ops PROPERTIES BUILD_RPATH "@loader_path/..")
elseif (UNIX)
set_target_properties(open3d_torch_ops PROPERTIES BUILD_RPATH "$ORIGIN/..")
endif()

target_include_directories(open3d_torch_ops SYSTEM PRIVATE
${PROJECT_SOURCE_DIR}/cpp
Expand All @@ -140,8 +146,7 @@ target_link_libraries(open3d_torch_ops PRIVATE
Open3D::3rdparty_eigen3
Open3D::3rdparty_fmt
Open3D::3rdparty_nanoflann
Open3D::3rdparty_parallelstl
Open3D::3rdparty_tbb
TBB::tbb
)

if (BUILD_CUDA_MODULE)
Expand Down Expand Up @@ -176,7 +181,7 @@ URL: @PROJECT_HOMEPAGE_URL@
Version: @PROJECT_VERSION@
Requires: Open3D = @PROJECT_VERSION@
Cflags:
Libs: -lopen3d_torch_ops]=] @ONLY NEWLINE_STYLE LF)
Libs: -lopen3d_torch_ops -ltbb]=] @ONLY NEWLINE_STYLE LF)
file(GENERATE OUTPUT open3d_torch_ops.pc INPUT
"${CMAKE_CURRENT_BINARY_DIR}/open3d_torch_ops.pc.in"
TARGET open3d_torch_ops)
Expand Down
17 changes: 8 additions & 9 deletions cpp/open3d/ml/tensorflow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ open3d_show_and_abort_on_warning(open3d_tf_ops)
open3d_enable_strip(open3d_tf_ops)
# do not use open3d_set_global_properties(open3d_tf_ops) here because some
# options are not compatible for tf op libraries
# RPATH handling (for TBB DSO). Check current folder, one folder above and the lib sibling folder
if (APPLE)
set_target_properties(open3d_tf_ops PROPERTIES INSTALL_RPATH "@loader_path;@loader_path/../;@loader_path/../lib/")
elseif(UNIX)
set_target_properties(open3d_tf_ops PROPERTIES INSTALL_RPATH "$ORIGIN;$ORIGIN/../;$ORIGIN/../lib/")
endif()

# Set output directory according to architecture (cpu/cuda)
get_target_property(TF_OPS_DIR open3d_tf_ops LIBRARY_OUTPUT_DIRECTORY)
Expand All @@ -141,6 +135,12 @@ set_target_properties(open3d_tf_ops PROPERTIES
# Do not add "lib" prefix
set_target_properties(open3d_tf_ops PROPERTIES PREFIX "")
set_target_properties(open3d_tf_ops PROPERTIES DEBUG_POSTFIX "_debug")
# Set BUILD_RPATH to find tbb. We don't install through cmake.
if (APPLE)
set_target_properties(open3d_tf_ops PROPERTIES BUILD_RPATH "@loader_path/..")
elseif (UNIX)
set_target_properties(open3d_tf_ops PROPERTIES BUILD_RPATH "$ORIGIN/..")
endif()

# _GLIBCXX_USER_CXX11_ABI is set separately
list(REMOVE_ITEM Tensorflow_DEFINITIONS "_GLIBCXX_USE_CXX11_ABI=0"
Expand All @@ -165,8 +165,7 @@ target_link_libraries(open3d_tf_ops PRIVATE
Open3D::Open3D
Open3D::3rdparty_fmt
Open3D::3rdparty_nanoflann
Open3D::3rdparty_parallelstl
Open3D::3rdparty_tbb
TBB::tbb
)

if (BUILD_CUDA_MODULE)
Expand Down Expand Up @@ -200,7 +199,7 @@ URL: @PROJECT_HOMEPAGE_URL@
Version: @PROJECT_VERSION@
Requires: Open3D = @PROJECT_VERSION@
Cflags:
Libs: -lopen3d_tf_ops]=] @ONLY NEWLINE_STYLE LF)
Libs: -lopen3d_tf_ops -ltbb]=] @ONLY NEWLINE_STYLE LF)
file(GENERATE OUTPUT open3d_tf_ops.pc INPUT
"${CMAKE_CURRENT_BINARY_DIR}/open3d_tf_ops.pc.in"
TARGET open3d_tf_ops)
Expand Down
11 changes: 9 additions & 2 deletions cpp/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,21 @@ endif()
# `build/lib/${CMAKE_BUILD_TYPE}/Python/{cpu|cuda}`
set(PYTHON_COMPILED_MODULE_DIR
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/Python/$<IF:$<BOOL:${BUILD_CUDA_MODULE}>,cuda,cpu>")

# Set BUILD_RPATH to find tbb. We don't install through cmake.
if (APPLE)
set_target_properties(pybind PROPERTIES BUILD_RPATH "@loader_path/..")
elseif (UNIX)
set_target_properties(pybind PROPERTIES BUILD_RPATH "$ORIGIN/..")
endif()
set_target_properties(pybind PROPERTIES
FOLDER "Python"
LIBRARY_OUTPUT_DIRECTORY "${PYTHON_COMPILED_MODULE_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${PYTHON_COMPILED_MODULE_DIR}")
# Include additional libraries that may be absent from the user system
# eg: libc++.so and libc++abi.so (needed by filament) for Linux.
# libc++.so is a linker script including libc++.so.1 and libc++abi.so, so append 1 to libc++.so
set(PYTHON_EXTRA_LIBRARIES ${TBB_LIBRARIES_PATH})
set(PYTHON_EXTRA_LIBRARIES $<TARGET_FILE:tbb>)
if (BUILD_GUI AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
list(APPEND PYTHON_EXTRA_LIBRARIES ${CPP_LIBRARY}.1 ${CPPABI_LIBRARY}
${MESA_CPU_GL_LIBRARY})
Expand All @@ -102,7 +109,7 @@ if (WITH_OPENMP AND APPLE AND NOT BUILD_SHARED_LIBS)
file(GENERATE OUTPUT update_pybind_libomp.sh
CONTENT [=[libomp_library=$(dyld_info -dependents "$<TARGET_FILE:pybind>" |
grep libomp | tr -d '[:space:]')
install_name_tool -change $libomp_library @loader_path/../libomp.dylib \
install_name_tool -change $libomp_library @rpath/$(basename $libomp_library) \
"$<TARGET_FILE:pybind>"]=])
add_custom_command(TARGET pybind POST_BUILD
COMMAND bash update_pybind_libomp.sh
Expand Down
Loading

0 comments on commit 8e20d19

Please sign in to comment.