Skip to content
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

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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

Expand Down Expand Up @@ -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 = [
Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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 :=
Expand Down Expand Up @@ -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
Expand Up @@ -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

Expand All @@ -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
Expand Up @@ -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) {}
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions proxy_wasm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
2 changes: 0 additions & 2 deletions proxy_wasm_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading