1cmake_minimum_required (VERSION 2.8.11) 2project (benchmark) 3 4foreach(p 5 CMP0054 # CMake 3.1 6 ) 7 if(POLICY ${p}) 8 cmake_policy(SET ${p} NEW) 9 endif() 10endforeach() 11 12option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON) 13option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF) 14# Make sure we can import out CMake functions 15list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 16 17# Read the git tags to determine the project version 18include(GetGitVersion) 19get_git_version(GIT_VERSION) 20 21# Tell the user what versions we are using 22string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION}) 23message("-- Version: ${VERSION}") 24 25# The version of the libraries 26set(GENERIC_LIB_VERSION ${VERSION}) 27string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION) 28 29# Import our CMake modules 30include(CheckCXXCompilerFlag) 31include(AddCXXCompilerFlag) 32include(CXXFeatureCheck) 33 34if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") 35 # Turn compiler warnings up to 11 36 add_cxx_compiler_flag(-W4) 37 add_definitions(-D_CRT_SECURE_NO_WARNINGS) 38 39 # Link time optimisation 40 if (BENCHMARK_ENABLE_LTO) 41 set(CMAKE_CXX_FLAGS_RELEASE "/GL") 42 set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "/LTCG") 43 set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "/LTCG") 44 set(CMAKE_EXE_LINKER_FLAGS_RELEASE "/LTCG") 45 endif() 46else() 47 # Try and enable C++11. Don't use C++14 because it doesn't work in some 48 # configurations. 49 add_cxx_compiler_flag(-std=c++11) 50 if (NOT HAVE_CXX_FLAG_STD_CXX11) 51 add_cxx_compiler_flag(-std=c++0x) 52 endif() 53 54 # Turn compiler warnings up to 11 55 add_cxx_compiler_flag(-Wall) 56 57 add_cxx_compiler_flag(-Wextra) 58 add_cxx_compiler_flag(-Wshadow) 59 add_cxx_compiler_flag(-Werror RELEASE) 60 add_cxx_compiler_flag(-pedantic) 61 add_cxx_compiler_flag(-pedantic-errors) 62 add_cxx_compiler_flag(-Wshorten-64-to-32) 63 add_cxx_compiler_flag(-Wfloat-equal) 64 add_cxx_compiler_flag(-Wzero-as-null-pointer-constant) 65 add_cxx_compiler_flag(-fstrict-aliasing) 66 if (HAVE_CXX_FLAG_FSTRICT_ALIASING) 67 add_cxx_compiler_flag(-Wstrict-aliasing) 68 endif() 69 add_cxx_compiler_flag(-Wthread-safety) 70 if (HAVE_WTHREAD_SAFETY) 71 add_definitions(-DHAVE_WTHREAD_SAFETY) 72 cxx_feature_check(THREAD_SAFETY_ATTRIBUTES) 73 endif() 74 75 # Link time optimisation 76 if (BENCHMARK_ENABLE_LTO) 77 add_cxx_compiler_flag(-flto) 78 if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU") 79 find_program(GCC_AR gcc-ar) 80 if (GCC_AR) 81 set(CMAKE_AR ${GCC_AR}) 82 endif() 83 find_program(GCC_RANLIB gcc-ranlib) 84 if (GCC_RANLIB) 85 set(CMAKE_RANLIB ${GCC_RANLIB}) 86 endif() 87 endif() 88 endif() 89 90 # Coverage build type 91 set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}" CACHE STRING 92 "Flags used by the C++ compiler during coverage builds." 93 FORCE) 94 set(CMAKE_EXE_LINKER_FLAGS_COVERAGE 95 "${CMAKE_EXE_LINKER_FLAGS_DEBUG}" CACHE STRING 96 "Flags used for linking binaries during coverage builds." 97 FORCE) 98 set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE 99 "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}" CACHE STRING 100 "Flags used by the shared libraries linker during coverage builds." 101 FORCE) 102 mark_as_advanced( 103 CMAKE_CXX_FLAGS_COVERAGE 104 CMAKE_EXE_LINKER_FLAGS_COVERAGE 105 CMAKE_SHARED_LINKER_FLAGS_COVERAGE) 106 set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING 107 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage." 108 FORCE) 109 add_cxx_compiler_flag(--coverage COVERAGE) 110endif() 111 112# C++ feature checks 113cxx_feature_check(STD_REGEX) 114cxx_feature_check(GNU_POSIX_REGEX) 115cxx_feature_check(POSIX_REGEX) 116cxx_feature_check(STEADY_CLOCK) 117 118# Ensure we have pthreads 119find_package(Threads REQUIRED) 120 121# Set up directories 122include_directories(${PROJECT_SOURCE_DIR}/include) 123 124# Build the targets 125add_subdirectory(src) 126 127if (BENCHMARK_ENABLE_TESTING) 128 enable_testing() 129 add_subdirectory(test) 130endif() 131