CMakeCommonCompilerMacros.cmake 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_CXX20_STANDARD_COMPILE_OPTION)
  91. if(CMAKE_CXX20_STANDARD__HAS_FULL_SUPPORT)
  92. _has_compiler_features_cxx(20)
  93. else()
  94. _record_compiler_features_cxx(20)
  95. endif()
  96. unset(CMAKE_CXX20_STANDARD__HAS_FULL_SUPPORT)
  97. endif()
  98. if(_result EQUAL 0 AND DEFINED CMAKE_CXX17_STANDARD_COMPILE_OPTION)
  99. if(CMAKE_CXX17_STANDARD__HAS_FULL_SUPPORT)
  100. _has_compiler_features_cxx(17)
  101. else()
  102. _record_compiler_features_cxx(17)
  103. endif()
  104. unset(CMAKE_CXX17_STANDARD__HAS_FULL_SUPPORT)
  105. endif()
  106. if(_result EQUAL 0 AND DEFINED CMAKE_CXX14_STANDARD_COMPILE_OPTION)
  107. if(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
  108. _has_compiler_features_cxx(14)
  109. else()
  110. _record_compiler_features_cxx(14)
  111. endif()
  112. unset(CMAKE_CXX14_STANDARD__HAS_FULL_SUPPORT)
  113. endif()
  114. if(_result EQUAL 0 AND DEFINED CMAKE_CXX11_STANDARD_COMPILE_OPTION)
  115. if(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
  116. _has_compiler_features_cxx(11)
  117. else()
  118. _record_compiler_features_cxx(11)
  119. endif()
  120. unset(CMAKE_CXX11_STANDARD__HAS_FULL_SUPPORT)
  121. endif()
  122. if(_result EQUAL 0 AND DEFINED CMAKE_CXX98_STANDARD_COMPILE_OPTION)
  123. if(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
  124. _has_compiler_features_cxx(98)
  125. else()
  126. _record_compiler_features_cxx(98)
  127. endif()
  128. unset(CMAKE_CXX98_STANDARD__HAS_FULL_SUPPORT)
  129. endif()
  130. endmacro()