FeatureTesting.cmake 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. macro(_record_compiler_features lang compile_flags feature_list)
  2. include("${CMAKE_ROOT}/Modules/Compiler/${CMAKE_${lang}_COMPILER_ID}-${lang}-FeatureTests.cmake" OPTIONAL)
  3. string(TOLOWER ${lang} lang_lc)
  4. file(REMOVE "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.bin")
  5. set(_content "
  6. const char features[] = {\"\\n\"\n")
  7. get_property(known_features GLOBAL PROPERTY CMAKE_${lang}_KNOWN_FEATURES)
  8. foreach(feature ${known_features})
  9. if (_cmake_feature_test_${feature})
  10. if (${_cmake_feature_test_${feature}} STREQUAL 1)
  11. set(_feature_condition "\"1\" ")
  12. else()
  13. set(_feature_condition "#if ${_cmake_feature_test_${feature}}\n\"1\"\n#else\n\"0\"\n#endif\n")
  14. endif()
  15. string(APPEND _content
  16. "\"${lang}_FEATURE:\"\n${_feature_condition}\"${feature}\\n\"\n")
  17. endif()
  18. endforeach()
  19. string(APPEND _content
  20. "\n};\n\nint main(int argc, char** argv) { (void)argv; return features[argc]; }\n")
  21. if(CMAKE_${lang}_LINK_WITH_STANDARD_COMPILE_OPTION)
  22. # This toolchain requires use of the language standard flag
  23. # when linking in order to use the matching standard library.
  24. set(compile_flags_for_link "${compile_flags}")
  25. else()
  26. set(compile_flags_for_link "")
  27. endif()
  28. try_compile(CMAKE_${lang}_FEATURE_TEST
  29. SOURCE_FROM_VAR "feature_tests.${lang_lc}" _content
  30. COMPILE_DEFINITIONS "${compile_flags}"
  31. LINK_LIBRARIES "${compile_flags_for_link}"
  32. COPY_FILE "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.bin"
  33. COPY_FILE_ERROR _copy_error
  34. __CMAKE_INTERNAL FEATURE_TESTING
  35. )
  36. if(NOT CMAKE_${lang}_FEATURE_TEST)
  37. set(_result 255)
  38. elseif(_copy_error)
  39. set(_result 255)
  40. message(WARNING "${_copy_error}")
  41. else()
  42. set(_result 0)
  43. endif()
  44. unset(CMAKE_${lang}_FEATURE_TEST CACHE)
  45. unset(compile_flags_for_link)
  46. if (_result EQUAL 0)
  47. if(EXISTS "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.bin")
  48. cmake_policy(PUSH)
  49. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  50. file(STRINGS "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.bin"
  51. features REGEX "${lang}_FEATURE:.*")
  52. cmake_policy(POP)
  53. foreach(info ${features})
  54. string(REPLACE "${lang}_FEATURE:" "" info ${info})
  55. string(SUBSTRING ${info} 0 1 has_feature)
  56. if(has_feature)
  57. string(REGEX REPLACE "^1" "" feature ${info})
  58. list(APPEND ${feature_list} ${feature})
  59. endif()
  60. endforeach()
  61. endif()
  62. endif()
  63. endmacro()
  64. macro(_record_compiler_features_c std)
  65. list(APPEND CMAKE_C${std}_COMPILE_FEATURES c_std_${std})
  66. get_property(lang_level_has_features GLOBAL PROPERTY CMAKE_C${std}_KNOWN_FEATURES)
  67. if(lang_level_has_features)
  68. _record_compiler_features(C "${CMAKE_C${std}_STANDARD_COMPILE_OPTION}" CMAKE_C${std}_COMPILE_FEATURES)
  69. endif()
  70. unset(lang_level_has_features)
  71. endmacro()
  72. macro(_record_compiler_features_cxx std)
  73. list(APPEND CMAKE_CXX${std}_COMPILE_FEATURES cxx_std_${std})
  74. get_property(lang_level_has_features GLOBAL PROPERTY CMAKE_CXX${std}_KNOWN_FEATURES)
  75. if(lang_level_has_features)
  76. _record_compiler_features(CXX "${CMAKE_CXX${std}_STANDARD_COMPILE_OPTION}" CMAKE_CXX${std}_COMPILE_FEATURES)
  77. endif()
  78. unset(lang_level_has_features)
  79. endmacro()
  80. macro(_record_compiler_features_cuda std)
  81. list(APPEND CMAKE_CUDA${std}_COMPILE_FEATURES cuda_std_${std})
  82. get_property(lang_level_has_features GLOBAL PROPERTY CMAKE_CUDA${std}_KNOWN_FEATURES)
  83. if(lang_level_has_features)
  84. _record_compiler_features(CUDA "${CMAKE_CUDA${std}_STANDARD_COMPILE_OPTION}" CMAKE_CUDA${std}_COMPILE_FEATURES)
  85. endif()
  86. unset(lang_level_has_features)
  87. endmacro()
  88. macro(_record_compiler_features_hip std)
  89. list(APPEND CMAKE_HIP${std}_COMPILE_FEATURES hip_std_${std})
  90. get_property(lang_level_has_features GLOBAL PROPERTY CMAKE_HIP${std}_KNOWN_FEATURES)
  91. if(lang_level_has_features)
  92. _record_compiler_features(HIP "${CMAKE_HIP${std}_STANDARD_COMPILE_OPTION}" CMAKE_HIP${std}_COMPILE_FEATURES)
  93. endif()
  94. unset(lang_level_has_features)
  95. endmacro()
  96. macro(_has_compiler_features lang level compile_flags feature_list)
  97. # presume all known features are supported
  98. get_property(known_features GLOBAL PROPERTY CMAKE_${lang}${level}_KNOWN_FEATURES)
  99. list(APPEND ${feature_list} ${known_features})
  100. endmacro()
  101. macro(_has_compiler_features_c std)
  102. list(APPEND CMAKE_C${std}_COMPILE_FEATURES c_std_${std})
  103. _has_compiler_features(C ${std} "${CMAKE_C${std}_STANDARD_COMPILE_OPTION}" CMAKE_C${std}_COMPILE_FEATURES)
  104. endmacro()
  105. macro(_has_compiler_features_cxx std)
  106. list(APPEND CMAKE_CXX${std}_COMPILE_FEATURES cxx_std_${std})
  107. _has_compiler_features(CXX ${std} "${CMAKE_CXX${std}_STANDARD_COMPILE_OPTION}" CMAKE_CXX${std}_COMPILE_FEATURES)
  108. endmacro()
  109. macro(_has_compiler_features_cuda std)
  110. list(APPEND CMAKE_CUDA${std}_COMPILE_FEATURES cuda_std_${std})
  111. _has_compiler_features(CUDA ${std} "${CMAKE_CUDA${std}_STANDARD_COMPILE_OPTION}" CMAKE_CUDA${std}_COMPILE_FEATURES)
  112. endmacro()
  113. macro(_has_compiler_features_hip std)
  114. list(APPEND CMAKE_HIP${std}_COMPILE_FEATURES hip_std_${std})
  115. _has_compiler_features(HIP ${std} "${CMAKE_HIP${std}_STANDARD_COMPILE_OPTION}" CMAKE_HIP${std}_COMPILE_FEATURES)
  116. endmacro()