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