Skip to content

Commit 7fa5513

Browse files
authored
build: external_cmake for cmake deps. (#5218)
This revives #4516, replacing all external cmake deps with the pending bazel foreign_cc support. Risk Level: Low Testing: bazel test //test/... Signed-off-by: Harvey Tuch <[email protected]>
1 parent 8cc406d commit 7fa5513

28 files changed

+260
-301
lines changed

WORKSPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ load("//bazel:cc_configure.bzl", "cc_configure")
55

66
envoy_dependencies()
77

8+
load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")
9+
10+
rules_foreign_cc_dependencies()
11+
812
cc_configure()
913

1014
load("@envoy_api//bazel:repositories.bzl", "api_dependencies")

bazel/EXTERNAL_DEPS.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ build process.
1717
`external_deps` attribute.
1818
3. `bazel test //test/...`
1919

20+
# Adding external dependencies to Envoy (external CMake)
21+
22+
This is the preferred style of adding dependencies that use CMake for their build system.
23+
24+
1. Define a the source Bazel repository in [`bazel/repositories.bzl`](repositories.bzl), in the
25+
`envoy_dependencies()` function.
26+
2. Add a `cmake_external` rule to [`bazel/foreign_cc/BUILD`](bazel/foreign_cc/BUILD). This will
27+
reference the source repository in step 1.
28+
3. Reference your new external dependency in some `envoy_cc_library` via the name bound in step 1
29+
`external_deps` attribute.
30+
4. `bazel test //test/...`
31+
2032
# Adding external dependencies to Envoy (genrule repository)
2133

2234
This is the newer style of adding dependencies with no upstream Bazel configs.
@@ -44,8 +56,9 @@ to binaries, libraries, headers, etc.
4456

4557
# Adding external dependencies to Envoy (build recipe)
4658

47-
This is the older style of adding dependencies. It uses shell scripts to build
48-
and install dependencies into a shared directory prefix.
59+
This is the older style of adding dependencies. It uses shell scripts to build and install
60+
dependencies into a shared directory prefix. This should no longer be used unless there are
61+
extenuating circumstances.
4962

5063
1. Add a build recipe X in [`ci/build_container/build_recipes`](../ci/build_container/build_recipes)
5164
for developer-local and CI external dependency build flows.

bazel/foreign_cc/BUILD

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
licenses(["notice"]) # Apache 2
2+
3+
load("//bazel:envoy_build_system.bzl", "envoy_package")
4+
load("@rules_foreign_cc//tools/build_defs:cmake.bzl", "cmake_external")
5+
6+
envoy_package()
7+
8+
cmake_external(
9+
name = "ares",
10+
cache_entries = {
11+
"CARES_SHARED": "no",
12+
"CARES_STATIC": "on",
13+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
14+
},
15+
cmake_options = ["-GNinja"],
16+
lib_source = "@com_github_c_ares_c_ares//:all",
17+
make_commands = [
18+
"ninja",
19+
"ninja install",
20+
],
21+
static_libraries = ["libcares.a"],
22+
)
23+
24+
cmake_external(
25+
name = "event",
26+
cache_entries = {
27+
"EVENT__DISABLE_OPENSSL": "on",
28+
"EVENT__DISABLE_REGRESS": "on",
29+
"CMAKE_BUILD_TYPE": "Release",
30+
},
31+
cmake_options = ["-GNinja"],
32+
lib_source = "@com_github_libevent_libevent//:all",
33+
make_commands = [
34+
"ninja",
35+
"ninja install",
36+
],
37+
static_libraries = ["libevent.a"],
38+
)
39+
40+
cmake_external(
41+
name = "nghttp2",
42+
cache_entries = {
43+
"ENABLE_STATIC_LIB": "on",
44+
"ENABLE_LIB_ONLY": "on",
45+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
46+
},
47+
cmake_options = ["-GNinja"],
48+
lib_source = "@com_github_nghttp2_nghttp2//:all",
49+
make_commands = [
50+
"ninja",
51+
"ninja install",
52+
],
53+
static_libraries = ["libnghttp2.a"],
54+
)
55+
56+
cmake_external(
57+
name = "yaml",
58+
cache_entries = {
59+
"YAML_CPP_BUILD_TESTS": "off",
60+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
61+
},
62+
cmake_options = ["-GNinja"],
63+
lib_source = "@com_github_jbeder_yaml_cpp//:all",
64+
make_commands = [
65+
"ninja",
66+
"ninja install",
67+
],
68+
static_libraries = ["libyaml-cpp.a"],
69+
)
70+
71+
cmake_external(
72+
name = "zlib",
73+
cache_entries = {
74+
"CMAKE_BUILD_TYPE": "RelWithDebInfo",
75+
},
76+
cmake_options = ["-GNinja"],
77+
lib_source = "@com_github_madler_zlib//:all",
78+
make_commands = [
79+
"ninja",
80+
"ninja install",
81+
],
82+
static_libraries = ["libz.a"],
83+
)

bazel/foreign_cc/nghttp2.patch

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
2+
index 17e422b2..e58070f5 100644
3+
--- a/lib/CMakeLists.txt
4+
+++ b/lib/CMakeLists.txt
5+
@@ -52,6 +52,7 @@
6+
COMPILE_FLAGS "${WARNCFLAGS}"
7+
VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION}
8+
ARCHIVE_OUTPUT_NAME nghttp2
9+
+ ARCHIVE_OUTPUT_DIRECTORY static
10+
)
11+
target_compile_definitions(nghttp2_static PUBLIC "-DNGHTTP2_STATICLIB")
12+
if(ENABLE_STATIC_LIB)

