1 # Require CMake 3.10. If available, use the policies up to CMake 3.22.
2 cmake_minimum_required (VERSION 3.10...3.22)
4 project (benchmark VERSION 1.8.3 LANGUAGES CXX)
6 option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
7 option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
8 option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)
9 option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)
10 option(BENCHMARK_ENABLE_WERROR "Build Release candidates with -Werror." ON)
11 option(BENCHMARK_FORCE_WERROR "Build Release candidates with -Werror regardless of compiler issues." OFF)
13 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
14 # PGC++ maybe reporting false positives.
15 set(BENCHMARK_ENABLE_WERROR OFF)
17 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "NVHPC")
18 set(BENCHMARK_ENABLE_WERROR OFF)
20 if(BENCHMARK_FORCE_WERROR)
21 set(BENCHMARK_ENABLE_WERROR ON)
22 endif(BENCHMARK_FORCE_WERROR)
24 if(NOT (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))
25 option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)
27 set(BENCHMARK_BUILD_32_BITS OFF CACHE BOOL "Build a 32 bit version of the library - unsupported when using MSVC)" FORCE)
29 option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)
30 option(BENCHMARK_ENABLE_DOXYGEN "Build documentation with Doxygen." OFF)
31 option(BENCHMARK_INSTALL_DOCS "Enable installation of documentation." ON)
33 # Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which
34 # may require downloading the source code.
35 option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF)
37 # This option can be used to disable building and running unit tests which depend on gtest
38 # in cases where it is not possible to build or find a valid version of gtest.
39 option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON)
40 option(BENCHMARK_USE_BUNDLED_GTEST "Use bundled GoogleTest. If disabled, the find_package(GTest) will be used." ON)
42 option(BENCHMARK_ENABLE_LIBPFM "Enable performance counters provided by libpfm" OFF)
44 # Export only public symbols
45 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
46 set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
48 if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
49 # As of CMake 3.18, CMAKE_SYSTEM_PROCESSOR is not set properly for MSVC and
50 # cross-compilation (e.g. Host=x86_64, target=aarch64) requires using the
51 # undocumented, but working variable.
52 # See https://gitlab.kitware.com/cmake/cmake/-/issues/15170
53 set(CMAKE_SYSTEM_PROCESSOR ${MSVC_CXX_ARCHITECTURE_ID})
54 if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ARM")
55 set(CMAKE_CROSSCOMPILING TRUE)
59 set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF)
60 function(should_enable_assembly_tests)
62 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
63 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
64 # FIXME: The --coverage flag needs to be removed when building assembly
65 # tests for this to work.
69 if (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
71 elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
73 elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
74 # FIXME: Make these work on 32 bit builds
76 elseif(BENCHMARK_BUILD_32_BITS)
77 # FIXME: Make these work on 32 bit builds
80 find_program(LLVM_FILECHECK_EXE FileCheck)
81 if (LLVM_FILECHECK_EXE)
82 set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE)
83 message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}")
85 message(STATUS "Failed to find LLVM FileCheck")
88 set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE)
90 should_enable_assembly_tests()
92 # This option disables the building and running of the assembly verification tests
93 option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests"
94 ${ENABLE_ASSEMBLY_TESTS_DEFAULT})
96 # Make sure we can import out CMake functions
97 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
98 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
101 # Read the git tags to determine the project version
102 include(GetGitVersion)
103 get_git_version(GIT_VERSION)
105 # If no git version can be determined, use the version
106 # from the project() command
107 if ("${GIT_VERSION}" STREQUAL "0.0.0")
108 set(VERSION "v${benchmark_VERSION}")
110 set(VERSION "${GIT_VERSION}")
113 # Normalize version: drop "v" prefix, replace first "-" with ".",
114 # drop everything after second "-" (including said "-").
115 string(STRIP ${VERSION} VERSION)
116 if(VERSION MATCHES v[^-]*-)
117 string(REGEX REPLACE "v([^-]*)-([0-9]+)-.*" "\\1.\\2" NORMALIZED_VERSION ${VERSION})
119 string(REGEX REPLACE "v(.*)" "\\1" NORMALIZED_VERSION ${VERSION})
122 # Tell the user what versions we are using
123 message(STATUS "Google Benchmark version: ${VERSION}, normalized to ${NORMALIZED_VERSION}")
125 # The version of the libraries
126 set(GENERIC_LIB_VERSION ${NORMALIZED_VERSION})
127 string(SUBSTRING ${NORMALIZED_VERSION} 0 1 GENERIC_LIB_SOVERSION)
129 # Import our CMake modules
130 include(AddCXXCompilerFlag)
131 include(CheckCXXCompilerFlag)
132 include(CheckLibraryExists)
133 include(CXXFeatureCheck)
135 check_library_exists(rt shm_open "" HAVE_LIB_RT)
137 if (BENCHMARK_BUILD_32_BITS)
138 add_required_cxx_compiler_flag(-m32)
141 if (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
142 set(BENCHMARK_CXX_STANDARD 14)
144 set(BENCHMARK_CXX_STANDARD 11)
147 set(CMAKE_CXX_STANDARD ${BENCHMARK_CXX_STANDARD})
148 set(CMAKE_CXX_STANDARD_REQUIRED YES)
149 set(CMAKE_CXX_EXTENSIONS OFF)
152 # Turn compiler warnings up to 11
153 string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
154 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
155 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
157 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
158 add_cxx_compiler_flag(-EHs-)
159 add_cxx_compiler_flag(-EHa-)
160 add_definitions(-D_HAS_EXCEPTIONS=0)
162 # Link time optimisation
163 if (BENCHMARK_ENABLE_LTO)
164 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
165 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
166 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
167 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
169 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
170 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")
171 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
172 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
173 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
174 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
175 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
177 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
178 set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")
179 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
180 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
183 # Turn on Large-file Support
184 add_definitions(-D_FILE_OFFSET_BITS=64)
185 add_definitions(-D_LARGEFILE64_SOURCE)
186 add_definitions(-D_LARGEFILE_SOURCE)
187 # Turn compiler warnings up to 11
188 add_cxx_compiler_flag(-Wall)
189 add_cxx_compiler_flag(-Wextra)
190 add_cxx_compiler_flag(-Wshadow)
191 add_cxx_compiler_flag(-Wfloat-equal)
192 add_cxx_compiler_flag(-Wold-style-cast)
193 if(BENCHMARK_ENABLE_WERROR)
194 add_cxx_compiler_flag(-Werror)
196 if (NOT BENCHMARK_ENABLE_TESTING)
197 # Disable warning when compiling tests as gtest does not use 'override'.
198 add_cxx_compiler_flag(-Wsuggest-override)
200 add_cxx_compiler_flag(-pedantic)
201 add_cxx_compiler_flag(-pedantic-errors)
202 add_cxx_compiler_flag(-Wshorten-64-to-32)
203 add_cxx_compiler_flag(-fstrict-aliasing)
204 # Disable warnings regarding deprecated parts of the library while building
205 # and testing those parts of the library.
206 add_cxx_compiler_flag(-Wno-deprecated-declarations)
207 if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
208 # Intel silently ignores '-Wno-deprecated-declarations',
209 # warning no. 1786 must be explicitly disabled.
210 # See #631 for rationale.
211 add_cxx_compiler_flag(-wd1786)
212 add_cxx_compiler_flag(-fno-finite-math-only)
214 # Disable deprecation warnings for release builds (when -Werror is enabled).
215 if(BENCHMARK_ENABLE_WERROR)
216 add_cxx_compiler_flag(-Wno-deprecated)
218 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
219 add_cxx_compiler_flag(-fno-exceptions)
222 if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
223 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") #ICC17u2: Many false positives for Wstrict-aliasing
224 add_cxx_compiler_flag(-Wstrict-aliasing)
227 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
228 # (because of deprecated overload)
229 add_cxx_compiler_flag(-wd654)
230 add_cxx_compiler_flag(-Wthread-safety)
231 if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
232 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES "-DINCLUDE_DIRECTORIES=${PROJECT_SOURCE_DIR}/include")
235 # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
236 # predefined macro, which turns on all of the wonderful libc extensions.
237 # However g++ doesn't do this in Cygwin so we have to define it ourselves
238 # since we depend on GNU/POSIX/BSD extensions.
240 add_definitions(-D_GNU_SOURCE=1)
244 add_definitions(-D_QNX_SOURCE)
247 # Link time optimisation
248 if (BENCHMARK_ENABLE_LTO)
249 add_cxx_compiler_flag(-flto)
250 add_cxx_compiler_flag(-Wno-lto-type-mismatch)
251 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
252 find_program(GCC_AR gcc-ar)
254 set(CMAKE_AR ${GCC_AR})
256 find_program(GCC_RANLIB gcc-ranlib)
258 set(CMAKE_RANLIB ${GCC_RANLIB})
260 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
261 include(llvm-toolchain)
265 # Coverage build type
266 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
267 CACHE STRING "Flags used by the C++ compiler during coverage builds."
269 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
270 CACHE STRING "Flags used for linking binaries during coverage builds."
272 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
273 CACHE STRING "Flags used by the shared libraries linker during coverage builds."
276 BENCHMARK_CXX_FLAGS_COVERAGE
277 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
278 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
279 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
280 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
281 add_cxx_compiler_flag(--coverage COVERAGE)
284 if (BENCHMARK_USE_LIBCXX)
285 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
286 add_cxx_compiler_flag(-stdlib=libc++)
287 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
288 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" OR
289 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM")
290 add_cxx_compiler_flag(-nostdinc++)
291 message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
292 # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
293 # configuration checks such as 'find_package(Threads)'
294 list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
295 # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
296 # linker flags appear before all linker inputs and -lc++ must appear after.
297 list(APPEND BENCHMARK_CXX_LIBRARIES c++)
299 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
301 endif(BENCHMARK_USE_LIBCXX)
303 set(EXTRA_CXX_FLAGS "")
304 if (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
305 # Clang on Windows fails to compile the regex feature check under C++11
306 set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14")
310 # Determine the correct regular expression engine to use
311 cxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS})
312 cxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS})
313 cxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS})
314 if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
315 message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
317 if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX
318 AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
319 message(WARNING "Using std::regex with exceptions disabled is not fully supported")
322 cxx_feature_check(STEADY_CLOCK)
323 # Ensure we have pthreads
324 set(THREADS_PREFER_PTHREAD_FLAG ON)
325 find_package(Threads REQUIRED)
326 cxx_feature_check(PTHREAD_AFFINITY)
328 if (BENCHMARK_ENABLE_LIBPFM)
329 find_package(PFM REQUIRED)
333 include_directories(${PROJECT_SOURCE_DIR}/include)
336 add_subdirectory(src)
338 if (BENCHMARK_ENABLE_TESTING)
340 if (BENCHMARK_ENABLE_GTEST_TESTS AND
341 NOT (TARGET gtest AND TARGET gtest_main AND
342 TARGET gmock AND TARGET gmock_main))
343 if (BENCHMARK_USE_BUNDLED_GTEST)
346 find_package(GTest CONFIG REQUIRED)
347 add_library(gtest ALIAS GTest::gtest)
348 add_library(gtest_main ALIAS GTest::gtest_main)
349 add_library(gmock ALIAS GTest::gmock)
350 add_library(gmock_main ALIAS GTest::gmock_main)
353 add_subdirectory(test)