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 14if(__c_feature_check) 15 return() 16endif() 17set(__c_feature_check INCLUDED) 18 19function(c_feature_check FILE) 20 string(TOLOWER ${FILE} FILE) 21 string(TOUPPER ${FILE} VAR) 22 string(TOUPPER "${VAR}_SUPPORTED" FEATURE) 23 if (DEFINED ${VAR}_SUPPORTED) 24 set(${VAR}_SUPPORTED 1 PARENT_SCOPE) 25 return() 26 endif() 27 28 if (NOT DEFINED COMPILE_${FEATURE}) 29 message(STATUS "Performing Test ${FEATURE}") 30 try_compile(COMPILE_${FEATURE} ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/cmake/${FILE}.c) 31 endif() 32 33 if(COMPILE_${FEATURE}) 34 message(STATUS "Performing Test ${FEATURE} -- success") 35 set(${VAR}_SUPPORTED 1 PARENT_SCOPE) 36 else() 37 message(STATUS "Performing Test ${FEATURE} -- failed to compile") 38 endif() 39endfunction() 40