Skip to content

cleanup/enhancement: remove unnecessary proto from the sdk and add compdb suport #184

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/bazel-*
.cache
compile_commands.json
42 changes: 1 addition & 41 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library")

licenses(["notice"]) # Apache 2

@@ -45,69 +44,30 @@ cc_library(
visibility = ["//visibility:public"],
)

cc_proto_library(
name = "proxy_wasm_intrinsics_cc_proto",
deps = [":proxy_wasm_intrinsics_proto"],
)

proto_library(
name = "proxy_wasm_intrinsics_proto",
srcs = ["proxy_wasm_intrinsics.proto"],
deps = [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:struct_proto",
],
)

# include lite protobuf support
cc_library(
name = "proxy_wasm_intrinsics_lite",
hdrs = ["proxy_wasm_intrinsics_lite.h"],
copts = ["-std=c++17"],
defines = ["PROXY_WASM_PROTOBUF_LITE"],
visibility = ["//visibility:public"],
deps = [
":proxy_wasm_intrinsics",
":proxy_wasm_intrinsics_lite_cc_proto",
"@com_google_protobuf//:protobuf_lite",
],
)

# include full protobuf support
cc_library(
name = "proxy_wasm_intrinsics_full",
hdrs = ["proxy_wasm_intrinsics_full.h"],
copts = ["-std=c++17"],
defines = ["PROXY_WASM_PROTOBUF_FULL"],
visibility = ["//visibility:public"],
deps = [
":proxy_wasm_intrinsics",
":proxy_wasm_intrinsics_cc_proto",
"@com_google_protobuf//:protobuf",
],
)

cc_proto_library(
name = "proxy_wasm_intrinsics_lite_cc_proto",
deps = [":proxy_wasm_intrinsics_lite_proto"],
)

proto_library(
name = "proxy_wasm_intrinsics_lite_proto",
srcs = [
"proxy_wasm_intrinsics_lite.proto",
"struct_lite.proto",
],
deps = [
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:duration_proto",
"@com_google_protobuf//:empty_proto",
"@com_google_protobuf//:struct_proto",
],
)

