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`. |
| 42 | The CMake command ``add_subdirectory`` will import a function with the signature |
| 43 | ``pybind11_add_module(<name> source1 [source2 ...])``. It will take care of all |
| 44 | the details needed to build a Python extension module on any platform. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 45 | |
Wenzel Jakob | fe34241 | 2016-09-06 13:02:29 +0900 | [diff] [blame] | 46 | The target Python version can be selected by setting the ``PYBIND11_PYTHON_VERSION`` |
| 47 | variable before adding the pybind11 subdirectory. Alternatively, an exact Python |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 48 | installation can be specified by setting ``PYTHON_EXECUTABLE``. |
Wenzel Jakob | 28f98aa | 2015-10-13 02:57:16 +0200 | [diff] [blame] | 49 | |
Dean Moldovan | 24ddf4b | 2016-05-27 00:11:52 +0200 | [diff] [blame] | 50 | A working sample project, including a way to invoke CMake from :file:`setup.py` for |
| 51 | PyPI integration, can be found in the [cmake_example]_ repository. |
Wenzel Jakob | caa9d44 | 2016-01-17 22:36:34 +0100 | [diff] [blame] | 52 | |
Wenzel Jakob | aa79af0 | 2016-06-03 12:23:24 +0200 | [diff] [blame] | 53 | .. [cmake_example] https://github.com/pybind/cmake_example |
Lori A. Burns | 5cafc99 | 2016-12-13 10:55:38 -0500 | [diff] [blame^] | 54 | |
| 55 | For CMake-based projects that don't include the pybind11 |
| 56 | repository internally, an external installation can be detected |
| 57 | through `find_package(pybind11 ... CONFIG ...)`. See the `Config file |
| 58 | <https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in>`_ |
| 59 | docstring for details of relevant CMake variables. |
| 60 | |
| 61 | Once detected, and after setting any variables to guide Python and C++ |
| 62 | standard detection, the aforementioned ``pybind11_add_module`` |
| 63 | wrapper to ``add_library`` can |
| 64 | be employed as described above (after ``include(pybind11Tools)``). This |
| 65 | procedure is available when using CMake >= 2.8.12. A |
| 66 | working example can be found at [test_installed_module]_ . |
| 67 | |
| 68 | .. code-block:: cmake |
| 69 | |
| 70 | cmake_minimum_required(VERSION 2.8.12) |
| 71 | project(example) |
| 72 | |
| 73 | find_package(pybind11 REQUIRED) |
| 74 | pybind11_add_module(example example.cpp) |
| 75 | |
| 76 | .. [test_installed_module] https://github.com/pybind/pybind11/blob/master/tests/test_installed_module/CMakeLists.txt |
| 77 | |
| 78 | When using a version of CMake greater than 3.0, pybind11 can |
| 79 | additionally be used as a special *interface library* following the call |
| 80 | to ``find_package``. CMake |
| 81 | variables to guide Python and C++ standard detection should be set |
| 82 | *before* ``find_package``. When ``find_package`` returns, the target |
| 83 | ``pybind11::pybind11`` is available with pybind11 headers, Python headers |
| 84 | and libraries as needed, and C++ compile definitions attached. This |
| 85 | target is suitable for linking to an independently constructed (through |
| 86 | ``add_library``, not ``pybind11_add_module``) target in the consuming |
| 87 | project. A working example can be found at [test_installed_target]_ . |
| 88 | |
| 89 | .. code-block:: cmake |
| 90 | |
| 91 | cmake_minimum_required(VERSION 3.0) |
| 92 | project(example) |
| 93 | |
| 94 | add_library(example MODULE main.cpp) |
| 95 | |
| 96 | find_package(pybind11 REQUIRED) |
| 97 | target_link_libraries(example PRIVATE pybind11::pybind11) |
| 98 | set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}" |
| 99 | SUFFIX "${PYTHON_MODULE_EXTENSION}") |
| 100 | |
| 101 | .. warning:: |
| 102 | |
| 103 | Since pybind11 is a metatemplate library, it is crucial that certain |
| 104 | compiler flags are provided to ensure high quality code generation. In |
| 105 | contrast to the ``pybind11_add_module()`` command, the CMake interface |
| 106 | library only provides the *minimal* set of parameters to ensure that the |
| 107 | code using pybind11 compiles, but it does **not** pass these extra compiler |
| 108 | flags (i.e. this is up to you). |
| 109 | |
| 110 | These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL`` |
| 111 | and ``/LTCG`` on Visual Studio). Default-hidden symbols on GCC/Clang/ICPC |
| 112 | (``-fvisibility=hidden``) and .OBJ files with many sections on Visual Studio |
| 113 | (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an |
| 114 | explanation on why these are needed. |
| 115 | |
| 116 | .. [test_installed_target] https://github.com/pybind/pybind11/blob/master/tests/test_installed_target/CMakeLists.txt |
| 117 | |