From 3f593af97f892b6913a95502bbe5392d8dec7049 Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Thu, 12 Oct 2023 12:39:30 +0200 Subject: [PATCH] Added Python bindings for the Dulcificum bot library Python binding for Dulcificum bot library has been initialized. This includes necessary includes and setup in `pyDulcificum.cpp` file, modification in `CMakeLists.txt` for option to build with Python bindings, and updating the Conan file to include Python bindings option and requirements necessary for these bindings. This will allow Python scripts to directly use Dulcificum's bot commands and functionalities. Contributes to CURA-10561 --- CMakeLists.txt | 12 +++++++++++- conanfile.py | 18 ++++++++++++++++++ pyDulcificum/pyDulcificum.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 pyDulcificum/pyDulcificum.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 28efce9..0c0dd78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ find_package(standardprojectsettings REQUIRED) option(EXTENSIVE_WARNINGS "Build with all warnings" ON) option(ENABLE_TESTS "Build with unit test" ON) option(WITH_APPS "Build with apps" ON) +option(WITH_PYTHON_BINDINGS "Build with Python bindings: `pyDulcificum`" ON) find_package(nlohmann_json REQUIRED) find_package(spdlog REQUIRED) @@ -35,7 +36,16 @@ if (${EXTENSIVE_WARNINGS}) endif () # --- Setup Python bindings --- - +if (WITH_PYTHON_BINDINGS) + find_package(pybind11 REQUIRED) + find_package(cpython REQUIRED) + + pybind11_add_module(pyDulcificum pyDulcificum/pyDulcificum.cpp) + target_link_libraries(pyDulcificum PUBLIC dulcificum pybind11::pybind11 cpython::cpython) + if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo) + pybind11_strip(pyArcus) + endif() +endif () # --- Setup tests --- if (ENABLE_TESTS) diff --git a/conanfile.py b/conanfile.py index 63951c4..d1c1890 100644 --- a/conanfile.py +++ b/conanfile.py @@ -29,12 +29,14 @@ class DulcificumConan(ConanFile): "fPIC": [True, False], "enable_extensive_warnings": [True, False], "with_apps": [True, False], + "with_python_bindings": [True, False], } default_options = { "shared": False, "fPIC": True, "enable_extensive_warnings": False, "with_apps": True, + "with_python_bindings": True, } def set_version(self): @@ -71,6 +73,7 @@ def export_sources(self): copy(self, "*", os.path.join(self.recipe_folder, "include"), os.path.join(self.export_sources_folder, "include")) copy(self, "*", os.path.join(self.recipe_folder, "test"), os.path.join(self.export_sources_folder, "test")) copy(self, "*", os.path.join(self.recipe_folder, "apps"), os.path.join(self.export_sources_folder, "apps")) + copy(self, "*", os.path.join(self.recipe_folder, "pyDulcificum"), os.path.join(self.export_sources_folder, "pyDulcificum")) copy(self, "*", os.path.join(self.recipe_folder, "templates"), os.path.join(self.export_sources_folder, "templates")) def config_options(self): @@ -90,6 +93,9 @@ def requirements(self): self.requires("spdlog/1.10.0") if self.options.with_apps: self.requires("docopt.cpp/0.6.3") + if self.options.with_python_bindings: + self.requires("cpython/3.10.4") + self.requires("pybind11/2.10.4") def build_requirements(self): self.test_requires("standardprojectsettings/[>=0.1.0]@ultimaker/stable") @@ -115,7 +121,19 @@ def generate(self): tc = CMakeToolchain(self) tc.variables["ENABLE_TESTS"] = not self.conf.get("tools.build:skip_test", False, check_type = bool) tc.variables["EXTENSIVE_WARNINGS"] = self.options.enable_extensive_warnings + tc.variables["WITH_APPS"] = self.options.with_apps + + tc.variables["WITH_PYTHON_BINDINGS"] = self.options.with_python_bindings + if self.options.with_python_bindings: + tc.variables["Python_EXECUTABLE"] = self.deps_user_info["cpython"].python.replace("\\", "/") + tc.variables["Python_USE_STATIC_LIBS"] = not self.options["cpython"].shared + tc.variables["Python_ROOT_DIR"] = self.deps_cpp_info["cpython"].rootpath.replace("\\", "/") + tc.variables["Python_FIND_FRAMEWORK"] = "NEVER" + tc.variables["Python_FIND_REGISTRY"] = "NEVER" + tc.variables["Python_FIND_IMPLEMENTATIONS"] = "CPython" + tc.variables["Python_FIND_STRATEGY"] = "LOCATION" + if is_msvc(self): tc.variables["USE_MSVC_RUNTIME_LIBRARY_DLL"] = not is_msvc_static_runtime(self) tc.cache_variables["CMAKE_POLICY_DEFAULT_CMP0077"] = "NEW" diff --git a/pyDulcificum/pyDulcificum.cpp b/pyDulcificum/pyDulcificum.cpp new file mode 100644 index 0000000..192f463 --- /dev/null +++ b/pyDulcificum/pyDulcificum.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +namespace py = pybind11; + +PYBIND11_MODULE(pyDulcificum, module) +{ + module.doc() = R"pbdoc(exit + pyDulcificum + ----------------------- + .. currentmodule:: Python bindings for Dulcificum + .. autosummary:: + :toctree: _generate + add + subtract + )pbdoc"; + + py::class_(module, "Move") + .def(py::init<>()) + .def_readwrite("point", &dulcificum::botcmd::Move::point, "The point of the move command") + .def_readwrite("feedrate", &dulcificum::botcmd::Move::feedrate, "The feedrate of the move command") + .def_readwrite("is_point_relative", &dulcificum::botcmd::Move::is_point_relative, "Components of the point relative") + ; +} \ No newline at end of file