CMakeCommonCompilerMacros.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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)
  38. message(FATAL_ERROR "CMAKE_${lang}_STANDARD_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. else ()
  42. list(REVERSE __std_ver_pairs)
  43. foreach (__std_ver_pair IN LISTS __std_ver_pairs)
  44. string(REGEX MATCH "([^ ]+) (.+)" __std_ver_pair "${__std_ver_pair}")
  45. set(__stdver ${CMAKE_MATCH_1})
  46. set(__std ${CMAKE_MATCH_2})
  47. if (CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL __stdver AND
  48. NOT DEFINED CMAKE_${lang}_STANDARD_DEFAULT)
  49. # Compiler id was forced so just guess the default standard level.
  50. set(CMAKE_${lang}_STANDARD_DEFAULT ${__std})
  51. endif ()
  52. unset(__std)
  53. unset(__stdver)
  54. endforeach ()
  55. endif ()
  56. endif ()
  57. unset(__std_ver_pairs)
  58. endmacro()
  59. # Define to allow compile features to be automatically determined
  60. macro(cmake_record_c_compile_features)
  61. set(_result 0)
  62. if(_result EQUAL 0 AND DEFINED CMAKE_C11_STANDARD_COMPILE_OPTION)
  63. if(CMAKE_C11_STANDARD__HAS_FULL_SUPPORT)
  64. _has_compiler_features_c(11)
  65. else()
  66. _record_compiler_features_c(11)
  67. endif()
  68. unset(CMAKE_C11_STANDARD__HAS_FULL_SUPPORT)
  69. endif()
  70. if(_result EQUAL 0 AND DEFINED CMAKE_C99_STANDARD_COMPILE_OPTION)
  71. if(CMAKE_C99_STANDARD__HAS_FULL_SUPPORT)
  72. _has_compiler_features_c(99)
  73. else()
  74. _record_compiler_features_c(99)
  75. endif()
  76. unset(CMAKE_C99_STANDARD__HAS_FULL_SUPPORT)
  77. endif()
  78. if(_result EQUAL 0 AND DEFINED CMAKE_C90_STANDARD_COMPILE_OPTION)
  79. if(CMAKE_C90_STANDARD__HAS_FULL_SUPPORT)
  80. _has_compiler_features_c(90)
  81. else()
  82. _record_compiler_features_c(90)
  83. endif()
  84. unset(CMAKE_C90_STANDARD__HAS_FULL_SUPPORT)
  85. endif()
  86. endmacro()
  87. # Define to allow compile features to be automatically determined
  88. macro(cmake_record_cxx_compile_features)
  89. set(_result 0)
  90. if(_result EQUAL 0 AND DEFINED CMAKE_CXX23_STANDARD_COMPILE_OPTION)
  91. _has_compiler_features_cxx(23)
  92. endif()
  93. if(_result EQUAL 0 AND DEFINED CMAKE_CXX20_STANDARD_COMPILE_OPTION)
  94. _has_compiler_features_cxx(20)
  95. endif()
  96. if(_result EQUAL 0 AND DEFINED CMAKE_CXX17_STANDARD_COMPILE_OPTION)
  97. _has_compiler_features_cxx(17)
  98. endif()
  99. if(_result EQUAL 0 AND DEFINED CMAKE_CXX14_STANDARD_COMPILE_OPTION)
  100. if(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
  101. _has_compiler_features_cxx(14)
  102. else()
  103. _record_compiler_features_cxx(14)
  104. endif()
  105. unset(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
  106. endif()
  107. if(_result EQUAL 0 AND DEFINED CMAKE_CXX11_STANDARD_COMPILE_OPTION)
  108. if(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
  109. _has_compiler_features_cxx(11)
  110. else()
  111. _record_compiler_features_cxx(11)
  112. endif()
  113. unset(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
  114. endif()
  115. if(_result EQUAL 0 AND DEFINED CMAKE_CXX98_STANDARD_COMPILE_OPTION)
  116. if(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
  117. _has_compiler_features_cxx(98)
  118. else()
  119. _record_compiler_features_cxx(98)
  120. endif()
  121. unset(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
  122. endif()
  123. endmacro()
  124. macro(cmake_record_cuda_compile_features)
  125. set(_result 0)
  126. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA23_STANDARD_COMPILE_OPTION)
  127. _has_compiler_features_cuda(23)
  128. endif()
  129. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA20_STANDARD_COMPILE_OPTION)
  130. _has_compiler_features_cuda(20)
  131. endif()
  132. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA17_STANDARD_COMPILE_OPTION)
  133. _has_compiler_features_cuda(17)
  134. endif()
  135. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA14_STANDARD_COMPILE_OPTION)
  136. if(CMAKE_CUDA14_STANDARD__HAS_FULL_SUPPORT)
  137. _has_compiler_features_cuda(14)
  138. else()
  139. _record_compiler_features_cuda(14)
  140. endif()
  141. unset(CMAKE_CUDA14_STANDARD__HAS_FULL_SUPPORT)
  142. endif()
  143. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA11_STANDARD_COMPILE_OPTION)
  144. if(CMAKE_CUDA11_STANDARD__HAS_FULL_SUPPORT)
  145. _has_compiler_features_cuda(11)
  146. else()
  147. _record_compiler_features_cuda(11)
  148. endif()
  149. unset(CMAKE_CUDA11_STANDARD__HAS_FULL_SUPPORT)
  150. endif()
  151. if(_result EQUAL 0 AND DEFINED CMAKE_CUDA03_STANDARD_COMPILE_OPTION)
  152. if(CMAKE_CUDA03_STANDARD__HAS_FULL_SUPPORT)
  153. _has_compiler_features_cuda(03)
  154. else()
  155. _record_compiler_features_cuda(03)
  156. endif()
  157. unset(CMAKE_CUDA03_STANDARD__HAS_FULL_SUPPORT)
  158. endif()
  159. endmacro()