1#!/usr/bin/env python 2#coding=utf-8 3 4# 5# Copyright (c) 2025 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 22from .base_rule import BaseRule 23 24 25class LLndkRule(BaseRule): 26 RULE_NAME = "LLndk" 27 28 def __init__(self, mgr, args): 29 super().__init__(mgr, args) 30 self.__out_path = mgr.get_product_out_path() 31 self.__white_lists = self.load_llndk_json("llndk_info.json") 32 self.__ignored_tags = ["platformsdk", "sasdk", "platformsdk_indirect", "ndk"] 33 self.__valid_mod_tags = ["llndk"] + self.__ignored_tags 34 35 def get_white_lists(self): 36 return self.__white_lists 37 38 def get_out_path(self): 39 return self.__out_path 40 41 def load_llndk_json(self, name): 42 rules_dir = [] 43 rules_dir.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "../rules")) 44 if self._args and self._args.rules: 45 self.log("****add more llndk info in:{}****".format(self._args.rules)) 46 rules_dir = rules_dir + self._args.rules 47 48 llndk_rules_path = self.get_out_path().replace("out", "out/products_ext") 49 if os.path.exists(llndk_rules_path): 50 self.log("****add more llndk info in dir:{}****".format(llndk_rules_path)) 51 rules_dir.append(llndk_rules_path) 52 else: 53 self.warn("****add llndk_rules_path path not exist: {}****".format(llndk_rules_path)) 54 res = [] 55 for d in rules_dir: 56 rules_file = os.path.join(d, self.__class__.RULE_NAME, name) 57 if os.path.isfile(rules_file): 58 res = self.__parser_rules_file(rules_file, res) 59 else: 60 self.warn("****rules path not exist: {}****".format(rules_file)) 61 62 return res 63 64 def check(self): 65 self.__modules_with_llndk_tag = [] 66 white_lists = self.get_white_lists() 67 68 for mod in self.get_mgr().get_all(): 69 if self.__is_llndk_tagged(mod): 70 self.__modules_with_llndk_tag.append(mod) 71 72 # Check if all llndk modules are correctly tagged by innerapi_tags 73 passed = self.__check_if_tagged_correctly() 74 self.log(f"****check_if_tagged_correctly result:{passed}****") 75 if not passed: 76 return passed 77 78 passed = self.check_if_deps_correctly( 79 self.__modules_with_llndk_tag, self.__valid_mod_tags, self.__valid_mod_tags, white_lists) 80 self.log(f"****check_if_deps_correctly result:{passed}****") 81 if not passed: 82 return passed 83 84 return True 85 86 def __parser_rules_file(self, rules_file, res): 87 try: 88 self.log("****Parsing rules file in {}****".format(rules_file)) 89 with open(rules_file, "r") as f: 90 contents = f.read() 91 if not contents: 92 self.log("****rules file {} is null****".format(rules_file)) 93 return res 94 json_data = json.loads(contents) 95 for so in json_data: 96 so_file_name = so.get("so_file_name") 97 if so_file_name and so_file_name not in res: 98 res.append(so_file_name) 99 except (FileNotFoundError, IOError, UnicodeDecodeError) as file_open_or_decode_err: 100 self.error(file_open_or_decode_err) 101 102 return res 103 104 def __is_llndk_tagged(self, mod): 105 if not "innerapi_tags" in mod: 106 return False 107 if "llndk" in mod["innerapi_tags"]: 108 return True 109 return False 110 111 def __check_if_tagged_correctly(self): 112 passed = True 113 114 for mod in self.__modules_with_llndk_tag: 115 if mod["name"] not in self.get_white_lists(): 116 passed = False 117 self.error('non llndk module %s with innerapi_tags="llndk", %s' 118 % (mod["name"], mod["labelPath"])) 119 120 return passed 121