355 lines · plain
1# Require CMake 3.10. If available, use the policies up to CMake 3.22.2cmake_minimum_required (VERSION 3.10...3.22)3 4project (benchmark VERSION 1.8.3 LANGUAGES CXX)5 6option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)7option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)8option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)9option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)10option(BENCHMARK_ENABLE_WERROR "Build Release candidates with -Werror." ON)11option(BENCHMARK_FORCE_WERROR "Build Release candidates with -Werror regardless of compiler issues." OFF)12 13if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")14 # PGC++ maybe reporting false positives.15 set(BENCHMARK_ENABLE_WERROR OFF)16endif()17if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "NVHPC")18 set(BENCHMARK_ENABLE_WERROR OFF)19endif()20if(BENCHMARK_FORCE_WERROR)21 set(BENCHMARK_ENABLE_WERROR ON)22endif(BENCHMARK_FORCE_WERROR)23 24if(NOT (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))25 option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)26else()27 set(BENCHMARK_BUILD_32_BITS OFF CACHE BOOL "Build a 32 bit version of the library - unsupported when using MSVC)" FORCE)28endif()29option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)30option(BENCHMARK_ENABLE_DOXYGEN "Build documentation with Doxygen." OFF)31option(BENCHMARK_INSTALL_DOCS "Enable installation of documentation." ON)32 33# Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which34# may require downloading the source code.35option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF)36 37# This option can be used to disable building and running unit tests which depend on gtest38# in cases where it is not possible to build or find a valid version of gtest.39option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON)40option(BENCHMARK_USE_BUNDLED_GTEST "Use bundled GoogleTest. If disabled, the find_package(GTest) will be used." ON)41 42option(BENCHMARK_ENABLE_LIBPFM "Enable performance counters provided by libpfm" OFF)43 44# Export only public symbols45set(CMAKE_CXX_VISIBILITY_PRESET hidden)46set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)47 48if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")49 # As of CMake 3.18, CMAKE_SYSTEM_PROCESSOR is not set properly for MSVC and50 # cross-compilation (e.g. Host=x86_64, target=aarch64) requires using the51 # undocumented, but working variable.52 # See https://gitlab.kitware.com/cmake/cmake/-/issues/1517053 set(CMAKE_SYSTEM_PROCESSOR ${MSVC_CXX_ARCHITECTURE_ID})54 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ARM")55 set(CMAKE_CROSSCOMPILING TRUE)56 endif()57endif()58 59set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF)60function(should_enable_assembly_tests)61 if(CMAKE_BUILD_TYPE)62 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)63 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")64 # FIXME: The --coverage flag needs to be removed when building assembly65 # tests for this to work.66 return()67 endif()68 endif()69 if (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")70 return()71 elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")72 return()73 elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)74 # FIXME: Make these work on 32 bit builds75 return()76 elseif(BENCHMARK_BUILD_32_BITS)77 # FIXME: Make these work on 32 bit builds78 return()79 endif()80 find_program(LLVM_FILECHECK_EXE FileCheck)81 if (LLVM_FILECHECK_EXE)82 set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE)83 message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}")84 else()85 message(STATUS "Failed to find LLVM FileCheck")86 return()87 endif()88 set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE)89endfunction()90should_enable_assembly_tests()91 92# This option disables the building and running of the assembly verification tests93option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests"94 ${ENABLE_ASSEMBLY_TESTS_DEFAULT})95 96# Make sure we can import out CMake functions97list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")98list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")99 100 101# Read the git tags to determine the project version102include(GetGitVersion)103get_git_version(GIT_VERSION)104 105# If no git version can be determined, use the version106# from the project() command107if ("${GIT_VERSION}" STREQUAL "0.0.0")108 set(VERSION "v${benchmark_VERSION}")109else()110 set(VERSION "${GIT_VERSION}")111endif()112 113# Normalize version: drop "v" prefix, replace first "-" with ".",114# drop everything after second "-" (including said "-").115string(STRIP ${VERSION} VERSION)116if(VERSION MATCHES v[^-]*-)117 string(REGEX REPLACE "v([^-]*)-([0-9]+)-.*" "\\1.\\2" NORMALIZED_VERSION ${VERSION})118else()119 string(REGEX REPLACE "v(.*)" "\\1" NORMALIZED_VERSION ${VERSION})120endif()121 122# Tell the user what versions we are using123message(STATUS "Google Benchmark version: ${VERSION}, normalized to ${NORMALIZED_VERSION}")124 125# The version of the libraries126set(GENERIC_LIB_VERSION ${NORMALIZED_VERSION})127string(SUBSTRING ${NORMALIZED_VERSION} 0 1 GENERIC_LIB_SOVERSION)128 129# Import our CMake modules130include(AddCXXCompilerFlag)131include(CheckCXXCompilerFlag)132include(CheckLibraryExists)133include(CXXFeatureCheck)134 135check_library_exists(rt shm_open "" HAVE_LIB_RT)136 137if (BENCHMARK_BUILD_32_BITS)138 add_required_cxx_compiler_flag(-m32)139endif()140 141if (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")142 set(BENCHMARK_CXX_STANDARD 14)143else()144 set(BENCHMARK_CXX_STANDARD 11)145endif()146 147set(CMAKE_CXX_STANDARD ${BENCHMARK_CXX_STANDARD})148set(CMAKE_CXX_STANDARD_REQUIRED YES)149set(CMAKE_CXX_EXTENSIONS OFF)150 151if (MSVC)152 # Turn compiler warnings up to 11153 string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")154 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")155 add_definitions(-D_CRT_SECURE_NO_WARNINGS)156 157 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)158 add_cxx_compiler_flag(-EHs-)159 add_cxx_compiler_flag(-EHa-)160 add_definitions(-D_HAS_EXCEPTIONS=0)161 endif()162 # Link time optimisation163 if (BENCHMARK_ENABLE_LTO)164 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")165 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")166 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")167 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")168 169 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")170 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")171 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")172 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")173 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")174 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")175 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")176 177 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")178 set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")179 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")180 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")181 endif()182else()183 # Turn on Large-file Support184 add_definitions(-D_FILE_OFFSET_BITS=64)185 add_definitions(-D_LARGEFILE64_SOURCE)186 add_definitions(-D_LARGEFILE_SOURCE)187 # Turn compiler warnings up to 11188 add_cxx_compiler_flag(-Wall)189 add_cxx_compiler_flag(-Wextra)190 add_cxx_compiler_flag(-Wshadow)191 add_cxx_compiler_flag(-Wfloat-equal)192 add_cxx_compiler_flag(-Wold-style-cast)193 if(BENCHMARK_ENABLE_WERROR)194 add_cxx_compiler_flag(-Werror)195 endif()196 if (NOT BENCHMARK_ENABLE_TESTING)197 # Disable warning when compiling tests as gtest does not use 'override'.198 add_cxx_compiler_flag(-Wsuggest-override)199 endif()200 add_cxx_compiler_flag(-pedantic)201 add_cxx_compiler_flag(-pedantic-errors)202 add_cxx_compiler_flag(-Wshorten-64-to-32)203 add_cxx_compiler_flag(-fstrict-aliasing)204 # Disable warnings regarding deprecated parts of the library while building205 # and testing those parts of the library.206 add_cxx_compiler_flag(-Wno-deprecated-declarations)207 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")208 # Intel silently ignores '-Wno-deprecated-declarations',209 # warning no. 1786 must be explicitly disabled.210 # See #631 for rationale.211 add_cxx_compiler_flag(-wd1786)212 add_cxx_compiler_flag(-fno-finite-math-only)213 endif()214 # Disable deprecation warnings for release builds (when -Werror is enabled).215 if(BENCHMARK_ENABLE_WERROR)216 add_cxx_compiler_flag(-Wno-deprecated)217 endif()218 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)219 add_cxx_compiler_flag(-fno-exceptions)220 endif()221 222 if (HAVE_CXX_FLAG_FSTRICT_ALIASING)223 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") #ICC17u2: Many false positives for Wstrict-aliasing224 add_cxx_compiler_flag(-Wstrict-aliasing)225 endif()226 endif()227 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden228 # (because of deprecated overload)229 add_cxx_compiler_flag(-wd654)230 add_cxx_compiler_flag(-Wthread-safety)231 if (HAVE_CXX_FLAG_WTHREAD_SAFETY)232 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES "-DINCLUDE_DIRECTORIES=${PROJECT_SOURCE_DIR}/include")233 endif()234 235 # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a236 # predefined macro, which turns on all of the wonderful libc extensions.237 # However g++ doesn't do this in Cygwin so we have to define it ourselves238 # since we depend on GNU/POSIX/BSD extensions.239 if (CYGWIN)240 add_definitions(-D_GNU_SOURCE=1)241 endif()242 243 if (QNXNTO)244 add_definitions(-D_QNX_SOURCE)245 endif()246 247 # Link time optimisation248 if (BENCHMARK_ENABLE_LTO)249 add_cxx_compiler_flag(-flto)250 add_cxx_compiler_flag(-Wno-lto-type-mismatch)251 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")252 find_program(GCC_AR gcc-ar)253 if (GCC_AR)254 set(CMAKE_AR ${GCC_AR})255 endif()256 find_program(GCC_RANLIB gcc-ranlib)257 if (GCC_RANLIB)258 set(CMAKE_RANLIB ${GCC_RANLIB})259 endif()260 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")261 include(llvm-toolchain)262 endif()263 endif()264 265 # Coverage build type266 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"267 CACHE STRING "Flags used by the C++ compiler during coverage builds."268 FORCE)269 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"270 CACHE STRING "Flags used for linking binaries during coverage builds."271 FORCE)272 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"273 CACHE STRING "Flags used by the shared libraries linker during coverage builds."274 FORCE)275 mark_as_advanced(276 BENCHMARK_CXX_FLAGS_COVERAGE277 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE278 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)279 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING280 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")281 add_cxx_compiler_flag(--coverage COVERAGE)282endif()283 284if (BENCHMARK_USE_LIBCXX)285 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")286 add_cxx_compiler_flag(-stdlib=libc++)287 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR288 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" OR289 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM")290 add_cxx_compiler_flag(-nostdinc++)291 message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS")292 # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break293 # configuration checks such as 'find_package(Threads)'294 list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)295 # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because296 # linker flags appear before all linker inputs and -lc++ must appear after.297 list(APPEND BENCHMARK_CXX_LIBRARIES c++)298 else()299 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")300 endif()301endif(BENCHMARK_USE_LIBCXX)302 303set(EXTRA_CXX_FLAGS "")304if (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")305 # Clang on Windows fails to compile the regex feature check under C++11306 set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14")307endif()308 309# C++ feature checks310# Determine the correct regular expression engine to use311cxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS})312cxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS})313cxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS})314if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)315 message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")316endif()317if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX318 AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)319 message(WARNING "Using std::regex with exceptions disabled is not fully supported")320endif()321 322cxx_feature_check(STEADY_CLOCK)323# Ensure we have pthreads324set(THREADS_PREFER_PTHREAD_FLAG ON)325find_package(Threads REQUIRED)326cxx_feature_check(PTHREAD_AFFINITY)327 328if (BENCHMARK_ENABLE_LIBPFM)329 find_package(PFM REQUIRED)330endif()331 332# Set up directories333include_directories(${PROJECT_SOURCE_DIR}/include)334 335# Build the targets336add_subdirectory(src)337 338if (BENCHMARK_ENABLE_TESTING)339 enable_testing()340 if (BENCHMARK_ENABLE_GTEST_TESTS AND341 NOT (TARGET gtest AND TARGET gtest_main AND342 TARGET gmock AND TARGET gmock_main))343 if (BENCHMARK_USE_BUNDLED_GTEST)344 include(GoogleTest)345 else()346 find_package(GTest CONFIG REQUIRED)347 add_library(gtest ALIAS GTest::gtest)348 add_library(gtest_main ALIAS GTest::gtest_main)349 add_library(gmock ALIAS GTest::gmock)350 add_library(gmock_main ALIAS GTest::gmock_main)351 endif()352 endif()353 add_subdirectory(test)354endif()355