CMakeCommonCompilerMacros.cmake 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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()