blob: 49d4aaf8829c483273057462054f526ca4e02548 [file] [log] [blame]
Primiano Tucci1d409982019-09-19 10:15:18 +01001# 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
15load("@perfetto_cfg//:perfetto_cfg.bzl", "PERFETTO_CONFIG")
Lalit Maganti117272f2020-09-11 14:01:18 +010016load("@perfetto//bazel:proto_gen.bzl", "proto_descriptor_gen", "proto_gen")
Primiano Tucci1d409982019-09-19 10:15:18 +010017
18# +----------------------------------------------------------------------------+
19# | Base C++ rules. |
20# +----------------------------------------------------------------------------+
21
22def default_cc_args():
23 return {
Pascal Muetschardd2723862019-09-23 13:10:05 -070024 "deps": PERFETTO_CONFIG.deps.build_config,
Sami Kyostila4a7bcd62020-01-17 13:38:31 +000025 "copts": [
26 "-Wno-pragma-system-header-outside-header",
27 ],
Primiano Tucci1d409982019-09-19 10:15:18 +010028 "includes": ["include"],
29 "linkopts": select({
Primiano Tuccie8020f92019-11-26 13:24:01 +000030 "@perfetto//bazel:os_linux": ["-ldl", "-lrt", "-lpthread"],
Primiano Tucci1d409982019-09-19 10:15:18 +010031 "@perfetto//bazel:os_osx": [],
Pascal Muetschardc6911232019-09-24 13:40:28 -070032 "@perfetto//bazel:os_windows": [],
33 "//conditions:default": ["-ldl"],
Primiano Tucci1d409982019-09-19 10:15:18 +010034 }),
35 }
36
Harvey Yang321c4fc2021-10-25 14:11:50 +080037def perfetto_build_config_cc_library(**kwargs):
38 if not _rule_override("cc_library", **kwargs):
39 native.cc_library(**kwargs)
40
41def perfetto_filegroup(**kwargs):
42 if not _rule_override("filegroup", **kwargs):
43 native.filegroup(**kwargs)
44
45def perfetto_genrule(**kwargs):
46 if not _rule_override("genrule", **kwargs):
47 native.genrule(**kwargs)
48
Primiano Tucci1d409982019-09-19 10:15:18 +010049def perfetto_cc_library(**kwargs):
50 args = _merge_dicts(default_cc_args(), kwargs)
51 if not _rule_override("cc_library", **args):
52 native.cc_library(**args)
53
54def perfetto_cc_binary(**kwargs):
55 args = _merge_dicts(default_cc_args(), kwargs)
56 if not _rule_override("cc_binary", **args):
57 native.cc_binary(**args)
58
59def perfetto_py_binary(**kwargs):
60 if not _rule_override("py_binary", **kwargs):
61 native.py_binary(**kwargs)
62
Anindita Ghosh237a7762020-06-30 10:46:52 +000063def perfetto_py_library(**kwargs):
64 if not _rule_override("py_library", **kwargs):
65 native.py_library(**kwargs)
66
Primiano Tucci1d409982019-09-19 10:15:18 +010067# +----------------------------------------------------------------------------+
68# | Proto-related rules |
69# +----------------------------------------------------------------------------+
70
71def perfetto_proto_library(**kwargs):
72 if not _rule_override("proto_library", **kwargs):
73 native.proto_library(**kwargs)
74
75def perfetto_cc_proto_library(**kwargs):
76 if not _rule_override("cc_proto_library", **kwargs):
77 native.cc_proto_library(**kwargs)
78
Lalit Maganti65600342019-09-19 21:35:35 +010079def perfetto_java_proto_library(**kwargs):
80 if not _rule_override("java_proto_library", **kwargs):
81 native.java_proto_library(**kwargs)
82
Lalit Maganti46e2bda2020-05-06 12:51:33 +010083def perfetto_java_lite_proto_library(**kwargs):
84 if not _rule_override("java_lite_proto_library", **kwargs):
85 native.java_lite_proto_library(**kwargs)
86
Lalit Maganti65600342019-09-19 21:35:35 +010087# +----------------------------------------------------------------------------+
88# | Misc rules. |
89# +----------------------------------------------------------------------------+
90
91# Unlike all the other rules, this is an noop by default because Bazel does not
92# support gensignature.
93def perfetto_gensignature_internal_only(**kwargs):
94 _rule_override("gensignature_internal_only", **kwargs)
95
Primiano Tucci1d409982019-09-19 10:15:18 +010096# Generates .pbzero.{cc,h} from .proto(s). We deliberately do NOT generate
97# conventional .pb.{cc,h} from here as protozero gen sources do not have any
98# dependency on libprotobuf.
99def perfetto_cc_protozero_library(name, deps, **kwargs):
100 if _rule_override(
101 "cc_protozero_library",
102 name = name,
103 deps = deps,
104 **kwargs
105 ):
106 return
107
Lalit Maganti25b59732021-04-20 16:18:32 +0100108 # A perfetto_cc_protozero_library has two types of dependencies:
109 # 1. Exactly one dependency on a proto_library target. This defines the
110 # .proto sources for the target
111 # 2. Zero or more deps on other perfetto_cc_protozero_library targets. This
112 # to deal with the case of foo.proto including common.proto from another
113 # target.
114 _proto_deps = [d for d in deps if d.endswith("_protos")]
115 _cc_deps = [d for d in deps if d not in _proto_deps]
116 if len(_proto_deps) != 1:
117 fail("Too many proto deps for target %s" % name)
118
Harvey Yang321c4fc2021-10-25 14:11:50 +0800119 args = {
120 'name': name + "_src",
121 'deps': _proto_deps,
122 'suffix': "pbzero",
123 'plugin': PERFETTO_CONFIG.root + ":protozero_plugin",
124 'wrapper_namespace': "pbzero",
125 'protoc': PERFETTO_CONFIG.deps.protoc[0],
126 'root': PERFETTO_CONFIG.root,
127 }
128 if not _rule_override("proto_gen", **args):
129 proto_gen(**args)
Primiano Tucci1d409982019-09-19 10:15:18 +0100130
Harvey Yang321c4fc2021-10-25 14:11:50 +0800131 perfetto_filegroup(
Primiano Tucci1d409982019-09-19 10:15:18 +0100132 name = name + "_h",
133 srcs = [":" + name + "_src"],
134 output_group = "h",
135 )
136
137 perfetto_cc_library(
138 name = name,
139 srcs = [":" + name + "_src"],
140 hdrs = [":" + name + "_h"],
Lalit Maganti25b59732021-04-20 16:18:32 +0100141 deps = [PERFETTO_CONFIG.root + ":protozero"] + _cc_deps,
Primiano Tucci1d409982019-09-19 10:15:18 +0100142 **kwargs
143 )
144
145# Generates .ipc.{cc,h} and .pb.{cc.h} from .proto(s). The IPC sources depend
146# on .pb.h so we need to generate also the standard protobuf sources here.
147def perfetto_cc_ipc_library(name, deps, **kwargs):
148 if _rule_override("cc_ipc_library", name = name, deps = deps, **kwargs):
149 return
150
Primiano Tuccie8020f92019-11-26 13:24:01 +0000151 # A perfetto_cc_ipc_library has two types of dependencies:
152 # 1. Exactly one dependency on a proto_library target. This defines the
153 # .proto sources for the target
154 # 2. Zero or more deps on other perfetto_cc_protocpp_library targets. This
155 # to deal with the case of foo.proto including common.proto from another
156 # target.
157 _proto_deps = [d for d in deps if d.endswith("_protos")]
158 _cc_deps = [d for d in deps if d not in _proto_deps]
159 if len(_proto_deps) != 1:
Lalit Maganti6e1a4d92019-12-19 13:43:56 +0000160 fail("Too many proto deps for target %s" % name)
Primiano Tucci1d409982019-09-19 10:15:18 +0100161
162 # Generates .ipc.{cc,h}.
Harvey Yang4f5db1b2021-10-28 15:02:03 +0800163 args = {
164 'name': name + "_src",
165 'deps': _proto_deps,
166 'suffix': "ipc",
167 'plugin': PERFETTO_CONFIG.root + ":ipc_plugin",
168 'wrapper_namespace': "gen",
169 'protoc': PERFETTO_CONFIG.deps.protoc[0],
170 'root': PERFETTO_CONFIG.root,
171 }
172 if not _rule_override("proto_gen", **args):
173 proto_gen(**args)
Primiano Tucci1d409982019-09-19 10:15:18 +0100174
Harvey Yang4f5db1b2021-10-28 15:02:03 +0800175 perfetto_filegroup(
Primiano Tucci1d409982019-09-19 10:15:18 +0100176 name = name + "_h",
177 srcs = [":" + name + "_src"],
178 output_group = "h",
179 )
180
181 perfetto_cc_library(
182 name = name,
183 srcs = [":" + name + "_src"],
184 hdrs = [":" + name + "_h"],
185 deps = [
Primiano Tuccie8020f92019-11-26 13:24:01 +0000186 # Generated .ipc.{cc,h} depend on this and protozero.
Primiano Tucci1d409982019-09-19 10:15:18 +0100187 PERFETTO_CONFIG.root + ":perfetto_ipc",
Primiano Tucci916f4e52020-10-16 20:40:33 +0200188 PERFETTO_CONFIG.root + ":protozero",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000189 ] + _cc_deps,
Primiano Tucci1d409982019-09-19 10:15:18 +0100190 **kwargs
191 )
192
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100193# Generates .gen.{cc,h} from .proto(s).
194def perfetto_cc_protocpp_library(name, deps, **kwargs):
195 if _rule_override(
196 "cc_protocpp_library",
197 name = name,
198 deps = deps,
199 **kwargs
200 ):
201 return
202
203 # A perfetto_cc_protocpp_library has two types of dependencies:
204 # 1. Exactly one dependency on a proto_library target. This defines the
205 # .proto sources for the target
206 # 2. Zero or more deps on other perfetto_cc_protocpp_library targets. This
207 # to deal with the case of foo.proto including common.proto from another
208 # target.
209 _proto_deps = [d for d in deps if d.endswith("_protos")]
210 _cc_deps = [d for d in deps if d not in _proto_deps]
Primiano Tuccie8020f92019-11-26 13:24:01 +0000211 if len(_proto_deps) != 1:
Lalit Maganti6e1a4d92019-12-19 13:43:56 +0000212 fail("Too many proto deps for target %s" % name)
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100213
Harvey Yang4f5db1b2021-10-28 15:02:03 +0800214 args = {
215 'name': name + "_gen",
216 'deps': _proto_deps,
217 'suffix': "gen",
218 'plugin': PERFETTO_CONFIG.root + ":cppgen_plugin",
219 'wrapper_namespace': "gen",
220 'protoc': PERFETTO_CONFIG.deps.protoc[0],
221 'root': PERFETTO_CONFIG.root,
222 }
223 if not _rule_override("proto_gen", **args):
224 proto_gen(**args)
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100225
Harvey Yang4f5db1b2021-10-28 15:02:03 +0800226 perfetto_filegroup(
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100227 name = name + "_gen_h",
228 srcs = [":" + name + "_gen"],
229 output_group = "h",
230 )
231
Lalit Magantib8698fa2020-05-11 19:25:36 +0100232 # The headers from the gen plugin have implicit dependencies
233 # on each other so will fail when compiled independently. Use
234 # textual_hdrs to indicate this to Bazel.
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100235 perfetto_cc_library(
236 name = name,
237 srcs = [":" + name + "_gen"],
Lalit Magantib8698fa2020-05-11 19:25:36 +0100238 textual_hdrs = [":" + name + "_gen_h"],
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100239 deps = [
Primiano Tucci916f4e52020-10-16 20:40:33 +0200240 PERFETTO_CONFIG.root + ":protozero",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100241 ] + _cc_deps,
242 **kwargs
243 )
244
Lalit Maganti117272f2020-09-11 14:01:18 +0100245def perfetto_proto_descriptor(name, deps, outs, **kwargs):
Harvey Yang321c4fc2021-10-25 14:11:50 +0800246 args = {
247 'name': name,
248 'deps': deps,
249 'outs': outs,
250 }
251 if not _rule_override("proto_descriptor_gen", **args):
252 proto_descriptor_gen(**args)
Lalit Maganti117272f2020-09-11 14:01:18 +0100253
254# Generator .descriptor.h from protos
255def perfetto_cc_proto_descriptor(name, deps, outs, **kwargs):
256 cmd = [
257 "$(location gen_cc_proto_descriptor_py)",
258 "--cpp_out=$@",
259 "--gen_dir=$(GENDIR)",
260 "$<"
261 ]
Harvey Yang321c4fc2021-10-25 14:11:50 +0800262 perfetto_genrule(
Lalit Maganti117272f2020-09-11 14:01:18 +0100263 name = name + "_gen",
264 cmd = " ".join(cmd),
265 exec_tools = [
266 ":gen_cc_proto_descriptor_py",
267 ],
268 srcs = deps,
269 outs = outs,
270 )
271
272 perfetto_cc_library(
273 name = name,
274 hdrs = [":" + name + "_gen"],
275 **kwargs
276 )
277
Primiano Tucci1d409982019-09-19 10:15:18 +0100278# +----------------------------------------------------------------------------+
279# | Misc utility functions |
280# +----------------------------------------------------------------------------+
281
282def _rule_override(rule_name, **kwargs):
283 overrides = getattr(PERFETTO_CONFIG, "rule_overrides", struct())
284 overridden_rule = getattr(overrides, rule_name, None)
285 if overridden_rule:
286 overridden_rule(**kwargs)
287 return True
288 return False
289
290def _merge_dicts(*args):
291 res = {}
292 for arg in args:
293 for k, v in arg.items():
Lalit Magantif9c004d2020-01-06 14:44:34 +0000294 if type(v) == "string" or type(v) == "bool":
Primiano Tucci1d409982019-09-19 10:15:18 +0100295 res[k] = v
296 elif type(v) == "list" or type(v) == "select":
297 res[k] = res.get(k, []) + v
298 else:
299 fail("key type not supported: " + type(v))
300 return res