GodotCompilerWarnings.cmake 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Add warnings based on compiler & version
  2. # Set some helper variables for readability
  3. set( compiler_less_than_v8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" )
  4. set( compiler_greater_than_or_equal_v9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" )
  5. set( compiler_greater_than_or_equal_v11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
  6. set( compiler_less_than_v11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
  7. set( compiler_greater_than_or_equal_v12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
  8. # These compiler options reflect what is in godot/SConstruct.
  9. target_compile_options( ${PROJECT_NAME} PRIVATE
  10. # MSVC only
  11. $<${compiler_is_msvc}:
  12. /W4
  13. # Disable warnings which we don't plan to fix.
  14. /wd4100 # C4100 (unreferenced formal parameter): Doesn't play nice with polymorphism.
  15. /wd4127 # C4127 (conditional expression is constant)
  16. /wd4201 # C4201 (non-standard nameless struct/union): Only relevant for C89.
  17. /wd4244 # C4244 C4245 C4267 (narrowing conversions): Unavoidable at this scale.
  18. /wd4245
  19. /wd4267
  20. /wd4305 # C4305 (truncation): double to float or real_t, too hard to avoid.
  21. /wd4514 # C4514 (unreferenced inline function has been removed)
  22. /wd4714 # C4714 (function marked as __forceinline not inlined)
  23. /wd4820 # C4820 (padding added after construct)
  24. >
  25. # Clang and GNU common options
  26. $<$<OR:${compiler_is_clang},${compiler_is_gnu}>:
  27. -Wall
  28. -Wctor-dtor-privacy
  29. -Wextra
  30. -Wno-unused-parameter
  31. -Wnon-virtual-dtor
  32. -Wwrite-strings
  33. >
  34. # Clang only
  35. $<${compiler_is_clang}:
  36. -Wimplicit-fallthrough
  37. -Wno-ordered-compare-function-pointers
  38. >
  39. # GNU only
  40. $<${compiler_is_gnu}:
  41. -Walloc-zero
  42. -Wduplicated-branches
  43. -Wduplicated-cond
  44. -Wno-misleading-indentation
  45. -Wplacement-new=1
  46. -Wshadow-local
  47. -Wstringop-overflow=4
  48. >
  49. $<$<AND:${compiler_is_gnu},${compiler_less_than_v8}>:
  50. # Bogus warning fixed in 8+.
  51. -Wno-strict-overflow
  52. >
  53. $<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v9}>:
  54. -Wattribute-alias=2
  55. >
  56. $<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v11}>:
  57. # Broke on MethodBind templates before GCC 11.
  58. -Wlogical-op
  59. >
  60. $<$<AND:${compiler_is_gnu},${compiler_less_than_v11}>:
  61. # Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it.
  62. -Wno-type-limits
  63. >
  64. $<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v12}>:
  65. # False positives in our error macros, see GH-58747.
  66. -Wno-return-type
  67. >
  68. )
  69. # Treat warnings as errors
  70. function( set_warning_as_error )
  71. message( STATUS "[${PROJECT_NAME}] Treating warnings as errors")
  72. if ( CMAKE_VERSION VERSION_GREATER_EQUAL "3.24" )
  73. set_target_properties( ${PROJECT_NAME}
  74. PROPERTIES
  75. COMPILE_WARNING_AS_ERROR ON
  76. )
  77. else()
  78. target_compile_options( ${PROJECT_NAME}
  79. PRIVATE
  80. $<${compiler_is_msvc}:/WX>
  81. $<$<OR:${compiler_is_clang},${compiler_is_gnu}>:-Werror>
  82. )
  83. endif()
  84. endfunction()
  85. if ( GODOT_CPP_WARNING_AS_ERROR )
  86. set_warning_as_error()
  87. endif()