Home | History | Annotate | Download | only in cmake
      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 
     15 if(__cxx_feature_check)
     16   return()
     17 endif()
     18 set(__cxx_feature_check INCLUDED)
     19 
     20 function(cxx_feature_check FILE)
     21   string(TOLOWER ${FILE} FILE)
     22   string(TOUPPER ${FILE} VAR)
     23   string(TOUPPER "HAVE_${VAR}" FEATURE)
     24   message("-- Performing Test ${FEATURE}")
     25   try_run(RUN_${FEATURE} COMPILE_${FEATURE}
     26           ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp)
     27   if(RUN_${FEATURE} EQUAL 0)
     28     message("-- Performing Test ${FEATURE} -- success")
     29     set(HAVE_${VAR} 1 PARENT_SCOPE)
     30     add_definitions(-DHAVE_${VAR})
     31   else()
     32     if(NOT COMPILE_${FEATURE})
     33       message("-- Performing Test ${FEATURE} -- failed to compile")
     34     else()
     35       message("-- Performing Test ${FEATURE} -- compiled but failed to run")
     36     endif()
     37   endif()
     38 endfunction()
     39 
     40