common_compiler_flags.cmake 6.0 KB

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