Skip to content

compdb add the compdb support to the proxy_wasm_cpp_host #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/bazel-*
/external
/compile_commands.json
/.cache/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# WebAssembly for Proxies (C++ host implementation)

## Development

You could use the following commands to generate the `compile_commands.json` file:

```
BAZEL_BUILD_OPTION_LIST="--define=engine=multi" ./tools/gen_compdb.py --include_all //test/... //:lib
```
9 changes: 9 additions & 0 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ def proxy_wasm_cpp_host_repositories():
urls = ["https://github.com/proxy-wasm/proxy-wasm-cpp-sdk/archive/95bb82ce45c41d9100fd1ec15d2ffc67f7f3ceee.tar.gz"],
)

# Compile DB dependencies.
maybe(
http_archive,
name = "bazel_compdb",
sha256 = "acd2a9eaf49272bb1480c67d99b82662f005b596a8c11739046a4220ec73c4da",
strip_prefix = "bazel-compilation-database-40864791135333e1446a04553b63cbe744d358d0",
url = "https://github.com/grailbio/bazel-compilation-database/archive/40864791135333e1446a04553b63cbe744d358d0.tar.gz",
)

# Test dependencies.

maybe(
Expand Down
79 changes: 79 additions & 0 deletions tools/gen_compdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3

# Copyright 2016-2019 Envoy Project Authors
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import json
import os
import shlex
import subprocess
from pathlib import Path

# This is copied from https://github.com/envoyproxy/envoy and remove unnecessary code.

# This method is equivalent to https://github.com/grailbio/bazel-compilation-database/blob/master/generate.py
def generate_compilation_database(args):
# We need to download all remote outputs for generated source code. This option lives here to override those
# specified in bazelrc.
bazel_startup_options = shlex.split(os.environ.get("BAZEL_STARTUP_OPTION_LIST", ""))
bazel_options = shlex.split(os.environ.get("BAZEL_BUILD_OPTION_LIST", "")) + [
"--remote_download_outputs=all",
]

source_dir_targets = args.bazel_targets

subprocess.check_call(["bazel", *bazel_startup_options, "build"] + bazel_options + [
"--aspects=@bazel_compdb//:aspects.bzl%compilation_database_aspect",
"--output_groups=compdb_files,header_files"
] + source_dir_targets)

execroot = subprocess.check_output(
["bazel", *bazel_startup_options, "info", *bazel_options,
"execution_root"]).decode().strip()

db_entries = []
for db in Path(execroot).glob('**/*.compile_commands.json'):
db_entries.extend(json.loads(db.read_text()))

def replace_execroot_marker(db_entry):
if 'directory' in db_entry and db_entry['directory'] == '__EXEC_ROOT__':
db_entry['directory'] = execroot
if 'command' in db_entry:
db_entry['command'] = (
db_entry['command'].replace('-isysroot __BAZEL_XCODE_SDKROOT__', ''))
return db_entry

return list(map(replace_execroot_marker, db_entries))

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate JSON compilation database')
parser.add_argument('--include_external', action='store_true')
parser.add_argument('--include_genfiles', action='store_true')
parser.add_argument('--include_headers', action='store_true')
parser.add_argument('--include_all', action='store_true')
parser.add_argument(
'--system-clang',
action='store_true',
help=
'Use `clang++` instead of the bazel wrapper for commands. This may help if `clangd` cannot find/run the tools.'
)
parser.add_argument('bazel_targets', nargs='*', default=[])

args = parser.parse_args()
db = generate_compilation_database(args)

with open("compile_commands.json", "w") as db_file:
json.dump(db, db_file, indent=2)
Loading