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}" diff --git a/src/StaticAnalyzers.cmake b/src/StaticAnalyzers.cmake index b7776c46..a537921d 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() @@ -54,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() @@ -143,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()