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 { |
| 24 | "deps": [PERFETTO_CONFIG.root + ":build_config_hdr"], |
| 25 | "copts": [], |
| 26 | "includes": ["include"], |
| 27 | "linkopts": select({ |
| 28 | "@perfetto//bazel:os_osx": [], |
| 29 | "//conditions:default": ["-ldl", "-lrt"], |
| 30 | }), |
| 31 | } |
| 32 | |
| 33 | def perfetto_cc_library(**kwargs): |
| 34 | args = _merge_dicts(default_cc_args(), kwargs) |
| 35 | if not _rule_override("cc_library", **args): |
| 36 | native.cc_library(**args) |
| 37 | |
| 38 | def perfetto_cc_binary(**kwargs): |
| 39 | args = _merge_dicts(default_cc_args(), kwargs) |
| 40 | if not _rule_override("cc_binary", **args): |
| 41 | native.cc_binary(**args) |
| 42 | |
| 43 | def perfetto_py_binary(**kwargs): |
| 44 | if not _rule_override("py_binary", **kwargs): |
| 45 | native.py_binary(**kwargs) |
| 46 | |
| 47 | # +----------------------------------------------------------------------------+ |
| 48 | # | Proto-related rules | |
| 49 | # +----------------------------------------------------------------------------+ |
| 50 | |
| 51 | def perfetto_proto_library(**kwargs): |
| 52 | if not _rule_override("proto_library", **kwargs): |
| 53 | native.proto_library(**kwargs) |
| 54 | |
| 55 | def perfetto_cc_proto_library(**kwargs): |
| 56 | if not _rule_override("cc_proto_library", **kwargs): |
| 57 | native.cc_proto_library(**kwargs) |
| 58 | |
Lalit Maganti | 6560034 | 2019-09-19 21:35:35 +0100 | [diff] [blame^] | 59 | def perfetto_java_proto_library(**kwargs): |
| 60 | if not _rule_override("java_proto_library", **kwargs): |
| 61 | native.java_proto_library(**kwargs) |
| 62 | |
| 63 | # +----------------------------------------------------------------------------+ |
| 64 | # | Misc rules. | |
| 65 | # +----------------------------------------------------------------------------+ |
| 66 | |
| 67 | # Unlike all the other rules, this is an noop by default because Bazel does not |
| 68 | # support gensignature. |
| 69 | def perfetto_gensignature_internal_only(**kwargs): |
| 70 | _rule_override("gensignature_internal_only", **kwargs) |
| 71 | |
Primiano Tucci | 1d40998 | 2019-09-19 10:15:18 +0100 | [diff] [blame] | 72 | # Generates .pbzero.{cc,h} from .proto(s). We deliberately do NOT generate |
| 73 | # conventional .pb.{cc,h} from here as protozero gen sources do not have any |
| 74 | # dependency on libprotobuf. |
| 75 | def perfetto_cc_protozero_library(name, deps, **kwargs): |
| 76 | if _rule_override( |
| 77 | "cc_protozero_library", |
| 78 | name = name, |
| 79 | deps = deps, |
| 80 | **kwargs |
| 81 | ): |
| 82 | return |
| 83 | |
| 84 | proto_gen( |
| 85 | name = name + "_src", |
| 86 | deps = deps, |
| 87 | suffix = "pbzero", |
| 88 | plugin = PERFETTO_CONFIG.root + ":protozero_plugin", |
| 89 | protoc = PERFETTO_CONFIG.deps.protoc[0], |
| 90 | root = PERFETTO_CONFIG.root, |
| 91 | ) |
| 92 | |
| 93 | native.filegroup( |
| 94 | name = name + "_h", |
| 95 | srcs = [":" + name + "_src"], |
| 96 | output_group = "h", |
| 97 | ) |
| 98 | |
| 99 | perfetto_cc_library( |
| 100 | name = name, |
| 101 | srcs = [":" + name + "_src"], |
| 102 | hdrs = [":" + name + "_h"], |
| 103 | deps = [PERFETTO_CONFIG.root + ":libprotozero"], |
| 104 | **kwargs |
| 105 | ) |
| 106 | |
| 107 | # Generates .ipc.{cc,h} and .pb.{cc.h} from .proto(s). The IPC sources depend |
| 108 | # on .pb.h so we need to generate also the standard protobuf sources here. |
| 109 | def perfetto_cc_ipc_library(name, deps, **kwargs): |
| 110 | if _rule_override("cc_ipc_library", name = name, deps = deps, **kwargs): |
| 111 | return |
| 112 | |
| 113 | # Takes care of generating .pb.{cc,h}. |
| 114 | perfetto_cc_proto_library(name = name + "_pb", deps = deps) |
| 115 | |
| 116 | # Generates .ipc.{cc,h}. |
| 117 | proto_gen( |
| 118 | name = name + "_src", |
| 119 | deps = deps, |
| 120 | suffix = "ipc", |
| 121 | plugin = PERFETTO_CONFIG.root + ":ipc_plugin", |
| 122 | protoc = PERFETTO_CONFIG.deps.protoc[0], |
| 123 | root = PERFETTO_CONFIG.root, |
| 124 | ) |
| 125 | |
| 126 | native.filegroup( |
| 127 | name = name + "_h", |
| 128 | srcs = [":" + name + "_src"], |
| 129 | output_group = "h", |
| 130 | ) |
| 131 | |
| 132 | perfetto_cc_library( |
| 133 | name = name, |
| 134 | srcs = [":" + name + "_src"], |
| 135 | hdrs = [":" + name + "_h"], |
| 136 | deps = [ |
| 137 | ":" + name + "_pb", |
| 138 | |
| 139 | # Generated .ipc.{cc,h} depend on this. |
| 140 | PERFETTO_CONFIG.root + ":perfetto_ipc", |
| 141 | |
| 142 | # Generated .pb.{cc,h} depend on this. |
| 143 | ] + PERFETTO_CONFIG.deps.protobuf_lite, |
| 144 | **kwargs |
| 145 | ) |
| 146 | |
| 147 | # +----------------------------------------------------------------------------+ |
| 148 | # | Misc utility functions | |
| 149 | # +----------------------------------------------------------------------------+ |
| 150 | |
| 151 | def _rule_override(rule_name, **kwargs): |
| 152 | overrides = getattr(PERFETTO_CONFIG, "rule_overrides", struct()) |
| 153 | overridden_rule = getattr(overrides, rule_name, None) |
| 154 | if overridden_rule: |
| 155 | overridden_rule(**kwargs) |
| 156 | return True |
| 157 | return False |
| 158 | |
| 159 | def _merge_dicts(*args): |
| 160 | res = {} |
| 161 | for arg in args: |
| 162 | for k, v in arg.items(): |
| 163 | if type(v) == "string": |
| 164 | res[k] = v |
| 165 | elif type(v) == "list" or type(v) == "select": |
| 166 | res[k] = res.get(k, []) + v |
| 167 | else: |
| 168 | fail("key type not supported: " + type(v)) |
| 169 | return res |