Skip to content

Commit 3212034

Browse files
authored
compdb add the compdb support to the proxy_wasm_cpp_host (#419)
* compdb add the compdb support to the proxy_wasm_cpp_host Signed-off-by: wangbaiping <[email protected]>
1 parent 0699c05 commit 3212034

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/bazel-*
2+
/external
3+
/compile_commands.json
4+
/.cache/

CONTRIBUTING.md

+4
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ information on using pull requests.
2626

2727
This project follows [Google's Open Source Community
2828
Guidelines](https://opensource.google/conduct/).
29+
30+
## Development
31+
32+
See the [Development Guidelines](DEVELOPMENT.md).

DEVELOPMENT.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Development guidelines
2+
3+
## Generate compilation database
4+
5+
[JSON Compilation Database](https://clang.llvm.org/docs/JSONCompilationDatabase.html) files can be used by [clangd](https://clangd.llvm.org/) or similar tools to add source code cross-references and code completion functionality to editors.
6+
7+
The following command can be used to generate the `compile_commands.json` file:
8+
9+
```
10+
BAZEL_BUILD_OPTION_LIST="--define=engine=multi" ./tools/gen_compilation_database.py --include_all //test/... //:lib
11+
```

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
# WebAssembly for Proxies (C++ host implementation)
2+
3+
## How to Contribute
4+
5+
See [CONTRIBUTING](CONTRIBUTING.md).

bazel/repositories.bzl

+9
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ def proxy_wasm_cpp_host_repositories():
135135
urls = ["https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/archive/95bb82ce45c41d9100fd1ec15d2ffc67f7f3ceee.tar.gz"],
136136
)
137137

138+
# Compile DB dependencies.
139+
maybe(
140+
http_archive,
141+
name = "bazel_compdb",
142+
sha256 = "acd2a9eaf49272bb1480c67d99b82662f005b596a8c11739046a4220ec73c4da",
143+
strip_prefix = "bazel-compilation-database-40864791135333e1446a04553b63cbe744d358d0",
144+
url = "https://github.com/grailbio/bazel-compilation-database/archive/40864791135333e1446a04553b63cbe744d358d0.tar.gz",
145+
)
146+
138147
# Test dependencies.
139148

140149
maybe(

tools/gen_compilation_database.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2016-2019 Envoy Project Authors
4+
# Copyright 2020 Google LLC
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
import argparse
19+
import json
20+
import os
21+
import shlex
22+
import subprocess
23+
from pathlib import Path
24+
25+
# This is copied from https://github.com/envoyproxy/envoy and remove unnecessary code.
26+
27+
# This method is equivalent to https://github.com/grailbio/bazel-compilation-database/blob/master/generate.py
28+
def generate_compilation_database(args):
29+
# We need to download all remote outputs for generated source code. This option lives here to override those
30+
# specified in bazelrc.
31+
bazel_startup_options = shlex.split(os.environ.get("BAZEL_STARTUP_OPTION_LIST", ""))
32+
bazel_options = shlex.split(os.environ.get("BAZEL_BUILD_OPTION_LIST", "")) + [
33+
"--remote_download_outputs=all",
34+
]
35+
36+
source_dir_targets = args.bazel_targets
37+
38+
subprocess.check_call(["bazel", *bazel_startup_options, "build"] + bazel_options + [
39+
"--aspects=@bazel_compdb//:aspects.bzl%compilation_database_aspect",
40+
"--output_groups=compdb_files,header_files"
41+
] + source_dir_targets)
42+
43+
execroot = subprocess.check_output(
44+
["bazel", *bazel_startup_options, "info", *bazel_options,
45+
"execution_root"]).decode().strip()
46+
47+
db_entries = []
48+
for db in Path(execroot).glob('**/*.compile_commands.json'):
49+
db_entries.extend(json.loads(db.read_text()))
50+
51+
def replace_execroot_marker(db_entry):
52+
if 'directory' in db_entry and db_entry['directory'] == '__EXEC_ROOT__':
53+
db_entry['directory'] = execroot
54+
if 'command' in db_entry:
55+
db_entry['command'] = (
56+
db_entry['command'].replace('-isysroot __BAZEL_XCODE_SDKROOT__', ''))
57+
return db_entry
58+
59+
return list(map(replace_execroot_marker, db_entries))
60+
61+
if __name__ == "__main__":
62+
parser = argparse.ArgumentParser(description='Generate JSON compilation database')
63+
parser.add_argument('--include_external', action='store_true')
64+
parser.add_argument('--include_genfiles', action='store_true')
65+
parser.add_argument('--include_headers', action='store_true')
66+
parser.add_argument('--include_all', action='store_true')
67+
parser.add_argument(
68+
'--system-clang',
69+
action='store_true',
70+
help=
71+
'Use `clang++` instead of the bazel wrapper for commands. This may help if `clangd` cannot find/run the tools.'
72+
)
73+
parser.add_argument('bazel_targets', nargs='*', default=[])
74+
75+
args = parser.parse_args()
76+
db = generate_compilation_database(args)
77+
78+
with open("compile_commands.json", "w") as db_file:
79+
json.dump(db, db_file, indent=2)

0 commit comments

Comments
 (0)