CMakeCommonCompilerMacros.cmake 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This module is shared by multiple languages and compilers; use include guard
  4. if (__COMPILER_CMAKE_COMMON_COMPILER_MACROS)
  5. return()
  6. endif ()
  7. set(__COMPILER_CMAKE_COMMON_COMPILER_MACROS 1)
  8. # Check that a compiler's language standard is properly detected
  9. # Parameters:
  10. # lang - Language to check
  11. # stdver1 - Minimum version to set a given default for
  12. # std1 - Default to use for compiler ver >= stdver1
  13. # stdverN - Minimum version to set a given default for
  14. # stdN - Default to use for compiler ver >= stdverN
  15. #
  16. # The order of stdverN stdN pairs passed as arguments is expected to be in
  17. # monotonically increasing version order.
  18. #
  19. # Note:
  20. # This macro can be called with multiple version / std pairs to convey that
  21. # newer compiler versions may use a newer standard default.
  22. #
  23. # Example:
  24. # To specify that compiler version 6.1 and newer defaults to C++11 while
  25. # 4.8 <= ver < 6.1 default to C++98, you would call:
  26. #
  27. # __compiler_check_default_language_standard(CXX 4.8 98 6.1 11)
  28. #
  29. macro(__compiler_check_default_language_standard lang stdver1 std1)
  30. set(__std_ver_pairs "${stdver1};${std1};${ARGN}")
  31. string(REGEX REPLACE " *; *" " " __std_ver_pairs "${__std_ver_pairs}")
  32. string(REGEX MATCHALL "[^ ]+ [^ ]+" __std_ver_pairs "${__std_ver_pairs}")
  33. # If the compiler version is below the threshold of even having CMake
  34. # support for language standards, then don't bother.
  35. if (CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL "${stdver1}")
  36. if (NOT CMAKE_${lang}_COMPILER_FORCED)
  37. if (NOT CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT OR NOT DEFINED CMAKE_${lang}_EXTENSIONS_COMPUTED_DEFAULT)
  38. message(FATAL_ERROR "CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT and CMAKE_${lang}_EXTENSIONS_COMPUTED_DEFAULT should be set for ${CMAKE_${lang}_COMPILER_ID} (${CMAKE_${lang}_COMPILER}) version ${CMAKE_${lang}_COMPILER_VERSION}")
  39. endif ()
  40. set(CMAKE_${lang}_STANDARD_DEFAULT ${CMAKE_${lang}_STANDARD_COMPUTED_DEFAULT})
  41. set(CMAKE_${lang}_EXTENSIONS_DEFAULT ${CMAKE_${lang}_EXTENSIONS_COMPUTED_DEFAULT})
  42. else ()
  43. list(REVERSE __std_ver_pairs)
  44. foreach (__std_ver_pair IN LISTS __std_ver_pairs)
  45. string(REGEX MATCH "([^ ]+) (.+)" __std_ver_pair "${__std_ver_pair}")
  46. set(__stdver ${CMAKE_MATCH_1})
  47. set(__std ${CMAKE_MATCH_2})
  48. # Compiler id was forced so just guess the defaults.
  49. if (CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL __stdver)
  50. if(NOT DEFINED CMAKE_${lang}_EXTENSIONS_DEFAULT)
  51. # Currently known compilers default to enabling extensions.
  52. set(CMAKE_${lang}_EXTENSIONS_DEFAULT ON)
  53. endif()
  54. if(NOT DEFINED CMAKE_${lang}_STANDARD_DEFAULT)
  55. set(CMAKE_${lang}_STANDARD_DEFAULT ${__std})
  56. endif()
  57. endif ()
  58. unset(__std)
  59. unset(__stdver)
  60. endforeach ()
  61. endif ()
  62. endif ()
  63. unset(__std_ver_pairs)
  64. endmacro()
  65. # Define to allow compile features to be automatically determined
  66. macro(cmake_record_c_compile_features)
  67. set(_result 0)
  68. if(_result EQUAL 0 AND DEFINED CMAKE_C23_STANDARD_COMPILE_OPTION)
  69. _has_compiler_features_c(23)
  70. endif()
  71. if(_result EQUAL 0 AND DEFINED CMAKE_C17_STANDARD_COMPILE_OPTION)
  72. _has_compiler_features_c(17)
  73. endif()
  74. if(_result EQUAL 0 AND DEFINED CMAKE_C11_STANDARD_COMPILE_OPTION)
  75. if(CMAKE_C11_STANDARD__HAS_FULL_SUPPORT)
  76. _has_compiler_features_c(11)
  77. else()
  78. _record_compiler_features_c(11)
  79. endif()
  80. unset(CMAKE_C11_STANDARD__HAS_FULL_SUPPORT)
  81. endif()
  82. if(_result EQUAL 0 AND DEFINED CMAKE_C99_STANDARD_COMPILE_OPTION)
  83. if(CMAKE_C99_STANDARD__HAS_FULL_SUPPORT)
  84. _has_compiler_features_c(99)
  85. else()
  86. _record_compiler_features_c(99)
  87. endif()
  88. unset(CMAKE_C99_STANDARD__HAS_FULL_SUPPORT)
  89. endif()
  90. if(_result EQUAL 0 AND DEFINED CMAKE_C90_STANDARD_COMPILE_OPTION)
  91. if(CMAKE_C90_STANDARD__HAS_FULL_SUPPORT)
  92. _has_compiler_features_c(90)
  93. else()
  94. _record_compiler_features_c(90)
  95. endif()
  96. unset(CMAKE_C90_STANDARD__HAS_FULL_SUPPORT)
  97. endif()
  98. endmacro()
  99. # Define to allow compile features to be automatically determined
  100. macro(cmake_record_cxx_compile_features)
  101. set(_result 0)
  102. if(_result EQUAL 0 AND DEFINED CMAKE_CXX26_STANDARD_COMPILE_OPTION)
  103. _has_compiler_features_cxx(26)
  104. endif()
  105. if(_result EQUAL 0 AND DEFINED CMAKE_CXX23_STANDARD_COMPILE_OPTION)
  106. _has_compiler_features_cxx(23)
  107. endif()
  108. if(_result EQUAL 0 AND DEFINED CMAKE_CXX20_STANDARD_COMPILE_OPTION)
  109. _has_compiler_features_cxx(20)
  110. endif()
  111. if(_result EQUAL 0 AND DEFINED CMAKE_CXX17_STANDARD_COMPILE_OPTION)
  112. _has_compiler_features_cxx(17)
  113. endif()
  114. if(_result EQUAL 0 AND DEFINED CMAKE_CXX14_STANDARD_COMPILE_OPTION)
  115. if(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
  116. _has_compiler_features_cxx(14)
  117. else()
  118. _record_compiler_features_cxx(14)
  119. endif()
  120. unset(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
  121. endif()
  122. if(_result EQUAL 0 AND DEFINED CMAKE_CXX11_STANDARD_COMPILE_OPTION)
  123. if(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
  124. _has_compiler_features_cxx(11)
  125. else()
  126. _record_compiler_features_cxx(11)
  127. endif()
  128. unset(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
  129. endif()
  130. if(_result EQUAL 0 AND DEFINED CMAKE_CXX98_STANDARD_COMPILE_OPTION)
  131. if(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
  132. _has_compiler_features_cxx(98)
  133. else()
  134. _record_compiler_features_cxx(98)
  135. endif()
  136. unset(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
  137. endif()
  138. endmacro()
  139. macro(cmake_record_cuda_compile_features)
  140. set(_result 0)
  141. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA26_STANDARD_COMPILE_OPTION)
  142. _has_compiler_features_cuda(26)
  143. endif()
  144. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA23_STANDARD_COMPILE_OPTION)
  145. _has_compiler_features_cuda(23)
  146. endif()
  147. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA20_STANDARD_COMPILE_OPTION)
  148. _has_compiler_features_cuda(20)
  149. endif()
  150. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA17_STANDARD_COMPILE_OPTION)
  151. _has_compiler_features_cuda(17)
  152. endif()
  153. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA14_STANDARD_COMPILE_OPTION)
  154. if(CMAKE_CUDA14_STANDARD__HAS_FULL_SUPPORT)
  155. _has_compiler_features_cuda(14)
  156. else()
  157. _record_compiler_features_cuda(14)
  158. endif()
  159. unset(CMAKE_CUDA14_STANDARD__HAS_FULL_SUPPORT)
  160. endif()
  161. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA11_STANDARD_COMPILE_OPTION)
  162. if(CMAKE_CUDA11_STANDARD__HAS_FULL_SUPPORT)
  163. _has_compiler_features_cuda(11)
  164. else()
  165. _record_compiler_features_cuda(11)
  166. endif()
  167. unset(CMAKE_CUDA11_STANDARD__HAS_FULL_SUPPORT)
  168. endif()
  169. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA03_STANDARD_COMPILE_OPTION)
  170. if(CMAKE_CUDA03_STANDARD__HAS_FULL_SUPPORT)
  171. _has_compiler_features_cuda(03)
  172. else()
  173. _record_compiler_features_cuda(03)
  174. endif()
  175. unset(CMAKE_CUDA03_STANDARD__HAS_FULL_SUPPORT)
  176. endif()
  177. endmacro()
  178. macro(cmake_record_hip_compile_features)
  179. set(_result 0)
  180. if(_result EQUAL 0 AND DEFINED CMAKE_HIP26_STANDARD_COMPILE_OPTION)
  181. _has_compiler_features_hip(26)
  182. endif()
  183. if(_result EQUAL 0 AND DEFINED CMAKE_HIP23_STANDARD_COMPILE_OPTION)
  184. _has_compiler_features_hip(23)
  185. endif()
  186. if(_result EQUAL 0 AND DEFINED CMAKE_HIP20_STANDARD_COMPILE_OPTION)
  187. _has_compiler_features_hip(20)
  188. endif()
  189. if(_result EQUAL 0 AND DEFINED CMAKE_HIP17_STANDARD_COMPILE_OPTION)
  190. _has_compiler_features_hip(17)
  191. endif()
  192. _has_compiler_features_hip(14)
  193. _has_compiler_features_hip(11)
  194. _has_compiler_features_hip(98)
  195. endmacro()
  196. function(cmake_create_cxx_import_std std variable)
  197. set(_cmake_supported_import_std_features
  198. # Compilers support `import std` in C++20 as an extension. Skip
  199. # for now.
  200. # 20
  201. 23
  202. 26)
  203. list(FIND _cmake_supported_import_std_features "${std}" _cmake_supported_import_std_idx)
  204. if (_cmake_supported_import_std_idx EQUAL "-1")
  205. set("${variable}"
  206. "set(CMAKE_CXX${std}_COMPILER_IMPORT_STD_NOT_FOUND_MESSAGE \"Unsupported C++ standard: C++${std}\")\n"
  207. PARENT_SCOPE)
  208. return ()
  209. endif ()
  210. # If the target exists, skip. A toolchain file may have provided it.
  211. if (TARGET "__CMAKE::CXX${std}")
  212. return ()
  213. endif ()
  214. # The generator must support imported C++ modules.
  215. if (NOT CMAKE_GENERATOR MATCHES "Ninja")
  216. set("${variable}"
  217. "set(CMAKE_CXX${std}_COMPILER_IMPORT_STD_NOT_FOUND_MESSAGE \"Unsupported generator: ${CMAKE_GENERATOR}\")\n"
  218. PARENT_SCOPE)
  219. return ()
  220. endif ()
  221. # Check if the compiler understands how to `import std;`.
  222. include("${CMAKE_ROOT}/Modules/Compiler/${CMAKE_CXX_COMPILER_ID}-CXX-CXXImportStd.cmake" OPTIONAL RESULT_VARIABLE _cmake_import_std_res)
  223. if (NOT _cmake_import_std_res)
  224. set("${variable}"
  225. "set(CMAKE_CXX${std}_COMPILER_IMPORT_STD_NOT_FOUND_MESSAGE \"Toolchain does not support discovering `import std` support\")\n"
  226. PARENT_SCOPE)
  227. return ()
  228. endif ()
  229. if (NOT COMMAND _cmake_cxx_import_std)
  230. set("${variable}"
  231. "set(CMAKE_CXX${std}_COMPILER_IMPORT_STD_NOT_FOUND_MESSAGE \"Toolchain does not provide `import std` discovery command\")\n"
  232. PARENT_SCOPE)
  233. return ()
  234. endif ()
  235. # Check the experimental flag. Check it here to avoid triggering warnings in
  236. # situations that don't support the feature anyways.
  237. set(_cmake_supported_import_std_experimental "")
  238. cmake_language(GET_EXPERIMENTAL_FEATURE_ENABLED
  239. "CxxImportStd"
  240. _cmake_supported_import_std_experimental)
  241. if (NOT _cmake_supported_import_std_experimental)
  242. set("${variable}"
  243. "set(CMAKE_CXX${std}_COMPILER_IMPORT_STD_NOT_FOUND_MESSAGE \"Experimental `import std` support not enabled when detecting toolchain\")\n"
  244. PARENT_SCOPE)
  245. return ()
  246. endif ()
  247. _cmake_cxx_import_std("${std}" target_definition)
  248. string(CONCAT guarded_target_definition
  249. "if (NOT TARGET \"__CMAKE::CXX${std}\")\n"
  250. "${target_definition}"
  251. "endif ()\n"
  252. "if (TARGET \"__CMAKE::CXX${std}\")\n"
  253. " list(APPEND CMAKE_CXX_COMPILER_IMPORT_STD \"${std}\")\n"
  254. "endif ()\n")
  255. set("${variable}" "${guarded_target_definition}" PARENT_SCOPE)
  256. endfunction()