1 cmake_minimum_required (VERSION 3.5.1)
4 CMP0048 # OK to clear PROJECT_VERSION on project()
6 CMP0056 # export EXE_LINKER_FLAGS to try_run
7 CMP0057 # Support no if() IN_LIST operator
8 CMP0063 # Honor visibility properties for all targets
9 CMP0077 # Allow option() overrides in importing projects
12 cmake_policy(SET ${p} NEW)
16 project (benchmark VERSION 1.6.0 LANGUAGES CXX)
18 option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
19 option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
20 option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)
21 option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)
22 option(BENCHMARK_ENABLE_WERROR "Build Release candidates with -Werror." ON)
23 option(BENCHMARK_FORCE_WERROR "Build Release candidates with -Werror regardless of compiler issues." OFF)
25 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
26 # PGC++ maybe reporting false positives.
27 set(BENCHMARK_ENABLE_WERROR OFF)
29 if(BENCHMARK_FORCE_WERROR)
30 set(BENCHMARK_ENABLE_WERROR ON)
31 endif(BENCHMARK_FORCE_WERROR)
34 option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)
36 set(BENCHMARK_BUILD_32_BITS OFF CACHE BOOL "Build a 32 bit version of the library - unsupported when using MSVC)" FORCE)
38 option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)
39 option(BENCHMARK_ENABLE_DOXYGEN "Build documentation with Doxygen." OFF)
40 option(BENCHMARK_INSTALL_DOCS "Enable installation of documentation." ON)
42 # Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which
43 # may require downloading the source code.
44 option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF)
46 # This option can be used to disable building and running unit tests which depend on gtest
47 # in cases where it is not possible to build or find a valid version of gtest.
48 option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON)
49 option(BENCHMARK_USE_BUNDLED_GTEST "Use bundled GoogleTest. If disabled, the find_package(GTest) will be used." ON)
51 option(BENCHMARK_ENABLE_LIBPFM "Enable performance counters provided by libpfm" OFF)
53 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
55 # As of CMake 3.18, CMAKE_SYSTEM_PROCESSOR is not set properly for MSVC and
56 # cross-compilation (e.g. Host=x86_64, target=aarch64) requires using the
57 # undocumented, but working variable.
58 # See https://gitlab.kitware.com/cmake/cmake/-/issues/15170
59 set(CMAKE_SYSTEM_PROCESSOR ${MSVC_CXX_ARCHITECTURE_ID})
60 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ARM")
61 set(CMAKE_CROSSCOMPILING TRUE)
65 set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF)
66 function(should_enable_assembly_tests)
68 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
69 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
70 # FIXME: The --coverage flag needs to be removed when building assembly
71 # tests for this to work.
77 elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
79 elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
80 # FIXME: Make these work on 32 bit builds
82 elseif(BENCHMARK_BUILD_32_BITS)
83 # FIXME: Make these work on 32 bit builds
86 find_program(LLVM_FILECHECK_EXE FileCheck)
87 if (LLVM_FILECHECK_EXE)
88 set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE)
89 message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}")
91 message(STATUS "Failed to find LLVM FileCheck")
94 set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE)
96 should_enable_assembly_tests()
98 # This option disables the building and running of the assembly verification tests
99 option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests"
100 ${ENABLE_ASSEMBLY_TESTS_DEFAULT})
102 # Make sure we can import out CMake functions
103 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
104 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
107 # Read the git tags to determine the project version
108 include(GetGitVersion)
109 get_git_version(GIT_VERSION)
111 # If no git version can be determined, use the version
112 # from the project() command
113 if ("${GIT_VERSION}" STREQUAL "0.0.0")
114 set(VERSION "${benchmark_VERSION}")
116 set(VERSION "${GIT_VERSION}")
118 # Tell the user what versions we are using
119 message(STATUS "Version: ${VERSION}")
121 # The version of the libraries
122 set(GENERIC_LIB_VERSION ${VERSION})
123 string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION)
125 # Import our CMake modules
126 include(CheckCXXCompilerFlag)
127 include(AddCXXCompilerFlag)
128 include(CXXFeatureCheck)
129 include(CheckLibraryExists)
131 check_library_exists(rt shm_open "" HAVE_LIB_RT)
133 if (BENCHMARK_BUILD_32_BITS)
134 add_required_cxx_compiler_flag(-m32)
138 # Turn compiler warnings up to 11
139 string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
140 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
141 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
143 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
144 add_cxx_compiler_flag(-EHs-)
145 add_cxx_compiler_flag(-EHa-)
146 add_definitions(-D_HAS_EXCEPTIONS=0)
148 # Link time optimisation
149 if (BENCHMARK_ENABLE_LTO)
150 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
151 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
152 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
153 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
155 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
156 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")
157 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
158 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
159 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
160 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
161 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
163 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
164 set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")
165 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
166 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
169 # Try and enable C++11. Don't use C++14 because it doesn't work in some
171 add_cxx_compiler_flag(-std=c++11)
172 if (NOT HAVE_CXX_FLAG_STD_CXX11)
173 add_cxx_compiler_flag(-std=c++0x)
176 # Turn compiler warnings up to 11
177 add_cxx_compiler_flag(-Wall)
178 add_cxx_compiler_flag(-Wextra)
179 add_cxx_compiler_flag(-Wshadow)
180 if(BENCHMARK_ENABLE_WERROR)
181 add_cxx_compiler_flag(-Werror RELEASE)
182 add_cxx_compiler_flag(-Werror RELWITHDEBINFO)
183 add_cxx_compiler_flag(-Werror MINSIZEREL)
185 if (NOT BENCHMARK_ENABLE_TESTING)
186 # Disable warning when compiling tests as gtest does not use 'override'.
187 add_cxx_compiler_flag(-Wsuggest-override)
189 add_cxx_compiler_flag(-pedantic)
190 add_cxx_compiler_flag(-pedantic-errors)
191 add_cxx_compiler_flag(-Wshorten-64-to-32)
192 add_cxx_compiler_flag(-fstrict-aliasing)
193 # Disable warnings regarding deprecated parts of the library while building
194 # and testing those parts of the library.
195 add_cxx_compiler_flag(-Wno-deprecated-declarations)
196 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
197 # Intel silently ignores '-Wno-deprecated-declarations',
198 # warning no. 1786 must be explicitly disabled.
199 # See #631 for rationale.
200 add_cxx_compiler_flag(-wd1786)
202 # Disable deprecation warnings for release builds (when -Werror is enabled).
203 if(BENCHMARK_ENABLE_WERROR)
204 add_cxx_compiler_flag(-Wno-deprecated RELEASE)
205 add_cxx_compiler_flag(-Wno-deprecated RELWITHDEBINFO)
206 add_cxx_compiler_flag(-Wno-deprecated MINSIZEREL)
208 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
209 add_cxx_compiler_flag(-fno-exceptions)
212 if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
213 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing
214 add_cxx_compiler_flag(-Wstrict-aliasing)
217 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
218 # (because of deprecated overload)
219 add_cxx_compiler_flag(-wd654)
220 add_cxx_compiler_flag(-Wthread-safety)
221 if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
222 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES)
225 # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
226 # predefined macro, which turns on all of the wonderful libc extensions.
227 # However g++ doesn't do this in Cygwin so we have to define it ourselfs
228 # since we depend on GNU/POSIX/BSD extensions.
230 add_definitions(-D_GNU_SOURCE=1)
234 add_definitions(-D_QNX_SOURCE)
237 # Link time optimisation
238 if (BENCHMARK_ENABLE_LTO)
239 add_cxx_compiler_flag(-flto)
240 add_cxx_compiler_flag(-Wno-lto-type-mismatch)
241 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
242 find_program(GCC_AR gcc-ar)
244 set(CMAKE_AR ${GCC_AR})
246 find_program(GCC_RANLIB gcc-ranlib)
248 set(CMAKE_RANLIB ${GCC_RANLIB})
250 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
251 include(llvm-toolchain)
255 # Coverage build type
256 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
257 CACHE STRING "Flags used by the C++ compiler during coverage builds."
259 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
260 CACHE STRING "Flags used for linking binaries during coverage builds."
262 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
263 CACHE STRING "Flags used by the shared libraries linker during coverage builds."
266 BENCHMARK_CXX_FLAGS_COVERAGE
267 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
268 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
269 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
270 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
271 add_cxx_compiler_flag(--coverage COVERAGE)
274 if (BENCHMARK_USE_LIBCXX)
275 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
276 add_cxx_compiler_flag(-stdlib=libc++)
277 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
278 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
279 add_cxx_compiler_flag(-nostdinc++)
280 message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
281 # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
282 # configuration checks such as 'find_package(Threads)'
283 list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
284 # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
285 # linker flags appear before all linker inputs and -lc++ must appear after.
286 list(APPEND BENCHMARK_CXX_LIBRARIES c++)
288 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
290 endif(BENCHMARK_USE_LIBCXX)
292 set(EXTRA_CXX_FLAGS "")
293 if (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
294 # Clang on Windows fails to compile the regex feature check under C++11
295 set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14")
299 # Determine the correct regular expression engine to use
300 cxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS})
301 cxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS})
302 cxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS})
303 if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
304 message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
306 if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX
307 AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
308 message(WARNING "Using std::regex with exceptions disabled is not fully supported")
311 cxx_feature_check(STEADY_CLOCK)
312 # Ensure we have pthreads
313 set(THREADS_PREFER_PTHREAD_FLAG ON)
314 find_package(Threads REQUIRED)
316 if (BENCHMARK_ENABLE_LIBPFM)
321 include_directories(${PROJECT_SOURCE_DIR}/include)
324 add_subdirectory(src)
326 if (BENCHMARK_ENABLE_TESTING)
328 if (BENCHMARK_ENABLE_GTEST_TESTS AND
329 NOT (TARGET gtest AND TARGET gtest_main AND
330 TARGET gmock AND TARGET gmock_main))
331 if (BENCHMARK_USE_BUNDLED_GTEST)
334 find_package(GTest CONFIG REQUIRED)
335 add_library(gtest ALIAS GTest::gtest)
336 add_library(gtest_main ALIAS GTest::gtest_main)
337 add_library(gmock ALIAS GTest::gmock)
338 add_library(gmock_main ALIAS GTest::gmock_main)
341 add_subdirectory(test)