xref: /llvm-project/third-party/benchmark/CMakeLists.txt (revision a5b797172cc902db166e9a695716fb81405f86e4)
1*a5b79717SMircea Trofin# Require CMake 3.10. If available, use the policies up to CMake 3.22.
2*a5b79717SMircea Trofincmake_minimum_required (VERSION 3.10...3.22)
35dda2efdSMircea Trofin
4*a5b79717SMircea Trofinproject (benchmark VERSION 1.8.3 LANGUAGES CXX)
55dda2efdSMircea Trofin
65dda2efdSMircea Trofinoption(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
75dda2efdSMircea Trofinoption(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
85dda2efdSMircea Trofinoption(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)
95dda2efdSMircea Trofinoption(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)
10a290770fSMircea Trofinoption(BENCHMARK_ENABLE_WERROR "Build Release candidates with -Werror." ON)
11a290770fSMircea Trofinoption(BENCHMARK_FORCE_WERROR "Build Release candidates with -Werror regardless of compiler issues." OFF)
12a290770fSMircea Trofin
13a290770fSMircea Trofinif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
14a290770fSMircea Trofin  # PGC++ maybe reporting false positives.
15a290770fSMircea Trofin  set(BENCHMARK_ENABLE_WERROR OFF)
16a290770fSMircea Trofinendif()
17*a5b79717SMircea Trofinif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "NVHPC")
18*a5b79717SMircea Trofin  set(BENCHMARK_ENABLE_WERROR OFF)
19*a5b79717SMircea Trofinendif()
20a290770fSMircea Trofinif(BENCHMARK_FORCE_WERROR)
21a290770fSMircea Trofin  set(BENCHMARK_ENABLE_WERROR ON)
22a290770fSMircea Trofinendif(BENCHMARK_FORCE_WERROR)
23a290770fSMircea Trofin
24*a5b79717SMircea Trofinif(NOT (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))
255dda2efdSMircea Trofin  option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)
265dda2efdSMircea Trofinelse()
275dda2efdSMircea Trofin  set(BENCHMARK_BUILD_32_BITS OFF CACHE BOOL "Build a 32 bit version of the library - unsupported when using MSVC)" FORCE)
285dda2efdSMircea Trofinendif()
295dda2efdSMircea Trofinoption(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)
30a290770fSMircea Trofinoption(BENCHMARK_ENABLE_DOXYGEN "Build documentation with Doxygen." OFF)
31a290770fSMircea Trofinoption(BENCHMARK_INSTALL_DOCS "Enable installation of documentation." ON)
325dda2efdSMircea Trofin
335dda2efdSMircea Trofin# Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which
345dda2efdSMircea Trofin# may require downloading the source code.
355dda2efdSMircea Trofinoption(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF)
365dda2efdSMircea Trofin
375dda2efdSMircea Trofin# This option can be used to disable building and running unit tests which depend on gtest
385dda2efdSMircea Trofin# in cases where it is not possible to build or find a valid version of gtest.
395dda2efdSMircea Trofinoption(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON)
40a290770fSMircea Trofinoption(BENCHMARK_USE_BUNDLED_GTEST "Use bundled GoogleTest. If disabled, the find_package(GTest) will be used." ON)
415dda2efdSMircea Trofin
425dda2efdSMircea Trofinoption(BENCHMARK_ENABLE_LIBPFM "Enable performance counters provided by libpfm" OFF)
435dda2efdSMircea Trofin
44*a5b79717SMircea Trofin# Export only public symbols
45*a5b79717SMircea Trofinset(CMAKE_CXX_VISIBILITY_PRESET hidden)
46*a5b79717SMircea Trofinset(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
47*a5b79717SMircea Trofin
48*a5b79717SMircea Trofinif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
495dda2efdSMircea Trofin    # As of CMake 3.18, CMAKE_SYSTEM_PROCESSOR is not set properly for MSVC and
505dda2efdSMircea Trofin    # cross-compilation (e.g. Host=x86_64, target=aarch64) requires using the
515dda2efdSMircea Trofin    # undocumented, but working variable.
525dda2efdSMircea Trofin    # See https://gitlab.kitware.com/cmake/cmake/-/issues/15170
535dda2efdSMircea Trofin    set(CMAKE_SYSTEM_PROCESSOR ${MSVC_CXX_ARCHITECTURE_ID})
545dda2efdSMircea Trofin    if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ARM")
555dda2efdSMircea Trofin      set(CMAKE_CROSSCOMPILING TRUE)
565dda2efdSMircea Trofin    endif()
575dda2efdSMircea Trofinendif()
585dda2efdSMircea Trofin
595dda2efdSMircea Trofinset(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF)
605dda2efdSMircea Trofinfunction(should_enable_assembly_tests)
615dda2efdSMircea Trofin  if(CMAKE_BUILD_TYPE)
625dda2efdSMircea Trofin    string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
635dda2efdSMircea Trofin    if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
645dda2efdSMircea Trofin      # FIXME: The --coverage flag needs to be removed when building assembly
655dda2efdSMircea Trofin      # tests for this to work.
665dda2efdSMircea Trofin      return()
675dda2efdSMircea Trofin    endif()
685dda2efdSMircea Trofin  endif()
69*a5b79717SMircea Trofin  if (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
705dda2efdSMircea Trofin    return()
715dda2efdSMircea Trofin  elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
725dda2efdSMircea Trofin    return()
735dda2efdSMircea Trofin  elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
745dda2efdSMircea Trofin    # FIXME: Make these work on 32 bit builds
755dda2efdSMircea Trofin    return()
765dda2efdSMircea Trofin  elseif(BENCHMARK_BUILD_32_BITS)
775dda2efdSMircea Trofin     # FIXME: Make these work on 32 bit builds
785dda2efdSMircea Trofin    return()
795dda2efdSMircea Trofin  endif()
805dda2efdSMircea Trofin  find_program(LLVM_FILECHECK_EXE FileCheck)
815dda2efdSMircea Trofin  if (LLVM_FILECHECK_EXE)
825dda2efdSMircea Trofin    set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE)
835dda2efdSMircea Trofin    message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}")
845dda2efdSMircea Trofin  else()
855dda2efdSMircea Trofin    message(STATUS "Failed to find LLVM FileCheck")
865dda2efdSMircea Trofin    return()
875dda2efdSMircea Trofin  endif()
885dda2efdSMircea Trofin  set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE)
895dda2efdSMircea Trofinendfunction()
905dda2efdSMircea Trofinshould_enable_assembly_tests()
915dda2efdSMircea Trofin
925dda2efdSMircea Trofin# This option disables the building and running of the assembly verification tests
935dda2efdSMircea Trofinoption(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests"
945dda2efdSMircea Trofin    ${ENABLE_ASSEMBLY_TESTS_DEFAULT})
955dda2efdSMircea Trofin
965dda2efdSMircea Trofin# Make sure we can import out CMake functions
975dda2efdSMircea Trofinlist(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
985dda2efdSMircea Trofinlist(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
995dda2efdSMircea Trofin
1005dda2efdSMircea Trofin
1015dda2efdSMircea Trofin# Read the git tags to determine the project version
1025dda2efdSMircea Trofininclude(GetGitVersion)
1035dda2efdSMircea Trofinget_git_version(GIT_VERSION)
1045dda2efdSMircea Trofin
1055dda2efdSMircea Trofin# If no git version can be determined, use the version
1065dda2efdSMircea Trofin# from the project() command
1075dda2efdSMircea Trofinif ("${GIT_VERSION}" STREQUAL "0.0.0")
108*a5b79717SMircea Trofin  set(VERSION "v${benchmark_VERSION}")
1095dda2efdSMircea Trofinelse()
1105dda2efdSMircea Trofin  set(VERSION "${GIT_VERSION}")
1115dda2efdSMircea Trofinendif()
112*a5b79717SMircea Trofin
113*a5b79717SMircea Trofin# Normalize version: drop "v" prefix, replace first "-" with ".",
114*a5b79717SMircea Trofin# drop everything after second "-" (including said "-").
115*a5b79717SMircea Trofinstring(STRIP ${VERSION} VERSION)
116*a5b79717SMircea Trofinif(VERSION MATCHES v[^-]*-)
117*a5b79717SMircea Trofin   string(REGEX REPLACE "v([^-]*)-([0-9]+)-.*" "\\1.\\2"  NORMALIZED_VERSION ${VERSION})
118*a5b79717SMircea Trofinelse()
119*a5b79717SMircea Trofin   string(REGEX REPLACE "v(.*)" "\\1" NORMALIZED_VERSION ${VERSION})
120*a5b79717SMircea Trofinendif()
121*a5b79717SMircea Trofin
1225dda2efdSMircea Trofin# Tell the user what versions we are using
123*a5b79717SMircea Trofinmessage(STATUS "Google Benchmark version: ${VERSION}, normalized to ${NORMALIZED_VERSION}")
1245dda2efdSMircea Trofin
1255dda2efdSMircea Trofin# The version of the libraries
126*a5b79717SMircea Trofinset(GENERIC_LIB_VERSION ${NORMALIZED_VERSION})
127*a5b79717SMircea Trofinstring(SUBSTRING ${NORMALIZED_VERSION} 0 1 GENERIC_LIB_SOVERSION)
1285dda2efdSMircea Trofin
1295dda2efdSMircea Trofin# Import our CMake modules
130aec6a04bSMircea Trofininclude(AddCXXCompilerFlag)
131*a5b79717SMircea Trofininclude(CheckCXXCompilerFlag)
132aec6a04bSMircea Trofininclude(CheckLibraryExists)
133*a5b79717SMircea Trofininclude(CXXFeatureCheck)
134a290770fSMircea Trofin
135a290770fSMircea Trofincheck_library_exists(rt shm_open "" HAVE_LIB_RT)
1365dda2efdSMircea Trofin
1375dda2efdSMircea Trofinif (BENCHMARK_BUILD_32_BITS)
1385dda2efdSMircea Trofin  add_required_cxx_compiler_flag(-m32)
1395dda2efdSMircea Trofinendif()
1405dda2efdSMircea Trofin
141*a5b79717SMircea Trofinif (MSVC OR CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")
142*a5b79717SMircea Trofin  set(BENCHMARK_CXX_STANDARD 14)
143*a5b79717SMircea Trofinelse()
144*a5b79717SMircea Trofin  set(BENCHMARK_CXX_STANDARD 11)
145*a5b79717SMircea Trofinendif()
146*a5b79717SMircea Trofin
147*a5b79717SMircea Trofinset(CMAKE_CXX_STANDARD ${BENCHMARK_CXX_STANDARD})
148*a5b79717SMircea Trofinset(CMAKE_CXX_STANDARD_REQUIRED YES)
149*a5b79717SMircea Trofinset(CMAKE_CXX_EXTENSIONS OFF)
150*a5b79717SMircea Trofin
1515dda2efdSMircea Trofinif (MSVC)
1525dda2efdSMircea Trofin  # Turn compiler warnings up to 11
1535dda2efdSMircea Trofin  string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
1545dda2efdSMircea Trofin  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
1555dda2efdSMircea Trofin  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
1565dda2efdSMircea Trofin
1575dda2efdSMircea Trofin  if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
1585dda2efdSMircea Trofin    add_cxx_compiler_flag(-EHs-)
1595dda2efdSMircea Trofin    add_cxx_compiler_flag(-EHa-)
1605dda2efdSMircea Trofin    add_definitions(-D_HAS_EXCEPTIONS=0)
1615dda2efdSMircea Trofin  endif()
1625dda2efdSMircea Trofin  # Link time optimisation
1635dda2efdSMircea Trofin  if (BENCHMARK_ENABLE_LTO)
1645dda2efdSMircea Trofin    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
1655dda2efdSMircea Trofin    set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
1665dda2efdSMircea Trofin    set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
1675dda2efdSMircea Trofin    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
1685dda2efdSMircea Trofin
1695dda2efdSMircea Trofin    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
1705dda2efdSMircea Trofin    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")
1715dda2efdSMircea Trofin    set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
1725dda2efdSMircea Trofin    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
1735dda2efdSMircea Trofin    set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
1745dda2efdSMircea Trofin    string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
1755dda2efdSMircea Trofin    set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
1765dda2efdSMircea Trofin
1775dda2efdSMircea Trofin    set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
1785dda2efdSMircea Trofin    set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")
1795dda2efdSMircea Trofin    set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
1805dda2efdSMircea Trofin    set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
1815dda2efdSMircea Trofin  endif()
1825dda2efdSMircea Trofinelse()
183*a5b79717SMircea Trofin  # Turn on Large-file Support
184*a5b79717SMircea Trofin  add_definitions(-D_FILE_OFFSET_BITS=64)
185*a5b79717SMircea Trofin  add_definitions(-D_LARGEFILE64_SOURCE)
186*a5b79717SMircea Trofin  add_definitions(-D_LARGEFILE_SOURCE)
1875dda2efdSMircea Trofin  # Turn compiler warnings up to 11
1885dda2efdSMircea Trofin  add_cxx_compiler_flag(-Wall)
1895dda2efdSMircea Trofin  add_cxx_compiler_flag(-Wextra)
1905dda2efdSMircea Trofin  add_cxx_compiler_flag(-Wshadow)
191*a5b79717SMircea Trofin  add_cxx_compiler_flag(-Wfloat-equal)
192*a5b79717SMircea Trofin  add_cxx_compiler_flag(-Wold-style-cast)
193a290770fSMircea Trofin  if(BENCHMARK_ENABLE_WERROR)
194*a5b79717SMircea Trofin      add_cxx_compiler_flag(-Werror)
195a290770fSMircea Trofin  endif()
1965dda2efdSMircea Trofin  if (NOT BENCHMARK_ENABLE_TESTING)
1975dda2efdSMircea Trofin    # Disable warning when compiling tests as gtest does not use 'override'.
1985dda2efdSMircea Trofin    add_cxx_compiler_flag(-Wsuggest-override)
1995dda2efdSMircea Trofin  endif()
2005dda2efdSMircea Trofin  add_cxx_compiler_flag(-pedantic)
2015dda2efdSMircea Trofin  add_cxx_compiler_flag(-pedantic-errors)
2025dda2efdSMircea Trofin  add_cxx_compiler_flag(-Wshorten-64-to-32)
2035dda2efdSMircea Trofin  add_cxx_compiler_flag(-fstrict-aliasing)
2045dda2efdSMircea Trofin  # Disable warnings regarding deprecated parts of the library while building
2055dda2efdSMircea Trofin  # and testing those parts of the library.
2065dda2efdSMircea Trofin  add_cxx_compiler_flag(-Wno-deprecated-declarations)
207*a5b79717SMircea Trofin  if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel" OR CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
2085dda2efdSMircea Trofin    # Intel silently ignores '-Wno-deprecated-declarations',
2095dda2efdSMircea Trofin    # warning no. 1786 must be explicitly disabled.
2105dda2efdSMircea Trofin    # See #631 for rationale.
2115dda2efdSMircea Trofin    add_cxx_compiler_flag(-wd1786)
212*a5b79717SMircea Trofin    add_cxx_compiler_flag(-fno-finite-math-only)
2135dda2efdSMircea Trofin  endif()
2145dda2efdSMircea Trofin  # Disable deprecation warnings for release builds (when -Werror is enabled).
215a290770fSMircea Trofin  if(BENCHMARK_ENABLE_WERROR)
216*a5b79717SMircea Trofin      add_cxx_compiler_flag(-Wno-deprecated)
217a290770fSMircea Trofin  endif()
2185dda2efdSMircea Trofin  if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
2195dda2efdSMircea Trofin    add_cxx_compiler_flag(-fno-exceptions)
2205dda2efdSMircea Trofin  endif()
2215dda2efdSMircea Trofin
2225dda2efdSMircea Trofin  if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
223*a5b79717SMircea Trofin    if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel" AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") #ICC17u2: Many false positives for Wstrict-aliasing
2245dda2efdSMircea Trofin      add_cxx_compiler_flag(-Wstrict-aliasing)
2255dda2efdSMircea Trofin    endif()
2265dda2efdSMircea Trofin  endif()
2275dda2efdSMircea Trofin  # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
2285dda2efdSMircea Trofin  # (because of deprecated overload)
2295dda2efdSMircea Trofin  add_cxx_compiler_flag(-wd654)
2305dda2efdSMircea Trofin  add_cxx_compiler_flag(-Wthread-safety)
2315dda2efdSMircea Trofin  if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
232*a5b79717SMircea Trofin    cxx_feature_check(THREAD_SAFETY_ATTRIBUTES "-DINCLUDE_DIRECTORIES=${PROJECT_SOURCE_DIR}/include")
2335dda2efdSMircea Trofin  endif()
2345dda2efdSMircea Trofin
2355dda2efdSMircea Trofin  # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
2365dda2efdSMircea Trofin  # predefined macro, which turns on all of the wonderful libc extensions.
237*a5b79717SMircea Trofin  # However g++ doesn't do this in Cygwin so we have to define it ourselves
2385dda2efdSMircea Trofin  # since we depend on GNU/POSIX/BSD extensions.
2395dda2efdSMircea Trofin  if (CYGWIN)
2405dda2efdSMircea Trofin    add_definitions(-D_GNU_SOURCE=1)
2415dda2efdSMircea Trofin  endif()
2425dda2efdSMircea Trofin
2435dda2efdSMircea Trofin  if (QNXNTO)
2445dda2efdSMircea Trofin    add_definitions(-D_QNX_SOURCE)
2455dda2efdSMircea Trofin  endif()
2465dda2efdSMircea Trofin
2475dda2efdSMircea Trofin  # Link time optimisation
2485dda2efdSMircea Trofin  if (BENCHMARK_ENABLE_LTO)
2495dda2efdSMircea Trofin    add_cxx_compiler_flag(-flto)
2505dda2efdSMircea Trofin    add_cxx_compiler_flag(-Wno-lto-type-mismatch)
2515dda2efdSMircea Trofin    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
2525dda2efdSMircea Trofin      find_program(GCC_AR gcc-ar)
2535dda2efdSMircea Trofin      if (GCC_AR)
2545dda2efdSMircea Trofin        set(CMAKE_AR ${GCC_AR})
2555dda2efdSMircea Trofin      endif()
2565dda2efdSMircea Trofin      find_program(GCC_RANLIB gcc-ranlib)
2575dda2efdSMircea Trofin      if (GCC_RANLIB)
2585dda2efdSMircea Trofin        set(CMAKE_RANLIB ${GCC_RANLIB})
2595dda2efdSMircea Trofin      endif()
2605dda2efdSMircea Trofin    elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
2615dda2efdSMircea Trofin      include(llvm-toolchain)
2625dda2efdSMircea Trofin    endif()
2635dda2efdSMircea Trofin  endif()
2645dda2efdSMircea Trofin
2655dda2efdSMircea Trofin  # Coverage build type
2665dda2efdSMircea Trofin  set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
2675dda2efdSMircea Trofin    CACHE STRING "Flags used by the C++ compiler during coverage builds."
2685dda2efdSMircea Trofin    FORCE)
2695dda2efdSMircea Trofin  set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
2705dda2efdSMircea Trofin    CACHE STRING "Flags used for linking binaries during coverage builds."
2715dda2efdSMircea Trofin    FORCE)
2725dda2efdSMircea Trofin  set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
2735dda2efdSMircea Trofin    CACHE STRING "Flags used by the shared libraries linker during coverage builds."
2745dda2efdSMircea Trofin    FORCE)
2755dda2efdSMircea Trofin  mark_as_advanced(
2765dda2efdSMircea Trofin    BENCHMARK_CXX_FLAGS_COVERAGE
2775dda2efdSMircea Trofin    BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
2785dda2efdSMircea Trofin    BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
2795dda2efdSMircea Trofin  set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
2805dda2efdSMircea Trofin    "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
2815dda2efdSMircea Trofin  add_cxx_compiler_flag(--coverage COVERAGE)
2825dda2efdSMircea Trofinendif()
2835dda2efdSMircea Trofin
2845dda2efdSMircea Trofinif (BENCHMARK_USE_LIBCXX)
2855dda2efdSMircea Trofin  if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
2865dda2efdSMircea Trofin    add_cxx_compiler_flag(-stdlib=libc++)
2875dda2efdSMircea Trofin  elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
288*a5b79717SMircea Trofin          "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" OR
289*a5b79717SMircea Trofin          "${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM")
2905dda2efdSMircea Trofin    add_cxx_compiler_flag(-nostdinc++)
2915dda2efdSMircea Trofin    message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
2925dda2efdSMircea Trofin    # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
2935dda2efdSMircea Trofin    # configuration checks such as 'find_package(Threads)'
2945dda2efdSMircea Trofin    list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
2955dda2efdSMircea Trofin    # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
2965dda2efdSMircea Trofin    # linker flags appear before all linker inputs and -lc++ must appear after.
2975dda2efdSMircea Trofin    list(APPEND BENCHMARK_CXX_LIBRARIES c++)
2985dda2efdSMircea Trofin  else()
2995dda2efdSMircea Trofin    message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
3005dda2efdSMircea Trofin  endif()
3015dda2efdSMircea Trofinendif(BENCHMARK_USE_LIBCXX)
3025dda2efdSMircea Trofin
3035dda2efdSMircea Trofinset(EXTRA_CXX_FLAGS "")
3045dda2efdSMircea Trofinif (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
3055dda2efdSMircea Trofin  # Clang on Windows fails to compile the regex feature check under C++11
3065dda2efdSMircea Trofin  set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14")
3075dda2efdSMircea Trofinendif()
3085dda2efdSMircea Trofin
3095dda2efdSMircea Trofin# C++ feature checks
3105dda2efdSMircea Trofin# Determine the correct regular expression engine to use
3115dda2efdSMircea Trofincxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS})
3125dda2efdSMircea Trofincxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS})
3135dda2efdSMircea Trofincxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS})
3145dda2efdSMircea Trofinif(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
3155dda2efdSMircea Trofin  message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
3165dda2efdSMircea Trofinendif()
3175dda2efdSMircea Trofinif (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX
3185dda2efdSMircea Trofin        AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
3195dda2efdSMircea Trofin  message(WARNING "Using std::regex with exceptions disabled is not fully supported")
3205dda2efdSMircea Trofinendif()
3215dda2efdSMircea Trofin
3225dda2efdSMircea Trofincxx_feature_check(STEADY_CLOCK)
3235dda2efdSMircea Trofin# Ensure we have pthreads
3245dda2efdSMircea Trofinset(THREADS_PREFER_PTHREAD_FLAG ON)
3255dda2efdSMircea Trofinfind_package(Threads REQUIRED)
326*a5b79717SMircea Trofincxx_feature_check(PTHREAD_AFFINITY)
3275dda2efdSMircea Trofin
3285dda2efdSMircea Trofinif (BENCHMARK_ENABLE_LIBPFM)
329*a5b79717SMircea Trofin  find_package(PFM REQUIRED)
3305dda2efdSMircea Trofinendif()
3315dda2efdSMircea Trofin
3325dda2efdSMircea Trofin# Set up directories
3335dda2efdSMircea Trofininclude_directories(${PROJECT_SOURCE_DIR}/include)
3345dda2efdSMircea Trofin
3355dda2efdSMircea Trofin# Build the targets
3365dda2efdSMircea Trofinadd_subdirectory(src)
3375dda2efdSMircea Trofin
3385dda2efdSMircea Trofinif (BENCHMARK_ENABLE_TESTING)
3395dda2efdSMircea Trofin  enable_testing()
3405dda2efdSMircea Trofin  if (BENCHMARK_ENABLE_GTEST_TESTS AND
3415dda2efdSMircea Trofin      NOT (TARGET gtest AND TARGET gtest_main AND
3425dda2efdSMircea Trofin           TARGET gmock AND TARGET gmock_main))
343a290770fSMircea Trofin    if (BENCHMARK_USE_BUNDLED_GTEST)
3445dda2efdSMircea Trofin      include(GoogleTest)
345a290770fSMircea Trofin    else()
346a290770fSMircea Trofin      find_package(GTest CONFIG REQUIRED)
347a290770fSMircea Trofin      add_library(gtest ALIAS GTest::gtest)
348a290770fSMircea Trofin      add_library(gtest_main ALIAS GTest::gtest_main)
349a290770fSMircea Trofin      add_library(gmock ALIAS GTest::gmock)
350a290770fSMircea Trofin      add_library(gmock_main ALIAS GTest::gmock_main)
351a290770fSMircea Trofin    endif()
3525dda2efdSMircea Trofin  endif()
3535dda2efdSMircea Trofin  add_subdirectory(test)
3545dda2efdSMircea Trofinendif()
355