Skip to content

Commit af17c99

Browse files
committed
feat: setup.py redesign and helpers
1 parent b3d8fec commit af17c99

File tree

14 files changed

+471
-143
lines changed

14 files changed

+471
-143
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,13 @@ jobs:
349349
- name: Install system requirements
350350
run: apk add doxygen python3-dev
351351

352-
- name: Ensure pip
353-
run: python3 -m ensurepip
352+
- name: Ensure and upgrade pip
353+
run: |
354+
python3 -m ensurepip
355+
python3 -m pip -U pip
354356
355357
- name: Install docs & setup requirements
356-
run: python3 -m pip install -r docs/requirements.txt pytest setuptools
358+
run: python3 -m pip install -r docs/requirements.txt pytest setuptools cmake --prefer-binary
357359

358360
- name: Build docs
359361
run: python3 -m sphinx -W -b html docs docs/.build

.github/workflows/format.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ jobs:
1717
- uses: actions/checkout@v2
1818
- uses: actions/setup-python@v2
1919
- uses: pre-commit/[email protected]
20+
with:
21+
extra_args: --hook-stage manual

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ pybind11Config*.cmake
3939
pybind11Targets.cmake
4040
/*env*
4141
/.vscode
42+
/pybind11/include/*
43+
/pybind11/share/*

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ repos:
3434
types: [file]
3535
files: (\.cmake|CMakeLists.txt)(.in)?$
3636

37+
- repo: https://github.com/mgedmin/check-manifest
38+
rev: "0.42"
39+
hooks:
40+
- id: check-manifest
41+
stages: [manual]
42+
additional_dependencies: [cmake, ninja]
43+
3744
- repo: local
3845
hooks:
3946
- id: check-style

MANIFEST.in

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
recursive-include include/pybind11 *.h
2-
include LICENSE README.md .github/CONTRIBUTING.md
2+
recursive-include pybind11 *.h
3+
recursive-include pybind11 *.cmake
4+
recursive-include pybind11 *.py
5+
recursive-include tools *.cmake
6+
recursive-include tools *.in
7+
include CMakeLists.txt LICENSE README.md .github/CONTRIBUTING.md

docs/compiling.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,51 @@ the [python_example]_ repository.
1313

1414
.. [python_example] https://github.com/pybind/python_example
1515
16+
A helper file is provided with pybind11 that can simplify usage with setuptools
17+
if you have pybind11 installed as a Python package; the file is also standalone,
18+
if you want to copy it to your package. If use use PEP518's ``pyproject.toml``
19+
file:
20+
21+
.. code-block:: toml
22+
23+
[build-system]
24+
requires = ["setuptools", "wheel", "pybind11==2.6.0"]
25+
build-backend = "setuptools.build_meta"
26+
27+
you can ensure that pybind11 is available during the building of your project
28+
(pip 10+ required).
29+
30+
An example of a ``setup.py`` using pybind11's helpers:
31+
32+
.. code-block:: python
33+
34+
from setuptools import setup, Extension
35+
from pybind11.setup_helpers import BuildExt
36+
37+
ext_modules = [
38+
Extension(
39+
"python_example",
40+
sorted(["src/main.cpp"]),
41+
language="c++",
42+
),
43+
]
44+
45+
setup(
46+
...,
47+
cmdclass={"build_ext": BuildExt},
48+
ext_modules=ext_modules
49+
)
50+
51+
52+
If you copy ``setup_helpers.py`` into your local project to try to support the
53+
classic build procedure, then you will need to use the deprecated
54+
``setup_requires=["pybind11>=2.6.0"]`` keyword argument to setup;
55+
``setup_helpers`` tries to support this as well.
56+
57+
.. versionchanged:: 2.6
58+
59+
Added support file.
60+
1661
Building with cppimport
1762
========================
1863

pybind11/__init__.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# -*- coding: utf-8 -*-
2-
from ._version import version_info, __version__ # noqa: F401 imported but unused
32

3+
from ._version import version_info, __version__
4+
from .commands import get_include, get_cmake_dir
45

5-
def get_include(user=False):
6-
import os
7-
d = os.path.dirname(__file__)
8-
if os.path.exists(os.path.join(d, "include")):
9-
# Package is installed
10-
return os.path.join(d, "include")
11-
else:
12-
# Package is from a source directory
13-
return os.path.join(os.path.dirname(d), "include")
6+
7+
__all__ = (
8+
"version_info",
9+
"__version__",
10+
"get_include",
11+
"get_cmake_dir",
12+
)

pybind11/__main__.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,34 @@
99

1010

1111
def print_includes():
12-
dirs = [sysconfig.get_path('include'),
13-
sysconfig.get_path('platinclude'),
14-
get_include()]
12+
dirs = [
13+
sysconfig.get_path("include"),
14+
sysconfig.get_path("platinclude"),
15+
get_include(),
16+
]
1517

1618
# Make unique but preserve order
1719
unique_dirs = []
1820
for d in dirs:
1921
if d not in unique_dirs:
2022
unique_dirs.append(d)
2123

22-
print(' '.join('-I' + d for d in unique_dirs))
24+
print(" ".join("-I" + d for d in unique_dirs))
2325

2426

2527
def main():
26-
parser = argparse.ArgumentParser(prog='python -m pybind11')
27-
parser.add_argument('--includes', action='store_true',
28-
help='Include flags for both pybind11 and Python headers.')
28+
parser = argparse.ArgumentParser(prog="python -m pybind11")
29+
parser.add_argument(
30+
"--includes",
31+
action="store_true",
32+
help="Include flags for both pybind11 and Python headers.",
33+
)
2934
args = parser.parse_args()
3035
if not sys.argv[1:]:
3136
parser.print_help()
3237
if args.includes:
3338
print_includes()
3439

3540

36-
if __name__ == '__main__':
41+
if __name__ == "__main__":
3742
main()

pybind11/_version.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# -*- coding: utf-8 -*-
2-
version_info = (2, 5, 'dev1')
3-
__version__ = '.'.join(map(str, version_info))
2+
3+
version_info = (2, 6, 0, "dev1")
4+
__version__ = ".".join(map(str, version_info))

pybind11/commands.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
4+
5+
DIR = os.path.abspath(os.path.dirname(__file__))
6+
7+
8+
def get_include(user=False):
9+
installed_path = os.path.join(DIR, "include")
10+
source_path = os.path.join(DIR, "include")
11+
return installed_path if os.path.exists(installed_path) else source_path
12+
13+
14+
def get_cmake_dir():
15+
cmake_installed_path = os.path.join(DIR, "share", "cmake", "pybind11")
16+
if os.path.exists(cmake_installed_path):
17+
return cmake_installed_path
18+
else:
19+
raise ImportError(
20+
"pybind11 not installed, installation required to access the CMake files"
21+
)

0 commit comments

Comments
 (0)