Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions CMake/SofaPython3Tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function(SP3_add_python_package)
get_filename_component(relative_directory ${file_relative_path} DIRECTORY)
install(
FILES "${OUTPUT_DIRECTORY}/${file_relative_path}"
DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${A_TARGET_DIRECTORY}/${relative_directory}"
DESTINATION "lib/${SP3_PYTHON_PACKAGES_DIRECTORY}/${A_TARGET_DIRECTORY}/${relative_directory}"
)
endforeach()

Expand Down Expand Up @@ -171,9 +171,35 @@ function(SP3_add_python_module)

if (APPLE)
# In MacOS, the target dependency name is RPATH/site-packages/PackageName, so we need to add
# an RPATH to the directory that contains "site-paclages"
# an RPATH to the directory that contains "site-packages"
list(APPEND ${A_TARGET}_DEPENDECIES_RPATH "$ORIGIN/../..")
endif()

# Compute the installation RPATHs from the target's SOFA relocatable dependencies
# 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}_DEPENDECIES_RPATH "$ORIGIN/${relative_towards_dependency_dir}/lib")
endif()
endif()
endforeach()

set_target_properties(
${A_TARGET}
Expand Down Expand Up @@ -220,9 +246,9 @@ function(SP3_add_python_module)

install(TARGETS ${A_TARGET}
EXPORT BindingsTargets
RUNTIME DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" COMPONENT applications
LIBRARY DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" COMPONENT libraries
ARCHIVE DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" COMPONENT libraries
RUNTIME DESTINATION "lib/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" COMPONENT applications
LIBRARY DESTINATION "lib/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" COMPONENT libraries
ARCHIVE DESTINATION "lib/${SP3_PYTHON_PACKAGES_DIRECTORY}/${DESTINATION}" COMPONENT libraries
)

foreach(header ${A_HEADERS})
Expand Down
44 changes: 13 additions & 31 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
cmake_minimum_required(VERSION 3.12)

project(SofaPython3)

# Manually define VERSION
set(SOFAPYTHON3_VERSION_MAJOR 20)
set(SOFAPYTHON3_VERSION_MINOR 12)
set(SOFAPYTHON3_VERSION_PATCH 0)
set(SOFAPYTHON3_VERSION ${SOFAPYTHON3_VERSION_MAJOR}.${SOFAPYTHON3_VERSION_MINOR}.${SOFAPYTHON3_VERSION_PATCH})
project(SofaPython3 VERSION 20.12.0)

# Detect if SofaPython3 is a subproject of another project (eg. when compiled within Sofa)
if (NOT "${CMAKE_PROJECT_NAME}" STREQUAL "${PROJECT_NAME}")
Expand All @@ -31,16 +25,17 @@ if (SP3_COMPILED_AS_SOFA_SUBPROJECT)
endif()
endif()

# CMAKE TOOLS
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
include(SofaPython3Tools)

# SofaPython3 cannot be compiled alongside SofaPython(2)
if (SP3_COMPILED_AS_SOFA_SUBPROJECT)
if(PLUGIN_SOFAPYTHON)
message(FATAL_ERROR "SofaPython3 cannot be built alongside SofaPython. Please set PLUGIN_SOFAPYTHON to OFF")
endif()
endif()

# CMAKE TOOLS
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
include(SofaPython3Tools)

# OPTIONS
include(CMakeDependentOption)

Expand Down Expand Up @@ -173,24 +168,11 @@ SP3_add_python_package(
Sofa/constants
)

configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/SofaPython3Config.cmake.in"
"${CMAKE_BINARY_DIR}/cmake/SofaPython3/SofaPython3Config.cmake"
INSTALL_DESTINATION
"${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3"
)
write_basic_package_version_file(
${CMAKE_BINARY_DIR}/cmake/SofaPython3/SofaPython3ConfigVersion.cmake
VERSION ${SOFAPYTHON3_VERSION}
COMPATIBILITY AnyNewerVersion
)

