CMakeLists.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. cmake_minimum_required(VERSION 3.0)
  2. project(CompileFeatures)
  3. if (NOT CMAKE_C_COMPILE_FEATURES AND NOT CMAKE_CXX_COMPILE_FEATURES)
  4. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp"
  5. "int main(int,char**) { return 0; }\n"
  6. )
  7. add_executable(CompileFeatures "${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp")
  8. return()
  9. endif()
  10. macro(run_test feature lang)
  11. if (";${CMAKE_${lang}_COMPILE_FEATURES};" MATCHES ${feature})
  12. add_library(test_${feature} OBJECT ${feature})
  13. set_property(TARGET test_${feature}
  14. PROPERTY COMPILE_FEATURES "${feature}"
  15. )
  16. else()
  17. list(APPEND ${lang}_non_features ${feature})
  18. endif()
  19. endmacro()
  20. get_property(c_features GLOBAL PROPERTY CMAKE_C_KNOWN_FEATURES)
  21. foreach(feature ${c_features})
  22. run_test(${feature} C)
  23. endforeach()
  24. get_property(cxx_features GLOBAL PROPERTY CMAKE_CXX_KNOWN_FEATURES)
  25. foreach(feature ${cxx_features})
  26. run_test(${feature} CXX)
  27. endforeach()
  28. if (CMAKE_CXX_COMPILER_ID STREQUAL GNU
  29. AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
  30. list(REMOVE_ITEM CXX_non_features
  31. cxx_alignof
  32. )
  33. endif()
  34. set(C_ext c)
  35. set(C_standard_flag 11)
  36. set(CXX_ext cpp)
  37. set(CXX_standard_flag 11)
  38. foreach(lang CXX C)
  39. if (CMAKE_${lang}_COMPILE_FEATURES)
  40. foreach(feature ${${lang}_non_features})
  41. message("Testing feature : ${feature}")
  42. try_compile(${feature}_works
  43. "${CMAKE_CURRENT_BINARY_DIR}/${feature}_test"
  44. "${CMAKE_CURRENT_SOURCE_DIR}/feature_test.${${lang}_ext}"
  45. COMPILE_DEFINITIONS "-DTEST=${CMAKE_CURRENT_SOURCE_DIR}/${feature}.${${lang}_ext}"
  46. CMAKE_FLAGS "-DCMAKE_${lang}_STANDARD=${${lang}_standard_flag}"
  47. OUTPUT_VARIABLE OUTPUT
  48. )
  49. if (${feature}_works)
  50. message(SEND_ERROR
  51. "Feature ${feature} expected not to work for ${lang} ${CMAKE_${lang}_COMPILER_ID}-${CMAKE_${lang}_COMPILER_VERSION}.
  52. Update the supported features or blacklist it.\n${OUTPUT}")
  53. else()
  54. message("Testing feature : ${feature} -- Fails, as expected.")
  55. endif()
  56. endforeach()
  57. endif()
  58. endforeach()
  59. add_executable(CompileFeatures main.cpp)
  60. set_property(TARGET CompileFeatures
  61. PROPERTY COMPILE_FEATURES "cxx_auto_type"
  62. )
  63. set_property(TARGET CompileFeatures
  64. PROPERTY CXX_STANDARD_REQUIRED TRUE
  65. )
  66. add_executable(GenexCompileFeatures main.cpp)
  67. set_property(TARGET GenexCompileFeatures
  68. PROPERTY COMPILE_FEATURES "$<1:cxx_auto_type>;$<0:not_a_feature>"
  69. )
  70. add_library(iface INTERFACE)
  71. set_property(TARGET iface
  72. PROPERTY INTERFACE_COMPILE_FEATURES "cxx_auto_type"
  73. )
  74. add_executable(IfaceCompileFeatures main.cpp)
  75. target_link_libraries(IfaceCompileFeatures iface)
  76. add_executable(CompileFeaturesGenex genex_test.cpp)
  77. set_property(TARGET CompileFeaturesGenex PROPERTY CXX_STANDARD 11)
  78. target_compile_definitions(CompileFeaturesGenex PRIVATE HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>)
  79. add_executable(CompileFeaturesGenex2 genex_test.cpp)
  80. target_compile_features(CompileFeaturesGenex2 PRIVATE cxx_constexpr)
  81. target_compile_definitions(CompileFeaturesGenex2 PRIVATE HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>)
  82. add_library(noexcept_iface INTERFACE)
  83. target_compile_features(noexcept_iface INTERFACE cxx_noexcept)
  84. add_executable(CompileFeaturesGenex3 genex_test.cpp)
  85. target_link_libraries(CompileFeaturesGenex3 PRIVATE noexcept_iface)
  86. target_compile_definitions(CompileFeaturesGenex3 PRIVATE HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>)