diff --git a/CMake/SofaPython3Tools.cmake b/CMake/SofaPython3Tools.cmake
index 8e784ddf..d3302db2 100644
--- a/CMake/SofaPython3Tools.cmake
+++ b/CMake/SofaPython3Tools.cmake
@@ -48,17 +48,23 @@ function(SP3_add_python_package)
cmake_parse_arguments(A "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
+ set(OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${A_TARGET_DIRECTORY})
+ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
+ set(OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${A_TARGET_DIRECTORY})
+ endif()
+
+
file(GLOB_RECURSE files RELATIVE ${A_SOURCE_DIRECTORY} ${A_SOURCE_DIRECTORY}/*)
foreach(file_relative_path ${files})
set(file_absolute_path ${A_SOURCE_DIRECTORY}/${file_relative_path})
configure_file(
${file_absolute_path}
- ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${A_TARGET_DIRECTORY}/${file_relative_path}
+ ${OUTPUT_DIRECTORY}/${file_relative_path}
@ONLY
)
get_filename_component(relative_directory ${file_relative_path} DIRECTORY)
install(
- FILES "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${A_TARGET_DIRECTORY}/${file_relative_path}"
+ FILES "${OUTPUT_DIRECTORY}/${file_relative_path}"
DESTINATION "${LIBRARY_OUTPUT_DIRECTORY}/${SP3_PYTHON_PACKAGES_DIRECTORY}/${A_TARGET_DIRECTORY}/${relative_directory}"
)
endforeach()
@@ -117,7 +123,7 @@ function(SP3_add_python_module)
find_package(pybind11 CONFIG QUIET REQUIRED)
- pybind11_add_module(${A_TARGET} SHARED "${A_SOURCES}")
+ pybind11_add_module(${A_TARGET} SHARED NO_EXTRAS "${A_SOURCES}")
add_library(SofaPython3::${A_TARGET} ALIAS ${A_TARGET})
target_include_directories(${A_TARGET}
@@ -131,10 +137,8 @@ function(SP3_add_python_module)
if (NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_compile_options(${A_TARGET} PRIVATE -Dregister=)
- target_compile_options(${A_TARGET} PRIVATE -fvisibility=hidden)
endif()
- target_link_libraries(${A_TARGET} PUBLIC pybind11::module)
target_link_libraries(${A_TARGET} PUBLIC "${A_DEPENDS}")
set_target_properties(
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5e2f8370..aee9d14b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -73,6 +73,9 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
+# Enable use of folder for IDE like VS
+set_property(GLOBAL PROPERTY USE_FOLDERS ON)
+
# Set the minimum python version to 3.7
set(PYBIND11_PYTHON_VERSION 3.7)
diff --git a/bindings/CMakeLists.txt b/bindings/CMakeLists.txt
index b5067f78..c1a588c8 100644
--- a/bindings/CMakeLists.txt
+++ b/bindings/CMakeLists.txt
@@ -1,10 +1,10 @@
project(Bindings)
-add_subdirectory(Sofa)
-add_subdirectory(SofaRuntime)
-add_subdirectory(SofaGui)
-add_subdirectory(SofaTypes)
-add_subdirectory(Modules)
+set(BINDINGS_MODULE_LIST Sofa SofaRuntime SofaGui SofaTypes Modules)
+
+foreach(bindings_module ${BINDINGS_MODULE_LIST})
+ add_subdirectory(${bindings_module})
+endforeach()
if (SP3_WITH_SOFAEXPORTER)
add_subdirectory(SofaExporter)
@@ -13,12 +13,13 @@ endif()
add_library(${PROJECT_NAME} INTERFACE)
add_library(SofaPython3::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.Modules)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.Sofa)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.SofaExporter)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.SofaGui)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.SofaRuntime)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.SofaTypes)
+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"
diff --git a/bindings/Modules/CMakeLists.txt b/bindings/Modules/CMakeLists.txt
index ea31381c..dd75a919 100644
--- a/bindings/Modules/CMakeLists.txt
+++ b/bindings/Modules/CMakeLists.txt
@@ -1,9 +1,18 @@
project(Bindings.Modules)
-add_subdirectory(src/SofaPython3/SofaBaseTopology)
+set(MODULEBINDINGS_MODULE_LIST SofaBaseTopology)
+
+foreach(modulebindings_module ${MODULEBINDINGS_MODULE_LIST})
+ add_subdirectory(src/SofaPython3/${modulebindings_module})
+endforeach()
+
add_library(${PROJECT_NAME} INTERFACE)
add_library(SofaPython3::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.SofaBaseTopology)
+foreach(modulebindings_module ${MODULEBINDINGS_MODULE_LIST})
+ target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.${modulebindings_module})
+ set_target_properties(${PROJECT_NAME}.${modulebindings_module} PROPERTIES FOLDER "Bindings/Modules")
+endforeach()
+
install(TARGETS ${PROJECT_NAME} EXPORT BindingsTargets)
\ No newline at end of file
diff --git a/bindings/Sofa/CMakeLists.txt b/bindings/Sofa/CMakeLists.txt
index 77a15719..669f8edb 100644
--- a/bindings/Sofa/CMakeLists.txt
+++ b/bindings/Sofa/CMakeLists.txt
@@ -1,10 +1,10 @@
project(Bindings.Sofa)
-add_subdirectory(src/SofaPython3/Sofa/Core)
-add_subdirectory(src/SofaPython3/Sofa/Components)
-add_subdirectory(src/SofaPython3/Sofa/Helper)
-add_subdirectory(src/SofaPython3/Sofa/Simulation)
-add_subdirectory(src/SofaPython3/Sofa/Types)
+set(SOFABINDINGS_MODULE_LIST Core Components Helper Simulation Types)
+
+foreach(sofabindings_module ${SOFABINDINGS_MODULE_LIST})
+ add_subdirectory(src/SofaPython3/Sofa/${sofabindings_module})
+endforeach()
SP3_add_python_package(
SOURCE_DIRECTORY
@@ -16,11 +16,9 @@ SP3_add_python_package(
add_library(${PROJECT_NAME} INTERFACE)
add_library(SofaPython3::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.Core)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.Components)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.Helper)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.Simulation)
-target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.Types)
+foreach(sofabindings_module ${SOFABINDINGS_MODULE_LIST})
+ target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}.${sofabindings_module})
+endforeach()
install(TARGETS ${PROJECT_NAME} EXPORT BindingsTargets)
diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h
index ccfdf2e0..ced532ad 100644
--- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h
+++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h
@@ -34,145 +34,12 @@ along with sofaqtquick. If not, see .
namespace sofapython3 {
-/**
- * This is the main pybind11 type holder that will manage the underlying reference counter of the SOFA C++ objects. It
- * should be used when creating bindings for a C++ class that inherits sofa::core::objectmodel::Base.
- *
- * \note There is a problem in pybind11 which sometime does a kind of slicing between the python object and its bound
- * C++ object. This usually happens when instantiating a virtual (trampoline) class in python that then calls the
- * new operator in the pybind11::init() and return the newly created object. Python will sometime destroy the
- * initial object before the C++ one is created. We go around this by keeping here a reference to the initial
- * python object, hence incrementing its reference counter by one.
- *
- * See https://github.com/pybind/pybind11/issues/1546 for more details.
- * @tparam T
- */
-
template
-struct py_shared_ptr : public sofa::core::sptr {
- using Base = sofa::core::sptr;
- using Base::Base;
-
- py_shared_ptr(const sofa::core::sptr & sptr) : Base(sptr) {}
-
- py_shared_ptr(const sofa::core::sptr & sptr, T * ptr) : Base(ptr) {}
-
- py_shared_ptr(const py_shared_ptr & py_ptr) : Base(py_ptr) {
- p_object = py_ptr.p_object;
- }
-
- py_shared_ptr(py_shared_ptr & py_ptr) : Base(py_ptr) {
- p_object = py_ptr.p_object;
- }
-
- void set_object(const pybind11::object & o) {
- p_object = o;
- }
-
-private:
- pybind11::object p_object;
-};
+using py_shared_ptr = sofa::core::sptr;
} // namespace sofapython3
-// Type caster for SP3 and SOFA holders
-namespace pybind11::detail {
-/**
- * This is an alias type caster that converts sofa::core::sptr to sofapython3::py_shared_ptr.
- * It is needed for when bound C++ function returns sofa::core::sptr instead of py_shared_ptr while the holder
- * type was set to the latter.
- * @tparam T
- */
-template
-class type_caster> {
-
-PYBIND11_TYPE_CASTER (sofa::core::sptr , _("sofa::core::sptr"));
-
- using BaseCaster = copyable_holder_caster>;
-
- bool load (pybind11::handle src, bool b)
- {
- // First make sure the py::object is an instanced of sofapython3::py_shared_ptr
- BaseCaster bc;
- bool success = bc.load (src, b);
- if (!success) {
- return false;
- }
-
- // Convert the holder_caster to a sofapython3::py_shared_ptr instance
- auto base_ptr = static_cast> (bc);
-
- // Take ownership of the py::object
- auto h = BaseCaster::cast(base_ptr, return_value_policy(), handle());
- auto py_obj = reinterpret_borrow