CheckCompilerToolchain.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #
  2. # Copyright (c) 2008-2020 the Urho3D project.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. # Check the chosen compiler toolchain in the build tree
  23. #
  24. # Native ABI:
  25. # NATIVE_64BIT
  26. #
  27. # Target architecture:
  28. # ARM
  29. # MIPS
  30. # POWERPC
  31. # WEB
  32. # X86
  33. # E2K
  34. #
  35. # CPU SIMD instruction extensions support for x86/x86_64 archs:
  36. # HAVE_MMX
  37. # HAVE_3DNOW
  38. # HAVE_SSE
  39. # HAVE_SSE2
  40. # HAVE_SSE3
  41. # HAVE_SSE4
  42. # HAVE_AVX
  43. # HAVE_AVX2
  44. # HAVE_ALTIVEC
  45. #
  46. # CPU SIMD instruction extension support for arm/arm64 archs:
  47. # HAVE_NEON
  48. # NEON
  49. #
  50. # C++ features:
  51. # RTTI
  52. # EXCEPTIONS
  53. #
  54. # C++11 type traits standard:
  55. # IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE
  56. # IS_TRIVIALLY_DESTRUCTIBLE
  57. # IS_TRIVIALLY_COPY_ASSIGNABLE
  58. # IS_TRIVIALLY_COPY_CONSTRUCTIBLE
  59. # CLANG_PRE_STANDARD (Clang with pre-standard type trait templates)
  60. if (EMSCRIPTEN AND CMAKE_HOST_WIN32)
  61. set (EMCC_FIX EMCC_FIX)
  62. set (NULL_DEVICE${EMCC_FIX} ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/null.c)
  63. execute_process (COMMAND ${CMAKE_COMMAND} -E touch ${NULL_DEVICE${EMCC_FIX}})
  64. endif ()
  65. # Macro for checking if a predefined macro is emitted by the chosen compiler toolchain natively
  66. macro (check_native_define REGEX OUTPUT_VAR)
  67. if (INVALIDATE_CCT OR NOT DEFINED ${OUTPUT_VAR})
  68. string (REGEX MATCH "#define +${REGEX} +([^;]+)" matched "${NATIVE_PREDEFINED_MACROS}")
  69. if (matched)
  70. string (REGEX MATCH "\\(.*\\)" captured "${REGEX}")
  71. if (captured)
  72. set (GROUP 2)
  73. else ()
  74. set (GROUP 1)
  75. endif ()
  76. string (REGEX REPLACE "#define +${REGEX} +([^;]+)" \\${GROUP} matched "${matched}")
  77. set (${OUTPUT_VAR} ${matched})
  78. else ()
  79. set (${OUTPUT_VAR} 0)
  80. endif ()
  81. set (${OUTPUT_VAR} ${${OUTPUT_VAR}} CACHE INTERNAL "Compiler toolchain has predefined macros matching ${REGEX}")
  82. endif ()
  83. endmacro ()
  84. # Macro for checking if a CPU instruction extension is supported by the chosen compiler toolchain
  85. macro (check_extension EXTENSION)
  86. string (TOUPPER "${EXTENSION}" UCASE_EXT_NAME) # Stringify to guard against empty variable
  87. string (REGEX REPLACE [^=]+= "" UCASE_EXT_NAME "${UCASE_EXT_NAME}")
  88. if (INVALIDATE_CCT OR NOT DEFINED HAVE_${UCASE_EXT_NAME})
  89. execute_process (COMMAND ${CMAKE_C_COMPILER} ${ARCH_FLAGS} -m${EXTENSION} -E -dM -xc ${NULL_DEVICE${EMCC_FIX}} RESULT_VARIABLE CC_EXIT_STATUS OUTPUT_VARIABLE PREDEFINED_MACROS ERROR_QUIET)
  90. if (NOT CC_EXIT_STATUS EQUAL 0)
  91. message (FATAL_ERROR "Could not check compiler toolchain CPU instruction extension as it does not handle '-E -dM' compiler flags correctly")
  92. endif ()
  93. if (NOT ${ARGN} STREQUAL "")
  94. set (EXPECTED_MACRO ${ARGN})
  95. else ()
  96. set (EXPECTED_MACRO __${UCASE_EXT_NAME}__)
  97. endif ()
  98. if (PREDEFINED_MACROS MATCHES "#define +${EXPECTED_MACRO} +1")
  99. set (matched 1)
  100. else ()
  101. set (matched 0)
  102. endif ()
  103. set (HAVE_${UCASE_EXT_NAME} ${matched} CACHE INTERNAL "Compiler toolchain supports ${UCASE_EXT_NAME} CPU instruction extension")
  104. endif ()
  105. endmacro ()
  106. # Macro for checking if a C++ feature is enabled by the configured CXX compiler flags
  107. macro (check_feature_enabled FEATURE)
  108. if (INVALIDATE_CCT OR NOT DEFINED ${FEATURE})
  109. set (COMPILER_FLAGS ${CMAKE_CXX_FLAGS})
  110. separate_arguments (COMPILER_FLAGS)
  111. execute_process (COMMAND ${CMAKE_CXX_COMPILER} ${ARCH_FLAGS} ${COMPILER_FLAGS} -E -dM -xc++ ${NULL_DEVICE${EMCC_FIX}} RESULT_VARIABLE CXX_EXIT_STATUS OUTPUT_VARIABLE PREDEFINED_MACROS ERROR_QUIET)
  112. if (NOT CXX_EXIT_STATUS EQUAL 0)
  113. message (FATAL_ERROR "Could not check compiler toolchain CPU instruction extension as it does not handle '-E -dM' compiler flags correctly")
  114. endif ()
  115. if (NOT ${ARGN} STREQUAL "")
  116. set (EXPECTED_MACRO ${ARGN})
  117. else ()
  118. set (EXPECTED_MACRO __${FEATURE})
  119. endif ()
  120. if (PREDEFINED_MACROS MATCHES "#define +${EXPECTED_MACRO} +1")
  121. set (matched 1)
  122. else ()
  123. set (matched 0)
  124. endif ()
  125. set (${FEATURE} ${matched} CACHE INTERNAL "Is ${FEATURE} enabled")
  126. endif ()
  127. endmacro ()
  128. include (CheckCXXSourceCompiles)
  129. # Macro for checking if the type trait template is matching the C++11 standard
  130. macro (check_type_trait TYPE_TRAIT)
  131. string (TOUPPER ${TYPE_TRAIT} UPPERCASE_${TYPE_TRAIT})
  132. if (INVALIDATE_CCT)
  133. unset (${UPPERCASE_${TYPE_TRAIT}} CACHE)
  134. endif ()
  135. if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
  136. set (ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  137. set (CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}")
  138. endif ()
  139. check_cxx_source_compiles ("#include <type_traits>\nint main() { return std::${TYPE_TRAIT}<bool>::value; }" ${UPPERCASE_${TYPE_TRAIT}})
  140. if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
  141. set (CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
  142. endif ()
  143. endmacro ()
  144. # Macro for checking if a native compiler toolchain exists for building the host tool targets
  145. # This macro is designed to be used in cross-compiling build
  146. macro (check_native_compiler_exist)
  147. if (NOT HAVE_NATIVE_COMPILER)
  148. message (STATUS "Performing Test HAVE_NATIVE_COMPILER")
  149. file (WRITE ${CMAKE_BINARY_DIR}/generated/CMakeLists.txt "message (\"Probing native compiler toolchain...\")\n")
  150. execute_process (COMMAND ${CMAKE_COMMAND} -E env CC=${SAVED_CC} CXX=${SAVED_CXX} ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} .
  151. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/generated RESULT_VARIABLE EXIT_CODE ERROR_VARIABLE ERR_VAR OUTPUT_QUIET)
  152. if (NOT EXIT_CODE EQUAL 0)
  153. message (STATUS "Performing Test HAVE_NATIVE_COMPILER - Failed")
  154. execute_process (COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/generated/CMakeCache.txt)
  155. message (FATAL_ERROR "Could not find native compiler toolchain. This is usually caused by wrong PATH env-var value.\n${ERR_VAR}")
  156. endif ()
  157. message (STATUS "Performing Test HAVE_NATIVE_COMPILER - Success")
  158. set (HAVE_NATIVE_COMPILER 1 CACHE INTERNAL "Check native compiler exist")
  159. endif ()
  160. endmacro ()
  161. if (MSVC)
  162. # TODO: revisit this later because VS may use Clang as compiler in the future
  163. # On MSVC compiler, use the chosen CMake/VS generator to determine the ABI
  164. set (NATIVE_64BIT ${CMAKE_CL_64})
  165. # We only support one target arch when using MSVC for now and make certain assumptions as per documentation instead of querying the compiler
  166. foreach (VAR X86 HAVE_MMX HAVE_SSE HAVE_SSE2 RTTI EXCEPTIONS IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE IS_TRIVIALLY_DESTRUCTIBLE IS_TRIVIALLY_COPY_ASSIGNABLE IS_TRIVIALLY_COPY_CONSTRUCTIBLE)
  167. set (${VAR} 1)
  168. endforeach ()
  169. else ()
  170. # The 'invalidate event' will be sent by toolchain file when it reconfigures the cross-compiler or compiler flags
  171. if (INVALIDATE_CCT OR NOT DEFINED NATIVE_PREDEFINED_MACROS)
  172. if (IOS OR TVOS)
  173. # Assume arm64 is the native arch (this does not prevent our build system to target armv7 later in universal binary build)
  174. set (ARCH_FLAGS -arch arm64)
  175. elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang)
  176. # Use the same target flag as configured by CMake toolchain file, if any
  177. if (ANDROID)
  178. set (ARCH_FLAGS -target ${ANDROID_LLVM_TRIPLE})
  179. elseif (CMAKE_CXX_FLAGS MATCHES -target)
  180. string (REGEX REPLACE "^.*-target ([^ ]+).*$" "-target;\\1" ARCH_FLAGS "${CMAKE_CXX_FLAGS}")
  181. endif ()
  182. endif ()
  183. execute_process (COMMAND ${CMAKE_C_COMPILER} ${ARCH_FLAGS} -E -dM -xc ${NULL_DEVICE${EMCC_FIX}} RESULT_VARIABLE CC_EXIT_STATUS OUTPUT_VARIABLE NATIVE_PREDEFINED_MACROS ERROR_QUIET)
  184. if (NOT CC_EXIT_STATUS EQUAL 0)
  185. message (FATAL_ERROR "Could not check compiler toolchain as it does not handle '-E -dM' compiler flags correctly")
  186. endif ()
  187. string (REPLACE \n ";" NATIVE_PREDEFINED_MACROS "${NATIVE_PREDEFINED_MACROS}") # Stringify for string replacement
  188. set (NATIVE_PREDEFINED_MACROS ${NATIVE_PREDEFINED_MACROS} CACHE INTERNAL "Compiler toolchain native predefined macros")
  189. endif ()
  190. # Determine the native ABI based on the size of pointer
  191. check_native_define (__SIZEOF_POINTER__ SIZEOF_POINTER)
  192. if (SIZEOF_POINTER EQUAL 8)
  193. set (NATIVE_64BIT 1)
  194. endif ()
  195. # Android arm64 compiler only emits __aarch64__ while iOS/tvOS arm64 emits __aarch64__ and __arm64__; for armv7a all emit __arm__
  196. check_native_define ("__(arm|aarch64)__" ARM)
  197. # For completeness sake as currently we do not support MIPS and PowerPC (yet)
  198. check_native_define (__MIPSEL__ MIPS)
  199. check_native_define ("__(ppc|PPC|powerpc|POWERPC)(64)*__" POWERPC)
  200. # For now we only support Emscripten compiler toolchain when targeting Web platform
  201. check_native_define (__EMSCRIPTEN__ WEB)
  202. # Compiler should emit __x86_64__, __i686__, or __i386__, etc when targeting archs using Intel or AMD processors
  203. check_native_define ("__(i.86|x86_64)__" X86)
  204. # MCST lcc compiler only emits __e2k__ when targeting arch using MCST Elbrus 2000 processor
  205. check_native_define ("__e2k__" E2K)
  206. if (ARM)
  207. check_feature_enabled (NEON __ARM_NEON)
  208. if (NEON)
  209. # NEON is enabled by default on aarch64 arch so its compiler emits __ARM_NEON by default even though it does not support '-mfpu' compiler flag
  210. set (HAVE_NEON 1)
  211. else ()
  212. check_extension (fpu=neon __ARM_NEON)
  213. endif ()
  214. elseif (POWERPC)
  215. check_extension (altivec)
  216. elseif (X86)
  217. foreach (ext sse sse2 sse3 sse4 avx avx2)
  218. check_extension (${ext})
  219. endforeach ()
  220. if (CMAKE_SYSTEM_NAME STREQUAL Linux)
  221. check_native_define (__MMX__ HAVE_MMX)
  222. check_native_define (__3dNOW__ HAVE_3DNOW)
  223. endif ()
  224. endif ()
  225. # Check if C++ feature is being turned on/off in the configured compiler flags
  226. check_feature_enabled (RTTI __GXX_RTTI)
  227. check_feature_enabled (EXCEPTIONS)
  228. # Check if C++11 type trait standard is being followed
  229. check_type_trait (is_trivially_default_constructible)
  230. check_type_trait (is_trivially_destructible)
  231. check_type_trait (is_trivially_copy_assignable)
  232. check_type_trait (is_trivially_copy_constructible)
  233. if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND NOT IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE AND IS_TRIVIALLY_DESTRUCTIBLE)
  234. set (CLANG_PRE_STANDARD 1)
  235. endif ()
  236. endif ()