Skip to content

Commit 2083cf3

Browse files
committed
Making pybind cmake version optional for CMake 3.8+
1 parent 0aef642 commit 2083cf3

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ if(NOT (CMAKE_VERSION VERSION_LESS 3.0)) # CMake >= 3.0
9898
target_include_directories(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_INCLUDE_DIR}>
9999
$<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>
100100
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
101-
target_compile_options(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)
101+
if(CMAKE_VERSION VERSION_LESS 3.8 OR PYBIND_CPP_STANDARD)
102+
target_compile_options(pybind11 INTERFACE $<BUILD_INTERFACE:${PYBIND11_CPP_STANDARD}>)
103+
else()
104+
target_compile_features(pybind11 INTERFACE cxx_std_11)
105+
endif()
102106

103107
add_library(module INTERFACE)
104108
add_library(pybind11::module ALIAS module)

tools/pybind11Tools.cmake

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,28 @@ find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
1818
include(CheckCXXCompilerFlag)
1919
include(CMakeParseArguments)
2020

21-
if(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD)
22-
if(NOT MSVC)
23-
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
24-
25-
if (HAS_CPP14_FLAG)
26-
set(PYBIND11_CPP_STANDARD -std=c++14)
27-
else()
28-
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
29-
if (HAS_CPP11_FLAG)
30-
set(PYBIND11_CPP_STANDARD -std=c++11)
21+
if(NOT CMAKE_CXX_STANDARD)
22+
if(NOT PYBIND11_CPP_STANDARD AND CMAKE_VERSION VERSION_LESS 3.8)
23+
if(NOT MSVC)
24+
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)
25+
26+
if (HAS_CPP14_FLAG)
27+
set(PYBIND11_CPP_STANDARD -std=c++14)
3128
else()
32-
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
29+
check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG)
30+
if (HAS_CPP11_FLAG)
31+
set(PYBIND11_CPP_STANDARD -std=c++11)
32+
else()
33+
message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!")
34+
endif()
3335
endif()
36+
elseif(MSVC)
37+
set(PYBIND11_CPP_STANDARD /std:c++14)
3438
endif()
35-
elseif(MSVC)
36-
set(PYBIND11_CPP_STANDARD /std:c++14)
39+
40+
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
41+
"C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE)
3742
endif()
38-
39-
set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING
40-
"C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE)
4143
endif()
4244

4345
# Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and
@@ -173,7 +175,11 @@ function(pybind11_add_module target_name)
173175
endif()
174176

175177
# Make sure C++11/14 are enabled
176-
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
178+
if(PYBIND11_CPP_STANDARD OR ${CMAKE_VERSION} VERSION_LESS 3.8)
179+
target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})
180+
else()
181+
target_compile_features(${target_name} PUBLIC cxx_std_11) # Will be 14 if parent project requests 14
182+
endif()
177183

178184
if(ARG_NO_EXTRAS)
179185
return()

0 commit comments

Comments
 (0)