1 cmake_minimum_required (VERSION 2.8.12)
3 # Tell cmake 3.0+ that it's safe to clear the PROJECT_VERSION variable in the
4 # call to project() below.
6 cmake_policy(SET CMP0048 NEW)
13 CMP0056 # export EXE_LINKER_FLAGS to try_run
14 CMP0057 # Support no if() IN_LIST operator
17 cmake_policy(SET ${p} NEW)
21 option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
22 option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
23 option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)
24 option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)
25 option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)
26 option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)
28 # Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which
29 # may require downloading the source code.
30 option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF)
32 # This option can be used to disable building and running unit tests which depend on gtest
33 # in cases where it is not possible to build or find a valid version of gtest.
34 option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" OFF)
36 set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF)
37 function(should_enable_assembly_tests)
39 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
40 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
41 # FIXME: The --coverage flag needs to be removed when building assembly
42 # tests for this to work.
48 elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
50 elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
51 # FIXME: Make these work on 32 bit builds
53 elseif(BENCHMARK_BUILD_32_BITS)
54 # FIXME: Make these work on 32 bit builds
57 find_program(LLVM_FILECHECK_EXE FileCheck)
58 if (LLVM_FILECHECK_EXE)
59 set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE)
60 message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}")
62 message(STATUS "Failed to find LLVM FileCheck")
65 set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE)
67 should_enable_assembly_tests()
69 # This option disables the building and running of the assembly verification tests
70 option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests"
71 ${ENABLE_ASSEMBLY_TESTS_DEFAULT})
73 # Make sure we can import out CMake functions
74 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
75 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
78 # Read the git tags to determine the project version
79 # WARNING: This is meaningless for when the benchmark library is being built in-tree,
80 # so disable it and hardcode a null version.
81 # include(GetGitVersion)
82 # get_git_version(GIT_VERSION)
83 set(GIT_VERSION "v0.0.0")
85 # Tell the user what versions we are using
86 string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION})
87 message("-- Version: ${VERSION}")
89 # The version of the libraries
90 set(GENERIC_LIB_VERSION ${VERSION})
91 string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION)
93 # Import our CMake modules
94 include(CheckCXXCompilerFlag)
95 include(AddCXXCompilerFlag)
96 include(CXXFeatureCheck)
98 if (BENCHMARK_BUILD_32_BITS)
99 add_required_cxx_compiler_flag(-m32)
103 # Turn compiler warnings up to 11
104 string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
105 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
106 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
108 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
109 add_cxx_compiler_flag(-EHs-)
110 add_cxx_compiler_flag(-EHa-)
111 add_definitions(-D_HAS_EXCEPTIONS=0)
113 # Link time optimisation
114 if (BENCHMARK_ENABLE_LTO)
115 set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
116 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
117 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
118 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
120 set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
121 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")
122 set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
123 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
124 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
125 string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
126 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
128 set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
129 set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")
130 set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
131 set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
134 # Try and enable C++11. Don't use C++14 because it doesn't work in some
136 add_cxx_compiler_flag(-std=c++11)
137 if (NOT HAVE_CXX_FLAG_STD_CXX11)
138 add_cxx_compiler_flag(-std=c++0x)
141 # Turn compiler warnings up to 11
142 add_cxx_compiler_flag(-Wall)
144 add_cxx_compiler_flag(-Wextra)
145 add_cxx_compiler_flag(-Wshadow)
146 # FIXME(kbobyrev): Document this change.
147 # add_cxx_compiler_flag(-Werror RELEASE)
148 # add_cxx_compiler_flag(-Werror RELWITHDEBINFO)
149 # add_cxx_compiler_flag(-Werror MINSIZEREL)
150 add_cxx_compiler_flag(-pedantic)
151 add_cxx_compiler_flag(-pedantic-errors)
152 add_cxx_compiler_flag(-Wshorten-64-to-32)
153 add_cxx_compiler_flag(-Wfloat-equal)
154 add_cxx_compiler_flag(-fstrict-aliasing)
155 if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
156 add_cxx_compiler_flag(-fno-exceptions)
159 if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
160 if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing
161 add_cxx_compiler_flag(-Wstrict-aliasing)
164 # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
165 # (because of deprecated overload)
166 add_cxx_compiler_flag(-wd654)
167 add_cxx_compiler_flag(-Wthread-safety)
168 if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
169 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES)
172 # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
173 # predefined macro, which turns on all of the wonderful libc extensions.
174 # However g++ doesn't do this in Cygwin so we have to define it ourselfs
175 # since we depend on GNU/POSIX/BSD extensions.
177 add_definitions(-D_GNU_SOURCE=1)
180 # Link time optimisation
181 if (BENCHMARK_ENABLE_LTO)
182 add_cxx_compiler_flag(-flto)
183 if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
184 find_program(GCC_AR gcc-ar)
186 set(CMAKE_AR ${GCC_AR})
188 find_program(GCC_RANLIB gcc-ranlib)
190 set(CMAKE_RANLIB ${GCC_RANLIB})
192 elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
193 include(llvm-toolchain)
197 # Coverage build type
198 set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
199 CACHE STRING "Flags used by the C++ compiler during coverage builds."
201 set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
202 CACHE STRING "Flags used for linking binaries during coverage builds."
204 set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
205 CACHE STRING "Flags used by the shared libraries linker during coverage builds."
208 BENCHMARK_CXX_FLAGS_COVERAGE
209 BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
210 BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
211 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
212 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
213 add_cxx_compiler_flag(--coverage COVERAGE)
216 if (BENCHMARK_USE_LIBCXX)
217 if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
218 add_cxx_compiler_flag(-stdlib=libc++)
219 elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
220 "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
221 add_cxx_compiler_flag(-nostdinc++)
222 message("libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
223 # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
224 # configuration checks such as 'find_package(Threads)'
225 list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
226 # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
227 # linker flags appear before all linker inputs and -lc++ must appear after.
228 list(APPEND BENCHMARK_CXX_LIBRARIES c++)
230 message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
232 endif(BENCHMARK_USE_LIBCXX)
235 # Determine the correct regular expression engine to use
236 cxx_feature_check(STD_REGEX)
237 cxx_feature_check(GNU_POSIX_REGEX)
238 cxx_feature_check(POSIX_REGEX)
239 if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
240 message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
242 if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX
243 AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
244 message(WARNING "Using std::regex with exceptions disabled is not fully supported")
246 cxx_feature_check(STEADY_CLOCK)
247 # Ensure we have pthreads
248 find_package(Threads REQUIRED)
251 include_directories(${PROJECT_SOURCE_DIR}/include)
254 add_subdirectory(src)
256 if (BENCHMARK_ENABLE_TESTING)
258 if (BENCHMARK_ENABLE_GTEST_TESTS)
261 add_subdirectory(test)