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