CFeatureCheck.cmake 1.0 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. # c_feature_check(<FLAG> [<VARIANT>])
  8. #
  9. # - Example
  10. #
  11. # include(CFeatureCheck)
  12. # c_feature_check(VLA)
  13. if(__c_feature_check)
  14. return()
  15. endif()
  16. set(__c_feature_check INCLUDED)
  17. function(c_feature_check FILE)
  18. string(TOLOWER ${FILE} FILE)
  19. string(TOUPPER ${FILE} VAR)
  20. string(TOUPPER "${VAR}_SUPPORTED" FEATURE)
  21. if (DEFINED ${VAR}_SUPPORTED)
  22. set(${VAR}_SUPPORTED 1 PARENT_SCOPE)
  23. return()
  24. endif()
  25. if (NOT DEFINED COMPILE_${FEATURE})
  26. message(STATUS "Performing Test ${FEATURE}")
  27. try_compile(COMPILE_${FEATURE} ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/cmake/${FILE}.c)
  28. endif()
  29. if(COMPILE_${FEATURE})
  30. message(STATUS "Performing Test ${FEATURE} -- success")
  31. set(${VAR}_SUPPORTED 1 PARENT_SCOPE)
  32. else()
  33. message(STATUS "Performing Test ${FEATURE} -- failed to compile")
  34. endif()
  35. endfunction()