blob: 90b3829523f4e84dd9db333c3d0f82f8996158ec [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
Wenzel Jakobfe342412016-09-06 13:02:29 +090029For C++ codebases that have an existing CMake-based build system, a Python
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020030extension 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
Wenzel Jakobfe342412016-09-06 13:02:29 +090040This assumes that the pybind11 repository is located in a subdirectory named
Dean Moldovan24ddf4b2016-05-27 00:11:52 +020041:file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
Dean Moldovan0cbec5c2016-12-16 22:58:37 +010042The CMake command ``add_subdirectory`` will import the pybind11 project which
43provides the ``pybind11_add_module`` function. It will take care of all the
44details 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 +020046A working sample project, including a way to invoke CMake from :file:`setup.py` for
47PyPI integration, can be found in the [cmake_example]_ repository.
Wenzel Jakobcaa9d442016-01-17 22:36:34 +010048
Wenzel Jakobaa79af02016-06-03 12:23:24 +020049.. [cmake_example] https://github.com/pybind/cmake_example
Lori A. Burns5cafc992016-12-13 10:55:38 -050050
Dean Moldovan0cbec5c2016-12-16 22:58:37 +010051pybind11_add_module
52-------------------
Lori A. Burns5cafc992016-12-13 10:55:38 -050053
Dean Moldovan0cbec5c2016-12-16 22:58:37 +010054To ease the creation of Python extension modules, pybind11 provides a CMake
55function with the following signature:
56
57.. code-block:: cmake
58
59 pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
60 [NO_EXTRAS] [THIN_LTO] source1 [source2 ...])
61
62This function behaves very much like CMake's builtin ``add_library`` (in fact,
63it's a wrapper function around that command). It will add a library target
64called ``<name>`` to be built from the listed source files. In addition, it
65will take care of all the Python-specific compiler and linker flags as well
66as the OS- and Python-version-specific file extension. The produced target
67``<name>`` can be further manipulated with regular CMake commands.
68
69``MODULE`` or ``SHARED`` may be given to specify the type of library. If no
70type is given, ``MODULE`` is used by default which ensures the creation of a
71Python-exclusive module. Specifying ``SHARED`` will create a more traditional
72dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL``
73removes this target from the default build (see CMake docs for details).
74
75Since pybind11 is a template library, ``pybind11_add_module`` adds compiler
76flags to ensure high quality code generation without bloat arising from long
Jason Rhinelander97aa54f2017-08-10 12:08:42 -040077symbol names and duplication of code in different translation units. It
78sets default visibility to *hidden*, which is required for some pybind11
79features and functionality when attempting to load multiple pybind11 modules
80compiled under different pybind11 versions. It also adds additional flags
81enabling LTO (Link Time Optimization) and strip unneeded symbols. See the
82:ref:`FAQ entry <faq:symhidden>` for a more detailed explanation. These
83latter optimizations are never applied in ``Debug`` mode. If ``NO_EXTRAS`` is
84given, they will always be disabled, even in ``Release`` mode. However, this
85will result in code bloat and is generally not recommended.
Dean Moldovan0cbec5c2016-12-16 22:58:37 +010086
87As stated above, LTO is enabled by default. Some newer compilers also support
88different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause
89the function to prefer this flavor if available. The function falls back to
90regular LTO if ``-flto=thin`` is not available.
91
92.. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html
93
94Configuration variables
95-----------------------
96
Jason Rhinelander77710ff2017-05-09 14:37:48 -040097By default, pybind11 will compile modules with the C++14 standard, if available
98on the target compiler, falling back to C++11 if C++14 support is not
99available. Note, however, that this default is subject to change: future
100pybind11 releases are expected to migrate to newer C++ standards as they become
101available. To override this, the standard flag can be given explicitly in
102``PYBIND11_CPP_STANDARD``:
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100103
104.. code-block:: cmake
105
Jason Rhinelander77710ff2017-05-09 14:37:48 -0400106 # Use just one of these:
107 # GCC/clang:
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100108 set(PYBIND11_CPP_STANDARD -std=c++11)
Jason Rhinelander77710ff2017-05-09 14:37:48 -0400109 set(PYBIND11_CPP_STANDARD -std=c++14)
110 set(PYBIND11_CPP_STANDARD -std=c++1z) # Experimental C++17 support
111 # MSVC:
112 set(PYBIND11_CPP_STANDARD /std:c++14)
113 set(PYBIND11_CPP_STANDARD /std:c++latest) # Enables some MSVC C++17 features
114
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100115 add_subdirectory(pybind11) # or find_package(pybind11)
116
117Note that this and all other configuration variables must be set **before** the
Jason Rhinelander77710ff2017-05-09 14:37:48 -0400118call to ``add_subdirectory`` or ``find_package``. The variables can also be set
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100119when calling CMake from the command line using the ``-D<variable>=<value>`` flag.
120
121The target Python version can be selected by setting ``PYBIND11_PYTHON_VERSION``
122or an exact Python installation can be specified with ``PYTHON_EXECUTABLE``.
123For example:
124
125.. code-block:: bash
126
127 cmake -DPYBIND11_PYTHON_VERSION=3.6 ..
128 # or
129 cmake -DPYTHON_EXECUTABLE=path/to/python ..
130
131find_package vs. add_subdirectory
132---------------------------------
133
134For CMake-based projects that don't include the pybind11 repository internally,
135an external installation can be detected through ``find_package(pybind11)``.
136See the `Config file`_ docstring for details of relevant CMake variables.
Lori A. Burns5cafc992016-12-13 10:55:38 -0500137
138.. code-block:: cmake
139
140 cmake_minimum_required(VERSION 2.8.12)
141 project(example)
142
143 find_package(pybind11 REQUIRED)
144 pybind11_add_module(example example.cpp)
145
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100146Once detected, the aforementioned ``pybind11_add_module`` can be employed as
147before. The function usage and configuration variables are identical no matter
148if pybind11 is added as a subdirectory or found as an installed package. You
149can refer to the same [cmake_example]_ repository for a full sample project
150-- just swap out ``add_subdirectory`` for ``find_package``.
Lori A. Burns5cafc992016-12-13 10:55:38 -0500151
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100152.. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in
153
154Advanced: interface library target
155----------------------------------
156
157When using a version of CMake greater than 3.0, pybind11 can additionally
Dean Moldovan71e8a792016-12-17 21:38:57 +0100158be used as a special *interface library* . The target ``pybind11::module``
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100159is available with pybind11 headers, Python headers and libraries as needed,
160and C++ compile definitions attached. This target is suitable for linking
161to an independently constructed (through ``add_library``, not
162``pybind11_add_module``) target in the consuming project.
Lori A. Burns5cafc992016-12-13 10:55:38 -0500163
164.. code-block:: cmake
165
166 cmake_minimum_required(VERSION 3.0)
167 project(example)
168
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100169 find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
Lori A. Burns5cafc992016-12-13 10:55:38 -0500170
Dean Moldovan0cbec5c2016-12-16 22:58:37 +0100171 add_library(example MODULE main.cpp)
Dean Moldovan71e8a792016-12-17 21:38:57 +0100172 target_link_libraries(example PRIVATE pybind11::module)
Lori A. Burns5cafc992016-12-13 10:55:38 -0500173 set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
174 SUFFIX "${PYTHON_MODULE_EXTENSION}")
175
176.. warning::
177
178 Since pybind11 is a metatemplate library, it is crucial that certain
179 compiler flags are provided to ensure high quality code generation. In
180 contrast to the ``pybind11_add_module()`` command, the CMake interface
181 library only provides the *minimal* set of parameters to ensure that the
182 code using pybind11 compiles, but it does **not** pass these extra compiler
183 flags (i.e. this is up to you).
184
185 These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL``
Jason Rhinelander97aa54f2017-08-10 12:08:42 -0400186 and ``/LTCG`` on Visual Studio) and .OBJ files with many sections on Visual
187 Studio (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
Lori A. Burns5cafc992016-12-13 10:55:38 -0500188 explanation on why these are needed.
Wenzel Jakobf3de2d52016-12-26 13:34:28 +0100189
Dean Moldovan6d2411f2017-04-22 23:24:13 +0200190Embedding the Python interpreter
191--------------------------------
192
193In addition to extension modules, pybind11 also supports embedding Python into
194a C++ executable or library. In CMake, simply link with the ``pybind11::embed``
195target. It provides everything needed to get the interpreter running. The Python
196headers and libraries are attached to the target. Unlike ``pybind11::module``,
197there is no need to manually set any additional properties here. For more
198information about usage in C++, see :doc:`/advanced/embedding`.
199
200.. code-block:: cmake
201
202 cmake_minimum_required(VERSION 3.0)
203 project(example)
204
205 find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
206
Dean Moldovan8f6c1292017-05-31 13:48:39 +0200207 add_executable(example main.cpp)
Dean Moldovan6d2411f2017-04-22 23:24:13 +0200208 target_link_libraries(example PRIVATE pybind11::embed)
209
210
Wenzel Jakobf3de2d52016-12-26 13:34:28 +0100211Generating binding code automatically
212=====================================
213
214The ``Binder`` project is a tool for automatic generation of pybind11 binding
215code by introspecting existing C++ codebases using LLVM/Clang. See the
216[binder]_ documentation for details.
217
218.. [binder] http://cppbinder.readthedocs.io/en/latest/about.html