CMakeCommonCompilerMacros.cmake 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst 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_cxx_find_modules_json)
  197. # The generator must support imported C++ modules.
  198. if (NOT CMAKE_GENERATOR MATCHES "Ninja")
  199. set(CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE "Unsupported generator: ${CMAKE_GENERATOR}" PARENT_SCOPE)
  200. return ()
  201. endif ()
  202. # Check if the compiler understands how to `import std;`.
  203. include("${CMAKE_ROOT}/Modules/Compiler/${CMAKE_CXX_COMPILER_ID}-CXX-CXXImportStd.cmake" OPTIONAL RESULT_VARIABLE _cmake_import_std_res)
  204. if (NOT _cmake_import_std_res)
  205. set(CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE "Toolchain does not support discovering module metadata" PARENT_SCOPE)
  206. return ()
  207. endif ()
  208. if (NOT COMMAND _cmake_cxx_find_modules_json)
  209. set(CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE "Toolchain does not provide module metadata discovery command" PARENT_SCOPE)
  210. return ()
  211. endif ()
  212. # Check the experimental flag. Check it here to avoid triggering warnings in
  213. # situations that don't support the feature anyways.
  214. set(_cmake_supported_import_std_experimental "")
  215. cmake_language(GET_EXPERIMENTAL_FEATURE_ENABLED
  216. "CxxImportStd"
  217. _cmake_supported_import_std_experimental)
  218. if (NOT _cmake_supported_import_std_experimental)
  219. set(CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE "Experimental `import std` support not enabled when detecting toolchain; it must be set before `CXX` is enabled (usually a `project()` call)" PARENT_SCOPE)
  220. return ()
  221. endif ()
  222. _cmake_cxx_find_modules_json()
  223. set(CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE "${CMAKE_CXX_COMPILER_IMPORT_STD_ERROR_MESSAGE}" PARENT_SCOPE)
  224. set(CMAKE_CXX_STDLIB_MODULES_JSON "${CMAKE_CXX_STDLIB_MODULES_JSON}" PARENT_SCOPE)
  225. endfunction()