CheckCompilerToolchain.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/e2k 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. #
  45. # CPU SIMD instruction extension support for powerpc/powerpc64 archs:
  46. # HAVE_ALTIVEC
  47. #
  48. # CPU SIMD instruction extension support for arm/arm64 archs:
  49. # HAVE_NEON
  50. # NEON
  51. #
  52. # C++ features:
  53. # RTTI
  54. # EXCEPTIONS
  55. #
  56. # C++11 type traits standard:
  57. # IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE
  58. # IS_TRIVIALLY_DESTRUCTIBLE
  59. # IS_TRIVIALLY_COPY_ASSIGNABLE
  60. # IS_TRIVIALLY_COPY_CONSTRUCTIBLE
  61. # CLANG_PRE_STANDARD (Clang with pre-standard type trait templates)
  62. if (EMSCRIPTEN AND CMAKE_HOST_WIN32)
  63. set (EMCC_FIX EMCC_FIX)
  64. set (NULL_DEVICE${EMCC_FIX} ${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/null.c)
  65. execute_process (COMMAND ${CMAKE_COMMAND} -E touch ${NULL_DEVICE${EMCC_FIX}})
  66. endif ()
  67. # Macro for checking if a predefined macro is emitted by the chosen compiler toolchain natively
  68. macro (check_native_define REGEX OUTPUT_VAR)
  69. if (INVALIDATE_CCT OR NOT DEFINED ${OUTPUT_VAR})
  70. string (REGEX MATCH "#define +${REGEX} +([^;]+)" matched "${NATIVE_PREDEFINED_MACROS}")
  71. if (matched)
  72. string (REGEX MATCH "\\(.*\\)" captured "${REGEX}")
  73. if (captured)
  74. set (GROUP 2)
  75. else ()
  76. set (GROUP 1)
  77. endif ()
  78. string (REGEX REPLACE "#define +${REGEX} +([^;]+)" \\${GROUP} matched "${matched}")
  79. set (${OUTPUT_VAR} ${matched})
  80. else ()
  81. set (${OUTPUT_VAR} 0)
  82. endif ()
  83. set (${OUTPUT_VAR} ${${OUTPUT_VAR}} CACHE INTERNAL "Compiler toolchain has predefined macros matching ${REGEX}")
  84. endif ()
  85. endmacro ()
  86. # Macro for checking if a CPU instruction extension is supported by the chosen compiler toolchain
  87. macro (check_extension EXTENSION)
  88. string (TOUPPER "${EXTENSION}" UCASE_EXT_NAME) # Stringify to guard against empty variable
  89. string (REGEX REPLACE [^=]+= "" UCASE_EXT_NAME "${UCASE_EXT_NAME}")
  90. if (INVALIDATE_CCT OR NOT DEFINED HAVE_${UCASE_EXT_NAME})
  91. 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)
  92. if (NOT CC_EXIT_STATUS EQUAL 0)
  93. message (FATAL_ERROR "Could not check compiler toolchain CPU instruction extension as it does not handle '-E -dM' compiler flags correctly")
  94. endif ()
  95. if (NOT ${ARGN} STREQUAL "")
  96. set (EXPECTED_MACRO ${ARGN})
  97. else ()
  98. set (EXPECTED_MACRO __${UCASE_EXT_NAME}__)
  99. endif ()
  100. if (PREDEFINED_MACROS MATCHES "#define +${EXPECTED_MACRO} +1")
  101. set (matched 1)
  102. else ()
  103. set (matched 0)
  104. endif ()
  105. set (HAVE_${UCASE_EXT_NAME} ${matched} CACHE INTERNAL "Compiler toolchain supports ${UCASE_EXT_NAME} CPU instruction extension")
  106. endif ()
  107. endmacro ()
  108. # Macro for checking if a C++ feature is enabled by the configured CXX compiler flags
  109. macro (check_feature_enabled FEATURE)
  110. if (INVALIDATE_CCT OR NOT DEFINED ${FEATURE})
  111. set (COMPILER_FLAGS ${CMAKE_CXX_FLAGS})
  112. separate_arguments (COMPILER_FLAGS)
  113. 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)
  114. if (NOT CXX_EXIT_STATUS EQUAL 0)
  115. message (FATAL_ERROR "Could not check compiler toolchain CPU instruction extension as it does not handle '-E -dM' compiler flags correctly")
  116. endif ()
  117. if (NOT ${ARGN} STREQUAL "")
  118. set (EXPECTED_MACRO ${ARGN})
  119. else ()
  120. set (EXPECTED_MACRO __${FEATURE})
  121. endif ()
  122. if (PREDEFINED_MACROS MATCHES "#define +${EXPECTED_MACRO} +1")
  123. set (matched 1)
  124. else ()
  125. set (matched 0)
  126. endif ()
  127. set (${FEATURE} ${matched} CACHE INTERNAL "Is ${FEATURE} enabled")
  128. endif ()
  129. endmacro ()
  130. include (CheckCXXSourceCompiles)
  131. # Macro for checking if the type trait template is matching the C++11 standard
  132. macro (check_type_trait TYPE_TRAIT)
  133. string (TOUPPER ${TYPE_TRAIT} UPPERCASE_${TYPE_TRAIT})
  134. if (INVALIDATE_CCT)
  135. unset (${UPPERCASE_${TYPE_TRAIT}} CACHE)
  136. endif ()
  137. if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
  138. set (ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  139. set (CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}")
  140. endif ()
  141. check_cxx_source_compiles ("#include <type_traits>\nint main() { return std::${TYPE_TRAIT}<bool>::value; }" ${UPPERCASE_${TYPE_TRAIT}})
  142. if (CMAKE_CXX_COMPILER_ID MATCHES GNU|Clang)
  143. set (CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS})
  144. endif ()
  145. endmacro ()
  146. # Macro for checking if a native compiler toolchain exists for building the host tool targets
  147. # This macro is designed to be used in cross-compiling build
  148. macro (check_native_compiler_exist)
  149. if (NOT HAVE_NATIVE_COMPILER)
  150. message (STATUS "Performing Test HAVE_NATIVE_COMPILER")
  151. file (WRITE ${CMAKE_BINARY_DIR}/generated/CMakeLists.txt "message (\"Probing native compiler toolchain...\")\n")
  152. execute_process (COMMAND ${CMAKE_COMMAND} -E env CC=${SAVED_CC} CXX=${SAVED_CXX} ${CMAKE_COMMAND} -G${CMAKE_GENERATOR} -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} .
  153. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/generated RESULT_VARIABLE EXIT_CODE ERROR_VARIABLE ERR_VAR OUTPUT_QUIET)
  154. if (NOT EXIT_CODE EQUAL 0)
  155. message (STATUS "Performing Test HAVE_NATIVE_COMPILER - Failed")
  156. execute_process (COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/generated/CMakeCache.txt)
  157. message (FATAL_ERROR "Could not find native compiler toolchain. This is usually caused by wrong PATH env-var value.\n${ERR_VAR}")
  158. endif ()
  159. message (STATUS "Performing Test HAVE_NATIVE_COMPILER - Success")
  160. set (HAVE_NATIVE_COMPILER 1 CACHE INTERNAL "Check native compiler exist")
  161. endif ()
  162. endmacro ()
  163. if (MSVC)
  164. # TODO: revisit this later because VS may use Clang as compiler in the future
  165. # On MSVC compiler, use the chosen CMake/VS generator to determine the ABI
  166. set (NATIVE_64BIT ${CMAKE_CL_64})
  167. # We only support one target arch when using MSVC for now and make certain assumptions as per documentation instead of querying the compiler
  168. 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)
  169. set (${VAR} 1)
  170. endforeach ()
  171. else ()
  172. # The 'invalidate event' will be sent by toolchain file when it reconfigures the cross-compiler or compiler flags
  173. if (INVALIDATE_CCT OR NOT DEFINED NATIVE_PREDEFINED_MACROS)
  174. if (IOS OR TVOS)
  175. # Assume arm64 is the native arch (this does not prevent our build system to target armv7 later in universal binary build)
  176. set (ARCH_FLAGS -arch arm64)
  177. elseif (CMAKE_CXX_COMPILER_ID MATCHES Clang)
  178. # Use the same target flag as configured by CMake toolchain file, if any
  179. if (ANDROID)
  180. set (ARCH_FLAGS -target ${ANDROID_LLVM_TRIPLE})
  181. elseif (CMAKE_CXX_FLAGS MATCHES -target)
  182. string (REGEX REPLACE "^.*-target ([^ ]+).*$" "-target;\\1" ARCH_FLAGS "${CMAKE_CXX_FLAGS}")
  183. endif ()
  184. endif ()
  185. 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)
  186. if (NOT CC_EXIT_STATUS EQUAL 0)
  187. message (FATAL_ERROR "Could not check compiler toolchain as it does not handle '-E -dM' compiler flags correctly")
  188. endif ()
  189. string (REPLACE \n ";" NATIVE_PREDEFINED_MACROS "${NATIVE_PREDEFINED_MACROS}") # Stringify for string replacement
  190. set (NATIVE_PREDEFINED_MACROS ${NATIVE_PREDEFINED_MACROS} CACHE INTERNAL "Compiler toolchain native predefined macros")
  191. endif ()
  192. # Determine the native ABI based on the size of pointer
  193. check_native_define (__SIZEOF_POINTER__ SIZEOF_POINTER)
  194. if (SIZEOF_POINTER EQUAL 8)
  195. set (NATIVE_64BIT 1)
  196. endif ()
  197. # Android arm64 compiler only emits __aarch64__ while iOS/tvOS arm64 emits __aarch64__ and __arm64__; for armv7a all emit __arm__
  198. check_native_define ("__(arm|aarch64)__" ARM)
  199. # For completeness sake as currently we do not support MIPS and PowerPC (yet)
  200. check_native_define (__MIPSEL__ MIPS)
  201. check_native_define ("__(ppc|PPC|powerpc|POWERPC)(64)*__" POWERPC)
  202. # For now we only support Emscripten compiler toolchain when targeting Web platform
  203. check_native_define (__EMSCRIPTEN__ WEB)
  204. # Compiler should emit __x86_64__, __i686__, or __i386__, etc when targeting archs using Intel or AMD processors
  205. check_native_define ("__(i.86|x86_64)__" X86)
  206. # MCST lcc compiler only emits __e2k__ when targeting arch using MCST Elbrus 2000 processor
  207. check_native_define ("__e2k__" E2K)
  208. if (ARM)
  209. check_feature_enabled (NEON __ARM_NEON)
  210. if (NEON)
  211. # 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
  212. set (HAVE_NEON 1)
  213. else ()
  214. check_extension (fpu=neon __ARM_NEON)
  215. endif ()
  216. elseif (POWERPC)
  217. check_extension (altivec)
  218. elseif (X86 OR E2K)
  219. foreach (ext sse sse2 sse3 sse4 avx avx2)
  220. check_extension (${ext})
  221. endforeach ()
  222. if (CMAKE_SYSTEM_NAME STREQUAL Linux)
  223. check_native_define (__MMX__ HAVE_MMX)
  224. check_native_define (__3dNOW__ HAVE_3DNOW)
  225. endif ()
  226. endif ()
  227. # Check if C++ feature is being turned on/off in the configured compiler flags
  228. check_feature_enabled (RTTI __GXX_RTTI)
  229. check_feature_enabled (EXCEPTIONS)
  230. # Check if C++11 type trait standard is being followed
  231. check_type_trait (is_trivially_default_constructible)
  232. check_type_trait (is_trivially_destructible)
  233. check_type_trait (is_trivially_copy_assignable)
  234. check_type_trait (is_trivially_copy_constructible)
  235. if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND NOT IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE AND IS_TRIVIALLY_DESTRUCTIBLE)
  236. set (CLANG_PRE_STANDARD 1)
  237. endif ()
  238. endif ()