Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,10 @@ if (BENCHMARK_USE_LIBCXX)
endif(BENCHMARK_USE_LIBCXX)

# C++ feature checks
# Determine the correct regular expression engine to use. First compatible engine found is used.
# Determine the correct regular expression engine to use
cxx_feature_check(STD_REGEX)

if(NOT HAVE_STD_REGEX)
cxx_feature_check(GNU_POSIX_REGEX)
endif()

if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX)
cxx_feature_check(POSIX_REGEX)
endif()

cxx_feature_check(GNU_POSIX_REGEX)
cxx_feature_check(POSIX_REGEX)
if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
endif()
Expand Down
114 changes: 48 additions & 66 deletions cmake/CXXFeatureCheck.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,22 @@
#
# include(CXXFeatureCheck)
# cxx_feature_check(STD_REGEX)
# Requires CMake 3.13+
# Requires CMake 2.8.12+

if(__cxx_feature_check)
return()
endif()
set(__cxx_feature_check INCLUDED)

option(CXXFEATURECHECK_DEBUG OFF "Enable debug messages for CXX feature checks")
option(CXXFEATURECHECK_DEBUG OFF)

function(cxx_feature_check_print log)
if(CXXFEATURECHECK_DEBUG)
message(STATUS "${log}")
endif()
endfunction()

function(cxx_feature_check FEATURE)
string(TOLOWER ${FEATURE} FILE)
string(TOUPPER HAVE_${FEATURE} VAR)

# Check if the variable is already defined to a true or false for a quick return.
# This allows users to predefine the variable to skip the check.
# Or, if the variable is already defined by a previous check, we skip the costly check.
if (DEFINED ${VAR})
if (${VAR})
cxx_feature_check_print("Feature ${FEATURE} already enabled.")
add_compile_definitions(${VAR})
else()
cxx_feature_check_print("Feature ${FEATURE} already disabled.")
endif()
function(cxx_feature_check FILE)
string(TOLOWER ${FILE} FILE)
string(TOUPPER ${FILE} VAR)
string(TOUPPER "HAVE_${VAR}" FEATURE)
if (DEFINED HAVE_${VAR})
set(HAVE_${VAR} 1 PARENT_SCOPE)
add_definitions(-DHAVE_${VAR})
return()
endif()

Expand All @@ -48,53 +35,48 @@ function(cxx_feature_check FEATURE)
list(APPEND FEATURE_CHECK_CMAKE_FLAGS ${ARGV1})
endif()

if(CMAKE_CROSSCOMPILING)
cxx_feature_check_print("Cross-compiling to test ${FEATURE}")
try_compile(
COMPILE_STATUS
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CMAKE_FLAGS "${FEATURE_CHECK_CMAKE_FLAGS}"
LINK_LIBRARIES "${BENCHMARK_CXX_LIBRARIES}"
OUTPUT_VARIABLE COMPILE_OUTPUT_VAR
)
if(COMPILE_STATUS)
set(RUN_STATUS 0)
message(WARNING
"If you see build failures due to cross compilation, try setting ${VAR} to 0")
if (NOT DEFINED COMPILE_${FEATURE})
if(CMAKE_CROSSCOMPILING)
message(STATUS "Cross-compiling to test ${FEATURE}")
try_compile(COMPILE_${FEATURE}
${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS}
LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}
OUTPUT_VARIABLE COMPILE_OUTPUT_VAR)
if(COMPILE_${FEATURE})
message(WARNING
"If you see build failures due to cross compilation, try setting HAVE_${VAR} to 0")
set(RUN_${FEATURE} 0 CACHE INTERNAL "")
else()
set(RUN_${FEATURE} 1 CACHE INTERNAL "")
endif()
else()
message(STATUS "Compiling and running to test ${FEATURE}")
try_run(RUN_${FEATURE} COMPILE_${FEATURE}
${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CMAKE_FLAGS ${FEATURE_CHECK_CMAKE_FLAGS}
LINK_LIBRARIES ${BENCHMARK_CXX_LIBRARIES}
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT_VAR)
endif()
else()
cxx_feature_check_print("Compiling and running to test ${FEATURE}")
try_run(
RUN_STATUS
COMPILE_STATUS
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/cmake/${FILE}.cpp
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CMAKE_FLAGS "${FEATURE_CHECK_CMAKE_FLAGS}"
LINK_LIBRARIES "${BENCHMARK_CXX_LIBRARIES}"
COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
RUN_OUTPUT_VARIABLE RUN_OUTPUT
)
endif()

if(COMPILE_STATUS AND RUN_STATUS EQUAL 0)
message(STATUS "Performing Test ${FEATURE} -- success")
set(${VAR} TRUE CACHE BOOL "" FORCE)
add_compile_definitions(${VAR})
return()
endif()

set(${VAR} FALSE CACHE BOOL "" FORCE)
message(STATUS "Performing Test ${FEATURE} -- failed")

if(NOT COMPILE_STATUS)
cxx_feature_check_print("Compile Output: ${COMPILE_OUTPUT}")
if(COMPILE_${FEATURE})
if(DEFINED RUN_${FEATURE} AND RUN_${FEATURE} EQUAL 0)
message(STATUS "Performing Test ${FEATURE} -- success")
set(HAVE_${VAR} 1 PARENT_SCOPE)
add_definitions(-DHAVE_${VAR})
else()
message(STATUS "Performing Test ${FEATURE} -- compiled but failed to run")
endif()
else()
cxx_feature_check_print("Run Output: ${RUN_OUTPUT}")
if(CXXFEATURECHECK_DEBUG)
message(STATUS "Performing Test ${FEATURE} -- failed to compile: ${COMPILE_OUTPUT_VAR}")
else()
message(STATUS "Performing Test ${FEATURE} -- failed to compile")
endif()
endif()

endfunction()