|
| 1 | +#*************************************************************************** |
| 2 | +# _ _ ____ _ |
| 3 | +# Project ___| | | | _ \| | |
| 4 | +# / __| | | | |_) | | |
| 5 | +# | (__| |_| | _ <| |___ |
| 6 | +# \___|\___/|_| \_\_____| |
| 7 | +# |
| 8 | +# Copyright (C) Daniel Stenberg, <[email protected]>, 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 | +########################################################################### |
| 24 | +include(CheckCCompilerFlag) |
| 25 | + |
| 26 | +if(PICKY_COMPILER) |
| 27 | + if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang") |
| 28 | + |
| 29 | + # https://clang.llvm.org/docs/DiagnosticsReference.html |
| 30 | + # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html |
| 31 | + |
| 32 | + # WPICKY_ENABLE = Options we want to enable as-is. |
| 33 | + # WPICKY_DETECT = Options we want to test first and enable if available. |
| 34 | + |
| 35 | + # Prefer the -Wextra alias with clang. |
| 36 | + if(CMAKE_C_COMPILER_ID MATCHES "Clang") |
| 37 | + set(WPICKY_ENABLE "-Wextra") |
| 38 | + else() |
| 39 | + set(WPICKY_ENABLE "-W") |
| 40 | + endif() |
| 41 | + |
| 42 | + list(APPEND WPICKY_ENABLE |
| 43 | + -Wall -pedantic |
| 44 | + ) |
| 45 | + |
| 46 | + # ---------------------------------- |
| 47 | + # Add new options here, if in doubt: |
| 48 | + # ---------------------------------- |
| 49 | + set(WPICKY_DETECT |
| 50 | + ) |
| 51 | + |
| 52 | + # Assume these options always exist with both clang and gcc. |
| 53 | + # Require clang 3.0 / gcc 2.95 or later. |
| 54 | + list(APPEND WPICKY_ENABLE |
| 55 | + -Wbad-function-cast # clang 3.0 gcc 2.95 |
| 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 | + -Wno-long-long # clang 1.0 gcc 2.95 |
| 62 | + -Wno-multichar # clang 1.0 gcc 2.95 |
| 63 | + -Wpointer-arith # clang 1.0 gcc 1.4 |
| 64 | + -Wshadow # clang 1.0 gcc 2.95 |
| 65 | + -Wsign-compare # clang 1.0 gcc 2.95 |
| 66 | + -Wundef # clang 1.0 gcc 2.95 |
| 67 | + -Wunused # clang 1.1 gcc 2.95 |
| 68 | + -Wwrite-strings # clang 1.0 gcc 1.4 |
| 69 | + ) |
| 70 | + |
| 71 | + # Always enable with clang, version dependent with gcc |
| 72 | + set(WPICKY_COMMON_OLD |
| 73 | + -Wcast-align # clang 1.0 gcc 4.2 |
| 74 | + -Wdeclaration-after-statement # clang 1.0 gcc 3.4 |
| 75 | + -Wempty-body # clang 3.0 gcc 4.3 |
| 76 | + -Wendif-labels # clang 1.0 gcc 3.3 |
| 77 | + -Wfloat-equal # clang 1.0 gcc 2.96 (3.0) |
| 78 | + -Wignored-qualifiers # clang 3.0 gcc 4.3 |
| 79 | + -Wno-format-nonliteral # clang 1.0 gcc 2.96 (3.0) |
| 80 | + -Wno-sign-conversion # clang 3.0 gcc 4.3 |
| 81 | + -Wno-system-headers # clang 1.0 gcc 3.0 |
| 82 | + -Wstrict-prototypes # clang 1.0 gcc 3.3 |
| 83 | + -Wtype-limits # clang 3.0 gcc 4.3 |
| 84 | + -Wvla # clang 2.8 gcc 4.3 |
| 85 | + ) |
| 86 | + |
| 87 | + set(WPICKY_COMMON |
| 88 | + -Wdouble-promotion # clang 3.6 gcc 4.6 appleclang 6.3 |
| 89 | + -Wenum-conversion # clang 3.2 gcc 10.0 appleclang 4.6 g++ 11.0 |
| 90 | + -Wunused-const-variable # clang 3.4 gcc 6.0 appleclang 5.1 |
| 91 | + ) |
| 92 | + |
| 93 | + if(CMAKE_C_COMPILER_ID MATCHES "Clang") |
| 94 | + list(APPEND WPICKY_ENABLE |
| 95 | + ${WPICKY_COMMON_OLD} |
| 96 | + -Wshift-sign-overflow # clang 2.9 |
| 97 | + -Wshorten-64-to-32 # clang 1.0 |
| 98 | + ) |
| 99 | + # Enable based on compiler version |
| 100 | + if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR |
| 101 | + (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3)) |
| 102 | + list(APPEND WPICKY_ENABLE |
| 103 | + ${WPICKY_COMMON} |
| 104 | + ) |
| 105 | + endif() |
| 106 | + if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR |
| 107 | + (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3)) |
| 108 | + list(APPEND WPICKY_ENABLE |
| 109 | + -Wcomma # clang 3.9 appleclang 8.3 |
| 110 | + -Wmissing-variable-declarations # clang 3.2 appleclang 4.6 |
| 111 | + ) |
| 112 | + endif() |
| 113 | + if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR |
| 114 | + (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3)) |
| 115 | + list(APPEND WPICKY_ENABLE |
| 116 | + -Wassign-enum # clang 7.0 appleclang 10.3 |
| 117 | + -Wextra-semi-stmt # clang 7.0 appleclang 10.3 |
| 118 | + ) |
| 119 | + endif() |
| 120 | + else() # gcc |
| 121 | + list(APPEND WPICKY_DETECT |
| 122 | + ${WPICKY_COMMON} |
| 123 | + ) |
| 124 | + # Enable based on compiler version |
| 125 | + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3) |
| 126 | + list(APPEND WPICKY_ENABLE |
| 127 | + ${WPICKY_COMMON_OLD} |
| 128 | + -Wmissing-parameter-type # gcc 4.3 |
| 129 | + -Wold-style-declaration # gcc 4.3 |
| 130 | + -Wstrict-aliasing=3 # gcc 4.0 |
| 131 | + ) |
| 132 | + endif() |
| 133 | + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW) |
| 134 | + list(APPEND WPICKY_ENABLE |
| 135 | + -Wno-pedantic-ms-format # gcc 4.5 (mingw-only) |
| 136 | + ) |
| 137 | + endif() |
| 138 | + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8) |
| 139 | + list(APPEND WPICKY_ENABLE |
| 140 | + -Wformat=2 # clang 3.0 gcc 4.8 (clang part-default, enabling it fully causes -Wformat-nonliteral warnings) |
| 141 | + ) |
| 142 | + endif() |
| 143 | + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0) |
| 144 | + list(APPEND WPICKY_ENABLE |
| 145 | + -Warray-bounds=2 -ftree-vrp # clang 3.0 gcc 5.0 (clang default: -Warray-bounds) |
| 146 | + ) |
| 147 | + endif() |
| 148 | + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0) |
| 149 | + list(APPEND WPICKY_ENABLE |
| 150 | + -Wduplicated-cond # gcc 6.0 |
| 151 | + -Wnull-dereference # clang 3.0 gcc 6.0 (clang default) |
| 152 | + -fdelete-null-pointer-checks |
| 153 | + -Wshift-negative-value # clang 3.7 gcc 6.0 (clang default) |
| 154 | + -Wshift-overflow=2 # clang 3.0 gcc 6.0 (clang default: -Wshift-overflow) |
| 155 | + ) |
| 156 | + endif() |
| 157 | + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) |
| 158 | + list(APPEND WPICKY_ENABLE |
| 159 | + -Walloc-zero # gcc 7.0 |
| 160 | + -Wduplicated-branches # gcc 7.0 |
| 161 | + -Wformat-overflow=2 # gcc 7.0 |
| 162 | + -Wformat-truncation=1 # gcc 7.0 |
| 163 | + -Wrestrict # gcc 7.0 |
| 164 | + ) |
| 165 | + endif() |
| 166 | + if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) |
| 167 | + list(APPEND WPICKY_ENABLE |
| 168 | + -Warith-conversion # gcc 10.0 |
| 169 | + ) |
| 170 | + endif() |
| 171 | + endif() |
| 172 | + |
| 173 | + # |
| 174 | + |
| 175 | + unset(WPICKY) |
| 176 | + |
| 177 | + foreach(_CCOPT ${WPICKY_ENABLE}) |
| 178 | + set(WPICKY "${WPICKY} ${_CCOPT}") |
| 179 | + endforeach() |
| 180 | + |
| 181 | + foreach(_CCOPT ${WPICKY_DETECT}) |
| 182 | + # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new |
| 183 | + # test result in. |
| 184 | + string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname) |
| 185 | + # GCC only warns about unknown -Wno- options if there are also other diagnostic messages, |
| 186 | + # so test for the positive form instead |
| 187 | + string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}") |
| 188 | + check_c_compiler_flag(${_CCOPT_ON} ${_optvarname}) |
| 189 | + if(${_optvarname}) |
| 190 | + set(WPICKY "${WPICKY} ${_CCOPT}") |
| 191 | + endif() |
| 192 | + endforeach() |
| 193 | + |
| 194 | + message(STATUS "Picky compiler options:${WPICKY}") |
| 195 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}") |
| 196 | + endif() |
| 197 | +endif() |
0 commit comments