compiling.rst revision 11986:c12e4625ab56
1955SN/ABuild systems
2955SN/A#############
31762SN/A
4955SN/ABuilding with setuptools
5955SN/A========================
6955SN/A
7955SN/AFor projects on PyPI, building with setuptools is the way to go. Sylvain Corlay
8955SN/Ahas kindly provided an example project which shows how to set up everything,
9955SN/Aincluding automatic generation of documentation using Sphinx. Please refer to
10955SN/Athe [python_example]_ repository.
11955SN/A
12955SN/A.. [python_example] https://github.com/pybind/python_example
13955SN/A
14955SN/ABuilding with cppimport
15955SN/A========================
16955SN/A
17955SN/A cppimport is a small Python import hook that determines whether there is a C++
18955SN/A source file whose name matches the requested module. If there is, the file is
19955SN/A compiled as a Python extension using pybind11 and placed in the same folder as
20955SN/A the C++ source file. Python is then able to find the module and load it.
21955SN/A
22955SN/A.. [cppimport] https://github.com/tbenthompson/cppimport
23955SN/A
24955SN/A.. _cmake:
25955SN/A
26955SN/ABuilding with CMake
27955SN/A===================
282665Ssaidi@eecs.umich.edu
294762Snate@binkert.orgFor C++ codebases that have an existing CMake-based build system, a Python
30955SN/Aextension module can be created with just a few lines of code:
315522Snate@binkert.org
326143Snate@binkert.org.. code-block:: cmake
334762Snate@binkert.org
345522Snate@binkert.org    cmake_minimum_required(VERSION 2.8.12)
35955SN/A    project(example)
365522Snate@binkert.org
37955SN/A    add_subdirectory(pybind11)
385522Snate@binkert.org    pybind11_add_module(example example.cpp)
394202Sbinkertn@umich.edu
405742Snate@binkert.orgThis assumes that the pybind11 repository is located in a subdirectory named
41955SN/A:file:`pybind11` and that the code is located in a file named :file:`example.cpp`.
424381Sbinkertn@umich.eduThe CMake command ``add_subdirectory`` will import a function with the signature
434381Sbinkertn@umich.edu``pybind11_add_module(<name> source1 [source2 ...])``. It will take care of all
448334Snate@binkert.orgthe details needed to build a Python extension module on any platform.
45955SN/A
46955SN/AThe target Python version can be selected by setting the ``PYBIND11_PYTHON_VERSION``
474202Sbinkertn@umich.eduvariable before adding the pybind11 subdirectory. Alternatively, an exact Python
48955SN/Ainstallation can be specified by setting ``PYTHON_EXECUTABLE``.
494382Sbinkertn@umich.edu
504382Sbinkertn@umich.eduA working sample project, including a way to invoke CMake from :file:`setup.py` for
514382Sbinkertn@umich.eduPyPI integration, can be found in the [cmake_example]_  repository.
526654Snate@binkert.org
535517Snate@binkert.org.. [cmake_example] https://github.com/pybind/cmake_example
548614Sgblack@eecs.umich.edu
557674Snate@binkert.orgFor CMake-based projects that don't include the pybind11
566143Snate@binkert.orgrepository internally, an external installation can be detected
576143Snate@binkert.orgthrough `find_package(pybind11 ... CONFIG ...)`. See the `Config file
586143Snate@binkert.org<https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in>`_
598233Snate@binkert.orgdocstring for details of relevant CMake variables.
608233Snate@binkert.org
618233Snate@binkert.orgOnce detected, and after setting any variables to guide Python and
628233Snate@binkert.orgC++ standard detection, the aforementioned ``pybind11_add_module``
638233Snate@binkert.orgwrapper to ``add_library`` can be employed as described above (after
648334Snate@binkert.org``include(pybind11Tools)``). This procedure is available when using CMake
658334Snate@binkert.org>= 2.8.12. A working example can be found at [test_installed_module]_ .
668233Snate@binkert.org
678233Snate@binkert.org.. code-block:: cmake
688233Snate@binkert.org
698233Snate@binkert.org    cmake_minimum_required(VERSION 2.8.12)
708233Snate@binkert.org    project(example)
718233Snate@binkert.org
726143Snate@binkert.org    find_package(pybind11 REQUIRED)
738233Snate@binkert.org    pybind11_add_module(example example.cpp)
748233Snate@binkert.org
758233Snate@binkert.org.. [test_installed_module] https://github.com/pybind/pybind11/blob/master/tests/test_installed_module/CMakeLists.txt
766143Snate@binkert.org
776143Snate@binkert.orgWhen using a version of CMake greater than 3.0, pybind11 can
786143Snate@binkert.orgadditionally be used as a special *interface library* following the
796143Snate@binkert.orgcall to ``find_package``. CMake variables to guide Python and C++
808233Snate@binkert.orgstandard detection should be set *before* ``find_package``. When
818233Snate@binkert.org``find_package`` returns, the target ``pybind11::pybind11`` is
828233Snate@binkert.orgavailable with pybind11 headers, Python headers and libraries as
836143Snate@binkert.orgneeded, and C++ compile definitions attached. This target is suitable
848233Snate@binkert.orgfor linking to an independently constructed (through ``add_library``,
858233Snate@binkert.orgnot ``pybind11_add_module``) target in the consuming project. A working
868233Snate@binkert.orgexample can be found at [test_installed_target]_ .
878233Snate@binkert.org
886143Snate@binkert.org.. code-block:: cmake
896143Snate@binkert.org
906143Snate@binkert.org    cmake_minimum_required(VERSION 3.0)
914762Snate@binkert.org    project(example)
926143Snate@binkert.org
938233Snate@binkert.org    add_library(example MODULE main.cpp)
948233Snate@binkert.org
958233Snate@binkert.org    find_package(pybind11 REQUIRED)
968233Snate@binkert.org    target_link_libraries(example PRIVATE pybind11::pybind11)
978233Snate@binkert.org    set_target_properties(example PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX}"
986143Snate@binkert.org                                             SUFFIX "${PYTHON_MODULE_EXTENSION}")
998233Snate@binkert.org
1008233Snate@binkert.org.. warning::
1018233Snate@binkert.org
1028233Snate@binkert.org    Since pybind11 is a metatemplate library, it is crucial that certain
1036143Snate@binkert.org    compiler flags are provided to ensure high quality code generation. In
1046143Snate@binkert.org    contrast to the ``pybind11_add_module()`` command, the CMake interface
1056143Snate@binkert.org    library only provides the *minimal* set of parameters to ensure that the
1066143Snate@binkert.org    code using pybind11 compiles, but it does **not** pass these extra compiler
1076143Snate@binkert.org    flags (i.e. this is up to you).
1086143Snate@binkert.org
1096143Snate@binkert.org    These include Link Time Optimization (``-flto`` on GCC/Clang/ICPC, ``/GL``
1106143Snate@binkert.org    and ``/LTCG`` on Visual Studio). Default-hidden symbols on GCC/Clang/ICPC
1116143Snate@binkert.org    (``-fvisibility=hidden``) and .OBJ files with many sections on Visual Studio
1127065Snate@binkert.org    (``/bigobj``). The :ref:`FAQ <faq:symhidden>` contains an
1136143Snate@binkert.org    explanation on why these are needed.
1148233Snate@binkert.org
1158233Snate@binkert.org.. [test_installed_target] https://github.com/pybind/pybind11/blob/master/tests/test_installed_target/CMakeLists.txt
1168233Snate@binkert.org
1178233Snate@binkert.org