install(FILES
"${CMAKE_BINARY_DIR}/cmake/SofaPython3/SofaPython3Config.cmake"
"${CMAKE_BINARY_DIR}/cmake/SofaPython3/SofaPython3ConfigVersion.cmake"
DESTINATION
"${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3"
)
sofa_create_package(
PACKAGE_NAME ${PROJECT_NAME}
PACKAGE_VERSION ${PROJECT_VERSION}
RELOCATABLE "plugins"
)

if (SP3_LINK_TO_USER_SITE AND SP3_PYTHON_PACKAGES_LINK_DIRECTORY)
file(GLOB directories RELATIVE "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}" "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/*")
Expand All @@ -202,15 +184,15 @@ if (SP3_LINK_TO_USER_SITE AND SP3_PYTHON_PACKAGES_LINK_DIRECTORY)
install(CODE "\
execute_process( \
COMMAND ${CMAKE_COMMAND} -E copy_directory \
${CMAKE_INSTALL_PREFIX}/${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${directory}/ \
${CMAKE_INSTALL_PREFIX}/lib/${SP3_PYTHON_PACKAGES_DIRECTORY}/${directory}/ \
${SP3_PYTHON_PACKAGES_LINK_DIRECTORY}/${directory} \
)"
)
else()
install(CODE "\
execute_process( \
COMMAND ${CMAKE_COMMAND} -E create_symlink \
${CMAKE_INSTALL_PREFIX}/${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${directory}/ \
${CMAKE_INSTALL_PREFIX}/lib/${SP3_PYTHON_PACKAGES_DIRECTORY}/${directory}/ \
${SP3_PYTHON_PACKAGES_LINK_DIRECTORY}/${directory} \
)"
)
Expand Down
55 changes: 11 additions & 44 deletions Plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,12 @@ find_package(SofaSimulation REQUIRED)
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})
add_library(SofaPython3::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX "_d")
target_compile_definitions(${PROJECT_NAME} PRIVATE "-DSOFA_BUILD_SOFAPYTHON3")
target_compile_definitions(${PROJECT_NAME} PUBLIC "-DSOFA_HAVE_SOFAPYTHON3")

target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/>")
target_include_directories(${PROJECT_NAME} PUBLIC "$<INSTALL_INTERFACE:include>")

target_link_libraries(${PROJECT_NAME} PUBLIC SofaCore SofaDefaultType SofaSimulationCore SofaSimulationGraph SofaHelper)
target_link_libraries(${PROJECT_NAME} PUBLIC pybind11::module pybind11::embed)
target_link_libraries(${PROJECT_NAME} PUBLIC pybind11::module pybind11::embed)

set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME SofaPython3)
set_target_properties(${target} PROPERTIES VERSION "${SOFAPYTHON3_VERSION}")

set_target_properties(
${PROJECT_NAME}
PROPERTIES
INSTALL_RPATH_USE_LINK_PATH TRUE
)

if (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
# Sized deallocaion is not enabled by default under clang after c++14
Expand All @@ -84,34 +72,13 @@ set_target_properties(
CUDA_VISIBILITY_PRESET "hidden"
)

# Install header files
foreach(header ${HEADER_FILES})
file(RELATIVE_PATH relative_path "${CMAKE_CURRENT_SOURCE_DIR}/src" "${header}")
get_filename_component(relative_directory ${relative_path} DIRECTORY)

install(FILES "${header}" DESTINATION "include/${relative_directory}" COMPONENT headers)
endforeach()

configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/PluginConfig.cmake.in"
"${CMAKE_BINARY_DIR}/cmake/SofaPython3/PluginConfig.cmake"
INSTALL_DESTINATION
"${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3"
)

install(FILES "${CMAKE_BINARY_DIR}/cmake/SofaPython3/PluginConfig.cmake" DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3")

install(TARGETS ${PROJECT_NAME}
EXPORT PluginTargets
RUNTIME DESTINATION "${RUNTIME_OUTPUT_DIRECTORY}" COMPONENT applications
LIBRARY DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}" COMPONENT libraries
ARCHIVE DESTINATION "${ARCHIVE_OUTPUT_DIRECTORY}" COMPONENT libraries
)

