From fad5d3386ce76807a6195d069bed8b0fee5ddcab Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Wed, 12 Jul 2017 14:59:16 -0400 Subject: [PATCH] Detect c++ standard unconditionally Currently select_cxx_standard(), which sets PYBIND11_CPP_STANDARD when not externally set, is only called from pybind11_add_module(), but the embed target setup (which runs unconditionally) makes use of ${PYBIND11_CPP_STANDARD}, which isn't set yet. This commit removes the `select_cxx_standard` function completely and just always runs the standard detection code. This also tweaks the detection code to not bothering checking for the `-std=c++11` flag when the `-std=c++14` detection succeeded. --- tools/pybind11Config.cmake.in | 1 - tools/pybind11Tools.cmake | 29 ++++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/tools/pybind11Config.cmake.in b/tools/pybind11Config.cmake.in index 6f0a406fb..3dd1b2c1a 100644 --- a/tools/pybind11Config.cmake.in +++ b/tools/pybind11Config.cmake.in @@ -90,7 +90,6 @@ if(NOT TARGET ${PN}::pybind11) set_property(TARGET ${PN}::module APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${PYTHON_LIBRARIES}) endif() - select_cxx_standard() set_property(TARGET ${PN}::pybind11 APPEND PROPERTY INTERFACE_COMPILE_OPTIONS "${PYBIND11_CPP_STANDARD}") get_property(_iid TARGET ${PN}::pybind11 PROPERTY INTERFACE_INCLUDE_DIRECTORIES) diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake index 62de9c9fd..86ccd58d3 100644 --- a/tools/pybind11Tools.cmake +++ b/tools/pybind11Tools.cmake @@ -18,27 +18,27 @@ find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED) include(CheckCXXCompilerFlag) include(CMakeParseArguments) -function(select_cxx_standard) - if(NOT PYBIND11_CPP_STANDARD) - if(NOT MSVC) - check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG) - check_cxx_compiler_flag("-std=c++11" HAS_CPP11_FLAG) +if(NOT PYBIND11_CPP_STANDARD) + if(NOT MSVC) + check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG) - if (HAS_CPP14_FLAG) - set(PYBIND11_CPP_STANDARD -std=c++14) - elseif (HAS_CPP11_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) set(PYBIND11_CPP_STANDARD -std=c++11) else() message(FATAL_ERROR "Unsupported compiler -- pybind11 requires C++11 support!") endif() - elseif(MSVC) - set(PYBIND11_CPP_STANDARD /std:c++14) 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) + elseif(MSVC) + set(PYBIND11_CPP_STANDARD /std:c++14) endif() -endfunction() + + 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() # Checks whether the given CXX/linker flags can compile and link a cxx file. cxxflags and # linkerflags are lists of flags to use. The result variable is a unique variable name for each set @@ -165,7 +165,6 @@ function(pybind11_add_module target_name) endif() endif() - select_cxx_standard() # Make sure C++11/14 are enabled target_compile_options(${target_name} PUBLIC ${PYBIND11_CPP_STANDARD})