Internal change

PiperOrigin-RevId: 548746432
This commit is contained in:
MediaPipe Team 2023-07-17 11:15:12 -07:00 committed by Copybara-Service
parent 17bc1a5ab5
commit f1f9f80cd9
2 changed files with 49 additions and 31 deletions

View File

@ -44,6 +44,9 @@ bzl_library(
"encode_binary_proto.bzl", "encode_binary_proto.bzl",
], ],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [
"@bazel_skylib//lib:paths",
],
) )
alias( alias(

View File

@ -37,29 +37,33 @@ Args:
output: The desired name of the output file. Optional. output: The desired name of the output file. Optional.
""" """
load("@bazel_skylib//lib:paths.bzl", "paths")
PROTOC = "@com_google_protobuf//:protoc" PROTOC = "@com_google_protobuf//:protoc"
def _canonicalize_proto_path_oss(all_protos, genfile_path): def _canonicalize_proto_path_oss(f):
"""For the protos from external repository, canonicalize the proto path and the file name. if not f.root.path:
return struct(
proto_path = ".",
file_name = f.short_path,
)
Returns: # `f.path` looks like "<genfiles>/external/<repo>/(_virtual_imports/<library>/)?<file_name>"
Proto path list and proto source file list. repo_name, _, file_name = f.path[len(paths.join(f.root.path, "external") + "/"):].partition("/")
""" if file_name.startswith("_virtual_imports/"):
proto_paths = [] # This is a virtual import; move "_virtual_imports/<library>" from `repo_name` to `file_name`.
proto_file_names = [] repo_name = paths.join(repo_name, *file_name.split("/", 2)[:2])
for s in all_protos.to_list(): file_name = file_name.split("/", 2)[-1]
if s.path.startswith(genfile_path): return struct(
repo_name, _, file_name = s.path[len(genfile_path + "/external/"):].partition("/") proto_path = paths.join(f.root.path, "external", repo_name),
file_name = file_name,
)
# handle virtual imports def _map_root_path(f):
if file_name.startswith("_virtual_imports"): return _canonicalize_proto_path_oss(f).proto_path
repo_name = repo_name + "/" + "/".join(file_name.split("/", 2)[:2])
file_name = file_name.split("/", 2)[-1] def _map_short_path(f):
proto_paths.append(genfile_path + "/external/" + repo_name) return _canonicalize_proto_path_oss(f).file_name
proto_file_names.append(file_name)
else:
proto_file_names.append(s.path)
return ([" --proto_path=" + path for path in proto_paths], proto_file_names)
def _get_proto_provider(dep): def _get_proto_provider(dep):
"""Get the provider for protocol buffers from a dependnecy. """Get the provider for protocol buffers from a dependnecy.
@ -90,24 +94,35 @@ def _encode_binary_proto_impl(ctx):
sibling = textpb, sibling = textpb,
) )
path_list, file_list = _canonicalize_proto_path_oss(all_protos, ctx.genfiles_dir.path) args = ctx.actions.args()
args.add(textpb)
args.add(binarypb)
args.add(ctx.executable._proto_compiler)
args.add(ctx.attr.message_type, format = "--encode=%s")
args.add("--proto_path=.")
args.add_all(
all_protos,
map_each = _map_root_path,
format_each = "--proto_path=%s",
uniquify = True,
)
args.add_all(
all_protos,
map_each = _map_short_path,
uniquify = True,
)
# Note: the combination of absolute_paths and proto_path, as well as the exact # Note: the combination of absolute_paths and proto_path, as well as the exact
# order of gendir before ., is needed for the proto compiler to resolve # order of gendir before ., is needed for the proto compiler to resolve
# import statements that reference proto files produced by a genrule. # import statements that reference proto files produced by a genrule.
ctx.actions.run_shell( ctx.actions.run_shell(
tools = all_protos.to_list() + [textpb, ctx.executable._proto_compiler], tools = depset(
outputs = [binarypb], direct = [textpb, ctx.executable._proto_compiler],
command = " ".join( transitive = [all_protos],
[
ctx.executable._proto_compiler.path,
"--encode=" + ctx.attr.message_type,
"--proto_path=" + ctx.genfiles_dir.path,
"--proto_path=" + ctx.bin_dir.path,
"--proto_path=.",
] + path_list + file_list +
["<", textpb.path, ">", binarypb.path],
), ),
outputs = [binarypb],
command = "${@:3} < $1 > $2",
arguments = [args],
mnemonic = "EncodeProto", mnemonic = "EncodeProto",
) )