Skip to content

Commit 8c4eb48

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 f4e5086 commit 8c4eb48

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
@@ -573,6 +573,24 @@ if(EXECUTORCH_BUILD_PYBIND)
573573
${PYBIND_LINK_COREML}
574574
${PYBIND_LINK_MPS}
575575
${PYBIND_LINK_XNNPACK})
576+
if(APPLE)
577+
# pip wheels will need to be able to find the torch libraries. On Linux, the
578+
# .so has non-absolute dependencies on libs like "libtorch.so" without
579+
# paths; as long as we `import torch` first, those dependencies will work.
580+
# But Apple dylibs do not support non-absolute dependencies, so we need to
581+
# tell the loader where to look for its libraries. The LC_LOAD_DYLIB entries
582+
# for the torch libraries will look like "@rpath/libtorch.dylib", so we can
583+
# add an LC_RPATH entry to look in a directory relative to the installed
584+
# location of our _portable_lib.so file. To see these LC_* values, run
585+
# `otool -l _portable_lib*.so`.
586+
set_target_properties(
587+
portable_lib
588+
PROPERTIES # Assume that this library will be installed in
589+
# `site-packages/executorch/extension/pybindings`, and that
590+
# the torch libs are in `site-packages/torch/lib`.
591+
BUILD_RPATH "@loader_path/../../../torch/lib"
592+
INSTALL_RPATH "@loader_path/../../../torch/lib")
593+
endif()
576594

577595
install(TARGETS portable_lib
578596
LIBRARY DESTINATION executorch/extension/pybindings)

0 commit comments

Comments
 (0)