blob: 3f11172963fadd973a7124778f7ff501fa580a9a [file] [log] [blame]
Henry Schreinerdf115972020-07-31 22:45:19 -04001#[=============================================================================[.rst
2
3pybind11Config.cmake
4--------------------
5
6PYBIND11 cmake module.
7This module sets the following variables in your project::
8
9 pybind11_FOUND - true if pybind11 and all required components found on the system
10 pybind11_VERSION - pybind11 version in format Major.Minor.Release
Henry Schreiner1729aae2020-08-19 12:26:26 -040011 pybind11_VERSION_TYPE - pybind11 version type (dev, release)
Henry Schreinerdf115972020-07-31 22:45:19 -040012 pybind11_INCLUDE_DIRS - Directories where pybind11 and python headers are located.
13 pybind11_INCLUDE_DIR - Directory where pybind11 headers are located.
14 pybind11_DEFINITIONS - Definitions necessary to use pybind11, namely USING_pybind11.
15 pybind11_LIBRARIES - compile flags and python libraries (as needed) to link against.
16 pybind11_LIBRARY - empty.
17
18
19Available components: None
20
21
22Exported targets::
23
24If pybind11 is found, this module defines the following :prop_tgt:`IMPORTED`
25interface library targets::
26
27 pybind11::module - for extension modules
28 pybind11::embed - for embedding the Python interpreter
29
30Python headers, libraries (as needed by platform), and the C++ standard
31are attached to the target.
Henry Schreiner1729aae2020-08-19 12:26:26 -040032
33Advanced targets are also supplied - these are primary for users building
34complex applications, and they are available in all modes::
35
36 pybind11::headers - Just the pybind11 headers and minimum compile requirements
37 pybind11::pybind11 - Python headers too
38 pybind11::python_link_helper - Just the "linking" part of pybind11:module, for CMake < 3.15
39 pybind11::python2_no_register - Quiets the warning/error when mixing C++14+ and Python 2, also included in pybind11::module
40 pybind11::thin_lto - An alternative to INTERPROCEDURAL_OPTIMIZATION
41 pybind11::lto - An alternative to INTERPROCEDURAL_OPTIMIZATION (also avoids thin LTO on clang)
42 pybind11::windows_extras - Adds bigobj and mp for MSVC
43
44Modes::
45
46There are two modes provided; classic, which is built on the old Python
47discovery packages in CMake, or the new FindPython mode, which uses FindPython
48from 3.12+ forward (3.15+ _highly_ recommended).
49
50New FindPython mode::
51
52To activate this mode, either call ``find_package(Python COMPONENTS Interpreter Development)``
53before finding this package, or set the ``PYBIND11_FINDPYTHON`` variable to ON. In this mode,
54you can either use the basic targets, or use the FindPython tools::
55
56 find_package(Python COMPONENTS Interpreter Development)
57 find_package(pybind11 CONFIG)
58
59 # pybind11 method:
60 pybind11_add_module(MyModule1 src1.cpp)
61
62 # Python method:
63 Python_add_library(MyModule2 src2.cpp)
64 target_link_libraries(MyModule2 pybind11::headers)
65 set_target_properties(MyModule2 PROPERTIES
66 INTERPROCEDURAL_OPTIMIZATION ON
67 CXX__VISIBILITY_PRESET ON
68 VISIBLITY_INLINES_HIDDEN ON)
69
70If you build targets yourself, you may be interested in stripping the output
71for reduced size; this is the one other feature that the helper function gives you.
72
Henry Schreinerdf115972020-07-31 22:45:19 -040073Classic mode::
74
75Set PythonLibsNew variables to influence python detection and
76CMAKE_CXX_STANDARD to influence standard setting. ::
77
78 find_package(pybind11 CONFIG REQUIRED)
Henry Schreinerdf115972020-07-31 22:45:19 -040079
80 # Create an extension module
81 add_library(mylib MODULE main.cpp)
Henry Schreiner1729aae2020-08-19 12:26:26 -040082 target_link_libraries(mylib PUBLIC pybind11::module)
Henry Schreinerdf115972020-07-31 22:45:19 -040083
84 # Or embed the Python interpreter into an executable
85 add_executable(myexe main.cpp)
Henry Schreiner1729aae2020-08-19 12:26:26 -040086 target_link_libraries(myexe PUBLIC pybind11::embed)
Henry Schreinerdf115972020-07-31 22:45:19 -040087
88Suggested usage::
89
90find_package with version info is not recommended except for release versions. ::
91
92 find_package(pybind11 CONFIG)
93 find_package(pybind11 2.0 EXACT CONFIG REQUIRED)
94
95
96The following variables can be set to guide the search for this package::
97
98 pybind11_DIR - CMake variable, set to directory containing this Config file
99 CMAKE_PREFIX_PATH - CMake variable, set to root directory of this package
100 PATH - environment variable, set to bin directory of this package
101 CMAKE_DISABLE_FIND_PACKAGE_pybind11 - CMake variable, disables
102 find_package(pybind11) when not REQUIRED, perhaps to force internal build
Lori A. Burns5cafc992016-12-13 10:55:38 -0500103
Henry Schreiner1729aae2020-08-19 12:26:26 -0400104Helper functions::
105
106 pybind11_add_module(...) - Add a library and setup all helpers
107 pybind11_strip(target) - Strip a target after building it (linux/macOS)
108 pybind11_extension(target) - Injects the Python extension name
109
110See ``pybind11Tools.cmake`` or ``pybind11NewTools.cmake`` for details on
111``pybind11_add_module``.
112
113#]=============================================================================]
Lori A. Burns5cafc992016-12-13 10:55:38 -0500114@PACKAGE_INIT@
115
Henry Schreiner1b92cd12020-07-29 15:02:53 -0400116# Location of pybind11/pybind11.h
117set(pybind11_INCLUDE_DIR "${PACKAGE_PREFIX_DIR}/@CMAKE_INSTALL_INCLUDEDIR@")
Lori A. Burns5cafc992016-12-13 10:55:38 -0500118
Henry Schreiner1b92cd12020-07-29 15:02:53 -0400119set(pybind11_LIBRARY "")
120set(pybind11_DEFINITIONS USING_pybind11)
Henry Schreinerdf115972020-07-31 22:45:19 -0400121set(pybind11_VERSION_TYPE "@pybind11_VERSION_TYPE@")
Lori A. Burns5cafc992016-12-13 10:55:38 -0500122
Henry Schreiner1b92cd12020-07-29 15:02:53 -0400123check_required_components(pybind11)
Lori A. Burns5cafc992016-12-13 10:55:38 -0500124
Henry Schreiner1729aae2020-08-19 12:26:26 -0400125if(TARGET pybind11::python_link_helper)
126 # This has already been setup elsewhere, such as with a previous call or
127 # add_subdirectory
128 return()
129endif()
Lori A. Burns5cafc992016-12-13 10:55:38 -0500130
Henry Schreiner1729aae2020-08-19 12:26:26 -0400131include("${CMAKE_CURRENT_LIST_DIR}/pybind11Targets.cmake")
Lori A. Burns5cafc992016-12-13 10:55:38 -0500132
Henry Schreinerc58f7b72020-08-22 09:06:01 -0400133# Easier to use / remember
134add_library(pybind11::headers IMPORTED INTERFACE)
135set_target_properties(pybind11::headers PROPERTIES INTERFACE_LINK_LIBRARIES
136 pybind11::pybind11_headers)
137
Henry Schreiner1729aae2020-08-19 12:26:26 -0400138include("${CMAKE_CURRENT_LIST_DIR}/pybind11Common.cmake")
Lori A. Burns5cafc992016-12-13 10:55:38 -0500139
Henry Schreiner1729aae2020-08-19 12:26:26 -0400140if(NOT pybind11_FIND_QUIETLY)
141 message(
142 STATUS
143 "Found pybind11: ${pybind11_INCLUDE_DIR} (found version \"${pybind11_VERSION}\" ${pybind11_VERSION_TYPE})"
Henry Schreinered6de122020-08-01 23:47:47 -0400144 )
Lori A. Burns5cafc992016-12-13 10:55:38 -0500145endif()