22#
33# Copyright (c) 2015 Wenzel Jakob <[email protected] > 44#
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.
77
88cmake_minimum_required (VERSION 3.7)
99
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:
1212if (${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} )
1414else ()
15- cmake_policy (VERSION 3.18)
15+ cmake_policy (VERSION 3.18)
1616endif ()
1717
1818# 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) " )
2521
2622foreach (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 ()
3026endforeach ()
3127
3228project (
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} " )
3932
4033# Standard includes
4134include (GNUInstallDirs)
@@ -44,27 +37,26 @@ include(CMakeDependentOption)
4437
4538# Check if pybind11 is being used directly or via add_subdirectory
4639if (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 ()
5548else ()
56- set (PYBIND11_MASTER_PROJECT OFF )
57- set (pybind11_system SYSTEM )
49+ set (PYBIND11_MASTER_PROJECT OFF )
50+ set (pybind11_system SYSTEM )
5851endif ()
5952
6053option (PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT} )
6154option (PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT} )
6255option (PYBIND11_CLASSIC_LTO "Use the classic LTO flag algorithm, even on CMake 3.9+" OFF )
6356cmake_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 )
6860
6961# NB: when adding a header don't forget to also add it to setup.py
7062set (PYBIND11_HEADERS
@@ -90,71 +82,82 @@ set(PYBIND11_HEADERS
9082 include /pybind11/pybind11.h
9183 include /pybind11/pytypes.h
9284 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} " )
10089
10190# Classic mode
10291
10392list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR} /tools" )
10493include (pybind11Tools)
10594
10695# 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 "" )
116120
117121if (PYBIND11_TEST OR (BUILD_TESTING AND PYBIND11_MASTER_PROJECT))
118- add_subdirectory (tests)
122+ add_subdirectory (tests)
119123endif ()
120124
121-
122125if (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} )
124127endif ()
125128
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.
128131
129132# Build an interface library target:
130133add_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
132135
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.
136141target_include_directories (pybind11 SYSTEM INTERFACE $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS} >)
137142
138143if (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)
153156else ()
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)
158161endif ()
159162
160163add_library (module INTERFACE )
@@ -164,78 +167,74 @@ target_link_libraries(module INTERFACE pybind11::pybind11)
164167
165168# See https://github.com/Kitware/CMake/blob/master/Modules/CMakePlatformId.h.in for platform IDs
166169# 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} >>" )
168174
169175if (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>" )
171177else ()
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>" )
174180endif ()
175181
176-
177182message (STATUS "CXX Compiler version: ${CMAKE_CXX_COMPILER_VERSION} " )
178183
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>>>" )
180186set (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>" )
182189
183190add_library (embed INTERFACE )
184191add_library (pybind11::embed ALIAS embed)
185192target_link_libraries (embed INTERFACE pybind11::pybind11 $<BUILD_INTERFACE:${PYTHON_LIBRARIES} >)
186193
187194if (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} )
241240endif ()
0 commit comments