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(self): 28 return self.__check_depends_on_hdi() 29 30 def __check_depends_on_hdi(self): 31 lists = self.get_white_lists() 32 33 passed = True 34 35 hdi_without_shlib_type = [] 36 non_hdi_with_hdi_shlib_type = [] 37 38 # Check if any napi modules has dependedBy 39 for mod in self.get_mgr().get_all(): 40 is_hdi = False 41 if "hdiType" in mod and mod["hdiType"] == "hdi_service": 42 is_hdi = True 43 # Collect non HDI modules with shlib_type of value "hdi" 44 if not is_hdi and ("shlib_type" in mod and mod["shlib_type"] == "hdi"): 45 non_hdi_with_hdi_shlib_type.append(mod) 46 47 # Collect HDI modules without shlib_type with value of "hdi" 48 if is_hdi and ("shlib_type" not in mod or mod["shlib_type"] != "hdi"): 49 if mod["name"] not in lists: 50 hdi_without_shlib_type.append(mod) 51 52 if self.__ignore_mod(mod, is_hdi, lists): 53 continue 54 55 # Check if HDI modules is depended by other modules 56 self.error("hdi module %s depended by:" % mod["name"]) 57 for dep in mod["dependedBy"]: 58 caller = dep["caller"] 59 self.log(" module [%s] defined in [%s]" % (caller["name"], caller["labelPath"])) 60 passed = False 61 62 if len(hdi_without_shlib_type) > 0: 63 for mod in hdi_without_shlib_type: 64 if mod["name"] not in lists: 65 passed = False 66 self.error('hdi module %s has no shlib_type="hdi", add it in %s' % (mod["name"], mod["labelPath"])) 67 68 if len(non_hdi_with_hdi_shlib_type) > 0: 69 for mod in non_hdi_with_hdi_shlib_type: 70 self.warn('non hdi module %s with shlib_type="hdi", %s' % (mod["name"], mod["labelPath"])) 71 72 return passed 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