Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update default language standard to the latest standard + feat: Set cppcheck default std instead of defaulting standard based on it #271

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
FeignClaims marked this conversation as resolved.
Show resolved Hide resolved
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]])
FeignClaims marked this conversation as resolved.
Show resolved Hide resolved
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)
FeignClaims marked this conversation as resolved.
Show resolved Hide resolved
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
Loading