bazel/repositories.bzl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ PPC_SKIP_TARGETS = {"luajit": "envoy.filters.http.lua"}
1515
# go version for rules_go
1616
GO_VERSION = "1.10.4"
1717

18+
# Make all contents of an external repository accessible under a filegroup. Used for external HTTP
19+
# archives, e.g. cares.
20+
BUILD_ALL_CONTENT = """filegroup(name = "all", srcs = glob(["**"]), visibility = ["//visibility:public"])"""
21+
1822
def _repository_impl(name, **kwargs):
1923
# `existing_rule_keys` contains the names of repositories that have already
2024
# been defined in the Bazel workspace. By skipping repos with existing keys,
@@ -265,6 +269,9 @@ def envoy_dependencies(path = "@envoy_deps//", skip_targets = []):
265269
if "envoy_build_config" not in native.existing_rules().keys():
266270
_default_envoy_build_config(name = "envoy_build_config")
267271

272+
# Setup rules_foreign_cc
273+
_foreign_cc_dependencies()
274+
268275
# Binding to an alias pointing to the selected version of BoringSSL:
269276
# - BoringSSL FIPS from @boringssl_fips//:ssl,
270277
# - non-FIPS BoringSSL from @boringssl//:ssl.
@@ -281,6 +288,7 @@ def envoy_dependencies(path = "@envoy_deps//", skip_targets = []):
281288
_com_google_absl()
282289
_com_github_bombela_backward()
283290
_com_github_circonus_labs_libcircllhist()
291+
_com_github_c_ares_c_ares()
284292
_com_github_cyan4973_xxhash()
285293
_com_github_eile_tclap()
286294
_com_github_fmtlib_fmt()
@@ -291,8 +299,13 @@ def envoy_dependencies(path = "@envoy_deps//", skip_targets = []):
291299
_com_lightstep_tracer_cpp()
292300
_com_github_datadog_dd_opentracing_cpp()
293301
_com_github_grpc_grpc()
302+
_com_github_google_benchmark()
294303
_com_github_google_jwt_verify()
304+
_com_github_jbeder_yaml_cpp()
305+
_com_github_libevent_libevent()
306+
_com_github_madler_zlib()
295307
_com_github_nanopb_nanopb()
308+
_com_github_nghttp2_nghttp2()
296309
_com_github_nodejs_http_parser()
297310
_com_github_tencent_rapidjson()
298311
_com_google_googletest()
@@ -341,6 +354,18 @@ def _com_github_circonus_labs_libcircllhist():
341354
actual = "@com_github_circonus_labs_libcircllhist//:libcircllhist",
342355
)
343356

357+
def _com_github_c_ares_c_ares():
358+
location = REPOSITORY_LOCATIONS["com_github_c_ares_c_ares"]
359+
http_archive(
360+
name = "com_github_c_ares_c_ares",
361+
build_file_content = BUILD_ALL_CONTENT,
362+
**location
363+
)
364+
native.bind(
365+
name = "ares",
366+
actual = "//bazel/foreign_cc:ares",
367+
)
368+
344369
def _com_github_cyan4973_xxhash():
345370
_repository_impl(
346371
name = "com_github_cyan4973_xxhash",
@@ -401,6 +426,17 @@ def _com_github_gcovr_gcovr():
401426
actual = "@com_github_gcovr_gcovr//:gcovr",
402427
)
403428

429+
def _com_github_google_benchmark():
430+
location = REPOSITORY_LOCATIONS["com_github_google_benchmark"]
431+
http_archive(
432+
name = "com_github_google_benchmark",
433+
**location
434+
)
435+
native.bind(
436+
name = "benchmark",
437+
actual = "@com_github_google_benchmark//:benchmark",
438+
)
439+
404440
def _com_github_google_libprotobuf_mutator():
405441
_repository_impl(
406442
name = "com_github_google_libprotobuf_mutator",
@@ -411,6 +447,57 @@ def _com_github_google_libprotobuf_mutator():
411447
actual = "@com_github_google_libprotobuf_mutator//:libprotobuf_mutator",
412448
)
413449

