Commit daff5fea by Jordan Williams Committed by Roman Lebedev

Alias CMake Targets. Fixes #921 (#926)

* add Jordan Williams to both CONTRIBUTORS and AUTHORS * alias benchmark libraries Provide aliased CMake targets for the benchmark and benchmark_main targets. The alias targets are namespaced under benchmark::, which is the namespace when they are exported. I chose not to use either the PROJECT_NAME or the namespace variable but to hard-code the namespace. This is because the benchmark and benchmark_main targets are hard-coded by name themselves. Hard-coding the namespace is also much cleaner and easier to read. * link to aliased benchmark targets It is safer to link against namespaced targets because of how CMake interprets the double colon. Typo's will be caught by CMake at configuration-time instead of during compile / link time. * document the provided alias targets * add "Usage with CMake" section in documentation This section covers linking against the alias/import CMake targets and including them using either find_package or add_subdirectory. * format the "Usage with CMake" README section Added a newline after the "Usage with CMake" section header. Dropped the header level of the section by one to make it a direct subsection of the "Usage" section. Wrapped lines to be no longer than 80 characters in length.
parent 5ce2429a
...@@ -33,6 +33,7 @@ Ismael Jimenez Martinez <ismael.jimenez.martinez@gmail.com> ...@@ -33,6 +33,7 @@ Ismael Jimenez Martinez <ismael.jimenez.martinez@gmail.com>
Jern-Kuan Leong <jernkuan@gmail.com> Jern-Kuan Leong <jernkuan@gmail.com>
JianXiong Zhou <zhoujianxiong2@gmail.com> JianXiong Zhou <zhoujianxiong2@gmail.com>
Joao Paulo Magalhaes <joaoppmagalhaes@gmail.com> Joao Paulo Magalhaes <joaoppmagalhaes@gmail.com>
Jordan Williams <jwillikers@protonmail.com>
Jussi Knuuttila <jussi.knuuttila@gmail.com> Jussi Knuuttila <jussi.knuuttila@gmail.com>
Kaito Udagawa <umireon@gmail.com> Kaito Udagawa <umireon@gmail.com>
Kishan Kumar <kumar.kishan@outlook.com> Kishan Kumar <kumar.kishan@outlook.com>
......
...@@ -50,6 +50,7 @@ Jern-Kuan Leong <jernkuan@gmail.com> ...@@ -50,6 +50,7 @@ Jern-Kuan Leong <jernkuan@gmail.com>
JianXiong Zhou <zhoujianxiong2@gmail.com> JianXiong Zhou <zhoujianxiong2@gmail.com>
Joao Paulo Magalhaes <joaoppmagalhaes@gmail.com> Joao Paulo Magalhaes <joaoppmagalhaes@gmail.com>
John Millikin <jmillikin@stripe.com> John Millikin <jmillikin@stripe.com>
Jordan Williams <jwillikers@protonmail.com>
Jussi Knuuttila <jussi.knuuttila@gmail.com> Jussi Knuuttila <jussi.knuuttila@gmail.com>
Kai Wolf <kai.wolf@gmail.com> Kai Wolf <kai.wolf@gmail.com>
Kaito Udagawa <umireon@gmail.com> Kaito Udagawa <umireon@gmail.com>
......
...@@ -178,8 +178,8 @@ BENCHMARK_MAIN(); ...@@ -178,8 +178,8 @@ BENCHMARK_MAIN();
``` ```
To run the benchmark, compile and link against the `benchmark` library To run the benchmark, compile and link against the `benchmark` library
(libbenchmark.a/.so). If you followed the build steps above, this (libbenchmark.a/.so). If you followed the build steps above, this library will
library will be under the build directory you created. be under the build directory you created.
```bash ```bash
# Example on linux after running the build steps above. Assumes the # Example on linux after running the build steps above. Assumes the
...@@ -194,6 +194,26 @@ Alternatively, link against the `benchmark_main` library and remove ...@@ -194,6 +194,26 @@ Alternatively, link against the `benchmark_main` library and remove
The compiled executable will run all benchmarks by default. Pass the `--help` The compiled executable will run all benchmarks by default. Pass the `--help`
flag for option information or see the guide below. flag for option information or see the guide below.
### Usage with CMake
If using CMake, it is recommended to link against the project-provided
`benchmark::benchmark` and `benchmark::benchmark_main` targets using
`target_link_libraries`.
It is possible to use ```find_package``` to import an installed version of the
library.
```cmake
find_package(benchmark REQUIRED)
```
Alternatively, ```add_subdirectory``` will incorporate the library directly in
to one's CMake project.
```cmake
add_subdirectory(benchmark)
```
Either way, link to the library as follows.
```cmake
target_link_libraries(MyTarget benchmark::benchmark)
```
## Platform Specific Build Instructions ## Platform Specific Build Instructions
### Building with GCC ### Building with GCC
......
...@@ -18,6 +18,7 @@ foreach(item ${BENCHMARK_MAIN}) ...@@ -18,6 +18,7 @@ foreach(item ${BENCHMARK_MAIN})
endforeach() endforeach()
add_library(benchmark ${SOURCE_FILES}) add_library(benchmark ${SOURCE_FILES})
add_library(benchmark::benchmark ALIAS benchmark)
set_target_properties(benchmark PROPERTIES set_target_properties(benchmark PROPERTIES
OUTPUT_NAME "benchmark" OUTPUT_NAME "benchmark"
VERSION ${GENERIC_LIB_VERSION} VERSION ${GENERIC_LIB_VERSION}
...@@ -55,6 +56,7 @@ endif() ...@@ -55,6 +56,7 @@ endif()
# Benchmark main library # Benchmark main library
add_library(benchmark_main "benchmark_main.cc") add_library(benchmark_main "benchmark_main.cc")
add_library(benchmark::benchmark_main ALIAS benchmark_main)
set_target_properties(benchmark_main PROPERTIES set_target_properties(benchmark_main PROPERTIES
OUTPUT_NAME "benchmark_main" OUTPUT_NAME "benchmark_main"
VERSION ${GENERIC_LIB_VERSION} VERSION ${GENERIC_LIB_VERSION}
...@@ -64,7 +66,7 @@ set_target_properties(benchmark_main PROPERTIES ...@@ -64,7 +66,7 @@ set_target_properties(benchmark_main PROPERTIES
target_include_directories(benchmark PUBLIC target_include_directories(benchmark PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
) )
target_link_libraries(benchmark_main benchmark) target_link_libraries(benchmark_main benchmark::benchmark)
set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
......
...@@ -38,17 +38,17 @@ add_library(output_test_helper STATIC output_test_helper.cc output_test.h) ...@@ -38,17 +38,17 @@ add_library(output_test_helper STATIC output_test_helper.cc output_test.h)
macro(compile_benchmark_test name) macro(compile_benchmark_test name)
add_executable(${name} "${name}.cc") add_executable(${name} "${name}.cc")
target_link_libraries(${name} benchmark ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries(${name} benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT})
endmacro(compile_benchmark_test) endmacro(compile_benchmark_test)
macro(compile_benchmark_test_with_main name) macro(compile_benchmark_test_with_main name)
add_executable(${name} "${name}.cc") add_executable(${name} "${name}.cc")
target_link_libraries(${name} benchmark_main) target_link_libraries(${name} benchmark::benchmark_main)
endmacro(compile_benchmark_test_with_main) endmacro(compile_benchmark_test_with_main)
macro(compile_output_test name) macro(compile_output_test name)
add_executable(${name} "${name}.cc" output_test.h) add_executable(${name} "${name}.cc" output_test.h)
target_link_libraries(${name} output_test_helper benchmark target_link_libraries(${name} output_test_helper benchmark::benchmark
${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) ${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
endmacro(compile_output_test) endmacro(compile_output_test)
...@@ -178,7 +178,7 @@ add_test(NAME complexity_benchmark COMMAND complexity_test --benchmark_min_time= ...@@ -178,7 +178,7 @@ add_test(NAME complexity_benchmark COMMAND complexity_test --benchmark_min_time=
if (BENCHMARK_ENABLE_GTEST_TESTS) if (BENCHMARK_ENABLE_GTEST_TESTS)
macro(compile_gtest name) macro(compile_gtest name)
add_executable(${name} "${name}.cc") add_executable(${name} "${name}.cc")
target_link_libraries(${name} benchmark target_link_libraries(${name} benchmark::benchmark
gmock_main ${CMAKE_THREAD_LIBS_INIT}) gmock_main ${CMAKE_THREAD_LIBS_INIT})
endmacro(compile_gtest) endmacro(compile_gtest)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment