common_compiler_flags.cmake 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #[=======================================================================[.rst:
  2. Common Compiler Flags
  3. ---------------------
  4. This file contains a single function to configure platform agnostic compiler
  5. flags like optimization levels, warnings, and features. For platform specific
  6. flags look to each of the ``cmake/<platform>.cmake`` files.
  7. ]=======================================================================]
  8. #[[ Compiler Configuration, not to be confused with build targets ]]
  9. set( DEBUG_SYMBOLS "$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>" )
  10. #[[ Compiler Identification ]]
  11. set( IS_CLANG "$<CXX_COMPILER_ID:Clang>" )
  12. set( IS_APPLECLANG "$<CXX_COMPILER_ID:AppleClang>" )
  13. set( IS_GNU "$<CXX_COMPILER_ID:GNU>" )
  14. set( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" )
  15. set( NOT_MSVC "$<NOT:$<CXX_COMPILER_ID:MSVC>>" )
  16. set( GNU_LT_V8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" )
  17. set( GNU_GE_V9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" )
  18. set( GNU_GT_V11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
  19. set( GNU_LT_V11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
  20. set( GNU_GE_V12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
  21. function( common_compiler_flags TARGET_NAME )
  22. target_compile_features(${TARGET_NAME}
  23. PUBLIC
  24. cxx_std_17
  25. )
  26. # These compiler options reflect what is in godot/SConstruct.
  27. target_compile_options( ${TARGET_NAME}
  28. PUBLIC
  29. # Disable exception handling. Godot doesn't use exceptions anywhere, and this
  30. # saves around 20% of binary size and very significant build time.
  31. $<${DISABLE_EXCEPTIONS}:
  32. $<${NOT_MSVC}:-fno-exceptions>
  33. >
  34. # Enabling Debug Symbols
  35. $<${DEBUG_SYMBOLS}:
  36. # Adding dwarf-4 explicitly makes stacktraces work with clang builds,
  37. # otherwise addr2line doesn't understand them.
  38. $<${NOT_MSVC}:
  39. -gdwarf-4
  40. $<IF:${IS_DEV_BUILD},-g3,-g2>
  41. >
  42. >
  43. $<${IS_DEV_BUILD}:
  44. $<${NOT_MSVC}:-fno-omit-frame-pointer -O0>
  45. >
  46. $<${HOT_RELOAD}:
  47. $<${IS_GNU}:-fno-gnu-unique>
  48. >
  49. # MSVC only
  50. $<${IS_MSVC}:
  51. "/MP ${PROC_N}"
  52. /W4
  53. # Disable warnings which we don't plan to fix.
  54. /wd4100 # C4100 (unreferenced formal parameter): Doesn't play nice with polymorphism.
  55. /wd4127 # C4127 (conditional expression is constant)
  56. /wd4201 # C4201 (non-standard nameless struct/union): Only relevant for C89.
  57. /wd4244 # C4244 C4245 C4267 (narrowing conversions): Unavoidable at this scale.
  58. /wd4245
  59. /wd4267
  60. /wd4305 # C4305 (truncation): double to float or real_t, too hard to avoid.
  61. /wd4514 # C4514 (unreferenced inline function has been removed)
  62. /wd4714 # C4714 (function marked as __forceinline not inlined)
  63. /wd4820 # C4820 (padding added after construct)
  64. /utf-8
  65. >
  66. # Clang and GNU common options
  67. $<$<OR:${IS_CLANG},${IS_GNU}>:
  68. -Wall
  69. -Wctor-dtor-privacy
  70. -Wextra
  71. -Wno-unused-parameter
  72. -Wnon-virtual-dtor
  73. -Wwrite-strings
  74. >
  75. # Clang only
  76. $<${IS_CLANG}:
  77. -Wimplicit-fallthrough
  78. -Wno-ordered-compare-function-pointers
  79. >
  80. # GNU only
  81. $<${IS_GNU}:
  82. -Walloc-zero
  83. -Wduplicated-branches
  84. -Wduplicated-cond
  85. -Wno-misleading-indentation
  86. -Wplacement-new=1
  87. -Wshadow-local
  88. -Wstringop-overflow=4
  89. # Bogus warning fixed in 8+.
  90. $<${GNU_LT_V8}:-Wno-strict-overflow>
  91. $<${GNU_GE_V9}:-Wattribute-alias=2>
  92. # Broke on MethodBind templates before GCC 11.
  93. $<${GNU_GT_V11}:-Wlogical-op>
  94. # Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it.
  95. $<${GNU_LT_V11}:-Wno-type-limits>
  96. # False positives in our error macros, see GH-58747.
  97. $<${GNU_GE_V12}:-Wno-return-type>
  98. >
  99. )
  100. target_compile_definitions(${TARGET_NAME}
  101. PUBLIC
  102. GDEXTENSION
  103. # features
  104. $<${DEBUG_FEATURES}:DEBUG_ENABLED DEBUG_METHODS_ENABLED>
  105. $<${IS_DEV_BUILD}:DEV_ENABLED>
  106. $<${HOT_RELOAD}:HOT_RELOAD_ENABLED>
  107. $<$<STREQUAL:${GODOT_PRECISION},double>:REAL_T_IS_DOUBLE>
  108. $<${IS_MSVC}:$<${DISABLE_EXCEPTIONS}:_HAS_EXCEPTIONS=0>>
  109. )
  110. target_link_options( ${TARGET_NAME}
  111. PUBLIC
  112. $<${IS_MSVC}:
  113. /WX # treat link warnings as errors.
  114. /MANIFEST:NO # We dont need a manifest
  115. >
  116. $<${DEBUG_SYMBOLS}:$<${IS_MSVC}:/DEBUG:FULL>>
  117. $<$<NOT:${DEBUG_SYMBOLS}>:
  118. $<${IS_GNU}:-s>
  119. $<${IS_CLANG}:-s>
  120. $<${IS_APPLECLANG}:-Wl,-S -Wl,-x -Wl,-dead_strip>
  121. >
  122. )
  123. endfunction()