450+
def _com_github_jbeder_yaml_cpp():
451+
location = REPOSITORY_LOCATIONS["com_github_jbeder_yaml_cpp"]
452+
http_archive(
453+
name = "com_github_jbeder_yaml_cpp",
454+
build_file_content = BUILD_ALL_CONTENT,
455+
**location
456+
)
457+
native.bind(
458+
name = "yaml_cpp",
459+
actual = "//bazel/foreign_cc:yaml",
460+
)
461+
462+
def _com_github_libevent_libevent():
463+
location = REPOSITORY_LOCATIONS["com_github_libevent_libevent"]
464+
http_archive(
465+
name = "com_github_libevent_libevent",
466+
build_file_content = BUILD_ALL_CONTENT,
467+
**location
468+
)
469+
native.bind(
470+
name = "event",
471+
actual = "//bazel/foreign_cc:event",
472+
)
473+
474+
def _com_github_madler_zlib():
475+
location = REPOSITORY_LOCATIONS["com_github_madler_zlib"]
476+
http_archive(
477+
name = "com_github_madler_zlib",
478+
build_file_content = BUILD_ALL_CONTENT,
479+
**location
480+
)
481+
native.bind(
482+
name = "zlib",
483+
actual = "//bazel/foreign_cc:zlib",
484+
)
485+
486+
def _com_github_nghttp2_nghttp2():
487+
location = REPOSITORY_LOCATIONS["com_github_nghttp2_nghttp2"]
488+
http_archive(
489+
name = "com_github_nghttp2_nghttp2",
490+
build_file_content = BUILD_ALL_CONTENT,
491+
patch_args = ["-p1"],
492+
patch_cmds = ["find . -name '*.sh' -exec sed -i.orig '1s|#!/usr/bin/env sh\$|/bin/sh\$|' {} +"],
493+
patches = ["//bazel/foreign_cc:nghttp2.patch"],
494+
**location
495+
)
496+
native.bind(
497+
name = "nghttp2",
498+
actual = "//bazel/foreign_cc:nghttp2",
499+
)
500+
414501
def _io_opentracing_cpp():
415502
_repository_impl("io_opentracing_cpp")
416503
native.bind(
@@ -645,6 +732,9 @@ def _com_github_google_jwt_verify():
645732
actual = "@com_github_google_jwt_verify//:jwt_verify_lib",
646733
)
647734

735+
def _foreign_cc_dependencies():
736+
_repository_impl("rules_foreign_cc")
737+
648738
def _apply_dep_blacklist(ctxt, recipes):
649739
newlist = []
650740
skip_list = []

