Skip to content

Commit 1da1fcc

Browse files
committed
cmake: tidy up picky warning initialization
- use CMake 3.12 syntax when available, in clang-cl branch. Follow-up to e89491e curl#15337 - rename internal variables to underscore-lowercase. Follow-up to d8de480 curl#14571 - update comment. Closes curl#15404
1 parent fff6afb commit 1da1fcc

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

CMake/PickyWarnings.cmake

+48-46
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@
2323
###########################################################################
2424
include(CheckCCompilerFlag)
2525

26-
unset(WPICKY)
26+
unset(_picky)
2727

2828
if(CURL_WERROR AND
2929
((CMAKE_COMPILER_IS_GNUCC AND
3030
NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0 AND
3131
NOT CMAKE_VERSION VERSION_LESS 3.23.0) OR # to avoid check_symbol_exists() conflicting with GCC -pedantic-errors
3232
CMAKE_C_COMPILER_ID MATCHES "Clang"))
33-
list(APPEND WPICKY "-pedantic-errors")
33+
list(APPEND _picky "-pedantic-errors")
3434
endif()
3535

3636
if(APPLE AND
3737
(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
3838
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
39-
list(APPEND WPICKY "-Werror=partial-availability") # clang 3.6 appleclang 6.3
39+
list(APPEND _picky "-Werror=partial-availability") # clang 3.6 appleclang 6.3
4040
endif()
4141

4242
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
43-
list(APPEND WPICKY "-Werror-implicit-function-declaration") # clang 1.0 gcc 2.95
43+
list(APPEND _picky "-Werror-implicit-function-declaration") # clang 1.0 gcc 2.95
4444
endif()
4545

4646
if(PICKY_COMPILER)
@@ -49,29 +49,29 @@ if(PICKY_COMPILER)
4949
# https://clang.llvm.org/docs/DiagnosticsReference.html
5050
# https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
5151

52-
# WPICKY_ENABLE = Options we want to enable as-is.
53-
# WPICKY_DETECT = Options we want to test first and enable if available.
52+
# _picky_enable = Options we want to enable as-is.
53+
# _picky_detect = Options we want to test first and enable if available.
5454

5555
# Prefer the -Wextra alias with clang.
5656
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
57-
set(WPICKY_ENABLE "-Wextra")
57+
set(_picky_enable "-Wextra")
5858
else()
59-
set(WPICKY_ENABLE "-W")
59+
set(_picky_enable "-W")
6060
endif()
6161

62-
list(APPEND WPICKY_ENABLE
62+
list(APPEND _picky_enable
6363
-Wall -pedantic
6464
)
6565

6666
# ----------------------------------
6767
# Add new options here, if in doubt:
6868
# ----------------------------------
69-
set(WPICKY_DETECT
69+
set(_picky_detect
7070
)
7171

7272
# Assume these options always exist with both clang and gcc.
7373
# Require clang 3.0 / gcc 2.95 or later.
74-
list(APPEND WPICKY_ENABLE
74+
list(APPEND _picky_enable
7575
-Wbad-function-cast # clang 2.7 gcc 2.95
7676
-Wconversion # clang 2.7 gcc 2.95
7777
-Winline # clang 1.0 gcc 1.0
@@ -89,7 +89,7 @@ if(PICKY_COMPILER)
8989
)
9090

9191
# Always enable with clang, version dependent with gcc
92-
set(WPICKY_COMMON_OLD
92+
set(_picky_common_old
9393
-Waddress # clang 2.7 gcc 4.3
9494
-Wattributes # clang 2.7 gcc 4.1
9595
-Wcast-align # clang 1.0 gcc 4.2
@@ -118,16 +118,16 @@ if(PICKY_COMPILER)
118118
-Wvla # clang 2.8 gcc 4.3
119119
)
120120

121-
set(WPICKY_COMMON
121+
set(_picky_common
122122
-Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3
123123
-Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0
124124
-Wpragmas # clang 3.5 gcc 4.1 appleclang 6.0
125125
-Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1
126126
)
127127

128128
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
129-
list(APPEND WPICKY_ENABLE
130-
${WPICKY_COMMON_OLD}
129+
list(APPEND _picky_enable
130+
${_picky_common_old}
131131
-Wshift-sign-overflow # clang 2.9
132132
-Wshorten-64-to-32 # clang 1.0
133133
-Wlanguage-extension-token # clang 3.0
@@ -136,41 +136,41 @@ if(PICKY_COMPILER)
136136
# Enable based on compiler version
137137
if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
138138
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
139-
list(APPEND WPICKY_ENABLE
140-
${WPICKY_COMMON}
139+
list(APPEND _picky_enable
140+
${_picky_common}
141141
-Wunreachable-code-break # clang 3.5 appleclang 6.0
142142
-Wheader-guard # clang 3.4 appleclang 5.1
143143
-Wsometimes-uninitialized # clang 3.2 appleclang 4.6
144144
)
145145
endif()
146146
if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
147147
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
148-
list(APPEND WPICKY_ENABLE
148+
list(APPEND _picky_enable
149149
-Wcomma # clang 3.9 appleclang 8.3
150150
-Wmissing-variable-declarations # clang 3.2 appleclang 4.6
151151
)
152152
endif()
153153
if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
154154
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
155-
list(APPEND WPICKY_ENABLE
155+
list(APPEND _picky_enable
156156
-Wassign-enum # clang 7.0 appleclang 10.3
157157
-Wextra-semi-stmt # clang 7.0 appleclang 10.3
158158
)
159159
endif()
160160
if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
161161
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
162-
list(APPEND WPICKY_ENABLE
162+
list(APPEND _picky_enable
163163
-Wimplicit-fallthrough # clang 4.0 gcc 7.0 appleclang 12.4 # We do silencing for clang 10.0 and above only
164164
)
165165
endif()
166166
else() # gcc
167-
list(APPEND WPICKY_DETECT
168-
${WPICKY_COMMON}
167+
list(APPEND _picky_detect
168+
${_picky_common}
169169
)
170170
# Enable based on compiler version
171171
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
172-
list(APPEND WPICKY_ENABLE
173-
${WPICKY_COMMON_OLD}
172+
list(APPEND _picky_enable
173+
${_picky_common_old}
174174
-Wclobbered # gcc 4.3
175175
-Wmissing-parameter-type # gcc 4.3
176176
-Wold-style-declaration # gcc 4.3
@@ -179,22 +179,22 @@ if(PICKY_COMPILER)
179179
)
180180
endif()
181181
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
182-
list(APPEND WPICKY_ENABLE
182+
list(APPEND _picky_enable
183183
-Wno-pedantic-ms-format # gcc 4.5 (MinGW-only)
184184
)
185185
endif()
186186
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
187-
list(APPEND WPICKY_ENABLE
187+
list(APPEND _picky_enable
188188
-Wformat=2 # clang 3.0 gcc 4.8
189189
)
190190
endif()
191191
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
192-
list(APPEND WPICKY_ENABLE
192+
list(APPEND _picky_enable
193193
-Warray-bounds=2 -ftree-vrp # clang 3.0 gcc 5.0 (clang default: -Warray-bounds)
194194
)
195195
endif()
196196
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
197-
list(APPEND WPICKY_ENABLE
197+
list(APPEND _picky_enable
198198
-Wduplicated-cond # gcc 6.0
199199
-Wnull-dereference # clang 3.0 gcc 6.0 (clang default)
200200
-fdelete-null-pointer-checks
@@ -203,7 +203,7 @@ if(PICKY_COMPILER)
203203
)
204204
endif()
205205
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
206-
list(APPEND WPICKY_ENABLE
206+
list(APPEND _picky_enable
207207
-Walloc-zero # gcc 7.0
208208
-Wduplicated-branches # gcc 7.0
209209
-Wformat-truncation=2 # gcc 7.0
@@ -212,45 +212,47 @@ if(PICKY_COMPILER)
212212
)
213213
endif()
214214
if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
215-
list(APPEND WPICKY_ENABLE
215+
list(APPEND _picky_enable
216216
-Warith-conversion # gcc 10.0
217217
)
218218
endif()
219219
endif()
220220

221221
#
222222

223-
foreach(_ccopt IN LISTS WPICKY_ENABLE)
224-
list(APPEND WPICKY "${_ccopt}")
223+
foreach(_ccopt IN LISTS _picky_enable)
224+
list(APPEND _picky "${_ccopt}")
225225
endforeach()
226226

227-
foreach(_ccopt IN LISTS WPICKY_DETECT)
228-
# surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
229-
# test result in.
227+
foreach(_ccopt IN LISTS _picky_detect)
228+
# Use a unique variable name 1. for meaningful log output 2. to have a fresh, undefined variable for each detection
230229
string(MAKE_C_IDENTIFIER "OPT${_ccopt}" _optvarname)
231230
# GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
232231
# so test for the positive form instead
233232
string(REPLACE "-Wno-" "-W" _ccopt_on "${_ccopt}")
234233
check_c_compiler_flag(${_ccopt_on} ${_optvarname})
235234
if(${_optvarname})
236-
list(APPEND WPICKY "${_ccopt}")
235+
list(APPEND _picky "${_ccopt}")
237236
endif()
238237
endforeach()
239238
endif()
240239
endif()
241240

242241
# clang-cl
243242
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
244-
# list(TRANSFORM WPICKY PREPEND "/clang:") # since CMake 3.12
245-
set(_wpicky "")
246-
foreach(_ccopt IN LISTS WPICKY)
247-
list(APPEND _wpicky "/clang:${_ccopt}")
248-
endforeach()
249-
set(WPICKY ${_wpicky})
243+
if(CMAKE_VERSION VERSION_LESS 3.12)
244+
set(_picky_tmp "")
245+
foreach(_ccopt IN LISTS _picky)
246+
list(APPEND _picky_tmp "/clang:${_ccopt}")
247+
endforeach()
248+
set(_picky ${_picky_tmp})
249+
else()
250+
list(TRANSFORM _picky PREPEND "/clang:")
251+
endif()
250252
endif()
251253

252-
if(WPICKY)
253-
string(REPLACE ";" " " WPICKY "${WPICKY}")
254-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
255-
message(STATUS "Picky compiler options:${WPICKY}")
254+
if(_picky)
255+
string(REPLACE ";" " " _picky "${_picky}")
256+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_picky}")
257+
message(STATUS "Picky compiler options:${_picky}")
256258
endif()

0 commit comments

Comments
 (0)