[cmake] add default compiler flags

This commit is contained in:
Armin Novak 2023-10-11 14:05:18 +02:00 committed by akallabeth
parent 2b51551cb8
commit 1761edf2f8
2 changed files with 46 additions and 12 deletions

View File

@ -77,6 +77,7 @@ if(WITH_CLANG_FORMAT)
include(ClangFormat) include(ClangFormat)
endif() endif()
include(CompilerFlags)
include(DetectBSD) include(DetectBSD)
include(CheckIncludeFiles) include(CheckIncludeFiles)
include(CheckLibraryExists) include(CheckLibraryExists)
@ -392,19 +393,7 @@ if(${CMAKE_C_COMPILER_ID} STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCC)
endif() endif()
if(MSVC) if(MSVC)
# Remove previous warning definitions,
# NMake is otherwise complaining.
foreach (flags_var_to_scrub
CMAKE_C_FLAGS
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_MINSIZEREL)
string (REGEX REPLACE "(^| )[/-]W[ ]*[1-9]" " "
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
endforeach()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Gd")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}) set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR})

45
cmake/CompilerFlags.cmake Normal file
View File

@ -0,0 +1,45 @@
include(CheckCCompilerFlag)
macro (checkCFlag FLAG)
CHECK_C_COMPILER_FLAG("${FLAG}" CFLAG${FLAG})
if(CFLAG${FLAG})
string(APPEND CMAKE_C_FLAGS " ${FLAG}")
else()
message(WARNING "compiler does not support ${FLAG}")
endif()
endmacro()
option(ENABLE_WARNING_VERBOSE "enable -Weveryting (and some exceptions) for compile" ON)
option(ENABLE_WARNING_ERROR "enable -Werror for compile" OFF)
if (ENABLE_WARNING_VERBOSE)
if (MSVC)
set(C_WARNING_FLAGS
/Wall
)
else()
set(C_WARNING_FLAGS
-Weverything
-Wall
-Wpedantic
-Wno-padded
-Wno-cast-align
-Wno-declaration-after-statement
-Wno-unsafe-buffer-usage
-Wno-reserved-identifier
-Wno-covered-switch-default
)
endif()
foreach(FLAG ${C_WARNING_FLAGS})
CheckCFlag(${FLAG})
endforeach()
endif()
if (ENABLE_WARNING_ERROR)
CheckCFlag(-Werror)
endif()
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} CACHE STRING "default CFLAGS")
message("Using CFLAGS ${CMAKE_C_FLAGS}")