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