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