common_compiler_flags.cmake 6.8 KB

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