blob: e51482659b0f10fd68c9ec008914d3f3a458b224 [file] [log] [blame]
Matt Clarksonedfa60a2014-07-31 15:20:20 +01001# - 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#
Matt Clarkson9593e642014-08-22 14:36:19 +01007# cxx_feature_check(<FLAG> [<VARIANT>])
Matt Clarksonedfa60a2014-07-31 15:20:20 +01008#
9# - Example
10#
Matt Clarkson9593e642014-08-22 14:36:19 +010011# include(CXXFeatureCheck)
12# cxx_feature_check(STD_REGEX)
Erica3eac442016-12-05 10:22:46 -070013# Requires CMake 2.8.12+
Matt Clarksonedfa60a2014-07-31 15:20:20 +010014
Matt Clarkson9593e642014-08-22 14:36:19 +010015if(__cxx_feature_check)
Matt Clarksonedfa60a2014-07-31 15:20:20 +010016 return()
17endif()
Matt Clarkson9593e642014-08-22 14:36:19 +010018set(__cxx_feature_check INCLUDED)
Matt Clarksonedfa60a2014-07-31 15:20:20 +010019
Dominic Hamon77d1e742022-08-10 12:42:27 -070020option(CXXFEATURECHECK_DEBUG OFF)
21
Matt Clarksonedfa60a2014-07-31 15:20:20 +010022function(cxx_feature_check FILE)
23 string(TOLOWER ${FILE} FILE)
24 string(TOUPPER ${FILE} VAR)
25 string(TOUPPER "HAVE_${VAR}" FEATURE)
Eric Fiselier9fcdd6f2016-05-23 20:51:15 -060026 if (DEFINED HAVE_${VAR})
Roman Lebedev6e066482017-08-29 02:10:19 +030027 set(HAVE_${VAR} 1 PARENT_SCOPE)
Jern-Kuan Leong710c2b82017-06-16 21:46:11 +000028 add_definitions(-DHAVE_${VAR})
Eric Fiselier9fcdd6f2016-05-23 20:51:15 -060029 return()
30 endif()
Victor Costan90293602017-08-15 15:53:30 -070031
Dominic Hamon974cd5a2022-08-04 15:33:35 +010032 set(FEATURE_CHECK_CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS})
Jeremy Ong01c0ab72020-08-27 04:17:19 -060033 if (ARGC GREATER 1)
34 message(STATUS "Enabling additional flags: ${ARGV1}")
Dominic Hamon974cd5a2022-08-04 15:33:35 +010035 list(APPEND FEATURE_CHECK_CMAKE_FLAGS ${ARGV1})
Jeremy Ong01c0ab72020-08-27 04:17:19 -060036 endif()
37
Yangqing Jia105ac142018-04-23 12:57:03 -070038 if (NOT DEFINED COMPILE_${FEATURE})
Yangqing Jia105ac142018-04-23 12:57:03 -070039 if(CMAKE_CROSSCOMPILING)
Dominic Hamon974cd5a2022-08-04 15:33:35 +010040 message(STATUS "Cross-compiling to test ${FEATURE}")
Yangqing Jia105ac142018-04-23 12:57:03 -070041 try_compile(COMPILE_${FEATURE}
42 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
dominic hamonda652a72022-12-10 19:42:44 -040043 CXX_STANDARD 11
44 CXX_STANDARD_REQUIRED ON
Dominic Hamon974cd5a2022-08-04 15:33:35 +010045 CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS}
Dominic Hamon82055472022-07-05 10:41:38 +010046 LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}
47 OUTPUT_VARIABLE COMPILE_OUTPUT_VAR)
Yangqing Jia105ac142018-04-23 12:57:03 -070048 if(COMPILE_${FEATURE})
49 message(WARNING
50 "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0")
Kyle Edwardsf4f5dba2019-10-04 04:50:53 -040051 set(RUN_${FEATURE} 0 CACHE INTERNAL "")
Yangqing Jia105ac142018-04-23 12:57:03 -070052 else()
Kyle Edwardsf4f5dba2019-10-04 04:50:53 -040053 set(RUN_${FEATURE} 1 CACHE INTERNAL "")
Yangqing Jia105ac142018-04-23 12:57:03 -070054 endif()
55 else()
Dominic Hamon974cd5a2022-08-04 15:33:35 +010056 message(STATUS "Compiling and running to test ${FEATURE}")
Yangqing Jia105ac142018-04-23 12:57:03 -070057 try_run(RUN_${FEATURE} COMPILE_${FEATURE}
58 ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
dominic hamonda652a72022-12-10 19:42:44 -040059 CXX_STANDARD 11
60 CXX_STANDARD_REQUIRED ON
Dominic Hamon974cd5a2022-08-04 15:33:35 +010061 CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS}
Dominic Hamon82055472022-07-05 10:41:38 +010062 LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}
63 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT_VAR)
Yangqing Jia105ac142018-04-23 12:57:03 -070064 endif()
Victor Costan90293602017-08-15 15:53:30 -070065 endif()
66
Matt Clarksonedfa60a2014-07-31 15:20:20 +010067 if(RUN_${FEATURE} EQUAL 0)
Dominic Hamonbb15a4e2018-11-13 09:56:22 +000068 message(STATUS "Performing Test ${FEATURE} -- success")
Roman Lebedev6e066482017-08-29 02:10:19 +030069 set(HAVE_${VAR} 1 PARENT_SCOPE)
Matt Clarksonedfa60a2014-07-31 15:20:20 +010070 add_definitions(-DHAVE_${VAR})
71 else()
Dominic Hamone6107a72014-10-11 15:52:50 -070072 if(NOT COMPILE_${FEATURE})
Dominic Hamon77d1e742022-08-10 12:42:27 -070073 if(CXXFEATURECHECK_DEBUG)
74 message(STATUS "Performing Test ${FEATURE} -- failed to compile: ${COMPILE_OUTPUT_VAR}")
75 else()
76 message(STATUS "Performing Test ${FEATURE} -- failed to compile")
77 endif()
Dominic Hamone6107a72014-10-11 15:52:50 -070078 else()
Dominic Hamonbb15a4e2018-11-13 09:56:22 +000079 message(STATUS "Performing Test ${FEATURE} -- compiled but failed to run")
Dominic Hamone6107a72014-10-11 15:52:50 -070080 endif()
Matt Clarksonedfa60a2014-07-31 15:20:20 +010081 endif()
82endfunction()