CMakeCommonCompilerMacros.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. _record_compiler_features_c(11)
  64. endif()
  65. if(_result EQUAL 0 AND DEFINED CMAKE_C99_STANDARD_COMPILE_OPTION)
  66. _record_compiler_features_c(99)
  67. endif()
  68. if(_result EQUAL 0 AND DEFINED CMAKE_C90_STANDARD_COMPILE_OPTION)
  69. _record_compiler_features_c(90)
  70. endif()
  71. endmacro()
  72. # Define to allow compile features to be automatically determined
  73. macro(cmake_record_cxx_compile_features)
  74. set(_result 0)
  75. if(_result EQUAL 0 AND DEFINED CMAKE_CXX17_STANDARD_COMPILE_OPTION)
  76. _record_compiler_features_cxx(17)
  77. endif()
  78. if(_result EQUAL 0 AND DEFINED CMAKE_CXX14_STANDARD_COMPILE_OPTION)
  79. _record_compiler_features_cxx(14)
  80. endif()
  81. if(_result EQUAL 0 AND DEFINED CMAKE_CXX11_STANDARD_COMPILE_OPTION)
  82. _record_compiler_features_cxx(11)
  83. endif()
  84. if(_result EQUAL 0 AND DEFINED CMAKE_CXX98_STANDARD_COMPILE_OPTION)
  85. _record_compiler_features_cxx(98)
  86. endif()
  87. endmacro()