From ea379f2f180befabd2039342db8eaeb757fdd2b7 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Sat, 15 Jun 2024 18:07:04 +0300 Subject: [PATCH] CMake: Add warning options for GCC and Clang The list was copied from configure.ac and should be kept in sync. (Pretend that the deleted comment in CMakeLists.txt didn't exist.) There is no need to add equivalent of --enable-werror as CMake >= 3.24 supports -DCMAKE_COMPILE_WARNING_AS_ERROR=ON. --- CMakeLists.txt | 64 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c3a7e31b..62ddc02a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,8 +25,6 @@ # - External SHA-256 code isn't supported but it's disabled by # default in the Autotools build too (--enable-external-sha256). # -# - Extra compiler warning flags aren't added by default. -# # About CMAKE_BUILD_TYPE: # # - CMake's standard choices are fine to use for production builds, @@ -106,6 +104,7 @@ include(CheckIncludeFile) include(CheckSymbolExists) include(CheckStructHasMember) include(CheckCSourceCompiles) +include(CheckCCompilerFlag) include(cmake/tuklib_large_file_support.cmake) include(cmake/tuklib_integer.cmake) include(cmake/tuklib_cpucores.cmake) @@ -349,10 +348,65 @@ if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.20") endif() endif() -# Options for new enough GCC or Clang on any arch or operating system: +# Add warning options for GCC or Clang. Keep this in sync with configure.ac. +# +# NOTE: add_compile_options() doesn't affect the feature checks; +# only the new targets being created use these flags. Thus +# the -Werror usage in checks won't be break because of these. if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang) - # configure.ac has a long list but it won't be copied here: - add_compile_options(-Wall -Wextra) + foreach(OPT -Wall + -Wextra + -Wvla + -Wformat=2 + -Winit-self + -Wmissing-include-dirs + -Wshift-overflow=2 + -Wstrict-overflow=3 + -Walloc-zero + -Wduplicated-cond + -Wfloat-equal + -Wundef + -Wshadow + -Wpointer-arith + -Wbad-function-cast + -Wwrite-strings + -Wdate-time + -Wsign-conversion + -Wfloat-conversion + -Wlogical-op + -Waggregate-return + -Wstrict-prototypes + -Wold-style-definition + -Wmissing-prototypes + -Wmissing-declarations + -Wredundant-decls + + -Wc99-compat + -Wc11-extensions + -Wc2x-compat + -Wc2x-extensions + -Wpre-c2x-compat + -Warray-bounds-pointer-arithmetic + -Wassign-enum + -Wconditional-uninitialized + -Wdocumentation + -Wduplicate-enum + -Wempty-translation-unit + -Wflexible-array-extensions + -Wmissing-variable-declarations + -Wnewline-eof + -Wshift-sign-overflow + -Wstring-conversion + ) + # A variable name cannot have = in it so replace = with _. + string(REPLACE = _ CACHE_VAR "HAVE_COMPILER_OPTION_${OPT}") + + check_c_compiler_flag("${OPT}" "${CACHE_VAR}") + + if("${${CACHE_VAR}}") + add_compile_options("${OPT}") + endif() + endforeach() endif()