From a9beffda0b89a6995372100456a4ad894d29b93b Mon Sep 17 00:00:00 2001 From: jmillikin-stripe Date: Thu, 8 Mar 2018 04:48:46 -0800 Subject: [PATCH] Add support for building with Bazel. (#533) * Add myself to CONTRIBUTORS under the corp CLA for Stripe, Inc. * Add support for building with Bazel. Limitations compared to existing CMake rules: * Defaults to using C++11 ``, with an override via Bazel flag `--define` of `google_benchmark.have_regex`. The TravisCI config sets the regex implementation to `posix` because it uses ancient compilers. * Debug vs Opt mode can't be set per test. TravisCI runs all the tests in debug mode to satisfy `diagnostics_test`, which depends on `CHECK` being live. * Set Bazel workspace name so other repos can refer to it by stable name. This is recommended by the Bazel style guide to avoid each dependent workspace defining its own name for the dependency. --- .gitignore | 3 +++ .travis.yml | 11 +++++++++++ AUTHORS | 1 + BUILD.bazel | 21 ++++++++++++++++++++ CONTRIBUTORS | 1 + WORKSPACE | 9 +++++++++ bazel/BUILD | 16 +++++++++++++++ bazel/have_regex.bzl | 7 +++++++ test/BUILD | 47 ++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 116 insertions(+) create mode 100644 BUILD.bazel create mode 100644 WORKSPACE create mode 100644 bazel/BUILD create mode 100644 bazel/have_regex.bzl create mode 100644 test/BUILD diff --git a/.gitignore b/.gitignore index 93d5ced6..aca5f938 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,9 @@ build.ninja install_manifest.txt rules.ninja +# bazel output symlinks. +bazel-* + # out-of-source build top-level folders. build/ _build/ diff --git a/.travis.yml b/.travis.yml index a52683f1..137cc987 100644 --- a/.travis.yml +++ b/.travis.yml @@ -159,11 +159,22 @@ install: brew update; brew install gcc@7; fi + - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then + sudo apt-get update -qq; + sudo apt-get install -qq unzip; + wget https://github.com/bazelbuild/bazel/releases/download/0.10.1/bazel-0.10.1-installer-linux-x86_64.sh --output-document bazel-installer.sh; + sudo bash bazel-installer.sh; + fi + - if [ "${TRAVIS_OS_NAME}" == "osx" ]; then + curl -L -o bazel-installer.sh https://github.com/bazelbuild/bazel/releases/download/0.10.1/bazel-0.10.1-installer-darwin-x86_64.sh; + sudo bash bazel-installer.sh; + fi script: - cmake -DCMAKE_C_COMPILER=${C_COMPILER} -DCMAKE_CXX_COMPILER=${COMPILER} -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCMAKE_CXX_FLAGS="${EXTRA_FLAGS}" -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DBENCHMARK_BUILD_32_BITS=${BUILD_32_BITS} .. - make - ctest -C ${BUILD_TYPE} --output-on-failure + - bazel test -c dbg --define google_benchmark.have_regex=posix --announce_rc --verbose_failures --test_output=errors --keep_going //test/... after_success: - if [ "${BUILD_TYPE}" == "Coverage" -a "${TRAVIS_OS_NAME}" == "linux" ]; then diff --git a/AUTHORS b/AUTHORS index e574b23b..45adb27e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -39,6 +39,7 @@ Radoslav Yovchev Roman Lebedev Shuo Chen Steinar H. Gunderson +Stripe, Inc. Yixuan Qiu Yusuke Suzuki Zbigniew Skowron diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 00000000..883ccc44 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1,21 @@ +licenses(["notice"]) + +load("//bazel:have_regex.bzl", "have_regex_copts") + +cc_library( + name = "benchmark", + srcs = glob([ + "src/*.cc", + "src/*.h", + ]), + hdrs = ["include/benchmark/benchmark.h"], + copts = have_regex_copts(), + strip_include_prefix = "include", + visibility = ["//visibility:public"], +) + +cc_library( + name = "benchmark_internal_headers", + hdrs = glob(["src/*.h"]), + visibility = ["//test:__pkg__"], +) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index b942722e..2f1999be 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -38,6 +38,7 @@ Ismael Jimenez Martinez Jern-Kuan Leong JianXiong Zhou Joao Paulo Magalhaes +John Millikin Jussi Knuuttila Kai Wolf Kishan Kumar diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 00000000..9ba32fd7 --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,9 @@ +workspace(name = "com_github_google_benchmark") + +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") + +git_repository( + name = "com_google_googletest", + commit = "3f0cf6b62ad1eb50d8736538363d3580dd640c3e", # HEAD + remote = "https://github.com/google/googletest", +) diff --git a/bazel/BUILD b/bazel/BUILD new file mode 100644 index 00000000..dcbd4376 --- /dev/null +++ b/bazel/BUILD @@ -0,0 +1,16 @@ +package(default_visibility = ["//:__subpackages__"]) + +config_setting( + name = "have_std_regex", + values = {"define": "google_benchmark.have_regex=std"}, +) + +config_setting( + name = "have_posix_regex", + values = {"define": "google_benchmark.have_regex=posix"}, +) + +config_setting( + name = "have_gnu_posix_regex", + values = {"define": "google_benchmark.have_regex=gnu_posix"}, +) diff --git a/bazel/have_regex.bzl b/bazel/have_regex.bzl new file mode 100644 index 00000000..6ea5ba01 --- /dev/null +++ b/bazel/have_regex.bzl @@ -0,0 +1,7 @@ +def have_regex_copts(): + return select({ + "//bazel:have_std_regex": ["-DHAVE_STD_REGEX"], + "//bazel:have_posix_regex": ["-DHAVE_POSIX_REGEX"], + "//bazel:have_gnu_posix_regex": ["-DHAVE_GNU_POSIX_REGEX"], + "//conditions:default": ["-DHAVE_STD_REGEX"], + }) diff --git a/test/BUILD b/test/BUILD new file mode 100644 index 00000000..9e5d4936 --- /dev/null +++ b/test/BUILD @@ -0,0 +1,47 @@ +load("//bazel:have_regex.bzl", "have_regex_copts") + +NEEDS_GTEST_MAIN = [ + "statistics_test.cc", +] + +TEST_COPTS = [ + "-pedantic", + "-pedantic-errors", + "-std=c++11", +] + have_regex_copts() + +TEST_ARGS = ["--benchmark_min_time=0.01"] + +cc_library( + name = "output_test_helper", + testonly = 1, + srcs = ["output_test_helper.cc"], + hdrs = ["output_test.h"], + copts = TEST_COPTS, + deps = [ + "//:benchmark", + "//:benchmark_internal_headers", + ], +) + +[cc_test( + name = test_src[:-len(".cc")], + size = "small", + srcs = [test_src], + args = TEST_ARGS + ({ + "user_counters_tabular_test.cc": ["--benchmark_counters_tabular=true"], + }).get(test_src, []), + copts = TEST_COPTS + ({ + "cxx03_test.cc": ["-std=c++03"], + # Some of the issues with DoNotOptimize only occur when optimization is enabled + "donotoptimize_test.cc": ["-O3"], + }).get(test_src, []), + deps = [ + ":output_test_helper", + "//:benchmark", + "//:benchmark_internal_headers", + "@com_google_googletest//:gtest", + ] + ( + ["@com_google_googletest//:gtest_main"] if (test_src in NEEDS_GTEST_MAIN) else [] + ), +) for test_src in glob(["*_test.cc"])]