Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Qining committed Jun 1, 2018
1 parent 17b5ac2 commit 7df78e1
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 18 deletions.
23 changes: 23 additions & 0 deletions core/log/log_pb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")

go_library(
name = "go_default_library",
Expand Down Expand Up @@ -51,3 +52,25 @@ java_proto_library(
visibility = ["//visibility:public"],
deps = [":log_pb_proto"],
)

cc_proto_library(
name = "log_pb_cc_proto",
visibility = ["//visibility:public"],
deps = [":log_pb_proto"],
)


# cc_grpc_library(
# name = "log_pb_cc_proto",
# srcs = ["log.proto"],
# generate_mocks = False,
# proto_only = True,
# use_external = True,
# visibility = ["//visibility:public"],
# well_known_protos = [],
# # deps = [],
# deps = [
# "//core/data/pod:pod_proto",
# ],
# )

40 changes: 31 additions & 9 deletions gapir/replay_service/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
# load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
load("@gapid//tools/build/rules:grpc_c++.bzl", "cc_grpc_library")

go_proto_library(
name = "service_go_proto",
Expand All @@ -24,20 +25,41 @@ go_proto_library(
visibility = ["//visibility:public"],
)

# cc_grpc_library(
# name = "log_proto_cc",
# srcs = ["//core/log/log_pb:log.proto"],
# generate_mocks = False,
# proto_only = True,
# use_external = True,
# visibility = ["//visibility:public"],
# well_known_protos = [],
# deps = [],
# )

# cc_grpc_library(
# name = "service_cc_proto",
# srcs = [":replay_service_proto"],
# generate_mocks = False,
# proto_only = False,
# use_external = True,
# visibility = ["//visibility:public"],
# well_known_protos = True,
# deps = [],
# # deps = ["//core/log/log_pb:log_pb_cc_proto"],
# )

cc_grpc_library(
name = "service_cc_proto",
srcs = ["service.proto"],
generate_mocks = False,
proto_only = False,
use_external = True,
visibility = ["//visibility:public"],
well_known_protos = [],
deps = [],
name = "service_cc_proto",
proto = "replay_service_proto",
deps = ["//core/log/log_pb:log_pb_cc_proto"],
proto_only = False,
visibility = ["//visibility:public"]
)

proto_library(
name = "replay_service_proto",
srcs = ["service.proto"],
deps = ["//core/log/log_pb:log_pb_proto"],
visibility = ["//visibility:public"],
)

Expand Down
20 changes: 11 additions & 9 deletions gapir/replay_service/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

syntax = "proto3";

import "core/log/log_pb/log.proto";

package replay_service;

option go_package = "github.com/google/gapid/gapir/replay_service";
Expand Down Expand Up @@ -89,15 +91,15 @@ message Notification {
// Serverity defines the severity of a notification message.
// The values are identical to values in core/log/serverity.go and
// gapis/service/service.proto
enum Severity {
Verbose = 0;
Debug = 1;
Info = 2;
Warning = 3;
Error = 4;
Fatal = 5;
}
Severity severity = 2;
// enum Severity {
// Verbose = 0;
// Debug = 1;
// Info = 2;
// Warning = 3;
// Error = 4;
// Fatal = 5;
// }
log.Severity severity = 2;
// API index should be in uint8 type. Extend to uint32 as protobuf does not
// support uint8.
uint32 api_index = 3;
Expand Down
161 changes: 161 additions & 0 deletions tools/build/rules/grpc_c++.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,167 @@ def grpc_cc_library(name, srcs = [], public_hdrs = [], hdrs = [],
alwayslink = alwayslink,
)


"""Generates C++ grpc stubs from proto_library rules.
This is an internal rule used by cc_grpc_library, and shouldn't be used
directly.
"""

def generate_cc_impl(ctx):
"""Implementation of the generate_cc rule."""
protos = [f for src in ctx.attr.srcs for f in src.proto.direct_sources]
# protos = [f for src in ctx.attr.srcs for f in src.proto.transitive_sources]
includes = [f for src in ctx.attr.srcs for f in src.proto.transitive_imports]
outs = []
# label_len is length of the path from WORKSPACE root to the location of this build file
label_len = 0
# proto_root is the directory relative to which generated include paths should be
proto_root = ""
if ctx.label.package:
# The +1 is for the trailing slash.
label_len += len(ctx.label.package) + 1
if ctx.label.workspace_root:
label_len += len(ctx.label.workspace_root) + 1
proto_root = "/" + ctx.label.workspace_root

if ctx.executable.plugin:
outs += [proto.path[label_len:-len(".proto")] + ".grpc.pb.h" for proto in protos]
outs += [proto.path[label_len:-len(".proto")] + ".grpc.pb.cc" for proto in protos]
if ctx.attr.generate_mocks:
outs += [proto.path[label_len:-len(".proto")] + "_mock.grpc.pb.h" for proto in protos]
else:
outs += [proto.path[label_len:-len(".proto")] + ".pb.h" for proto in protos]
outs += [proto.path[label_len:-len(".proto")] + ".pb.cc" for proto in protos]
out_files = [ctx.new_file(out) for out in outs]
dir_out = str(ctx.genfiles_dir.path + proto_root)

arguments = []
if ctx.executable.plugin:
arguments += ["--plugin=protoc-gen-PLUGIN=" + ctx.executable.plugin.path]
flags = list(ctx.attr.flags)
if ctx.attr.generate_mocks:
flags.append("generate_mock_code=true")
arguments += ["--PLUGIN_out=" + ",".join(flags) + ":" + dir_out]
additional_input = [ctx.executable.plugin]
else:
arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out]
additional_input = []

# Import protos relative to their workspace root so that protoc prints the
# right include paths.
for include in includes:
directory = include.path
if directory.startswith("external"):
external_sep = directory.find("/")
repository_sep = directory.find("/", external_sep + 1)
arguments += ["--proto_path=" + directory[:repository_sep]]
else:
arguments += ["--proto_path=."]
# Include the output directory so that protoc puts the generated code in the
# right directory.
arguments += ["--proto_path={0}{1}".format(dir_out, proto_root)]
arguments += [proto.path for proto in protos]

# create a list of well known proto files if the argument is non-None
well_known_proto_files = []
if ctx.attr.well_known_protos:
f = ctx.attr.well_known_protos.files.to_list()[0].dirname
if f != "external/com_google_protobuf/src/google/protobuf":
print("Error: Only @com_google_protobuf//:well_known_protos is supported")
else:
# f points to "external/com_google_protobuf/src/google/protobuf"
# add -I argument to protoc so it knows where to look for the proto files.
arguments += ["-I{0}".format(f + "/../..")]
well_known_proto_files = [f for f in ctx.attr.well_known_protos.files]

ctx.action(
inputs = protos + includes + additional_input + well_known_proto_files,
outputs = out_files,
executable = ctx.executable._protoc,
arguments = arguments,
)

return struct(files=depset(out_files))

_generate_cc = rule(
attrs = {
"srcs": attr.label_list(
mandatory = True,
non_empty = True,
providers = ["proto"],
),
"plugin": attr.label(
executable = True,
providers = ["files_to_run"],
cfg = "host",
),
"flags": attr.string_list(
mandatory = False,
allow_empty = True,
),
"well_known_protos" : attr.label(
mandatory = False,
),
"generate_mocks" : attr.bool(
default = False,
mandatory = False,
),
"_protoc": attr.label(
default = Label("//external:protocol_compiler"),
executable = True,
cfg = "host",
),
},
# We generate .h files, so we need to output to genfiles.
output_to_genfiles = True,
implementation = generate_cc_impl,
)

def generate_cc(well_known_protos, **kwargs):
if well_known_protos:
_generate_cc(well_known_protos="@com_google_protobuf//:well_known_protos", **kwargs)
else:
_generate_cc(**kwargs)

def cc_grpc_library(name, proto, deps, proto_only, **kwargs):
codegen_target = "_" + name + "_codegen"
codegen_grpc_target = "_" + name + "_grpc_codegen"

generate_cc(
name = codegen_target,
srcs = [proto],
well_known_protos = True,
**kwargs
)

if not proto_only:
plugin = "//external:grpc_cpp_plugin"
generate_cc(
name = codegen_grpc_target,
srcs = [proto],
plugin = plugin,
well_known_protos = True,
**kwargs
)
grpc_deps = ["//external:grpc++_codegen_proto",
"//external:protobuf"]
native.cc_library(
name = name,
srcs = [":" + codegen_grpc_target, ":" + codegen_target],
hdrs = [":" + codegen_grpc_target, ":" + codegen_target],
deps = deps + grpc_deps,
**kwargs
)
else:
native.cc_library(
name = name,
srcs = [":" + codegen_target],
hdrs = [":" + codegen_target],
deps = deps + ["//external:protobuf"],
**kwargs
)

"""Load dependencies needed to compile and test the grpc library as a 3rd-party consumer."""

def grpc_deps():
Expand Down

0 comments on commit 7df78e1

Please sign in to comment.