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