bazel/repository_locations.bzl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ REPOSITORY_LOCATIONS = dict(
3131
strip_prefix = "backward-cpp-1.4",
3232
urls = ["https://github.com/bombela/backward-cpp/archive/v1.4.tar.gz"],
3333
),
34+
com_github_c_ares_c_ares = dict(
35+
sha256 = "7deb7872cbd876c29036d5f37e30c4cbc3cc068d59d8b749ef85bb0736649f04",
36+
strip_prefix = "c-ares-cares-1_15_0",
37+
urls = ["https://github.com/c-ares/c-ares/archive/cares-1_15_0.tar.gz"],
38+
),
3439
com_github_circonus_labs_libcircllhist = dict(
3540
sha256 = "9949e2864b8ad00ee5c3e9c1c3c01e51b6b68bb442a919652fc66b9776477987",
3641
strip_prefix = "libcircllhist-fd8a14463739d247b414825cc56ca3946792a3b9",
@@ -83,6 +88,11 @@ REPOSITORY_LOCATIONS = dict(
8388
strip_prefix = "nanopb-0.3.9.2",
8489
urls = ["https://github.com/nanopb/nanopb/archive/0.3.9.2.tar.gz"],
8590
),
91+
com_github_nghttp2_nghttp2 = dict(
92+
sha256 = "cb70261634c33dc5adbe780afcfc5dab17838ee303631a02b983c6a217bc16ba",
93+
strip_prefix = "nghttp2-1.35.1",
94+
urls = ["https://github.com/nghttp2/nghttp2/releases/download/v1.35.1/nghttp2-1.35.1.tar.gz"],
95+
),
8696
io_opentracing_cpp = dict(
8797
sha256 = "015c4187f7a6426a2b5196f0ccd982aa87f010cf61f507ae3ce5c90523f92301",
8898
strip_prefix = "opentracing-cpp-1.5.1",
@@ -104,6 +114,27 @@ REPOSITORY_LOCATIONS = dict(
104114
strip_prefix = "dd-opentracing-cpp-0.4.1",
105115
urls = ["https://github.com/DataDog/dd-opentracing-cpp/archive/v0.4.1.tar.gz"],
106116
),
117+
com_github_google_benchmark = dict(
118+
# TODO (moderation) change back to tarball method on next benchmark release
119+
sha256 = "0de43b6eaddd356f1d6cd164f73f37faf2f6c96fd684e1f7ea543ce49c1d144e",
120+
strip_prefix = "benchmark-505be96ab23056580a3a2315abba048f4428b04e",
121+
urls = ["https://github.com/google/benchmark/archive/505be96ab23056580a3a2315abba048f4428b04e.tar.gz"],
122+
),
123+
com_github_libevent_libevent = dict(
124+
sha256 = "316ddb401745ac5d222d7c529ef1eada12f58f6376a66c1118eee803cb70f83d",
125+
strip_prefix = "libevent-release-2.1.8-stable",
126+
urls = ["https://github.com/libevent/libevent/archive/release-2.1.8-stable.tar.gz"],
127+
),
128+
com_github_madler_zlib = dict(
129+
sha256 = "629380c90a77b964d896ed37163f5c3a34f6e6d897311f1df2a7016355c45eff",
130+
strip_prefix = "zlib-1.2.11",
131+
urls = ["https://github.com/madler/zlib/archive/v1.2.11.tar.gz"],
132+
),
133+
com_github_jbeder_yaml_cpp = dict(
134+
sha256 = "53dcffd55f3433b379fcc694f45c54898711c0e29159a7bd02e82a3e0253bac3",
135+
strip_prefix = "yaml-cpp-0f9a586ca1dc29c2ecb8dd715a315b93e3f40f79",
136+
urls = ["https://github.com/jbeder/yaml-cpp/archive/0f9a586ca1dc29c2ecb8dd715a315b93e3f40f79.tar.gz"],
137+
),
107138
com_github_msgpack_msgpack_c = dict(
108139
sha256 = "bda49f996a73d2c6080ff0523e7b535917cd28c8a79c3a5da54fc29332d61d1e",
109140
strip_prefix = "msgpack-c-cpp-3.1.1",
@@ -187,6 +218,11 @@ REPOSITORY_LOCATIONS = dict(
187218
sha256 = "7be7dc01f1e0afdba6c8eb2b43d2fa01c743be1b9273ab1eaf6c233df078d705",
188219
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.16.5/rules_go-0.16.5.tar.gz"],
189220
),
221+
rules_foreign_cc = dict(
222+
sha256 = "78cbd1a8134b2f0ead8e637228d8ac1ac7c0ab3f0fbcd149a85e55330697d9a3",
223+
strip_prefix = "rules_foreign_cc-216ded8acb95d81e312b228dce3c39872c7a7c34",
224+
urls = ["https://github.com/bazelbuild/rules_foreign_cc/archive/216ded8acb95d81e312b228dce3c39872c7a7c34.tar.gz"],
225+
),
190226
six_archive = dict(
191227
sha256 = "105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a",
192228
urls = ["https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz#md5=34eed507548117b2ab523ab14b2f8b55"],

bazel/target_recipes.bzl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22
# target in //ci/prebuilt/BUILD to the underlying build recipe in
33
# ci/build_container/build_recipes.
44
TARGET_RECIPES = {
5-
"ares": "cares",
6-
"benchmark": "benchmark",
7-
"event": "libevent",
85
"tcmalloc_and_profiler": "gperftools",
96
"tcmalloc_debug": "gperftools",
107
"luajit": "luajit",
11-
"nghttp2": "nghttp2",
12-
"yaml_cpp": "yaml-cpp",
13-
"zlib": "zlib",
148
}

ci/WORKSPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ envoy_dependencies(
1313
path = "@envoy//ci/prebuilt",
1414
)
1515

16+
# TODO(htuch): Roll this into envoy_dependencies()
17+
load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")
18+
19+
rules_foreign_cc_dependencies()
20+
1621
cc_configure()
1722

1823
load("@envoy_api//bazel:repositories.bzl", "api_dependencies")

ci/WORKSPACE.filter.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ load("//bazel:cc_configure.bzl", "cc_configure")
1111
envoy_dependencies(
1212
path = "@envoy//ci/prebuilt",
1313
)
14+
# TODO(htuch): Roll this into envoy_dependencies()
15+
load("@rules_foreign_cc//:workspace_definitions.bzl", "rules_foreign_cc_dependencies")
16+
rules_foreign_cc_dependencies()
1417

1518
cc_configure()
1619

0 commit comments

Comments
 (0)