install(
EXPORT PluginTargets
NAMESPACE SofaPython3::
DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3"
COMPONENT headers
)

sofa_create_component_in_package_with_targets(
COMPONENT_NAME ${PROJECT_NAME}
COMPONENT_VERSION ${SofaPython3_VERSION}
PACKAGE_NAME SofaPython3
TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES
INCLUDE_SOURCE_DIR "src"
INCLUDE_INSTALL_DIR "."
OPTIMIZE_BUILD_DIR FALSE
RELOCATABLE ".."
)
5 changes: 5 additions & 0 deletions Plugin/src/SofaPython3/PythonTestExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <sofa/helper/StringUtils.h>
#include <sofa/helper/system/SetDirectory.h>

#include <sofa/helper/system/PluginManager.h>
using sofa::helper::system::PluginManager;

using sofa::helper::system::SetDirectory;
namespace py = pybind11;

Expand Down Expand Up @@ -45,6 +48,8 @@ py::object PythonTestExtractor::getTestSuite(py::module& unittest, py::module& m

std::vector<PythonTestData> PythonTestExtractor::extract () const
{
PluginManager::getInstance().loadPlugin("SofaPython3");

PythonEnvironment::Init();
PythonEnvironment::gil scoped_gil;

Expand Down
70 changes: 35 additions & 35 deletions bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
project(Bindings)

set(BINDINGS_MODULE_LIST Sofa SofaRuntime SofaGui SofaTypes Modules)
if(SP3_WITH_SOFAEXPORTER)
list(APPEND BINDINGS_MODULE_LIST SofaExporter)
endif()

# This will set rpaths relative to SP3 plugin library
if(UNIX)
set(CMAKE_INSTALL_RPATH
"$ORIGIN/../lib"
"$$ORIGIN/../lib"
)
if(APPLE)
set(CMAKE_MACOSX_RPATH ON)
list(APPEND CMAKE_INSTALL_RPATH
"@loader_path/../lib"
"@executable_path/../lib"
)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif()
endif()

foreach(bindings_module ${BINDINGS_MODULE_LIST})
add_subdirectory(${bindings_module})
endforeach()

if (SP3_WITH_SOFAEXPORTER)
add_subdirectory(SofaExporter)
endif()

add_library(${PROJECT_NAME} INTERFACE)
add_library(SofaPython3::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

foreach(bindings_module ${BINDINGS_MODULE_LIST})
target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.${bindings_module})
endforeach()

if (SP3_WITH_SOFAEXPORTER)
target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.SofaExporter)
endif()

configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/BindingsConfig.cmake.in"
"${CMAKE_BINARY_DIR}/cmake/SofaPython3/BindingsConfig.cmake"
INSTALL_DESTINATION
"${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3"
)

install(FILES "${CMAKE_BINARY_DIR}/cmake/SofaPython3/BindingsConfig.cmake" DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3")

install(TARGETS ${PROJECT_NAME} EXPORT BindingsTargets)

install(
EXPORT BindingsTargets
NAMESPACE SofaPython3::
DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/cmake/SofaPython3"
COMPONENT headers
)
sofa_create_component_in_package_with_targets(
COMPONENT_NAME ${PROJECT_NAME}
COMPONENT_VERSION ${SofaPython3_VERSION}
PACKAGE_NAME SofaPython3
TARGETS ${PROJECT_NAME}
)

if (SP3_COMPILED_AS_SOFA_SUBPROJECT)
## Python configuration file (build tree), the lib in the source dir (easier while developping .py files)
file(WRITE "${CMAKE_BINARY_DIR}/etc/sofa/python.d/plugin.SofaPython3.bindings" "${CMAKE_BINARY_DIR}/${SP3_PYTHON_PACKAGES_DIRECTORY}\n")
#if (SP3_COMPILED_AS_SOFA_SUBPROJECT)
# ## Python configuration file (build tree), the lib in the source dir (easier while developping .py files)
# file(WRITE "${CMAKE_BINARY_DIR}/etc/sofa/python.d/plugin.SofaPython3.bindings" "${CMAKE_BINARY_DIR}/${SP3_PYTHON_PACKAGES_DIRECTORY}\n")

## Python configuration file (install tree)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installed-bindings-config"
"${CMAKE_INSTALL_PREFIX}/${SP3_PYTHON_PACKAGES_DIRECTORY}\n")
# ## Python configuration file (install tree)
# file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installed-bindings-config"
# "${CMAKE_INSTALL_PREFIX}/${SP3_PYTHON_PACKAGES_DIRECTORY}\n")


