Skip to content

Commit 266b9ef

Browse files
authored
Merge pull request #1571 from kmuseth/feature/pybind11
Feature/pybind11
2 parents d2f8d4a + 0b8c1f7 commit 266b9ef

21 files changed

+2059
-2635
lines changed

ci/build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ fi
160160

161161
################################################
162162

163+
###### TEMPORARY CHANGE: Install pybind11 2.10.0 as it's not available on the linux docker images yet
164+
if [ $(uname) == "Linux" ]; then
165+
if [ ! -f "/usr/local/include/pybind11.h" ]; then
166+
$CI_DIR/install_pybind11.sh 2.10.0
167+
fi
168+
fi
169+
###### TEMPORARY CHANGE: always install pybind11 2.10.0 as it's not available on the docker images yet
170+
171+
################################################
172+
163173
# github actions runners have 8 threads
164174
# https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners
165175
export CMAKE_BUILD_PARALLEL_LEVEL=${PARMS[-j]}

ci/install_macos.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ brew update
66
brew install bash gnu-getopt # for CI scripts
77
brew install cmake
88
brew install boost
9-
brew install boost-python3 # also installs the dependent python version
9+
brew install pybind11 # also installs the dependent python version
1010
brew install zlib
1111
brew install glfw
1212
brew install googletest
1313
brew install c-blosc
1414
brew install jq # for trivial parsing of brew json
1515

