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# 15 16import os 17import yaml 18 19 20def parse_hierarchy(build_top): 21 """Parse build hierarchy file from given build top directory, and returns a dict from child targets to parent targets. 22 23 Example of hierarchy file: 24 ========== 25 aosp_arm64: 26 - armv8 27 - aosp_cf_arm64_phone 28 29 armv8: 30 - aosp_oriole 31 - aosp_sunfish 32 33 aosp_oriole: 34 - oriole 35 36 aosp_sunfish: 37 - sunfish 38 39 oriole: 40 # leaf 41 42 sunfish: 43 # leaf 44 ========== 45 46 If we parse this yaml, we get a dict looking like: 47 48 { 49 "sunfish": "aosp_sunfish", 50 "oriole": "aosp_oriole", 51 "aosp_oriole": "armv8", 52 "aosp_sunfish": "armv8", 53 "armv8": "aosp_arm64", 54 "aosp_cf_arm64_phone": "aosp_arm64", 55 "aosp_arm64": None, # no parent 56 } 57 """ 58 metadata_path = os.path.join(build_top, 'tools', 'build', 'hierarchy.yaml') 59 if not os.path.isfile(metadata_path): 60 raise RuntimeError("target metadata file %s doesn't exist" % metadata_path) 61 62 with open(metadata_path, 'r') as f: 63 hierarchy_yaml = yaml.load(f, Loader=yaml.SafeLoader) 64 65 hierarchy_map = dict() 66 67 for parent_target, child_targets in hierarchy_yaml.items(): 68 if not child_targets: 69 # leaf 70 continue 71 for child_target in child_targets: 72 hierarchy_map[child_target] = parent_target 73 74 for parent_target in hierarchy_yaml: 75 # targets with no parent 76 if parent_target not in hierarchy_map: 77 hierarchy_map[parent_target] = None 78 79 return hierarchy_map 80