Skip to content

Commit

Permalink
feat: Update C and C++ standard checking to latest standard
Browse files Browse the repository at this point in the history
  • Loading branch information
FeignClaims committed Aug 13, 2024
1 parent 8da3903 commit d6f7e89
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 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 26 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,18 +26,10 @@ 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)
if(${ProjectOptions_ENABLE_CPPCHECK})
_set_language_standard(C_LATEST_STANDARD C 17 11 99 90)
else()
set(C_LATEST_STANDARD 90)
_set_language_standard(C_LATEST_STANDARD C 23 20 17 11 99 90)
endif()
message(
STATUS
Expand Down

0 comments on commit d6f7e89

Please sign in to comment.