1cmake_minimum_required (VERSION 3.5.1) 2 3foreach(p 4 CMP0048 # OK to clear PROJECT_VERSION on project() 5 CMP0054 # CMake 3.1 6 CMP0056 # export EXE_LINKER_FLAGS to try_run 7 CMP0057 # Support no if() IN_LIST operator 8 CMP0063 # Honor visibility properties for all targets 9 CMP0077 # Allow option() overrides in importing projects 10 ) 11 if(POLICY ${p}) 12 cmake_policy(SET ${p} NEW) 13 endif() 14endforeach() 15 16project (benchmark VERSION 1.6.1 LANGUAGES CXX) 17 18option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON) 19option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON) 20option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF) 21option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF) 22option(BENCHMARK_ENABLE_WERROR "Build Release candidates with -Werror." ON) 23option(BENCHMARK_FORCE_WERROR "Build Release candidates with -Werror regardless of compiler issues." OFF) 24 25if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI") 26 # PGC++ maybe reporting false positives. 27 set(BENCHMARK_ENABLE_WERROR OFF) 28endif() 29if(BENCHMARK_FORCE_WERROR) 30 set(BENCHMARK_ENABLE_WERROR ON) 31endif(BENCHMARK_FORCE_WERROR) 32 33if(NOT MSVC) 34 option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF) 35else() 36 set(BENCHMARK_BUILD_32_BITS OFF CACHE BOOL "Build a 32 bit version of the library - unsupported when using MSVC)" FORCE) 37endif() 38option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON) 39option(BENCHMARK_ENABLE_DOXYGEN "Build documentation with Doxygen." OFF) 40option(BENCHMARK_INSTALL_DOCS "Enable installation of documentation." ON) 41 42# Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which 43# may require downloading the source code. 44option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF) 45 46# This option can be used to disable building and running unit tests which depend on gtest 47# in cases where it is not possible to build or find a valid version of gtest. 48option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON) 49option(BENCHMARK_USE_BUNDLED_GTEST "Use bundled GoogleTest. If disabled, the find_package(GTest) will be used." ON) 50 51option(BENCHMARK_ENABLE_LIBPFM "Enable performance counters provided by libpfm" OFF) 52 53set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) 54if(MSVC) 55 # As of CMake 3.18, CMAKE_SYSTEM_PROCESSOR is not set properly for MSVC and 56 # cross-compilation (e.g. Host=x86_64, target=aarch64) requires using the 57 # undocumented, but working variable. 58 # See https://gitlab.kitware.com/cmake/cmake/-/issues/15170 59 set(CMAKE_SYSTEM_PROCESSOR ${MSVC_CXX_ARCHITECTURE_ID}) 60 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ARM") 61 set(CMAKE_CROSSCOMPILING TRUE) 62 endif() 63endif() 64 65set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF) 66function(should_enable_assembly_tests) 67 if(CMAKE_BUILD_TYPE) 68 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER) 69 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage") 70 # FIXME: The --coverage flag needs to be removed when building assembly 71 # tests for this to work. 72 return() 73 endif() 74 endif() 75 if (MSVC) 76 return() 77 elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") 78 return() 79 elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) 80 # FIXME: Make these work on 32 bit builds 81 return() 82 elseif(BENCHMARK_BUILD_32_BITS) 83 # FIXME: Make these work on 32 bit builds 84 return() 85 endif() 86 find_program(LLVM_FILECHECK_EXE FileCheck) 87 if (LLVM_FILECHECK_EXE) 88 set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE) 89 message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}") 90 else() 91 message(STATUS "Failed to find LLVM FileCheck") 92 return() 93 endif() 94 set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE) 95endfunction() 96should_enable_assembly_tests() 97 98# This option disables the building and running of the assembly verification tests 99option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests" 100 ${ENABLE_ASSEMBLY_TESTS_DEFAULT}) 101 102# Make sure we can import out CMake functions 103list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules") 104list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 105 106 107# Read the git tags to determine the project version 108include(GetGitVersion) 109get_git_version(GIT_VERSION) 110 111# If no git version can be determined, use the version 112# from the project() command 113if ("${GIT_VERSION}" STREQUAL "0.0.0") 114 set(VERSION "${benchmark_VERSION}") 115else() 116 set(VERSION "${GIT_VERSION}") 117endif() 118# Tell the user what versions we are using 119message(STATUS "Version: ${VERSION}") 120 121# The version of the libraries 122set(GENERIC_LIB_VERSION ${VERSION}) 123string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION) 124 125# Import our CMake modules 126include(CheckCXXCompilerFlag) 127include(AddCXXCompilerFlag) 128include(CXXFeatureCheck) 129include(CheckLibraryExists) 130 131check_library_exists(rt shm_open "" HAVE_LIB_RT) 132 133if (BENCHMARK_BUILD_32_BITS) 134 add_required_cxx_compiler_flag(-m32) 135endif() 136 137if (MSVC) 138 # Turn compiler warnings up to 11 139 string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 140 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") 141 add_definitions(-D_CRT_SECURE_NO_WARNINGS) 142 143 if (NOT BENCHMARK_ENABLE_EXCEPTIONS) 144 add_cxx_compiler_flag(-EHs-) 145 add_cxx_compiler_flag(-EHa-) 146 add_definitions(-D_HAS_EXCEPTIONS=0) 147 endif() 148 # Link time optimisation 149 if (BENCHMARK_ENABLE_LTO) 150 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL") 151 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG") 152 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG") 153 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG") 154 155 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL") 156 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}") 157 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") 158 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}") 159 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") 160 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}") 161 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") 162 163 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL") 164 set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG") 165 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG") 166 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG") 167 endif() 168else() 169 # Try and enable C++11. Don't use C++14 because it doesn't work in some 170 # configurations. 171 add_cxx_compiler_flag(-std=c++11) 172 if (NOT HAVE_CXX_FLAG_STD_CXX11) 173 add_cxx_compiler_flag(-std=c++0x) 174 endif() 175 176 # Turn compiler warnings up to 11 177 add_cxx_compiler_flag(-Wall) 178 add_cxx_compiler_flag(-Wextra) 179 add_cxx_compiler_flag(-Wshadow) 180 if(BENCHMARK_ENABLE_WERROR) 181 add_cxx_compiler_flag(-Werror RELEASE) 182 add_cxx_compiler_flag(-Werror RELWITHDEBINFO) 183 add_cxx_compiler_flag(-Werror MINSIZEREL) 184 endif() 185 if (NOT BENCHMARK_ENABLE_TESTING) 186 # Disable warning when compiling tests as gtest does not use 'override'. 187 add_cxx_compiler_flag(-Wsuggest-override) 188 endif() 189 add_cxx_compiler_flag(-pedantic) 190 add_cxx_compiler_flag(-pedantic-errors) 191 add_cxx_compiler_flag(-Wshorten-64-to-32) 192 add_cxx_compiler_flag(-fstrict-aliasing) 193 # Disable warnings regarding deprecated parts of the library while building 194 # and testing those parts of the library. 195 add_cxx_compiler_flag(-Wno-deprecated-declarations) 196 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") 197 # Intel silently ignores '-Wno-deprecated-declarations', 198 # warning no. 1786 must be explicitly disabled. 199 # See #631 for rationale. 200 add_cxx_compiler_flag(-wd1786) 201 endif() 202 # Disable deprecation warnings for release builds (when -Werror is enabled). 203 if(BENCHMARK_ENABLE_WERROR) 204 add_cxx_compiler_flag(-Wno-deprecated RELEASE) 205 add_cxx_compiler_flag(-Wno-deprecated RELWITHDEBINFO) 206 add_cxx_compiler_flag(-Wno-deprecated MINSIZEREL) 207 endif() 208 if (NOT BENCHMARK_ENABLE_EXCEPTIONS) 209 add_cxx_compiler_flag(-fno-exceptions) 210 endif() 211 212 if (HAVE_CXX_FLAG_FSTRICT_ALIASING) 213 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing 214 add_cxx_compiler_flag(-Wstrict-aliasing) 215 endif() 216 endif() 217 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden 218 # (because of deprecated overload) 219 add_cxx_compiler_flag(-wd654) 220 add_cxx_compiler_flag(-Wthread-safety) 221 if (HAVE_CXX_FLAG_WTHREAD_SAFETY) 222 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES) 223 endif() 224 225 # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a 226 # predefined macro, which turns on all of the wonderful libc extensions. 227 # However g++ doesn't do this in Cygwin so we have to define it ourselfs 228 # since we depend on GNU/POSIX/BSD extensions. 229 if (CYGWIN) 230 add_definitions(-D_GNU_SOURCE=1) 231 endif() 232 233 if (QNXNTO) 234 add_definitions(-D_QNX_SOURCE) 235 endif() 236 237 # Link time optimisation 238 if (BENCHMARK_ENABLE_LTO) 239 add_cxx_compiler_flag(-flto) 240 add_cxx_compiler_flag(-Wno-lto-type-mismatch) 241 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 242 find_program(GCC_AR gcc-ar) 243 if (GCC_AR) 244 set(CMAKE_AR ${GCC_AR}) 245 endif() 246 find_program(GCC_RANLIB gcc-ranlib) 247 if (GCC_RANLIB) 248 set(CMAKE_RANLIB ${GCC_RANLIB}) 249 endif() 250 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 251 include(llvm-toolchain) 252 endif() 253 endif() 254 255 # Coverage build type 256 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" 257 CACHE STRING "Flags used by the C++ compiler during coverage builds." 258 FORCE) 259 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" 260 CACHE STRING "Flags used for linking binaries during coverage builds." 261 FORCE) 262 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" 263 CACHE STRING "Flags used by the shared libraries linker during coverage builds." 264 FORCE) 265 mark_as_advanced( 266 BENCHMARK_CXX_FLAGS_COVERAGE 267 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE 268 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE) 269 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING 270 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") 271 add_cxx_compiler_flag(--coverage COVERAGE) 272endif() 273 274if (BENCHMARK_USE_LIBCXX) 275 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 276 add_cxx_compiler_flag(-stdlib=libc++) 277 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR 278 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") 279 add_cxx_compiler_flag(-nostdinc++) 280 message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS") 281 # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break 282 # configuration checks such as 'find_package(Threads)' 283 list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs) 284 # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because 285 # linker flags appear before all linker inputs and -lc++ must appear after. 286 list(APPEND BENCHMARK_CXX_LIBRARIES c++) 287 else() 288 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler") 289 endif() 290endif(BENCHMARK_USE_LIBCXX) 291 292set(EXTRA_CXX_FLAGS "") 293if (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 294 # Clang on Windows fails to compile the regex feature check under C++11 295 set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14") 296endif() 297 298# C++ feature checks 299# Determine the correct regular expression engine to use 300cxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS}) 301cxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS}) 302cxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS}) 303if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) 304 message(FATAL_ERROR "Failed to determine the source files for the regular expression backend") 305endif() 306if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX 307 AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) 308 message(WARNING "Using std::regex with exceptions disabled is not fully supported") 309endif() 310 311cxx_feature_check(STEADY_CLOCK) 312# Ensure we have pthreads 313set(THREADS_PREFER_PTHREAD_FLAG ON) 314find_package(Threads REQUIRED) 315 316if (BENCHMARK_ENABLE_LIBPFM) 317 find_package(PFM) 318endif() 319 320# Set up directories 321include_directories(${PROJECT_SOURCE_DIR}/include) 322 323# Build the targets 324add_subdirectory(src) 325 326if (BENCHMARK_ENABLE_TESTING) 327 enable_testing() 328 if (BENCHMARK_ENABLE_GTEST_TESTS AND 329 NOT (TARGET gtest AND TARGET gtest_main AND 330 TARGET gmock AND TARGET gmock_main)) 331 if (BENCHMARK_USE_BUNDLED_GTEST) 332 include(GoogleTest) 333 else() 334 find_package(GTest CONFIG REQUIRED) 335 add_library(gtest ALIAS GTest::gtest) 336 add_library(gtest_main ALIAS GTest::gtest_main) 337 add_library(gmock ALIAS GTest::gmock) 338 add_library(gmock_main ALIAS GTest::gmock_main) 339 endif() 340 endif() 341 add_subdirectory(test) 342endif() 343