Skip to content

Commit fdc2232

Browse files
committed
Add pyproject.toml/setup.py for building wheels.
1 parent 2c1b18a commit fdc2232

File tree

4 files changed

+151
-4
lines changed

4 files changed

+151
-4
lines changed
File renamed without changes.

pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=42",
4+
"wheel",
5+
"ninja",
6+
"cmake>=3.16",
7+
]
8+
build-backend = "setuptools.build_meta"

python_examples/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
1. Ensure that BehaviorTree.CPP is build with `BTCPP_PYTHON=ON`.
2-
2. Add the build directory containing the `btpy_cpp.*.so` Python extension to
3-
your `PYTHONPATH`.
4-
3. Run an example, e.g. `python3 ex01_sample.py`
1+
1. Install the bindings by running `pip install .` from the project root.
2+
2. Run an example, e.g. `python3 ex01_sample.py`

setup.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import os
2+
import re
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
from setuptools import Extension, setup
8+
from setuptools.command.build_ext import build_ext
9+
10+
# Convert distutils Windows platform specifiers to CMake -A arguments
11+
PLAT_TO_CMAKE = {
12+
"win32": "Win32",
13+
"win-amd64": "x64",
14+
"win-arm32": "ARM",
15+
"win-arm64": "ARM64",
16+
}
17+
18+
19+
# A CMakeExtension needs a sourcedir instead of a file list.
20+
# The name must be the _single_ output extension from the CMake build.
21+
# If you need multiple extensions, see scikit-build.
22+
class CMakeExtension(Extension):
23+
def __init__(self, name: str, sourcedir: str = "") -> None:
24+
super().__init__(name, sources=[])
25+
self.sourcedir = os.fspath(Path(sourcedir).resolve())
26+
27+
28+
class CMakeBuild(build_ext):
29+
def build_extension(self, ext: CMakeExtension) -> None:
30+
# Must be in this form due to bug in .resolve() only fixed in Python 3.10+
31+
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
32+
extdir = ext_fullpath.parent.resolve()
33+
34+
# Using this requires trailing slash for auto-detection & inclusion of
35+
# auxiliary "native" libs
36+
37+
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
38+
cfg = "Debug" if debug else "Release"
39+
40+
# CMake lets you override the generator - we need to check this.
41+
# Can be set with Conda-Build, for example.
42+
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
43+
44+
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
45+
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
46+
# from Python.
47+
cmake_args = [
48+
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
49+
f"-DPYTHON_EXECUTABLE={sys.executable}",
50+
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
51+
# BehaviorTree.CPP specific CMake options
52+
"-DBTCPP_BUILD_TOOLS=OFF",
53+
"-DBTCPP_EXAMPLES=OFF",
54+
"-DBTCPP_UNIT_TESTS=OFF",
55+
]
56+
build_args = []
57+
# Adding CMake arguments set as environment variable
58+
# (needed e.g. to build for ARM OSx on conda-forge)
59+
if "CMAKE_ARGS" in os.environ:
60+
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
61+
62+
if self.compiler.compiler_type != "msvc":
63+
# Using Ninja-build since it a) is available as a wheel and b)
64+
# multithreads automatically. MSVC would require all variables be
65+
# exported for Ninja to pick it up, which is a little tricky to do.
66+
# Users can override the generator with CMAKE_GENERATOR in CMake
67+
# 3.15+.
68+
if not cmake_generator or cmake_generator == "Ninja":
69+
try:
70+
import ninja
71+
72+
ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
73+
cmake_args += [
74+
"-GNinja",
75+
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
76+
]
77+
except ImportError:
78+
pass
79+
80+
else:
81+
# Single config generators are handled "normally"
82+
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
83+
84+
# CMake allows an arch-in-generator style for backward compatibility
85+
contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
86+
87+
# Specify the arch if using MSVC generator, but only if it doesn't
88+
# contain a backward-compatibility arch spec already in the
89+
# generator name.
90+
if not single_config and not contains_arch:
91+
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
92+
93+
# Multi-config generators have a different way to specify configs
94+
if not single_config:
95+
cmake_args += [
96+
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"
97+
]
98+
build_args += ["--config", cfg]
99+
100+
if sys.platform.startswith("darwin"):
101+
# Cross-compile support for macOS - respect ARCHFLAGS if set
102+
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
103+
if archs:
104+
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
105+
106+
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
107+
# across all generators.
108+
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
109+
# self.parallel is a Python 3 only way to set parallel jobs by hand
110+
# using -j in the build_ext call, not supported by pip or PyPA-build.
111+
if hasattr(self, "parallel") and self.parallel:
112+
# CMake 3.12+ only.
113+
build_args += [f"-j{self.parallel}"]
114+
115+
build_temp = Path(self.build_temp) / ext.name
116+
if not build_temp.exists():
117+
build_temp.mkdir(parents=True)
118+
119+
subprocess.run(
120+
["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True
121+
)
122+
subprocess.run(
123+
["cmake", "--build", ".", *build_args], cwd=build_temp, check=True
124+
)
125+
126+
127+
# The information here can also be placed in setup.cfg - better separation of
128+
# logic and declaration, and simpler if you include description/version in a file.
129+
setup(
130+
name="btcpp",
131+
version="0.0.1",
132+
author="Kyle Cesare",
133+
author_email="[email protected]",
134+
description="Python bindings to the BehaviorTree.CPP project",
135+
long_description="",
136+
packages=["btpy"],
137+
ext_modules=[CMakeExtension("btcpp")],
138+
cmdclass={"build_ext": CMakeBuild},
139+
zip_safe=False,
140+
python_requires=">=3.7",
141+
)

0 commit comments

Comments
 (0)