Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6aab191

Browse files
committedFeb 27, 2025·
add compile db support
Signed-off-by: wangbaiping(wbpcode) <[email protected]>
1 parent bcb7dc5 commit 6aab191

9 files changed

+174
-9
lines changed
 

‎.gitignore

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

‎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+
./tools/gen_compilation_database.py --include_all //example/... //:proxy_wasm_intrinsics_full
11+
```

‎bazel/repositories.bzl

+9
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ def proxy_wasm_cpp_sdk_repositories():
5656
strip_prefix = "re2-2023-07-01",
5757
url = "https://github.com/google/re2/archive/2023-07-01.tar.gz",
5858
)
59+
60+
# Compile DB dependencies.
61+
maybe(
62+
http_archive,
63+
name = "bazel_compdb",
64+
sha256 = "acd2a9eaf49272bb1480c67d99b82662f005b596a8c11739046a4220ec73c4da",
65+
strip_prefix = "bazel-compilation-database-40864791135333e1446a04553b63cbe744d358d0",
66+
url = "https://github.com/grailbio/bazel-compilation-database/archive/40864791135333e1446a04553b63cbe744d358d0.tar.gz",
67+
)

‎example/BUILD

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
load("@proxy_wasm_cpp_sdk//bazel:defs.bzl", "proxy_wasm_cc_binary")
2+
load("@rules_cc//cc:defs.bzl", "cc_proto_library")
3+
load("@rules_proto//proto:defs.bzl", "proto_library")
24

35
licenses(["notice"]) # Apache 2
46

@@ -21,22 +23,45 @@ proxy_wasm_cc_binary(
2123
copts = ["-std=c++17"],
2224
)
2325

24-
proxy_wasm_cc_binary(
25-
name = "http_proto_example_lite.wasm",
26+
proto_library(
27+
name = "example_proto",
28+
srcs = ["example.proto"],
29+
deps = ["@com_google_protobuf//:struct_proto"],
30+
)
31+
32+
cc_proto_library(
33+
name = "example_proto_cc",
34+
deps = [":example_proto"],
35+
)
36+
37+
cc_library(
38+
name = "http_proto_example_lite_lib",
2639
srcs = ["http_proto_example.cc"],
2740
copts = ["-std=c++17"],
28-
protobuf = "lite",
2941
deps = [
30-
"@com_google_protobuf//:struct_cc_proto",
42+
":example_proto_cc",
43+
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_lite",
3144
],
45+
alwayslink = 1,
3246
)
3347

3448
proxy_wasm_cc_binary(
35-
name = "http_proto_example_full.wasm",
49+
name = "http_proto_example_lite.wasm",
50+
deps = [":http_proto_example_lite_lib"],
51+
)
52+
53+
cc_library(
54+
name = "http_proto_example_full_lib",
3655
srcs = ["http_proto_example.cc"],
3756
copts = ["-std=c++17"],
38-
protobuf = "full",
3957
deps = [
40-
"@com_google_protobuf//:struct_cc_proto",
58+
":example_proto_cc",
59+
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_full",
4160
],
61+
alwayslink = 1,
62+
)
63+
64+
proxy_wasm_cc_binary(
65+
name = "http_proto_example_full.wasm",
66+
deps = [":http_proto_example_full_lib"],
4267
)

‎example/example.proto

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
package example;
18+
19+
import "google/protobuf/struct.proto";
20+
21+
message Example {
22+
google.protobuf.Struct data = 1;
23+
}

‎example/http_proto_example.cc

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "google/protobuf/struct.pb.h"
16-
1715
#include "proxy_wasm_intrinsics.h"
1816

17+
#ifdef PROXY_WASM_PROTOBUF
18+
#include "example/example.pb.h"
19+
#endif
20+
1921
class MyRootContext : public RootContext {
2022
public:
2123
explicit MyRootContext(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {}
@@ -25,10 +27,16 @@ class MyRootContext : public RootContext {
2527
LOG_TRACE("onStart with protobuf (full)");
2628
google::protobuf::Value value;
2729
value.set_string_value("unused");
30+
31+
example::Example example_proto;
32+
(*example_proto.mutable_data()->mutable_fields())["key"] = value;
2833
#elif defined(PROXY_WASM_PROTOBUF_LITE)
2934
LOG_TRACE("onStart with protobuf (lite)");
3035
google::protobuf::Value value;
3136
value.set_string_value("unused");
37+
38+
example::Example example_proto;
39+
(*example_proto.mutable_data()->mutable_fields())["key"] = value;
3240
#else
3341
LOG_TRACE("onStart without protobuf");
3442
#endif

‎proxy_wasm_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include <utility>
3636
#include <vector>
3737

38+
#include "proxy_wasm_common.h"
39+
#include "proxy_wasm_enums.h"
40+
#include "proxy_wasm_externs.h"
41+
3842
// Macro to log a message and abort the plugin if the given value is not
3943
// `WasmResult::Ok`.
4044
#define CHECK_RESULT(_c) \

‎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)
Please sign in to comment.