• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#***************************************************************************
2#                                  _   _ ____  _
3#  Project                     ___| | | |  _ \| |
4#                             / __| | | | |_) | |
5#                            | (__| |_| |  _ <| |___
6#                             \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23###########################################################################
24include(CheckCCompilerFlag)
25
26set(_picky "")
27
28if(CURL_WERROR AND
29   ((CMAKE_COMPILER_IS_GNUCC AND
30     NOT DOS AND  # Watt-32 headers use the '#include_next' GCC extension
31     NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0 AND
32     NOT CMAKE_VERSION VERSION_LESS 3.23.0) OR  # to avoid check_symbol_exists() conflicting with GCC -pedantic-errors
33   CMAKE_C_COMPILER_ID MATCHES "Clang"))
34  list(APPEND _picky "-pedantic-errors")
35endif()
36
37if(APPLE AND
38   (CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
39   (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
40  list(APPEND _picky "-Werror=partial-availability")  # clang 3.6  appleclang 6.3
41endif()
42
43if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
44  list(APPEND _picky "-Werror-implicit-function-declaration")  # clang 1.0  gcc 2.95
45endif()
46
47if(PICKY_COMPILER)
48  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
49
50    # https://clang.llvm.org/docs/DiagnosticsReference.html
51    # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
52
53    # _picky_enable = Options we want to enable as-is.
54    # _picky_detect = Options we want to test first and enable if available.
55
56    # Prefer the -Wextra alias with clang.
57    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
58      set(_picky_enable "-Wextra")
59    else()
60      set(_picky_enable "-W")
61    endif()
62
63    list(APPEND _picky_enable
64      -Wall -pedantic
65    )
66
67    # ----------------------------------
68    # Add new options here, if in doubt:
69    # ----------------------------------
70    set(_picky_detect
71    )
72
73    # Assume these options always exist with both clang and gcc.
74    # Require clang 3.0 / gcc 2.95 or later.
75    list(APPEND _picky_enable
76      -Wbad-function-cast                  # clang  2.7  gcc  2.95
77      -Wconversion                         # clang  2.7  gcc  2.95
78      -Wmissing-declarations               # clang  1.0  gcc  2.7
79      -Wmissing-prototypes                 # clang  1.0  gcc  1.0
80      -Wnested-externs                     # clang  1.0  gcc  2.7
81      -Wno-long-long                       # clang  1.0  gcc  2.95
82      -Wno-multichar                       # clang  1.0  gcc  2.95
83      -Wpointer-arith                      # clang  1.0  gcc  1.4
84      -Wshadow                             # clang  1.0  gcc  2.95
85      -Wsign-compare                       # clang  1.0  gcc  2.95
86      -Wundef                              # clang  1.0  gcc  2.95
87      -Wunused                             # clang  1.1  gcc  2.95
88      -Wwrite-strings                      # clang  1.0  gcc  1.4
89    )
90
91    # Always enable with clang, version dependent with gcc
92    set(_picky_common_old
93      -Waddress                            # clang  2.7  gcc  4.3
94      -Wattributes                         # clang  2.7  gcc  4.1
95      -Wcast-align                         # clang  1.0  gcc  4.2
96      -Wdeclaration-after-statement        # clang  1.0  gcc  3.4
97      -Wdiv-by-zero                        # clang  2.7  gcc  4.1
98      -Wempty-body                         # clang  2.7  gcc  4.3
99      -Wendif-labels                       # clang  1.0  gcc  3.3
100      -Wfloat-equal                        # clang  1.0  gcc  2.96 (3.0)
101      -Wformat-security                    # clang  2.7  gcc  4.1
102      -Wignored-qualifiers                 # clang  2.8  gcc  4.3
103      -Wmissing-field-initializers         # clang  2.7  gcc  4.1
104      -Wmissing-noreturn                   # clang  2.7  gcc  4.1
105      -Wno-format-nonliteral               # clang  1.0  gcc  2.96 (3.0)
106      -Wno-sign-conversion                 # clang  2.9  gcc  4.3
107      -Wno-system-headers                  # clang  1.0  gcc  3.0
108    # -Wpadded                             # clang  2.9  gcc  4.1               # Not used: We cannot change public structs
109      -Wold-style-definition               # clang  2.7  gcc  3.4
110      -Wredundant-decls                    # clang  2.7  gcc  4.1
111      -Wstrict-prototypes                  # clang  1.0  gcc  3.3
112    # -Wswitch-enum                        # clang  2.7  gcc  4.1               # Not used: It basically disallows default case
113      -Wtype-limits                        # clang  2.7  gcc  4.3
114      -Wunreachable-code                   # clang  2.7  gcc  4.1
115    # -Wunused-macros                      # clang  2.7  gcc  4.1               # Not practical
116      -Wunused-parameter                   # clang  2.7  gcc  4.1
117      -Wvla                                # clang  2.8  gcc  4.3
118    )
119
120    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
121      list(APPEND _picky_enable
122        ${_picky_common_old}
123        -Wshift-sign-overflow              # clang  2.9
124        -Wshorten-64-to-32                 # clang  1.0
125        -Wformat=2                         # clang  3.0  gcc  4.8
126      )
127      if(NOT MSVC)
128        list(APPEND _picky_enable
129          -Wlanguage-extension-token       # clang  3.0
130        )
131      endif()
132      # Enable based on compiler version
133      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
134         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
135        list(APPEND _picky_enable
136          -Wdouble-promotion               # clang  3.6  gcc  4.6  appleclang  6.3
137          -Wenum-conversion                # clang  3.2  gcc 10.0  appleclang  4.6  g++ 11.0
138          -Wheader-guard                   # clang  3.4            appleclang  5.1
139          -Wpragmas                        # clang  3.5  gcc  4.1  appleclang  6.0
140          -Wsometimes-uninitialized        # clang  3.2            appleclang  4.6
141        # -Wunreachable-code-break         # clang  3.5            appleclang  6.0  # Not used: Silent in "unity" builds
142          -Wunused-const-variable          # clang  3.4  gcc  6.0  appleclang  5.1
143        )
144      endif()
145      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
146         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
147        list(APPEND _picky_enable
148          -Wcomma                          # clang  3.9            appleclang  8.3
149          -Wmissing-variable-declarations  # clang  3.2            appleclang  4.6
150        )
151      endif()
152      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
153         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
154        list(APPEND _picky_enable
155          -Wassign-enum                    # clang  7.0            appleclang 10.3
156          -Wextra-semi-stmt                # clang  7.0            appleclang 10.3
157        )
158      endif()
159      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
160         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
161        list(APPEND _picky_enable
162          -Wimplicit-fallthrough           # clang  4.0  gcc  7.0  appleclang 12.4  # We do silencing for clang 10.0 and above only
163        )
164      endif()
165    else()  # gcc
166      # Enable based on compiler version
167      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
168        list(APPEND _picky_enable
169          ${_picky_common_old}
170          -Wclobbered                      #             gcc  4.3
171          -Wmissing-parameter-type         #             gcc  4.3
172          -Wold-style-declaration          #             gcc  4.3
173          -Wpragmas                        # clang  3.5  gcc  4.1  appleclang  6.0
174          -Wstrict-aliasing=3              #             gcc  4.0
175        )
176      endif()
177      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
178        list(APPEND _picky_enable
179          -Wno-pedantic-ms-format          #             gcc  4.5 (MinGW-only)
180        )
181      endif()
182      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
183        list(APPEND _picky_enable
184          -Wdouble-promotion               # clang  3.6  gcc  4.6  appleclang  6.3
185          -Wformat=2                       # clang  3.0  gcc  4.8
186          -Wtrampolines                    #             gcc  4.6
187        )
188      endif()
189      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
190        list(APPEND _picky_enable
191          -Warray-bounds=2 -ftree-vrp      # clang  3.0  gcc  5.0 (clang default: -Warray-bounds)
192        )
193      endif()
194      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
195        list(APPEND _picky_enable
196          -Wduplicated-cond                #             gcc  6.0
197          -Wnull-dereference               # clang  3.0  gcc  6.0 (clang default)
198            -fdelete-null-pointer-checks
199          -Wshift-negative-value           # clang  3.7  gcc  6.0 (clang default)
200          -Wshift-overflow=2               # clang  3.0  gcc  6.0 (clang default: -Wshift-overflow)
201          -Wunused-const-variable          # clang  3.4  gcc  6.0  appleclang  5.1
202        )
203      endif()
204      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
205        list(APPEND _picky_enable
206          -Walloc-zero                     #             gcc  7.0
207          -Wduplicated-branches            #             gcc  7.0
208          -Wformat-truncation=2            #             gcc  7.0
209          -Wimplicit-fallthrough           # clang  4.0  gcc  7.0
210          -Wrestrict                       #             gcc  7.0
211        )
212      endif()
213      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
214        list(APPEND _picky_enable
215          -Warith-conversion               #             gcc 10.0
216          -Wenum-conversion                # clang  3.2  gcc 10.0  appleclang  4.6  g++ 11.0
217        )
218      endif()
219    endif()
220
221    #
222
223    foreach(_ccopt IN LISTS _picky_enable)
224      list(APPEND _picky "${_ccopt}")
225    endforeach()
226
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
229      string(MAKE_C_IDENTIFIER "OPT${_ccopt}" _optvarname)
230      # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
231      # so test for the positive form instead
232      string(REPLACE "-Wno-" "-W" _ccopt_on "${_ccopt}")
233      check_c_compiler_flag(${_ccopt_on} ${_optvarname})
234      if(${_optvarname})
235        list(APPEND _picky "${_ccopt}")
236      endif()
237    endforeach()
238
239    if(CMAKE_COMPILER_IS_GNUCC)
240      if(CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5)
241        # Avoid false positives
242        list(APPEND _picky "-Wno-shadow")
243        list(APPEND _picky "-Wno-unreachable-code")
244      endif()
245      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.2 AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.6)
246        # GCC <4.6 do not support #pragma to suppress warnings locally. Disable them globally instead.
247        list(APPEND _picky "-Wno-overlength-strings")
248      endif()
249      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.0 AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.7)
250        list(APPEND _picky "-Wno-missing-field-initializers")  # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750
251      endif()
252      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3 AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
253        list(APPEND _picky "-Wno-type-limits")  # Avoid false positives
254      endif()
255    endif()
256  endif()
257endif()
258
259# clang-cl
260if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND MSVC)
261  list(APPEND _picky "-Wno-language-extension-token")  # Allow __int64
262
263  set(_picky_tmp "")
264  foreach(_ccopt IN LISTS _picky)
265    # Prefix -Wall, otherwise clang-cl interprets it as an MSVC option and translates it to -Weverything
266    if(_ccopt MATCHES "^-W" AND NOT _ccopt STREQUAL "-Wall")
267      list(APPEND _picky_tmp ${_ccopt})
268    else()
269      list(APPEND _picky_tmp "-clang:${_ccopt}")
270    endif()
271  endforeach()
272  set(_picky ${_picky_tmp})
273endif()
274
275if(_picky)
276  string(REPLACE ";" " " _picky "${_picky}")
277  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_picky}")
278  message(STATUS "Picky compiler options: ${_picky}")
279endif()
280