Skip to content

Commit 31989dd

Browse files
danmarclaude
andauthored
Fix #762 (CI: use target commands instead of directory-level commands in CMakeLists.txt) (#763)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 79c4663 commit 31989dd

1 file changed

Lines changed: 51 additions & 38 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,72 @@ endif()
1010

1111
include(CheckCXXCompilerFlag)
1212

13+
# compile options and definitions for all targets, they are applied with target_compile_options() and
14+
# target_compile_definitions() since directory-level commands like add_compile_options() are not recommended
15+
set(SIMPLECPP_COMPILE_OPTIONS)
16+
set(SIMPLECPP_COMPILE_DEFINITIONS)
17+
1318
if (WIN32)
1419
# prevent simplifyPath_cppcheck() from wasting time on looking for a hypothetical network host
15-
add_definitions(-DUNCHOST=$ENV{COMPUTERNAME})
20+
list(APPEND SIMPLECPP_COMPILE_DEFINITIONS UNCHOST=$ENV{COMPUTERNAME})
1621
endif()
1722

1823
function(add_compile_options_safe FLAG)
1924
string(MAKE_C_IDENTIFIER "HAS_CXX_FLAG${FLAG}" mangled_flag)
2025
check_cxx_compiler_flag(${FLAG} ${mangled_flag})
2126
if (${mangled_flag})
22-
add_compile_options(${FLAG})
27+
set(SIMPLECPP_COMPILE_OPTIONS ${SIMPLECPP_COMPILE_OPTIONS} ${FLAG} PARENT_SCOPE)
2328
endif()
2429
endfunction()
2530

2631
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
27-
add_compile_options(-pedantic)
28-
29-
add_compile_options(-Wall)
30-
add_compile_options(-Wextra)
31-
add_compile_options(-Wcast-qual) # Cast for removing type qualifiers
32-
add_compile_options(-Wfloat-equal) # Floating values used in equality comparisons
33-
add_compile_options(-Wmissing-declarations) # If a global function is defined without a previous declaration
34-
add_compile_options(-Wmissing-format-attribute) #
35-
add_compile_options(-Wpacked) #
36-
add_compile_options(-Wredundant-decls) # if anything is declared more than once in the same scope
37-
add_compile_options(-Wundef)
38-
add_compile_options(-Woverloaded-virtual) # when a function declaration hides virtual functions from a base class
39-
40-
add_compile_options(-Wsuggest-attribute=noreturn)
32+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -pedantic)
33+
34+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wall)
35+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wextra)
36+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wcast-qual) # Cast for removing type qualifiers
37+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wfloat-equal) # Floating values used in equality comparisons
38+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wmissing-declarations) # If a global function is defined without a previous declaration
39+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wmissing-format-attribute) #
40+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wpacked) #
41+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wredundant-decls) # if anything is declared more than once in the same scope
42+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wundef)
43+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Woverloaded-virtual) # when a function declaration hides virtual functions from a base class
44+
45+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wsuggest-attribute=noreturn)
4146
if (NOT MINGW)
4247
add_compile_options_safe(-Wuseless-cast)
4348
endif()
4449

4550
# we are not interested in these
4651
set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS -Wno-multichar)
4752
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
48-
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
53+
list(APPEND SIMPLECPP_COMPILE_DEFINITIONS _CRT_SECURE_NO_WARNINGS)
4954

50-
add_compile_options(/W4) # Warning Level
55+
list(APPEND SIMPLECPP_COMPILE_OPTIONS /W4) # Warning Level
5156

52-
add_compile_options(/wd4127) # warning C4127: conditional expression is constant
53-
add_compile_options(/wd4244) # warning C4244: 'x': conversion from 'int' to 'char', possible loss of data
54-
add_compile_options(/wd4267) # warning C4267: '...': conversion from 'size_t' to 'unsigned int', possible loss of data
55-
add_compile_options(/wd4706) # warning C4706: assignment within conditional expression
57+
list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4127) # warning C4127: conditional expression is constant
58+
list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4244) # warning C4244: 'x': conversion from 'int' to 'char', possible loss of data
59+
list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4267) # warning C4267: '...': conversion from 'size_t' to 'unsigned int', possible loss of data
60+
list(APPEND SIMPLECPP_COMPILE_OPTIONS /wd4706) # warning C4706: assignment within conditional expression
5661
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
57-
add_compile_options(-Weverything)
62+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Weverything)
5863

