Skip to content

Commit

Permalink
Minimal Bazel infrastructure (#415). (#577)
Browse files Browse the repository at this point in the history
* Minimal Bazel infrastructure (#415).

This patch introduces the minimal Bazel infrastructure required to build and execute utility_test,
as a step towards conversion of the cmake build system to Bazel (#415). The main contributions are:

* Machinery for declaring Envoy C++ library and test targets (envoy_cc_library, envoy_cc_test).

* External dependency management that works both inside the Docker CI environment (using prebuilts)
  and with developer-local dependency fetch/build (based on #416).

* Handling of the implicit dependencies that are today sourced via prebuilts.

* Simple example of building and running a unit test with only its dependencies built. With the
  cmake system, we would need to build all of Envoy and all tests to run utility_test. E.g.

  blaze test //test/common/common:utility_test

This is not intended to be used by anyone other than those working with the Bazel conversion at this
point. The plan is to add "ci/do_ci.sh bazel.debug" as a Travis target to ensure we don't bit rot.
  • Loading branch information
htuch authored and mattklein123 committed Mar 17, 2017
1 parent dea277a commit 72e88af
Show file tree
Hide file tree
Showing 14 changed files with 261 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/build
/docs/landing_source/.bundle
/generated
/bazel-*
/ci/bazel-*
/ci/prebuilt/thirdparty
/ci/prebuilt/thirdparty_build
cscope.*
BROWSE
15 changes: 15 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
workspace(name = "envoy")

load("//bazel:repositories.bzl", "envoy_dependencies")

envoy_dependencies()

bind(
name = "googletest_main",
actual = "@googletest//:googletest_main",
)

bind(
name = "spdlog",
actual = "@spdlog_git//:spdlog",
)
Empty file added bazel/BUILD
Empty file.
54 changes: 54 additions & 0 deletions bazel/envoy_build_system.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
ENVOY_COPTS = [
"-fno-omit-frame-pointer",
"-Wall",
"-Wextra",
"-Werror",
"-Wnon-virtual-dtor",
"-Woverloaded-virtual",
"-Wold-style-cast",
"-std=c++0x",
"-includesource/precompiled/precompiled.h",
"-iquoteinclude",
"-iquotesource",
]

# References to Envoy external dependencies should be wrapped with this function.
def envoy_external_dep_path(dep):
return "//external:%s" % dep

# Envoy C++ library targets should be specified with this function.
def envoy_cc_library(name,
srcs = [],
hdrs = [],
public_hdrs = [],
copts = [],
alwayslink = None,
visibility = None,
external_deps = [],
deps = []):
native.cc_library(
name = name,
srcs = srcs,
hdrs = hdrs + public_hdrs,
copts = ENVOY_COPTS + copts,
visibility = visibility,
deps = deps + [envoy_external_dep_path(dep) for dep in external_deps] + [
"//source/precompiled:precompiled_includes",
],
alwayslink = alwayslink,
)

# Envoy C++ test targets should be specified with this function.
def envoy_cc_test(name,
srcs = [],
deps = []):
native.cc_test(
name = name,
srcs = srcs,
copts = ENVOY_COPTS + ["-includetest/precompiled/precompiled_test.h"],
linkopts = ["-pthread"],
deps = deps + [
"//source/precompiled:precompiled_includes",
"//test/precompiled:precompiled_includes",
],
)
69 changes: 69 additions & 0 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# The build rules below for external dependencies build rules are maintained on a best effort basis.
# The rules are provided for developer convenience. For production builds, we recommend building the
# libraries according to their canonical build systems and expressing the dependencies in a manner
# similar to ci/WORKSPACE.

def googletest_repositories():
BUILD = """
cc_library(
name = "googletest",
srcs = [
"googlemock/src/gmock-all.cc",
"googletest/src/gtest-all.cc",
],
hdrs = glob([
"googlemock/include/**/*.h",
"googlemock/src/*.cc",
"googletest/include/**/*.h",
"googletest/src/*.cc",
"googletest/src/*.h",
]),
includes = [
"googlemock",
"googlemock/include",
"googletest",
"googletest/include",
],
visibility = ["//visibility:public"],
)
cc_library(
name = "googletest_main",
srcs = ["googlemock/src/gmock_main.cc"],
visibility = ["//visibility:public"],
deps = [":googletest"],
)
"""
native.new_git_repository(
name = "googletest",
build_file_content = BUILD,
# v1.8.0 release
commit = "ec44c6c1675c25b9827aacd08c02433cccde7780",
remote = "https://github.com/google/googletest.git",
)

def spdlog_repositories():
BUILD = """
package(default_visibility = ["//visibility:public"])
cc_library(
name = "spdlog",
hdrs = glob([
"include/**/*.cc",
"include/**/*.h",
]),
strip_include_prefix = "include",
)
"""

native.new_git_repository(
name = "spdlog_git",
build_file_content = BUILD,
# v0.11.0 release
commit = "1f1f6a5f3b424203a429e9cb78e6548037adefa8",
remote = "https://github.com/gabime/spdlog.git",
)

def envoy_dependencies():
googletest_repositories()
spdlog_repositories()
9 changes: 9 additions & 0 deletions ci/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
bind(
name = "googletest_main",
actual = "//ci/prebuilt:googletest_main",
)

bind(
name = "spdlog",
actual = "//ci/prebuilt:spdlog",
)
20 changes: 20 additions & 0 deletions ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ set -e

echo "building using $NUM_CPUS CPUs"

if [[ "$1" == "bazel.debug" ]]; then
echo "debug bazel build with tests..."
cd ci
ln -sf /thirdparty prebuilt
ln -sf /thirdparty_build prebuilt
# Not sandboxing, since non-privileged Docker can't do nested namespaces.
echo "Building..."
export CC=gcc-4.9
export CXX=g++-4.9
export USER=bazel
export TEST_TMPDIR=/source
BAZEL_OPTIONS="--strategy=CppCompile=standalone --strategy=CppLink=standalone \
--strategy=TestRunner=standalone --verbose_failures --package_path %workspace%:.."
[[ "$BAZEL_INTERACTIVE" == "1" ]] && BAZEL_BATCH="" || BAZEL_BATCH="--batch"
bazel $BAZEL_BATCH build $BAZEL_OPTIONS //source/...
echo "Testing..."
bazel $BAZEL_BATCH test $BAZEL_OPTIONS --test_output=all //test/...
exit 0
fi

. "$(dirname "$0")"/build_setup.sh

if [[ "$1" == "fix_format" ]]; then
Expand Down
24 changes: 24 additions & 0 deletions ci/prebuilt/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "googletest_main",
srcs = [
"thirdparty_build/lib/libgmock.a",
"thirdparty_build/lib/libgtest.a",
"thirdparty_build/lib/libgtest_main.a",
],
hdrs = glob([
"thirdparty_build/include/gmock/**/*.h",
"thirdparty_build/include/gtest/**/*.h",
]),
strip_include_prefix = "thirdparty_build/include",
)

cc_library(
name = "spdlog",
hdrs = glob([
"thirdparty/spdlog-0.11.0/include/**/*.cc",
"thirdparty/spdlog-0.11.0/include/**/*.h",
]),
strip_include_prefix = "thirdparty/spdlog-0.11.0/include",
)
22 changes: 22 additions & 0 deletions include/envoy/common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package(default_visibility = ["//visibility:public"])

load("//bazel:envoy_build_system.bzl", "envoy_cc_library")

envoy_cc_library(
name = "base_includes",
hdrs = [
"exception.h",
"pure.h",
],
)

envoy_cc_library(
name = "time_includes",
hdrs = ["time.h"],
deps = [":base_includes"],
)

envoy_cc_library(
name = "optional_includes",
hdrs = ["optional.h"],
)
10 changes: 10 additions & 0 deletions source/common/common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package(default_visibility = ["//visibility:public"])

load("//bazel:envoy_build_system.bzl", "envoy_cc_library")

envoy_cc_library(
name = "utility_lib",
srcs = ["utility.cc"],
hdrs = ["utility.h"],
deps = ["//include/envoy/common:time_includes"],
)
9 changes: 9 additions & 0 deletions source/precompiled/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package(default_visibility = ["//visibility:public"])

load("//bazel:envoy_build_system.bzl", "envoy_external_dep_path")

cc_library(
name = "precompiled_includes",
hdrs = ["precompiled.h"],
deps = [envoy_external_dep_path("spdlog")],
)
7 changes: 7 additions & 0 deletions test/common/common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//bazel:envoy_build_system.bzl", "envoy_cc_test")

envoy_cc_test(
name = "utility_test",
srcs = ["utility_test.cc"],
deps = ["//source/common/common:utility_lib"],
)
12 changes: 12 additions & 0 deletions test/precompiled/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package(default_visibility = ["//visibility:public"])

load("//bazel:envoy_build_system.bzl", "envoy_external_dep_path")

cc_library(
name = "precompiled_includes",
hdrs = ["precompiled_test.h"],
deps = [
envoy_external_dep_path("googletest_main"),
"//test/test_common:printers_includes",
],
)
6 changes: 6 additions & 0 deletions test/test_common/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//visibility:public"])

cc_library(
name = "printers_includes",
hdrs = ["printers.h"],
)

0 comments on commit 72e88af

Please sign in to comment.