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 | load("@perfetto_cfg//:perfetto_cfg.bzl", "PERFETTO_CONFIG") |
| 16 | load("@perfetto//bazel:proto_gen.bzl", "proto_gen") |
| 17 | |
| 18 | # +----------------------------------------------------------------------------+ |
| 19 | # | Base C++ rules. | |
| 20 | # +----------------------------------------------------------------------------+ |
| 21 | |
| 22 | def default_cc_args(): |
| 23 | return { |
Pascal Muetschard | d272386 | 2019-09-23 13:10:05 -0700 | [diff] [blame] | 24 | "deps": PERFETTO_CONFIG.deps.build_config, |
Sami Kyostila | 4a7bcd6 | 2020-01-17 13:38:31 +0000 | [diff] [blame] | 25 | "copts": [ |
| 26 | "-Wno-pragma-system-header-outside-header", |
| 27 | ], |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 28 | "includes": ["include"], |
| 29 | "linkopts": select({ |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 30 | "@perfetto//bazel:os_linux": ["-ldl", "-lrt", "-lpthread"], |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 31 | "@perfetto//bazel:os_osx": [], |
Pascal Muetschard | c691123 | 2019-09-24 13:40:28 -0700 | [diff] [blame] | 32 | "@perfetto//bazel:os_windows": [], |
| 33 | "//conditions:default": ["-ldl"], |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 34 | }), |
| 35 | } |
| 36 | |
| 37 | def 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 | |
| 42 | def 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 | |
| 47 | def 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 | |
| 55 | def perfetto_proto_library(**kwargs): |
| 56 | if not _rule_override("proto_library", **kwargs): |
| 57 | native.proto_library(**kwargs) |
| 58 | |
| 59 | def perfetto_cc_proto_library(**kwargs): |
| 60 | if not _rule_override("cc_proto_library", **kwargs): |
| 61 | native.cc_proto_library(**kwargs) |
| 62 | |
Lalit Maganti | 6560034 | 2019-09-19 21:35:35 +0100 | [diff] [blame] | 63 | def 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. |
| 73 | def perfetto_gensignature_internal_only(**kwargs): |
| 74 | _rule_override("gensignature_internal_only", **kwargs) |
| 75 | |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 76 | # 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. |
| 79 | def 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 Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 93 | wrapper_namespace = "pbzero", |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 94 | 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. |
| 114 | def perfetto_cc_ipc_library(name, deps, **kwargs): |
| 115 | if _rule_override("cc_ipc_library", name = name, deps = deps, **kwargs): |
| 116 | return |
| 117 | |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 118 | # 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 Maganti | 6e1a4d9 | 2019-12-19 13:43:56 +0000 | [diff] [blame] | 127 | fail("Too many proto deps for target %s" % name) |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 128 | |
| 129 | # Generates .ipc.{cc,h}. |
| 130 | proto_gen( |
| 131 | name = name + "_src", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 132 | deps = _proto_deps, |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 133 | suffix = "ipc", |
| 134 | plugin = PERFETTO_CONFIG.root + ":ipc_plugin", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 135 | wrapper_namespace = "gen", |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 136 | 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 Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 151 | # Generated .ipc.{cc,h} depend on this and protozero. |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 152 | PERFETTO_CONFIG.root + ":perfetto_ipc", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 153 | PERFETTO_CONFIG.root + ":libprotozero", |
| 154 | ] + _cc_deps, |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 155 | **kwargs |
| 156 | ) |
| 157 | |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 158 | |
| 159 | # Generates .gen.{cc,h} from .proto(s). |
| 160 | def 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 Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 177 | if len(_proto_deps) != 1: |
Lalit Maganti | 6e1a4d9 | 2019-12-19 13:43:56 +0000 | [diff] [blame] | 178 | fail("Too many proto deps for target %s" % name) |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 179 | |
| 180 | proto_gen( |
| 181 | name = name + "_gen", |
| 182 | deps = _proto_deps, |
| 183 | suffix = "gen", |
| 184 | plugin = PERFETTO_CONFIG.root + ":cppgen_plugin", |
Primiano Tucci | e8020f9 | 2019-11-26 13:24:01 +0000 | [diff] [blame] | 185 | wrapper_namespace = "gen", |
Primiano Tucci | 57dd66b | 2019-10-15 23:09:04 +0100 | [diff] [blame] | 186 | 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 Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 206 | # +----------------------------------------------------------------------------+ |
| 207 | # | Misc utility functions | |
| 208 | # +----------------------------------------------------------------------------+ |
| 209 | |
| 210 | def _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 | |
| 218 | def _merge_dicts(*args): |
| 219 | res = {} |
| 220 | for arg in args: |
| 221 | for k, v in arg.items(): |
Lalit Maganti | f9c004d | 2020-01-06 14:44:34 +0000 | [diff] [blame] | 222 | if type(v) == "string" or type(v) == "bool": |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 223 | 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 |