5964
# no need for c++98 compatibility
60-
add_compile_options(-Wno-c++98-compat-pedantic)
65+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-c++98-compat-pedantic)
6166

6267
# these are not really fixable until newer standards
63-
add_compile_options(-Wno-exit-time-destructors)
64-
add_compile_options(-Wno-global-constructors)
65-
add_compile_options(-Wno-weak-vtables)
68+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-exit-time-destructors)
69+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-global-constructors)
70+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-weak-vtables)
6671
add_compile_options_safe(-Wno-unsafe-buffer-usage)
6772
add_compile_options_safe(-Wno-nrvo)
6873

6974
# contradicts -Wcovered-switch-default
70-
add_compile_options(-Wno-switch-default)
75+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-switch-default)
7176
if (MINGW)
72-
add_compile_options(-Wno-reserved-macro-identifier)
73-
add_compile_options(-Wno-unused-macros)
77+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-reserved-macro-identifier)
78+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-unused-macros)
7479
endif()
7580

7681
# these are experimental warnings which might produce false positives
@@ -83,11 +88,11 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
8388
add_compile_options_safe(-Wno-lifetime-safety-cross-tu-constructor-suggestions)
8489

8590
# TODO: fix these?
86-
add_compile_options(-Wno-padded)
87-
add_compile_options(-Wno-sign-conversion)
88-
add_compile_options(-Wno-implicit-int-conversion)
89-
add_compile_options(-Wno-shorten-64-to-32)
90-
add_compile_options(-Wno-shadow-field-in-constructor)
91+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-padded)
92+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-sign-conversion)
93+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-implicit-int-conversion)
94+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-shorten-64-to-32)
95+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-shadow-field-in-constructor)
9196

9297
# we are not interested in these
9398
set_source_files_properties(test.cpp PROPERTIES COMPILE_FLAGS "-Wno-multichar -Wno-four-char-constants")
@@ -96,23 +101,31 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
96101
# TODO: verify this regression still exists in clang-15
97102
if (CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
98103
# work around performance regression - see https://github.com/llvm/llvm-project/issues/53555
99-
add_compile_options(-mllvm -inline-deferral)
104+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -mllvm -inline-deferral)
100105
endif()
101106

102107
# 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
103-
add_compile_options(-gdwarf-4)
108+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -gdwarf-4)
104109
endif()
105110
if (APPLE)
106111
# CMake is sometimes chosing the wrong compiler on macos-* runners
107112
# see https://github.com/actions/runner/issues/4034
108-
add_compile_options(-Wno-poison-system-directories)
113+
list(APPEND SIMPLECPP_COMPILE_OPTIONS -Wno-poison-system-directories)
109114
endif()
110115
endif()
111116

112117
add_library(simplecpp_obj OBJECT simplecpp.cpp)
113118

114119
add_executable(simplecpp $<TARGET_OBJECTS:simplecpp_obj> main.cpp)
115120
add_executable(testrunner $<TARGET_OBJECTS:simplecpp_obj> test.cpp)
121+
foreach(target simplecpp_obj simplecpp testrunner)
122+
if (SIMPLECPP_COMPILE_OPTIONS)
123+
target_compile_options(${target} PRIVATE ${SIMPLECPP_COMPILE_OPTIONS})
124+
endif()
125+
if (SIMPLECPP_COMPILE_DEFINITIONS)
126+
target_compile_definitions(${target} PRIVATE ${SIMPLECPP_COMPILE_DEFINITIONS})
127+
endif()
128+
endforeach()
116129
target_compile_definitions(testrunner
117130
PRIVATE
118131
SIMPLECPP_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}"

0 commit comments

Comments
 (0)