113498266Sopenharmony_ci#***************************************************************************
213498266Sopenharmony_ci#                                  _   _ ____  _
313498266Sopenharmony_ci#  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci#                             / __| | | | |_) | |
513498266Sopenharmony_ci#                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci#                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci#
813498266Sopenharmony_ci# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci#
1013498266Sopenharmony_ci# This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci# you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci# are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci#
1413498266Sopenharmony_ci# You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci# copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci# furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci#
1813498266Sopenharmony_ci# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci# KIND, either express or implied.
2013498266Sopenharmony_ci#
2113498266Sopenharmony_ci# SPDX-License-Identifier: curl
2213498266Sopenharmony_ci#
2313498266Sopenharmony_ci###########################################################################
2413498266Sopenharmony_ciinclude(CheckCCompilerFlag)
2513498266Sopenharmony_ci
2613498266Sopenharmony_ciunset(WPICKY)
2713498266Sopenharmony_ci
2813498266Sopenharmony_ciif(CURL_WERROR AND CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
2913498266Sopenharmony_ci  set(WPICKY "${WPICKY} -pedantic-errors")
3013498266Sopenharmony_ciendif()
3113498266Sopenharmony_ci
3213498266Sopenharmony_ciif(PICKY_COMPILER)
3313498266Sopenharmony_ci  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci    # https://clang.llvm.org/docs/DiagnosticsReference.html
3613498266Sopenharmony_ci    # https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
3713498266Sopenharmony_ci
3813498266Sopenharmony_ci    # WPICKY_ENABLE = Options we want to enable as-is.
3913498266Sopenharmony_ci    # WPICKY_DETECT = Options we want to test first and enable if available.
4013498266Sopenharmony_ci
4113498266Sopenharmony_ci    # Prefer the -Wextra alias with clang.
4213498266Sopenharmony_ci    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
4313498266Sopenharmony_ci      set(WPICKY_ENABLE "-Wextra")
4413498266Sopenharmony_ci    else()
4513498266Sopenharmony_ci      set(WPICKY_ENABLE "-W")
4613498266Sopenharmony_ci    endif()
4713498266Sopenharmony_ci
4813498266Sopenharmony_ci    list(APPEND WPICKY_ENABLE
4913498266Sopenharmony_ci      -Wall -pedantic
5013498266Sopenharmony_ci    )
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci    # ----------------------------------
5313498266Sopenharmony_ci    # Add new options here, if in doubt:
5413498266Sopenharmony_ci    # ----------------------------------
5513498266Sopenharmony_ci    set(WPICKY_DETECT
5613498266Sopenharmony_ci    )
5713498266Sopenharmony_ci
5813498266Sopenharmony_ci    # Assume these options always exist with both clang and gcc.
5913498266Sopenharmony_ci    # Require clang 3.0 / gcc 2.95 or later.
6013498266Sopenharmony_ci    list(APPEND WPICKY_ENABLE
6113498266Sopenharmony_ci      -Wbad-function-cast                  # clang  2.7  gcc  2.95
6213498266Sopenharmony_ci      -Wconversion                         # clang  2.7  gcc  2.95
6313498266Sopenharmony_ci      -Winline                             # clang  1.0  gcc  1.0
6413498266Sopenharmony_ci      -Wmissing-declarations               # clang  1.0  gcc  2.7
6513498266Sopenharmony_ci      -Wmissing-prototypes                 # clang  1.0  gcc  1.0
6613498266Sopenharmony_ci      -Wnested-externs                     # clang  1.0  gcc  2.7
6713498266Sopenharmony_ci      -Wno-long-long                       # clang  1.0  gcc  2.95
6813498266Sopenharmony_ci      -Wno-multichar                       # clang  1.0  gcc  2.95
6913498266Sopenharmony_ci      -Wpointer-arith                      # clang  1.0  gcc  1.4
7013498266Sopenharmony_ci      -Wshadow                             # clang  1.0  gcc  2.95
7113498266Sopenharmony_ci      -Wsign-compare                       # clang  1.0  gcc  2.95
7213498266Sopenharmony_ci      -Wundef                              # clang  1.0  gcc  2.95
7313498266Sopenharmony_ci      -Wunused                             # clang  1.1  gcc  2.95
7413498266Sopenharmony_ci      -Wwrite-strings                      # clang  1.0  gcc  1.4
7513498266Sopenharmony_ci    )
7613498266Sopenharmony_ci
7713498266Sopenharmony_ci    # Always enable with clang, version dependent with gcc
7813498266Sopenharmony_ci    set(WPICKY_COMMON_OLD
7913498266Sopenharmony_ci      -Waddress                            # clang  2.7  gcc  4.3
8013498266Sopenharmony_ci      -Wattributes                         # clang  2.7  gcc  4.1
8113498266Sopenharmony_ci      -Wcast-align                         # clang  1.0  gcc  4.2
8213498266Sopenharmony_ci      -Wdeclaration-after-statement        # clang  1.0  gcc  3.4
8313498266Sopenharmony_ci      -Wdiv-by-zero                        # clang  2.7  gcc  4.1
8413498266Sopenharmony_ci      -Wempty-body                         # clang  2.7  gcc  4.3
8513498266Sopenharmony_ci      -Wendif-labels                       # clang  1.0  gcc  3.3
8613498266Sopenharmony_ci      -Wfloat-equal                        # clang  1.0  gcc  2.96 (3.0)
8713498266Sopenharmony_ci      -Wformat-security                    # clang  2.7  gcc  4.1
8813498266Sopenharmony_ci      -Wignored-qualifiers                 # clang  2.8  gcc  4.3
8913498266Sopenharmony_ci      -Wmissing-field-initializers         # clang  2.7  gcc  4.1
9013498266Sopenharmony_ci      -Wmissing-noreturn                   # clang  2.7  gcc  4.1
9113498266Sopenharmony_ci      -Wno-format-nonliteral               # clang  1.0  gcc  2.96 (3.0)
9213498266Sopenharmony_ci      -Wno-system-headers                  # clang  1.0  gcc  3.0
9313498266Sopenharmony_ci    # -Wpadded                             # clang  2.9  gcc  4.1               # Not used because we cannot change public structs
9413498266Sopenharmony_ci      -Wold-style-definition               # clang  2.7  gcc  3.4
9513498266Sopenharmony_ci      -Wredundant-decls                    # clang  2.7  gcc  4.1
9613498266Sopenharmony_ci      -Wsign-conversion                    # clang  2.9  gcc  4.3
9713498266Sopenharmony_ci        -Wno-error=sign-conversion                                              # FIXME
9813498266Sopenharmony_ci      -Wstrict-prototypes                  # clang  1.0  gcc  3.3
9913498266Sopenharmony_ci    # -Wswitch-enum                        # clang  2.7  gcc  4.1               # Not used because this basically disallows default case
10013498266Sopenharmony_ci      -Wtype-limits                        # clang  2.7  gcc  4.3
10113498266Sopenharmony_ci      -Wunreachable-code                   # clang  2.7  gcc  4.1
10213498266Sopenharmony_ci    # -Wunused-macros                      # clang  2.7  gcc  4.1               # Not practical
10313498266Sopenharmony_ci      -Wunused-parameter                   # clang  2.7  gcc  4.1
10413498266Sopenharmony_ci      -Wvla                                # clang  2.8  gcc  4.3
10513498266Sopenharmony_ci    )
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci    set(WPICKY_COMMON
10813498266Sopenharmony_ci      -Wdouble-promotion                   # clang  3.6  gcc  4.6  appleclang  6.3
10913498266Sopenharmony_ci      -Wenum-conversion                    # clang  3.2  gcc 10.0  appleclang  4.6  g++ 11.0
11013498266Sopenharmony_ci      -Wpragmas                            # clang  3.5  gcc  4.1  appleclang  6.0
11113498266Sopenharmony_ci      -Wunused-const-variable              # clang  3.4  gcc  6.0  appleclang  5.1
11213498266Sopenharmony_ci    )
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
11513498266Sopenharmony_ci      list(APPEND WPICKY_ENABLE
11613498266Sopenharmony_ci        ${WPICKY_COMMON_OLD}
11713498266Sopenharmony_ci        -Wshift-sign-overflow              # clang  2.9
11813498266Sopenharmony_ci        -Wshorten-64-to-32                 # clang  1.0
11913498266Sopenharmony_ci        -Wlanguage-extension-token         # clang  3.0
12013498266Sopenharmony_ci        -Wformat=2                         # clang  3.0  gcc  4.8
12113498266Sopenharmony_ci      )
12213498266Sopenharmony_ci      # Enable based on compiler version
12313498266Sopenharmony_ci      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.6) OR
12413498266Sopenharmony_ci         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.3))
12513498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
12613498266Sopenharmony_ci          ${WPICKY_COMMON}
12713498266Sopenharmony_ci          -Wunreachable-code-break         # clang  3.5            appleclang  6.0
12813498266Sopenharmony_ci          -Wheader-guard                   # clang  3.4            appleclang  5.1
12913498266Sopenharmony_ci          -Wsometimes-uninitialized        # clang  3.2            appleclang  4.6
13013498266Sopenharmony_ci        )
13113498266Sopenharmony_ci      endif()
13213498266Sopenharmony_ci      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) OR
13313498266Sopenharmony_ci         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.3))
13413498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
13513498266Sopenharmony_ci          -Wcomma                          # clang  3.9            appleclang  8.3
13613498266Sopenharmony_ci          -Wmissing-variable-declarations  # clang  3.2            appleclang  4.6
13713498266Sopenharmony_ci        )
13813498266Sopenharmony_ci      endif()
13913498266Sopenharmony_ci      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0) OR
14013498266Sopenharmony_ci         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.3))
14113498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
14213498266Sopenharmony_ci          -Wassign-enum                    # clang  7.0            appleclang 10.3
14313498266Sopenharmony_ci          -Wextra-semi-stmt                # clang  7.0            appleclang 10.3
14413498266Sopenharmony_ci        )
14513498266Sopenharmony_ci      endif()
14613498266Sopenharmony_ci      if((CMAKE_C_COMPILER_ID STREQUAL "Clang"      AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0) OR
14713498266Sopenharmony_ci         (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 12.4))
14813498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
14913498266Sopenharmony_ci          -Wimplicit-fallthrough           # clang  4.0  gcc  7.0  appleclang 12.4  # we have silencing markup for clang 10.0 and above only
15013498266Sopenharmony_ci        )
15113498266Sopenharmony_ci      endif()
15213498266Sopenharmony_ci    else()  # gcc
15313498266Sopenharmony_ci      list(APPEND WPICKY_DETECT
15413498266Sopenharmony_ci        ${WPICKY_COMMON}
15513498266Sopenharmony_ci      )
15613498266Sopenharmony_ci      # Enable based on compiler version
15713498266Sopenharmony_ci      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.3)
15813498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
15913498266Sopenharmony_ci          ${WPICKY_COMMON_OLD}
16013498266Sopenharmony_ci          -Wclobbered                      #             gcc  4.3
16113498266Sopenharmony_ci          -Wmissing-parameter-type         #             gcc  4.3
16213498266Sopenharmony_ci          -Wold-style-declaration          #             gcc  4.3
16313498266Sopenharmony_ci          -Wstrict-aliasing=3              #             gcc  4.0
16413498266Sopenharmony_ci          -Wtrampolines                    #             gcc  4.3
16513498266Sopenharmony_ci        )
16613498266Sopenharmony_ci      endif()
16713498266Sopenharmony_ci      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.5 AND MINGW)
16813498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
16913498266Sopenharmony_ci          -Wno-pedantic-ms-format          #             gcc  4.5 (mingw-only)
17013498266Sopenharmony_ci        )
17113498266Sopenharmony_ci      endif()
17213498266Sopenharmony_ci      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.8)
17313498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
17413498266Sopenharmony_ci          -Wformat=2                       # clang  3.0  gcc  4.8
17513498266Sopenharmony_ci        )
17613498266Sopenharmony_ci      endif()
17713498266Sopenharmony_ci      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0)
17813498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
17913498266Sopenharmony_ci          -Warray-bounds=2 -ftree-vrp      # clang  3.0  gcc  5.0 (clang default: -Warray-bounds)
18013498266Sopenharmony_ci        )
18113498266Sopenharmony_ci      endif()
18213498266Sopenharmony_ci      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 6.0)
18313498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
18413498266Sopenharmony_ci          -Wduplicated-cond                #             gcc  6.0
18513498266Sopenharmony_ci          -Wnull-dereference               # clang  3.0  gcc  6.0 (clang default)
18613498266Sopenharmony_ci            -fdelete-null-pointer-checks
18713498266Sopenharmony_ci          -Wshift-negative-value           # clang  3.7  gcc  6.0 (clang default)
18813498266Sopenharmony_ci          -Wshift-overflow=2               # clang  3.0  gcc  6.0 (clang default: -Wshift-overflow)
18913498266Sopenharmony_ci        )
19013498266Sopenharmony_ci      endif()
19113498266Sopenharmony_ci      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 7.0)
19213498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
19313498266Sopenharmony_ci          -Walloc-zero                     #             gcc  7.0
19413498266Sopenharmony_ci          -Wduplicated-branches            #             gcc  7.0
19513498266Sopenharmony_ci          -Wformat-overflow=2              #             gcc  7.0
19613498266Sopenharmony_ci          -Wformat-truncation=2            #             gcc  7.0
19713498266Sopenharmony_ci          -Wimplicit-fallthrough           # clang  4.0  gcc  7.0
19813498266Sopenharmony_ci          -Wrestrict                       #             gcc  7.0
19913498266Sopenharmony_ci        )
20013498266Sopenharmony_ci      endif()
20113498266Sopenharmony_ci      if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 10.0)
20213498266Sopenharmony_ci        list(APPEND WPICKY_ENABLE
20313498266Sopenharmony_ci          -Warith-conversion               #             gcc 10.0
20413498266Sopenharmony_ci        )
20513498266Sopenharmony_ci      endif()
20613498266Sopenharmony_ci    endif()
20713498266Sopenharmony_ci
20813498266Sopenharmony_ci    #
20913498266Sopenharmony_ci
21013498266Sopenharmony_ci    foreach(_CCOPT IN LISTS WPICKY_ENABLE)
21113498266Sopenharmony_ci      set(WPICKY "${WPICKY} ${_CCOPT}")
21213498266Sopenharmony_ci    endforeach()
21313498266Sopenharmony_ci
21413498266Sopenharmony_ci    foreach(_CCOPT IN LISTS WPICKY_DETECT)
21513498266Sopenharmony_ci      # surprisingly, CHECK_C_COMPILER_FLAG needs a new variable to store each new
21613498266Sopenharmony_ci      # test result in.
21713498266Sopenharmony_ci      string(MAKE_C_IDENTIFIER "OPT${_CCOPT}" _optvarname)
21813498266Sopenharmony_ci      # GCC only warns about unknown -Wno- options if there are also other diagnostic messages,
21913498266Sopenharmony_ci      # so test for the positive form instead
22013498266Sopenharmony_ci      string(REPLACE "-Wno-" "-W" _CCOPT_ON "${_CCOPT}")
22113498266Sopenharmony_ci      check_c_compiler_flag(${_CCOPT_ON} ${_optvarname})
22213498266Sopenharmony_ci      if(${_optvarname})
22313498266Sopenharmony_ci        set(WPICKY "${WPICKY} ${_CCOPT}")
22413498266Sopenharmony_ci      endif()
22513498266Sopenharmony_ci    endforeach()
22613498266Sopenharmony_ci  endif()
22713498266Sopenharmony_ciendif()
22813498266Sopenharmony_ci
22913498266Sopenharmony_ciif(WPICKY)
23013498266Sopenharmony_ci  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WPICKY}")
23113498266Sopenharmony_ci  message(STATUS "Picky compiler options:${WPICKY}")
23213498266Sopenharmony_ciendif()
233