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.8.12+
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    set(HAVE_${VAR} 1 PARENT_SCOPE)
26    add_definitions(-DHAVE_${VAR})
27    return()
28  endif()
29
30  message("-- Performing Test ${FEATURE}")
31  if(CMAKE_CROSSCOMPILING)
32    try_compile(COMPILE_${FEATURE}
33            ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
34            CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
35            LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
36    if(COMPILE_${FEATURE})
37      message(WARNING
38            "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0")
39      set(RUN_${FEATURE} 0)
40    else()
41      set(RUN_${FEATURE} 1)
42    endif()
43  else()
44    message("-- Performing Test ${FEATURE}")
45    try_run(RUN_${FEATURE} COMPILE_${FEATURE}
46            ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
47            CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
48            LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
49  endif()
50
51  if(RUN_${FEATURE} EQUAL 0)
52    message("-- Performing Test ${FEATURE} -- success")
53    set(HAVE_${VAR} 1 PARENT_SCOPE)
54    add_definitions(-DHAVE_${VAR})
55  else()
56    if(NOT COMPILE_${FEATURE})
57      message("-- Performing Test ${FEATURE} -- failed to compile")
58    else()
59      message("-- Performing Test ${FEATURE} -- compiled but failed to run")
60    endif()
61  endif()
62endfunction()
63