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