blob: 3059024dc08fefe6d81e23d6be5732073837b10f [file] [log] [blame]
Eric Fiselierb08d8b12016-07-19 23:07:03 +00001# - 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
15if(__cxx_feature_check)
16 return()
17endif()
18set(__cxx_feature_check INCLUDED)
19
20function(cxx_feature_check FILE)
21 string(TOLOWER ${FILE} FILE)
22 string(TOUPPER ${FILE} VAR)
23 string(TOUPPER "HAVE_${VAR}" FEATURE)
24 if (DEFINED HAVE_${VAR})
25 return()
26 endif()
27 message("-- Performing Test ${FEATURE}")
28 try_run(RUN_${FEATURE} COMPILE_${FEATURE}
29 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp)
30 if(RUN_${FEATURE} EQUAL 0)
31 message("-- Performing Test ${FEATURE} -- success")
32 set(HAVE_${VAR} 1 CACHE INTERNAL "Feature test for ${FILE}" PARENT_SCOPE)
33 add_definitions(-DHAVE_${VAR})
34 else()
35 if(NOT COMPILE_${FEATURE})
36 message("-- Performing Test ${FEATURE} -- failed to compile")
37 else()
38 message("-- Performing Test ${FEATURE} -- compiled but failed to run")
39 endif()
40 endif()
41endfunction()
42