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
10+ # VERSION 3.7...3.18, but some versions of MCVS have a patched CMake 3.11
1111# that do not work properly with this syntax, so using the following workaround:
1212if (${CMAKE_VERSION} VERSION_LESS 3.18)
1313 cmake_policy (VERSION ${CMAKE_MAJOR_VERSION} .${CMAKE_MINOR_VERSION} )
@@ -21,14 +21,19 @@ file(
2121 "${CMAKE_CURRENT_SOURCE_DIR} /include/pybind11/detail/common.h"
2222 pybind11_version_defines
2323 REGEX
24- "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH|TYPE ) " )
24+ "#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) " )
2525
2626foreach (ver ${pybind11_version_defines} )
27- if (ver MATCHES [= [#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH|TYPE ) +"? ([^ ^" ]+)"?$]= ])
27+ if (ver MATCHES [[#define PYBIND11_VERSION_(MAJOR|MINOR|PATCH) +([^ ]+)$] ])
2828 set (PYBIND11_VERSION_${CMAKE_MATCH_1} "${CMAKE_MATCH_2} " )
2929 endif ()
3030endforeach ()
3131
32+ if (PYBIND11_VERSION_PATCH MATCHES [[([a-zA-Z]+)]])
33+ set (PYBIND11_VERSION_TYPE "${CMAKE_MATCH_1} " )
34+ endif ()
35+ string (REGEX MATCH "[0-9]+" PYBIND11_VERSION_PATCH "${PYBIND11_VERSION_PATCH} " )
36+
3237project (
3338 pybind11
3439 LANGUAGES
@@ -42,10 +47,11 @@ include(GNUInstallDirs)
4247include (CMakePackageConfigHelpers)
4348include (CMakeDependentOption)
4449
50+ message (STATUS "pybind11 v${pybind11_VERSION} ${PYBIND11_VERSION_TYPE} " )
51+
4552# Check if pybind11 is being used directly or via add_subdirectory
4653if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR )
4754 set (PYBIND11_MASTER_PROJECT ON )
48- message (STATUS "pybind11 v${pybind11_VERSION} ${PYBIND11_VERSION_TYPE} " )
4955 message (STATUS "CMake ${CMAKE_VERSION} " )
5056
5157 if (CMAKE_CXX_STANDARD)
@@ -92,6 +98,9 @@ set(PYBIND11_HEADERS
9298 include /pybind11/stl.h
9399 include /pybind11/stl_bind.h
94100)
101+
102+ # TODO: compare with grep and warn if missmatched
103+
95104# cmake 3.12 added list(TRANSFORM <list> PREPEND
96105# But we can't use it yet
97106string (REPLACE "include/" "${CMAKE_CURRENT_SOURCE_DIR} /include/"
@@ -125,6 +134,7 @@ endif()
125134
126135# Note: when creating targets, you cannot use if statements at configure time -
127136# you need generator expressions, because those will be placed in the target file.
137+ # You can also place ifs *in* the Config.in, but not here.
128138
129139# Build an interface library target:
130140add_library (pybind11 INTERFACE )
@@ -139,21 +149,14 @@ if(CMAKE_VERSION VERSION_LESS 3.13)
139149 target_compile_features (
140150 pybind11
141151 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
152+ cxx_inheriting_constructors
150153 cxx_user_literals
151- cxx_variadic_templates
154+ cxx_right_angle_brackets
152155 )
153156else ()
154157 # This was added in CMake 3.8, but we are keeping a consistent breaking
155158 # 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.
159+ # can only be read in 3.13+ due to the SHELL usage later, so this is safe to do.
157160 target_compile_features (pybind11 INTERFACE cxx_std_11)
158161endif ()
159162
@@ -170,13 +173,16 @@ if(CMAKE_VERSION VERSION_LESS 3.13)
170173 target_link_libraries (module INTERFACE "$<$<PLATFORM_ID:Darwin>:-undefined dynamic_lookup>" )
171174else ()
172175 # 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.
173178 target_link_options (module INTERFACE "$<$<PLATFORM_ID:Darwin>:SHELL:-undefined dynamic_lookup>" )
174179endif ()
175180
176181
177- message (STATUS "CXX Compiler version: ${CMAKE_CXX_COMPILER_VERSION} " )
178-
179182# Workaround for Python 2.7 and C++17 (C++14 as a warning) incompatibility
183+ # This adds the flags -Wno-register and -Wno-deprecated-register if the compiler
184+ # is Clang 3.9+ or AppleClang and the compile language is CXX, or /wd5033 for MSVC (all languages,
185+ # since MSVC didn't recognize COMPILE_LANGUAGE until CMake 3.11+).
180186set (clang_4plus "$<AND:$<CXX_COMPILER_ID:Clang>,$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,3.9>>>" )
181187set (no_register "$<OR:${clang_4plus} ,$<CXX_COMPILER_ID:AppleClang>>" )
182188set (cxx_no_register "$<AND:$<COMPILE_LANGUAGE:CXX>,${no_register} >" )
@@ -214,6 +220,7 @@ if(PYBIND11_INSTALL)
214220
215221 set (CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P} )
216222 else ()
223+ # CMake 3.14+ natively supports header-only libraries
217224 write_basic_package_version_file(
218225 ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} ConfigVersion.cmake
219226 VERSION
0 commit comments