From 4a2388a06bd2611381bc899e4a5dff1053078228 Mon Sep 17 00:00:00 2001 From: Guillaume Paran Date: Wed, 20 Jan 2021 17:57:26 +0100 Subject: [PATCH 1/2] CLEAN SofaPython3Config.cmake.in --- SofaPython3Config.cmake.in | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/SofaPython3Config.cmake.in b/SofaPython3Config.cmake.in index 364460b5..636c55b6 100644 --- a/SofaPython3Config.cmake.in +++ b/SofaPython3Config.cmake.in @@ -5,26 +5,20 @@ set(SP3_WITH_SOFAEXPORTER @SP3_WITH_SOFAEXPORTER@) -if (SofaPython3_FIND_COMPONENTS) - foreach(component ${SofaPython3_FIND_COMPONENTS}) - if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/${component}Config.cmake") - # For requested component, execute its "config" script - set_and_check(config_file ${CMAKE_CURRENT_LIST_DIR}/${component}Config.cmake) - include(${config_file}) - set(SofaPython3_${component}_FOUND True) - else() - set(SofaPython3_FOUND False) - set(SofaPython3_NOT_FOUND_MESSAGE "Unsupported component: ${component}.") - endif() - endforeach() -else() - foreach(component Plugin Bindings) - # For all available components, execute its "config" script - set_and_check(config_file "${CMAKE_CURRENT_LIST_DIR}/${component}Config.cmake") - include(${config_file}) - set(Caribou_${component}_FOUND True) - endforeach() +if(NOT SofaPython3_FIND_COMPONENTS) + set(SofaPython3_FIND_COMPONENTS Plugin Bindings) endif() +foreach(component ${SofaPython3_FIND_COMPONENTS}) + if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${component}Config.cmake") + # For requested component, execute its "config" script + set_and_check(config_file ${CMAKE_CURRENT_LIST_DIR}/${component}Config.cmake) + include(${config_file}) + set(SofaPython3_${component}_FOUND True) + else() + set(SofaPython3_FOUND False) + set(SofaPython3_NOT_FOUND_MESSAGE "Unsupported component: ${component}.") + endif() +endforeach() # Check that the component/target is there. check_required_components(@PROJECT_NAME@) From fbcab5acc01c59b4ef8050a847463c9a9c1f7dfc Mon Sep 17 00:00:00 2001 From: Guillaume Paran Date: Wed, 20 Jan 2021 17:58:19 +0100 Subject: [PATCH 2/2] [cmake] Wider RPATH support for python modules --- CMake/SofaPython3Tools.cmake | 80 +++++++++++++++++++++--------------- CMakeLists.txt | 4 +- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/CMake/SofaPython3Tools.cmake b/CMake/SofaPython3Tools.cmake index 54d7a171..942573c6 100644 --- a/CMake/SofaPython3Tools.cmake +++ b/CMake/SofaPython3Tools.cmake @@ -189,6 +189,8 @@ function(SP3_add_python_module) LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" ) + sofa_get_target_dependencies(DEPENDS_ALL ${A_TARGET}) + # Compute the installation RPATHs from the target's SP3 dependencies since they are not installed in a same directory # and are not automatically added from the cmake option INSTALL_RPATH_USE_LINK_PATH. # 1. Get all dependencies that are @@ -198,18 +200,23 @@ function(SP3_add_python_module) # We compute its path relative to this target output file # Ex: ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/site-packages/Sofa --> $ORIGIN/../Sofa # 3. Add the relative path computed in 2 to the list of RPATHS - set(${A_TARGET}_DEPENDENCIES_RPATH "${CMAKE_INSTALL_PREFIX}/${LIBRARY_OUTPUT_DIRECTORY}") - foreach(DEPENDENCY ${A_DEPENDS}) - if (TARGET ${DEPENDENCY}) - get_target_property(DEPENDENCY_LIBRARY_OUTPUT_DIRECTORY "${DEPENDENCY}" LIBRARY_OUTPUT_DIRECTORY) - if (DEPENDENCY_LIBRARY_OUTPUT_DIRECTORY) - file(RELATIVE_PATH dependency_path_from_packages "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}" "${DEPENDENCY_LIBRARY_OUTPUT_DIRECTORY}") - if (NOT "${dependency_path_from_packages}" STREQUAL "" AND NOT "${dependency_path_from_packages}" STREQUAL "../") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$ORIGIN/../${dependency_path_from_packages}") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$$ORIGIN/../${dependency_path_from_packages}") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@loader_path/../${dependency_path_from_packages}") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@executable_path/../${dependency_path_from_packages}") - endif() + get_target_property(${A_TARGET}_DEPENDENCIES_RPATH ${A_TARGET} "INSTALL_RPATH") + foreach(DEPENDENCY ${DEPENDS_ALL}) + if(NOT TARGET ${DEPENDENCY}) + continue() + endif() + get_target_property(aliased_dep ${DEPENDENCY} ALIASED_TARGET) + if(aliased_dep) + set(DEPENDENCY ${aliased_dep}) + endif() + get_target_property(DEPENDENCY_LIBRARY_OUTPUT_DIRECTORY "${DEPENDENCY}" LIBRARY_OUTPUT_DIRECTORY) + if (DEPENDENCY_LIBRARY_OUTPUT_DIRECTORY) + file(RELATIVE_PATH dependency_path_from_packages "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}" "${DEPENDENCY_LIBRARY_OUTPUT_DIRECTORY}") + if (NOT "${dependency_path_from_packages}" STREQUAL "" AND NOT "${dependency_path_from_packages}" STREQUAL "../") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$ORIGIN/../${dependency_path_from_packages}") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$$ORIGIN/../${dependency_path_from_packages}") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@loader_path/../${dependency_path_from_packages}") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@executable_path/../${dependency_path_from_packages}") endif() endif() endforeach() @@ -227,28 +234,33 @@ function(SP3_add_python_module) # 1. First, compute the relative path from the current target towards the "plugins" relocatable directory of SOFA # 2. Append to the previous computed path the RELOCATABLE_INSTALL_DIR target property of the dependency since the # latter is formulated with respect to the "plugins" relocatable (e.g. "plugins/SofaBoundaryConditions/lib") - foreach(DEPENDENCY ${A_DEPENDS}) - if (TARGET ${DEPENDENCY}) - get_target_property(DEPENDENCY_RELOCATABLE_INSTALL_DIR "${DEPENDENCY}" RELOCATABLE_INSTALL_DIR) - if (DEPENDENCY_RELOCATABLE_INSTALL_DIR) - # Get the relative path from this binding module to the install lib directory - # For example, for lib/python3/site-packages/Sofa/Core.***.so, the relative path will be - # "../../.." - file(RELATIVE_PATH relative_towards_plugins_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") - # Here, we assume that the SP3 plugin will be installed in the SOFA plugins directory (i.e. $SOFA_ROOT/plugins) - # Hence, we need to compute the relative path from this plugins directory to the current binding - # modules, for example, plugins/SofaPython3/lib/python3/site-packages/Sofa/Core.***.so - # will become "../../../../../.." (three levels upper than the previous computed relative path) - set(relative_towards_plugins_dir "${relative_towards_plugins_dir}../../..") - - # Alright, now we have the path from the current target towards the "plugins" relocatable directory of SOFA - # We can compute the relative path from the current target towards the dependency relocatable path. - set(relative_towards_dependency_dir "${relative_towards_plugins_dir}/${DEPENDENCY_RELOCATABLE_INSTALL_DIR}") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$ORIGIN/${relative_towards_dependency_dir}/lib") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$$ORIGIN/${relative_towards_dependency_dir}/lib") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@loader_path/${relative_towards_dependency_dir}/lib") - list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@executable_path/${relative_towards_dependency_dir}/lib") - endif() + foreach(DEPENDENCY ${DEPENDS_ALL}) + if(NOT TARGET ${DEPENDENCY}) + continue() + endif() + get_target_property(aliased_dep ${DEPENDENCY} ALIASED_TARGET) + if(aliased_dep) + set(DEPENDENCY ${aliased_dep}) + endif() + get_target_property(DEPENDENCY_RELOCATABLE_INSTALL_DIR "${DEPENDENCY}" RELOCATABLE_INSTALL_DIR) + if (DEPENDENCY_RELOCATABLE_INSTALL_DIR) + # Get the relative path from this binding module to the install lib directory + # For example, for lib/python3/site-packages/Sofa/Core.***.so, the relative path will be + # "../../.." + file(RELATIVE_PATH relative_towards_plugins_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") + # Here, we assume that the SP3 plugin will be installed in the SOFA plugins directory (i.e. $SOFA_ROOT/plugins) + # Hence, we need to compute the relative path from this plugins directory to the current binding + # modules, for example, plugins/SofaPython3/lib/python3/site-packages/Sofa/Core.***.so + # will become "../../../../../.." (three levels upper than the previous computed relative path) + set(relative_towards_plugins_dir "${relative_towards_plugins_dir}../../..") + + # Alright, now we have the path from the current target towards the "plugins" relocatable directory of SOFA + # We can compute the relative path from the current target towards the dependency relocatable path. + set(relative_towards_dependency_dir "${relative_towards_plugins_dir}/${DEPENDENCY_RELOCATABLE_INSTALL_DIR}") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$ORIGIN/${relative_towards_dependency_dir}/lib") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "$$ORIGIN/${relative_towards_dependency_dir}/lib") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@loader_path/${relative_towards_dependency_dir}/lib") + list(APPEND ${A_TARGET}_DEPENDENCIES_RPATH "@executable_path/${relative_towards_dependency_dir}/lib") endif() endforeach() diff --git a/CMakeLists.txt b/CMakeLists.txt index 974bcbd5..54785b8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.12) project(SofaPython3 VERSION 20.12.0) +find_package(SofaFramework REQUIRED) + # Detect if SofaPython3 is a subproject of another project (eg. when compiled within Sofa) if (NOT "${CMAKE_PROJECT_NAME}" STREQUAL "${PROJECT_NAME}") set(SP3_COMPILED_AS_SUBPROJECT 1) @@ -159,8 +161,6 @@ if (APPLE) set(CMAKE_BUILD_RPATH "/Applications/Xcode.app/Contents/Developer/Library/Frameworks") endif() -find_package(SofaFramework REQUIRED) - if (NOT SP3_COMPILED_AS_SUBPROJECT) get_filename_component(SOFA_ROOT_DIR "${SofaFramework_DIR}/../../.." ABSOLUTE) message(STATUS "SOFA Framework:\n\tVersion: ${SofaFramework_VERSION}\n\tLocation: ${SOFA_ROOT_DIR}")