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
30# Make sure we can import out CMake functions
31list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
32list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
33
34# Read the git tags to determine the project version
35include(GetGitVersion)
36get_git_version(GIT_VERSION)
37
38# Tell the user what versions we are using
39string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION})
40message("-- Version: ${VERSION}")
41
42# The version of the libraries
43set(GENERIC_LIB_VERSION ${VERSION})
44string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION)
45
46# Import our CMake modules
47include(CheckCXXCompilerFlag)
48include(AddCXXCompilerFlag)
49include(CXXFeatureCheck)
50
51if (BENCHMARK_BUILD_32_BITS)
52  add_required_cxx_compiler_flag(-m32)
53endif()
54
55if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
56  # Turn compiler warnings up to 11
57  string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
58  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
59  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
60
61  if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
62    add_cxx_compiler_flag(-EHs-)
63    add_cxx_compiler_flag(-EHa-)
64  endif()
65  # Link time optimisation
66  if (BENCHMARK_ENABLE_LTO)
67    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
68    set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
69    set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
70    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
71
72    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
73    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")
74    set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
75    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
76    set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
77    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
78    set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
79
80    set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
81    set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")
82    set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
83    set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
84  endif()
85else()
86  # Try and enable C++11. Don't use C++14 because it doesn't work in some
87  # configurations.
88  add_cxx_compiler_flag(-std=c++11)
89  if (NOT HAVE_CXX_FLAG_STD_CXX11)
90    add_cxx_compiler_flag(-std=c++0x)
91  endif()
92
93  # Turn compiler warnings up to 11
94  add_cxx_compiler_flag(-Wall)
95
96  add_cxx_compiler_flag(-Wextra)
97  add_cxx_compiler_flag(-Wshadow)
98  add_cxx_compiler_flag(-Werror RELEASE)
99  add_cxx_compiler_flag(-Werror RELWITHDEBINFO)
100  add_cxx_compiler_flag(-Werror MINSIZEREL)
101  add_cxx_compiler_flag(-pedantic)
102  add_cxx_compiler_flag(-pedantic-errors)
103  add_cxx_compiler_flag(-Wshorten-64-to-32)
104  add_cxx_compiler_flag(-Wfloat-equal)
105  add_cxx_compiler_flag(-fstrict-aliasing)
106  if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
107    add_cxx_compiler_flag(-fno-exceptions)
108  endif()
109
110  if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
111    if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing
112      add_cxx_compiler_flag(-Wstrict-aliasing)
113    endif()
114  endif()
115  # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
116  # (because of deprecated overload)
117  add_cxx_compiler_flag(-wd654)
118  add_cxx_compiler_flag(-Wthread-safety)
119  if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
120    cxx_feature_check(THREAD_SAFETY_ATTRIBUTES)
121  endif()
122
123  # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
124  # predefined macro, which turns on all of the wonderful libc extensions.
125  # However g++ doesn't do this in Cygwin so we have to define it ourselfs
126  # since we depend on GNU/POSIX/BSD extensions.
127  if (CYGWIN)
128    add_definitions(-D_GNU_SOURCE=1)
129  endif()
130
131  # Link time optimisation
132  if (BENCHMARK_ENABLE_LTO)
133    add_cxx_compiler_flag(-flto)
134    if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
135      find_program(GCC_AR gcc-ar)
136      if (GCC_AR)
137        set(CMAKE_AR ${GCC_AR})
138      endif()
139      find_program(GCC_RANLIB gcc-ranlib)
140      if (GCC_RANLIB)
141        set(CMAKE_RANLIB ${GCC_RANLIB})
142      endif()
143    elseif("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
144      include(llvm-toolchain)
145    endif()
146  endif()
147
148  # Coverage build type
149  set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
150    CACHE STRING "Flags used by the C++ compiler during coverage builds."
151    FORCE)
152  set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
153    CACHE STRING "Flags used for linking binaries during coverage builds."
154    FORCE)
155  set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
156    CACHE STRING "Flags used by the shared libraries linker during coverage builds."
157    FORCE)
158  mark_as_advanced(
159    BENCHMARK_CXX_FLAGS_COVERAGE
160    BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
161    BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
162  set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
163    "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
164  add_cxx_compiler_flag(--coverage COVERAGE)
165endif()
166
167if (BENCHMARK_USE_LIBCXX)
168  if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
169    add_cxx_compiler_flag(-stdlib=libc++)
170  elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
171          "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
172    add_cxx_compiler_flag(-nostdinc++)
173    message("libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
174    # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
175    # configuration checks such as 'find_package(Threads)'
176    list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
177    # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
178    # linker flags appear before all linker inputs and -lc++ must appear after.
179    list(APPEND BENCHMARK_CXX_LIBRARIES c++)
180  else()
181    message(FATAL "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
182  endif()
183endif(BENCHMARK_USE_LIBCXX)
184
185# C++ feature checks
186# Determine the correct regular expression engine to use
187cxx_feature_check(STD_REGEX)
188cxx_feature_check(GNU_POSIX_REGEX)
189cxx_feature_check(POSIX_REGEX)
190if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
191  message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
192endif()
193if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX
194        AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
195  message(WARNING "Using std::regex with exceptions disabled is not fully supported")
196endif()
197cxx_feature_check(STEADY_CLOCK)
198# Ensure we have pthreads
199find_package(Threads REQUIRED)
200
201# Set up directories
202include_directories(${PROJECT_SOURCE_DIR}/include)
203
204# Build the targets
205add_subdirectory(src)
206
207if (BENCHMARK_ENABLE_TESTING)
208  enable_testing()
209  if (BENCHMARK_ENABLE_GTEST_TESTS)
210    include(HandleGTest)
211  endif()
212  add_subdirectory(test)
213endif()
214