1cmake_minimum_required (VERSION 2.8.11)
2project (benchmark)
3
4foreach(p
5    CMP0054 # CMake 3.1
6    CMP0056 # export EXE_LINKER_FLAGS to try_run
7    )
8  if(POLICY ${p})
9    cmake_policy(SET ${p} NEW)
10  endif()
11endforeach()
12
13option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
14option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)
15option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)
16# Make sure we can import out CMake functions
17list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
18
19# Read the git tags to determine the project version
20include(GetGitVersion)
21get_git_version(GIT_VERSION)
22
23# Tell the user what versions we are using
24string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION})
25message("-- Version: ${VERSION}")
26
27# The version of the libraries
28set(GENERIC_LIB_VERSION ${VERSION})
29string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION)
30
31# Import our CMake modules
32include(CheckCXXCompilerFlag)
33include(AddCXXCompilerFlag)
34include(CXXFeatureCheck)
35
36if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
37  # Turn compiler warnings up to 11
38  string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
39  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
40  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
41
42  # Link time optimisation
43  if (BENCHMARK_ENABLE_LTO)
44    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
45    set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
46    set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
47    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
48
49    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
50    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")
51    set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
52    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
53    set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
54    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
55    set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
56
57    set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
58    set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")
59    set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
60    set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
61  endif()
62else()
63  # Try and enable C++11. Don't use C++14 because it doesn't work in some
64  # configurations.
65  add_cxx_compiler_flag(-std=c++11)
66  if (NOT HAVE_CXX_FLAG_STD_CXX11)
67    add_cxx_compiler_flag(-std=c++0x)
68  endif()
69
70  # Turn compiler warnings up to 11
71  add_cxx_compiler_flag(-Wall)
72
73  add_cxx_compiler_flag(-Wextra)
74  add_cxx_compiler_flag(-Wshadow)
75  add_cxx_compiler_flag(-Werror RELEASE)
76  add_cxx_compiler_flag(-Werror RELWITHDEBINFO)
77  add_cxx_compiler_flag(-Werror MINSIZEREL)
78  add_cxx_compiler_flag(-pedantic)
79  add_cxx_compiler_flag(-pedantic-errors)
80  add_cxx_compiler_flag(-Wshorten-64-to-32)
81  add_cxx_compiler_flag(-Wfloat-equal)
82  add_cxx_compiler_flag(-fstrict-aliasing)
83  if (NOT BENCHMARK_USE_LIBCXX)
84    add_cxx_compiler_flag(-Wzero-as-null-pointer-constant)
85  endif()
86  if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
87    add_cxx_compiler_flag(-Wstrict-aliasing)
88  endif()
89  add_cxx_compiler_flag(-Wthread-safety)
90  if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
91    cxx_feature_check(THREAD_SAFETY_ATTRIBUTES)
92  endif()
93
94  # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
95  # predefined macro, which turns on all of the wonderful libc extensions.
96  # However g++ doesn't do this in Cygwin so we have to define it ourselfs
97  # since we depend on GNU/POSIX/BSD extensions.
98  if (CYGWIN)
99    add_definitions(-D_GNU_SOURCE=1)
100  endif()
101
102  # Link time optimisation
103  if (BENCHMARK_ENABLE_LTO)
104    add_cxx_compiler_flag(-flto)
105    if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
106      find_program(GCC_AR gcc-ar)
107      if (GCC_AR)
108        set(CMAKE_AR ${GCC_AR})
109      endif()
110      find_program(GCC_RANLIB gcc-ranlib)
111      if (GCC_RANLIB)
112        set(CMAKE_RANLIB ${GCC_RANLIB})
113      endif()
114    endif()
115  endif()
116
117  # Coverage build type
118  set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" CACHE STRING
119    "Flags used by the C++ compiler during coverage builds."
120    FORCE)
121  set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
122    "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" CACHE STRING
123    "Flags used for linking binaries during coverage builds."
124    FORCE)
125  set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
126    "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE STRING
127    "Flags used by the shared libraries linker during coverage builds."
128    FORCE)
129  mark_as_advanced(
130    CMAKE_CXX_FLAGS_COVERAGE
131    CMAKE_EXE_LINKER_FLAGS_COVERAGE
132    CMAKE_SHARED_LINKER_FLAGS_COVERAGE)
133  set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
134    "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage."
135    FORCE)
136  add_cxx_compiler_flag(--coverage COVERAGE)
137endif()
138
139if (BENCHMARK_USE_LIBCXX)
140  if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
141    add_cxx_compiler_flag(-stdlib=libc++)
142  elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
143          "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
144    add_cxx_compiler_flag(-nostdinc++)
145    message("libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
146    # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
147    # configuration checks such as 'find_package(Threads)'
148    list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
149    # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
150    # linker flags appear before all linker inputs and -lc++ must appear after.
151    list(APPEND BENCHMARK_CXX_LIBRARIES c++)
152  else()
153    message(FATAL "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
154  endif()
155endif(BENCHMARK_USE_LIBCXX)
156
157# C++ feature checks
158# Determine the correct regular expression engine to use
159cxx_feature_check(STD_REGEX)
160cxx_feature_check(GNU_POSIX_REGEX)
161cxx_feature_check(POSIX_REGEX)
162if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
163  message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
164endif()
165
166cxx_feature_check(STEADY_CLOCK)
167# Ensure we have pthreads
168find_package(Threads REQUIRED)
169
170# Set up directories
171include_directories(${PROJECT_SOURCE_DIR}/include)
172
173# Build the targets
174add_subdirectory(src)
175
176if (BENCHMARK_ENABLE_TESTING)
177  enable_testing()
178  add_subdirectory(test)
179endif()
180