• 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    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
198      find_program(GCC_AR gcc-ar)
199      if (GCC_AR)
200        set(CMAKE_AR ${GCC_AR})
201      endif()
202      find_program(GCC_RANLIB gcc-ranlib)
203      if (GCC_RANLIB)
204        set(CMAKE_RANLIB ${GCC_RANLIB})
205      endif()
206    elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
207      include(llvm-toolchain)
208    endif()
209  endif()
210
211  # Coverage build type
212  set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
213    CACHE STRING "Flags used by the C++ compiler during coverage builds."
214    FORCE)
215  set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
216    CACHE STRING "Flags used for linking binaries during coverage builds."
217    FORCE)
218  set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
219    CACHE STRING "Flags used by the shared libraries linker during coverage builds."
220    FORCE)
221  mark_as_advanced(
222    BENCHMARK_CXX_FLAGS_COVERAGE
223    BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
224    BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
225  set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
226    "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
227  add_cxx_compiler_flag(--coverage COVERAGE)
228endif()
229
230if (BENCHMARK_USE_LIBCXX)
231  if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
232    add_cxx_compiler_flag(-stdlib=libc++)
233  elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
234          "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
235    add_cxx_compiler_flag(-nostdinc++)
236    message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
237    # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
238    # configuration checks such as 'find_package(Threads)'
239    list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
240    # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
241    # linker flags appear before all linker inputs and -lc++ must appear after.
242    list(APPEND BENCHMARK_CXX_LIBRARIES c++)
243  else()
244    message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
245  endif()
246endif(BENCHMARK_USE_LIBCXX)
247
248set(EXTRA_CXX_FLAGS "")
249if (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
250  # Clang on Windows fails to compile the regex feature check under C++11
251  set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14")
252endif()
253
254# C++ feature checks
255# Determine the correct regular expression engine to use
256cxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS})
257cxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS})
258cxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS})
259if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
260  message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
261endif()
262if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX
263        AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
264  message(WARNING "Using std::regex with exceptions disabled is not fully supported")
265endif()
266
267cxx_feature_check(STEADY_CLOCK)
268# Ensure we have pthreads
269set(THREADS_PREFER_PTHREAD_FLAG ON)
270find_package(Threads REQUIRED)
271
272# Set up directories
273include_directories(${PROJECT_SOURCE_DIR}/include)
274
275# Build the targets
276add_subdirectory(src)
277
278if (BENCHMARK_ENABLE_TESTING)
279  enable_testing()
280  if (BENCHMARK_ENABLE_GTEST_TESTS AND
281      NOT (TARGET gtest AND TARGET gtest_main AND
282           TARGET gmock AND TARGET gmock_main))
283    include(GoogleTest)
284  endif()
285  add_subdirectory(test)
286endif()
287