grpcio_tools.bzl 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. Houses utility rules for grpcio-tools.
  16. """
  17. def _generate_copied_files_impl(ctx):
  18. srcs = ctx.attr.srcs[0]
  19. strip_prefix = ctx.attr.strip_prefix
  20. dest = ctx.attr.dest
  21. outs = []
  22. for f in srcs.files.to_list():
  23. destination_path = f.path
  24. if f.path.startswith("external"):
  25. external_separator = f.path.find("/")
  26. repository_separator = f.path.find("/", external_separator + 1)
  27. destination_path = f.path[repository_separator + 1:]
  28. if not destination_path.startswith(strip_prefix):
  29. fail("File '{}' did not start with '{}'.".format(
  30. destination_path,
  31. strip_prefix,
  32. ))
  33. destination_path = dest + destination_path[len(strip_prefix):]
  34. destination_dir = destination_path.rfind("/")
  35. out_file = ctx.actions.declare_file(destination_path)
  36. outs.append(out_file)
  37. ctx.actions.run_shell(
  38. inputs = [f],
  39. outputs = [out_file],
  40. command = "mkdir -p {0} && cp {1} {2}".format(
  41. out_file.dirname,
  42. f.path,
  43. out_file.path,
  44. ),
  45. )
  46. return [DefaultInfo(files = depset(direct = outs))]
  47. _generate_copied_files = rule(
  48. attrs = {
  49. "srcs": attr.label_list(
  50. mandatory = True,
  51. allow_empty = False,
  52. ),
  53. "strip_prefix": attr.string(
  54. default = "",
  55. ),
  56. "dest": attr.string(
  57. mandatory = True,
  58. ),
  59. },
  60. implementation = _generate_copied_files_impl,
  61. )
  62. def internal_copied_filegroup(name, srcs, strip_prefix, dest):
  63. """Copies a file group to the current package.
  64. Useful for using an existing filegroup as a data dependency.
  65. Args:
  66. name: The name of the rule.
  67. srcs: A single filegroup.
  68. strip_prefix: An optional string to strip from the beginning
  69. of the path of each file in the filegroup. Must end in a slash.
  70. dest: The directory in which to put the files, relative to the
  71. current package. Must end in a slash.
  72. """
  73. if len(srcs) != 1:
  74. fail("srcs must be a single filegroup.")
  75. if not dest.endswith("/"):
  76. fail("dest must end with a '/' character.")
  77. _symlink_target = name + "_symlink"
  78. _generate_copied_files(
  79. name = _symlink_target,
  80. srcs = srcs,
  81. strip_prefix = strip_prefix,
  82. dest = dest,
  83. )
  84. native.filegroup(
  85. name = name,
  86. srcs = [":" + _symlink_target],
  87. )