Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 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 | # |
Matt Clarkson | 9593e64 | 2014-08-22 14:36:19 +0100 | [diff] [blame] | 7 | # cxx_feature_check(<FLAG> [<VARIANT>]) |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 8 | # |
| 9 | # - Example |
| 10 | # |
Matt Clarkson | 9593e64 | 2014-08-22 14:36:19 +0100 | [diff] [blame] | 11 | # include(CXXFeatureCheck) |
| 12 | # cxx_feature_check(STD_REGEX) |
Eric | a3eac44 | 2016-12-05 10:22:46 -0700 | [diff] [blame] | 13 | # Requires CMake 2.8.12+ |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 14 | |
Matt Clarkson | 9593e64 | 2014-08-22 14:36:19 +0100 | [diff] [blame] | 15 | if(__cxx_feature_check) |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 16 | return() |
| 17 | endif() |
Matt Clarkson | 9593e64 | 2014-08-22 14:36:19 +0100 | [diff] [blame] | 18 | set(__cxx_feature_check INCLUDED) |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 19 | |
Dominic Hamon | 77d1e74 | 2022-08-10 12:42:27 -0700 | [diff] [blame] | 20 | option(CXXFEATURECHECK_DEBUG OFF) |
| 21 | |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 22 | function(cxx_feature_check FILE) |
| 23 | string(TOLOWER ${FILE} FILE) |
| 24 | string(TOUPPER ${FILE} VAR) |
| 25 | string(TOUPPER "HAVE_${VAR}" FEATURE) |
Eric Fiselier | 9fcdd6f | 2016-05-23 20:51:15 -0600 | [diff] [blame] | 26 | if (DEFINED HAVE_${VAR}) |
Roman Lebedev | 6e06648 | 2017-08-29 02:10:19 +0300 | [diff] [blame] | 27 | set(HAVE_${VAR} 1 PARENT_SCOPE) |
Jern-Kuan Leong | 710c2b8 | 2017-06-16 21:46:11 +0000 | [diff] [blame] | 28 | add_definitions(-DHAVE_${VAR}) |
Eric Fiselier | 9fcdd6f | 2016-05-23 20:51:15 -0600 | [diff] [blame] | 29 | return() |
| 30 | endif() |
Victor Costan | 9029360 | 2017-08-15 15:53:30 -0700 | [diff] [blame] | 31 | |
Dominic Hamon | 974cd5a | 2022-08-04 15:33:35 +0100 | [diff] [blame] | 32 | set(FEATURE_CHECK_CMAKE_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}) |
Jeremy Ong | 01c0ab7 | 2020-08-27 04:17:19 -0600 | [diff] [blame] | 33 | if (ARGC GREATER 1) |
| 34 | message(STATUS "Enabling additional flags: ${ARGV1}") |
Dominic Hamon | 974cd5a | 2022-08-04 15:33:35 +0100 | [diff] [blame] | 35 | list(APPEND FEATURE_CHECK_CMAKE_FLAGS ${ARGV1}) |
Jeremy Ong | 01c0ab7 | 2020-08-27 04:17:19 -0600 | [diff] [blame] | 36 | endif() |
| 37 | |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 38 | if (NOT DEFINED COMPILE_${FEATURE}) |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 39 | if(CMAKE_CROSSCOMPILING) |
Dominic Hamon | 974cd5a | 2022-08-04 15:33:35 +0100 | [diff] [blame] | 40 | message(STATUS "Cross-compiling to test ${FEATURE}") |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 41 | try_compile(COMPILE_${FEATURE} |
| 42 | ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp |
dominic hamon | da652a7 | 2022-12-10 19:42:44 -0400 | [diff] [blame] | 43 | CXX_STANDARD 11 |
| 44 | CXX_STANDARD_REQUIRED ON |
Dominic Hamon | 974cd5a | 2022-08-04 15:33:35 +0100 | [diff] [blame] | 45 | CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS} |
Dominic Hamon | 8205547 | 2022-07-05 10:41:38 +0100 | [diff] [blame] | 46 | LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES} |
| 47 | OUTPUT_VARIABLE COMPILE_OUTPUT_VAR) |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 48 | if(COMPILE_${FEATURE}) |
| 49 | message(WARNING |
| 50 | "If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0") |
Kyle Edwards | f4f5dba | 2019-10-04 04:50:53 -0400 | [diff] [blame] | 51 | set(RUN_${FEATURE} 0 CACHE INTERNAL "") |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 52 | else() |
Kyle Edwards | f4f5dba | 2019-10-04 04:50:53 -0400 | [diff] [blame] | 53 | set(RUN_${FEATURE} 1 CACHE INTERNAL "") |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 54 | endif() |
| 55 | else() |
Dominic Hamon | 974cd5a | 2022-08-04 15:33:35 +0100 | [diff] [blame] | 56 | message(STATUS "Compiling and running to test ${FEATURE}") |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 57 | try_run(RUN_${FEATURE} COMPILE_${FEATURE} |
| 58 | ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp |
dominic hamon | da652a7 | 2022-12-10 19:42:44 -0400 | [diff] [blame] | 59 | CXX_STANDARD 11 |
| 60 | CXX_STANDARD_REQUIRED ON |
Dominic Hamon | 974cd5a | 2022-08-04 15:33:35 +0100 | [diff] [blame] | 61 | CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS} |
Dominic Hamon | 8205547 | 2022-07-05 10:41:38 +0100 | [diff] [blame] | 62 | LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES} |
| 63 | COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT_VAR) |
Yangqing Jia | 105ac14 | 2018-04-23 12:57:03 -0700 | [diff] [blame] | 64 | endif() |
Victor Costan | 9029360 | 2017-08-15 15:53:30 -0700 | [diff] [blame] | 65 | endif() |
| 66 | |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 67 | if(RUN_${FEATURE} EQUAL 0) |
Dominic Hamon | bb15a4e | 2018-11-13 09:56:22 +0000 | [diff] [blame] | 68 | message(STATUS "Performing Test ${FEATURE} -- success") |
Roman Lebedev | 6e06648 | 2017-08-29 02:10:19 +0300 | [diff] [blame] | 69 | set(HAVE_${VAR} 1 PARENT_SCOPE) |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 70 | add_definitions(-DHAVE_${VAR}) |
| 71 | else() |
Dominic Hamon | e6107a7 | 2014-10-11 15:52:50 -0700 | [diff] [blame] | 72 | if(NOT COMPILE_${FEATURE}) |
Dominic Hamon | 77d1e74 | 2022-08-10 12:42:27 -0700 | [diff] [blame] | 73 | 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 Hamon | e6107a7 | 2014-10-11 15:52:50 -0700 | [diff] [blame] | 78 | else() |
Dominic Hamon | bb15a4e | 2018-11-13 09:56:22 +0000 | [diff] [blame] | 79 | message(STATUS "Performing Test ${FEATURE} -- compiled but failed to run") |
Dominic Hamon | e6107a7 | 2014-10-11 15:52:50 -0700 | [diff] [blame] | 80 | endif() |
Matt Clarkson | edfa60a | 2014-07-31 15:20:20 +0100 | [diff] [blame] | 81 | endif() |
| 82 | endfunction() |