1 cmake_minimum_required (VERSION 2.8.11) 2 project (benchmark) 3 4 foreach(p 5 CMP0054 # CMake 3.1 6 ) 7 if(POLICY ${p}) 8 cmake_policy(SET ${p} NEW) 9 endif() 10 endforeach() 11 12 option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON) 13 option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF) 14 # Make sure we can import out CMake functions 15 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") 16 17 # Read the git tags to determine the project version 18 include(GetGitVersion) 19 get_git_version(GIT_VERSION) 20 21 # Tell the user what versions we are using 22 string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION}) 23 message("-- Version: ${VERSION}") 24 25 # The version of the libraries 26 set(GENERIC_LIB_VERSION ${VERSION}) 27 string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION) 28 29 # Import our CMake modules 30 include(CheckCXXCompilerFlag) 31 include(AddCXXCompilerFlag) 32 include(CXXFeatureCheck) 33 34 if ("${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() 46 else() 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) 110 endif() 111 112 # C++ feature checks 113 cxx_feature_check(STD_REGEX) 114 cxx_feature_check(GNU_POSIX_REGEX) 115 cxx_feature_check(POSIX_REGEX) 116 cxx_feature_check(STEADY_CLOCK) 117 118 # Ensure we have pthreads 119 find_package(Threads REQUIRED) 120 121 # Set up directories 122 include_directories(${PROJECT_SOURCE_DIR}/include) 123 124 # Build the targets 125 add_subdirectory(src) 126 127 if (BENCHMARK_ENABLE_TESTING) 128 enable_testing() 129 add_subdirectory(test) 130 endif() 131