Broken links when building and installing VTK Python wrapper with Wheels
Context: I'm trying to build the VTK Python library from source (Master branch, 7bdac23f) on MacOS 12.5.
In my setup, I set the prefix/installation path of the library. For this to work I need to set the rpath explicitly, otherwise I would get an import error after installing the library, when loading the vtk package in Python. With the following commands, I can build VTK with Python wrapper successfully:
rm -rf build
mkdir build
cd build
VTK_INSTALL_DIR=path/to/install/dir
QT_DIR=path/to/dir/with/qmake
cmake -G Ninja .. -DCMAKE_BUILD_TYPE="Release" \ 130 ↵
-DVTK_WRAP_PYTHON="ON" \
-DBUILD_SHARED_LIBS="ON" \
-DVTK_BUILD_TESTING="OFF" \
-DVTK_BUILD_EXAMPLES="OFF" \
-DVTK_GROUP_ENABLE_Qt="YES" \
-DVTK_WHEEL_BUILD="OFF" \
-DBUILD_TESTING="OFF" \
-DCMAKE_PREFIX_PATH="$QT_DIR/.." \
-DCMAKE_INSTALL_PREFIX="$VTK_INSTALL_DIR" \
-DCMAKE_MACOSX_RPATH="ON" \
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH="ON" \
-DCMAKE_INSTALL_RPATH="$VTK_INSTALL_DIR/lib"
ninja
ninja install
export PYTHONPATH="$VTK_INSTALL_DIR/lib/python3.10/site-packages/":$PYTHONPATH
python -c "import vtk; print(vtk.vtkVersion().GetVTKVersion())"
# This runs successfully!
Problem
However, if enable option VTK_WHEEL_BUILD="ON"
, I run into problems after installation.
First, the folder structure of the install directory is broken. For instance, the include directory now resides inside the wheel data folder (in my case, this was: vtk-9.2.20221011.data). Second, when loading the library in Python, I get an import error with "Library not loaded": Library not loaded: '@rpath/libvtkWrappingPythonCore3.10.dylib' Referenced from: '/opt/pyenv/versions/3.10.5/lib/python3.10/site-packages/vtkmodules/vtkCommonCore.cpython-310-darwin.so
The cmake command is the same as above, just with VTK_WHEEL_BUILD="ON" changed.
cmake -G Ninja .. -DCMAKE_BUILD_TYPE="Release" \
-DVTK_WRAP_PYTHON="ON" \
-DBUILD_SHARED_LIBS="ON" \
-DVTK_BUILD_TESTING="OFF" \
-DVTK_BUILD_EXAMPLES="OFF" \
-DVTK_GROUP_ENABLE_Qt="YES" \
-DVTK_WHEEL_BUILD="ON" \
-DBUILD_TESTING="OFF" \
-DCMAKE_PREFIX_PATH="$QT_DIR/.." \
-DCMAKE_INSTALL_PREFIX="$VTK_INSTALL_DIR" \
-DCMAKE_MACOSX_RPATH="ON" \
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH="ON" \
-DCMAKE_INSTALL_RPATH="$VTK_INSTALL_DIR/lib"
ninja
python setup.py bdist_wheel
# python -m pip uninstall vtk
python -m pip install dist/*.whl --force-reinstall
The above runs successfully, but I get an error if I enter import vtk
in a Python console.
Workaround:
I can get rid of the import error if I comment out the following lines in the file CMake/vtkWheelPreparation.cmake:
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_NAME_DIR "@rpath")
list(APPEND CMAKE_INSTALL_RPATH
"@loader_path")
Then, everything works again. In other words: The rpath flags are overridden automatically when VTK_WHEEL_BUILD is set, which causes the problem observed.