You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.6 KiB

1 month ago
  1. # - Compile and run code to check for C++ features
  2. #
  3. # This functions compiles a source file under the `cmake` folder
  4. # and adds the corresponding `HAVE_[FILENAME]` flag to the CMake
  5. # environment
  6. #
  7. # cxx_feature_check(<FLAG> [<VARIANT>])
  8. #
  9. # - Example
  10. #
  11. # include(CXXFeatureCheck)
  12. # cxx_feature_check(STD_REGEX)
  13. # Requires CMake 2.8.12+
  14. if(__cxx_feature_check)
  15. return()
  16. endif()
  17. set(__cxx_feature_check INCLUDED)
  18. option(CXXFEATURECHECK_DEBUG OFF)
  19. function(cxx_feature_check FILE)
  20. string(TOLOWER ${FILE} FILE)
  21. string(TOUPPER ${FILE} VAR)
  22. string(TOUPPER "HAVE_${VAR}" FEATURE)
  23. if (DEFINED HAVE_${VAR})
  24. set(HAVE_${VAR} 1 PARENT_SCOPE)
  25. add_definitions(-DHAVE_${VAR})
  26. return()
  27. endif()
  28. set(FEATURE_CHECK_CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS})
  29. if (ARGC GREATER 1)
  30. message(STATUS "Enabling additional flags: ${ARGV1}")
  31. list(APPEND FEATURE_CHECK_CMAKE_FLAGS ${ARGV1})
  32. endif()
  33. if (NOT DEFINED COMPILE_${FEATURE})
  34. if(CMAKE_CROSSCOMPILING)
  35. message(STATUS "Cross-compiling to test ${FEATURE}")
  36. try_compile(COMPILE_${FEATURE}
  37. ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
  38. CXX_STANDARD 11
  39. CXX_STANDARD_REQUIRED ON
  40. CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS}
  41. LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}
  42. OUTPUT_VARIABLE COMPILE_OUTPUT_VAR)
  43. if(COMPILE_${FEATURE})
  44. message(WARNING
  45. "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0")
  46. set(RUN_${FEATURE} 0 CACHE INTERNAL "")
  47. else()
  48. set(RUN_${FEATURE} 1 CACHE INTERNAL "")
  49. endif()
  50. else()
  51. message(STATUS "Compiling and running to test ${FEATURE}")
  52. try_run(RUN_${FEATURE} COMPILE_${FEATURE}
  53. ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
  54. CXX_STANDARD 11
  55. CXX_STANDARD_REQUIRED ON
  56. CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS}
  57. LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}
  58. COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT_VAR)
  59. endif()
  60. endif()
  61. if(RUN_${FEATURE} EQUAL 0)
  62. message(STATUS "Performing Test ${FEATURE} -- success")
  63. set(HAVE_${VAR} 1 PARENT_SCOPE)
  64. add_definitions(-DHAVE_${VAR})
  65. else()
  66. if(NOT COMPILE_${FEATURE})
  67. if(CXXFEATURECHECK_DEBUG)
  68. message(STATUS "Performing Test ${FEATURE} -- failed to compile: ${COMPILE_OUTPUT_VAR}")
  69. else()
  70. message(STATUS "Performing Test ${FEATURE} -- failed to compile")
  71. endif()
  72. else()
  73. message(STATUS "Performing Test ${FEATURE} -- compiled but failed to run")
  74. endif()
  75. endif()
  76. endfunction()