Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Scikit-build as the build backend #3

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
export PATH="/opt/drake/bin${PATH:+:${PATH}}"
export PYTHONPATH="/opt/drake/lib/python$(python3 -c 'import sys; print("{0}.{1}".format(*sys.version_info))')/site-packages${PYTHONPATH:+:${PYTHONPATH}}"
export LD_LIBRARY_PATH="/opt/drake/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
pip3 install pybind11[global] pytest
pip3 uninstall numpy
pip3 install pybind11[global] pytest numpy drake
pip3 install --verbose .[test]
py.test
python3 -m pytest
25 changes: 20 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ 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_package(pybind11 CONFIG REQUIRED)

execute_process(COMMAND ${Python3_EXECUTABLE}-config --exec-prefix
OUTPUT_VARIABLE PYTHON_EXEC_PREFIX
Expand All @@ -24,6 +26,11 @@ get_filename_component(PYTHONPATH
REALPATH
)

# Find the module development requirements (requires FindPython from 3.17 or
# scikit-build-core's built-in backport)
#find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
find_package(pybind11 CONFIG REQUIRED)

set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -32,6 +39,14 @@ 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)
#python3_add_library(_ext MODULE src/ext.cpp WITH_SOABI)
#target_link_libraries(_ext PUBLIC drake::drake pybind11::headers)
pybind11_add_module(_ext MODULE src/ext.cpp)
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)
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down
13 changes: 6 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -40,6 +36,9 @@ filterwarnings = [
]
testpaths = ["tests"]

[tool.scikit-build]
cmake.verbose = true
logging.level = "INFO"

[tool.cibuildwheel]
build-frontend = "build[uv]"
Expand Down
141 changes: 0 additions & 141 deletions setup.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/drake_extension/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from __future__ import annotations
from ._ext import __doc__, __version__, SimpleAdder

__all__ = ["__doc__", "__version__", "SimpleAdder"]
11 changes: 10 additions & 1 deletion src/drake_extension.cpp → src/ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <pybind11/pybind11.h>
#include <drake/systems/framework/leaf_system.h>

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)

namespace py = pybind11;

using drake::systems::BasicVector;
Expand Down Expand Up @@ -37,7 +40,7 @@ class SimpleAdder : public LeafSystem<T> {
};


PYBIND11_MODULE(drake_extension , m) {
PYBIND11_MODULE(_ext , m) {
m.doc() = "Example module interfacing with pydrake and Drake C++";

py::module::import("pydrake.systems.framework");
Expand All @@ -46,6 +49,12 @@ PYBIND11_MODULE(drake_extension , m) {

py::class_<SimpleAdder<T>, LeafSystem<T>>(m, "SimpleAdder")
.def(py::init<T>(), py::arg("add"));

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
}

} // namespace drake_extension
2 changes: 2 additions & 0 deletions tests/test_simple_adder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import numpy as np
from drake_extension import SimpleAdder

Expand Down
Loading