common_compiler_flags.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. # gersemi: off
  41. # These compiler options reflect what is in godot/SConstruct.
  42. target_compile_options(
  43. ${TARGET_NAME}
  44. PUBLIC
  45. # Disable exception handling. Godot doesn't use exceptions anywhere, and this
  46. # saves around 20% of binary size and very significant build time.
  47. $<${DISABLE_EXCEPTIONS}:$<${NOT_MSVC}:-fno-exceptions>>
  48. # Enabling Debug Symbols
  49. $<${DEBUG_SYMBOLS}:
  50. # Adding dwarf-4 explicitly makes stacktraces work with clang builds,
  51. # otherwise addr2line doesn't understand them.
  52. $<${NOT_MSVC}:
  53. -gdwarf-4
  54. $<IF:${IS_DEV_BUILD},-g3,-g2>
  55. >
  56. >
  57. $<${IS_DEV_BUILD}:$<${NOT_MSVC}:-fno-omit-frame-pointer -O0>>
  58. $<${HOT_RELOAD}:$<${IS_GNU}:-fno-gnu-unique>>
  59. # MSVC only
  60. $<${IS_MSVC}:
  61. # /MP isn't valid for clang-cl with msvc frontend
  62. $<$<CXX_COMPILER_ID:MSVC>:/MP${PROC_N}>
  63. /W4
  64. # Disable warnings which we don't plan to fix.
  65. /wd4100 # C4100 (unreferenced formal parameter): Doesn't play nice with polymorphism.
  66. /wd4127 # C4127 (conditional expression is constant)
  67. /wd4201 # C4201 (non-standard nameless struct/union): Only relevant for C89.
  68. /wd4244 # C4244 C4245 C4267 (narrowing conversions): Unavoidable at this scale.
  69. /wd4245
  70. /wd4267
  71. /wd4305 # C4305 (truncation): double to float or real_t, too hard to avoid.
  72. /wd4514 # C4514 (unreferenced inline function has been removed)
  73. /wd4714 # C4714 (function marked as __forceinline not inlined)
  74. /wd4820 # C4820 (padding added after construct)
  75. /utf-8
  76. >
  77. # Clang and GNU common options
  78. $<$<OR:${IS_CLANG},${IS_GNU}>:
  79. -Wall
  80. -Wctor-dtor-privacy
  81. -Wextra
  82. -Wno-unused-parameter
  83. -Wnon-virtual-dtor
  84. -Wwrite-strings
  85. >
  86. # Clang only
  87. $<${IS_CLANG}:
  88. -Wimplicit-fallthrough
  89. -Wno-ordered-compare-function-pointers
  90. >
  91. # GNU only
  92. $<${IS_GNU}:
  93. -Walloc-zero
  94. -Wduplicated-branches
  95. -Wduplicated-cond
  96. -Wno-misleading-indentation
  97. -Wplacement-new=1
  98. -Wshadow-local
  99. -Wstringop-overflow=4
  100. # Bogus warning fixed in 8+.
  101. $<${GNU_LT_V8}:-Wno-strict-overflow>
  102. $<${GNU_GE_V9}:-Wattribute-alias=2>
  103. # Broke on MethodBind templates before GCC 11.
  104. $<${GNU_GT_V11}:-Wlogical-op>
  105. # Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it.
  106. $<${GNU_LT_V11}:-Wno-type-limits>
  107. # False positives in our error macros, see GH-58747.
  108. $<${GNU_GE_V12}:-Wno-return-type>
  109. >
  110. )
  111. target_compile_definitions(
  112. ${TARGET_NAME}
  113. PUBLIC
  114. GDEXTENSION
  115. # features
  116. $<${DEBUG_FEATURES}:DEBUG_ENABLED DEBUG_METHODS_ENABLED>
  117. $<${IS_DEV_BUILD}:DEV_ENABLED>
  118. $<${HOT_RELOAD}:HOT_RELOAD_ENABLED>
  119. $<$<STREQUAL:${GODOTCPP_PRECISION},double>:REAL_T_IS_DOUBLE>
  120. $<${IS_MSVC}:$<${DISABLE_EXCEPTIONS}:_HAS_EXCEPTIONS=0>>
  121. $<${THREADS_ENABLED}:THREADS_ENABLED>
  122. )
  123. target_link_options(
  124. ${TARGET_NAME}
  125. PUBLIC
  126. $<${IS_MSVC}:
  127. /WX # treat link warnings as errors.
  128. /MANIFEST:NO # We dont need a manifest
  129. >
  130. $<${DEBUG_SYMBOLS}:$<${IS_MSVC}:/DEBUG:FULL>>
  131. $<$<NOT:${DEBUG_SYMBOLS}>:
  132. $<${IS_GNU}:-s>
  133. $<${IS_CLANG}:-s>
  134. $<${IS_APPLECLANG}:-Wl,-S -Wl,-x -Wl,-dead_strip>
  135. >
  136. )
  137. # gersemi: on
  138. endfunction()