diff --git a/bazel/cc_grpc_library.bzl b/bazel/cc_grpc_library.bzl index c91bc66ff4f..572af756176 100644 --- a/bazel/cc_grpc_library.bzl +++ b/bazel/cc_grpc_library.bzl @@ -34,17 +34,17 @@ def cc_grpc_library( provides the compiled code of any message that the services depend on. proto_only (bool): If True, create only C++ proto classes library, avoid creating C++ grpc classes library (expect it in deps). - Deprecated, use native cc_proto_library instead. + Deprecated, use native cc_proto_library instead. False by default. well_known_protos (bool): Should this library additionally depend on - well known protos. Deprecated, pass well_known_protos - explicitly (proto_library targets in srcs and corresponding - cc_proto_library in deps). + well known protos. Deprecated, the well known protos should be + specified as explicit dependencies of the proto_library target + (passed in srcs parameter) instead. False by default. generate_mocks (bool): when True, Google Mock code for client stub is - generated. + generated. False by default. use_external (bool): Not used. grpc_only (bool): if True, generate only grpc library, expecting protobuf messages library (cc_proto_library target) to be passed as - deps. + deps. False by default (will become True by default eventually). **kwargs: rest of arguments, e.g., compatible_with and visibility """ if len(srcs) > 1: @@ -53,7 +53,7 @@ def cc_grpc_library( fail("A mutualy exclusive configuration is specified: grpc_only = True and proto_only = True") extra_deps = [] - proto_target = None + proto_targets = [] if not grpc_only: proto_target = "_" + name + "_only" @@ -77,16 +77,17 @@ def cc_grpc_library( **kwargs ) extra_deps.append(":" + cc_proto_target) + proto_targets.append(proto_target) else: if not srcs: fail("srcs cannot be empty", "srcs") - proto_target = srcs[0] + proto_targets += srcs if not proto_only: codegen_grpc_target = "_" + name + "_grpc_codegen" generate_cc( name = codegen_grpc_target, - srcs = [proto_target], + srcs = proto_targets, plugin = "@com_github_grpc_grpc//:grpc_cpp_plugin", well_known_protos = well_known_protos, generate_mocks = generate_mocks, diff --git a/bazel/generate_cc.bzl b/bazel/generate_cc.bzl index 82f5cbad310..29a888f608f 100644 --- a/bazel/generate_cc.bzl +++ b/bazel/generate_cc.bzl @@ -18,12 +18,22 @@ _GRPC_PROTO_MOCK_HEADER_FMT = "{}_mock.grpc.pb.h" _PROTO_HEADER_FMT = "{}.pb.h" _PROTO_SRC_FMT = "{}.pb.cc" -def _strip_package_from_path(label_package, path): +def _strip_package_from_path(label_package, file): + prefix_len = 0 + if not file.is_source and file.path.startswith(file.root.path): + prefix_len = len(file.root.path) + 1 + + path = file.path if len(label_package) == 0: return path - if not path.startswith(label_package + "/"): + if not path.startswith(label_package + "/", prefix_len): fail("'{}' does not lie within '{}'.".format(path, label_package)) - return path[len(label_package + "/"):] + return path[prefix_len + len(label_package + "/"):] + +def _get_srcs_file_path(file): + if not file.is_source and file.path.startswith(file.root.path): + return file.path[len(file.root.path) + 1:] + return file.path def _join_directories(directories): massaged_directories = [directory for directory in directories if len(directory) != 0] @@ -31,7 +41,7 @@ def _join_directories(directories): 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.check_deps_sources] includes = [ f for src in ctx.attr.srcs @@ -46,14 +56,14 @@ def generate_cc_impl(ctx): if ctx.executable.plugin: outs += [ proto_path_to_generated_filename( - _strip_package_from_path(label_package, proto.path), + _strip_package_from_path(label_package, proto), _GRPC_PROTO_HEADER_FMT, ) for proto in protos ] outs += [ proto_path_to_generated_filename( - _strip_package_from_path(label_package, proto.path), + _strip_package_from_path(label_package, proto), _GRPC_PROTO_SRC_FMT, ) for proto in protos @@ -61,7 +71,7 @@ def generate_cc_impl(ctx): if ctx.attr.generate_mocks: outs += [ proto_path_to_generated_filename( - _strip_package_from_path(label_package, proto.path), + _strip_package_from_path(label_package, proto), _GRPC_PROTO_MOCK_HEADER_FMT, ) for proto in protos @@ -69,14 +79,14 @@ def generate_cc_impl(ctx): else: outs += [ proto_path_to_generated_filename( - _strip_package_from_path(label_package, proto.path), + _strip_package_from_path(label_package, proto), _PROTO_HEADER_FMT, ) for proto in protos ] outs += [ proto_path_to_generated_filename( - _strip_package_from_path(label_package, proto.path), + _strip_package_from_path(label_package, proto), _PROTO_SRC_FMT, ) for proto in protos @@ -102,7 +112,7 @@ def generate_cc_impl(ctx): # 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] + arguments += [_get_srcs_file_path(proto) for proto in protos] # create a list of well known proto files if the argument is non-None well_known_proto_files = [] diff --git a/bazel/protobuf.bzl b/bazel/protobuf.bzl index 55124195d8f..f2df7bd87b2 100644 --- a/bazel/protobuf.bzl +++ b/bazel/protobuf.bzl @@ -58,12 +58,16 @@ def proto_path_to_generated_filename(proto_path, fmt_str): def _get_include_directory(include): directory = include.path - if directory.startswith("external"): - external_separator = directory.find("/") + prefix_len = 0 + if not include.is_source and directory.startswith(include.root.path): + prefix_len = len(include.root.path) + 1 + + if directory.startswith("external", prefix_len): + external_separator = directory.find("/", prefix_len) repository_separator = directory.find("/", external_separator + 1) return directory[:repository_separator] else: - return "." + return include.root.path if include.root.path else "." def get_include_protoc_args(includes): """Returns protoc args that imports protos relative to their import root. diff --git a/src/proto/grpc/testing/BUILD b/src/proto/grpc/testing/BUILD index 16e47d81061..727c99cf99c 100644 --- a/src/proto/grpc/testing/BUILD +++ b/src/proto/grpc/testing/BUILD @@ -146,6 +146,37 @@ grpc_proto_library( ], ) +# Test that grpc_proto_library/cc_grpc_library can consume generated files +genrule( + name = "messages_gen_proto_file", + srcs = ["messages.proto"], + outs = ["messages_gen.proto"], + cmd = "cp $< $@", +) + +grpc_proto_library( + name = "messages_gen_proto", + srcs = ["messages_gen_proto_file"], + has_services = False, +) + +genrule( + name = "test_gen_proto_file", + srcs = ["test.proto"], + outs = ["test_gen.proto"], + cmd = "sed 's/messages.proto/messages_gen.proto/' $< > $@", +) + +# Consume generated files in srcs and in deps +grpc_proto_library( + name = "test_gen_proto", + srcs = ["test_gen_proto_file"], + deps = [ + "empty_proto", + "messages_gen_proto", + ], +) + proto_library( name = "test_proto_descriptor", srcs = ["test.proto"],