From d730fbc0d54a3c3a7655cabe0d4f091f122b4998 Mon Sep 17 00:00:00 2001 From: Chuck Atkins Date: Tue, 12 Jun 2018 13:18:48 -0400 Subject: [PATCH] Utilize CMake's language standards abstraction when possible --- tools/pybind11Tools.cmake | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake index 508e4742..8d85dd4e 100644 --- a/tools/pybind11Tools.cmake +++ b/tools/pybind11Tools.cmake @@ -18,24 +18,40 @@ find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED) include(CheckCXXCompilerFlag) include(CMakeParseArguments) -if(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD) - if(NOT MSVC) - check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG) +# Use the language standards abstraction if CMake supports it with the current compiler +if(NOT CMAKE_VERSION VERSION_LESS 3.1) + if(NOT CMAKE_CXX_STANDARD) + if(CMAKE_CXX14_STANDARD_COMPILE_OPTION) + set(CMAKE_CXX_STANDARD 14) + elseif(CMAKE_CXX11_STANDARD_COMPILE_OPTION) + set(CMAKE_CXX_STANDARD 11) + endif() + endif() + if(CMAKE_CXX_STANDARD) + set(CMAKE_CXX_EXTENSIONS OFF) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + endif() +endif() - if (HAS_CPP14_FLAG) +# Fall back to heuristics +if(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD) + if(MSVC) + set(PYBIND11_CPP_STANDARD /std:c++14) + else() + check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG) + if(HAS_CPP14_FLAG) set(PYBIND11_CPP_STANDARD -std=c++14) else() check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG) - if (HAS_CPP11_FLAG) + if(HAS_CPP11_FLAG) set(PYBIND11_CPP_STANDARD -std=c++11) - else() - message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!") endif() endif() - elseif(MSVC) - set(PYBIND11_CPP_STANDARD /std:c++14) endif() + if(NOT PYBIND11_CPP_STANDARD) + message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!") + endif() set(PYBIND11_CPP_STANDARD ${PYBIND11_CPP_STANDARD} CACHE STRING "C++ standard flag, e.g. -std=c++11, -std=c++14, /std:c++14. Defaults to C++14 mode." FORCE) endif()