filegroup(
name = "proxy_wasm_intrinsics_js",
srcs = [
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -26,3 +26,7 @@ information on using pull requests.

This project follows [Google's Open Source Community
Guidelines](https://opensource.google/conduct/).

## Development

See the [Development Guidelines](DEVELOPMENT.md).
11 changes: 11 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Development guidelines

## Generate compilation database

[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.

The following command can be used to generate the `compile_commands.json` file:

```
./tools/gen_compilation_database.py --include_all //example/... //:proxy_wasm_intrinsics_full
```
2 changes: 1 addition & 1 deletion Dockerfile-sdk
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM ubuntu:noble

COPY *.sh /
COPY *.cc *.h *.js *.proto Makefile* /sdk/
COPY *.cc *.h *.js Makefile* /sdk/

RUN ./sdk_container.sh
14 changes: 2 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -7,13 +7,10 @@ endif
PROTOBUF ?= none
ifeq ($(PROTOBUF), full)
PROTO_DEPS := protobuf
PROTO_OPTS := -DPROXY_WASM_PROTOBUF_FULL=1 \
${PROXY_WASM_CPP_SDK}/proxy_wasm_intrinsics.pb.cc
PROTO_OPTS := -DPROXY_WASM_PROTOBUF_FULL=1
else ifeq ($(PROTOBUF), lite)
PROTO_DEPS := protobuf-lite
PROTO_OPTS := -DPROXY_WASM_PROTOBUF_LITE=1 \
${PROXY_WASM_CPP_SDK}/proxy_wasm_intrinsics_lite.pb.cc \
${PROXY_WASM_CPP_SDK}/struct_lite.pb.cc
PROTO_OPTS := -DPROXY_WASM_PROTOBUF_LITE=1
else
PROTO_DEPS :=
PROTO_OPTS :=
@@ -54,10 +51,3 @@ debug-deps:

clean:
rm *.wasm

# NOTE: How to regenerate .pb.h and .pb.cc files for a protobuf update:
# - download + extract protobuf release (currently v26.1)
# - regenerate:
# ./bin/protoc --cpp_out=../ -I../ -Iinclude/google/protobuf/ ../struct_lite.proto
# ./bin/protoc --cpp_out=../ -I../ -Iinclude/google/protobuf/ ../proxy_wasm_intrinsics_lite.proto
# ./bin/protoc --cpp_out=../ -I../ -Iinclude/google/protobuf/ ../proxy_wasm_intrinsics.proto
9 changes: 9 additions & 0 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
@@ -56,3 +56,12 @@ def proxy_wasm_cpp_sdk_repositories():
strip_prefix = "re2-2023-07-01",
url = "https://github.com/google/re2/archive/2023-07-01.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",
)
39 changes: 35 additions & 4 deletions example/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
load("@proxy_wasm_cpp_sdk//bazel:defs.bzl", "proxy_wasm_cc_binary")
load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

licenses(["notice"]) # Apache 2

@@ -21,16 +23,45 @@ proxy_wasm_cc_binary(
copts = ["-std=c++17"],
)

proto_library(
name = "example_proto",
srcs = ["example.proto"],
deps = ["@com_google_protobuf//:struct_proto"],
)

cc_proto_library(
name = "example_proto_cc",
deps = [":example_proto"],
)

cc_library(
name = "http_proto_example_lite_lib",
srcs = ["http_proto_example.cc"],
copts = ["-std=c++17"],
deps = [
":example_proto_cc",
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_lite",
],
alwayslink = 1,
)

proxy_wasm_cc_binary(
name = "http_proto_example_lite.wasm",
deps = [":http_proto_example_lite_lib"],
)

cc_library(
name = "http_proto_example_full_lib",
srcs = ["http_proto_example.cc"],
copts = ["-std=c++17"],
protobuf = "lite",
deps = [
":example_proto_cc",
"@proxy_wasm_cpp_sdk//:proxy_wasm_intrinsics_full",
],
alwayslink = 1,
)

proxy_wasm_cc_binary(
name = "http_proto_example_full.wasm",
srcs = ["http_proto_example.cc"],
copts = ["-std=c++17"],
protobuf = "full",
deps = [":http_proto_example_full_lib"],
)
23 changes: 23 additions & 0 deletions example/example.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2024 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.

syntax = "proto3";

package example;

import "google/protobuf/struct.proto";

message Example {
google.protobuf.Struct data = 1;
}
10 changes: 10 additions & 0 deletions example/http_proto_example.cc
Original file line number Diff line number Diff line change
@@ -14,6 +14,10 @@

#include "proxy_wasm_intrinsics.h"

#ifdef PROXY_WASM_PROTOBUF
#include "example/example.pb.h"
#endif

class MyRootContext : public RootContext {
public:
explicit MyRootContext(uint32_t id, std::string_view root_id) : RootContext(id, root_id) {}
@@ -23,10 +27,16 @@ class MyRootContext : public RootContext {
LOG_TRACE("onStart with protobuf (full)");
google::protobuf::Value value;
value.set_string_value("unused");

example::Example example_proto;
(*example_proto.mutable_data()->mutable_fields())["key"] = value;
#elif defined(PROXY_WASM_PROTOBUF_LITE)
LOG_TRACE("onStart with protobuf (lite)");
google::protobuf::Value value;
value.set_string_value("unused");

example::Example example_proto;
(*example_proto.mutable_data()->mutable_fields())["key"] = value;
#else
LOG_TRACE("onStart without protobuf");
#endif
4 changes: 4 additions & 0 deletions proxy_wasm_api.h
Original file line number Diff line number Diff line change
@@ -35,6 +35,10 @@
#include <utility>
#include <vector>

#include "proxy_wasm_common.h"
#include "proxy_wasm_enums.h"
#include "proxy_wasm_externs.h"

// Macro to log a message and abort the plugin if the given value is not
// `WasmResult::Ok`.
#define CHECK_RESULT(_c) \
2 changes: 0 additions & 2 deletions proxy_wasm_intrinsics.h
Original file line number Diff line number Diff line change
@@ -38,10 +38,8 @@
#include "proxy_wasm_externs.h"
#ifdef PROXY_WASM_PROTOBUF_FULL
#define PROXY_WASM_PROTOBUF 1
#include "proxy_wasm_intrinsics.pb.h"
#endif
#ifdef PROXY_WASM_PROTOBUF_LITE
#define PROXY_WASM_PROTOBUF 1
#include "proxy_wasm_intrinsics_lite.pb.h"
#endif
#include "proxy_wasm_api.h"
4,899 changes: 0 additions & 4,899 deletions proxy_wasm_intrinsics.pb.cc

This file was deleted.

6,398 changes: 0 additions & 6,398 deletions proxy_wasm_intrinsics.pb.h

This file was deleted.

109 changes: 0 additions & 109 deletions proxy_wasm_intrinsics.proto

This file was deleted.

25 changes: 0 additions & 25 deletions proxy_wasm_intrinsics_full.h

This file was deleted.

25 changes: 0 additions & 25 deletions proxy_wasm_intrinsics_lite.h

This file was deleted.

5,137 changes: 0 additions & 5,137 deletions proxy_wasm_intrinsics_lite.pb.cc

This file was deleted.

6,520 changes: 0 additions & 6,520 deletions proxy_wasm_intrinsics_lite.pb.h

This file was deleted.

113 changes: 0 additions & 113 deletions proxy_wasm_intrinsics_lite.proto

This file was deleted.

999 changes: 0 additions & 999 deletions struct_lite.pb.cc

This file was deleted.

1,157 changes: 0 additions & 1,157 deletions struct_lite.pb.h

This file was deleted.

74 changes: 0 additions & 74 deletions struct_lite.proto

This file was deleted.

79 changes: 79 additions & 0 deletions tools/gen_compilation_database.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)