16-
# Alias python version installed by boost-python3 to path
17-
py_version=$(brew info boost-python3 --json | \
16+
# Alias python version installed by pybind11 to path
17+
py_version=$(brew info pybind11 --json | \
1818
jq -cr '.[].dependencies[] | select(. | startswith("python"))')
1919
echo "Using python $py_version"
2020
# export for subsequent action steps (note, not exported for this env)

ci/install_macos_ax.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ brew update
66
brew install bash gnu-getopt # for CI scripts
77
brew install cmake
88
brew install boost
9-
brew install boost-python3 # also installs the dependent python version
9+
brew install pybind11 # also installs the dependent python version
1010
brew install cppunit
1111
brew install c-blosc
1212
brew install zlib
1313
brew install jq # for trivial parsing of brew json
1414

15-
# Alias python version installed by boost-python3 to path
16-
py_version=$(brew info boost-python3 --json | \
15+
# Alias python version installed by pybind11 to path
16+
py_version=$(brew info pybind11 --json | \
1717
jq -cr '.[].dependencies[] | select(. | startswith("python"))')
1818
echo "Using python $py_version"
1919
# export for subsequent action steps (note, not exported for this env)

ci/install_pybind11.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
PYBIND11_VERSION="$1"
6+
INSTALL_ROOT="$2"
7+
CMAKE_EXTRA=()
8+
if [ ! -z "${INSTALL_ROOT}" ]; then
9+
CMAKE_EXTRA+=("-DCMAKE_INSTALL_PREFIX=${INSTALL_ROOT}")
10+
fi
11+
12+
git clone https://github.com/pybind/pybind11.git
13+
cd pybind11
14+
15+
if [ "$PYBIND11_VERSION" != "latest" ]; then
16+
git checkout tags/v${PYBIND11_VERSION} -b v${PYBIND11_VERSION}
17+
fi
18+
19+
mkdir build
20+
cd build
21+
22+
cmake \
23+
-DPYBIND11_TEST=OFF \
24+
"${CMAKE_EXTRA[@]}" \
25+
..
26+
27+
make -j8
28+
make install

ci/install_windows.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ set -ex
44

55
vcpkg update
66
vcpkg install zlib libpng openexr tbb gtest blosc glfw3 glew python3 \
7-
boost-iostreams boost-any boost-uuid boost-interprocess boost-algorithm boost-python \
7+
boost-iostreams boost-any boost-uuid boost-interprocess boost-algorithm pybind11 \
88
--clean-after-build

openvdb/openvdb/python/CMakeLists.txt

Lines changed: 4 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ set(OPENVDB_PYTHON_DEPS)
9595

9696
# @note explicitly only search for Development.Module from 3.18 as searching
9797
# Development.Embed can cause issues on linux systems where it doesn't exist
98-
set(OPENVDB_PYTHON_REQUIRED_COMPONENTS Development.Module)
98+
set(OPENVDB_PYTHON_REQUIRED_COMPONENTS Development.Module Interpreter)
9999

100100
if(NOT DEFINED PYOPENVDB_INSTALL_DIRECTORY)
101101
list(APPEND OPENVDB_PYTHON_REQUIRED_COMPONENTS Interpreter)
@@ -107,6 +107,7 @@ endif()
107107

108108
# Make sure find_package(Python) is only ever invoked once with all required components
109109
find_package(Python COMPONENTS ${OPENVDB_PYTHON_REQUIRED_COMPONENTS})
110+
find_package(pybind11 REQUIRED)
110111

111112
openvdb_check_python_version(Python::Module
112113
"${Python_VERSION}"
@@ -124,110 +125,13 @@ if(USE_NUMPY)
124125
list(APPEND OPENVDB_PYTHON_DEPS Python::NumPy)
125126
endif()
126127

127-
if(TARGET openvdb_shared AND NOT Boost_USE_STATIC_LIBS)
128-
# @note Both of these must be set for Boost 1.70 (VFX2020) to link against
129-
# boost shared libraries (more specifically libraries built with -fPIC).
130-
# http://boost.2283326.n4.nabble.com/CMake-config-scripts-broken-in-1-70-td4708957.html
131-
# https://github.com/boostorg/boost_install/commit/160c7cb2b2c720e74463865ef0454d4c4cd9ae7c
132-
set(BUILD_SHARED_LIBS ON)
133-
set(Boost_USE_STATIC_LIBS OFF)
134-
endif()
135-
136-
# Boost python cmake is a mess. Implementations provided by boost's config
137-
# cmake and kitware's module differ significantly and different cmake versions
138-
# also do slightly different things regarding the major/minor suffixing. We
139-
# previously attempted to handle all these cases by searching for boost_python
140-
# three times, prioritize the version suffixed library and falling back such:
141-
# - boost_python{Python_VERSION_MAJOR}${Python_VERSION_MINOR}
142-
# - boost_python{Python_VERSION_MAJOR}
143-
# - boost_python
144-
# This unfortunately fails as CMake sometimes creates an empty Boost::python
145-
# target on failed searches which stops subsequent searches. CMake also
146-
# sometimes just fails to find the suffixed versions regardless.
147-
#
148-
# Newer boost versions now seems to say that the suffixing is only required if
149-
# you have multiple boost installations, however the CMake implementation still
150-
# requires it. Boost specifically reads from Boost_PYTHON_VERSION and
151-
# Boost_PYTHON_VERSION_MAJOR variables. If the user has set these we just
152-
# search for the non suffixed versions. Note that users can also provide
153-
# Boost_NO_BOOST_CMAKE to skip the CMake implementation of FindBoost, though
154-
# this will impact other boost components.
155-
#
156-
# @todo just get rid of boost python and migrate to pybind asap.
157-
158-
set(_REQUIRED_BOOST_COMPONENTS python)
159-
if(USE_NUMPY)
160-
list(APPEND _REQUIRED_BOOST_COMPONENTS numpy)
161-
endif()
162-
163-
if(Boost_PYTHON_VERSION OR
164-
Boost_PYTHON_VERSION_MAJOR)
165-
# Search for non-suffixed boost libraries
166-
find_package(Boost ${MINIMUM_BOOST_VERSION} COMPONENTS ${_REQUIRED_BOOST_COMPONENTS} REQUIRED)
167-
else()
168-
# Try to find matching boost components to the version of python detected
169-
list(TRANSFORM _REQUIRED_BOOST_COMPONENTS APPEND ${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
170-
171-
# Explicitly don't pass REQUIRED so we can provide the user a message with
172-
# instructions to circumvent the major/minor requirement.
173-
find_package(Boost ${MINIMUM_BOOST_VERSION} COMPONENTS ${_REQUIRED_BOOST_COMPONENTS})
174-
175-
# See if we found the target. If we didn't and CMake/Boost created a broken
176-
# aliased target, we can't try another search. Build an error message and stop.
177-
set(_BOOST_ERROR "")
178-
if(NOT TARGET Boost::python${Python_VERSION_MAJOR}${Python_VERSION_MINOR} AND TARGET Boost::python)
179-
list(APPEND _BOOST_ERROR "boost_python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")
180-
endif()
181-
if(USE_NUMPY AND NOT TARGET Boost::numpy${Python_VERSION_MAJOR}${Python_VERSION_MINOR} AND TARGET Boost::numpy)
182-
list(APPEND _BOOST_ERROR "boost_numpy${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")
183-
endif()
184-
185-
if(_BOOST_ERROR)
186-
message(FATAL_ERROR "Unable to find versioned boost python libraries (${_BOOST_ERROR}). It's "
187-
"recommended that your installation of boost_python/boost_numpy match your python version "
188-
"exactly.\n"
189-
"Alternatively, you can try to search for boost python versions explicitly with either:\n"
190-
" 'Boost_PYTHON_VERSION=XY'\n"
191-
" 'Boost_PYTHON_VERSION_MAJOR=X'")
192-
endif()
193-
194-
# If we didn't create the target but CMake didn't create a broken alias, try another search
195-
if(NOT TARGET Boost::python${Python_VERSION_MAJOR}${Python_VERSION_MINOR} AND NOT TARGET Boost::python)
196-
find_package(Boost ${MINIMUM_BOOST_VERSION} COMPONENTS python REQUIRED)
197-
message(STATUS "Found non-suffixed boost_python, assuming to be python version "
198-
"\"${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}\" compatible")
199-
endif()
200-
if(USE_NUMPY AND
201-
NOT TARGET Boost::numpy${Python_VERSION_MAJOR}${Python_VERSION_MINOR} AND NOT TARGET Boost::numpy)
202-
find_package(Boost ${MINIMUM_BOOST_VERSION} COMPONENTS numpy REQUIRED)
203-
message(STATUS "Found non-suffixed boost_numpy, assuming to be python version "
204-
"\"${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}\" compatible")
205-
endif()
206-
endif()
207-
208-
if(TARGET Boost::python${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
209-
list(APPEND OPENVDB_PYTHON_DEPS Boost::python${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
210-
elseif(TARGET Boost::python)
211-
list(APPEND OPENVDB_PYTHON_DEPS Boost::python)
212-
message(STATUS "Found non-suffixed boost_python, assuming to be python version "
213-
"\"${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}\" compatible")
214-
endif()
215-
216-
if(TARGET Boost::numpy${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
217-
list(APPEND OPENVDB_PYTHON_DEPS Boost::numpy${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
218-
elseif(TARGET Boost::numpy)
219-
list(APPEND OPENVDB_PYTHON_DEPS Boost::numpy)
220-
message(STATUS "Found non-suffixed boost_numpy, assuming to be python version "
221-
"\"${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}\" compatible")
222-
endif()
223-
224128
##########################################################################
225129

226130
set(OPENVDB_PYTHON_MODULE_SOURCE_FILES
227131
pyFloatGrid.cc
132+
pyGridBase.cc
228133
pyIntGrid.cc
229134
pyMetadata.cc
230-
pyPointGrid.cc
231135
pyOpenVDBModule.cc
232136
pyPointGrid.cc
233137
pyTransform.cc
@@ -242,21 +146,7 @@ if(NOT DEFINED PYOPENVDB_INSTALL_DIRECTORY)
242146
)
243147
endif()
244148

245-
# @todo pyopenvdb is currently advertised as "linkable" by extension libraries
246-
# so we have to mark it as SHARED. Really it should be MODULE, as the
247-
# amount of manipulation required for native python support nullifies
248-
# the ability for compilers to link against it (suffix/prefix renaming).
249-
# A proper shared lib should be built with the functions required for
250-
# extension (pyopenvdb.h) and a further module lib should be added
251-
# that can be imported.
252-
253-
add_library(pyopenvdb SHARED ${OPENVDB_PYTHON_MODULE_SOURCE_FILES})
254-
set_target_properties(pyopenvdb PROPERTIES PREFIX "") # no 'lib' prefix
255-
if(UNIX)
256-
set_target_properties(pyopenvdb PROPERTIES SUFFIX ".so") # must be .so (not .dylib)
257-
elseif(WIN32)
258-
set_target_properties(pyopenvdb PROPERTIES SUFFIX ".pyd") # .pyd on windows
259-
endif()
149+
pybind11_add_module(pyopenvdb ${OPENVDB_PYTHON_MODULE_SOURCE_FILES})
260150

261151
target_link_libraries(pyopenvdb PUBLIC
262152
${OPENVDB_LIB}
@@ -274,44 +164,12 @@ if(USE_AX)
274164
target_compile_definitions(pyopenvdb PUBLIC "-DPY_OPENVDB_USE_AX")
275165
endif()
276166

277-
# Configure static boost linkage
278-
# @note This is usually undesirable. pyopenvdb should ideally be built
279-
# against dynamic libs/runtime
280-
# @note For some reason vcpkg decides to turn Boost_USE_STATIC_LIBS off
281-
# when calling find_package(Boost), so we have to manually query the
282-
# triplet to see if we're using boost static libs.
283-
set(_BUILDING_STATIC_TRIPLET OFF)
284-
if(VCPKG_TARGET_TRIPLET)
285-
string(FIND ${VCPKG_TARGET_TRIPLET} "static" _BUILDING_STATIC_TRIPLET)
286-
if(NOT (_BUILDING_STATIC_TRIPLET EQUAL -1))
287-
set(_BUILDING_STATIC_TRIPLET ON)
288-
else()
289-
set(_BUILDING_STATIC_TRIPLET OFF)
290-
endif()
291-
endif()
292-
293-
if(Boost_USE_STATIC_LIBS OR _BUILDING_STATIC_TRIPLET)
294-
message(WARNING "Building pyopenvdb against static variants of python/boost.python is not recommended.")
295-
# Boost CMake should define these but doesn't...
296-
target_compile_definitions(pyopenvdb PUBLIC "-DBOOST_PYTHON_STATIC_LIB")
297-
if(USE_NUMPY)
298-
target_compile_definitions(pyopenvdb PUBLIC "-DBOOST_NUMPY_STATIC_LIB")
299-
endif()
300-
endif()
301-
unset(_BUILDING_STATIC_TRIPLET)
302-
303-
set(PYTHON_PUBLIC_INCLUDE_NAMES
304-
pyopenvdb.h
305-
)
306-
307167
install(TARGETS
308168
pyopenvdb
309169
DESTINATION
310170
${PYOPENVDB_INSTALL_DIRECTORY}
311171
)
312172

313-
install(FILES ${PYTHON_PUBLIC_INCLUDE_NAMES} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/openvdb/python)
314-
315173
# pytest
316174
if(OPENVDB_BUILD_PYTHON_UNITTESTS)
317175

0 commit comments

Comments
 (0)