diff options
author | Nicolas Noble <nicolasnoble@users.noreply.github.com> | 2018-03-29 13:56:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-29 13:56:33 -0700 |
commit | 1995816b6b5fc9e03a886af65bd28bc8a5c2161c (patch) | |
tree | be78994f7b7d9a6d20184eb1047ea2e9dd494252 /bazel | |
parent | ff5e380e3dcc67fafda4481b8e970a593483a6a6 (diff) | |
parent | e6dfa9cd74fe7fa187072b362719d2a1ba386e3e (diff) |
Merge pull request #14625 from isturdy/generate-cc
Fix C++ codegen with bazel in subrepositories
Diffstat (limited to 'bazel')
-rw-r--r-- | bazel/generate_cc.bzl | 28 | ||||
-rw-r--r-- | bazel/grpc_deps.bzl | 10 |
2 files changed, 35 insertions, 3 deletions
diff --git a/bazel/generate_cc.bzl b/bazel/generate_cc.bzl index e610123dc5..ae747aa42c 100644 --- a/bazel/generate_cc.bzl +++ b/bazel/generate_cc.bzl @@ -10,7 +10,16 @@ def generate_cc_impl(ctx): 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 = len(ctx.label.package) + 1 + 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] @@ -20,7 +29,7 @@ def generate_cc_impl(ctx): 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) + dir_out = str(ctx.genfiles_dir.path + proto_root) arguments = [] if ctx.executable.plugin: @@ -33,7 +42,20 @@ def generate_cc_impl(ctx): else: arguments += ["--cpp_out=" + ",".join(ctx.attr.flags) + ":" + dir_out] additional_input = [] - arguments += ["-I{0}={0}".format(include.path) for include in includes] + + # 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 diff --git a/bazel/grpc_deps.bzl b/bazel/grpc_deps.bzl index a441c3ff3d..6b3a55379e 100644 --- a/bazel/grpc_deps.bzl +++ b/bazel/grpc_deps.bzl @@ -57,6 +57,16 @@ def grpc_deps(): actual = "@com_github_gflags_gflags//:gflags", ) + native.bind( + name = "grpc_cpp_plugin", + actual = "@com_github_grpc_grpc//:grpc_cpp_plugin" + ) + + native.bind( + name = "grpc++_codegen_proto", + actual = "@com_github_grpc_grpc//:grpc++_codegen_proto" + ) + if "boringssl" not in native.existing_rules(): native.http_archive( name = "boringssl", |