Skip to content

Generalize cmake compiler support #1428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ find_package(Boost 1.56)
function(pybind11_enable_warnings target_name)
if(MSVC)
target_compile_options(${target_name} PRIVATE /W4)
else()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)")
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated)
endif()

if(PYBIND11_WERROR)
if(MSVC)
target_compile_options(${target_name} PRIVATE /WX)
else()
elseif(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Intel|Clang)")
target_compile_options(${target_name} PRIVATE -Werror)
endif()
endif()
Expand Down
32 changes: 24 additions & 8 deletions tools/pybind11Tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,40 @@ find_package(PythonLibsNew ${PYBIND11_PYTHON_VERSION} REQUIRED)
include(CheckCXXCompilerFlag)
include(CMakeParseArguments)

# 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()

# Fall back to heuristics
if(NOT PYBIND11_CPP_STANDARD AND NOT CMAKE_CXX_STANDARD)
if(NOT MSVC)
if(MSVC)
set(PYBIND11_CPP_STANDARD /std:c++14)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is potentially problematic. The version requirement for pybind11 is MSVC 2015 Update 3. I don't think it will be able to deal with C++14 mode.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSVC 2015 Update 3 is the one that introduced /std:c++14 and /std:c++latest.

https://blogs.msdn.microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm good point -- and I forgot that we're actually testing MSVC 2015 on AppVeyor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW the MSVC behavior isn't changed in this PR from what it was before. I only inverted the if/else order to have the larger, most often followed, code block to be the if condition rather than the else.

else()
check_cxx_compiler_flag("-std=c++14" HAS_CPP14_FLAG)

if (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()
Expand Down