1 # Allow the source files to find headers in src/
2 include(GNUInstallDirs)
3 include_directories(${PROJECT_SOURCE_DIR}/src)
5 if (DEFINED BENCHMARK_CXX_LINKER_FLAGS)
6 list(APPEND CMAKE_SHARED_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS})
7 list(APPEND CMAKE_MODULE_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS})
13 ${PROJECT_SOURCE_DIR}/include/benchmark/*.h
14 ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
15 file(GLOB BENCHMARK_MAIN "benchmark_main.cc")
16 foreach(item ${BENCHMARK_MAIN})
17 list(REMOVE_ITEM SOURCE_FILES "${item}")
20 add_library(benchmark ${SOURCE_FILES})
21 add_library(benchmark::benchmark ALIAS benchmark)
22 set_target_properties(benchmark PROPERTIES
23 OUTPUT_NAME "benchmark"
24 VERSION ${GENERIC_LIB_VERSION}
25 SOVERSION ${GENERIC_LIB_SOVERSION}
27 target_include_directories(benchmark PUBLIC
28 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
34 PROPERTY COMPILE_DEFINITIONS
35 BENCHMARK_VERSION="${VERSION}"
38 # libpfm, if available
40 target_link_libraries(benchmark PRIVATE PFM::libpfm)
41 target_compile_definitions(benchmark PRIVATE -DHAVE_LIBPFM)
44 # pthread affinity, if available
45 if(HAVE_PTHREAD_AFFINITY)
46 target_compile_definitions(benchmark PRIVATE -DBENCHMARK_HAS_PTHREAD_AFFINITY)
50 target_link_libraries(benchmark PRIVATE Threads::Threads)
52 target_link_libraries(benchmark PRIVATE ${BENCHMARK_CXX_LIBRARIES})
55 target_link_libraries(benchmark PRIVATE rt)
59 # We need extra libraries on Windows
60 if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
61 target_link_libraries(benchmark PRIVATE shlwapi)
64 # We need extra libraries on Solaris
65 if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
66 target_link_libraries(benchmark PRIVATE kstat)
69 if (NOT BUILD_SHARED_LIBS)
70 target_compile_definitions(benchmark PUBLIC -DBENCHMARK_STATIC_DEFINE)
73 # Benchmark main library
74 add_library(benchmark_main "benchmark_main.cc")
75 add_library(benchmark::benchmark_main ALIAS benchmark_main)
76 set_target_properties(benchmark_main PROPERTIES
77 OUTPUT_NAME "benchmark_main"
78 VERSION ${GENERIC_LIB_VERSION}
79 SOVERSION ${GENERIC_LIB_SOVERSION}
80 DEFINE_SYMBOL benchmark_EXPORTS
82 target_link_libraries(benchmark_main PUBLIC benchmark::benchmark)
84 set(generated_dir "${PROJECT_BINARY_DIR}")
86 set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
87 set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
88 set(pkg_config "${generated_dir}/${PROJECT_NAME}.pc")
89 set(targets_to_export benchmark benchmark_main)
90 set(targets_export_name "${PROJECT_NAME}Targets")
92 set(namespace "${PROJECT_NAME}::")
94 include(CMakePackageConfigHelpers)
96 configure_package_config_file (
97 ${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in
99 INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
100 NO_SET_AND_CHECK_MACRO
101 NO_CHECK_REQUIRED_COMPONENTS_MACRO
103 write_basic_package_version_file(
104 "${version_config}" VERSION ${GENERIC_LIB_VERSION} COMPATIBILITY SameMajorVersion
107 configure_file("${PROJECT_SOURCE_DIR}/cmake/benchmark.pc.in" "${pkg_config}" @ONLY)
110 TARGETS ${targets_to_export}
111 NAMESPACE "${namespace}"
112 FILE ${generated_dir}/${targets_export_name}.cmake
115 if (BENCHMARK_ENABLE_INSTALL)
116 # Install target (will install the library to specified CMAKE_INSTALL_PREFIX variable)
118 TARGETS ${targets_to_export}
119 EXPORT ${targets_export_name}
120 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
121 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
123 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
126 DIRECTORY "${PROJECT_SOURCE_DIR}/include/benchmark"
127 "${PROJECT_BINARY_DIR}/include/benchmark"
128 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
129 FILES_MATCHING PATTERN "*.*h")
132 FILES "${project_config}" "${version_config}"
133 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
136 FILES "${pkg_config}"
137 DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
140 EXPORT "${targets_export_name}"
141 NAMESPACE "${namespace}"
142 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
145 if (BENCHMARK_ENABLE_DOXYGEN)
146 find_package(Doxygen REQUIRED)
147 set(DOXYGEN_QUIET YES)
148 set(DOXYGEN_RECURSIVE YES)
149 set(DOXYGEN_GENERATE_HTML YES)
150 set(DOXYGEN_GENERATE_MAN NO)
151 set(DOXYGEN_MARKDOWN_SUPPORT YES)
152 set(DOXYGEN_BUILTIN_STL_SUPPORT YES)
153 set(DOXYGEN_EXTRACT_PACKAGE YES)
154 set(DOXYGEN_EXTRACT_STATIC YES)
155 set(DOXYGEN_SHOW_INCLUDE_FILES YES)
156 set(DOXYGEN_BINARY_TOC YES)
157 set(DOXYGEN_TOC_EXPAND YES)
158 set(DOXYGEN_USE_MDFILE_AS_MAINPAGE "index.md")
159 doxygen_add_docs(benchmark_doxygen
164 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
165 COMMENT "Building documentation with Doxygen.")
166 if (BENCHMARK_ENABLE_INSTALL AND BENCHMARK_INSTALL_DOCS)
168 DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/html/"
169 DESTINATION ${CMAKE_INSTALL_DOCDIR})
172 if (BENCHMARK_ENABLE_INSTALL AND BENCHMARK_INSTALL_DOCS)
174 DIRECTORY "${PROJECT_SOURCE_DIR}/docs/"
175 DESTINATION ${CMAKE_INSTALL_DOCDIR})