1#!/usr/bin/env python 2#coding=utf-8 3 4# 5# Copyright (c) 2022 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20import json 21 22class CompileInfoLoader(object): 23 @staticmethod 24 def __load_output_module_info(product_out_path): 25 try: 26 with open(os.path.join(product_out_path, "packages/phone/system_module_info.json")) as f: 27 modules = json.load(f) 28 except: 29 print("file info not found.") 30 return None 31 32 res = [] 33 for item in modules: 34 info = {} 35 info["name"] = item["dest"][0] 36 if info["name"].startswith("updater/"): 37 if len(item["dest"]) > 1: 38 info["name"] = item["dest"][1] 39 else: 40 continue 41 42 if "label" in item: 43 info["labelPath"] = item["label"] 44 else: 45 info["labelPath"] = "" 46 if info["labelPath"].find("(") > 0: 47 info["labelPath"] = info["labelPath"][:info["labelPath"].find("(")] 48 if "subsystem_name" in item: 49 info["subsystem"] = item["subsystem_name"] 50 else: 51 if info["labelPath"].startswith("//build/common"): 52 info["subsystem"] = "commonlibrary" 53 else: 54 info["subsystem"] = "unknown" 55 if "part_name" in item: 56 info["componentName"] = item["part_name"] 57 else: 58 if info["labelPath"].startswith("//build/common"): 59 info["componentName"] = "c_utils" 60 else: 61 info["componentName"] = "unknown" 62 if "label_name" in item: 63 info["moduleName"] = item["label_name"] 64 else: 65 info["moduleName"] = "" 66 if "version_script" in item: 67 info["version_script"] = item["version_script"] 68 info["third_party"] = False 69 info["chipset"] = False 70 info["napi"] = False 71 info["innerapi"] = False 72 info["innerapi_declared"] = False 73 if "shlib_type" in item: 74 info["shlib_type"] = item["shlib_type"] 75 if "innerapi_tags" in item: 76 info["innerapi_tags"] = item["innerapi_tags"] 77 info["sa_id"] = 0 78 res.append(info) 79 return res 80 81 @staticmethod 82 def load(mgr, product_out_path): 83 info = CompileInfoLoader.__load_output_module_info(product_out_path) 84 85 defaultInfo = { 86 "subsystem": "unknown", 87 "componentName": "unknown", 88 "moduleName": "unknown", 89 "third_party": False, 90 "chipset": False, 91 "napi": False, 92 "sa_id": 0, 93 "labelPath": "", 94 "version_script": "", 95 "shlib_type": "", 96 "innerapi": False, 97 "innerapi_tags": [], 98 "innerapi_declared": False 99 } 100 101 if info: 102 for item in info: 103 elf = mgr.get_elf_by_path(item["name"]) 104 if not elf: 105 continue 106 for k in defaultInfo.keys(): 107 if k in item: 108 elf[k] = item[k] 109 110 unknown_items = [] 111 for elf in mgr.get_all(): 112 if "componentName" not in elf: 113 print("%s does not match in module info file" % (elf["path"])) 114 unknown = defaultInfo.copy() 115 unknown["name"] = elf["path"] 116 unknown["fileName"] = elf["name"] 117 for k in defaultInfo.keys(): 118 elf[k] = defaultInfo[k] 119 unknown_items.append(unknown) 120 elif elf["componentName"] == "unknown": 121 print("%s has no componentName info" % (elf["path"])) 122 unknown = defaultInfo.copy() 123 unknown["name"] = elf["path"] 124 for k in defaultInfo.keys(): 125 if k in elf: 126 defaultInfo[k] = elf[k] 127 unknown_items.append(unknown) 128 129 if elf["path"].startswith("system/lib64/module/") or elf["path"].startswith("system/lib/module/"): 130 elf["napi"] = True 131 132 if not elf["path"].startswith("system/"): 133 elf["chipset"] = True 134 135 # Add if not exists 136 if "shlib_type" not in elf: 137 elf["shlib_type"] = "" 138 if "innerapi_tags" not in elf: 139 elf["innerapi_tags"] = [] 140 if elf["labelPath"].startswith("//third_party/"): 141 elf["third_party"] = True 142 143 if len(unknown_items) > 0: 144 print("%d modules has no component info" % len(unknown_items)) 145 with open(os.path.join(product_out_path, "unknown.json"), "w") as f: 146 res = json.dumps(unknown_items, indent=4) 147 f.write(res) 148 149 # init platformsdk, chipsetsdk, innerapi flags 150 for elf in mgr.get_all(): 151 elf["deps_internal"] = [] 152 elf["deps_external"] = [] 153 elf["dependedBy_internal"] = [] 154 elf["dependedBy_external"] = [] 155 156 elf["modGroup"] = "private" 157 elf["platformsdk"] = False 158 elf["chipsetsdk"] = False 159 160 elf["hdiType"] = "" 161 if elf["shlib_type"] == "hdi_proxy": 162 elf["hdiType"] = "hdi_proxy" # HDI proxy client library 163 elif elf["shlib_type"] == "hdi_stub": 164 elf["hdiType"] = "hdi_stub" # HDI proxy client library 165 166 if elf["name"] in ("libc.so", "libc++.so", "libhilog.so"): 167 elf["innerapi"] = True 168 169 # Highest priority 170 if elf["napi"]: 171 elf["modGroup"] = "publicapi" 172 173 if elf["sa_id"] > 0 or elf["type"] == "bin": 174 elf["modGroup"] = "pentry" 175 176 # for component dependedBy_internal and dependedBy_external 177 178 platformsdks = [] 179 chipsetsdks = [] 180 innerapi_ccs = [] 181 182 for dep in mgr.get_all_deps(): 183 caller = dep["caller"] 184 callee = dep["callee"] 185 186 dep["platformsdk"] = False 187 dep["chipsetsdk"] = False 188 dep["external"] = False 189 190 # For Inner API modules detection 191 if caller["componentName"] == callee["componentName"]: 192 caller["deps_internal"].append(dep) 193 callee["dependedBy_internal"].append(dep) 194 else: 195 caller["deps_external"].append(dep) 196 callee["dependedBy_external"].append(dep) 197 callee["innerapi"] = True 198 dep["external"] = True 199 200 callee["modGroup"] = "innerapi_cc" # Cross component 201 202 if caller["napi"]: 203 caller["modGroup"] = "publicapi" 204 205 # For Platform SDK modules detection 206 callee["modGroup"] = "innerapi_chc" # Cross high level component 207 208 dep["platformsdk"] = True 209 callee["platformsdk"] = True 210 if callee not in platformsdks: 211 platformsdks.append(callee) 212 elif caller["chipset"] != callee["chipset"]: 213 # For Chipset SDK modules detection 214 if callee["modGroup"] not in ("publicapi", "pentry"): 215 callee["modGroup"] = "innerapi_chc" # Cross high level component 216 if callee["hdiType"] != "hdi_proxy": # hdi proxy modules can be called by both system and chipset 217 dep["chipsetsdk"] = True 218 callee["chipsetsdk"] = True 219 if callee not in chipsetsdks: 220 chipsetsdks.append(callee) 221 elif dep["external"] == True: 222 if callee not in innerapi_ccs: 223 innerapi_ccs.append(callee) 224 225 # Highest priority 226 if caller["napi"]: 227 caller["modGroup"] = "publicapi" 228 if callee["napi"]: 229 callee["modGroup"] = "publicapi" 230 231 if caller["sa_id"] > 0 or caller["type"] == "bin": 232 caller["modGroup"] = "pentry" 233 if callee["sa_id"] > 0 or callee["type"] == "bin": 234 callee["modGroup"] = "pentry" 235 236if __name__ == "__main__": 237 import sqlite3 238 import elf_modules 239 conn = sqlite3.connect("symdb.db") 240 cursor = conn.cursor() 241 242 mgr = elf_modules.ElfModuleMgr(cursor) 243