From 79f54e9d5783a2d521f20a5f5c0e11496709abb3 Mon Sep 17 00:00:00 2001 From: Richard Belleville Date: Mon, 19 Oct 2020 23:39:01 -0700 Subject: [PATCH] Include well known types in bazel build --- tools/distrib/python/grpcio_tools/BUILD.bazel | 10 +- .../python/grpcio_tools/grpcio_tools.bzl | 91 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tools/distrib/python/grpcio_tools/grpcio_tools.bzl diff --git a/tools/distrib/python/grpcio_tools/BUILD.bazel b/tools/distrib/python/grpcio_tools/BUILD.bazel index a23975feeb8..eaf4895b49b 100644 --- a/tools/distrib/python/grpcio_tools/BUILD.bazel +++ b/tools/distrib/python/grpcio_tools/BUILD.bazel @@ -19,6 +19,7 @@ package(default_visibility = [ ]) load("//bazel:cython_library.bzl", "pyx_library") +load("grpcio_tools.bzl", "internal_copied_filegroup") cc_library( name = "protoc_lib", @@ -37,6 +38,13 @@ pyx_library( deps = [":protoc_lib"], ) +internal_copied_filegroup( + name = "well_known_protos", + srcs = ["@com_google_protobuf//:well_known_protos"], + strip_prefix = "src/", + dest = "grpc_tools/_proto/", +) + py_library( name = "grpc_tools", srcs = [ @@ -50,5 +58,5 @@ py_library( "//src/python/grpcio/grpc:grpcio", "@com_google_protobuf//:protobuf_python", ], - data = glob(["grpc_tools/_proto/**/*"]), + data = [":well_known_protos"], ) diff --git a/tools/distrib/python/grpcio_tools/grpcio_tools.bzl b/tools/distrib/python/grpcio_tools/grpcio_tools.bzl new file mode 100644 index 00000000000..8b6c121ffa6 --- /dev/null +++ b/tools/distrib/python/grpcio_tools/grpcio_tools.bzl @@ -0,0 +1,91 @@ +# Copyright 2020 The gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +def _generate_copied_files_impl(ctx): + srcs = ctx.attr.srcs[0] + strip_prefix = ctx.attr.strip_prefix + dest = ctx.attr.dest + + outs = [] + for f in srcs.files.to_list(): + destination_path = f.path + if f.path.startswith("external"): + external_separator = f.path.find("/") + repository_separator = f.path.find("/", external_separator + 1) + destination_path = f.path[repository_separator+1:] + if not destination_path.startswith(strip_prefix): + fail("File '{}' did not start with '{}'.".format( + destination_path, strip_prefix)) + destination_path = dest + destination_path[len(strip_prefix):] + destination_dir = destination_path.rfind("/") + out_file = ctx.actions.declare_file(destination_path) + outs.append(out_file) + ctx.actions.run_shell( + inputs = [f], + outputs = [out_file], + command = "mkdir -p {0} && cp {1} {2}".format( + out_file.dirname, f.path, out_file.path), + ) + + return [DefaultInfo(files = depset(direct = outs))] + + +_generate_copied_files = rule( + attrs = { + "srcs": attr.label_list( + mandatory = True, + allow_empty = False, + ), + "strip_prefix": attr.string( + default = "", + ), + "dest": attr.string( + mandatory = True, + ) + }, + implementation = _generate_copied_files_impl, +) + + +def internal_copied_filegroup(name, srcs, strip_prefix, dest): + """Copies a file group to the current package. + + Useful for using an existing filegroup as a data dependency. + + Args: + name: The name of the rule. + srcs: A single filegroup. + strip_prefix: An optional string to strip from the beginning + of the path of each file in the filegroup. Must end in a slash. + dest: The directory in which to put the files, relative to the + current package. Must end in a slash. + """ + if len(srcs) != 1: + fail("srcs must be a single filegroup.") + + if not dest.endswith("/"): + fail("dest must end with a '/' character.") + + _symlink_target = name + "_symlink" + _generate_copied_files( + name = _symlink_target, + srcs = srcs, + strip_prefix = strip_prefix, + dest = dest, + ) + + native.filegroup( + name = name, + srcs = [":" + _symlink_target], + )