Skip to content

Commit fe95470

Browse files
htuchlizan
authored andcommitted
tools: move clang_tools under @envoy_dev (envoyproxy#9249)
Fixes regression in gen_compilation_database introduced in envoyproxy#8597, this causes unpleasant errors if a developer has not configured LLVM_CONFIG. The solution is to move developer tools into an `@envoy_api` like local external repository called `@envoy_dev`. Risk level: Low Testing: Manual, with LLVM_CONFIG set/unset. Signed-off-by: Harvey Tuch <[email protected]>
1 parent b0df8fb commit fe95470

File tree

5 files changed

+48
-26
lines changed

5 files changed

+48
-26
lines changed

.bazelignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
api
22
examples/grpc-bridge/script
3+
tools/clang_tools

bazel/dev_binding.bzl

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def _default_envoy_dev_impl(ctxt):
2+
if "LLVM_CONFIG" in ctxt.os.environ:
3+
ctxt.file("WORKSPACE", "")
4+
ctxt.file("BUILD.bazel", "")
5+
ctxt.symlink(ctxt.path(ctxt.attr.envoy_root).dirname.get_child("tools").get_child("clang_tools"), "clang_tools")
6+
7+
_default_envoy_dev = repository_rule(
8+
implementation = _default_envoy_dev_impl,
9+
attrs = {
10+
"envoy_root": attr.label(default = "@envoy//:BUILD"),
11+
},
12+
)
13+
14+
def _clang_tools_impl(ctxt):
15+
if "LLVM_CONFIG" in ctxt.os.environ:
16+
llvm_config_path = ctxt.os.environ["LLVM_CONFIG"]
17+
exec_result = ctxt.execute([llvm_config_path, "--includedir"])
18+
if exec_result.return_code != 0:
19+
fail(llvm_config_path + " --includedir returned %d" % exec_result.return_code)
20+
clang_tools_include_path = exec_result.stdout.rstrip()
21+
exec_result = ctxt.execute([llvm_config_path, "--libdir"])
22+
if exec_result.return_code != 0:
23+
fail(llvm_config_path + " --libdir returned %d" % exec_result.return_code)
24+
clang_tools_lib_path = exec_result.stdout.rstrip()
25+
for include_dir in ["clang", "clang-c", "llvm", "llvm-c"]:
26+
ctxt.symlink(clang_tools_include_path + "/" + include_dir, include_dir)
27+
ctxt.symlink(clang_tools_lib_path, "lib")
28+
ctxt.symlink(Label("@envoy_dev//clang_tools/support:BUILD.prebuilt"), "BUILD")
29+
30+
_clang_tools = repository_rule(
31+
implementation = _clang_tools_impl,
32+
environ = ["LLVM_CONFIG"],
33+
)
34+
35+
def envoy_dev_binding():
36+
# Treat the Envoy developer tools that require llvm as an external repo, this avoids
37+
# breaking bazel build //... when llvm is not installed.
38+
if "envoy_dev" not in native.existing_rules().keys():
39+
_default_envoy_dev(name = "envoy_dev")
40+
_clang_tools(name = "clang_tools")

bazel/repositories.bzl

+4-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
load(":dev_binding.bzl", "envoy_dev_binding")
23
load(":genrule_repository.bzl", "genrule_repository")
34
load("@envoy_api//bazel:envoy_http_archive.bzl", "envoy_http_archive")
45
load(":repository_locations.bzl", "REPOSITORY_LOCATIONS")
@@ -89,28 +90,10 @@ def _go_deps(skip_targets):
8990
_repository_impl("io_bazel_rules_go")
9091
_repository_impl("bazel_gazelle")
9192

92-
def _clang_tools_impl(ctxt):
93-
if "LLVM_CONFIG" in ctxt.os.environ:
94-
llvm_config_path = ctxt.os.environ["LLVM_CONFIG"]
95-
exec_result = ctxt.execute([llvm_config_path, "--includedir"])
96-
if exec_result.return_code != 0:
97-
fail(llvm_config_path + " --includedir returned %d" % exec_result.return_code)
98-
clang_tools_include_path = exec_result.stdout.rstrip()
99-
exec_result = ctxt.execute([llvm_config_path, "--libdir"])
100-
if exec_result.return_code != 0:
101-
fail(llvm_config_path + " --libdir returned %d" % exec_result.return_code)
102-
clang_tools_lib_path = exec_result.stdout.rstrip()
103-
for include_dir in ["clang", "clang-c", "llvm", "llvm-c"]:
104-
ctxt.symlink(clang_tools_include_path + "/" + include_dir, include_dir)
105-
ctxt.symlink(clang_tools_lib_path, "lib")
106-
ctxt.symlink(Label("//tools/clang_tools/support:BUILD.prebuilt"), "BUILD")
107-
108-
_clang_tools = repository_rule(
109-
implementation = _clang_tools_impl,
110-
environ = ["LLVM_CONFIG"],
111-
)
112-
11393
def envoy_dependencies(skip_targets = []):
94+
# Setup Envoy developer tools.
95+
envoy_dev_binding()
96+
11497
# Treat Envoy's overall build config as an external repo, so projects that
11598
# build Envoy as a subcomponent can easily override the config.
11699
if "envoy_build_config" not in native.existing_rules().keys():
@@ -130,8 +113,6 @@ def envoy_dependencies(skip_targets = []):
130113
actual = "@envoy//bazel:boringssl",
131114
)
132115

133-
_clang_tools(name = "clang_tools")
134-
135116
# The long repo names (`com_github_fmtlib_fmt` instead of `fmtlib`) are
136117
# semi-standard in the Bazel community, intended to avoid both duplicate
137118
# dependencies and name conflicts.

tools/clang_tools/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Assuming that `CC` and `CXX` already point at Clang, you should be able to build
2626
with:
2727

2828
```console
29-
bazel build //tools/clang_tools/syntax_only
29+
bazel build @envoy_dev//clang_tools/syntax_only
3030
```
3131

3232
To run `libtooling` based tools against Envoy, you will need to first generate a
@@ -42,7 +42,7 @@ tools/gen_compilation_database.py --run_bazel_build --include_headers
4242
Finally, the tool can be run against source files in the Envoy tree:
4343

4444
```console
45-
bazel-bin/tools/clang_tools/syntax_only/syntax_only \
45+
bazel-bin/external/envoy_dev/clang_tools/syntax_only/syntax_only \
4646
source/common/common/logger.cc
4747
```
4848

tools/clang_tools/syntax_only/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//tools/clang_tools/support:clang_tools.bzl", "envoy_clang_tools_cc_binary")
1+
load("//clang_tools/support:clang_tools.bzl", "envoy_clang_tools_cc_binary")
22

33
licenses(["notice"]) # Apache 2
44

0 commit comments

Comments
 (0)