common_compiler_flags.cmake 7.2 KB

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