You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
7.6 KiB

8 years ago
  1. Build systems
  2. #############
  3. Building with setuptools
  4. ========================
  5. For projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
  6. has kindly provided an example project which shows how to set up everything,
  7. including automatic generation of documentation using Sphinx. Please refer to
  8. the [python_example]_ repository.
  9. .. [python_example] https://github.com/pybind/python_example
  10. Building with cppimport
  11. ========================
  12. cppimport is a small Python import hook that determines whether there is a C++
  13. source file whose name matches the requested module. If there is, the file is
  14. compiled as a Python extension using pybind11 and placed in the same folder as
  15. the C++ source file. Python is then able to find the module and load it.
  16. .. [cppimport] https://github.com/tbenthompson/cppimport
  17. .. _cmake:
  18. Building with CMake
  19. ===================
  20. For C++ codebases that have an existing CMake-based build system, a Python
  21. extension module can be created with just a few lines of code:
  22. .. code-block:: cmake
  23. cmake_minimum_required(VERSION 2.8.12)
  24. project(example)
  25. add_subdirectory(pybind11)
  26. pybind11_add_module(example example.cpp)
  27. This assumes that the pybind11 repository is located in a subdirectory named
  28. :file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
  29. The CMake command ``add_subdirectory`` will import the pybind11 project which
  30. provides the ``pybind11_add_module`` function. It will take care of all the
  31. details needed to build a Python extension module on any platform.
  32. A working sample project, including a way to invoke CMake from :file:`setup.py` for
  33. PyPI integration, can be found in the [cmake_example]_ repository.
  34. .. [cmake_example] https://github.com/pybind/cmake_example
  35. pybind11_add_module
  36. -------------------
  37. To ease the creation of Python extension modules, pybind11 provides a CMake
  38. function with the following signature:
  39. .. code-block:: cmake
  40. pybind11_add_module(<name> [MODULE | SHARED] [EXCLUDE_FROM_ALL]
  41. [NO_EXTRAS] [THIN_LTO] source1 [source2 ...])
  42. This function behaves very much like CMake's builtin ``add_library`` (in fact,
  43. it's a wrapper function around that command). It will add a library target
  44. called ``<name>`` to be built from the listed source files. In addition, it
  45. will take care of all the Python-specific compiler and linker flags as well
  46. as the OS- and Python-version-specific file extension. The produced target
  47. ``<name>`` can be further manipulated with regular CMake commands.
  48. ``MODULE`` or ``SHARED`` may be given to specify the type of library. If no
  49. type is given, ``MODULE`` is used by default which ensures the creation of a
  50. Python-exclusive module. Specifying ``SHARED`` will create a more traditional
  51. dynamic library which can also be linked from elsewhere. ``EXCLUDE_FROM_ALL``
  52. removes this target from the default build (see CMake docs for details).
  53. Since pybind11 is a template library, ``pybind11_add_module`` adds compiler
  54. flags to ensure high quality code generation without bloat arising from long
  55. symbol names and duplication of code in different translation units. The
  56. additional flags enable LTO (Link Time Optimization), set default visibility
  57. to *hidden* and strip unneeded symbols. See the :ref:`FAQ entry <faq:symhidden>`
  58. for a more detailed explanation. These optimizations are never applied in
  59. ``Debug`` mode. If ``NO_EXTRAS`` is given, they will always be disabled, even
  60. in ``Release`` mode. However, this will result in code bloat and is generally
  61. not recommended.
  62. As stated above, LTO is enabled by default. Some newer compilers also support
  63. different flavors of LTO such as `ThinLTO`_. Setting ``THIN_LTO`` will cause
  64. the function to prefer this flavor if available. The function falls back to
  65. regular LTO if ``-flto=thin`` is not available.
  66. .. _ThinLTO: http://clang.llvm.org/docs/ThinLTO.html
  67. Configuration variables
  68. -----------------------
  69. By default, pybind11 will compile modules with the latest C++ standard
  70. available on the target compiler. To override this, the standard flag can
  71. be given explicitly in ``PYBIND11_CPP_STANDARD``:
  72. .. code-block:: cmake
  73. set(PYBIND11_CPP_STANDARD -std=c++11)
  74. add_subdirectory(pybind11) # or find_package(pybind11)
  75. Note that this and all other configuration variables must be set **before** the
  76. call to ``add_subdiretory`` or ``find_package``. The variables can also be set
  77. when calling CMake from the command line using the ``-D<variable>=<value>`` flag.
  78. The target Python version can be selected by setting ``PYBIND11_PYTHON_VERSION``
  79. or an exact Python installation can be specified with ``PYTHON_EXECUTABLE``.
  80. For example:
  81. .. code-block:: bash
  82. cmake -DPYBIND11_PYTHON_VERSION=3.6 ..
  83. # or
  84. cmake -DPYTHON_EXECUTABLE=path/to/python ..
  85. find_package vs. add_subdirectory
  86. ---------------------------------
  87. For CMake-based projects that don't include the pybind11 repository internally,
  88. an external installation can be detected through ``find_package(pybind11)``.
  89. See the `Config file`_ docstring for details of relevant CMake variables.
  90. .. code-block:: cmake
  91. cmake_minimum_required(VERSION 2.8.12)
  92. project(example)
  93. find_package(pybind11 REQUIRED)
  94. pybind11_add_module(example example.cpp)
  95. Once detected, the aforementioned ``pybind11_add_module`` can be employed as
  96. before. The function usage and configuration variables are identical no matter
  97. if pybind11 is added as a subdirectory or found as an installed package. You
  98. can refer to the same [cmake_example]_ repository for a full sample project
  99. -- just swap out ``add_subdirectory`` for ``find_package``.
  100. .. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in
  101. Advanced: interface library target
  102. ----------------------------------
  103. When using a version of CMake greater than 3.0, pybind11 can additionally
  104. be used as a special *interface library* . The target ``pybind11::module``
  105. is available with pybind11 headers, Python headers and libraries as needed,
  106. and C++ compile definitions attached. This target is suitable for linking
  107. to an independently constructed (through ``add_library``, not
  108. ``pybind11_add_module``) target in the consuming project.
  109. .. code-block:: cmake
  110. cmake_minimum_required(VERSION 3.0)
  111. project(example)
  112. find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
  113. add_library(example MODULE main.cpp)
  114. target_link_libraries(example PRIVATE pybind11::module)
  115. set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
  116. SUFFIX "${PYTHON_MODULE_EXTENSION}")
  117. .. warning::
  118. Since pybind11 is a metatemplate library, it is crucial that certain
  119. compiler flags are provided to ensure high quality code generation. In
  120. contrast to the ``pybind11_add_module()`` command, the CMake interface
  121. library only provides the *minimal* set of parameters to ensure that the
  122. code using pybind11 compiles, but it does **not** pass these extra compiler
  123. flags (i.e. this is up to you).
  124. These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL``
  125. and ``/LTCG`` on Visual Studio). Default-hidden symbols on GCC/Clang/ICPC
  126. (``-fvisibility=hidden``) and .OBJ files with many sections on Visual Studio
  127. (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
  128. explanation on why these are needed.
  129. Generating binding code automatically
  130. =====================================
  131. The ``Binder`` project is a tool for automatic generation of pybind11 binding
  132. code by introspecting existing C++ codebases using LLVM/Clang. See the
  133. [binder]_ documentation for details.
  134. .. [binder] http://cppbinder.readthedocs.io/en/latest/about.html