• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1Thank you for your interest in this project! Please refer to the following
2sections on how to contribute code and bug reports.
3
4### Reporting bugs
5
6Before submitting a question or bug report, please take a moment of your time
7and ensure that your issue isn't already discussed in the project documentation
8provided at [pybind11.readthedocs.org][] or in the [issue tracker][]. You can
9also check [gitter][] to see if it came up before.
10
11Assuming that you have identified a previously unknown problem or an important
12question, it's essential that you submit a self-contained and minimal piece of
13code that reproduces the problem. In other words: no external dependencies,
14isolate the function(s) that cause breakage, submit matched and complete C++
15and Python snippets that can be easily compiled and run in isolation; or
16ideally make a small PR with a failing test case that can be used as a starting
17point.
18
19## Pull requests
20
21Contributions are submitted, reviewed, and accepted using GitHub pull requests.
22Please refer to [this article][using pull requests] for details and adhere to
23the following rules to make the process as smooth as possible:
24
25* Make a new branch for every feature you're working on.
26* Make small and clean pull requests that are easy to review but make sure they
27  do add value by themselves.
28* Add tests for any new functionality and run the test suite (`cmake --build
29  build --target pytest`) to ensure that no existing features break.
30* Please run [`pre-commit`][pre-commit] to check your code matches the
31  project style. (Note that `gawk` is required.) Use `pre-commit run
32  --all-files` before committing (or use installed-mode, check pre-commit docs)
33  to verify your code passes before pushing to save time.
34* This project has a strong focus on providing general solutions using a
35  minimal amount of code, thus small pull requests are greatly preferred.
36
37### Licensing of contributions
38
39pybind11 is provided under a BSD-style license that can be found in the
40``LICENSE`` file. By using, distributing, or contributing to this project, you
41agree to the terms and conditions of this license.
42
43You are under no obligation whatsoever to provide any bug fixes, patches, or
44upgrades to the features, functionality or performance of the source code
45("Enhancements") to anyone; however, if you choose to make your Enhancements
46available either publicly, or directly to the author of this software, without
47imposing a separate written license agreement for such Enhancements, then you
48hereby grant the following license: a non-exclusive, royalty-free perpetual
49license to install, use, modify, prepare derivative works, incorporate into
50other computer software, distribute, and sublicense such enhancements or
51derivative works thereof, in binary and source code form.
52
53
54## Development of pybind11
55
56To setup an ideal development environment, run the following commands on a
57system with CMake 3.14+:
58
59```bash
60python3 -m venv venv
61source venv/bin/activate
62pip install -r tests/requirements.txt
63cmake -S . -B build -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON
64cmake --build build -j4
65```
66
67Tips:
68
69* You can use `virtualenv` (from PyPI) instead of `venv` (which is Python 3
70  only).
71* You can select any name for your environment folder; if it contains "env" it
72  will be ignored by git.
73* If you don’t have CMake 3.14+, just add “cmake” to the pip install command.
74* You can use `-DPYBIND11_FINDPYTHON=ON` to use FindPython on CMake 3.12+
75* In classic mode, you may need to set `-DPYTHON_EXECUTABLE=/path/to/python`.
76  FindPython uses `-DPython_ROOT_DIR=/path/to` or
77  `-DPython_EXECUTABLE=/path/to/python`.
78
79### Configuration options
80
81In CMake, configuration options are given with “-D”. Options are stored in the
82build directory, in the `CMakeCache.txt` file, so they are remembered for each
83build directory. Two selections are special - the generator, given with `-G`,
84and the compiler, which is selected based on environment variables `CXX` and
85similar, or `-DCMAKE_CXX_COMPILER=`. Unlike the others, these cannot be changed
86after the initial run.
87
88The valid options are:
89
90* `-DCMAKE_BUILD_TYPE`: Release, Debug, MinSizeRel, RelWithDebInfo
91* `-DPYBIND11_FINDPYTHON=ON`: Use CMake 3.12+’s FindPython instead of the
92  classic, deprecated, custom FindPythonLibs
93* `-DPYBIND11_NOPYTHON=ON`: Disable all Python searching (disables tests)
94* `-DBUILD_TESTING=ON`: Enable the tests
95* `-DDOWNLOAD_CATCH=ON`: Download catch to build the C++ tests
96* `-DOWNLOAD_EIGEN=ON`: Download Eigen for the NumPy tests
97* `-DPYBIND11_INSTALL=ON/OFF`: Enable the install target (on by default for the
98  master project)
99* `-DUSE_PYTHON_INSTALL_DIR=ON`: Try to install into the python dir
100
101
102<details><summary>A few standard CMake tricks: (click to expand)</summary><p>
103
104* Use `cmake --build build -v` to see the commands used to build the files.
105* Use `cmake build -LH` to list the CMake options with help.
106* Use `ccmake` if available to see a curses (terminal) gui, or `cmake-gui` for
107  a completely graphical interface (not present in the PyPI package).
108* Use `cmake --build build -j12` to build with 12 cores (for example).
109* Use `-G` and the name of a generator to use something different. `cmake
110  --help` lists the generators available.
111      - On Unix, setting `CMAKE_GENERATER=Ninja` in your environment will give
112        you automatic mulithreading on all your CMake projects!
113* Open the `CMakeLists.txt` with QtCreator to generate for that IDE.
114* You can use `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON` to generate the `.json` file
115  that some tools expect.
116
117</p></details>
118
119
120To run the tests, you can "build" the check target:
121
122```bash
123cmake --build build --target check
124```
125
126`--target` can be spelled `-t` in CMake 3.15+. You can also run individual
127tests with these targets:
128
129* `pytest`: Python tests only, using the
130[pytest](https://docs.pytest.org/en/stable/) framework
131* `cpptest`: C++ tests only
132* `test_cmake_build`: Install / subdirectory tests
133
134If you want to build just a subset of tests, use
135`-DPYBIND11_TEST_OVERRIDE="test_callbacks.cpp;test_pickling.cpp"`. If this is
136empty, all tests will be built.
137
138You may also pass flags to the `pytest` target by editing `tests/pytest.ini` or
139by using the `PYTEST_ADDOPTS` environment variable
140(see [`pytest` docs](https://docs.pytest.org/en/2.7.3/customize.html#adding-default-options)). As an example:
141
142```bash
143env PYTEST_ADDOPTS="--capture=no --exitfirst" \
144    cmake --build build --target pytest
145# Or using abbreviated flags
146env PYTEST_ADDOPTS="-s -x" cmake --build build --target pytest
147```
148
149### Formatting
150
151All formatting is handled by pre-commit.
152
153Install with brew (macOS) or pip (any OS):
154
155```bash
156# Any OS
157python3 -m pip install pre-commit
158
159# OR macOS with homebrew:
160brew install pre-commit
161```
162
163Then, you can run it on the items you've added to your staging area, or all
164files:
165
166```bash
167pre-commit run
168# OR
169pre-commit run --all-files
170```
171
172And, if you want to always use it, you can install it as a git hook (hence the
173name, pre-commit):
174
175```bash
176pre-commit install
177```
178
179### Clang-Format
180
181As of v2.6.2, pybind11 ships with a [`clang-format`][clang-format]
182configuration file at the top level of the repo (the filename is
183`.clang-format`). Currently, formatting is NOT applied automatically, but
184manually using `clang-format` for newly developed files is highly encouraged.
185To check if a file needs formatting:
186
187```bash
188clang-format -style=file --dry-run some.cpp
189```
190
191The output will show things to be fixed, if any. To actually format the file:
192
193```bash
194clang-format -style=file -i some.cpp
195```
196
197Note that the `-style-file` option searches the parent directories for the
198`.clang-format` file, i.e. the commands above can be run in any subdirectory
199of the pybind11 repo.
200
201### Clang-Tidy
202
203[`clang-tidy`][clang-tidy] performs deeper static code analyses and is
204more complex to run, compared to `clang-format`, but support for `clang-tidy`
205is built into the pybind11 CMake configuration. To run `clang-tidy`, the
206following recipe should work. Files will be modified in place, so you can
207use git to monitor the changes.
208
209```bash
210docker run --rm -v $PWD:/pybind11 -it silkeh/clang:10
211apt-get update && apt-get install python3-dev python3-pytest
212cmake -S pybind11/ -B build -DCMAKE_CXX_CLANG_TIDY="$(which clang-tidy);-fix"
213cmake --build build
214```
215
216### Include what you use
217
218To run include what you use, install (`brew install include-what-you-use` on
219macOS), then run:
220
221```bash
222cmake -S . -B build-iwyu -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=$(which include-what-you-use)
223cmake --build build
224```
225
226The report is sent to stderr; you can pipe it into a file if you wish.
227
228### Build recipes
229
230This builds with the Intel compiler (assuming it is in your path, along with a
231recent CMake and Python 3):
232
233```bash
234python3 -m venv venv
235. venv/bin/activate
236pip install pytest
237cmake -S . -B build-intel -DCMAKE_CXX_COMPILER=$(which icpc) -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON -DPYBIND11_WERROR=ON
238```
239
240This will test the PGI compilers:
241
242```bash
243docker run --rm -it -v $PWD:/pybind11 nvcr.io/hpc/pgi-compilers:ce
244apt-get update && apt-get install -y python3-dev python3-pip python3-pytest
245wget -qO- "https://cmake.org/files/v3.18/cmake-3.18.2-Linux-x86_64.tar.gz" | tar --strip-components=1 -xz -C /usr/local
246cmake -S pybind11/ -B build
247cmake --build build
248```
249
250### Explanation of the SDist/wheel building design
251
252> These details below are _only_ for packaging the Python sources from git. The
253> SDists and wheels created do not have any extra requirements at all and are
254> completely normal.
255
256The main objective of the packaging system is to create SDists (Python's source
257distribution packages) and wheels (Python's binary distribution packages) that
258include everything that is needed to work with pybind11, and which can be
259installed without any additional dependencies. This is more complex than it
260appears: in order to support CMake as a first class language even when using
261the PyPI package, they must include the _generated_ CMake files (so as not to
262require CMake when installing the `pybind11` package itself). They should also
263provide the option to install to the "standard" location
264(`<ENVROOT>/include/pybind11` and `<ENVROOT>/share/cmake/pybind11`) so they are
265easy to find with CMake, but this can cause problems if you are not an
266environment or using ``pyproject.toml`` requirements. This was solved by having
267two packages; the "nice" pybind11 package that stores the includes and CMake
268files inside the package, that you get access to via functions in the package,
269and a `pybind11-global` package that can be included via `pybind11[global]` if
270you want the more invasive but discoverable file locations.
271
272If you want to install or package the GitHub source, it is best to have Pip 10
273or newer on Windows, macOS, or Linux (manylinux1 compatible, includes most
274distributions).  You can then build the SDists, or run any procedure that makes
275SDists internally, like making wheels or installing.
276
277
278```bash
279# Editable development install example
280python3 -m pip install -e .
281```
282
283Since Pip itself does not have an `sdist` command (it does have `wheel` and
284`install`), you may want to use the upcoming `build` package:
285
286```bash
287python3 -m pip install build
288
289# Normal package
290python3 -m build -s .
291
292# Global extra
293PYBIND11_GLOBAL_SDIST=1 python3 -m build -s .
294```
295
296If you want to use the classic "direct" usage of `python setup.py`, you will
297need CMake 3.15+ and either `make` or `ninja` preinstalled (possibly via `pip
298install cmake ninja`), since directly running Python on `setup.py` cannot pick
299up and install `pyproject.toml` requirements. As long as you have those two
300things, though, everything works the way you would expect:
301
302```bash
303# Normal package
304python3 setup.py sdist
305
306# Global extra
307PYBIND11_GLOBAL_SDIST=1 python3 setup.py sdist
308```
309
310A detailed explanation of the build procedure design for developers wanting to
311work on or maintain the packaging system is as follows:
312
313#### 1. Building from the source directory
314
315When you invoke any `setup.py` command from the source directory, including
316`pip wheel .` and `pip install .`, you will activate a full source build. This
317is made of the following steps:
318
3191. If the tool is PEP 518 compliant, like Pip 10+, it will create a temporary
320   virtual environment and install the build requirements (mostly CMake) into
321   it. (if you are not on Windows, macOS, or a manylinux compliant system, you
322   can disable this with `--no-build-isolation` as long as you have CMake 3.15+
323   installed)
3242. The environment variable `PYBIND11_GLOBAL_SDIST` is checked - if it is set
325   and truthy, this will be make the accessory `pybind11-global` package,
326   instead of the normal `pybind11` package. This package is used for
327   installing the files directly to your environment root directory, using
328   `pybind11[global]`.
3292. `setup.py` reads the version from `pybind11/_version.py` and verifies it
330   matches `includes/pybind11/detail/common.h`.
3313. CMake is run with `-DCMAKE_INSTALL_PREIFX=pybind11`. Since the CMake install
332   procedure uses only relative paths and is identical on all platforms, these
333   files are valid as long as they stay in the correct relative position to the
334   includes. `pybind11/share/cmake/pybind11` has the CMake files, and
335   `pybind11/include` has the includes. The build directory is discarded.
3364. Simpler files are placed in the SDist: `tools/setup_*.py.in`,
337   `tools/pyproject.toml` (`main` or `global`)
3385. The package is created by running the setup function in the
339   `tools/setup_*.py`.  `setup_main.py` fills in Python packages, and
340   `setup_global.py` fills in only the data/header slots.
3416. A context manager cleans up the temporary CMake install directory (even if
342   an error is thrown).
343
344### 2. Building from SDist
345
346Since the SDist has the rendered template files in `tools` along with the
347includes and CMake files in the correct locations, the builds are completely
348trivial and simple. No extra requirements are required. You can even use Pip 9
349if you really want to.
350
351
352[pre-commit]: https://pre-commit.com
353[clang-format]: https://clang.llvm.org/docs/ClangFormat.html
354[clang-tidy]: https://clang.llvm.org/extra/clang-tidy/
355[pybind11.readthedocs.org]: http://pybind11.readthedocs.org/en/latest
356[issue tracker]: https://github.com/pybind/pybind11/issues
357[gitter]: https://gitter.im/pybind/Lobby
358[using pull requests]: https://help.github.com/articles/using-pull-requests
359