• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# nghttp2
2#
3# Copyright (c) 2023 nghttp2 contributors
4#
5# Permission is hereby granted, free of charge, to any person obtaining
6# a copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sublicense, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
12#
13# The above copyright notice and this permission notice shall be
14# included in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24# C
25
26include(CheckCCompilerFlag)
27
28if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_C_COMPILER_ID MATCHES "Clang")
29
30  # https://clang.llvm.org/docs/DiagnosticsReference.html
31  # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
32
33  # WPICKY_ENABLE = Options we want to enable as-is.
34  # WPICKY_DETECT = Options we want to test first and enable if available.
35
36  # Prefer the -Wextra alias with clang.
37  if(CMAKE_C_COMPILER_ID MATCHES "Clang")
38    set(WPICKY_ENABLE "-Wextra")
39  else()
40    set(WPICKY_ENABLE "-W")
41  endif()
42
43  list(APPEND WPICKY_ENABLE
44    -Wall
45  )
46
47  # ----------------------------------
48  # Add new options here, if in doubt:
49  # ----------------------------------
50  set(WPICKY_DETECT
51  )
52
53  # Assume these options always exist with both clang and gcc.
54  # Require clang 3.0 / gcc 2.95 or later.
55  list(APPEND WPICKY_ENABLE
56    -Wconversion                         # clang  3.0  gcc  2.95
57    -Winline                             # clang  1.0  gcc  1.0
58    -Wmissing-declarations               # clang  1.0  gcc  2.7
59    -Wmissing-prototypes                 # clang  1.0  gcc  1.0
60    -Wnested-externs                     # clang  1.0  gcc  2.7
61    -Wpointer-arith                      # clang  1.0  gcc  1.4
62    -Wshadow                             # clang  1.0  gcc  2.95
63    -Wundef                              # clang  1.0  gcc  2.95
64    -Wwrite-strings                      # clang  1.0  gcc  1.4
65  )
66
67  # Always enable with clang, version dependent with gcc
68  set(WPICKY_COMMON_OLD
69    -Waddress                            # clang  3.0  gcc  4.3
70    -Wattributes                         # clang  3.0  gcc  4.1
71    -Wcast-align                         # clang  1.0  gcc  4.2
72    -Wdeclaration-after-statement        # clang  1.0  gcc  3.4
73    -Wdiv-by-zero                        # clang  3.0  gcc  4.1
74    -Wempty-body                         # clang  3.0  gcc  4.3
75    -Wendif-labels                       # clang  1.0  gcc  3.3
76    -Wfloat-equal                        # clang  1.0  gcc  2.96 (3.0)
77    -Wformat-nonliteral                  # clang  3.0  gcc  4.1
78    -Wformat-security                    # clang  3.0  gcc  4.1
79    -Wmissing-field-initializers         # clang  3.0  gcc  4.1
80    -Wmissing-noreturn                   # clang  3.0  gcc  4.1
81    -Wno-format-nonliteral               # clang  1.0  gcc  2.96 (3.0)        # This is required because we pass format string as "const char*"
82  # -Wpadded                             # clang  3.0  gcc  4.1               # Not used because we cannot change public structs
83    -Wredundant-decls                    # clang  3.0  gcc  4.1
84    -Wsign-conversion                    # clang  3.0  gcc  4.3
85    -Wstrict-prototypes                  # clang  1.0  gcc  3.3
86  # -Wswitch-enum                        # clang  3.0  gcc  4.1               # Not used because this basically disallows default case
87    -Wunreachable-code                   # clang  3.0  gcc  4.1
88    -Wunused-parameter                   # clang  3.0  gcc  4.1
89    -Wvla                                # clang  2.8  gcc  4.3
90  )
91
92  set(WPICKY_COMMON
93    -Wpragmas                            # clang  3.5  gcc  4.1  appleclang  6.0
94  )
95
96  if(CMAKE_C_COMPILER_ID MATCHES "Clang")
97    list(APPEND WPICKY_ENABLE
98      ${WPICKY_COMMON_OLD}
99      -Wshorten-64-to-32                 # clang  1.0
100      -Wlanguage-extension-token         # clang  3.0
101    )
102    # Enable based on compiler version
103    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
104       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
105      list(APPEND WPICKY_ENABLE
106        ${WPICKY_COMMON}
107        -Wunreachable-code-break         # clang  3.5            appleclang  6.0
108        -Wheader-guard                   # clang  3.4            appleclang  5.1
109      )
110    endif()
111    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
112       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
113      list(APPEND WPICKY_ENABLE
114        -Wmissing-variable-declarations  # clang  3.2            appleclang  4.6
115      )
116    endif()
117    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0) OR
118       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.4))
119      list(APPEND WPICKY_ENABLE
120      )
121    endif()
122    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
123       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
124      list(APPEND WPICKY_ENABLE
125      )
126    endif()
127  else()  # gcc
128    list(APPEND WPICKY_DETECT
129      ${WPICKY_COMMON}
130    )
131    # Enable based on compiler version
132    if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
133      list(APPEND WPICKY_ENABLE
134        ${WPICKY_COMMON_OLD}
135        -Wclobbered                      #             gcc  4.3
136      )
137    endif()
138  endif()
139
140  #
141
142  unset(_wpicky)
143
144  foreach(_CCOPT IN LISTS WPICKY_ENABLE)
145    set(_wpicky "${_wpicky} ${_CCOPT}")
146  endforeach()
147
148  foreach(_CCOPT IN LISTS WPICKY_DETECT)
149    # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
150    # test result in.
151    string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
152    # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
153    # so test for the positive form instead
154    string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
155    check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
156    if(${_optvarname})
157      set(_wpicky "${_wpicky} ${_CCOPT}")
158    endif()
159  endforeach()
160
161  set(WARNCFLAGS "${WARNCFLAGS} ${_wpicky}")
162endif()
163