1cmake_minimum_required (VERSION 2.8.12) 2 3project (benchmark) 4 5foreach(p 6 CMP0054 # CMake 3.1 7 CMP0056 # export EXE_LINKER_FLAGS to try_run 8 CMP0057 # Support no if() IN_LIST operator 9 ) 10 if(POLICY ${p}) 11 cmake_policy(SET ${p} NEW) 12 endif() 13endforeach() 14 15option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON) 16option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON) 17option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF) 18option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF) 19if(NOT MSVC) 20 option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF) 21else() 22 set(BENCHMARK_BUILD_32_BITS OFF CACHE BOOL "Build a 32 bit version of the library - unsupported when using MSVC)" FORCE) 23endif() 24option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON) 25 26# Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which 27# may require downloading the source code. 28option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF) 29 30# This option can be used to disable building and running unit tests which depend on gtest 31# in cases where it is not possible to build or find a valid version of gtest. 32option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON) 33 34set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF) 35function(should_enable_assembly_tests) 36 if(CMAKE_BUILD_TYPE) 37 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER) 38 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage") 39 # FIXME: The --coverage flag needs to be removed when building assembly 40 # tests for this to work. 41 return() 42 endif() 43 endif() 44 if (MSVC) 45 return() 46 elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") 47 return() 48 elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8) 49 # FIXME: Make these work on 32 bit builds 50 return() 51 elseif(BENCHMARK_BUILD_32_BITS) 52 # FIXME: Make these work on 32 bit builds 53 return() 54 endif() 55 find_program(LLVM_FILECHECK_EXE FileCheck) 56 if (LLVM_FILECHECK_EXE) 57 set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE) 58 message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}") 59 else() 60 message(STATUS "Failed to find LLVM FileCheck") 61 return() 62 endif() 63 set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE) 64endfunction() 65should_enable_assembly_tests() 66 67# This option disables the building and running of the assembly verification tests 68option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests" 69 ${ENABLE_ASSEMBLY_TESTS_DEFAULT}) 70 71# Make sure we can import out CMake functions 72list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules") 73list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 74 75 76# Read the git tags to determine the project version 77include(GetGitVersion) 78get_git_version(GIT_VERSION) 79 80# Tell the user what versions we are using 81string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION}) 82message(STATUS "Version: ${VERSION}") 83 84# The version of the libraries 85set(GENERIC_LIB_VERSION ${VERSION}) 86string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION) 87 88# Import our CMake modules 89include(CheckCXXCompilerFlag) 90include(AddCXXCompilerFlag) 91include(CXXFeatureCheck) 92 93if (BENCHMARK_BUILD_32_BITS) 94 add_required_cxx_compiler_flag(-m32) 95endif() 96 97if (MSVC) 98 # Turn compiler warnings up to 11 99 string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") 101 add_definitions(-D_CRT_SECURE_NO_WARNINGS) 102 103 if (NOT BENCHMARK_ENABLE_EXCEPTIONS) 104 add_cxx_compiler_flag(-EHs-) 105 add_cxx_compiler_flag(-EHa-) 106 add_definitions(-D_HAS_EXCEPTIONS=0) 107 endif() 108 # Link time optimisation 109 if (BENCHMARK_ENABLE_LTO) 110 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL") 111 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG") 112 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG") 113 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG") 114 115 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL") 116 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}") 117 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") 118 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}") 119 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") 120 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}") 121 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") 122 123 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL") 124 set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG") 125 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG") 126 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG") 127 endif() 128else() 129 # Try and enable C++11. Don't use C++14 because it doesn't work in some 130 # configurations. 131 add_cxx_compiler_flag(-std=c++11) 132 if (NOT HAVE_CXX_FLAG_STD_CXX11) 133 add_cxx_compiler_flag(-std=c++0x) 134 endif() 135 136 # Turn compiler warnings up to 11 137 add_cxx_compiler_flag(-Wall) 138 add_cxx_compiler_flag(-Wextra) 139 add_cxx_compiler_flag(-Wshadow) 140 add_cxx_compiler_flag(-Werror RELEASE) 141 add_cxx_compiler_flag(-Werror RELWITHDEBINFO) 142 add_cxx_compiler_flag(-Werror MINSIZEREL) 143 add_cxx_compiler_flag(-pedantic) 144 add_cxx_compiler_flag(-pedantic-errors) 145 add_cxx_compiler_flag(-Wshorten-64-to-32) 146 add_cxx_compiler_flag(-fstrict-aliasing) 147 # Disable warnings regarding deprecated parts of the library while building 148 # and testing those parts of the library. 149 add_cxx_compiler_flag(-Wno-deprecated-declarations) 150 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") 151 # Intel silently ignores '-Wno-deprecated-declarations', 152 # warning no. 1786 must be explicitly disabled. 153 # See #631 for rationale. 154 add_cxx_compiler_flag(-wd1786) 155 endif() 156 # Disable deprecation warnings for release builds (when -Werror is enabled). 157 add_cxx_compiler_flag(-Wno-deprecated RELEASE) 158 add_cxx_compiler_flag(-Wno-deprecated RELWITHDEBINFO) 159 add_cxx_compiler_flag(-Wno-deprecated MINSIZEREL) 160 if (NOT BENCHMARK_ENABLE_EXCEPTIONS) 161 add_cxx_compiler_flag(-fno-exceptions) 162 endif() 163 164 if (HAVE_CXX_FLAG_FSTRICT_ALIASING) 165 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing 166 add_cxx_compiler_flag(-Wstrict-aliasing) 167 endif() 168 endif() 169 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden 170 # (because of deprecated overload) 171 add_cxx_compiler_flag(-wd654) 172 add_cxx_compiler_flag(-Wthread-safety) 173 if (HAVE_CXX_FLAG_WTHREAD_SAFETY) 174 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES) 175 endif() 176 177 # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a 178 # predefined macro, which turns on all of the wonderful libc extensions. 179 # However g++ doesn't do this in Cygwin so we have to define it ourselfs 180 # since we depend on GNU/POSIX/BSD extensions. 181 if (CYGWIN) 182 add_definitions(-D_GNU_SOURCE=1) 183 endif() 184 185 # Link time optimisation 186 if (BENCHMARK_ENABLE_LTO) 187 add_cxx_compiler_flag(-flto) 188 if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") 189 find_program(GCC_AR gcc-ar) 190 if (GCC_AR) 191 set(CMAKE_AR ${GCC_AR}) 192 endif() 193 find_program(GCC_RANLIB gcc-ranlib) 194 if (GCC_RANLIB) 195 set(CMAKE_RANLIB ${GCC_RANLIB}) 196 endif() 197 elseif("${CMAKE_C_COMPILER_ID}" MATCHES "Clang") 198 include(llvm-toolchain) 199 endif() 200 endif() 201 202 # Coverage build type 203 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" 204 CACHE STRING "Flags used by the C++ compiler during coverage builds." 205 FORCE) 206 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" 207 CACHE STRING "Flags used for linking binaries during coverage builds." 208 FORCE) 209 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" 210 CACHE STRING "Flags used by the shared libraries linker during coverage builds." 211 FORCE) 212 mark_as_advanced( 213 BENCHMARK_CXX_FLAGS_COVERAGE 214 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE 215 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE) 216 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING 217 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.") 218 add_cxx_compiler_flag(--coverage COVERAGE) 219endif() 220 221if (BENCHMARK_USE_LIBCXX) 222 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 223 add_cxx_compiler_flag(-stdlib=libc++) 224 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR 225 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel") 226 add_cxx_compiler_flag(-nostdinc++) 227 message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS") 228 # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break 229 # configuration checks such as 'find_package(Threads)' 230 list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs) 231 # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because 232 # linker flags appear before all linker inputs and -lc++ must appear after. 233 list(APPEND BENCHMARK_CXX_LIBRARIES c++) 234 else() 235 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler") 236 endif() 237endif(BENCHMARK_USE_LIBCXX) 238 239# C++ feature checks 240# Determine the correct regular expression engine to use 241cxx_feature_check(STD_REGEX) 242cxx_feature_check(GNU_POSIX_REGEX) 243cxx_feature_check(POSIX_REGEX) 244if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) 245 message(FATAL_ERROR "Failed to determine the source files for the regular expression backend") 246endif() 247if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX 248 AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX) 249 message(WARNING "Using std::regex with exceptions disabled is not fully supported") 250endif() 251cxx_feature_check(STEADY_CLOCK) 252# Ensure we have pthreads 253find_package(Threads REQUIRED) 254 255# Set up directories 256include_directories(${PROJECT_SOURCE_DIR}/include) 257 258# Build the targets 259add_subdirectory(src) 260 261if (BENCHMARK_ENABLE_TESTING) 262 enable_testing() 263 if (BENCHMARK_ENABLE_GTEST_TESTS) 264 include(HandleGTest) 265 endif() 266 add_subdirectory(test) 267endif() 268