1# Copyright (C) 2022 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 15def get_dep_targets(attrs, *, predicate = lambda _: True): 16 """get_dep_targets returns all targets listed in the current rule's attributes 17 18 Args: 19 attrs (dict[str, attr]): dictionary containing the rule's attributes. 20 This may come from `ctx.attr` if called from a rule, or 21 `ctx.rule.attr` if called from an aspect. 22 predicate (function(Target) -> bool): a function used to filter out 23 unwanted targets; if predicate(target) == False, then do not include 24 target 25 Returns: 26 targets (dict[str, list[Target]]): map of attr to list of Targets for which 27 predicate returns True 28 """ 29 targets = {} 30 for a in dir(attrs): 31 if a.startswith("_"): 32 # Ignore private attributes 33 continue 34 targets[a] = [] 35 value = getattr(attrs, a) 36 vlist = value if type(value) == type([]) else [value] 37 for item in vlist: 38 if type(item) == "Target" and predicate(item): 39 targets[a].append(item) 40 return targets 41 42_BP2BUILD_LABEL_SUFFIXES = [ 43 # cc rules 44 "_bp2build_cc_library_static", 45 "_cc_proto_lite", 46 "_aidl_code_gen", 47 "_cc_aidl_library", 48] 49 50def strip_bp2build_label_suffix(name): 51 for suffix in _BP2BUILD_LABEL_SUFFIXES: 52 name = name.removesuffix(suffix) 53 return name 54