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