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