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 json 20 21from .base_rule import BaseRule 22 23 24class HdiRule(BaseRule): 25 RULE_NAME = "NO-Depends-On-HDI" 26 27 def __check_depends_on_hdi(self): 28 lists = self.get_white_lists() 29 30 passed = True 31 32 hdi_without_shlib_type = [] 33 non_hdi_with_hdi_shlib_type = [] 34 35 # Check if any napi modules has dependedBy 36 for mod in self.get_mgr().get_all(): 37 is_hdi = False 38 if "hdiType" in mod and mod["hdiType"] == "hdi_service": 39 is_hdi = True 40 # Collect non HDI modules with shlib_type of value "hdi" 41 if not is_hdi and ("shlib_type" in mod and mod["shlib_type"] == "hdi"): 42 non_hdi_with_hdi_shlib_type.append(mod) 43 44 # Collect HDI modules without shlib_type with value of "hdi" 45 if is_hdi and ("shlib_type" not in mod or mod["shlib_type"] != "hdi"): 46 if mod["name"] not in lists: 47 hdi_without_shlib_type.append(mod) 48 49 if self.__ignore_mod(mod, is_hdi, lists): 50 continue 51 52 # Check if HDI modules is depended by other modules 53 self.error("hdi module %s depended by:" % mod["name"]) 54 for dep in mod["dependedBy"]: 55 caller = dep["caller"] 56 self.log(" module [%s] defined in [%s]" % (caller["name"], caller["labelPath"])) 57 passed = False 58 59 if len(hdi_without_shlib_type) > 0: 60 for mod in hdi_without_shlib_type: 61 if mod["name"] not in lists: 62 passed = False 63 self.error('hdi module %s has no shlib_type="hdi", add it in %s' % (mod["name"], mod["labelPath"])) 64 65 if len(non_hdi_with_hdi_shlib_type) > 0: 66 for mod in non_hdi_with_hdi_shlib_type: 67 self.warn('non hdi module %s with shlib_type="hdi", %s' % (mod["name"], mod["labelPath"])) 68 69 return passed 70 71 def check(self): 72 return self.__check_depends_on_hdi() 73 74 def __ignore_mod(self, mod, is_hdi, lists): 75 ignore_flag = False 76 if not is_hdi: 77 ignore_flag = True 78 return ignore_flag 79 80 if len(mod["dependedBy"]) == 0: 81 ignore_flag = True 82 return ignore_flag 83 84 if mod["name"] in lists: 85 ignore_flag = True 86 return ignore_flag 87 88 # If hdi module has version_script to specify exported symbols, it can be depended by others 89 if "version_script" in mod: 90 ignore_flag = True 91 92 return ignore_flag 93