Skip to content

Commit

Permalink
Merge pull request #271 from FeignClaims/feat/update_c_cxx_latest_sta…
Browse files Browse the repository at this point in the history
…ndard

feat: Update default language standard to the latest standard + feat: Set cppcheck default std instead of defaulting standard based on it
  • Loading branch information
aminya authored Aug 29, 2024
2 parents 8da3903 + 67f136d commit 588c32b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 52 deletions.
37 changes: 11 additions & 26 deletions src/Standards.cmake
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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}"
Expand All @@ -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}"
Expand Down
38 changes: 12 additions & 26 deletions src/StaticAnalyzers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 588c32b

Please sign in to comment.