From 9ea0e32e18247551da26ff87f5e9bfcee721740a Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 13 Aug 2024 19:13:36 +0800 Subject: [PATCH 1/3] feat: Update C and C++ standard checking to latest standard --- src/Standards.cmake | 37 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/src/Standards.cmake b/src/Standards.cmake index 83624b0a..c8dc590f 100644 --- a/src/Standards.cmake +++ b/src/Standards.cmake @@ -1,5 +1,14 @@ include_guard() +function(_set_language_standard output language) + foreach(version IN LISTS ARGN) + if(DEFINED "CMAKE_${language}${version}_STANDARD_COMPILE_OPTION" OR DEFINED "CMAKE_${language}${version}_EXTENSION_COMPILE_OPTION") + set("${output}" "${version}" PARENT_SCOPE) + break() + endif() + endforeach() +endfunction() + # Set the default copmiler standards if not specified macro(set_standards) @@ -8,19 +17,7 @@ macro(set_standards) # Like not having compiler warnings on by default, this fixes another `bad` default for the compilers # If someone needs an older standard like c++11 although their compiler supports c++20, they can override this by passing -D CMAKE_CXX_STANDARD=11. if("${CMAKE_CXX_STANDARD}" STREQUAL "") - if(DEFINED CMAKE_CXX20_STANDARD_COMPILE_OPTION OR DEFINED CMAKE_CXX20_EXTENSION_COMPILE_OPTION) - set(CXX_LATEST_STANDARD 20) - elseif(DEFINED CMAKE_CXX17_STANDARD_COMPILE_OPTION OR DEFINED - CMAKE_CXX17_EXTENSION_COMPILE_OPTION - ) - set(CXX_LATEST_STANDARD 17) - elseif(DEFINED CMAKE_CXX14_STANDARD_COMPILE_OPTION OR DEFINED - CMAKE_CXX14_EXTENSION_COMPILE_OPTION - ) - set(CXX_LATEST_STANDARD 14) - else() - set(CXX_LATEST_STANDARD 11) - endif() + _set_language_standard(CXX_LATEST_STANDARD CXX 23 20 17 14 11) message( STATUS "The default CMAKE_CXX_STANDARD used by external targets and tools is not set yet. Using the latest supported C++ standard that is ${CXX_LATEST_STANDARD}" @@ -29,19 +26,7 @@ macro(set_standards) endif() if("${CMAKE_C_STANDARD}" STREQUAL "") - if(NOT - ${ProjectOptions_ENABLE_CPPCHECK} # cppcheck doesn't support C17 https://sourceforge.net/p/cppcheck/discussion/general/thread/19ea152bba/ - AND DEFINED CMAKE_C17_STANDARD_COMPILE_OPTION - OR DEFINED CMAKE_C17_EXTENSION_COMPILE_OPTION - ) - set(C_LATEST_STANDARD 17) - elseif(DEFINED CMAKE_C11_STANDARD_COMPILE_OPTION OR DEFINED CMAKE_C11_EXTENSION_COMPILE_OPTION) - set(C_LATEST_STANDARD 11) - elseif(DEFINED CMAKE_C99_STANDARD_COMPILE_OPTION OR DEFINED CMAKE_C99_EXTENSION_COMPILE_OPTION) - set(C_LATEST_STANDARD 99) - else() - set(C_LATEST_STANDARD 90) - endif() + _set_language_standard(C_LATEST_STANDARD C 23 20 17 11 99 90) message( STATUS "The default CMAKE_C_STANDARD used by external targets and tools is not set yet. Using the latest supported C standard that is ${C_LATEST_STANDARD}" From ca70709c828c713f9801de323de9171ddbe9e513 Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 13 Aug 2024 21:48:29 +0800 Subject: [PATCH 2/3] feat: Set cppcheck default std --- src/StaticAnalyzers.cmake | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/StaticAnalyzers.cmake b/src/StaticAnalyzers.cmake index b7776c46..9d87227f 100644 --- a/src/StaticAnalyzers.cmake +++ b/src/StaticAnalyzers.cmake @@ -38,11 +38,21 @@ macro(enable_cppcheck CPPCHECK_OPTIONS) set(CMAKE_C_CPPCHECK ${CMAKE_CXX_CPPCHECK}) if(NOT "${CMAKE_CXX_STANDARD}" STREQUAL "") - set(CMAKE_CXX_CPPCHECK ${CMAKE_CXX_CPPCHECK} --std=c++${CMAKE_CXX_STANDARD}) + if(CMAKE_CXX_STANDARD MATCHES [[03|11|14|17|20]]) + set(CMAKE_CXX_CPPCHECK ${CMAKE_CXX_CPPCHECK} --std=c++${CMAKE_CXX_STANDARD}) + else() + message(${WARNING_MESSAGE} "cppcheck dosen't support specified C++ standard ${CMAKE_CXX_STANDARD}. Using the cppcheck default C++ standard version.") + endif() endif() if(NOT "${CMAKE_C_STANDARD}" STREQUAL "") - set(CMAKE_C_CPPCHECK ${CMAKE_C_CPPCHECK} --std=c${CMAKE_C_STANDARD}) + if(CMAKE_C_STANDARD MATCHES [[90]]) + set(CMAKE_C_CPPCHECK ${CMAKE_C_CPPCHECK} --std=c89) + elseif(CMAKE_C_STANDARD MATCHES [[99|11]]) + set(CMAKE_C_CPPCHECK ${CMAKE_C_CPPCHECK} --std=c${CMAKE_C_STANDARD}) + else() + message(${WARNING_MESSAGE} "cppcheck dosen't support specified C standard ${CMAKE_C_STANDARD}. Using the cppcheck default C standard version.") + endif() endif() else() From 67f136d34e1b0cb79ff7eeafe154f7b7f4d81d1b Mon Sep 17 00:00:00 2001 From: FeignClaims Date: Tue, 13 Aug 2024 22:14:20 +0800 Subject: [PATCH 3/3] fix: Remove clang-tidy language standard arg This is not required according to [the implementation](https://gitlab.kitware.com/cmake/cmake/-/blob/8733dcd11588f2d9eb0bddc8af8a6db7a2e9eba8/Source/cmcmd.cxx#L370-416). --- src/StaticAnalyzers.cmake | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/StaticAnalyzers.cmake b/src/StaticAnalyzers.cmake index 9d87227f..a537921d 100644 --- a/src/StaticAnalyzers.cmake +++ b/src/StaticAnalyzers.cmake @@ -64,14 +64,6 @@ function(_enable_clang_tidy_setup_cl CXX_FLAGS C_FLAGS) set(CLANG_TIDY_CXX_FLAGS ${CXX_FLAGS}) set(CLANG_TIDY_C_FLAGS ${C_FLAGS}) - if(CMAKE_CXX_STANDARD) - list(APPEND CLANG_TIDY_CXX_FLAGS -extra-arg=/std:c++${CMAKE_CXX_STANDARD}) - endif() - - if(CMAKE_C_STANDARD) - list(APPEND CLANG_TIDY_C_FLAGS -extra-arg=/std:c${CMAKE_C_STANDARD}) - endif() - set(CLANG_TIDY_CXX_FLAGS ${CLANG_TIDY_CXX_FLAGS} PARENT_SCOPE) set(CLANG_TIDY_C_FLAGS ${CLANG_TIDY_C_FLAGS} PARENT_SCOPE) endfunction() @@ -153,22 +145,6 @@ function(_enable_clang_tidy_setup CXX_FLAGS C_FLAGS) set(CLANG_TIDY_CXX_FLAGS ${CXX_FLAGS}) set(CLANG_TIDY_C_FLAGS ${C_FLAGS}) - if(CMAKE_CXX_STANDARD) - if(CMAKE_CXX_EXTENSIONS) - list(APPEND CLANG_TIDY_CXX_FLAGS -extra-arg=-std=gnu++${CMAKE_CXX_STANDARD}) - else() - list(APPEND CLANG_TIDY_CXX_FLAGS -extra-arg=-std=c++${CMAKE_CXX_STANDARD}) - endif() - endif() - - if(CMAKE_C_STANDARD) - if(CMAKE_C_EXTENSIONS) - list(APPEND CLANG_TIDY_C_FLAGS -extra-arg=-std=gnu${CMAKE_C_STANDARD}) - else() - list(APPEND CLANG_TIDY_C_FLAGS -extra-arg=-std=c${CMAKE_C_STANDARD}) - endif() - endif() - if(CMAKE_CROSSCOMPILING) _enable_clang_tidy_setup_cross("${CLANG_TIDY_CXX_FLAGS}" "${CLANG_TIDY_C_FLAGS}") endif()