blob: c4c4d660f1eba1a484fcd2c2c11f7e94115ace31 [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)
Eric Fiselier133a7202017-04-18 07:17:20 +000013# Requires CMake 2.8.12+
Eric Fiselierb08d8b12016-07-19 23:07:03 +000014
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})
Eric Fiselier19039762018-01-18 04:23:01 +000025 set(HAVE_${VAR} 1 PARENT_SCOPE)
26 add_definitions(-DHAVE_${VAR})
Eric Fiselierb08d8b12016-07-19 23:07:03 +000027 return()
28 endif()
Eric Fiselier19039762018-01-18 04:23:01 +000029
Eric Fiselierfcafd3e2018-07-10 04:02:00 +000030 if (NOT DEFINED COMPILE_${FEATURE})
Eric Fiselier19039762018-01-18 04:23:01 +000031 message("-- Performing Test ${FEATURE}")
Eric Fiselierfcafd3e2018-07-10 04:02:00 +000032 if(CMAKE_CROSSCOMPILING)
33 try_compile(COMPILE_${FEATURE}
34 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
35 CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
36 LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
37 if(COMPILE_${FEATURE})
38 message(WARNING
39 "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0")
40 set(RUN_${FEATURE} 0)
41 else()
42 set(RUN_${FEATURE} 1)
43 endif()
44 else()
45 message("-- Performing Test ${FEATURE}")
46 try_run(RUN_${FEATURE} COMPILE_${FEATURE}
47 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
48 CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}
49 LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES})
50 endif()
Eric Fiselier19039762018-01-18 04:23:01 +000051 endif()
52
Eric Fiselierb08d8b12016-07-19 23:07:03 +000053 if(RUN_${FEATURE} EQUAL 0)
54 message("-- Performing Test ${FEATURE} -- success")
Eric Fiselier19039762018-01-18 04:23:01 +000055 set(HAVE_${VAR} 1 PARENT_SCOPE)
Eric Fiselierb08d8b12016-07-19 23:07:03 +000056 add_definitions(-DHAVE_${VAR})
57 else()
58 if(NOT COMPILE_${FEATURE})
59 message("-- Performing Test ${FEATURE} -- failed to compile")
60 else()
61 message("-- Performing Test ${FEATURE} -- compiled but failed to run")
62 endif()
63 endif()
64endfunction()