blob: 310c7ee9f6b2b792726a7cc644bdd7855274f588 [file] [log] [blame]
Eric Fiselier688edc72017-04-18 07:17:20 +00001cmake_minimum_required (VERSION 2.8.12)
Eric Fiselierfd2e3e92018-01-18 04:23:01 +00002
Eric Fiselierd9b9ef72016-07-19 23:07:03 +00003project (benchmark)
4
5foreach(p
6 CMP0054 # CMake 3.1
7 CMP0056 # export EXE_LINKER_FLAGS to try_run
Eric Fiselierfd2e3e92018-01-18 04:23:01 +00008 CMP0057 # Support no if() IN_LIST operator
Eric Fiselierd9b9ef72016-07-19 23:07:03 +00009 )
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)
Eric Fiselier688edc72017-04-18 07:17:20 +000016option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +000017option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)
Eric Fiselierf76a0872016-08-29 19:12:01 +000018option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)
Eric Fiselier622fc112018-11-15 19:22:53 +000019if(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()
Eric Fiselierfd2e3e92018-01-18 04:23:01 +000024option(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)
Eric Fiselier688edc72017-04-18 07:17:20 +000033
Eric Fiselierffc31db2018-07-10 04:02:00 +000034set(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
Eric Fiselierd9b9ef72016-07-19 23:07:03 +000071# Make sure we can import out CMake functions
Eric Fiselierfd2e3e92018-01-18 04:23:01 +000072list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
Eric Fiselierd9b9ef72016-07-19 23:07:03 +000073list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
74
Eric Fiselierffc31db2018-07-10 04:02:00 +000075
Eric Fiselierd9b9ef72016-07-19 23:07:03 +000076# 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})
Eric Fiselier622fc112018-11-15 19:22:53 +000082message(STATUS "Version: ${VERSION}")
Eric Fiselierd9b9ef72016-07-19 23:07:03 +000083
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
Eric Fiselier688edc72017-04-18 07:17:20 +000093if (BENCHMARK_BUILD_32_BITS)
94 add_required_cxx_compiler_flag(-m32)
95endif()
96
Eric Fiselier622fc112018-11-15 19:22:53 +000097if (MSVC)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +000098 # 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
Eric Fiselier688edc72017-04-18 07:17:20 +0000103 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
104 add_cxx_compiler_flag(-EHs-)
105 add_cxx_compiler_flag(-EHa-)
Eric Fiselier622fc112018-11-15 19:22:53 +0000106 add_definitions(-D_HAS_EXCEPTIONS=0)
Eric Fiselier688edc72017-04-18 07:17:20 +0000107 endif()
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000108 # 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)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000138 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)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000146 add_cxx_compiler_flag(-fstrict-aliasing)
Eric Fiselierffc31db2018-07-10 04:02:00 +0000147 # 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)
Eric Fiselier688edc72017-04-18 07:17:20 +0000160 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
161 add_cxx_compiler_flag(-fno-exceptions)
162 endif()
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000163
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000164 if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
Eric Fiselier688edc72017-04-18 07:17:20 +0000165 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing
166 add_cxx_compiler_flag(-Wstrict-aliasing)
167 endif()
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000168 endif()
Eric Fiselier688edc72017-04-18 07:17:20 +0000169 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
170 # (because of deprecated overload)
Eric Fiselier622fc112018-11-15 19:22:53 +0000171 add_cxx_compiler_flag(-wd654)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000172 add_cxx_compiler_flag(-Wthread-safety)
Eric Fiselierd87eb992016-11-05 00:30:27 +0000173 if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000174 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES)
175 endif()
176
Eric Fiselierd87eb992016-11-05 00:30:27 +0000177 # 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
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000185 # 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()
Eric Fiselier622fc112018-11-15 19:22:53 +0000197 elseif("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000198 include(llvm-toolchain)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000199 endif()
200 endif()
201
202 # Coverage build type
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000203 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
204 CACHE STRING "Flags used by the C++ compiler during coverage builds."
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000205 FORCE)
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000206 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
207 CACHE STRING "Flags used for linking binaries during coverage builds."
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000208 FORCE)
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000209 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
210 CACHE STRING "Flags used by the shared libraries linker during coverage builds."
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000211 FORCE)
212 mark_as_advanced(
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000213 BENCHMARK_CXX_FLAGS_COVERAGE
214 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
215 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000216 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000217 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000218 add_cxx_compiler_flag(--coverage COVERAGE)
219endif()
220
Eric Fiselierf76a0872016-08-29 19:12:01 +0000221if (BENCHMARK_USE_LIBCXX)
Eric Fiselier622fc112018-11-15 19:22:53 +0000222 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
Eric Fiselierf76a0872016-08-29 19:12:01 +0000223 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++)
Eric Fiselier622fc112018-11-15 19:22:53 +0000227 message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
Eric Fiselierf76a0872016-08-29 19:12:01 +0000228 # 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()
Eric Fiselierffc31db2018-07-10 04:02:00 +0000235 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
Eric Fiselierf76a0872016-08-29 19:12:01 +0000236 endif()
237endif(BENCHMARK_USE_LIBCXX)
238
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000239# C++ feature checks
Eric Fiselierd87eb992016-11-05 00:30:27 +0000240# Determine the correct regular expression engine to use
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000241cxx_feature_check(STD_REGEX)
242cxx_feature_check(GNU_POSIX_REGEX)
243cxx_feature_check(POSIX_REGEX)
Eric Fiselierd87eb992016-11-05 00:30:27 +0000244if(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()
Eric Fiselier688edc72017-04-18 07:17:20 +0000247if (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()
Eric Fiselierd87eb992016-11-05 00:30:27 +0000251cxx_feature_check(STEADY_CLOCK)
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000252# 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()
Eric Fiselierfd2e3e92018-01-18 04:23:01 +0000263 if (BENCHMARK_ENABLE_GTEST_TESTS)
264 include(HandleGTest)
265 endif()
Eric Fiselierd9b9ef72016-07-19 23:07:03 +0000266 add_subdirectory(test)
267endif()