The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

217 lines
8.8 KiB

4 weeks 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 C++14 standard, if available
  70. on the target compiler, falling back to C++11 if C++14 support is not
  71. available. Note, however, that this default is subject to change: future
  72. pybind11 releases are expected to migrate to newer C++ standards as they become
  73. available. To override this, the standard flag can be given explicitly in
  74. ``PYBIND11_CPP_STANDARD``:
  75. .. code-block:: cmake
  76. # Use just one of these:
  77. # GCC/clang:
  78. set(PYBIND11_CPP_STANDARD -std=c++11)
  79. set(PYBIND11_CPP_STANDARD -std=c++14)
  80. set(PYBIND11_CPP_STANDARD -std=c++1z) # Experimental C++17 support
  81. # MSVC:
  82. set(PYBIND11_CPP_STANDARD /std:c++14)
  83. set(PYBIND11_CPP_STANDARD /std:c++latest) # Enables some MSVC C++17 features
  84. add_subdirectory(pybind11) # or find_package(pybind11)
  85. Note that this and all other configuration variables must be set **before** the
  86. call to ``add_subdirectory`` or ``find_package``. The variables can also be set
  87. when calling CMake from the command line using the ``-D<variable>=<value>`` flag.
  88. The target Python version can be selected by setting ``PYBIND11_PYTHON_VERSION``
  89. or an exact Python installation can be specified with ``PYTHON_EXECUTABLE``.
  90. For example:
  91. .. code-block:: bash
  92. cmake -DPYBIND11_PYTHON_VERSION=3.6 ..
  93. # or
  94. cmake -DPYTHON_EXECUTABLE=path/to/python ..
  95. find_package vs. add_subdirectory
  96. ---------------------------------
  97. For CMake-based projects that don't include the pybind11 repository internally,
  98. an external installation can be detected through ``find_package(pybind11)``.
  99. See the `Config file`_ docstring for details of relevant CMake variables.
  100. .. code-block:: cmake
  101. cmake_minimum_required(VERSION 2.8.12)
  102. project(example)
  103. find_package(pybind11 REQUIRED)
  104. pybind11_add_module(example example.cpp)
  105. Once detected, the aforementioned ``pybind11_add_module`` can be employed as
  106. before. The function usage and configuration variables are identical no matter
  107. if pybind11 is added as a subdirectory or found as an installed package. You
  108. can refer to the same [cmake_example]_ repository for a full sample project
  109. -- just swap out ``add_subdirectory`` for ``find_package``.
  110. .. _Config file: https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in
  111. Advanced: interface library target
  112. ----------------------------------
  113. When using a version of CMake greater than 3.0, pybind11 can additionally
  114. be used as a special *interface library* . The target ``pybind11::module``
  115. is available with pybind11 headers, Python headers and libraries as needed,
  116. and C++ compile definitions attached. This target is suitable for linking
  117. to an independently constructed (through ``add_library``, not
  118. ``pybind11_add_module``) target in the consuming project.
  119. .. code-block:: cmake
  120. cmake_minimum_required(VERSION 3.0)
  121. project(example)
  122. find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
  123. add_library(example MODULE main.cpp)
  124. target_link_libraries(example PRIVATE pybind11::module)
  125. set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
  126. SUFFIX "${PYTHON_MODULE_EXTENSION}")
  127. .. warning::
  128. Since pybind11 is a metatemplate library, it is crucial that certain
  129. compiler flags are provided to ensure high quality code generation. In
  130. contrast to the ``pybind11_add_module()`` command, the CMake interface
  131. library only provides the *minimal* set of parameters to ensure that the
  132. code using pybind11 compiles, but it does **not** pass these extra compiler
  133. flags (i.e. this is up to you).
  134. These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL``
  135. and ``/LTCG`` on Visual Studio). Default-hidden symbols on GCC/Clang/ICPC
  136. (``-fvisibility=hidden``) and .OBJ files with many sections on Visual Studio
  137. (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
  138. explanation on why these are needed.
  139. Embedding the Python interpreter
  140. --------------------------------
  141. In addition to extension modules, pybind11 also supports embedding Python into
  142. a C++ executable or library. In CMake, simply link with the ``pybind11::embed``
  143. target. It provides everything needed to get the interpreter running. The Python
  144. headers and libraries are attached to the target. Unlike ``pybind11::module``,
  145. there is no need to manually set any additional properties here. For more
  146. information about usage in C++, see :doc:`/advanced/embedding`.
  147. .. code-block:: cmake
  148. cmake_minimum_required(VERSION 3.0)
  149. project(example)
  150. find_package(pybind11 REQUIRED) # or add_subdirectory(pybind11)
  151. add_executable(example main.cpp)
  152. target_link_libraries(example PRIVATE pybind11::embed)
  153. Generating binding code automatically
  154. =====================================
  155. The ``Binder`` project is a tool for automatic generation of pybind11 binding
  156. code by introspecting existing C++ codebases using LLVM/Clang. See the
  157. [binder]_ documentation for details.
  158. .. [binder] http://cppbinder.readthedocs.io/en/latest/about.html