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