• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Allow the source files to find headers in src/
2include(GNUInstallDirs)
3include_directories(${PROJECT_SOURCE_DIR}/src)
4
5if (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})
8endif()
9
10file(GLOB
11  SOURCE_FILES
12    *.cc
13    ${PROJECT_SOURCE_DIR}/include/benchmark/*.h
14    ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
15file(GLOB BENCHMARK_MAIN "benchmark_main.cc")
16foreach(item ${BENCHMARK_MAIN})
17  list(REMOVE_ITEM SOURCE_FILES "${item}")
18endforeach()
19
20add_library(benchmark ${SOURCE_FILES})
21add_library(benchmark::benchmark ALIAS benchmark)
22set_target_properties(benchmark PROPERTIES
23  OUTPUT_NAME "benchmark"
24  VERSION ${GENERIC_LIB_VERSION}
25  SOVERSION ${GENERIC_LIB_SOVERSION}
26)
27target_include_directories(benchmark PUBLIC
28    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
29    )
30
31# Link threads.
32target_link_libraries(benchmark  ${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
33find_library(LIBRT rt)
34if(LIBRT)
35  target_link_libraries(benchmark ${LIBRT})
36endif()
37
38if(CMAKE_BUILD_TYPE)
39  string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)
40endif()
41if(NOT CMAKE_THREAD_LIBS_INIT AND "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}" MATCHES ".*-fsanitize=[^ ]*address.*")
42  message(WARNING "CMake's FindThreads.cmake did not fail, but CMAKE_THREAD_LIBS_INIT ended up being empty. This was fixed in https://github.com/Kitware/CMake/commit/d53317130e84898c5328c237186dbd995aaf1c12 Let's guess that -pthread is sufficient.")
43  target_link_libraries(benchmark -pthread)
44endif()
45
46# We need extra libraries on Windows
47if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
48  target_link_libraries(benchmark shlwapi)
49endif()
50
51# We need extra libraries on Solaris
52if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
53  target_link_libraries(benchmark kstat)
54endif()
55
56# Benchmark main library
57add_library(benchmark_main "benchmark_main.cc")
58add_library(benchmark::benchmark_main ALIAS benchmark_main)
59set_target_properties(benchmark_main PROPERTIES
60  OUTPUT_NAME "benchmark_main"
61  VERSION ${GENERIC_LIB_VERSION}
62  SOVERSION ${GENERIC_LIB_SOVERSION}
63)
64target_include_directories(benchmark PUBLIC
65    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
66    )
67target_link_libraries(benchmark_main benchmark::benchmark)
68
69
70set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
71
72set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
73set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake")
74set(pkg_config "${generated_dir}/${PROJECT_NAME}.pc")
75set(targets_export_name "${PROJECT_NAME}Targets")
76
77set(namespace "${PROJECT_NAME}::")
78
79include(CMakePackageConfigHelpers)
80write_basic_package_version_file(
81  "${version_config}" VERSION ${GENERIC_LIB_VERSION} COMPATIBILITY SameMajorVersion
82)
83
84configure_file("${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in" "${project_config}" @ONLY)
85configure_file("${PROJECT_SOURCE_DIR}/cmake/benchmark.pc.in" "${pkg_config}" @ONLY)
86
87if (BENCHMARK_ENABLE_INSTALL)
88  # Install target (will install the library to specified CMAKE_INSTALL_PREFIX variable)
89  install(
90    TARGETS benchmark benchmark_main
91    EXPORT ${targets_export_name}
92    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
93    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
94    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
95    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
96
97  install(
98    DIRECTORY "${PROJECT_SOURCE_DIR}/include/benchmark"
99    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
100    FILES_MATCHING PATTERN "*.*h")
101
102  install(
103      FILES "${project_config}" "${version_config}"
104      DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
105
106  install(
107      FILES "${pkg_config}"
108      DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
109
110  install(
111      EXPORT "${targets_export_name}"
112      NAMESPACE "${namespace}"
113      DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
114endif()
115