Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 1 | Build systems |
| 2 | ############# |
| 3 | |
| 4 | Building with setuptools |
| 5 | ======================== |
| 6 | |
| 7 | For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay |
| 8 | has kindly provided an example project which shows how to set up everything, |
| 9 | including automatic generation of documentation using Sphinx. Please refer to |
Wenzel Jakob | ca8dc08 | 2016-06-03 14:24:17 +0200 | [diff] [blame] | 10 | the [python_example]_ repository. |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 11 | |
Wenzel Jakob | ca8dc08 | 2016-06-03 14:24:17 +0200 | [diff] [blame] | 12 | .. [python_example] https://github.com/pybind/python_example |
Wenzel Jakob | 4a48afb | 2016-03-09 21:31:21 +0100 | [diff] [blame] | 13 | |
Wenzel Jakob | a439cca | 2016-05-17 10:47:52 +0200 | [diff] [blame] | 14 | Building 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 Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 24 | .. _cmake: |
| 25 | |
| 26 | Building with CMake |
| 27 | =================== |
| 28 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 29 | For C++ codebases that have an existing CMake-based build system, a Python |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 30 | extension module can be created with just a few lines of code: |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 31 | |
| 32 | .. code-block:: cmake |
| 33 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 34 | cmake_minimum_required(VERSION 2.8.12) |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 35 | project(example) |
| 36 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 37 | add_subdirectory(pybind11) |
| 38 | pybind11_add_module(example example.cpp) |
Wenzel Jakob | f64feaf | 2016-04-28 14:33:45 +0200 | [diff] [blame] | 39 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 40 | This assumes that the pybind11 repository is located in a subdirectory named |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 41 | :file:`pybind11` and that the code is located in a file named :file:`example.cpp`. |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 42 | The CMake command ``add_subdirectory`` will import the pybind11 project which |
| 43 | provides the ``pybind11_add_module`` function. It will take care of all the |
| 44 | details needed to build a Python extension module on any platform. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 45 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 46 | A working sample project, including a way to invoke CMake from :file:`setup.py` for |
| 47 | PyPI integration, can be found in the [cmake_example]_ repository. |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 48 | |
Wenzel Jakob | aa79af0 | 2016-06-03 12:23:24 +0200 | [diff] [blame] | 49 | .. [cmake_example] https://github.com/pybind/cmake_example |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 50 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 51 | pybind11_add_module |
| 52 | ------------------- |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 53 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 54 | To ease the creation of Python extension modules, pybind11 provides a CMake |
| 55 | function 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 | |
| 62 | This function behaves very much like CMake's builtin ``add_library`` (in fact, |
| 63 | it's a wrapper function around that command). It will add a library target |
| 64 | called ``<name>`` to be built from the listed source files. In addition, it |
| 65 | will take care of all the Python-specific compiler and linker flags as well |
| 66 | as 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 |
| 70 | type is given, ``MODULE`` is used by default which ensures the creation of a |
| 71 | Python-exclusive module. Specifying ``SHARED`` will create a more traditional |
| 72 | dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL`` |
| 73 | removes this target from the default build (see CMake docs for details). |
| 74 | |
| 75 | Since pybind11 is a template library, ``pybind11_add_module`` adds compiler |
| 76 | flags to ensure high quality code generation without bloat arising from long |
| 77 | symbol names and duplication of code in different translation units. The |
| 78 | additional flags enable LTO (Link Time Optimization), set default visibility |
| 79 | to *hidden* and strip unneeded symbols. See the :ref:`FAQ entry <faq:symhidden>` |
| 80 | for a more detailed explanation. These optimizations are never applied in |
| 81 | ``Debug`` mode. If ``NO_EXTRAS`` is given, they will always be disabled, even |
| 82 | in ``Release`` mode. However, this will result in code bloat and is generally |
| 83 | not recommended. |
| 84 | |
| 85 | As stated above, LTO is enabled by default. Some newer compilers also support |
| 86 | different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause |
| 87 | the function to prefer this flavor if available. The function falls back to |
| 88 | regular LTO if ``-flto=thin`` is not available. |
| 89 | |
| 90 | .. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html |
| 91 | |
| 92 | Configuration variables |
| 93 | ----------------------- |
| 94 | |
| 95 | By default, pybind11 will compile modules with the latest C++ standard |
| 96 | available on the target compiler. To override this, the standard flag can |
| 97 | be given explicitly in ``PYBIND11_CPP_STANDARD``: |
| 98 | |
| 99 | .. code-block:: cmake |
| 100 | |
| 101 | set(PYBIND11_CPP_STANDARD -std=c++11) |
| 102 | add_subdirectory(pybind11) # or find_package(pybind11) |
| 103 | |
| 104 | Note that this and all other configuration variables must be set **before** the |
| 105 | call to ``add_subdiretory`` or ``find_package``. The variables can also be set |
| 106 | when calling CMake from the command line using the ``-D<variable>=<value>`` flag. |
| 107 | |
| 108 | The target Python version can be selected by setting ``PYBIND11_PYTHON_VERSION`` |
| 109 | or an exact Python installation can be specified with ``PYTHON_EXECUTABLE``. |
| 110 | For example: |
| 111 | |
| 112 | .. code-block:: bash |
| 113 | |
| 114 | cmake -DPYBIND11_PYTHON_VERSION=3.6 .. |
| 115 | # or |
| 116 | cmake -DPYTHON_EXECUTABLE=path/to/python .. |
| 117 | |
| 118 | find_package vs. add_subdirectory |
| 119 | --------------------------------- |
| 120 | |
| 121 | For CMake-based projects that don't include the pybind11 repository internally, |
| 122 | an external installation can be detected through ``find_package(pybind11)``. |
| 123 | See the `Config file`_ docstring for details of relevant CMake variables. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 124 | |
| 125 | .. code-block:: cmake |
| 126 | |
| 127 | cmake_minimum_required(VERSION 2.8.12) |
| 128 | project(example) |
| 129 | |
| 130 | find_package(pybind11 REQUIRED) |
| 131 | pybind11_add_module(example example.cpp) |
| 132 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 133 | Once detected, the aforementioned ``pybind11_add_module`` can be employed as |
| 134 | before. The function usage and configuration variables are identical no matter |
| 135 | if pybind11 is added as a subdirectory or found as an installed package. You |
| 136 | can refer to the same [cmake_example]_ repository for a full sample project |
| 137 | -- just swap out ``add_subdirectory`` for ``find_package``. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 138 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 139 | .. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in |
| 140 | |
| 141 | Advanced: interface library target |
| 142 | ---------------------------------- |
| 143 | |
| 144 | When using a version of CMake greater than 3.0, pybind11 can additionally |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 145 | be used as a special *interface library* . The target ``pybind11::module`` |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 146 | is available with pybind11 headers, Python headers and libraries as needed, |
| 147 | and C++ compile definitions attached. This target is suitable for linking |
| 148 | to an independently constructed (through ``add_library``, not |
| 149 | ``pybind11_add_module``) target in the consuming project. |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 150 | |
| 151 | .. code-block:: cmake |
| 152 | |
| 153 | cmake_minimum_required(VERSION 3.0) |
| 154 | project(example) |
| 155 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 156 | find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 157 | |
Dean Moldovan | 0cbec5c | 2016-12-16 22:58:37 +0100 | [diff] [blame] | 158 | add_library(example MODULE main.cpp) |
Dean Moldovan | 71e8a79 | 2016-12-17 21:38:57 +0100 | [diff] [blame] | 159 | target_link_libraries(example PRIVATE pybind11::module) |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame] | 160 | set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}" |
| 161 | SUFFIX "${PYTHON_MODULE_EXTENSION}") |
| 162 | |
| 163 | .. warning:: |
| 164 | |
| 165 | Since pybind11 is a metatemplate library, it is crucial that certain |
| 166 | compiler flags are provided to ensure high quality code generation. In |
| 167 | contrast to the ``pybind11_add_module()`` command, the CMake interface |
| 168 | library only provides the *minimal* set of parameters to ensure that the |
| 169 | code using pybind11 compiles, but it does **not** pass these extra compiler |
| 170 | flags (i.e. this is up to you). |
| 171 | |
| 172 | These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL`` |
| 173 | and ``/LTCG`` on Visual Studio). Default-hidden symbols on GCC/Clang/ICPC |
| 174 | (``-fvisibility=hidden``) and .OBJ files with many sections on Visual Studio |
| 175 | (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an |
| 176 | explanation on why these are needed. |