From 4c546d0a167afb7aba400324a219bef553e0528b Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 2 Apr 2024 13:22:48 -0400 Subject: [PATCH] fix: set rpath for libtorch and protobuf (#3636) Fix #3568. Set run path for libtorch and protobuf. Refactor the original `TensorFlow_LIBRARY_PATH` to `BACKEND_LIBRARY_PATH` (list). --------- Signed-off-by: Jinzhe Zeng --- source/CMakeLists.txt | 13 ++++++++++++- source/api_c/CMakeLists.txt | 4 ++-- source/api_c/tests/CMakeLists.txt | 2 +- source/api_cc/CMakeLists.txt | 2 +- source/gmx/CMakeLists.txt | 2 +- source/ipi/CMakeLists.txt | 4 ++-- source/lmp/env.sh.in | 4 ++-- source/lmp/env_c.sh.in | 4 ++-- source/lmp/env_py.sh.in | 4 ++-- source/lmp/env_py_c.sh.in | 4 ++-- source/lmp/plugin/CMakeLists.txt | 2 +- source/op/tf/CMakeLists.txt | 5 ++--- 12 files changed, 30 insertions(+), 20 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 02f2993912..105a7c2695 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -151,6 +151,8 @@ endif() # find tensorflow, I need tf abi info if(ENABLE_TENSORFLOW AND NOT DEEPMD_C_ROOT) find_package(tensorflow REQUIRED) + list(APPEND BACKEND_LIBRARY_PATH ${TensorFlow_LIBRARY_PATH}) + list(APPEND BACKEND_INCLUDE_DIRS ${TENSORFLOW_INCLUDE_DIRS}) endif() if(BUILD_CPP_IF AND USE_PT_PYTHON_LIBS @@ -174,7 +176,10 @@ if(BUILD_CPP_IF "Cannot determine PyTorch CMake prefix path, error code: $PYTORCH_CMAKE_PREFIX_PATH_RESULT_VAR}, error message: ${PYTORCH_CMAKE_PREFIX_PATH_ERROR_VAR}" ) endif() - list(APPEND CMAKE_PREFIX_PATH ${PYTORCH_CMAKE_PREFIX_PATH}) + list(APPEND BACKEND_LIBRARY_PATH ${TensorFlow_LIBRARY_PATH}) + if(Protobuf_LIBRARY_DIRECTORY) + list(APPEND BACKEND_LIBRARY_PATH ${Protobuf_LIBRARY_DIRECTORY}) + endif() endif() if(ENABLE_PYTORCH AND NOT DEEPMD_C_ROOT) find_package(Torch REQUIRED) @@ -194,6 +199,10 @@ if(ENABLE_PYTORCH AND NOT DEEPMD_C_ROOT) add_definitions(-D_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}) endif() endif() + # get torch directory + set(PyTorch_LIBRARY_PATH ${TORCH_INCLUDE_DIRS}/../lib) + list(APPEND BACKEND_LIBRARY_PATH ${PyTorch_LIBRARY_PATH}) + list(APPEND BACKEND_INCLUDE_DIRS ${TORCH_INCLUDE_DIRS}) endif() # log enabled backends if(NOT DEEPMD_C_ROOT) @@ -297,7 +306,9 @@ if(DEEPMD_C_ROOT) # use variable for TF path to set deepmd_c path set(TENSORFLOW_ROOT "${DEEPMD_C_ROOT}") set(TensorFlow_LIBRARY_PATH "${DEEPMD_C_ROOT}/lib") + set(BACKEND_LIBRARY_PATH "${DEEPMD_C_ROOT}/lib") set(TENSORFLOW_INCLUDE_DIRS "${DEEPMD_C_ROOT}/include") + set(BACKEND_INCLUDE_DIRS "${DEEPMD_C_ROOT}/include") set(TORCH_LIBRARIES "${DEEPMD_C_ROOT}/lib/libtorch.so") endif() diff --git a/source/api_c/CMakeLists.txt b/source/api_c/CMakeLists.txt index f6e741105a..f0b36a62d7 100644 --- a/source/api_c/CMakeLists.txt +++ b/source/api_c/CMakeLists.txt @@ -12,8 +12,8 @@ target_include_directories( ${libname} PUBLIC $ $) -set_target_properties( - ${libname} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}") +set_target_properties(${libname} PROPERTIES INSTALL_RPATH + "$ORIGIN;${BACKEND_LIBRARY_PATH}") if(CMAKE_TESTING_ENABLED) target_link_libraries(${libname} PRIVATE coverage_config) endif() diff --git a/source/api_c/tests/CMakeLists.txt b/source/api_c/tests/CMakeLists.txt index d4233a83e8..1b035b1a6c 100644 --- a/source/api_c/tests/CMakeLists.txt +++ b/source/api_c/tests/CMakeLists.txt @@ -4,7 +4,7 @@ project(deepmd_api_c_test) file(GLOB TEST_SRC test_*.cc) set_target_properties( - ${LIB_DEEPMD_C} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}") + ${LIB_DEEPMD_C} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}") add_executable(runUnitTests_c ${TEST_SRC}) target_link_libraries(runUnitTests_c PRIVATE GTest::gtest_main ${LIB_DEEPMD_C} diff --git a/source/api_cc/CMakeLists.txt b/source/api_cc/CMakeLists.txt index 10a1dafca2..e2c889b3b4 100644 --- a/source/api_cc/CMakeLists.txt +++ b/source/api_cc/CMakeLists.txt @@ -34,7 +34,7 @@ endif() set_target_properties( ${libname} - PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}" + PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}" INSTALL_RPATH_USE_LINK_PATH TRUE BUILD_RPATH "$ORIGIN/../op/tf;$ORIGIN/../op/pt") target_compile_definitions(${libname} PRIVATE TF_PRIVATE) diff --git a/source/gmx/CMakeLists.txt b/source/gmx/CMakeLists.txt index d445479d39..8fde1e6ab8 100644 --- a/source/gmx/CMakeLists.txt +++ b/source/gmx/CMakeLists.txt @@ -26,7 +26,7 @@ target_include_directories(${libgmxname} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/) set_target_properties( - ${libgmxname} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}") + ${libgmxname} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}") install( FILES dp_gmx_patch diff --git a/source/ipi/CMakeLists.txt b/source/ipi/CMakeLists.txt index 5d571a1950..f728270814 100644 --- a/source/ipi/CMakeLists.txt +++ b/source/ipi/CMakeLists.txt @@ -44,13 +44,13 @@ if(APPLE) ${ipiname} PROPERTIES LINK_FLAGS "${extra_link_flags}" INSTALL_RPATH - "@loader_path/../${LIB_DIR}:${TensorFlow_LIBRARY_PATH}") + "@loader_path/../${LIB_DIR};${BACKEND_LIBRARY_PATH}") else() set_target_properties( ${ipiname} PROPERTIES LINK_FLAGS "-Wl,-rpath,'$ORIGIN'/../${LIB_DIR} ${extra_link_flags}" - INSTALL_RPATH "$ORIGIN/../${LIB_DIR}:${TensorFlow_LIBRARY_PATH}") + INSTALL_RPATH "$ORIGIN/../${LIB_DIR};${BACKEND_LIBRARY_PATH}") endif() if(CMAKE_TESTING_ENABLED) diff --git a/source/lmp/env.sh.in b/source/lmp/env.sh.in index 8c3a7aedc1..55b90be886 100644 --- a/source/lmp/env.sh.in +++ b/source/lmp/env.sh.in @@ -1,6 +1,6 @@ DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@ -TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@" -TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@" +TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@" +TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@" TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"` TF_LIBRARY_PATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -L/g"` diff --git a/source/lmp/env_c.sh.in b/source/lmp/env_c.sh.in index 67f6a63a1c..af010497ba 100644 --- a/source/lmp/env_c.sh.in +++ b/source/lmp/env_c.sh.in @@ -1,6 +1,6 @@ DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@ -TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@" -TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@" +TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@" +TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@" TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"` TF_LIBRARY_PATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -L/g"` diff --git a/source/lmp/env_py.sh.in b/source/lmp/env_py.sh.in index e6fc217c26..be48191f33 100644 --- a/source/lmp/env_py.sh.in +++ b/source/lmp/env_py.sh.in @@ -1,6 +1,6 @@ DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@ -TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@" -TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@" +TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@" +TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@" PYTHON_LIBRARY_PATH="@Python_LIBRARY_DIRS@" TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"` diff --git a/source/lmp/env_py_c.sh.in b/source/lmp/env_py_c.sh.in index b8affd7ff5..34874cc24c 100644 --- a/source/lmp/env_py_c.sh.in +++ b/source/lmp/env_py_c.sh.in @@ -1,6 +1,6 @@ DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@ -TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@" -TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@" +TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@" +TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@" PYTHON_LIBRARY_PATH="@Python_LIBRARY_DIRS@" TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"` diff --git a/source/lmp/plugin/CMakeLists.txt b/source/lmp/plugin/CMakeLists.txt index 4fdae7ac5b..f912059261 100644 --- a/source/lmp/plugin/CMakeLists.txt +++ b/source/lmp/plugin/CMakeLists.txt @@ -99,7 +99,7 @@ if(DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION) "-Wl,-undefined,dynamic_lookup") else() set_target_properties( - ${libname} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}" + ${libname} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}" LINK_FLAGS "-rdynamic") endif() target_compile_definitions( diff --git a/source/op/tf/CMakeLists.txt b/source/op/tf/CMakeLists.txt index 7a92e259e0..6fc6422c6e 100644 --- a/source/op/tf/CMakeLists.txt +++ b/source/op/tf/CMakeLists.txt @@ -62,11 +62,10 @@ endif() if(APPLE) set_target_properties( ${LIB_DEEPMD_OP} PROPERTIES INSTALL_RPATH - "@loader_path;${TensorFlow_LIBRARY_PATH}") + "@loader_path;${BACKEND_LIBRARY_PATH}") else() set_target_properties( - ${LIB_DEEPMD_OP} PROPERTIES INSTALL_RPATH - "$ORIGIN;${TensorFlow_LIBRARY_PATH}") + ${LIB_DEEPMD_OP} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}") endif() if(CMAKE_TESTING_ENABLED) target_link_libraries(${LIB_DEEPMD_OP} PRIVATE coverage_config)