Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 1 | # Copyright (C) 2019 The Android Open Source Project |
| 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 | # This file defines the proto_gen() rule that is used for generating protos |
| 16 | # with custom plugins (ipc and protozero). |
| 17 | |
| 18 | def _proto_gen_impl(ctx): |
| 19 | proto_src = [ |
| 20 | f |
| 21 | for dep in ctx.attr.deps |
| 22 | for f in dep[ProtoInfo].direct_sources |
| 23 | ] |
| 24 | includes = [ |
| 25 | f |
| 26 | for dep in ctx.attr.deps |
| 27 | for f in dep[ProtoInfo].transitive_imports.to_list() |
| 28 | ] |
Kean Mariotti | 24e8906 | 2024-04-22 10:28:08 +0000 | [diff] [blame] | 29 | proto_paths = [ |
| 30 | f |
| 31 | for dep in ctx.attr.deps |
| 32 | for f in dep[ProtoInfo].transitive_proto_path.to_list() |
| 33 | ] |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 34 | |
| 35 | proto_path = "." |
| 36 | |
Lalit Maganti | e0a1237 | 2024-03-19 15:33:20 +0000 | [diff] [blame] | 37 | out_dir = ctx.bin_dir.path |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 38 | strip_base_path = "" |
| 39 | if ctx.attr.root != "//": |
| 40 | # This path is hit in Google internal builds, where root is typically |
| 41 | # //third_party/perfetto. |
| 42 | proto_path = "." |
Lalit Maganti | a4244e4 | 2019-09-20 12:03:32 +0100 | [diff] [blame] | 43 | |
| 44 | # The below will likely be //third_party/perfetto/ but may also be any |
| 45 | # subdir under //third_party/perfetto. |
Lalit Maganti | 40a75cb | 2024-02-05 16:13:59 +0000 | [diff] [blame] | 46 | strip_base_path = ctx.label.package + "/" |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 47 | elif ctx.label.workspace_root: |
| 48 | # This path is hit when proto targets are built as @perfetto//:xxx |
Erwin Jansen | b0b4de5 | 2024-05-21 21:55:07 -0700 | [diff] [blame] | 49 | # instead of //:xxx. This happens in embedder builds. |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 50 | proto_path = ctx.label.workspace_root |
Erwin Jansen | b0b4de5 | 2024-05-21 21:55:07 -0700 | [diff] [blame] | 51 | |
| 52 | # We could be using the sibling repository layout, in which case we do nothing. |
| 53 | if not ctx.label.workspace_root.startswith("../"): |
| 54 | # workspace_root == "external/perfetto" and we need to rebase the paths |
| 55 | # passed to protoc. |
| 56 | out_dir += "/" + ctx.label.workspace_root |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 57 | strip_base_path = ctx.label.workspace_root + "/" |
| 58 | |
Erwin Jansen | b0b4de5 | 2024-05-21 21:55:07 -0700 | [diff] [blame] | 59 | |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 60 | out_files = [] |
| 61 | suffix = ctx.attr.suffix |
| 62 | for src in proto_src: |
| 63 | base_path = src.path[:-len(".proto")] |
| 64 | if base_path.startswith(strip_base_path): |
| 65 | base_path = base_path[len(strip_base_path):] |
| 66 | out_files += [ctx.actions.declare_file(base_path + ".%s.h" % suffix)] |
| 67 | out_files += [ctx.actions.declare_file(base_path + ".%s.cc" % suffix)] |
| 68 | |
| 69 | arguments = [ |
Kean Mariotti | 24e8906 | 2024-04-22 10:28:08 +0000 | [diff] [blame] | 70 | "--proto_path=" + proto_path |
| 71 | for proto_path in proto_paths |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 72 | ] |
Kean Mariotti | 24e8906 | 2024-04-22 10:28:08 +0000 | [diff] [blame] | 73 | |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 74 | plugin_deps = [] |
| 75 | if ctx.attr.plugin: |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 76 | wrap_arg = ctx.attr.wrapper_namespace |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 77 | arguments += [ |
| 78 | "--plugin=protoc-gen-plugin=" + ctx.executable.plugin.path, |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 79 | "--plugin_out=wrapper_namespace=" + wrap_arg + ":" + out_dir, |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 80 | ] |
| 81 | plugin_deps += [ctx.executable.plugin] |
| 82 | else: |
| 83 | arguments += [ |
Primiano Tucci | 7b6a788 | 2020-01-20 22:34:31 +0000 | [diff] [blame] | 84 | "--cpp_out=lite=true:" + out_dir, |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 85 | ] |
| 86 | |
| 87 | arguments += [src.path for src in proto_src] |
| 88 | ctx.actions.run( |
| 89 | inputs = proto_src + includes + plugin_deps, |
| 90 | tools = plugin_deps, |
| 91 | outputs = out_files, |
| 92 | executable = ctx.executable.protoc, |
| 93 | arguments = arguments, |
| 94 | ) |
Lalit Maganti | b8698fa | 2020-05-11 19:25:36 +0100 | [diff] [blame] | 95 | cc_files = depset([f for f in out_files if f.path.endswith(".cc")]) |
| 96 | h_files = depset([f for f in out_files if f.path.endswith(".h")]) |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 97 | return [ |
Lalit Maganti | b8698fa | 2020-05-11 19:25:36 +0100 | [diff] [blame] | 98 | DefaultInfo(files = cc_files), |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 99 | OutputGroupInfo( |
Lalit Maganti | b8698fa | 2020-05-11 19:25:36 +0100 | [diff] [blame] | 100 | cc = cc_files, |
| 101 | h = h_files, |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 102 | ), |
| 103 | ] |
| 104 | |
Lalit Maganti | b8698fa | 2020-05-11 19:25:36 +0100 | [diff] [blame] | 105 | |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 106 | proto_gen = rule( |
| 107 | attrs = { |
| 108 | "deps": attr.label_list( |
| 109 | mandatory = True, |
| 110 | allow_empty = False, |
| 111 | providers = [ProtoInfo], |
| 112 | ), |
| 113 | "plugin": attr.label( |
| 114 | executable = True, |
| 115 | mandatory = False, |
| 116 | cfg = "host", |
| 117 | ), |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 118 | "wrapper_namespace": attr.string( |
| 119 | mandatory = False, |
| 120 | default = "" |
| 121 | ), |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 122 | "suffix": attr.string( |
| 123 | mandatory = True, |
| 124 | ), |
| 125 | "protoc": attr.label( |
| 126 | executable = True, |
| 127 | cfg = "host", |
| 128 | ), |
| 129 | "root": attr.string( |
| 130 | mandatory = False, |
| 131 | default = "//", |
| 132 | ), |
| 133 | }, |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 134 | implementation = _proto_gen_impl, |
| 135 | ) |
Lalit Maganti | 117272f | 2020-09-11 14:01:18 +0100 | [diff] [blame] | 136 | |
| 137 | |
| 138 | def _proto_descriptor_gen_impl(ctx): |
| 139 | descriptors = [ |
| 140 | f |
| 141 | for dep in ctx.attr.deps |
| 142 | for f in dep[ProtoInfo].transitive_descriptor_sets.to_list() |
| 143 | ] |
| 144 | ctx.actions.run_shell( |
| 145 | inputs=descriptors, |
| 146 | outputs=ctx.outputs.outs, |
| 147 | command='cat %s > %s' % ( |
| 148 | ' '.join([f.path for f in descriptors]), ctx.outputs.outs[0].path) |
| 149 | ) |
| 150 | |
| 151 | |
| 152 | proto_descriptor_gen = rule( |
| 153 | implementation=_proto_descriptor_gen_impl, |
| 154 | attrs = { |
| 155 | "deps": attr.label_list( |
| 156 | mandatory = True, |
| 157 | allow_empty = False, |
| 158 | providers = [ProtoInfo], |
| 159 | ), |
| 160 | "outs": attr.output_list(mandatory=True), |
| 161 | } |
| 162 | ) |