aboutsummaryrefslogtreecommitdiffhomepage
path: root/bazel
diff options
context:
space:
mode:
authorGravatar Ian Sturdy <sturdy@google.com>2018-03-07 11:27:59 -0800
committerGravatar Ian Sturdy <sturdy@google.com>2018-03-07 11:27:59 -0800
commitec27b28121dec4c8a6cad0eab60efec706114718 (patch)
tree4e163ec6d9eb00657c4525a95f1bf70121aa7ce2 /bazel
parent66d110034e92779addc1ddc0813abb2a19ec6a95 (diff)
Fix C++ codegen with bazel in subrepositories
Diffstat (limited to 'bazel')
-rw-r--r--bazel/generate_cc.bzl19
1 files changed, 16 insertions, 3 deletions
diff --git a/bazel/generate_cc.bzl b/bazel/generate_cc.bzl
index f88ee2f56f..11affaa4c7 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,11 @@ 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 the workspace root so that protoc prints the right
+ # include paths.
+ arguments += ["--proto_path=" + (ctx.label.workspace_root or ".")]
+ # A second include 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