• 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 os
20import json
21
22from .base_rule import BaseRule
23
24class ChipsetSDKRule(BaseRule):
25	RULE_NAME = "ChipsetSDK"
26
27	def __is_chipsetsdk_tagged(self, mod):
28		if not "innerapi_tags" in mod:
29			return False
30		if "ndk" in mod["innerapi_tags"]:
31			return True
32		if "chipsetsdk" in mod["innerapi_tags"]:
33			return True
34		return False
35
36	def __write_innerkits_header_files(self, chipsetsdks):
37		inner_kits_info = os.path.join(self.get_mgr().get_product_out_path(), "build_configs/parts_info/inner_kits_info.json")
38		with open(inner_kits_info, "r") as f:
39			info = json.load(f)
40
41		headers = []
42		for sdk in chipsetsdks:
43			path = sdk["labelPath"][:sdk["labelPath"].find(":")]
44			item = {"chipsetsdk": sdk["name"], "path": path, "headers": []}
45			if sdk["componentName"] not in info:
46				headers.append(item)
47				continue
48
49			for name, innerapi in info[sdk["componentName"]].items():
50				if innerapi["label"] != sdk["labelPath"]:
51					continue
52				gotHeaders = True
53				base = innerapi["header_base"]
54				for f in innerapi["header_files"]:
55					item["headers"].append(os.path.join(base, f))
56			headers.append(item)
57
58		try:
59			with open(os.path.join(self.get_mgr().get_product_images_path(), "chipsetsdk_info.json"), "w") as f:
60				json.dump(headers, f, indent = 4)
61		except:
62			pass
63
64		return headers
65
66	def __check_depends_on_chipsetsdk(self):
67		lists = self.get_white_lists()
68
69		passed = True
70
71		chipsetsdks = []
72		modules_with_chipsetsdk_tag = []
73
74		# Check if any napi modules has dependedBy
75		for mod in self.get_mgr().get_all():
76			if self.__is_chipsetsdk_tagged(mod):
77				modules_with_chipsetsdk_tag.append(mod)
78
79			# Check chipset modules only
80			if mod["path"].startswith("system"):
81				continue
82
83			# Check chipset modules depends
84			for dep in mod["deps"]:
85				callee = dep["callee"]
86
87				# If callee is chipset module, it is OK
88				if not callee["path"].startswith("system"):
89					continue
90
91				if callee not in chipsetsdks:
92					if "hdiType" not in callee or callee["hdiType"] != "hdi_proxy":
93						chipsetsdks.append(callee)
94				# If callee is in Chipset SDK white list module, it is OK
95				if callee["name"] in lists:
96					continue
97
98				# If callee is asan library, it is OK
99				if callee["name"].endswith(".asan.so"):
100					continue
101
102				# If callee is hdi proxy module, it is OK
103				if "hdiType" in callee and callee["hdiType"] == "hdi_proxy":
104					continue
105
106				# Not allowed
107				passed = False
108				self.error("chipset module %s depends on non Chipset SDK module %s in %s" % (mod["name"], callee["name"], mod["labelPath"]))
109
110			# Check chipset modules dependedBy
111			for dep in mod["dependedBy"]:
112				caller = dep["caller"]
113
114				# Called by chipset module, it is OK
115				if not caller["path"].startswith("system"):
116					continue
117
118				if mod not in chipsetsdks:
119					chipsetsdks.append(mod)
120
121				# If chipset module is in Chipset SDK white list module, it is OK
122				if mod["name"] in lists:
123					continue
124
125				# Not allowed
126				passed = False
127				self.error("system module %s depends on chipset module %s in %s" % (caller["name"], mod["name"], caller["labelPath"]))
128
129		for mod in chipsetsdks:
130			if not self.__is_chipsetsdk_tagged(mod):
131				self.warn('Chipset SDK module %s has no innerapi_tags with "chipsetsdk" or "csdk", add it in %s' % (mod["name"], mod["labelPath"]))
132
133		for mod in modules_with_chipsetsdk_tag:
134			if mod["name"] not in lists:
135				passed = False
136				self.error('non chipsetsdk module %s with innerapi_tags="chipsetsdk" or "csdk", %s' % (mod["name"], mod["labelPath"]))
137
138		self.__write_innerkits_header_files(chipsetsdks)
139
140		return passed
141
142	def check(self):
143		return self.__check_depends_on_chipsetsdk()
144