CXXFeatureCheck.cmake 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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.6+
  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. message("-- Performing Test ${FEATURE}")
  23. try_run(RUN_${FEATURE} COMPILE_${FEATURE}
  24. ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp)
  25. if(RUN_${FEATURE} EQUAL 0)
  26. message("-- Performing Test ${FEATURE} -- success")
  27. set(HAVE_${VAR} 1 PARENT_SCOPE)
  28. add_definitions(-DHAVE_${VAR})
  29. else()
  30. if(NOT COMPILE_${FEATURE})
  31. message("-- Performing Test ${FEATURE} -- failed to compile")
  32. else()
  33. message("-- Performing Test ${FEATURE} -- compiled but failed to run")
  34. endif()
  35. endif()
  36. endfunction()