Skip to content

Commit dedf77b

Browse files
authored
Fix shared library rpath once for all (#7096)
Test Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent 9b29b4b commit dedf77b

File tree

2 files changed

+16
-61
lines changed

2 files changed

+16
-61
lines changed

CMakeLists.txt

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,22 @@ if(EXECUTORCH_BUILD_PTHREADPOOL
682682
endif()
683683

684684
if(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

extension/llm/custom_ops/CMakeLists.txt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,5 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
109109
${_common_compile_options} -DET_USE_THREADPOOL
110110
)
111111

112-
# pip wheels will need to be able to find the dependent libraries. On Linux,
113-
# the .so has non-absolute dependencies on libs like "_portable_lib.so"
114-
# without paths; as long as we `import torch` first, those dependencies will
115-
# work. But Apple dylibs do not support non-absolute dependencies, so we need
116-
# to tell the loader where to look for its libraries. The LC_LOAD_DYLIB
117-
# entries for the portable_lib libraries will look like
118-
# "@rpath/_portable_lib.cpython-310-darwin.so", so we can add an LC_RPATH
119-
# entry to look in a directory relative to the installed location of our
120-
# _portable_lib.so file. To see these LC_* values, run `otool -l
121-
# libcustom_ops_aot_lib.dylib`.
122-
if(APPLE)
123-
set_target_properties(
124-
custom_ops_aot_lib
125-
PROPERTIES # Assume this library will be installed in
126-
# <site-packages>/executorch/extension/llm/custom_ops/, and the
127-
# _portable_lib.so is installed in
128-
# <site-packages>/executorch/extension/pybindings/
129-
BUILD_RPATH "@loader_path/../../pybindings"
130-
INSTALL_RPATH "@loader_path/../../pybindings"
131-
)
132-
endif()
133112
install(TARGETS custom_ops_aot_lib DESTINATION lib)
134113
endif()

0 commit comments

Comments
 (0)