FeatureTesting.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.${lang_lc}" "
  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. file(APPEND "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.${lang_lc}" "\"${lang}_FEATURE:\"\n${_feature_condition}\"${feature}\\n\"\n")
  16. endif()
  17. endforeach()
  18. file(APPEND "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.${lang_lc}"
  19. "\n};\n\nint main(int argc, char** argv) { (void)argv; return features[argc]; }\n")
  20. if(CMAKE_${lang}_LINK_WITH_STANDARD_COMPILE_OPTION)
  21. # This toolchain requires use of the language standard flag
  22. # when linking in order to use the matching standard library.
  23. set(compile_flags_for_link "${compile_flags}")
  24. else()
  25. set(compile_flags_for_link "")
  26. endif()
  27. try_compile(CMAKE_${lang}_FEATURE_TEST
  28. ${CMAKE_BINARY_DIR} "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.${lang_lc}"
  29. COMPILE_DEFINITIONS "${compile_flags}"
  30. LINK_LIBRARIES "${compile_flags_for_link}"
  31. OUTPUT_VARIABLE _output
  32. COPY_FILE "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.bin"
  33. COPY_FILE_ERROR _copy_error
  34. )
  35. if(CMAKE_${lang}_FEATURE_TEST AND NOT _copy_error)
  36. set(_result 0)
  37. else()
  38. set(_result 255)
  39. endif()
  40. unset(CMAKE_${lang}_FEATURE_TEST CACHE)
  41. unset(compile_flags_for_link)
  42. if (_result EQUAL 0)
  43. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  44. "\n\nDetecting ${lang} [${compile_flags}] compiler features compiled with the following output:\n${_output}\n\n")
  45. if(EXISTS "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.bin")
  46. file(STRINGS "${CMAKE_BINARY_DIR}/CMakeFiles/feature_tests.bin"
  47. features REGEX "${lang}_FEATURE:.*")
  48. foreach(info ${features})
  49. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  50. " Feature record: ${info}\n")
  51. string(REPLACE "${lang}_FEATURE:" "" info ${info})
  52. string(SUBSTRING ${info} 0 1 has_feature)
  53. if(has_feature)
  54. string(REGEX REPLACE "^1" "" feature ${info})
  55. list(APPEND ${feature_list} ${feature})
  56. endif()
  57. endforeach()
  58. endif()
  59. else()
  60. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  61. "Detecting ${lang} [${compile_flags}] compiler features failed to compile with the following output:\n${_output}\n${_copy_error}\n\n")
  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(_has_compiler_features lang level compile_flags feature_list)
  81. # presume all known features are supported
  82. get_property(known_features GLOBAL PROPERTY CMAKE_${lang}${level}_KNOWN_FEATURES)
  83. list(APPEND ${feature_list} ${known_features})
  84. endmacro()
  85. macro(_has_compiler_features_c std)
  86. list(APPEND CMAKE_C${std}_COMPILE_FEATURES c_std_${std})
  87. _has_compiler_features(C ${std} "${CMAKE_C${std}_STANDARD_COMPILE_OPTION}" CMAKE_C${std}_COMPILE_FEATURES)
  88. endmacro()
  89. macro(_has_compiler_features_cxx std)
  90. list(APPEND CMAKE_CXX${std}_COMPILE_FEATURES cxx_std_${std})
  91. _has_compiler_features(CXX ${std} "${CMAKE_CXX${std}_STANDARD_COMPILE_OPTION}" CMAKE_CXX${std}_COMPILE_FEATURES)
  92. endmacro()