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.9.0 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, which 34# 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 gtest 38# 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 symbols 45set(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 and 50 # cross-compilation (e.g. Host=x86_64, target=aarch64) requires using the 51 # undocumented, but working variable. 52 # See https://gitlab.kitware.com/cmake/cmake/-/issues/15170 53 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 assembly 65 # 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 builds 75 return() 76 elseif(BENCHMARK_BUILD_32_BITS) 77 # FIXME: Make these work on 32 bit builds 78 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 tests 93option(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 functions 97list(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 version 102include(GetGitVersion) 103get_git_version(GIT_VERSION) 104 105# If no git version can be determined, use the version 106# from the project() command 107if ("${GIT_VERSION}" STREQUAL "v0.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 using 123message(STATUS "Google Benchmark version: ${VERSION}, normalized to ${NORMALIZED_VERSION}") 124 125# The version of the libraries 126set(GENERIC_LIB_VERSION ${NORMALIZED_VERSION}) 127string(SUBSTRING ${NORMALIZED_VERSION} 0 1 GENERIC_LIB_SOVERSION) 128 129# Import our CMake modules 130include(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 141set(BENCHMARK_CXX_STANDARD 14) 142 143set(CMAKE_CXX_STANDARD ${BENCHMARK_CXX_STANDARD}) 144set(CMAKE_CXX_STANDARD_REQUIRED YES) 145set(CMAKE_CXX_EXTENSIONS OFF) 146 147if (MSVC) 148 # Turn compiler warnings up to 11 149 string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 150 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /MP") 151 add_definitions(-D_CRT_SECURE_NO_WARNINGS) 152 153 if(BENCHMARK_ENABLE_WERROR) 154 add_cxx_compiler_flag(-WX) 155 endif() 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 optimisation 163 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 Support 184 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 11 188 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 add_cxx_compiler_flag(-Wconversion) 194 if(BENCHMARK_ENABLE_WERROR) 195 add_cxx_compiler_flag(-Werror) 196 endif() 197 if (NOT BENCHMARK_ENABLE_TESTING) 198 # Disable warning when compiling tests as gtest does not use 'override'. 199 add_cxx_compiler_flag(-Wsuggest-override) 200 endif() 201 add_cxx_compiler_flag(-pedantic) 202 add_cxx_compiler_flag(-pedantic-errors) 203 add_cxx_compiler_flag(-Wshorten-64-to-32) 204 add_cxx_compiler_flag(-fstrict-aliasing) 205 # Disable warnings regarding deprecated parts of the library while building 206 # and testing those parts of the library. 207 add_cxx_compiler_flag(-Wno-deprecated-declarations) 208 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") 209 # Intel silently ignores '-Wno-deprecated-declarations', 210 # warning no. 1786 must be explicitly disabled. 211 # See #631 for rationale. 212 add_cxx_compiler_flag(-wd1786) 213 add_cxx_compiler_flag(-fno-finite-math-only) 214 endif() 215 # Disable deprecation warnings for release builds (when -Werror is enabled). 216 if(BENCHMARK_ENABLE_WERROR) 217 add_cxx_compiler_flag(-Wno-deprecated) 218 endif() 219 if (NOT BENCHMARK_ENABLE_EXCEPTIONS) 220 add_cxx_compiler_flag(-fno-exceptions) 221 endif() 222 223 if (HAVE_CXX_FLAG_FSTRICT_ALIASING) 224 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") #ICC17u2: Many false positives for Wstrict-aliasing 225 add_cxx_compiler_flag(-Wstrict-aliasing) 226 endif() 227 endif() 228 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden 229 # (because of deprecated overload) 230 add_cxx_compiler_flag(-wd654) 231 add_cxx_compiler_flag(-Wthread-safety) 232 if (HAVE_CXX_FLAG_WTHREAD_SAFETY) 233 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES "-DINCLUDE_DIRECTORIES=${PROJECT_SOURCE_DIR}/include") 234 endif() 235 236 # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a 237 # predefined macro, which turns on all of the wonderful libc extensions. 238 # However g++ doesn't do this in Cygwin so we have to define it ourselves 239 # since we depend on GNU/POSIX/BSD extensions. 240 if (CYGWIN) 241 add_definitions(-D_GNU_SOURCE=1) 242 endif() 243 244 if (QNXNTO) 245 add_definitions(-D_QNX_SOURCE) 246 endif() 247 248 # Link time optimisation 249 if (BENCHMARK_ENABLE_LTO) 250 add_cxx_compiler_flag(-flto) 251 add_cxx_compiler_flag(-Wno-lto-type-mismatch) 252 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 253 find_program(GCC_AR gcc-ar) 254 if (GCC_AR) 255 set(CMAKE_AR ${GCC_AR}) 256 endif() 257 find_program(GCC_RANLIB gcc-ranlib) 258 if (GCC_RANLIB) 259 set(CMAKE_RANLIB ${GCC_RANLIB}) 260 endif() 261 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 262 include(llvm-toolchain) 263 endif() 264 endif() 265 266 # Coverage build type 267 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" 268 CACHE STRING "Flags used by the C++ compiler during coverage builds." 269 FORCE) 270 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" 271 CACHE STRING "Flags used for linking binaries during coverage builds." 272 FORCE) 273 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" 274 CACHE STRING "Flags used by the shared libraries linker during coverage builds." 275 FORCE) 276 mark_as_advanced( 277 BENCHMARK_CXX_FLAGS_COVERAGE 278 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE 279 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE) 280 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING 281 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") 282 add_cxx_compiler_flag(--coverage COVERAGE) 283endif() 284 285if (BENCHMARK_USE_LIBCXX) 286 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 287 add_cxx_compiler_flag(-stdlib=libc++) 288 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR 289 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" OR 290 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM") 291 add_cxx_compiler_flag(-nostdinc++) 292 message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS") 293 # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break 294 # configuration checks such as 'find_package(Threads)' 295 list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs) 296 # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because 297 # linker flags appear before all linker inputs and -lc++ must appear after. 298 list(APPEND BENCHMARK_CXX_LIBRARIES c++) 299 else() 300 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler") 301 endif() 302endif(BENCHMARK_USE_LIBCXX) 303 304set(EXTRA_CXX_FLAGS "") 305if (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 306 # Clang on Windows fails to compile the regex feature check under C++11 307 set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14") 308endif() 309 310# C++ feature checks 311# Determine the correct regular expression engine to use 312cxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS}) 313cxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS}) 314cxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS}) 315if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) 316 message(FATAL_ERROR "Failed to determine the source files for the regular expression backend") 317endif() 318if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX 319 AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) 320 message(WARNING "Using std::regex with exceptions disabled is not fully supported") 321endif() 322 323cxx_feature_check(STEADY_CLOCK) 324# Ensure we have pthreads 325set(THREADS_PREFER_PTHREAD_FLAG ON) 326find_package(Threads REQUIRED) 327cxx_feature_check(PTHREAD_AFFINITY) 328 329if (BENCHMARK_ENABLE_LIBPFM) 330 find_package(PFM REQUIRED) 331endif() 332 333# Set up directories 334include_directories(${PROJECT_SOURCE_DIR}/include) 335 336# Build the targets 337add_subdirectory(src) 338 339if (BENCHMARK_ENABLE_TESTING) 340 enable_testing() 341 if (BENCHMARK_ENABLE_GTEST_TESTS AND 342 NOT (TARGET gtest AND TARGET gtest_main AND 343 TARGET gmock AND TARGET gmock_main)) 344 if (BENCHMARK_USE_BUNDLED_GTEST) 345 include(GoogleTest) 346 else() 347 find_package(GTest CONFIG REQUIRED) 348 add_library(gtest ALIAS GTest::gtest) 349 add_library(gtest_main ALIAS GTest::gtest_main) 350 add_library(gmock ALIAS GTest::gmock) 351 add_library(gmock_main ALIAS GTest::gmock_main) 352 endif() 353 endif() 354 add_subdirectory(test) 355endif() 356