CMakeLists.txt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. cmake_minimum_required(VERSION 3.1)
  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 "AppleClang"
  29. AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.1)
  30. # AppleClang prior to 5.1 does not set any preprocessor define to distinguish
  31. # c++1y from c++11, so CMake does not support c++1y features before AppleClang 5.1.
  32. list(REMOVE_ITEM CXX_non_features
  33. cxx_attribute_deprecated
  34. cxx_binary_literals
  35. )
  36. endif()
  37. if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"
  38. AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.2)
  39. # AppleClang prior to 4.1 reports false for __has_feature(cxx_local_type_template_args)
  40. # and __has_feature(cxx_unrestricted_unions) but it happens to pass these tests.
  41. list(REMOVE_ITEM CXX_non_features
  42. cxx_local_type_template_args
  43. cxx_unrestricted_unions
  44. )
  45. endif()
  46. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
  47. AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
  48. # The cxx_alignof feature happens to work (for *this* testcase) with
  49. # GNU 4.7, but it is first documented as available with GNU 4.8.
  50. list(REMOVE_ITEM CXX_non_features
  51. cxx_alignof
  52. )
  53. endif()
  54. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
  55. AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
  56. # GNU prior to 4.9 does not set any preprocessor define to distinguish
  57. # c++1y from c++11, so CMake does not support c++1y features before GNU 4.9.
  58. list(REMOVE_ITEM CXX_non_features
  59. # GNU 4.8 knows cxx_attributes, but doesn't know [[deprecated]]
  60. # and warns that it is unknown and ignored.
  61. cxx_attribute_deprecated
  62. cxx_binary_literals
  63. cxx_lambda_init_captures
  64. cxx_return_type_deduction
  65. )
  66. endif()
  67. set(C_ext c)
  68. set(C_standard_flag 11)
  69. set(CXX_ext cpp)
  70. set(CXX_standard_flag 14)
  71. foreach(lang CXX C)
  72. if (CMAKE_${lang}_COMPILE_FEATURES)
  73. foreach(feature ${${lang}_non_features})
  74. message("Testing feature : ${feature}")
  75. try_compile(${feature}_works
  76. "${CMAKE_CURRENT_BINARY_DIR}/${feature}_test"
  77. "${CMAKE_CURRENT_SOURCE_DIR}/feature_test.${${lang}_ext}"
  78. COMPILE_DEFINITIONS "-DTEST=${CMAKE_CURRENT_SOURCE_DIR}/${feature}.${${lang}_ext}"
  79. CMAKE_FLAGS "-DCMAKE_${lang}_STANDARD=${${lang}_standard_flag}"
  80. OUTPUT_VARIABLE OUTPUT
  81. )
  82. if (${feature}_works)
  83. message(SEND_ERROR
  84. "Feature ${feature} expected not to work for ${lang} ${CMAKE_${lang}_COMPILER_ID}-${CMAKE_${lang}_COMPILER_VERSION}.
  85. Update the supported features or blacklist it.\n${OUTPUT}")
  86. else()
  87. message("Testing feature : ${feature} -- Fails, as expected.")
  88. endif()
  89. endforeach()
  90. endif()
  91. endforeach()
  92. if (CMAKE_C_COMPILE_FEATURES)
  93. string(FIND "${CMAKE_C_FLAGS}" "-std=" std_flag_idx)
  94. if (std_flag_idx EQUAL -1)
  95. add_executable(default_dialect_C default_dialect.c)
  96. target_compile_definitions(default_dialect_C PRIVATE
  97. DEFAULT_C11=$<EQUAL:${CMAKE_C_STANDARD_DEFAULT},11>
  98. DEFAULT_C99=$<EQUAL:${CMAKE_C_STANDARD_DEFAULT},99>
  99. DEFAULT_C90=$<EQUAL:${CMAKE_C_STANDARD_DEFAULT},90>
  100. )
  101. endif()
  102. endif()
  103. if (CMAKE_CXX_COMPILE_FEATURES)
  104. string(FIND "${CMAKE_CXX_FLAGS}" "-std=" std_flag_idx)
  105. if (std_flag_idx EQUAL -1)
  106. add_executable(default_dialect default_dialect.cpp)
  107. target_compile_definitions(default_dialect PRIVATE
  108. DEFAULT_CXX14=$<EQUAL:${CMAKE_CXX_STANDARD_DEFAULT},14>
  109. DEFAULT_CXX11=$<EQUAL:${CMAKE_CXX_STANDARD_DEFAULT},11>
  110. DEFAULT_CXX98=$<EQUAL:${CMAKE_CXX_STANDARD_DEFAULT},98>
  111. )
  112. endif()
  113. add_executable(CompileFeatures main.cpp)
  114. set_property(TARGET CompileFeatures
  115. PROPERTY COMPILE_FEATURES "cxx_auto_type"
  116. )
  117. set_property(TARGET CompileFeatures
  118. PROPERTY CXX_STANDARD_REQUIRED TRUE
  119. )
  120. add_executable(GenexCompileFeatures main.cpp)
  121. set_property(TARGET GenexCompileFeatures
  122. PROPERTY COMPILE_FEATURES "$<1:cxx_auto_type>;$<0:not_a_feature>"
  123. )
  124. add_library(iface INTERFACE)
  125. set_property(TARGET iface
  126. PROPERTY INTERFACE_COMPILE_FEATURES "cxx_auto_type"
  127. )
  128. add_executable(IfaceCompileFeatures main.cpp)
  129. target_link_libraries(IfaceCompileFeatures iface)
  130. if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  131. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
  132. add_definitions(
  133. -DEXPECT_OVERRIDE_CONTROL=1
  134. -DEXPECT_INHERITING_CONSTRUCTORS=1
  135. -DEXPECT_FINAL=1
  136. -DEXPECT_INHERITING_CONSTRUCTORS_AND_FINAL=1
  137. )
  138. elseif (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.7)
  139. add_definitions(
  140. -DEXPECT_OVERRIDE_CONTROL=1
  141. -DEXPECT_INHERITING_CONSTRUCTORS=0
  142. -DEXPECT_FINAL=1
  143. -DEXPECT_INHERITING_CONSTRUCTORS_AND_FINAL=0
  144. )
  145. else()
  146. add_definitions(
  147. -DEXPECT_OVERRIDE_CONTROL=0
  148. -DEXPECT_INHERITING_CONSTRUCTORS=0
  149. -DEXPECT_FINAL=0
  150. -DEXPECT_INHERITING_CONSTRUCTORS_AND_FINAL=0
  151. )
  152. endif()
  153. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  154. add_definitions(
  155. -DEXPECT_OVERRIDE_CONTROL=1
  156. -DEXPECT_INHERITING_CONSTRUCTORS=1
  157. -DEXPECT_FINAL=1
  158. -DEXPECT_INHERITING_CONSTRUCTORS_AND_FINAL=1
  159. )
  160. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
  161. if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
  162. add_definitions(
  163. -DEXPECT_OVERRIDE_CONTROL=1
  164. -DEXPECT_INHERITING_CONSTRUCTORS=1
  165. -DEXPECT_FINAL=1
  166. -DEXPECT_INHERITING_CONSTRUCTORS_AND_FINAL=1
  167. )
  168. else()
  169. add_definitions(
  170. -DEXPECT_OVERRIDE_CONTROL=1
  171. -DEXPECT_INHERITING_CONSTRUCTORS=0
  172. -DEXPECT_FINAL=1
  173. -DEXPECT_INHERITING_CONSTRUCTORS_AND_FINAL=0
  174. )
  175. endif()
  176. endif()
  177. add_executable(CompileFeaturesGenex genex_test.cpp)
  178. set_property(TARGET CompileFeaturesGenex PROPERTY CXX_STANDARD 11)
  179. target_compile_definitions(CompileFeaturesGenex PRIVATE
  180. HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>
  181. HAVE_AUTO_TYPE=$<COMPILE_FEATURES:cxx_auto_type>
  182. HAVE_INHERITING_CONSTRUCTORS=$<COMPILE_FEATURES:cxx_inheriting_constructors>
  183. HAVE_FINAL=$<COMPILE_FEATURES:cxx_final>
  184. HAVE_INHERITING_CONSTRUCTORS_AND_FINAL=$<COMPILE_FEATURES:cxx_inheriting_constructors,cxx_final>
  185. )
  186. add_executable(CompileFeaturesGenex2 genex_test.cpp)
  187. target_compile_features(CompileFeaturesGenex2 PRIVATE cxx_static_assert)
  188. target_compile_definitions(CompileFeaturesGenex2 PRIVATE
  189. HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>
  190. HAVE_AUTO_TYPE=$<COMPILE_FEATURES:cxx_auto_type>
  191. HAVE_INHERITING_CONSTRUCTORS=$<COMPILE_FEATURES:cxx_inheriting_constructors>
  192. HAVE_FINAL=$<COMPILE_FEATURES:cxx_final>
  193. HAVE_INHERITING_CONSTRUCTORS_AND_FINAL=$<COMPILE_FEATURES:cxx_inheriting_constructors,cxx_final>
  194. )
  195. add_library(static_assert_iface INTERFACE)
  196. target_compile_features(static_assert_iface INTERFACE cxx_static_assert)
  197. add_executable(CompileFeaturesGenex3 genex_test.cpp)
  198. target_link_libraries(CompileFeaturesGenex3 PRIVATE static_assert_iface)
  199. target_compile_definitions(CompileFeaturesGenex3 PRIVATE
  200. HAVE_OVERRIDE_CONTROL=$<COMPILE_FEATURES:cxx_final,cxx_override>
  201. HAVE_AUTO_TYPE=$<COMPILE_FEATURES:cxx_auto_type>
  202. HAVE_INHERITING_CONSTRUCTORS=$<COMPILE_FEATURES:cxx_inheriting_constructors>
  203. HAVE_FINAL=$<COMPILE_FEATURES:cxx_final>
  204. HAVE_INHERITING_CONSTRUCTORS_AND_FINAL=$<COMPILE_FEATURES:cxx_inheriting_constructors,cxx_final>
  205. )
  206. endif()