blob: 498f34ae375eaeb59ed414879466ebb409cff22f [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")
16load("@perfetto//bazel:proto_gen.bzl", "proto_gen")
17
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
37def perfetto_cc_library(**kwargs):
38 args = _merge_dicts(default_cc_args(), kwargs)
39 if not _rule_override("cc_library", **args):
40 native.cc_library(**args)
41
42def perfetto_cc_binary(**kwargs):
43 args = _merge_dicts(default_cc_args(), kwargs)
44 if not _rule_override("cc_binary", **args):
45 native.cc_binary(**args)
46
47def perfetto_py_binary(**kwargs):
48 if not _rule_override("py_binary", **kwargs):
49 native.py_binary(**kwargs)
50
51# +----------------------------------------------------------------------------+
52# | Proto-related rules |
53# +----------------------------------------------------------------------------+
54
55def perfetto_proto_library(**kwargs):
56 if not _rule_override("proto_library", **kwargs):
57 native.proto_library(**kwargs)
58
59def perfetto_cc_proto_library(**kwargs):
60 if not _rule_override("cc_proto_library", **kwargs):
61 native.cc_proto_library(**kwargs)
62
Lalit Maganti65600342019-09-19 21:35:35 +010063def perfetto_java_proto_library(**kwargs):
64 if not _rule_override("java_proto_library", **kwargs):
65 native.java_proto_library(**kwargs)
66
67# +----------------------------------------------------------------------------+
68# | Misc rules. |
69# +----------------------------------------------------------------------------+
70
71# Unlike all the other rules, this is an noop by default because Bazel does not
72# support gensignature.
73def perfetto_gensignature_internal_only(**kwargs):
74 _rule_override("gensignature_internal_only", **kwargs)
75
Primiano Tucci1d409982019-09-19 10:15:18 +010076# Generates .pbzero.{cc,h} from .proto(s). We deliberately do NOT generate
77# conventional .pb.{cc,h} from here as protozero gen sources do not have any
78# dependency on libprotobuf.
79def perfetto_cc_protozero_library(name, deps, **kwargs):
80 if _rule_override(
81 "cc_protozero_library",
82 name = name,
83 deps = deps,
84 **kwargs
85 ):
86 return
87
88 proto_gen(
89 name = name + "_src",
90 deps = deps,
91 suffix = "pbzero",
92 plugin = PERFETTO_CONFIG.root + ":protozero_plugin",
Primiano Tuccie8020f92019-11-26 13:24:01 +000093 wrapper_namespace = "pbzero",
Primiano Tucci1d409982019-09-19 10:15:18 +010094 protoc = PERFETTO_CONFIG.deps.protoc[0],
95 root = PERFETTO_CONFIG.root,
96 )
97
98 native.filegroup(
99 name = name + "_h",
100 srcs = [":" + name + "_src"],
101 output_group = "h",
102 )
103
104 perfetto_cc_library(
105 name = name,
106 srcs = [":" + name + "_src"],
107 hdrs = [":" + name + "_h"],
108 deps = [PERFETTO_CONFIG.root + ":libprotozero"],
109 **kwargs
110 )
111
112# Generates .ipc.{cc,h} and .pb.{cc.h} from .proto(s). The IPC sources depend
113# on .pb.h so we need to generate also the standard protobuf sources here.
114def perfetto_cc_ipc_library(name, deps, **kwargs):
115 if _rule_override("cc_ipc_library", name = name, deps = deps, **kwargs):
116 return
117
Primiano Tuccie8020f92019-11-26 13:24:01 +0000118 # A perfetto_cc_ipc_library has two types of dependencies:
119 # 1. Exactly one dependency on a proto_library target. This defines the
120 # .proto sources for the target
121 # 2. Zero or more deps on other perfetto_cc_protocpp_library targets. This
122 # to deal with the case of foo.proto including common.proto from another
123 # target.
124 _proto_deps = [d for d in deps if d.endswith("_protos")]
125 _cc_deps = [d for d in deps if d not in _proto_deps]
126 if len(_proto_deps) != 1:
Lalit Maganti6e1a4d92019-12-19 13:43:56 +0000127 fail("Too many proto deps for target %s" % name)
Primiano Tucci1d409982019-09-19 10:15:18 +0100128
129 # Generates .ipc.{cc,h}.
130 proto_gen(
131 name = name + "_src",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000132 deps = _proto_deps,
Primiano Tucci1d409982019-09-19 10:15:18 +0100133 suffix = "ipc",
134 plugin = PERFETTO_CONFIG.root + ":ipc_plugin",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000135 wrapper_namespace = "gen",
Primiano Tucci1d409982019-09-19 10:15:18 +0100136 protoc = PERFETTO_CONFIG.deps.protoc[0],
137 root = PERFETTO_CONFIG.root,
138 )
139
140 native.filegroup(
141 name = name + "_h",
142 srcs = [":" + name + "_src"],
143 output_group = "h",
144 )
145
146 perfetto_cc_library(
147 name = name,
148 srcs = [":" + name + "_src"],
149 hdrs = [":" + name + "_h"],
150 deps = [
Primiano Tuccie8020f92019-11-26 13:24:01 +0000151 # Generated .ipc.{cc,h} depend on this and protozero.
Primiano Tucci1d409982019-09-19 10:15:18 +0100152 PERFETTO_CONFIG.root + ":perfetto_ipc",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000153 PERFETTO_CONFIG.root + ":libprotozero",
154 ] + _cc_deps,
Primiano Tucci1d409982019-09-19 10:15:18 +0100155 **kwargs
156 )
157
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100158
159# Generates .gen.{cc,h} from .proto(s).
160def perfetto_cc_protocpp_library(name, deps, **kwargs):
161 if _rule_override(
162 "cc_protocpp_library",
163 name = name,
164 deps = deps,
165 **kwargs
166 ):
167 return
168
169 # A perfetto_cc_protocpp_library has two types of dependencies:
170 # 1. Exactly one dependency on a proto_library target. This defines the
171 # .proto sources for the target
172 # 2. Zero or more deps on other perfetto_cc_protocpp_library targets. This
173 # to deal with the case of foo.proto including common.proto from another
174 # target.
175 _proto_deps = [d for d in deps if d.endswith("_protos")]
176 _cc_deps = [d for d in deps if d not in _proto_deps]
Primiano Tuccie8020f92019-11-26 13:24:01 +0000177 if len(_proto_deps) != 1:
Lalit Maganti6e1a4d92019-12-19 13:43:56 +0000178 fail("Too many proto deps for target %s" % name)
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100179
180 proto_gen(
181 name = name + "_gen",
182 deps = _proto_deps,
183 suffix = "gen",
184 plugin = PERFETTO_CONFIG.root + ":cppgen_plugin",
Primiano Tuccie8020f92019-11-26 13:24:01 +0000185 wrapper_namespace = "gen",
Primiano Tucci57dd66b2019-10-15 23:09:04 +0100186 protoc = PERFETTO_CONFIG.deps.protoc[0],
187 root = PERFETTO_CONFIG.root,
188 )
189
190 native.filegroup(
191 name = name + "_gen_h",
192 srcs = [":" + name + "_gen"],
193 output_group = "h",
194 )
195
196 perfetto_cc_library(
197 name = name,
198 srcs = [":" + name + "_gen"],
199 hdrs = [":" + name + "_gen_h"],
200 deps = [
201 PERFETTO_CONFIG.root + ":libprotozero"
202 ] + _cc_deps,
203 **kwargs
204 )
205
Primiano Tucci1d409982019-09-19 10:15:18 +0100206# +----------------------------------------------------------------------------+
207# | Misc utility functions |
208# +----------------------------------------------------------------------------+
209
210def _rule_override(rule_name, **kwargs):
211 overrides = getattr(PERFETTO_CONFIG, "rule_overrides", struct())
212 overridden_rule = getattr(overrides, rule_name, None)
213 if overridden_rule:
214 overridden_rule(**kwargs)
215 return True
216 return False
217
218def _merge_dicts(*args):
219 res = {}
220 for arg in args:
221 for k, v in arg.items():
Lalit Magantif9c004d2020-01-06 14:44:34 +0000222 if type(v) == "string" or type(v) == "bool":
Primiano Tucci1d409982019-09-19 10:15:18 +0100223 res[k] = v
224 elif type(v) == "list" or type(v) == "select":
225 res[k] = res.get(k, []) + v
226 else:
227 fail("key type not supported: " + type(v))
228 return res