Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / cmake / CXXFeatureCheck.cmake
blob62e6741fe3de0bb2bcf006ca0724e534d326dad0
1 # - Compile and run code to check for C++ features
3 # This functions compiles a source file under the `cmake` folder
4 # and adds the corresponding `HAVE_[FILENAME]` flag to the CMake
5 # environment
7 #  cxx_feature_check(<FLAG> [<VARIANT>])
9 # - Example
11 # include(CXXFeatureCheck)
12 # cxx_feature_check(STD_REGEX)
13 # Requires CMake 2.8.12+
15 if(__cxx_feature_check)
16   return()
17 endif()
18 set(__cxx_feature_check INCLUDED)
20 function(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()
30   if (ARGC GREATER 1)
31     message(STATUS "Enabling additional flags: ${ARGV1}")
32     list(APPEND BENCHMARK_CXX_LINKER_FLAGS ${ARGV1})
33   endif()
35   if (NOT DEFINED COMPILE_${FEATURE})
36     message(STATUS "Performing Test ${FEATURE}")
37     if(CMAKE_CROSSCOMPILING)
38       try_compile(COMPILE_${FEATURE}
39               ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
40               CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
41               LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
42       if(COMPILE_${FEATURE})
43         message(WARNING
44               "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0")
45         set(RUN_${FEATURE} 0 CACHE INTERNAL "")
46       else()
47         set(RUN_${FEATURE} 1 CACHE INTERNAL "")
48       endif()
49     else()
50       message(STATUS "Performing Test ${FEATURE}")
51       try_run(RUN_${FEATURE} COMPILE_${FEATURE}
52               ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
53               CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
54               LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
55     endif()
56   endif()
58   if(RUN_${FEATURE} EQUAL 0)
59     message(STATUS "Performing Test ${FEATURE} -- success")
60     set(HAVE_${VAR} 1 PARENT_SCOPE)
61     add_definitions(-DHAVE_${VAR})
62   else()
63     if(NOT COMPILE_${FEATURE})
64       message(STATUS "Performing Test ${FEATURE} -- failed to compile")
65     else()
66       message(STATUS "Performing Test ${FEATURE} -- compiled but failed to run")
67     endif()
68   endif()
69 endfunction()