CXXFeatureCheck.cmake 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. function(cxx_feature_check FILE)
  19. string(TOLOWER ${FILE} FILE)
  20. string(TOUPPER ${FILE} VAR)
  21. string(TOUPPER "HAVE_${VAR}" FEATURE)
  22. if (DEFINED HAVE_${VAR})
  23. set(HAVE_${VAR} 1 PARENT_SCOPE)
  24. add_definitions(-DHAVE_${VAR})
  25. return()
  26. endif()
  27. if (NOT DEFINED COMPILE_${FEATURE})
  28. message(STATUS "Performing Test ${FEATURE}")
  29. if(CMAKE_CROSSCOMPILING)
  30. try_compile(COMPILE_${FEATURE}
  31. ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
  32. CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
  33. LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
  34. if(COMPILE_${FEATURE})
  35. message(WARNING
  36. "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0")
  37. set(RUN_${FEATURE} 0)
  38. else()
  39. set(RUN_${FEATURE} 1)
  40. endif()
  41. else()
  42. message(STATUS "Performing Test ${FEATURE}")
  43. try_run(RUN_${FEATURE} COMPILE_${FEATURE}
  44. ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
  45. CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
  46. LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
  47. endif()
  48. endif()
  49. if(RUN_${FEATURE} EQUAL 0)
  50. message(STATUS "Performing Test ${FEATURE} -- success")
  51. set(HAVE_${VAR} 1 PARENT_SCOPE)
  52. add_definitions(-DHAVE_${VAR})
  53. else()
  54. if(NOT COMPILE_${FEATURE})
  55. message(STATUS "Performing Test ${FEATURE} -- failed to compile")
  56. else()
  57. message(STATUS "Performing Test ${FEATURE} -- compiled but failed to run")
  58. endif()
  59. endif()
  60. endfunction()