From 1f53c373e44ab80a1a3d9b0dbc6c4e8dd824e9c1 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 23 Jul 2020 22:55:29 -0400 Subject: [PATCH] fix: C++17 mode on Clang may error --- tests/CMakeLists.txt | 6 +++--- tests/test_cmake_build/installed_embed/CMakeLists.txt | 9 +++++++++ tests/test_cmake_build/installed_target/CMakeLists.txt | 8 ++++++++ .../test_cmake_build/subdirectory_embed/CMakeLists.txt | 10 ++++++++++ .../subdirectory_target/CMakeLists.txt | 8 ++++++++ tests/test_embed/CMakeLists.txt | 10 ++++++++++ tools/pybind11Tools.cmake | 8 ++++++++ 7 files changed, 56 insertions(+), 3 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b1c72b76a..1c32ececb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -152,10 +152,10 @@ function(pybind11_enable_warnings target_name) endif() endif() - if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) - if(CMAKE_CXX_STANDARD STREQUAL "14") + if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) + if(CMAKE_CXX_STANDARD LESS 17) target_compile_options(${target_name} PUBLIC -Wno-deprecated-register) - elseif(NOT CMAKE_CXX_STANDARD VERSION_LESS 17) + else() target_compile_options(${target_name} PUBLIC -Wno-register) endif() endif() diff --git a/tests/test_cmake_build/installed_embed/CMakeLists.txt b/tests/test_cmake_build/installed_embed/CMakeLists.txt index f7fc09c21..6ab3efd47 100644 --- a/tests/test_cmake_build/installed_embed/CMakeLists.txt +++ b/tests/test_cmake_build/installed_embed/CMakeLists.txt @@ -8,8 +8,17 @@ message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}") add_executable(test_cmake_build ../embed.cpp) target_link_libraries(test_cmake_build PRIVATE pybind11::embed) + # Do not treat includes from IMPORTED target as SYSTEM (Python headers in pybind11::embed). # This may be needed to resolve header conflicts, e.g. between Python release and debug headers. set_target_properties(test_cmake_build PROPERTIES NO_SYSTEM_FROM_IMPORTED ON) add_custom_target(check $ ${PROJECT_SOURCE_DIR}/../test.py) + +if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) + if(CMAKE_CXX_STANDARD LESS 17) + target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) + else() + target_compile_options(test_cmake_build PUBLIC -Wno-register) + endif() +endif() diff --git a/tests/test_cmake_build/installed_target/CMakeLists.txt b/tests/test_cmake_build/installed_target/CMakeLists.txt index cd3ae6f7d..38520fa9c 100644 --- a/tests/test_cmake_build/installed_target/CMakeLists.txt +++ b/tests/test_cmake_build/installed_target/CMakeLists.txt @@ -20,3 +20,11 @@ set_target_properties(test_cmake_build PROPERTIES NO_SYSTEM_FROM_IMPORTED ON) add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py ${PROJECT_NAME}) + +if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) + if(CMAKE_CXX_STANDARD LESS 17) + target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) + else() + target_compile_options(test_cmake_build PUBLIC -Wno-register) + endif() +endif() diff --git a/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt b/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt index 88ba60dd5..003e93c22 100644 --- a/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_embed/CMakeLists.txt @@ -23,3 +23,13 @@ install(TARGETS test_embed_lib RUNTIME DESTINATION lib) install(EXPORT test_export DESTINATION lib/cmake/test_export/test_export-Targets.cmake) + +if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) + if(CMAKE_CXX_STANDARD LESS 17) + target_compile_options(test_embed_lib PUBLIC -Wno-deprecated-register) + target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) + else() + target_compile_options(test_embed_lib PUBLIC -Wno-register) + target_compile_options(test_cmake_build PUBLIC -Wno-register) + endif() +endif() diff --git a/tests/test_cmake_build/subdirectory_target/CMakeLists.txt b/tests/test_cmake_build/subdirectory_target/CMakeLists.txt index 6b142d62a..507cb8b99 100644 --- a/tests/test_cmake_build/subdirectory_target/CMakeLists.txt +++ b/tests/test_cmake_build/subdirectory_target/CMakeLists.txt @@ -13,3 +13,11 @@ set_target_properties(test_cmake_build PROPERTIES PREFIX "${PYTHON_MODULE_PREFIX add_custom_target(check ${CMAKE_COMMAND} -E env PYTHONPATH=$ ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/../test.py ${PROJECT_NAME}) + +if(CMAKE_CXX_STANDARD AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) + if(CMAKE_CXX_STANDARD LESS 17) + target_compile_options(test_cmake_build PUBLIC -Wno-deprecated-register) + else() + target_compile_options(test_cmake_build PUBLIC -Wno-register) + endif() +endif() diff --git a/tests/test_embed/CMakeLists.txt b/tests/test_embed/CMakeLists.txt index c5aa7c6c5..4eef2dff1 100644 --- a/tests/test_embed/CMakeLists.txt +++ b/tests/test_embed/CMakeLists.txt @@ -43,3 +43,13 @@ endforeach() add_dependencies(cpptest external_module) add_dependencies(check cpptest) + +if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND PYTHON_VERSION VERSION_LESS 3.0) + if(CMAKE_CXX_STANDARD LESS 17) + target_compile_options(test_embed PUBLIC -Wno-deprecated-register) + target_compile_options(external_module PUBLIC -Wno-deprecated-register) + else() + target_compile_options(test_embed PUBLIC -Wno-register) + target_compile_options(external_module PUBLIC -Wno-register) + endif() +endif() diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake index a3603abe9..be536e6b6 100644 --- a/tools/pybind11Tools.cmake +++ b/tools/pybind11Tools.cmake @@ -224,6 +224,14 @@ function(pybind11_add_module target_name) endif() endif() + # Python 2 doesn't really support C++17, Clang warns/errors over it + if(CMAKE_CXX_STANDARD + AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" + AND PYTHON_VERSION VERSION_LESS 3.0 + AND NOT CMAKE_CXX_STANDARD LESS 17) + target_compile_options(${target_name} PUBLIC -Wno-register) + endif() + if(ARG_NO_EXTRAS) return() endif()