Skip to content

Commit 0d93135

Browse files
author
zhangyue
committed
build(pr47): add torch.libs/ to rpath-link for bundled libgfortran
torch wheels on aarch64 (including `torch==2.9.0+cpu` used in the ascend CI container) are auditwheel-repaired and bundle transitive dependencies (`libgfortran-<hash>.so`, `libopenblasp-<hash>.so`) into a sibling `torch.libs/` directory. `torch.utils.cpp_extension.library_paths()` returns only `torch/lib`, so the linker cannot resolve the bundled NEEDED entries and fails with `undefined reference to _gfortran_etime@GFORTRAN_8`. Add `torch.libs/` to both the build and install rpath, plus `-rpath-link` for link-time resolution without polluting our final NEEDED list.
1 parent 006aafd commit 0d93135

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ if(WITH_TORCH)
137137
find_library(C10_LIB c10 HINTS ${_torch_lib_dirs} REQUIRED)
138138
set(TORCH_LIBRARIES ${TORCH_LIB} ${TORCH_CPU_LIB} ${C10_LIB})
139139

140+
# `auditwheel`-repaired `torch` wheels bundle transitive dependencies
141+
# (e.g. `libgfortran-<hash>.so`, `libopenblasp-<hash>.so`) in a sibling
142+
# `torch.libs/` directory that `library_paths()` does not return. When
143+
# building against such a wheel, the linker needs this path to resolve
144+
# the bundled NEEDED entries (otherwise: `undefined reference to
145+
# _gfortran_etime@GFORTRAN_8` etc.).
146+
execute_process(
147+
COMMAND ${Python_EXECUTABLE} -c "import os, torch; d = os.path.dirname(torch.__file__); p = os.path.join(os.path.dirname(d), 'torch.libs'); print(p if os.path.isdir(p) else '')"
148+
OUTPUT_VARIABLE TORCH_BUNDLED_LIBS_DIR
149+
OUTPUT_STRIP_TRAILING_WHITESPACE
150+
)
151+
152+
if(TORCH_BUNDLED_LIBS_DIR)
153+
list(APPEND CMAKE_BUILD_RPATH "${TORCH_BUNDLED_LIBS_DIR}")
154+
list(APPEND CMAKE_INSTALL_RPATH "${TORCH_BUNDLED_LIBS_DIR}")
155+
# `rpath-link` is linker-only: lets `ld` resolve the bundled
156+
# transitive NEEDED entries at link time without adding them to our
157+
# own binary's direct NEEDED list.
158+
add_link_options("-Wl,-rpath-link,${TORCH_BUNDLED_LIBS_DIR}")
159+
message(STATUS "PyTorch bundled libs: ${TORCH_BUNDLED_LIBS_DIR}")
160+
endif()
161+
140162
# Query the `CXX11` ABI setting that `torch` was compiled with.
141163
# A mismatch causes linker errors (e.g. undefined reference to
142164
# `c10::Device::Device(std::string const&)`).

0 commit comments

Comments
 (0)