You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
11 KiB

6 years ago
2 years ago
  1. #
  2. # This program source code file is part of KICAD, a free EDA CAD application.
  3. #
  4. # Copyright (C) 2007-2020 Kicad Developers, see AUTHORS.txt for contributors.
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, you may find one here:
  18. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. # or you may search the http://www.gnu.org website for the version 2 license,
  20. # or you may write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. #
  23. # Configure warnings for Clang and GCC
  24. if( CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
  25. # The SWIG-generated files tend to throw a lot of warnings, so
  26. # we do not add the warnings directly to the flags here but instead
  27. # keep track of them and add them to the flags later in a controlled manner
  28. # (that way we can not put any warnings on the SWIG-generated files)
  29. set( COMPILER_SUPPORTS_WARNINGS TRUE )
  30. # Establish -Wall early, so specialized relaxations of this may come
  31. # subsequently on the command line, such as in pcbnew/github/CMakeLists.txt
  32. set( WARN_FLAGS_C "-Wall" )
  33. set( WARN_FLAGS_CXX "-Wall" )
  34. # Warn about missing override specifiers
  35. CHECK_CXX_COMPILER_FLAG( "-Wsuggest-override" COMPILER_SUPPORTS_WSUGGEST_OVERRIDE )
  36. if( COMPILER_SUPPORTS_WSUGGEST_OVERRIDE )
  37. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wsuggest-override" )
  38. message( STATUS "Enabling warning -Wsuggest-override" )
  39. endif()
  40. # This is Clang's version of -Wsuggest-override
  41. CHECK_CXX_COMPILER_FLAG( "-Winconsistent-missing-override" COMPILER_SUPPORTS_WINCONSISTENT_MISSING_OVERRIDE )
  42. if( COMPILER_SUPPORTS_WINCONSISTENT_MISSING_OVERRIDE )
  43. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Winconsistent-missing-override" )
  44. # Also use this to guard warning removal of the warning inside the code
  45. set( HAVE_WINCONSISTENT_MISSING_OVERRIDE true )
  46. message( STATUS "Enabling warning -Winconsistent-missing-override" )
  47. endif()
  48. # Warn on duplicated branches
  49. CHECK_CXX_COMPILER_FLAG( "-Wduplicated-branches" COMPILER_SUPPORTS_WDUPLICATED_BRANCHES )
  50. if( COMPILER_SUPPORTS_WDUPLICATED_BRANCHES )
  51. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wduplicated-branches" )
  52. message( STATUS "Enabling warning -Wduplicated-branches" )
  53. endif()
  54. # Warn on duplicated conditions
  55. CHECK_CXX_COMPILER_FLAG( "-Wduplicated-cond" COMPILER_SUPPORTS_WDUPLICATED_COND )
  56. if( COMPILER_SUPPORTS_WDUPLICATED_COND )
  57. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wduplicated-cond" )
  58. message( STATUS "Enabling warning -Wduplicated-cond" )
  59. endif()
  60. # Error on variable length arrays (gcc extension)
  61. CHECK_CXX_COMPILER_FLAG( "-Wvla" COMPILER_SUPPORTS_WVLA )
  62. if( COMPILER_SUPPORTS_WVLA )
  63. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Werror=vla" )
  64. message( STATUS "Enabling error for -Wvla" )
  65. endif()
  66. # Warn on implicit switch fallthrough
  67. CHECK_CXX_COMPILER_FLAG( "-Wimplicit-fallthrough" COMPILER_SUPPORTS_WIMPLICIT_FALLTHROUGH )
  68. if( COMPILER_SUPPORTS_WIMPLICIT_FALLTHROUGH )
  69. if( CMAKE_COMPILER_IS_GNUCXX )
  70. # GCC level 5 does not allow comments - mirrors the Clang warning
  71. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wimplicit-fallthrough=5" )
  72. else()
  73. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wimplicit-fallthrough" )
  74. endif()
  75. message( STATUS "Enabling warning -Wimplicit-fallthrough" )
  76. endif()
  77. # Error if there is a problem with function returns
  78. CHECK_CXX_COMPILER_FLAG( "-Wreturn-type" COMPILER_SUPPORTS_WRETURN_TYPE )
  79. if( COMPILER_SUPPORTS_WRETURN_TYPE )
  80. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Werror=return-type" )
  81. message( STATUS "Enabling error for -Wreturn-type" )
  82. endif()
  83. # Warn about shadowed variables
  84. CHECK_CXX_COMPILER_FLAG( "-Wshadow" COMPILER_SUPPORTS_WSHADOW )
  85. if( COMPILER_SUPPORTS_WSHADOW )
  86. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wshadow" )
  87. message( STATUS "Enabling warning -Wshadow" )
  88. endif()
  89. # Add additional warning flags to avoid signed/unsigned comparison
  90. CHECK_CXX_COMPILER_FLAG( "-Wsign-compare" COMPILER_SUPPORTS_WSIGN )
  91. if( COMPILER_SUPPORTS_WSIGN )
  92. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wsign-compare" )
  93. message( STATUS "Enabling warning -Wsign-compare" )
  94. endif()
  95. # Warn about missing initializers in construction
  96. CHECK_CXX_COMPILER_FLAG( "-Wmissing-field-initializers" COMPILER_SUPPORTS_WMISSING_INIT )
  97. if( COMPILER_SUPPORTS_WMISSING_INIT )
  98. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wmissing-field-initializers" )
  99. message( STATUS "Enabling warning -Wmissing-field-initializers" )
  100. endif()
  101. # Warn about empty if/for/while bodies
  102. CHECK_CXX_COMPILER_FLAG( "-Wempty-body" COMPILER_SUPPORTS_WEMPTY_BODY )
  103. if( COMPILER_SUPPORTS_WEMPTY_BODY )
  104. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wempty-body" )
  105. message( STATUS "Enabling warning -Wempty-body" )
  106. endif()
  107. # Warn about out of order intialization
  108. CHECK_CXX_COMPILER_FLAG( "-Wreorder" COMPILER_SUPPORTS_WREORDER )
  109. if( COMPILER_SUPPORTS_WREORDER )
  110. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wreorder" )
  111. message( STATUS "Enabling warning -Wreorder" )
  112. endif()
  113. # Warn about mismatched class/struct declarations
  114. CHECK_CXX_COMPILER_FLAG( "-Wmismatched-tags" COMPILER_SUPPORTS_WMISMATCHED_TAGS )
  115. if( COMPILER_SUPPORTS_WMISMATCHED_TAGS )
  116. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wmismatched-tags" )
  117. message( STATUS "Enabling warning -Wmismatched-tags" )
  118. endif()
  119. # See if the compiler will throw warnings on these conversions
  120. CHECK_CXX_COMPILER_FLAG( "-Wimplicit-int-float-conversion" COMPILER_SUPPORTS_WIMPLICIT_FLOAT_CONVERSION )
  121. if( COMPILER_SUPPORTS_WIMPLICIT_FLOAT_CONVERSION )
  122. # This one is different, it is used to guard warning removal for this inside the code
  123. set( HAVE_WIMPLICIT_FLOAT_CONVERSION true )
  124. endif()
  125. # Suppress GCC warnings about unknown/unused attributes (e.g. cdecl, [[maybe_unused, etc)
  126. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  127. set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-attributes" )
  128. endif()
  129. # Avoid ABI warnings, specifically one about an ABI change on ppc64el from gcc5 to gcc 6.
  130. CHECK_CXX_COMPILER_FLAG( "-Wpsabi" COMPILER_SUPPORTS_WPSABI )
  131. if( COMPILER_SUPPORTS_WPSABI )
  132. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} -Wno-psabi" )
  133. message( STATUS "Disabling warning -Wpsabi" )
  134. endif()
  135. # Append any additional warning flags so the end of the warning string
  136. if( KICAD_ADDITIONAL_WARN_FLAGS )
  137. set( WARN_FLAGS_CXX "${WARN_FLAGS_CXX} ${KICAD_ADDITIONAL_WARN_FLAGS}" )
  138. message( STATUS "Adding additional warning flags: ${KICAD_ADDITIONAL_WARN_FLAGS}")
  139. endif()
  140. endif()
  141. # MSVC specific warnings
  142. if( MSVC )
  143. set( COMPILER_SUPPORTS_WARNINGS TRUE )
  144. # Establish /Wall early and selectively disable some very common warnings in kicad code
  145. # or warnings that really shouldn't be warnings. Also some warnings like implicit fallthrough
  146. # in case statements happen in msvc std lib and despite /external:env:INCLUDE leak
  147. # into build log generating thousands of noise entries.
  148. # Unlike gcc /Wall actually enables all warnings on msvc.
  149. # Warnings for C are not enabled since C files are mostly generated
  150. # set( WARN_FLAGS_C "/external:W0 /external:env:INCLUDE /external:I${CMAKE_SOURCE_DIR}/thirdparty /Wall" )
  151. set( WARN_FLAGS_CXX "/external:W0 /external:env:INCLUDE /external:I${CMAKE_SOURCE_DIR}/thirdparty /Wall" )
  152. # disable 'type' : class 'type1' needs to have dll-interface to be used by clients of class 'type2'
  153. string( APPEND WARN_FLAGS_CXX " /wd4251" )
  154. # disable "function not inlined"
  155. string( APPEND WARN_FLAGS_CXX " /wd4710" )
  156. # disable "function selected for inline expansion"
  157. string( APPEND WARN_FLAGS_CXX " /wd4711" )
  158. # disable "bytes padding added"
  159. string( APPEND WARN_FLAGS_CXX " /wd4820" )
  160. # disable "unreferenced formal parameter"
  161. string( APPEND WARN_FLAGS_CXX " /wd4100" )
  162. # disable default/copy/move constructor/assignment implicitly defined as deleted
  163. string( APPEND WARN_FLAGS_CXX " /wd4623 /wd4625 /wd5026 /wd4626 /wd5027" )
  164. # disable "compiler will insert Spectre mitigation for..."
  165. string( APPEND WARN_FLAGS_CXX " /wd5045" )
  166. # disable "enumerator in switch for enum is not explicitly handled"
  167. string( APPEND WARN_FLAGS_CXX " /wd4061" )
  168. # disable "conversion from 'type_1' to 'type_2', signed/unsigned mismatch"
  169. string( APPEND WARN_FLAGS_CXX " /wd4245 /wd4365" )
  170. # disable "conversion from 'type1' to 'type2', possible loss of data"
  171. string( APPEND WARN_FLAGS_CXX " /wd4242 /wd5219" )
  172. # disable "member function does not override any base class virtual member function"
  173. string( APPEND WARN_FLAGS_CXX " /wd4263" )
  174. # disable "no override available for virtual member function from base 'class'; function is hidden"
  175. string( APPEND WARN_FLAGS_CXX " /wd4264" )
  176. # disable "no override available for virtual member function, function is hidden"
  177. string( APPEND WARN_FLAGS_CXX " /wd4266" )
  178. # disable "class has virtual functions, but its (non)trivial destructor is not virtual"
  179. string( APPEND WARN_FLAGS_CXX " /wd5204 /wd4265" )
  180. # disable "layout of class may have changed from a previous version of the compiler"
  181. string( APPEND WARN_FLAGS_CXX " /wd4371" )
  182. # disable "relative include path contains '..'"
  183. string( APPEND WARN_FLAGS_CXX " /wd4464" )
  184. # disable "'const' variable is not used"
  185. string( APPEND WARN_FLAGS_CXX " /wd5264" )
  186. # disable "implicit fall-through occurs here" in case statement
  187. string( APPEND WARN_FLAGS_CXX " /wd5262" )
  188. # disable "unreferenced inline function has been removed"
  189. string( APPEND WARN_FLAGS_CXX " /wd4514" )
  190. # disable "compiler may not enforce left-to-right evaluation order in ..."
  191. string( APPEND WARN_FLAGS_CXX " /wd4868 /wd4866" )
  192. # disable "XXX is not defined as a preprocessor macro, replacing with '0'"
  193. string( APPEND WARN_FLAGS_CXX " /wd4668" )
  194. # disable "definition of implicit copy constructor for 'X' is deprecated because it has a user-provided destructor"
  195. string( APPEND WARN_FLAGS_CXX " /wd5267" )
  196. endif()