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