@@ -10,37 +10,30 @@ cmake_minimum_required(VERSION 3.7)
10
10
# VERSION 3.7...3.18, but some versions of MCVS have a patched CMake 3.11
11
11
# that do not work properly with this syntax, so using the following workaround:
12
12
if (${CMAKE_VERSION} VERSION_LESS 3.18 )
13
- cmake_policy (VERSION ${CMAKE_MAJOR_VERSION} .${CMAKE_MINOR_VERSION} )
13
+ cmake_policy (VERSION ${CMAKE_MAJOR_VERSION} .${CMAKE_MINOR_VERSION} )
14
14
else ()
15
- cmake_policy (VERSION 3.18 )
15
+ cmake_policy (VERSION 3.18 )
16
16
endif ()
17
17
18
18
# Extract project version from source
19
- file (
20
- STRINGS
21
- "${CMAKE_CURRENT_SOURCE_DIR} /include/pybind11/detail/common.h"
22
- pybind11_version_defines
23
- REGEX
24
- "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) " )
19
+ file (STRINGS "${CMAKE_CURRENT_SOURCE_DIR} /include/pybind11/detail/common.h"
20
+ pybind11_version_defines REGEX "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) " )
25
21
26
22
foreach (ver ${pybind11_version_defines} )
27
- if (ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]] )
28
- set (PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2} " )
29
- endif ()
23
+ if (ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$]] )
24
+ set (PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2} " )
25
+ endif ()
30
26
endforeach ()
31
27
32
28
if (PYBIND11_VERSION_PATCH MATCHES [[([a-zA-Z]+)]] )
33
- set (PYBIND11_VERSION_TYPE "${CMAKE_MATCH_1} " )
29
+ set (PYBIND11_VERSION_TYPE "${CMAKE_MATCH_1} " )
34
30
endif ()
35
31
string (REGEX MATCH "[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH} " )
36
32
37
33
project (
38
- pybind11
39
- LANGUAGES
40
- CXX
41
- VERSION
42
- "${PYBIND11_VERSION_MAJOR} .${PYBIND11_VERSION_MINOR} .${PYBIND11_VERSION_PATCH} "
43
- )
34
+ pybind11
35
+ LANGUAGES CXX
36
+ VERSION "${PYBIND11_VERSION_MAJOR} .${PYBIND11_VERSION_MINOR} .${PYBIND11_VERSION_PATCH} " )
44
37
45
38
# Standard includes
46
39
include (GNUInstallDirs )
@@ -51,28 +44,27 @@ message(STATUS "pybind11 v${pybind11_VERSION} ${PYBIND11_VERSION_TYPE}")
51
44
52
45
# Check if pybind11 is being used directly or via add_subdirectory
53
46
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR )
54
- set (PYBIND11_MASTER_PROJECT ON )
55
- message (STATUS "CMake ${CMAKE_VERSION} " )
56
- message (STATUS "CXX Compiler: ${CMAKE_CXX_COMPILER} " )
57
- message (STATUS "CXX Compiler version: ${CMAKE_CXX_COMPILER_VERSION} " )
58
-
59
- if (CMAKE_CXX_STANDARD )
60
- set (CMAKE_CXX_EXTENSIONS OFF )
61
- set (CMAKE_CXX_STANDARD_REQUIRED ON )
62
- endif ()
47
+ set (PYBIND11_MASTER_PROJECT ON )
48
+ message (STATUS "CMake ${CMAKE_VERSION} " )
49
+ message (STATUS "CXX Compiler: ${CMAKE_CXX_COMPILER} " )
50
+ message (STATUS "CXX Compiler version: ${CMAKE_CXX_COMPILER_VERSION} " )
51
+
52
+ if (CMAKE_CXX_STANDARD )
53
+ set (CMAKE_CXX_EXTENSIONS OFF )
54
+ set (CMAKE_CXX_STANDARD_REQUIRED ON )
55
+ endif ()
63
56
else ()
64
- set (PYBIND11_MASTER_PROJECT OFF )
65
- set (pybind11_system SYSTEM )
57
+ set (PYBIND11_MASTER_PROJECT OFF )
58
+ set (pybind11_system SYSTEM )
66
59
endif ()
67
60
68
61
option (PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT} )
69
62
option (PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT} )
70
63
option (PYBIND11_CLASSIC_LTO "Use the classic LTO flag algorithm, even on CMake 3.9+" OFF )
71
64
cmake_dependent_option (
72
- USE_PYTHON_INCLUDE_DIR
73
- "Install pybind11 headers in Python include directory instead of default installation prefix" OFF
74
- "PYBIND11_INSTALL" OFF
75
- )
65
+ USE_PYTHON_INCLUDE_DIR
66
+ "Install pybind11 headers in Python include directory instead of default installation prefix"
67
+ OFF "PYBIND11_INSTALL" OFF )
76
68
77
69
# NB: when adding a header don't forget to also add it to setup.py
78
70
set (PYBIND11_HEADERS
@@ -98,40 +90,52 @@ set(PYBIND11_HEADERS
98
90
include /pybind11/pybind11.h
99
91
include /pybind11/pytypes.h
100
92
include /pybind11/stl.h
101
- include /pybind11/stl_bind.h
102
- )
93
+ include /pybind11/stl_bind.h )
103
94
104
95
# TODO: compare with grep and warn if missmatched
105
96
106
97
# cmake 3.12 added list(TRANSFORM <list> PREPEND
107
98
# But we can't use it yet
108
- string (REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR} /include/"
109
- PYBIND11_HEADERS "${PYBIND11_HEADERS} " )
110
-
99
+ string (REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR} /include/" PYBIND11_HEADERS
100
+ "${PYBIND11_HEADERS} " )
111
101
112
102
# Classic mode
113
103
114
104
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR} /tools" )
115
105
include (pybind11Tools )
116
106
117
107
# Cache variables so pybind11_add_module can be used in parent projects
118
- set (PYBIND11_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR} /include" CACHE INTERNAL "" )
119
- set (PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS} CACHE INTERNAL "" )
120
- set (PYTHON_LIBRARIES ${PYTHON_LIBRARIES} CACHE INTERNAL "" )
121
- set (PYTHON_MODULE_PREFIX ${PYTHON_MODULE_PREFIX} CACHE INTERNAL "" )
122
- set (PYTHON_MODULE_EXTENSION ${PYTHON_MODULE_EXTENSION} CACHE INTERNAL "" )
123
- set (PYTHON_VERSION_MAJOR ${PYTHON_VERSION_MAJOR} CACHE INTERNAL "" )
124
- set (PYTHON_VERSION_MINOR ${PYTHON_VERSION_MINOR} CACHE INTERNAL "" )
125
- set (PYTHON_IS_DEBUG "${PYTHON_IS_DEBUG} " CACHE INTERNAL "" )
126
-
108
+ set (PYBIND11_INCLUDE_DIR
109
+ "${CMAKE_CURRENT_LIST_DIR} /include"
110
+ CACHE INTERNAL "" )
111
+ set (PYTHON_INCLUDE_DIRS
112
+ ${PYTHON_INCLUDE_DIRS}
113
+ CACHE INTERNAL "" )
114
+ set (PYTHON_LIBRARIES
115
+ ${PYTHON_LIBRARIES}
116
+ CACHE INTERNAL "" )
117
+ set (PYTHON_MODULE_PREFIX
118
+ ${PYTHON_MODULE_PREFIX}
119
+ CACHE INTERNAL "" )
120
+ set (PYTHON_MODULE_EXTENSION
121
+ ${PYTHON_MODULE_EXTENSION}
122
+ CACHE INTERNAL "" )
123
+ set (PYTHON_VERSION_MAJOR
124
+ ${PYTHON_VERSION_MAJOR}
125
+ CACHE INTERNAL "" )
126
+ set (PYTHON_VERSION_MINOR
127
+ ${PYTHON_VERSION_MINOR}
128
+ CACHE INTERNAL "" )
129
+ set (PYTHON_IS_DEBUG
130
+ "${PYTHON_IS_DEBUG} "
131
+ CACHE INTERNAL "" )
127
132
128
133
if (PYBIND11_TEST OR (BUILD_TESTING AND PYBIND11_MASTER_PROJECT ))
129
- add_subdirectory (tests )
134
+ add_subdirectory (tests )
130
135
endif ()
131
136
132
-
133
137
if (USE_PYTHON_INCLUDE_DIR )
134
- file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS} )
138
+ file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS} )
135
139
endif ()
136
140
137
141
# Note: when creating targets, you cannot use if statements at configure time -
@@ -140,26 +144,22 @@ endif()
140
144
141
145
# Build an interface library target:
142
146
add_library (pybind11 INTERFACE )
143
- add_library (pybind11::pybind11 ALIAS pybind11 ) # to match exported target
147
+ add_library (pybind11::pybind11 ALIAS pybind11 ) # to match exported target
144
148
145
- target_include_directories (pybind11 ${pybind11_system} INTERFACE $< BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR} >
146
- $< INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} > )
149
+ target_include_directories (
150
+ pybind11 ${pybind11_system} INTERFACE $< BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR} >
151
+ $< INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} > )
147
152
# Only add Python for build - must be added during the import for config since it has to be re-discovered.
148
153
target_include_directories (pybind11 SYSTEM INTERFACE $< BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS} > )
149
154
150
155
if (CMAKE_VERSION VERSION_LESS 3.13 )
151
- target_compile_features (
152
- pybind11
153
- INTERFACE
154
- cxx_inheriting_constructors
155
- cxx_user_literals
156
- cxx_right_angle_brackets
157
- )
156
+ target_compile_features (pybind11 INTERFACE cxx_inheriting_constructors cxx_user_literals
157
+ cxx_right_angle_brackets )
158
158
else ()
159
- # This was added in CMake 3.8, but we are keeping a consistent breaking
160
- # point for the config file at 3.13. A config generated by CMake 3.13+
161
- # can only be read in 3.13+ due to the SHELL usage later, so this is safe to do.
162
- target_compile_features (pybind11 INTERFACE cxx_std_11 )
159
+ # This was added in CMake 3.8, but we are keeping a consistent breaking
160
+ # point for the config file at 3.13. A config generated by CMake 3.13+
161
+ # can only be read in 3.13+ due to the SHELL usage later, so this is safe to do.
162
+ target_compile_features (pybind11 INTERFACE cxx_std_11 )
163
163
endif ()
164
164
165
165
add_library (module INTERFACE )
@@ -169,23 +169,26 @@ target_link_libraries(module INTERFACE pybind11::pybind11)
169
169
170
170
# See https://github.com/Kitware/CMake/blob/master/Modules/CMakePlatformId.h.in for platform IDs
171
171
# Note: CMake 3.15 allows $<PLATFORM_ID:Windows,Cygwin>
172
- target_link_libraries (module INTERFACE "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES} >>" )
172
+ target_link_libraries (
173
+ module
174
+ INTERFACE
175
+ "$<$<OR:$<PLATFORM_ID:Windows>,$<PLATFORM_ID:Cygwin>>:$<BUILD_INTERFACE:${PYTHON_LIBRARIES} >>" )
173
176
174
177
if (CMAKE_VERSION VERSION_LESS 3.13 )
175
- target_link_libraries (module INTERFACE "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>" )
178
+ target_link_libraries (module INTERFACE "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>" )
176
179
else ()
177
- # SHELL (3.12+) forces this to remain together, and link_options was added in 3.13+
178
- # This is safer, because you are ensured the deduplication pass in CMake will not consider
179
- # these separate and remove one but not the other.
180
- target_link_options (module INTERFACE "$<$<PLATFORM_ID:Darwin>:SHELL:-undefined dynamic_lookup>" )
180
+ # SHELL (3.12+) forces this to remain together, and link_options was added in 3.13+
181
+ # This is safer, because you are ensured the deduplication pass in CMake will not consider
182
+ # these separate and remove one but not the other.
183
+ target_link_options (module INTERFACE "$<$<PLATFORM_ID:Darwin>:SHELL:-undefined dynamic_lookup>" )
181
184
endif ()
182
185
183
-
184
186
# Workaround for Python 2.7 and C++17 (C++14 as a warning) incompatibility
185
187
# This adds the flags -Wno-register and -Wno-deprecated-register if the compiler
186
188
# is Clang 3.9+ or AppleClang and the compile language is CXX, or /wd5033 for MSVC (all languages,
187
189
# since MSVC didn't recognize COMPILE_LANGUAGE until CMake 3.11+).
188
- set (clang_4plus "$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,3.9>>>" )
190
+ set (clang_4plus
191
+ "$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,3.9>>>" )
189
192
set (no_register "$<OR:${clang_4plus} ,$<CXX_COMPILER_ID:AppleClang>>" )
190
193
set (cxx_no_register "$<AND:$<COMPILE_LANGUAGE:CXX>,${no_register} >" )
191
194
set (msvc "$<CXX_COMPILER_ID:MSVC>" )
@@ -198,58 +201,50 @@ add_library(pybind11::embed ALIAS embed)
198
201
target_link_libraries (embed INTERFACE pybind11::pybind11 $< BUILD_INTERFACE:${PYTHON_LIBRARIES} > )
199
202
200
203
if (PYBIND11_INSTALL )
201
- install (DIRECTORY ${PYBIND11_INCLUDE_DIR} /pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
202
- # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
203
- set (PYBIND11_CMAKECONFIG_INSTALL_DIR "share/cmake/${PROJECT_NAME} " CACHE STRING "install path for pybind11Config.cmake" )
204
-
205
- configure_package_config_file (tools/${PROJECT_NAME}Config.cmake.in
206
- "${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake"
207
- INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
208
-
209
- if (CMAKE_VERSION VERSION_LESS 3.14 )
210
- # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
211
- # not depend on architecture specific settings or libraries.
212
- set (_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P} )
213
- unset (CMAKE_SIZEOF_VOID_P )
214
-
215
- write_basic_package_version_file (
216
- ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}ConfigVersion.cmake
217
- VERSION
218
- ${PROJECT_VERSION}
219
- COMPATIBILITY
220
- AnyNewerVersion
221
- )
222
-
223
- set (CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P} )
224
- else ()
225
- # CMake 3.14+ natively supports header-only libraries
226
- write_basic_package_version_file (
227
- ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}ConfigVersion.cmake
228
- VERSION
229
- ${PROJECT_VERSION}
230
- COMPATIBILITY
231
- AnyNewerVersion
232
- ARCH_INDEPENDENT
233
- )
234
- endif ()
235
-
236
- install (
237
- FILES
238
- ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}Config.cmake
239
- ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}ConfigVersion.cmake
240
- tools/FindPythonLibsNew.cmake
241
- tools/pybind11Tools.cmake
242
- DESTINATION
243
- ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
244
-
245
- if (NOT PYBIND11_EXPORT_NAME )
246
- set (PYBIND11_EXPORT_NAME "${PROJECT_NAME} Targets" )
247
- endif ()
248
-
249
- install (TARGETS pybind11 module embed
250
- EXPORT "${PYBIND11_EXPORT_NAME} " )
251
-
252
- install (EXPORT "${PYBIND11_EXPORT_NAME} "
253
- NAMESPACE "pybind11::"
254
- DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
204
+ install (DIRECTORY ${PYBIND11_INCLUDE_DIR} /pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
205
+ # GNUInstallDirs "DATADIR" wrong here; CMake search path wants "share".
206
+ set (PYBIND11_CMAKECONFIG_INSTALL_DIR
207
+ "share/cmake/${PROJECT_NAME} "
208
+ CACHE STRING "install path for pybind11Config.cmake" )
209
+
210
+ configure_package_config_file (
211
+ tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake"
212
+ INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
213
+
214
+ if (CMAKE_VERSION VERSION_LESS 3.14 )
215
+ # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does
216
+ # not depend on architecture specific settings or libraries.
217
+ set (_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P} )
218
+ unset (CMAKE_SIZEOF_VOID_P )
219
+
220
+ write_basic_package_version_file (
221
+ ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}ConfigVersion.cmake
222
+ VERSION ${PROJECT_VERSION}
223
+ COMPATIBILITY AnyNewerVersion )
224
+
225
+ set (CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P} )
226
+ else ()
227
+ # CMake 3.14+ natively supports header-only libraries
228
+ write_basic_package_version_file (
229
+ ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}ConfigVersion.cmake
230
+ VERSION ${PROJECT_VERSION}
231
+ COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT )
232
+ endif ()
233
+
234
+ install (
235
+ FILES ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}Config.cmake
236
+ ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME}ConfigVersion.cmake
237
+ tools/FindPythonLibsNew.cmake tools/pybind11Tools.cmake
238
+ DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
239
+
240
+ if (NOT PYBIND11_EXPORT_NAME )
241
+ set (PYBIND11_EXPORT_NAME "${PROJECT_NAME} Targets" )
242
+ endif ()
243
+
244
+ install (TARGETS pybind11 module embed EXPORT "${PYBIND11_EXPORT_NAME} " )
245
+
246
+ install (
247
+ EXPORT "${PYBIND11_EXPORT_NAME} "
248
+ NAMESPACE "pybind11::"
249
+ DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR} )
255
250
endif ()
0 commit comments