xref: /aosp_15_r20/external/grpc-grpc/tools/distrib/python/grpcio_tools/grpcio_tools.bzl (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2020 The gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16Houses utility rules for grpcio-tools.
17"""
18
19def _generate_copied_files_impl(ctx):
20    srcs = ctx.attr.srcs[0]
21    strip_prefix = ctx.attr.strip_prefix
22    dest = ctx.attr.dest
23
24    outs = []
25    for f in srcs.files.to_list():
26        destination_path = f.path
27        if f.path.startswith("external"):
28            external_separator = f.path.find("/")
29            repository_separator = f.path.find("/", external_separator + 1)
30            destination_path = f.path[repository_separator + 1:]
31        if not destination_path.startswith(strip_prefix):
32            fail("File '{}' did not start with '{}'.".format(
33                destination_path,
34                strip_prefix,
35            ))
36        destination_path = dest + destination_path[len(strip_prefix):]
37        destination_dir = destination_path.rfind("/")
38        out_file = ctx.actions.declare_file(destination_path)
39        outs.append(out_file)
40        ctx.actions.run_shell(
41            inputs = [f],
42            outputs = [out_file],
43            command = "mkdir -p {0} && cp {1} {2}".format(
44                out_file.dirname,
45                f.path,
46                out_file.path,
47            ),
48        )
49
50    return [DefaultInfo(files = depset(direct = outs))]
51
52_generate_copied_files = rule(
53    attrs = {
54        "srcs": attr.label_list(
55            mandatory = True,
56            allow_empty = False,
57        ),
58        "strip_prefix": attr.string(
59            default = "",
60        ),
61        "dest": attr.string(
62            mandatory = True,
63        ),
64    },
65    implementation = _generate_copied_files_impl,
66)
67
68def internal_copied_filegroup(name, srcs, strip_prefix, dest):
69    """Copies a file group to the current package.
70
71    Useful for using an existing filegroup as a data dependency.
72
73    Args:
74      name: The name of the rule.
75      srcs: A single filegroup.
76      strip_prefix: An optional string to strip from the beginning
77        of the path of each file in the filegroup. Must end in a slash.
78      dest: The directory in which to put the files, relative to the
79        current package. Must end in a slash.
80    """
81    if len(srcs) != 1:
82        fail("srcs must be a single filegroup.")
83
84    if not dest.endswith("/"):
85        fail("dest must end with a '/' character.")
86
87    _symlink_target = name + "_symlink"
88    _generate_copied_files(
89        name = _symlink_target,
90        srcs = srcs,
91        strip_prefix = strip_prefix,
92        dest = dest,
93    )
94
95    native.filegroup(
96        name = name,
97        srcs = [":" + _symlink_target],
98    )
99