Skip to content

Commit c8722f7

Browse files
committed
Set the RPATH of _portable_lib.so so it can find libtorch
pip wheels will need to be able to find the torch libraries. On Linux, the .so has non-absolute dependencies on libs like "libtorch.so" without paths; as long as we `import torch` first, those dependencies will work. But Apple dylibs do not support non-absolute dependencies, so we need to tell the loader where to look for its libraries. The LC_LOAD_DYLIB entries for the torch libraries will look like "@rpath/libtorch.dylib", so we can add an LC_RPATH entry to look in a directory relative to the installed location of our _portable_lib.so file. To see these LC_* values, run `otool -l _portable_lib*.so`.
1 parent af23777 commit c8722f7

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,24 @@ if(EXECUTORCH_BUILD_PYBIND)
569569
${PYBIND_LINK_COREML}
570570
${PYBIND_LINK_MPS}
571571
${PYBIND_LINK_XNNPACK})
572+
if(APPLE)
573+
# pip wheels will need to be able to find the torch libraries. On Linux, the
574+
# .so has non-absolute dependencies on libs like "libtorch.so" without
575+
# paths; as long as we `import torch` first, those dependencies will work.
576+
# But Apple dylibs do not support non-absolute dependencies, so we need to
577+
# tell the loader where to look for its libraries. The LC_LOAD_DYLIB entries
578+
# for the torch libraries will look like "@rpath/libtorch.dylib", so we can
579+
# add an LC_RPATH entry to look in a directory relative to the installed
580+
# location of our _portable_lib.so file. To see these LC_* values, run
581+
# `otool -l _portable_lib*.so`.
582+
set_target_properties(
583+
portable_lib
584+
PROPERTIES # Assume that this library will be installed in
585+
# `site-packages/executorch/extension/pybindings`, and that
586+
# the torch libs are in `site-packages/torch/lib`.
587+
BUILD_RPATH "@loader_path/../../../torch/lib"
588+
INSTALL_RPATH "@loader_path/../../../torch/lib")
589+
endif()
572590

573591
install(TARGETS portable_lib
574592
LIBRARY DESTINATION executorch/extension/pybindings)

0 commit comments

Comments
 (0)