@@ -682,6 +682,22 @@ if(EXECUTORCH_BUILD_PTHREADPOOL
682682endif ()
683683
684684if (EXECUTORCH_BUILD_PYBIND)
685+   # Setup RPATH. 
686+   # See https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling 
687+   if (APPLE )
688+     set (CMAKE_MACOSX_RPATH ON )
689+     set (_rpath_portable_origin "@loader_path" )
690+   else ()
691+     set (_rpath_portable_origin $ORIGIN)
692+   endif (APPLE )
693+   # Use separate rpaths during build and install phases 
694+   set (CMAKE_SKIP_BUILD_RPATH   FALSE )
695+   # Don't use the install-rpath during the build phase 
696+   set (CMAKE_BUILD_WITH_INSTALL_RPATH  FALSE )
697+   set (CMAKE_INSTALL_RPATH  "${_rpath_portable_origin} " )
698+   # Automatically add all linked folders that are NOT in the build directory to 
699+   # the rpath (per library?) 
700+   set (CMAKE_INSTALL_RPATH_USE_LINK_PATH  TRUE )
685701  add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /third-party/pybind11)
686702
687703  if (NOT  EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)
@@ -765,46 +781,6 @@ if(EXECUTORCH_BUILD_PYBIND)
765781  target_include_directories (portable_lib PRIVATE  ${TORCH_INCLUDE_DIRS} )
766782  target_compile_options (portable_lib PUBLIC  ${_pybind_compile_options} )
767783  target_link_libraries (portable_lib PRIVATE  ${_dep_libs} )
768-   if (APPLE )
769-     # pip wheels will need to be able to find the torch libraries. On Linux, the 
770-     # .so has non-absolute dependencies on libs like "libtorch.so" without 
771-     # paths; as long as we `import torch` first, those dependencies will work. 
772-     # But Apple dylibs do not support non-absolute dependencies, so we need to 
773-     # tell the loader where to look for its libraries. The LC_LOAD_DYLIB entries 
774-     # for the torch libraries will look like "@rpath/libtorch.dylib", so we can 
775-     # add an LC_RPATH entry to look in a directory relative to the installed 
776-     # location of our _portable_lib.so file. To see these LC_* values, run 
777-     # `otool -l _portable_lib*.so`. 
778-     set_target_properties (
779-       portable_lib
780-       PROPERTIES # Assume that this library will be installed in 
781-                  # `site-packages/executorch/extension/pybindings`, and that 
782-                  # the torch libs are in `site-packages/torch/lib`. 
783-                  BUILD_RPATH "@loader_path/../../../torch/lib" 
784-                  INSTALL_RPATH  "@loader_path/../../../torch/lib" 
785-                  # Assume <executorch> is the root `site-packages/executorch` 
786-                  # Need to add <executorch>/extension/llm/custom_ops for 
787-                  # libcustom_ops_aot_lib.dylib 
788-                  BUILD_RPATH "@loader_path/../../extension/llm/custom_ops" 
789-                  INSTALL_RPATH  "@loader_path/../../extension/llm/custom_ops" 
790-                  # Need to add <executorch>/kernels/quantized for 
791-                  # libquantized_ops_aot_lib.dylib 
792-                  BUILD_RPATH "@loader_path/../../kernels/quantized" 
793-                  INSTALL_RPATH  "@loader_path/../../kernels/quantized" 
794-     )
795-   else ()
796-     set_target_properties (
797-       portable_lib
798-       PROPERTIES
799-         # Assume <executorch> is the root `site-packages/executorch` 
800-         # Need to add <executorch>/extension/llm/custom_ops for 
801-         # libcustom_ops_aot_lib 
802-         # Need to add <executorch>/kernels/quantized for 
803-         # libquantized_ops_aot_lib 
804-         BUILD_RPATH
805-         "$ORIGIN:$ORIGIN/../../extension/llm/custom_ops:$ORIGIN/../../kernels/quantized" 
806-     )
807-   endif ()
808784
809785  install (TARGETS portable_lib
810786          LIBRARY DESTINATION  executorch/extension/pybindings
0 commit comments