CMakeLists.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if (CMAKE_CXX_COMPILER_ID STREQUAL GNU
  35. AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
  36. # GNU prior to 4.9 does not set any preprocessor define to distinguish
  37. # c++1y from c++11, so CMake does not support c++1y features before GNU 4.9.
  38. list(REMOVE_ITEM CXX_non_features
  39. # GNU 4.8 knows cxx_attributes, but doesn't know [[deprecated]]
  40. # and warns that it is unknown and ignored.
  41. cxx_attribute_deprecated
  42. cxx_binary_literals
  43. cxx_lambda_init_captures
  44. cxx_return_type_deduction
  45. )
  46. endif()
  47. set(C_ext c)
  48. set(C_standard_flag 11)
  49. set(CXX_ext cpp)
  50. set(CXX_standard_flag 14)
  51. foreach(lang CXX C)
  52. if (CMAKE_${lang}_COMPILE_FEATURES)
  53. foreach(feature ${${lang}_non_features})
  54. message("Testing feature : ${feature}")
  55. try_compile(${feature}_works
  56. "${CMAKE_CURRENT_BINARY_DIR}/${feature}_test"
  57. "${CMAKE_CURRENT_SOURCE_DIR}/feature_test.${${lang}_ext}"
  58. COMPILE_DEFINITIONS "-DTEST=${CMAKE_CURRENT_SOURCE_DIR}/${feature}.${${lang}_ext}"
  59. CMAKE_FLAGS "-DCMAKE_${lang}_STANDARD=${${lang}_standard_flag}"
  60. OUTPUT_VARIABLE OUTPUT
  61. )
  62. if (${feature}_works)
  63. message(SEND_ERROR
  64. "Feature ${feature} expected not to work for ${lang} ${CMAKE_${lang}_COMPILER_ID}-${CMAKE_${lang}_COMPILER_VERSION}.
  65. Update the supported features or blacklist it.\n${OUTPUT}")
  66. else()
  67. message("Testing feature : ${feature} -- Fails, as expected.")
  68. endif()
  69. endforeach()
  70. endif()
  71. endforeach()
  72. add_executable(CompileFeatures main.cpp)
  73. set_property(TARGET CompileFeatures
  74. PROPERTY COMPILE_FEATURES "cxx_auto_type"
  75. )
  76. set_property(TARGET CompileFeatures
  77. PROPERTY CXX_STANDARD_REQUIRED TRUE
  78. )
  79. add_executable(GenexCompileFeatures main.cpp)
  80. set_property(TARGET GenexCompileFeatures
  81. PROPERTY COMPILE_FEATURES "$<1:cxx_auto_type>;$<0:not_a_feature>"
  82. )
  83. add_library(iface INTERFACE)
  84. set_property(TARGET iface
  85. PROPERTY INTERFACE_COMPILE_FEATURES "cxx_auto_type"
  86. )
  87. add_executable(IfaceCompileFeatures main.cpp)
  88. target_link_libraries(IfaceCompileFeatures iface)
  89. add_executable(CompileFeaturesGenex genex_test.cpp)
  90. set_property(TARGET CompileFeaturesGenex PROPERTY CXX_STANDARD 11)
  91. target_compile_definitions(CompileFeaturesGenex PRIVATE HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>)
  92. add_executable(CompileFeaturesGenex2 genex_test.cpp)
  93. target_compile_features(CompileFeaturesGenex2 PRIVATE cxx_constexpr)
  94. target_compile_definitions(CompileFeaturesGenex2 PRIVATE HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>)
  95. add_library(noexcept_iface INTERFACE)
  96. target_compile_features(noexcept_iface INTERFACE cxx_noexcept)
  97. add_executable(CompileFeaturesGenex3 genex_test.cpp)
  98. target_link_libraries(CompileFeaturesGenex3 PRIVATE noexcept_iface)
  99. target_compile_definitions(CompileFeaturesGenex3 PRIVATE HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>)