install(FILES "${CMAKE_CURRENT_BINARY_DIR}/installed-bindings-config"
DESTINATION "etc/sofa/python.d"
RENAME "plugin.SofaPython3.bindings")
endif()
# install(FILES "${CMAKE_CURRENT_BINARY_DIR}/installed-bindings-config"
# DESTINATION "etc/sofa/python.d"
# RENAME "plugin.SofaPython3.bindings")
#endif()
16 changes: 5 additions & 11 deletions bindings/Modules/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ find_package(SofaFramework REQUIRED)

enable_testing()
add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${PYTHON_FILES})
target_link_libraries(${PROJECT_NAME} SofaGTestMain SofaHelper SofaPython3::Plugin SofaPython3::Bindings.Modules)
target_link_libraries(${PROJECT_NAME} SofaGTestMain SofaHelper SofaPython3::Plugin)
target_compile_definitions(${PROJECT_NAME} PRIVATE "PYTHON_TESTFILES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/\"")
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Bindings/Tests")

set(RPATH "$ORIGIN/../lib")
if (APPLE)
list(APPEND RPATH "@executable_path/../lib")
endif()

set_target_properties(
${PROJECT_NAME}
PROPERTIES
INSTALL_RPATH_USE_LINK_PATH TRUE
INSTALL_RPATH "${RPATH}"
# This will set rpaths relative to all SOFA core AND relocatable dependencies
sofa_auto_set_target_rpath(
TARGETS ${PROJECT_NAME}
RELOCATABLE "plugins"
)

add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
Expand Down
16 changes: 5 additions & 11 deletions bindings/Sofa/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,14 @@ find_package(SofaFramework REQUIRED)

enable_testing()
add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES} ${PYTHON_FILES})
target_link_libraries(${PROJECT_NAME} SofaGTestMain SofaHelper SofaPython3::Plugin SofaPython3::Bindings.Sofa SofaPython3::Bindings.SofaTypes SofaPython3::Bindings.SofaRuntime)
target_link_libraries(${PROJECT_NAME} SofaGTestMain SofaHelper SofaPython3::Plugin)
target_compile_definitions(${PROJECT_NAME} PRIVATE "PYTHON_TESTFILES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/\"")
set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER "Bindings/Tests")

set(RPATH "$ORIGIN/../lib")
if (APPLE)
list(APPEND RPATH "@executable_path/../lib")
endif()

set_target_properties(
${PROJECT_NAME}
PROPERTIES
INSTALL_RPATH_USE_LINK_PATH TRUE
INSTALL_RPATH "${RPATH}"
# This will set rpaths relative to all SOFA core AND relocatable dependencies
sofa_auto_set_target_rpath(
TARGETS ${PROJECT_NAME}
RELOCATABLE "plugins"
)

add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
Expand Down
6 changes: 1 addition & 5 deletions bindings/SofaExporter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ SP3_add_python_module(
SOURCES ${SOURCE_FILES}
HEADERS ${HEADER_FILES}
DEPENDS SofaExporter SofaPython3::Plugin SofaPython3::Bindings.Sofa.Core
)

if(SP3_BUILD_TEST)
add_subdirectory(tests)
endif()
)
Loading