From a6bde994bcb3c86475245f3dd01ed88fb6cb2a3a Mon Sep 17 00:00:00 2001
From: Ben Boeckel <ben.boeckel@kitware.com>
Date: Thu, 29 Apr 2021 15:07:05 -0400
Subject: [PATCH] wheels: add an rpath for Python modules in wheel builds

---
 CMakeLists.txt | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index d1465f42525..d0d437275f1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -378,6 +378,14 @@ if (VTK_WRAP_PYTHON)
   cmake_dependent_option(VTK_WINDOWS_PYTHON_DEBUGGABLE "Append `_d` to Python module names" OFF
     "WIN32;VTK_WRAP_PYTHON" OFF)
   mark_as_advanced(VTK_WINDOWS_PYTHON_DEBUGGABLE)
+
+  if (APPLE AND VTK_WHEEL_BUILD)
+    list(APPEND CMAKE_INSTALL_RPATH
+      # Wheels place loaded libraries under a `.dylibs` subdirectory. Add this
+      # to the rpath list.
+      "@loader_path/.dylibs")
+  endif ()
+
   vtk_module_wrap_python(
     MODULES         ${vtk_modules}
     INSTALL_EXPORT  VTKPython
@@ -392,6 +400,11 @@ if (VTK_WRAP_PYTHON)
     TARGET_SPECIFIC_COMPONENTS "${VTK_TARGET_SPECIFIC_COMPONENTS}"
     TARGET          VTK::vtkpythonmodules)
 
+  if (APPLE AND VTK_WHEEL_BUILD)
+    list(REMOVE_ITEM CMAKE_INSTALL_RPATH
+      "@loader_path/.dylibs")
+  endif ()
+
   add_subdirectory(Wrapping/Python)
 
   export(
-- 
GitLab