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