Skip to content

Commit

Permalink
Add benchmark_main target. (#601)
Browse files Browse the repository at this point in the history
* Add benchmark_main library with support for Bazel.

* fix newline at end of file

* Add CMake support for benchmark_main.

* Mention optionally using benchmark_main in README.
  • Loading branch information
astrelni authored and dominichamon committed May 25, 2018
1 parent d7aed73 commit e776aa0
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 7 deletions.
20 changes: 16 additions & 4 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ config_setting(

cc_library(
name = "benchmark",
srcs = glob([
"src/*.cc",
"src/*.h",
]),
srcs = glob(
[
"src/*.cc",
"src/*.h",
],
exclude = ["src/benchmark_main.cc"],
),
hdrs = ["include/benchmark/benchmark.h"],
linkopts = select({
":windows": ["-DEFAULTLIB:shlwapi.lib"],
Expand All @@ -23,6 +26,15 @@ cc_library(
visibility = ["//visibility:public"],
)

cc_library(
name = "benchmark_main",
srcs = ["src/benchmark_main.cc"],
hdrs = ["include/benchmark/benchmark.h"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
deps = [":benchmark"],
)

cc_library(
name = "benchmark_internal_headers",
hdrs = glob(["src/*.h"]),
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ BENCHMARK(BM_StringCopy);
BENCHMARK_MAIN();
```
Don't forget to inform your linker to add benchmark library e.g. through `-lbenchmark` compilation flag.
Don't forget to inform your linker to add benchmark library e.g. through
`-lbenchmark` compilation flag. Alternatively, you may leave out the
`BENCHMARK_MAIN();` at the end of the source file and link against
`-lbenchmark_main` to get the same default behavior.
The benchmark library will reporting the timing for the code within the `for(...)` loop.
Expand Down
15 changes: 14 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ file(GLOB
*.cc
${PROJECT_SOURCE_DIR}/include/benchmark/*.h
${CMAKE_CURRENT_SOURCE_DIR}/*.h)
list(FILTER SOURCE_FILES EXCLUDE REGEX "benchmark_main\\.cc")

add_library(benchmark ${SOURCE_FILES})
set_target_properties(benchmark PROPERTIES
Expand Down Expand Up @@ -39,6 +40,18 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
target_link_libraries(benchmark kstat)
endif()

# Benchmark main library
add_library(benchmark_main "benchmark_main.cc")
set_target_properties(benchmark_main PROPERTIES
OUTPUT_NAME "benchmark_main"
VERSION ${GENERIC_LIB_VERSION}
SOVERSION ${GENERIC_LIB_SOVERSION}
)
target_include_directories(benchmark PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
)
target_link_libraries(benchmark_main benchmark)

set(include_install_dir "include")
set(lib_install_dir "lib/")
set(bin_install_dir "bin/")
Expand All @@ -65,7 +78,7 @@ configure_file("${PROJECT_SOURCE_DIR}/cmake/benchmark.pc.in" "${pkg_config}" @ON
if (BENCHMARK_ENABLE_INSTALL)
# Install target (will install the library to specified CMAKE_INSTALL_PREFIX variable)
install(
TARGETS benchmark
TARGETS benchmark benchmark_main
EXPORT ${targets_export_name}
ARCHIVE DESTINATION ${lib_install_dir}
LIBRARY DESTINATION ${lib_install_dir}
Expand Down
17 changes: 17 additions & 0 deletions src/benchmark_main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2018 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "benchmark/benchmark.h"

BENCHMARK_MAIN();
10 changes: 9 additions & 1 deletion test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,13 @@ cc_library(
# FIXME: Add support for assembly tests to bazel.
# See Issue #556
# https://github.com/google/benchmark/issues/556
) for test_src in glob(["*test.cc"], exclude = ["*_assembly_test.cc"])
) for test_src in glob(["*test.cc"], exclude = ["*_assembly_test.cc", "link_main_test.cc"])
]

cc_test(
name = "link_main_test",
size = "small",
srcs = ["link_main_test.cc"],
copts = TEST_COPTS,
deps = ["//:benchmark_main"],
)
7 changes: 7 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ macro(compile_benchmark_test name)
target_link_libraries(${name} 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)
endmacro(compile_benchmark_test_with_main)

macro(compile_output_test name)
add_executable(${name} "${name}.cc" output_test.h)
Expand Down Expand Up @@ -109,6 +113,9 @@ add_test(map_test map_test --benchmark_min_time=0.01)
compile_benchmark_test(multiple_ranges_test)
add_test(multiple_ranges_test multiple_ranges_test --benchmark_min_time=0.01)

compile_benchmark_test_with_main(link_main_test)
add_test(link_main_test link_main_test --benchmark_min_time=0.01)

compile_output_test(reporter_output_test)
add_test(reporter_output_test reporter_output_test --benchmark_min_time=0.01)

Expand Down
8 changes: 8 additions & 0 deletions test/link_main_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "benchmark/benchmark.h"

void BM_empty(benchmark::State& state) {
for (auto _ : state) {
benchmark::DoNotOptimize(state.iterations());
}
}
BENCHMARK(BM_empty);

0 comments on commit e776aa0

Please sign in to comment.