• 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
26unset(WPICKY)
27
28if(CURL_WERROR AND CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
29  set(WPICKY "${WPICKY} -pedantic-errors")
30endif()
31
32if(PICKY_COMPILER)
33  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
34
35    # https://clang.llvm.org/docs/DiagnosticsReference.html
36    # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
37
38    # WPICKY_ENABLE = Options we want to enable as-is.
39    # WPICKY_DETECT = Options we want to test first and enable if available.
40
41    # Prefer the -Wextra alias with clang.
42    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
43      set(WPICKY_ENABLE "-Wextra")
44    else()
45      set(WPICKY_ENABLE "-W")
46    endif()
47
48    list(APPEND WPICKY_ENABLE
49      -Wall -pedantic
50    )
51
52    # ----------------------------------
53    # Add new options here, if in doubt:
54    # ----------------------------------
55    set(WPICKY_DETECT
56    )
57
58    # Assume these options always exist with both clang and gcc.
59    # Require clang 3.0 / gcc 2.95 or later.
60    list(APPEND WPICKY_ENABLE
61      -Wbad-function-cast                  # clang  2.7  gcc  2.95
62      -Wconversion                         # clang  2.7  gcc  2.95
63      -Winline                             # clang  1.0  gcc  1.0
64      -Wmissing-declarations               # clang  1.0  gcc  2.7
65      -Wmissing-prototypes                 # clang  1.0  gcc  1.0
66      -Wnested-externs                     # clang  1.0  gcc  2.7
67      -Wno-long-long                       # clang  1.0  gcc  2.95
68      -Wno-multichar                       # clang  1.0  gcc  2.95
69      -Wpointer-arith                      # clang  1.0  gcc  1.4
70      -Wshadow                             # clang  1.0  gcc  2.95
71      -Wsign-compare                       # clang  1.0  gcc  2.95
72      -Wundef                              # clang  1.0  gcc  2.95
73      -Wunused                             # clang  1.1  gcc  2.95
74      -Wwrite-strings                      # clang  1.0  gcc  1.4
75    )
76
77    # Always enable with clang, version dependent with gcc
78    set(WPICKY_COMMON_OLD
79      -Waddress                            # clang  2.7  gcc  4.3
80      -Wattributes                         # clang  2.7  gcc  4.1
81      -Wcast-align                         # clang  1.0  gcc  4.2
82      -Wdeclaration-after-statement        # clang  1.0  gcc  3.4
83      -Wdiv-by-zero                        # clang  2.7  gcc  4.1
84      -Wempty-body                         # clang  2.7  gcc  4.3
85      -Wendif-labels                       # clang  1.0  gcc  3.3
86      -Wfloat-equal                        # clang  1.0  gcc  2.96 (3.0)
87      -Wformat-security                    # clang  2.7  gcc  4.1
88      -Wignored-qualifiers                 # clang  2.8  gcc  4.3
89      -Wmissing-field-initializers         # clang  2.7  gcc  4.1
90      -Wmissing-noreturn                   # clang  2.7  gcc  4.1
91      -Wno-format-nonliteral               # clang  1.0  gcc  2.96 (3.0)
92      -Wno-system-headers                  # clang  1.0  gcc  3.0
93    # -Wpadded                             # clang  2.9  gcc  4.1               # Not used because we cannot change public structs
94      -Wold-style-definition               # clang  2.7  gcc  3.4
95      -Wredundant-decls                    # clang  2.7  gcc  4.1
96      -Wsign-conversion                    # clang  2.9  gcc  4.3
97        -Wno-error=sign-conversion                                              # FIXME
98      -Wstrict-prototypes                  # clang  1.0  gcc  3.3
99    # -Wswitch-enum                        # clang  2.7  gcc  4.1               # Not used because this basically disallows default case
100      -Wtype-limits                        # clang  2.7  gcc  4.3
101      -Wunreachable-code                   # clang  2.7  gcc  4.1
102    # -Wunused-macros                      # clang  2.7  gcc  4.1               # Not practical
103      -Wunused-parameter                   # clang  2.7  gcc  4.1
104      -Wvla                                # clang  2.8  gcc  4.3
105    )
106
107    set(WPICKY_COMMON
108      -Wdouble-promotion                   # clang  3.6  gcc  4.6  appleclang  6.3
109      -Wenum-conversion                    # clang  3.2  gcc 10.0  appleclang  4.6  g++ 11.0
110      -Wpragmas                            # clang  3.5  gcc  4.1  appleclang  6.0
111      -Wunused-const-variable              # clang  3.4  gcc  6.0  appleclang  5.1
112    )
113
114    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
115      list(APPEND WPICKY_ENABLE
116        ${WPICKY_COMMON_OLD}
117        -Wshift-sign-overflow              # clang  2.9
118        -Wshorten-64-to-32                 # clang  1.0
119        -Wlanguage-extension-token         # clang  3.0
120        -Wformat=2                         # clang  3.0  gcc  4.8
121      )
122      # Enable based on compiler version
123      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
124         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
125        list(APPEND WPICKY_ENABLE
126          ${WPICKY_COMMON}
127          -Wunreachable-code-break         # clang  3.5            appleclang  6.0
128          -Wheader-guard                   # clang  3.4            appleclang  5.1
129          -Wsometimes-uninitialized        # clang  3.2            appleclang  4.6
130        )
131      endif()
132      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
133         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
134        list(APPEND WPICKY_ENABLE
135          -Wcomma                          # clang  3.9            appleclang  8.3
136          -Wmissing-variable-declarations  # clang  3.2            appleclang  4.6
137        )
138      endif()
139      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
140         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
141        list(APPEND WPICKY_ENABLE
142          -Wassign-enum                    # clang  7.0            appleclang 10.3
143          -Wextra-semi-stmt                # clang  7.0            appleclang 10.3
144        )
145      endif()
146      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
147         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
148        list(APPEND WPICKY_ENABLE
149          -Wimplicit-fallthrough           # clang  4.0  gcc  7.0  appleclang 12.4  # we have silencing markup for clang 10.0 and above only
150        )
151      endif()
152    else()  # gcc
153      list(APPEND WPICKY_DETECT
154        ${WPICKY_COMMON}
155      )
156      # Enable based on compiler version
157      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
158        list(APPEND WPICKY_ENABLE
159          ${WPICKY_COMMON_OLD}
160          -Wclobbered                      #             gcc  4.3
161          -Wmissing-parameter-type         #             gcc  4.3
162          -Wold-style-declaration          #             gcc  4.3
163          -Wstrict-aliasing=3              #             gcc  4.0
164          -Wtrampolines                    #             gcc  4.3
165        )
166      endif()
167      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
168        list(APPEND WPICKY_ENABLE
169          -Wno-pedantic-ms-format          #             gcc  4.5 (mingw-only)
170        )
171      endif()
172      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
173        list(APPEND WPICKY_ENABLE
174          -Wformat=2                       # clang  3.0  gcc  4.8
175        )
176      endif()
177      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
178        list(APPEND WPICKY_ENABLE
179          -Warray-bounds=2 -ftree-vrp      # clang  3.0  gcc  5.0 (clang default: -Warray-bounds)
180        )
181      endif()
182      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
183        list(APPEND WPICKY_ENABLE
184          -Wduplicated-cond                #             gcc  6.0
185          -Wnull-dereference               # clang  3.0  gcc  6.0 (clang default)
186            -fdelete-null-pointer-checks
187          -Wshift-negative-value           # clang  3.7  gcc  6.0 (clang default)
188          -Wshift-overflow=2               # clang  3.0  gcc  6.0 (clang default: -Wshift-overflow)
189        )
190      endif()
191      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
192        list(APPEND WPICKY_ENABLE
193          -Walloc-zero                     #             gcc  7.0
194          -Wduplicated-branches            #             gcc  7.0
195          -Wformat-overflow=2              #             gcc  7.0
196          -Wformat-truncation=2            #             gcc  7.0
197          -Wimplicit-fallthrough           # clang  4.0  gcc  7.0
198          -Wrestrict                       #             gcc  7.0
199        )
200      endif()
201      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
202        list(APPEND WPICKY_ENABLE
203          -Warith-conversion               #             gcc 10.0
204        )
205      endif()
206    endif()
207
208    #
209
210    foreach(_CCOPT IN LISTS WPICKY_ENABLE)
211      set(WPICKY "${WPICKY} ${_CCOPT}")
212    endforeach()
213
214    foreach(_CCOPT IN LISTS WPICKY_DETECT)
215      # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
216      # test result in.
217      string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
218      # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
219      # so test for the positive form instead
220      string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
221      check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
222      if(${_optvarname})
223        set(WPICKY "${WPICKY} ${_CCOPT}")
224      endif()
225    endforeach()
226  endif()
227endif()
228
229if(WPICKY)
230  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
231  message(STATUS "Picky compiler options:${WPICKY}")
232endif()
233