-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reverting all the adventures into working
- Loading branch information
Showing
5 changed files
with
91 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,14 @@ | |
from setuptools import Extension, setup | ||
from setuptools.command.build_ext import build_ext | ||
|
||
# Convert distutils Windows platform specifiers to CMake -A arguments | ||
PLAT_TO_CMAKE = { | ||
"win32": "Win32", | ||
"win-amd64": "x64", | ||
"win-arm32": "ARM", | ||
"win-arm64": "ARM64", | ||
} | ||
|
||
|
||
# A CMakeExtension needs a sourcedir instead of a file list. | ||
# The name must be the _single_ output extension from the CMake build. | ||
|
@@ -68,6 +76,26 @@ def build_extension(self, ext: CMakeExtension) -> None: | |
except ImportError: | ||
pass | ||
|
||
else: | ||
# Single config generators are handled "normally" | ||
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"}) | ||
|
||
# CMake allows an arch-in-generator style for backward compatibility | ||
contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"}) | ||
|
||
# Specify the arch if using MSVC generator, but only if it doesn't | ||
# contain a backward-compatibility arch spec already in the | ||
# generator name. | ||
if not single_config and not contains_arch: | ||
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]] | ||
|
||
# Multi-config generators have a different way to specify configs | ||
if not single_config: | ||
cmake_args += [ | ||
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}" | ||
] | ||
build_args += ["--config", cfg] | ||
|
||
if sys.platform.startswith("darwin"): | ||
# Cross-compile support for macOS - respect ARCHFLAGS if set | ||
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", "")) | ||
|
@@ -102,11 +130,12 @@ def build_extension(self, ext: CMakeExtension) -> None: | |
version="0.0.1", | ||
author="M. Amin Safavi", | ||
author_email="[email protected]", | ||
description="A test project using pybind11 and Drake", | ||
description="A template project to extend Drake using pybind11 and CMake", | ||
long_description="", | ||
ext_modules=[CMakeExtension("drake_extension")], | ||
cmdclass={"build_ext": CMakeBuild}, | ||
zip_safe=False, | ||
extras_require={"test": ["pytest>=6.0"]}, | ||
extras_require={"test": ["pytest>=6.0"], | ||
"pydrake": ["pydrake>=1.20"]}, | ||
python_requires=">=3.10", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* @file | ||
* Provides an example of creating a simple Drake C++ system and binding it in | ||
* pybind11, to be used with pydrake. | ||
*/ | ||
#include <pybind11/pybind11.h> | ||
#include <drake/systems/framework/leaf_system.h> | ||
|
||
namespace py = pybind11; | ||
|
||
using drake::systems::BasicVector; | ||
using drake::systems::Context; | ||
using drake::systems::LeafSystem; | ||
using drake::systems::kVectorValued; | ||
|
||
namespace drake_extension { | ||
|
||
/// Adds a constant to an input. | ||
template <typename T> | ||
class SimpleAdder : public LeafSystem<T> { | ||
public: | ||
explicit SimpleAdder(T add) | ||
: add_(add) { | ||
this->DeclareInputPort("in", kVectorValued, 1); | ||
this->DeclareVectorOutputPort( | ||
"out", BasicVector<T>(1), &SimpleAdder::CalcOutput); | ||
} | ||
|
||
private: | ||
void CalcOutput(const Context<T>& context, BasicVector<T>* output) const { | ||
auto u = this->get_input_port(0).Eval(context); | ||
auto&& y = output->get_mutable_value(); | ||
y.array() = u.array() + add_; | ||
} | ||
|
||
const T add_{}; | ||
}; | ||
|
||
|
||
PYBIND11_MODULE(drake_extension , m) { | ||
m.doc() = "Example module interfacing with pydrake and Drake C++"; | ||
|
||
py::module::import("pydrake.systems.framework"); | ||
|
||
using T = double; | ||
|
||
py::class_<SimpleAdder<T>, LeafSystem<T>>(m, "SimpleAdder") | ||
.def(py::init<T>(), py::arg("add")); | ||
} | ||
|
||
} // namespace drake_extension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters