@@ -654,20 +654,33 @@ if(EXECUTORCH_BUILD_PYBIND)
654654 target_compile_options (portable_lib PUBLIC ${_pybind_compile_options} )
655655 target_link_libraries (portable_lib PRIVATE ${_dep_libs} )
656656
657- # Set RPATH to find PyTorch libraries relative to the installation location
658- # This goes from executorch/extension/pybindings up to site-packages, then to torch/lib
657+
658+ # Configure RPATH for portable_lib to find libtorch_python.so at runtime
659+ # Strategy: Use relative paths to find torch libraries in the user's environment
660+ # not the build environment's torch installation
661+
662+ # First, we need to override the global CMAKE_INSTALL_RPATH_USE_LINK_PATH for this target
663+ # to prevent build-time paths from being embedded
664+ set_target_properties (portable_lib PROPERTIES
665+ INSTALL_RPATH_USE_LINK_PATH OFF # Critical: Don't add build-time library paths
666+ )
667+
668+ # Set platform-specific relative paths
659669 if (APPLE )
670+ # macOS: use @loader_path for relative paths
671+ # @loader_path = directory containing the library itself
660672 set_target_properties (portable_lib PROPERTIES
661- BUILD_RPATH " @loader_path/../../../torch/lib"
662- INSTALL_RPATH "@loader_path/../../../torch/lib"
673+ INSTALL_RPATH "@loader_path; @loader_path/../../../torch/lib"
674+ BUILD_WITH_INSTALL_RPATH FALSE # Use normal build RPATH during build
663675 )
664676 else ()
677+ # Linux: use $ORIGIN for relative paths
678+ # $ORIGIN = directory containing the library itself
665679 set_target_properties (portable_lib PROPERTIES
666- BUILD_RPATH " $ORIGIN/../../../torch/lib"
667- INSTALL_RPATH "$ORIGIN/../../../torch/lib"
680+ INSTALL_RPATH " \$ ORIGIN; \ $ ORIGIN/../../../torch/lib"
681+ BUILD_WITH_INSTALL_RPATH FALSE # Use normal build RPATH during build
668682 )
669683 endif ()
670-
671684 install (TARGETS portable_lib
672685 LIBRARY DESTINATION executorch/extension/pybindings
673686 )
0 commit comments