Skip to content

Commit 9cf0e50

Browse files
committed
feat: setup.py redesign and helpers
1 parent 9b8cb02 commit 9cf0e50

File tree

17 files changed

+564
-150
lines changed

17 files changed

+564
-150
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,28 +332,34 @@ jobs:
332332
doxygen:
333333
name: "Documentation build test"
334334
runs-on: ubuntu-latest
335-
container: alpine:3.12
336335

337336
steps:
338337
- uses: actions/checkout@v2
339338

340-
- name: Install system requirements
341-
run: apk add doxygen python3-dev
339+
- uses: actions/setup-python@v2
342340

343-
- name: Ensure pip
344-
run: python3 -m ensurepip
341+
- name: Install Doxygen
342+
run: sudo apt install -y doxygen
345343

346344
- name: Install docs & setup requirements
347-
run: python3 -m pip install -r docs/requirements.txt pytest setuptools
345+
run: python3 -m pip install -r docs/requirements.txt
348346

349347
- name: Build docs
350348
run: python3 -m sphinx -W -b html docs docs/.build
351349

352350
- name: Make SDist
353351
run: python3 setup.py sdist
354352

353+
- run: git status --ignored
354+
355+
- name: Check local include dir
356+
run: >
357+
ls pybind11;
358+
python3 -c "import pybind11, pathlib; assert (a := pybind11.get_include()) == (b := str(pathlib.Path('include').resolve())), f'{a} != {b}'"
359+
355360
- name: Compare Dists (headers only)
361+
working-directory: include
356362
run: |
357-
python3 -m pip install --user -U ./dist/*
358-
installed=$(python3 -c "import pybind11; print(pybind11.get_include(True) + '/pybind11')")
359-
diff -rq $installed ./include/pybind11
363+
python3 -m pip install --user -U ../dist/*
364+
installed=$(python3 -c "import pybind11; print(pybind11.get_include() + '/pybind11')")
365+
diff -rq $installed ./pybind11

.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/action@v2.0.0
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ repos:
1414
- id: trailing-whitespace
1515
- id: fix-encoding-pragma
1616

17+
- repo: https://github.com/psf/black
18+
rev: 19.10b0
19+
hooks:
20+
- id: black
21+
files: ^(setup.py|pybind11)
22+
1723
- repo: https://github.com/Lucas-C/pre-commit-hooks
1824
rev: v1.1.9
1925
hooks:
@@ -34,6 +40,13 @@ repos:
3440
types: [file]
3541
files: (\.cmake|CMakeLists.txt)(.in)?$
3642

43+
- repo: https://github.com/mgedmin/check-manifest
44+
rev: "0.42"
45+
hooks:
46+
- id: check-manifest
47+
stages: [manual]
48+
additional_dependencies: [cmake, ninja]
49+
3750
- repo: local
3851
hooks:
3952
- 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(os.path.dirname(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)