From ec27b28121dec4c8a6cad0eab60efec706114718 Mon Sep 17 00:00:00 2001 From: Ian Sturdy Date: Wed, 7 Mar 2018 11:27:59 -0800 Subject: Fix C++ codegen with bazel in subrepositories --- bazel/generate_cc.bzl | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'bazel') 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 -- cgit v1.2.3