You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
13 KiB

1 month ago
  1. # Require CMake 3.10. If available, use the policies up to CMake 3.22.
  2. cmake_minimum_required (VERSION 3.10...3.22)
  3. project (benchmark VERSION 1.7.1 LANGUAGES CXX)
  4. option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
  5. option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
  6. option(BENCHMARK_ENABLE_LTO "Enable link time optimisation of the benchmark library." OFF)
  7. option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library." OFF)
  8. option(BENCHMARK_ENABLE_WERROR "Build Release candidates with -Werror." ON)
  9. option(BENCHMARK_FORCE_WERROR "Build Release candidates with -Werror regardless of compiler issues." OFF)
  10. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
  11. # PGC++ maybe reporting false positives.
  12. set(BENCHMARK_ENABLE_WERROR OFF)
  13. endif()
  14. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "NVHPC")
  15. set(BENCHMARK_ENABLE_WERROR OFF)
  16. endif()
  17. if(BENCHMARK_FORCE_WERROR)
  18. set(BENCHMARK_ENABLE_WERROR ON)
  19. endif(BENCHMARK_FORCE_WERROR)
  20. if(NOT MSVC)
  21. option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)
  22. else()
  23. set(BENCHMARK_BUILD_32_BITS OFF CACHE BOOL "Build a 32 bit version of the library - unsupported when using MSVC)" FORCE)
  24. endif()
  25. option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)
  26. option(BENCHMARK_ENABLE_DOXYGEN "Build documentation with Doxygen." OFF)
  27. option(BENCHMARK_INSTALL_DOCS "Enable installation of documentation." ON)
  28. # Allow unmet dependencies to be met using CMake's ExternalProject mechanics, which
  29. # may require downloading the source code.
  30. option(BENCHMARK_DOWNLOAD_DEPENDENCIES "Allow the downloading and in-tree building of unmet dependencies" OFF)
  31. # This option can be used to disable building and running unit tests which depend on gtest
  32. # in cases where it is not possible to build or find a valid version of gtest.
  33. option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON)
  34. option(BENCHMARK_USE_BUNDLED_GTEST "Use bundled GoogleTest. If disabled, the find_package(GTest) will be used." ON)
  35. option(BENCHMARK_ENABLE_LIBPFM "Enable performance counters provided by libpfm" OFF)
  36. # Export only public symbols
  37. set(CMAKE_CXX_VISIBILITY_PRESET hidden)
  38. set(CMAKE_VISIBILITY_INLINES_HIDDEN ON)
  39. if(MSVC)
  40. # As of CMake 3.18, CMAKE_SYSTEM_PROCESSOR is not set properly for MSVC and
  41. # cross-compilation (e.g. Host=x86_64, target=aarch64) requires using the
  42. # undocumented, but working variable.
  43. # See https://gitlab.kitware.com/cmake/cmake/-/issues/15170
  44. set(CMAKE_SYSTEM_PROCESSOR ${MSVC_CXX_ARCHITECTURE_ID})
  45. if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "ARM")
  46. set(CMAKE_CROSSCOMPILING TRUE)
  47. endif()
  48. endif()
  49. set(ENABLE_ASSEMBLY_TESTS_DEFAULT OFF)
  50. function(should_enable_assembly_tests)
  51. if(CMAKE_BUILD_TYPE)
  52. string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
  53. if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
  54. # FIXME: The --coverage flag needs to be removed when building assembly
  55. # tests for this to work.
  56. return()
  57. endif()
  58. endif()
  59. if (MSVC)
  60. return()
  61. elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
  62. return()
  63. elseif(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
  64. # FIXME: Make these work on 32 bit builds
  65. return()
  66. elseif(BENCHMARK_BUILD_32_BITS)
  67. # FIXME: Make these work on 32 bit builds
  68. return()
  69. endif()
  70. find_program(LLVM_FILECHECK_EXE FileCheck)
  71. if (LLVM_FILECHECK_EXE)
  72. set(LLVM_FILECHECK_EXE "${LLVM_FILECHECK_EXE}" CACHE PATH "llvm filecheck" FORCE)
  73. message(STATUS "LLVM FileCheck Found: ${LLVM_FILECHECK_EXE}")
  74. else()
  75. message(STATUS "Failed to find LLVM FileCheck")
  76. return()
  77. endif()
  78. set(ENABLE_ASSEMBLY_TESTS_DEFAULT ON PARENT_SCOPE)
  79. endfunction()
  80. should_enable_assembly_tests()
  81. # This option disables the building and running of the assembly verification tests
  82. option(BENCHMARK_ENABLE_ASSEMBLY_TESTS "Enable building and running the assembly tests"
  83. ${ENABLE_ASSEMBLY_TESTS_DEFAULT})
  84. # Make sure we can import out CMake functions
  85. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
  86. list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
  87. # Read the git tags to determine the project version
  88. include(GetGitVersion)
  89. get_git_version(GIT_VERSION)
  90. # If no git version can be determined, use the version
  91. # from the project() command
  92. if ("${GIT_VERSION}" STREQUAL "0.0.0")
  93. set(VERSION "${benchmark_VERSION}")
  94. else()
  95. set(VERSION "${GIT_VERSION}")
  96. endif()
  97. # Tell the user what versions we are using
  98. message(STATUS "Version: ${VERSION}")
  99. # The version of the libraries
  100. set(GENERIC_LIB_VERSION ${VERSION})
  101. string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION)
  102. # Import our CMake modules
  103. include(AddCXXCompilerFlag)
  104. include(CheckCXXCompilerFlag)
  105. include(CheckLibraryExists)
  106. include(CXXFeatureCheck)
  107. check_library_exists(rt shm_open "" HAVE_LIB_RT)
  108. if (BENCHMARK_BUILD_32_BITS)
  109. add_required_cxx_compiler_flag(-m32)
  110. endif()
  111. if (MSVC)
  112. set(BENCHMARK_CXX_STANDARD 14)
  113. else()
  114. set(BENCHMARK_CXX_STANDARD 11)
  115. endif()
  116. set(CMAKE_CXX_STANDARD ${BENCHMARK_CXX_STANDARD})
  117. set(CMAKE_CXX_STANDARD_REQUIRED YES)
  118. set(CMAKE_CXX_EXTENSIONS OFF)
  119. if (MSVC)
  120. # Turn compiler warnings up to 11
  121. string(REGEX REPLACE "[-/]W[1-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  122. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
  123. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  124. if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
  125. add_cxx_compiler_flag(-EHs-)
  126. add_cxx_compiler_flag(-EHa-)
  127. add_definitions(-D_HAS_EXCEPTIONS=0)
  128. endif()
  129. # Link time optimisation
  130. if (BENCHMARK_ENABLE_LTO)
  131. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL")
  132. set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} /LTCG")
  133. set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG")
  134. set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
  135. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL")
  136. string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO}")
  137. set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
  138. string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
  139. set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
  140. string(REGEX REPLACE "[-/]INCREMENTAL" "/INCREMENTAL:NO" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
  141. set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG")
  142. set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /GL")
  143. set(CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL "${CMAKE_STATIC_LINKER_FLAGS_MINSIZEREL} /LTCG")
  144. set(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL} /LTCG")
  145. set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} /LTCG")
  146. endif()
  147. else()
  148. # Turn compiler warnings up to 11
  149. add_cxx_compiler_flag(-Wall)
  150. add_cxx_compiler_flag(-Wextra)
  151. add_cxx_compiler_flag(-Wshadow)
  152. add_cxx_compiler_flag(-Wfloat-equal)
  153. if(BENCHMARK_ENABLE_WERROR)
  154. add_cxx_compiler_flag(-Werror)
  155. endif()
  156. if (NOT BENCHMARK_ENABLE_TESTING)
  157. # Disable warning when compiling tests as gtest does not use 'override'.
  158. add_cxx_compiler_flag(-Wsuggest-override)
  159. endif()
  160. add_cxx_compiler_flag(-pedantic)
  161. add_cxx_compiler_flag(-pedantic-errors)
  162. add_cxx_compiler_flag(-Wshorten-64-to-32)
  163. add_cxx_compiler_flag(-fstrict-aliasing)
  164. # Disable warnings regarding deprecated parts of the library while building
  165. # and testing those parts of the library.
  166. add_cxx_compiler_flag(-Wno-deprecated-declarations)
  167. if (CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
  168. # Intel silently ignores '-Wno-deprecated-declarations',
  169. # warning no. 1786 must be explicitly disabled.
  170. # See #631 for rationale.
  171. add_cxx_compiler_flag(-wd1786)
  172. endif()
  173. # Disable deprecation warnings for release builds (when -Werror is enabled).
  174. if(BENCHMARK_ENABLE_WERROR)
  175. add_cxx_compiler_flag(-Wno-deprecated)
  176. endif()
  177. if (NOT BENCHMARK_ENABLE_EXCEPTIONS)
  178. add_cxx_compiler_flag(-fno-exceptions)
  179. endif()
  180. if (HAVE_CXX_FLAG_FSTRICT_ALIASING)
  181. if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "Intel") #ICC17u2: Many false positives for Wstrict-aliasing
  182. add_cxx_compiler_flag(-Wstrict-aliasing)
  183. endif()
  184. endif()
  185. # ICC17u2: overloaded virtual function "benchmark::Fixture::SetUp" is only partially overridden
  186. # (because of deprecated overload)
  187. add_cxx_compiler_flag(-wd654)
  188. add_cxx_compiler_flag(-Wthread-safety)
  189. if (HAVE_CXX_FLAG_WTHREAD_SAFETY)
  190. cxx_feature_check(THREAD_SAFETY_ATTRIBUTES "-DINCLUDE_DIRECTORIES=${PROJECT_SOURCE_DIR}/include")
  191. endif()
  192. # On most UNIX like platforms g++ and clang++ define _GNU_SOURCE as a
  193. # predefined macro, which turns on all of the wonderful libc extensions.
  194. # However g++ doesn't do this in Cygwin so we have to define it ourselves
  195. # since we depend on GNU/POSIX/BSD extensions.
  196. if (CYGWIN)
  197. add_definitions(-D_GNU_SOURCE=1)
  198. endif()
  199. if (QNXNTO)
  200. add_definitions(-D_QNX_SOURCE)
  201. endif()
  202. # Link time optimisation
  203. if (BENCHMARK_ENABLE_LTO)
  204. add_cxx_compiler_flag(-flto)
  205. add_cxx_compiler_flag(-Wno-lto-type-mismatch)
  206. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
  207. find_program(GCC_AR gcc-ar)
  208. if (GCC_AR)
  209. set(CMAKE_AR ${GCC_AR})
  210. endif()
  211. find_program(GCC_RANLIB gcc-ranlib)
  212. if (GCC_RANLIB)
  213. set(CMAKE_RANLIB ${GCC_RANLIB})
  214. endif()
  215. elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  216. include(llvm-toolchain)
  217. endif()
  218. endif()
  219. # Coverage build type
  220. set(BENCHMARK_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG}"
  221. CACHE STRING "Flags used by the C++ compiler during coverage builds."
  222. FORCE)
  223. set(BENCHMARK_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG}"
  224. CACHE STRING "Flags used for linking binaries during coverage builds."
  225. FORCE)
  226. set(BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}"
  227. CACHE STRING "Flags used by the shared libraries linker during coverage builds."
  228. FORCE)
  229. mark_as_advanced(
  230. BENCHMARK_CXX_FLAGS_COVERAGE
  231. BENCHMARK_EXE_LINKER_FLAGS_COVERAGE
  232. BENCHMARK_SHARED_LINKER_FLAGS_COVERAGE)
  233. set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
  234. "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel Coverage.")
  235. add_cxx_compiler_flag(--coverage COVERAGE)
  236. endif()
  237. if (BENCHMARK_USE_LIBCXX)
  238. if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  239. add_cxx_compiler_flag(-stdlib=libc++)
  240. elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR
  241. "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
  242. add_cxx_compiler_flag(-nostdinc++)
  243. message(WARNING "libc++ header path must be manually specified using CMAKE_CXX_FLAGS")
  244. # Adding -nodefaultlibs directly to CMAKE_<TYPE>_LINKER_FLAGS will break
  245. # configuration checks such as 'find_package(Threads)'
  246. list(APPEND BENCHMARK_CXX_LINKER_FLAGS -nodefaultlibs)
  247. # -lc++ cannot be added directly to CMAKE_<TYPE>_LINKER_FLAGS because
  248. # linker flags appear before all linker inputs and -lc++ must appear after.
  249. list(APPEND BENCHMARK_CXX_LIBRARIES c++)
  250. else()
  251. message(FATAL_ERROR "-DBENCHMARK_USE_LIBCXX:BOOL=ON is not supported for compiler")
  252. endif()
  253. endif(BENCHMARK_USE_LIBCXX)
  254. set(EXTRA_CXX_FLAGS "")
  255. if (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
  256. # Clang on Windows fails to compile the regex feature check under C++11
  257. set(EXTRA_CXX_FLAGS "-DCMAKE_CXX_STANDARD=14")
  258. endif()
  259. # C++ feature checks
  260. # Determine the correct regular expression engine to use
  261. cxx_feature_check(STD_REGEX ${EXTRA_CXX_FLAGS})
  262. cxx_feature_check(GNU_POSIX_REGEX ${EXTRA_CXX_FLAGS})
  263. cxx_feature_check(POSIX_REGEX ${EXTRA_CXX_FLAGS})
  264. if(NOT HAVE_STD_REGEX AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
  265. message(FATAL_ERROR "Failed to determine the source files for the regular expression backend")
  266. endif()
  267. if (NOT BENCHMARK_ENABLE_EXCEPTIONS AND HAVE_STD_REGEX
  268. AND NOT HAVE_GNU_POSIX_REGEX AND NOT HAVE_POSIX_REGEX)
  269. message(WARNING "Using std::regex with exceptions disabled is not fully supported")
  270. endif()
  271. cxx_feature_check(STEADY_CLOCK)
  272. # Ensure we have pthreads
  273. set(THREADS_PREFER_PTHREAD_FLAG ON)
  274. find_package(Threads REQUIRED)
  275. cxx_feature_check(PTHREAD_AFFINITY)
  276. if (BENCHMARK_ENABLE_LIBPFM)
  277. find_package(PFM)
  278. endif()
  279. # Set up directories
  280. include_directories(${PROJECT_SOURCE_DIR}/include)
  281. # Build the targets
  282. add_subdirectory(src)
  283. if (BENCHMARK_ENABLE_TESTING)
  284. enable_testing()
  285. if (BENCHMARK_ENABLE_GTEST_TESTS AND
  286. NOT (TARGET gtest AND TARGET gtest_main AND
  287. TARGET gmock AND TARGET gmock_main))
  288. if (BENCHMARK_USE_BUNDLED_GTEST)
  289. include(GoogleTest)
  290. else()
  291. find_package(GTest CONFIG REQUIRED)
  292. add_library(gtest ALIAS GTest::gtest)
  293. add_library(gtest_main ALIAS GTest::gtest_main)
  294. add_library(gmock ALIAS GTest::gmock)
  295. add_library(gmock_main ALIAS GTest::gmock_main)
  296. endif()
  297. endif()
  298. add_subdirectory(test)
  299. endif()