cm_cxx_features.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. include(${CMAKE_CURRENT_LIST_DIR}/cm_message_checks_compat.cmake)
  2. function(cm_check_cxx_feature name)
  3. set(TRY_RUN_FEATURE "${ARGN}")
  4. string(TOUPPER ${name} FEATURE)
  5. if(NOT DEFINED CMake_HAVE_CXX_${FEATURE})
  6. cm_message_checks_compat(
  7. "Checking if compiler supports C++ ${name}"
  8. __checkStart __checkPass __checkFail)
  9. message(${__checkStart})
  10. if(CMAKE_CXX_STANDARD)
  11. set(maybe_cxx_standard -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD})
  12. else()
  13. set(maybe_cxx_standard "")
  14. endif()
  15. if (TRY_RUN_FEATURE)
  16. try_run(CMake_RUN_CXX_${FEATURE} CMake_COMPILE_CXX_${FEATURE}
  17. ${CMAKE_CURRENT_BINARY_DIR}
  18. ${CMAKE_CURRENT_LIST_DIR}/cm_cxx_${name}.cxx
  19. CMAKE_FLAGS ${maybe_cxx_standard}
  20. OUTPUT_VARIABLE OUTPUT
  21. )
  22. if (CMake_RUN_CXX_${FEATURE} EQUAL "0" AND CMake_COMPILE_CXX_${FEATURE})
  23. set(CMake_HAVE_CXX_${FEATURE} ON CACHE INTERNAL "TRY_RUN" FORCE)
  24. else()
  25. set(CMake_HAVE_CXX_${FEATURE} OFF CACHE INTERNAL "TRY_RUN" FORCE)
  26. endif()
  27. else()
  28. try_compile(CMake_HAVE_CXX_${FEATURE}
  29. ${CMAKE_CURRENT_BINARY_DIR}
  30. ${CMAKE_CURRENT_LIST_DIR}/cm_cxx_${name}.cxx
  31. CMAKE_FLAGS ${maybe_cxx_standard}
  32. OUTPUT_VARIABLE OUTPUT
  33. )
  34. endif()
  35. set(check_output "${OUTPUT}")
  36. # Filter out MSBuild output that looks like a warning.
  37. string(REGEX REPLACE " +0 Warning\\(s\\)" "" check_output "${check_output}")
  38. # Filter out MSBuild output that looks like a warning.
  39. string(REGEX REPLACE "[^\n]*warning MSB[0-9][0-9][0-9][0-9][^\n]*" "" check_output "${check_output}")
  40. # Filter out warnings caused by user flags.
  41. string(REGEX REPLACE "[^\n]*warning:[^\n]*-Winvalid-command-line-argument[^\n]*" "" check_output "${check_output}")
  42. # Filter out warnings caused by local configuration.
  43. string(REGEX REPLACE "[^\n]*warning:[^\n]*directory not found for option[^\n]*" "" check_output "${check_output}")
  44. string(REGEX REPLACE "[^\n]*warning:[^\n]*object file compiled with -mlong-branch which is no longer needed[^\n]*" "" check_output "${check_output}")
  45. # Filter out other warnings unrelated to feature checks.
  46. string(REGEX REPLACE "[^\n]*warning:[^\n]*sprintf\\(\\) is often misused, please use snprintf[^\n]*" "" check_output "${check_output}")
  47. # Filter out libhugetlbfs warnings.
  48. string(REGEX REPLACE "[^\n]*libhugetlbfs [^\n]*: WARNING[^\n]*" "" check_output "${check_output}")
  49. # Filter out xcodebuild warnings.
  50. string(REGEX REPLACE "[^\n]* xcodebuild\\[[0-9]*:[0-9]*\\][^\n]*[Ww]arning: [^\n]*" "" check_output "${check_output}")
  51. # Filter out icpc warnings
  52. string(REGEX REPLACE "[^\n]*icpc: command line warning #10121: overriding [^\n]*" "" check_output "${check_output}")
  53. # Filter out ld warnings.
  54. string(REGEX REPLACE "[^\n]*ld: warning: [^\n]*" "" check_output "${check_output}")
  55. # If using the feature causes warnings, treat it as broken/unavailable.
  56. if(check_output MATCHES "(^|[ :])[Ww][Aa][Rr][Nn][Ii][Nn][Gg]")
  57. set(CMake_HAVE_CXX_${FEATURE} OFF CACHE INTERNAL "TRY_COMPILE" FORCE)
  58. endif()
  59. if(CMake_HAVE_CXX_${FEATURE})
  60. message(${__checkPass} "yes")
  61. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  62. "Determining if compiler supports C++ ${name} passed with the following output:\n"
  63. "${OUTPUT}\n"
  64. "\n"
  65. )
  66. else()
  67. message(${__checkFail} "no")
  68. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  69. "Determining if compiler supports C++ ${name} failed with the following output:\n"
  70. "${OUTPUT}\n"
  71. "\n"
  72. )
  73. endif()
  74. endif()
  75. endfunction()
  76. cm_check_cxx_feature(make_unique)
  77. if(CMake_HAVE_CXX_MAKE_UNIQUE)
  78. set(CMake_HAVE_CXX_UNIQUE_PTR 1)
  79. endif()
  80. cm_check_cxx_feature(unique_ptr)
  81. if (NOT CMAKE_CXX_STANDARD LESS "17")
  82. if (NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR)
  83. cm_check_cxx_feature(filesystem TRY_RUN)
  84. else()
  85. # In cross-compiling mode, it is not possible to check implementation bugs
  86. # so rely only on conformance done by compilation
  87. cm_check_cxx_feature(filesystem)
  88. endif()
  89. else()
  90. set(CMake_HAVE_CXX_FILESYSTEM FALSE)
  91. endif()