blob: 9ec3bc273cd610dce693a0eb325e0831a612f2ad [file] [log] [blame]
Wenzel Jakob4a48afb2016-03-09 21:31:21 +01001Build systems
2#############
3
4Building with setuptools
5========================
6
7For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
8has kindly provided an example project which shows how to set up everything,
9including automatic generation of documentation using Sphinx. Please refer to
Wenzel Jakobca8dc082016-06-03 14:24:17 +020010the [python_example]_ repository.
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010011
Wenzel Jakobca8dc082016-06-03 14:24:17 +020012.. [python_example] https://github.com/pybind/python_example
Wenzel Jakob4a48afb2016-03-09 21:31:21 +010013
Wenzel Jakoba439cca2016-05-17 10:47:52 +020014Building with cppimport
15========================
16
17 cppimport is a small Python import hook that determines whether there is a C++
18 source file whose name matches the requested module. If there is, the file is
19 compiled as a Python extension using pybind11 and placed in the same folder as
20 the C++ source file. Python is then able to find the module and load it.
21
22.. [cppimport] https://github.com/tbenthompson/cppimport
23
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020024.. _cmake:
25
26Building with CMake
27===================
28
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020029For C++ codebases that have an existing CMake-based build system, a Python
30extension module can be created with just a few lines of code:
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020031
32.. code-block:: cmake
33
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020034 cmake_minimum_required(VERSION 2.8.12)
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020035 project(example)
36
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020037 add_subdirectory(pybind11)
38 pybind11_add_module(example example.cpp)
Wenzel Jakobf64feaf2016-04-28 14:33:45 +020039
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020040This assumes that the pybind11 repository is located in a subdirectory named
41:file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
42The CMake command ``add_subdirectory`` will import a function with the signature
43``pybind11_add_module(<name> source1 [source2 ...])``. It will take care of all
44the details needed to build a Python extension module on any platform.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020045
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020046The target Python version can be selected by setting the ``PYBIND11_PYTHON_VERSION``
47variable before adding the pybind11 subdirectory. Alternatively, an exact Python
48installation can be specified by setting ``PYTHON_EXECUTABLE``.
Wenzel Jakob28f98aa2015-10-13 02:57:16 +020049
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020050A working sample project, including a way to invoke CMake from :file:`setup.py` for
51PyPI integration, can be found in the [cmake_example]_ repository.
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010052
Wenzel Jakobaa79af02016-06-03 12:23:24 +020053.. [cmake_example] https://github.com/pybind/cmake_example