• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SaRule(BaseRule):
24	RULE_NAME = "NO-Depends-On-SA"
25
26	def __check_depends_on_sa(self):
27		lists = self.get_white_lists()
28
29		passed = True
30
31		sa_without_shlib_type = []
32		non_sa_with_sa_shlib_type = []
33
34		# Check if any napi modules has dependedBy
35		for mod in self.get_mgr().get_all():
36			is_sa = False
37			if "sa_id" in mod and mod["sa_id"] > 0:
38				is_sa = True
39			# Collect non SA modules with shlib_type of value "sa"
40			if not is_sa and ("shlib_type" in mod and mod["shlib_type"] == "sa"):
41				non_sa_with_sa_shlib_type.append(mod)
42
43			# Collect SA modules without shlib_type with value of "sa"
44			if is_sa and ("shlib_type" not in mod or mod["shlib_type"] != "sa"):
45				if mod["name"] not in lists:
46					sa_without_shlib_type.append(mod)
47
48			if not is_sa:
49				continue
50
51			if len(mod["dependedBy"]) == 0:
52				continue
53
54			if mod["name"] in lists:
55				continue
56
57			# If sa 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 SA modules is depended by other modules
62			self.error("sa 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(sa_without_shlib_type) > 0:
69			for mod in sa_without_shlib_type:
70				self.warn('sa module %s has no shlib_type="sa", add it in %s' % (mod["name"], mod["labelPath"]))
71
72		if len(non_sa_with_sa_shlib_type) > 0:
73			for mod in non_sa_with_sa_shlib_type:
74				self.warn('non sa module %s with shlib_type="sa", %s' % (mod["name"], mod["labelPath"]))
75
76		return passed
77
78	def check(self):
79		return self.__check_depends_on_sa()
80