From a0e23c8464374d3adf09e8a01ee7805ed6f4b3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 16 Nov 2026 09:08:36 +0100 Subject: [PATCH] Fix #762 (CI: use target commands instead of directory-level commands in CMakeLists.txt) Newer CMake reports add_definitions(), add_compile_definitions() and add_compile_options() as non-target directives, and -Werror=dev makes the configure step fail. Collect the compile options and definitions in lists and apply them with target_compile_options() and target_compile_definitions() to simplecpp_obj, simplecpp and testrunner. The compiler command lines are unchanged. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 89 +++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 220c6c3f..0031c023 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,34 +10,39 @@ endif() include(CheckCXXCompilerFlag) +# compile options and definitions for all targets, they are applied with target_compile_options() and +# target_compile_definitions() since directory-level commands like add_compile_options() are not recommended +set(SIMPLECPP_COMPILE_OPTIONS) +set(SIMPLECPP_COMPILE_DEFINITIONS) + if (WIN32) # prevent simplifyPath_cppcheck() from wasting time on looking for a hypothetical network host - add_definitions(-DUNCHOST=$ENV{COMPUTERNAME}) + list(APPEND SIMPLECPP_COMPILE_DEFINITIONS UNCHOST=$ENV{COMPUTERNAME}) endif() function(add_compile_options_safe FLAG) string(MAKE_C_IDENTIFIER "HAS_CXX_FLAG${FLAG}" mangled_flag) check_cxx_compiler_flag(${FLAG} ${mangled_flag}) if (${mangled_flag}) - add_compile_options(${FLAG}) + set(SIMPLECPP_COMPILE_OPTIONS ${SIMPLECPP_COMPILE_OPTIONS} ${FLAG} PARENT_SCOPE) endif() endfunction() if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - add_compile_options(-pedantic) - - add_compile_options(-Wall) - add_compile_options(-Wextra) - add_compile_options(-Wcast-qual) # Cast for removing type qualifiers - add_compile_options(-Wfloat-equal) # Floating values used in equality comparisons - add_compile_options(-Wmissing-declarations) # If a global function is defined without a previous declaration - add_compile_options(-Wmissing-format-attribute) # - add_compile_options(-Wpacked) # - add_compile_options(-Wredundant-decls) # if anything is declared more than once in the same scope - add_compile_options(-Wundef) - add_compile_options(-Woverloaded-virtual) # when a function declaration hides virtual functions from a base class - - add_compile_options(-Wsuggest-attribute=noreturn) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -pedantic) + + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wall) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wextra) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wcast-qual) # Cast for removing type qualifiers + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wfloat-equal) # Floating values used in equality comparisons + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wmissing-declarations) # If a global function is defined without a previous declaration + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wmissing-format-attribute) # + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wpacked) # + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wredundant-decls) # if anything is declared more than once in the same scope + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wundef) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Woverloaded-virtual) # when a function declaration hides virtual functions from a base class + + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wsuggest-attribute=noreturn) if (NOT MINGW) add_compile_options_safe(-Wuseless-cast) endif() @@ -45,32 +50,32 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # we are not interested in these set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS -Wno-multichar) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - add_compile_definitions(_CRT_SECURE_NO_WARNINGS) + list(APPEND SIMPLECPP_COMPILE_DEFINITIONS _CRT_SECURE_NO_WARNINGS) - add_compile_options(/W4) # Warning Level + list(APPEND SIMPLECPP_COMPILE_OPTIONS /W4) # Warning Level - add_compile_options(/wd4127) # warning C4127: conditional expression is constant - add_compile_options(/wd4244) # warning C4244: 'x': conversion from 'int' to 'char', possible loss of data - add_compile_options(/wd4267) # warning C4267: '...': conversion from 'size_t' to 'unsigned int', possible loss of data - add_compile_options(/wd4706) # warning C4706: assignment within conditional expression + list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4127) # warning C4127: conditional expression is constant + list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4244) # warning C4244: 'x': conversion from 'int' to 'char', possible loss of data + list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4267) # warning C4267: '...': conversion from 'size_t' to 'unsigned int', possible loss of data + list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4706) # warning C4706: assignment within conditional expression elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Weverything) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Weverything) # no need for c++98 compatibility - add_compile_options(-Wno-c++98-compat-pedantic) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-c++98-compat-pedantic) # these are not really fixable until newer standards - add_compile_options(-Wno-exit-time-destructors) - add_compile_options(-Wno-global-constructors) - add_compile_options(-Wno-weak-vtables) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-exit-time-destructors) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-global-constructors) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-weak-vtables) add_compile_options_safe(-Wno-unsafe-buffer-usage) add_compile_options_safe(-Wno-nrvo) # contradicts -Wcovered-switch-default - add_compile_options(-Wno-switch-default) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-switch-default) if (MINGW) - add_compile_options(-Wno-reserved-macro-identifier) - add_compile_options(-Wno-unused-macros) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-reserved-macro-identifier) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-unused-macros) endif() # these are experimental warnings which might produce false positives @@ -83,11 +88,11 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options_safe(-Wno-lifetime-safety-cross-tu-constructor-suggestions) # TODO: fix these? - add_compile_options(-Wno-padded) - add_compile_options(-Wno-sign-conversion) - add_compile_options(-Wno-implicit-int-conversion) - add_compile_options(-Wno-shorten-64-to-32) - add_compile_options(-Wno-shadow-field-in-constructor) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-padded) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-sign-conversion) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-implicit-int-conversion) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-shorten-64-to-32) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-shadow-field-in-constructor) # we are not interested in these set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS "-Wno-multichar -Wno-four-char-constants") @@ -96,16 +101,16 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # TODO: verify this regression still exists in clang-15 if (CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") # work around performance regression - see https://github.com/llvm/llvm-project/issues/53555 - add_compile_options(-mllvm -inline-deferral) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -mllvm -inline-deferral) endif() # use force DWARF 4 debug format since not all tools might be able to handle DWARF 5 yet - e.g. valgrind on ubuntu 20.04 - add_compile_options(-gdwarf-4) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -gdwarf-4) endif() if (APPLE) # CMake is sometimes chosing the wrong compiler on macos-* runners # see https://github.com/actions/runner/issues/4034 - add_compile_options(-Wno-poison-system-directories) + list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-poison-system-directories) endif() endif() @@ -113,6 +118,14 @@ add_library(simplecpp_obj OBJECT simplecpp.cpp) add_executable(simplecpp $ main.cpp) add_executable(testrunner $ test.cpp) +foreach(target simplecpp_obj simplecpp testrunner) + if (SIMPLECPP_COMPILE_OPTIONS) + target_compile_options(${target} PRIVATE ${SIMPLECPP_COMPILE_OPTIONS}) + endif() + if (SIMPLECPP_COMPILE_DEFINITIONS) + target_compile_definitions(${target} PRIVATE ${SIMPLECPP_COMPILE_DEFINITIONS}) + endif() +endforeach() target_compile_definitions(testrunner PRIVATE SIMPLECPP_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"