From 7eaa97b742c80b0f171c214b730faa930fe96d20 Mon Sep 17 00:00:00 2001 From: Jordan Williams Date: Mon, 13 Jan 2020 19:31:44 -0600 Subject: [PATCH 1/6] add Jordan Williams to both CONTRIBUTORS and AUTHORS --- AUTHORS | 1 + CONTRIBUTORS | 1 + 2 files changed, 2 insertions(+) diff --git a/AUTHORS b/AUTHORS index 06295c1ea0..89205a1adb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -33,6 +33,7 @@ Ismael Jimenez Martinez Jern-Kuan Leong JianXiong Zhou Joao Paulo Magalhaes +Jordan Williams Jussi Knuuttila Kaito Udagawa Kishan Kumar diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 04e8aa6a47..88f7eee06c 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -50,6 +50,7 @@ Jern-Kuan Leong JianXiong Zhou Joao Paulo Magalhaes John Millikin +Jordan Williams Jussi Knuuttila Kai Wolf Kaito Udagawa From 4c31f0cf03a95fc71f63e0fcef2f75a4d6021f83 Mon Sep 17 00:00:00 2001 From: Jordan Williams Date: Mon, 13 Jan 2020 19:57:30 -0600 Subject: [PATCH 2/6] 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. --- src/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2650e3d196..5490d61ad4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,6 +18,7 @@ foreach(item ${BENCHMARK_MAIN}) endforeach() add_library(benchmark ${SOURCE_FILES}) +add_library(benchmark::benchmark ALIAS benchmark) set_target_properties(benchmark PROPERTIES OUTPUT_NAME "benchmark" VERSION ${GENERIC_LIB_VERSION} @@ -55,6 +56,7 @@ endif() # Benchmark main library add_library(benchmark_main "benchmark_main.cc") +add_library(benchmark::benchmark_main ALIAS benchmark_main) set_target_properties(benchmark_main PROPERTIES OUTPUT_NAME "benchmark_main" VERSION ${GENERIC_LIB_VERSION} From e207edc45b6939aa16df837920e45c28fd74d02c Mon Sep 17 00:00:00 2001 From: Jordan Williams Date: Mon, 13 Jan 2020 20:11:26 -0600 Subject: [PATCH 3/6] 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. --- src/CMakeLists.txt | 2 +- test/CMakeLists.txt | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5490d61ad4..81c902c5cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -66,7 +66,7 @@ set_target_properties(benchmark_main PROPERTIES target_include_directories(benchmark PUBLIC $ ) -target_link_libraries(benchmark_main benchmark) +target_link_libraries(benchmark_main benchmark::benchmark) set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ddcb1a1eb9..6afe47c400 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -38,17 +38,17 @@ add_library(output_test_helper STATIC output_test_helper.cc output_test.h) macro(compile_benchmark_test name) 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) macro(compile_benchmark_test_with_main name) add_executable(${name} "${name}.cc") - target_link_libraries(${name} benchmark_main) + target_link_libraries(${name} benchmark::benchmark_main) endmacro(compile_benchmark_test_with_main) macro(compile_output_test name) 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}) endmacro(compile_output_test) @@ -178,7 +178,7 @@ add_test(NAME complexity_benchmark COMMAND complexity_test --benchmark_min_time= if (BENCHMARK_ENABLE_GTEST_TESTS) macro(compile_gtest name) add_executable(${name} "${name}.cc") - target_link_libraries(${name} benchmark + target_link_libraries(${name} benchmark::benchmark gmock_main ${CMAKE_THREAD_LIBS_INIT}) endmacro(compile_gtest) From 3711149e55ccb4f2b5d2cb6417c315c9b4264388 Mon Sep 17 00:00:00 2001 From: Jordan Williams Date: Mon, 13 Jan 2020 20:23:01 -0600 Subject: [PATCH 4/6] document the provided alias targets --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d972ab050a..ef0beba95f 100644 --- a/README.md +++ b/README.md @@ -177,9 +177,9 @@ BENCHMARK(BM_StringCopy); BENCHMARK_MAIN(); ``` -To run the benchmark, compile and link against the `benchmark` library -(libbenchmark.a/.so). If you followed the build steps above, this -library will be under the build directory you created. +To run the benchmark, compile and link against the `benchmark` library (libbenchmark.a/.so). +When using CMake, it is recommended to link against the provided `benchmark::benchmark` target. +If you followed the build steps above, this library will be under the build directory you created. ```bash # Example on linux after running the build steps above. Assumes the @@ -190,6 +190,7 @@ $ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \ Alternatively, link against the `benchmark_main` library and remove `BENCHMARK_MAIN();` above to get the same behavior. +As with the `benchmark` library, a `benchmark::benchmark_main` CMake target is provided. The compiled executable will run all benchmarks by default. Pass the `--help` flag for option information or see the guide below. From fbd519277aa319a388c07e980dd31494786b0f92 Mon Sep 17 00:00:00 2001 From: Jordan Williams Date: Mon, 13 Jan 2020 20:37:01 -0600 Subject: [PATCH 5/6] 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. --- README.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef0beba95f..7f414a479a 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,6 @@ BENCHMARK_MAIN(); ``` To run the benchmark, compile and link against the `benchmark` library (libbenchmark.a/.so). -When using CMake, it is recommended to link against the provided `benchmark::benchmark` target. If you followed the build steps above, this library will be under the build directory you created. ```bash @@ -190,11 +189,25 @@ $ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \ Alternatively, link against the `benchmark_main` library and remove `BENCHMARK_MAIN();` above to get the same behavior. -As with the `benchmark` library, a `benchmark::benchmark_main` CMake target is provided. The compiled executable will run all benchmarks by default. Pass the `--help` 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 ### Building with GCC From ef9aa754ae9f6afdbc3f9e414e6d1c528e714da4 Mon Sep 17 00:00:00 2001 From: Jordan Williams Date: Tue, 14 Jan 2020 11:28:31 -0600 Subject: [PATCH 6/6] 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. --- README.md | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7f414a479a..560464eb88 100644 --- a/README.md +++ b/README.md @@ -177,8 +177,9 @@ BENCHMARK(BM_StringCopy); BENCHMARK_MAIN(); ``` -To run the benchmark, compile and link against the `benchmark` library (libbenchmark.a/.so). -If you followed the build steps above, this library will be under the build directory you created. +To run the benchmark, compile and link against the `benchmark` library +(libbenchmark.a/.so). If you followed the build steps above, this library will +be under the build directory you created. ```bash # Example on linux after running the build steps above. Assumes the @@ -193,13 +194,18 @@ Alternatively, link against the `benchmark_main` library and remove The compiled executable will run all benchmarks by default. Pass the `--help` 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. +### 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. +Alternatively, ```add_subdirectory``` will incorporate the library directly in +to one's CMake project. ```cmake add_subdirectory(benchmark) ```