From ff5ee0154ea15b25a526622476b6d1ae28270a30 Mon Sep 17 00:00:00 2001 From: "M. Amin Safavi" Date: Sat, 14 Sep 2024 14:23:51 +0900 Subject: [PATCH] refactor --- CMakeLists.txt | 21 +++++++++++++++++---- README.md | 3 +-- pyproject.toml | 10 +++------- src/drake_extension/__init__.py | 4 ++++ src/{drake_extension.cpp => ext.cpp} | 6 ++++++ 5 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 src/drake_extension/__init__.py rename src/{drake_extension.cpp => ext.cpp} (90%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b25db1f..44e8671 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,11 +6,18 @@ cmake_minimum_required(VERSION 3.16...3.27) # Scikit-build-core sets these values for you, or you can just hard-code the # name and version. -project(drake_extension LANGUAGES CXX) +project( + ${SKBUILD_PROJECT_NAME} + VERSION ${SKBUILD_PROJECT_VERSION} + LANGUAGES CXX) include(CTest) option(RUN_X11_TESTS "Run tests that require X11" OFF) find_package(drake CONFIG REQUIRED) + +# Find the module development requirements (requires FindPython from 3.17 or +# scikit-build-core's built-in backport) +find_package(Python REQUIRED COMPONENTS Interpreter Development.Module) find_package(pybind11 CONFIG REQUIRED) execute_process(COMMAND ${Python3_EXECUTABLE}-config --exec-prefix @@ -32,6 +39,12 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) # Add a library using FindPython's tooling (pybind11 also provides a helper like # this) -pybind11_add_module(drake_extension MODULE src/drake_extension.cpp) -target_link_libraries(drake_extension PUBLIC drake::drake) -set_target_properties(drake_extension PROPERTIES CXX_VISIBILITY_PRESET default) +pybind11_add_module(_ext MODULE src/ext.cpp WITH_SOABI) +target_link_libraries(_ext PUBLIC drake::drake) +set_target_properties(_ext PROPERTIES CXX_VISIBILITY_PRESET default) + +# This is passing in the version as a define just as an example +target_compile_definitions(_ext PRIVATE VERSION_INFO=${PROJECT_VERSION}) + +# The install directory is the output (wheel) directory +install(TARGETS _ext DESTINATION drake_extension) diff --git a/README.md b/README.md index 9e7d85c..1a4f6af 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ If you think the current setup can be improved, please open an issue or a pull r ## Setup Guide > [!NOTE] -> The setup has only been tested with Ubuntu 24.04 (Noble) operating system, Python 3.12 and Drake v1.32.0 +> The setup has only been tested with Ubuntu 22.04 (Jammy) operating system, Python 3.10 and Drake v1.32.0 > If you encountered any issues feel free to leave an issue so we can fix it together. > You are also welcome to make a PR and extend the support. @@ -47,7 +47,6 @@ py.test ``` ## TODOs -- [ ] Add Github Actions to Automated Testing - [ ] Add a Dockerfile for more reproducibility and transparency of the setup - [ ] Try `nanobind` and compare resulting binary sizes with `pybind` - [ ] Add information about how to shared the packaged python wheel. diff --git a/pyproject.toml b/pyproject.toml index 07e2826..033c2e4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,11 +1,7 @@ [build-system] -requires = [ - "setuptools>=42", - "wheel", - "ninja", - "cmake>=3.12", -] -build-backend = "setuptools.build_meta" +requires = ["scikit-build-core>=0.3.3", "pybind11"] +build-backend = "scikit_build_core.build" + [project] name = "drake_extension" diff --git a/src/drake_extension/__init__.py b/src/drake_extension/__init__.py new file mode 100644 index 0000000..43dd8b2 --- /dev/null +++ b/src/drake_extension/__init__.py @@ -0,0 +1,4 @@ +from __future__ import annotations +from ._ext import __doc__, __version__, SimpleAdder + +__all__ = ["__doc__", "__version__", "SimpleAdder"] diff --git a/src/drake_extension.cpp b/src/ext.cpp similarity index 90% rename from src/drake_extension.cpp rename to src/ext.cpp index 141aa57..ed00f11 100644 --- a/src/drake_extension.cpp +++ b/src/ext.cpp @@ -46,6 +46,12 @@ PYBIND11_MODULE(drake_extension , m) { py::class_, LeafSystem>(m, "SimpleAdder") .def(py::init(), py::arg("add")); + + #ifdef VERSION_INFO + m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO); + #else + m.attr("__version__") = "dev"; + #endif } } // namespace drake_extension