• 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-macros                      # clang  3.0  gcc  4.1
89    -Wunused-parameter                   # clang  3.0  gcc  4.1
90    -Wvla                                # clang  2.8  gcc  4.3
91  )
92
93  set(WPICKY_COMMON
94    -Wpragmas                            # clang  3.5  gcc  4.1  appleclang  6.0
95  )
96
97  if(CMAKE_C_COMPILER_ID MATCHES "Clang")
98    list(APPEND WPICKY_ENABLE
99      ${WPICKY_COMMON_OLD}
100      -Wshorten-64-to-32                 # clang  1.0
101      -Wlanguage-extension-token         # clang  3.0
102    )
103    # Enable based on compiler version
104    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
105       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
106      list(APPEND WPICKY_ENABLE
107        ${WPICKY_COMMON}
108        -Wunreachable-code-break         # clang  3.5            appleclang  6.0
109        -Wheader-guard                   # clang  3.4            appleclang  5.1
110      )
111    endif()
112    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
113       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
114      list(APPEND WPICKY_ENABLE
115        -Wmissing-variable-declarations  # clang  3.2            appleclang  4.6
116      )
117    endif()
118    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0) OR
119       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.4))
120      list(APPEND WPICKY_ENABLE
121      )
122    endif()
123    if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
124       (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
125      list(APPEND WPICKY_ENABLE
126      )
127    endif()
128  else()  # gcc
129    list(APPEND WPICKY_DETECT
130      ${WPICKY_COMMON}
131    )
132    # Enable based on compiler version
133    if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
134      list(APPEND WPICKY_ENABLE
135        ${WPICKY_COMMON_OLD}
136        -Wclobbered                      #             gcc  4.3
137      )
138    endif()
139  endif()
140
141  #
142
143  unset(_wpicky)
144
145  foreach(_CCOPT IN LISTS WPICKY_ENABLE)
146    set(_wpicky "${_wpicky} ${_CCOPT}")
147  endforeach()
148
149  foreach(_CCOPT IN LISTS WPICKY_DETECT)
150    # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
151    # test result in.
152    string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
153    # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
154    # so test for the positive form instead
155    string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
156    check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
157    if(${_optvarname})
158      set(_wpicky "${_wpicky} ${_CCOPT}")
159    endif()
160  endforeach()
161
162  set(WARNCFLAGS "${WARNCFLAGS} ${_wpicky}")
163endif()
164