Simplify generation of pybind11 modules

* Install module libs into CATKIN_GLOBAL_PYTHON_DESTINATION (assuming unique names).
  This avoids the need to link them into the source space, because they are found also from devel space.
* Use pybind11's def_submodule() to create the `core` and `stages` submodules,
  everything linked into the same lib
This commit is contained in:
Robert Haschke 2021-03-17 17:26:58 +01:00
parent d9b7aa37a3
commit 3cf92442fb
10 changed files with 46 additions and 88 deletions

View File

@ -1 +1 @@
from ._python_tools import * from pymoveit_python_tools import *

View File

@ -0,0 +1 @@
from . import core, stages

View File

@ -1 +1 @@
from ._core import * from pymoveit_mtc.core import *

View File

@ -1,2 +1 @@
from ._core import Stage from pymoveit_mtc.stages import *
from ._stages import *

View File

@ -19,62 +19,27 @@ install(TARGETS ${TOOLS_LIB_NAME}
) )
# python_tools # moveit.python_tools
set(TOOLS_LIB_NAME _python_tools) pybind11_add_module(pymoveit_python_tools src/python_tools.cpp)
configure_file(src/python_tools.cpp.in python_tools.cpp @ONLY) target_link_libraries(pymoveit_python_tools PRIVATE ${TOOLS_LIB_NAME})
pybind11_add_module(${TOOLS_LIB_NAME} MODULE ${CMAKE_CURRENT_BINARY_DIR}/python_tools.cpp) set_target_properties(pymoveit_python_tools PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_PYTHON_DESTINATION})
target_link_libraries(${TOOLS_LIB_NAME} PRIVATE moveit_python_tools)
# python tools: configure install directory and create symlink in source space # moveit.task_constructor
set(MOVEIT_PYTHON_MODULE_PATH moveit/python_tools)
install(TARGETS ${TOOLS_LIB_NAME}
LIBRARY DESTINATION ${CATKIN_GLOBAL_PYTHON_DESTINATION}/${MOVEIT_PYTHON_MODULE_PATH}
)
add_custom_command(TARGET ${TOOLS_LIB_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink "$<TARGET_FILE:${TOOLS_LIB_NAME}>" "${CMAKE_CURRENT_SOURCE_DIR}/../src/${MOVEIT_PYTHON_MODULE_PATH}/$<TARGET_FILE_NAME:${TOOLS_LIB_NAME}>"
COMMENT "Creating symlink to ${TOOLS_LIB_NAME} in source space"
)
# core
set(CORE_LIB_NAME _core)
set(INCLUDES ${PROJECT_SOURCE_DIR}/include/moveit/python/task_constructor) set(INCLUDES ${PROJECT_SOURCE_DIR}/include/moveit/python/task_constructor)
configure_file(src/core.cpp.in core.cpp @ONLY) pybind11_add_module(pymoveit_mtc
pybind11_add_module(${CORE_LIB_NAME} SHARED
${INCLUDES}/properties.h ${INCLUDES}/properties.h
src/properties.cpp src/properties.cpp
src/solvers.cpp src/solvers.cpp
src/core.cpp src/core.cpp
${CMAKE_CURRENT_BINARY_DIR}/core.cpp
)
target_include_directories(${CORE_LIB_NAME}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)
target_link_libraries(${CORE_LIB_NAME} PUBLIC ${PROJECT_NAME} moveit_python_tools)
# stages
set(STAGES_LIB_NAME _stages)
configure_file(src/stages.cpp.in stages.cpp @ONLY)
pybind11_add_module(${STAGES_LIB_NAME} MODULE
src/stages.cpp src/stages.cpp
${CMAKE_CURRENT_BINARY_DIR}/stages.cpp src/module.cpp
) )
target_link_libraries(${STAGES_LIB_NAME} PUBLIC ${PROJECT_NAME}_stages ${CORE_LIB_NAME} moveit_python_tools) target_include_directories(pymoveit_mtc PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
target_link_libraries(pymoveit_mtc PUBLIC ${PROJECT_NAME} ${PROJECT_NAME}_stages ${TOOLS_LIB_NAME})
set_target_properties(pymoveit_mtc PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_PYTHON_DESTINATION})
# install python libs
install(TARGETS pymoveit_python_tools pymoveit_mtc
# core, stages: configure install directory LIBRARY DESTINATION ${CATKIN_GLOBAL_PYTHON_DESTINATION}
set(MOVEIT_PYTHON_MODULE_PATH moveit/task_constructor)
install(TARGETS ${CORE_LIB_NAME} ${STAGES_LIB_NAME}
LIBRARY DESTINATION ${CATKIN_GLOBAL_PYTHON_DESTINATION}/${MOVEIT_PYTHON_MODULE_PATH}
) )
# Add symlinks from source space to devel-space libs
# This is necessary to find them from devel-space too
foreach(tgt ${CORE_LIB_NAME} ${STAGES_LIB_NAME})
add_custom_command(TARGET ${tgt} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink "$<TARGET_FILE:${tgt}>" "${CMAKE_CURRENT_SOURCE_DIR}/../src/${MOVEIT_PYTHON_MODULE_PATH}/$<TARGET_FILE_NAME:${tgt}>"
COMMENT "Creating symlink to ${tgt} in source space"
)
endforeach()

View File

@ -1,19 +0,0 @@
#include <pybind11/pybind11.h>
namespace moveit {
namespace python {
void export_properties(pybind11::module& m);
void export_solvers(pybind11::module& m);
void export_core(pybind11::module& m);
}
}
PYBIND11_MODULE(@CORE_LIB_NAME@, m) {
m.doc() = "MoveIt Task Constructor";
moveit::python::export_properties(m);
moveit::python::export_solvers(m);
moveit::python::export_core(m);
}

View File

@ -0,0 +1,26 @@
#include <pybind11/pybind11.h>
namespace moveit {
namespace python {
void export_properties(pybind11::module& m);
void export_solvers(pybind11::module& m);
void export_core(pybind11::module& m);
void export_stages(pybind11::module& m);
} // namespace python
} // namespace moveit
PYBIND11_MODULE(pymoveit_mtc, m) {
m.doc() = "MoveIt Task Constructor";
auto msub = m.def_submodule("core");
msub.doc() = "MTC core components";
moveit::python::export_properties(msub);
moveit::python::export_solvers(msub);
moveit::python::export_core(msub);
msub = m.def_submodule("stages");
msub.doc() = "MTC stages";
moveit::python::export_stages(msub);
}

View File

@ -6,7 +6,7 @@
namespace py = pybind11; namespace py = pybind11;
using namespace moveit::python; using namespace moveit::python;
PYBIND11_MODULE(@TOOLS_LIB_NAME@, m) { PYBIND11_MODULE(pymoveit_python_tools, m) {
m.doc() = "MoveIt python tools"; m.doc() = "MoveIt python tools";
m.def("roscpp_init", &InitProxy::init, "Initialize C++ ROS", py::arg("node_name") = "moveit_python_wrapper", m.def("roscpp_init", &InitProxy::init, "Initialize C++ ROS", py::arg("node_name") = "moveit_python_wrapper",

View File

@ -1,14 +0,0 @@
#include <pybind11/pybind11.h>
namespace moveit {
namespace python {
void export_stages(pybind11::module& m);
} }
PYBIND11_MODULE(@STAGES_LIB_NAME@, m) {
m.doc() = "MoveIt Task Constructor Stages";
moveit::python::export_stages(m);
}

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python #!/usr/bin/env python
from setuptools import setup from setuptools import find_packages, setup
from catkin_pkg.python_setup import generate_distutils_setup from catkin_pkg.python_setup import generate_distutils_setup
d = generate_distutils_setup( d = generate_distutils_setup(
# list of packages to setup # list of packages to setup
packages=["moveit", "moveit.python_tools", "moveit.task_constructor"], packages=find_packages("python/src"),
# specify location of root ("") package dir # specify location of root ("") package dir
package_dir={"": "python/src"}, package_dir={"": "python/src"},
) )