diff --git a/tools/hdf_dev_eco_tool/command_line/__init__.py b/tools/hdf_dev_eco_tool/command_line/__init__.py new file mode 100755 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_add_handler.py b/tools/hdf_dev_eco_tool/command_line/hdf_add_handler.py new file mode 100755 index 0000000000000000000000000000000000000000..53dd1ffbc62d85c715d788fb70bc922a23b4e613 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_add_handler.py @@ -0,0 +1,331 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import json +import platform +from string import Template + +from .hdf_command_handler_base import HdfCommandHandlerBase +from .hdf_command_error_code import CommandErrorCode +from .hdf_device_info_hcs import HdfDeviceInfoHcsFile +from .hdf_vendor_build_file import HdfVendorBuildFile +from .hdf_vendor_kconfig_file import HdfVendorKconfigFile +from .hdf_vendor_mk_file import HdfVendorMkFile +from .hdf_driver_config_file import HdfDriverConfigFile +from hdf_tool_settings import HdfToolSettings +from hdf_tool_exception import HdfToolException +from .hdf_vendor_makefile import HdfVendorMakeFile +from .hdf_defconfig_patch import HdfDefconfigAndPatch +import hdf_utils + + +class HdfAddHandler(HdfCommandHandlerBase): + def __init__(self, args): + super(HdfAddHandler, self).__init__() + self.cmd = 'add' + self.handlers = { + 'vendor': self._add_vendor_handler, + 'module': self._add_module_handler, + 'driver': self._add_driver_handler, + 'config': self._add_config_handler, + } + self.parser.add_argument("--action_type", + help=' '.join(self.handlers.keys()), + required=True) + self.parser.add_argument("--root_dir", required=True) # 路径 + self.parser.add_argument("--vendor_name") # 厂商 + self.parser.add_argument("--module_name") # 模块名 + self.parser.add_argument("--driver_name") # 驱动名称 + self.parser.add_argument("--board_name") # 板子名称 + self.parser.add_argument("--kernel_name") # 内核名称 + self.args = self.parser.parse_args(args) + + @staticmethod + def _render(template_path, output_path, data_model): + if not os.path.exists(template_path): + return + raw_content = hdf_utils.read_file(template_path) + contents = Template(raw_content).safe_substitute(data_model) + hdf_utils.write_file(output_path, contents) + + def _file_gen_lite(self, template, out_dir, filename, model): + templates_dir = hdf_utils.get_templates_lite_dir() + template_path = os.path.join(templates_dir, template) + file_path = os.path.join(out_dir, filename) + self._render(template_path, file_path, model) + + def _add_vendor_handler(self): + self.check_arg_raise_if_not_exist("vendor_name") + root, vendor, _, _, board = self.get_args() + target_dir = hdf_utils.get_vendor_hdf_dir(root, vendor) + if os.path.exists(target_dir): + raise HdfToolException( + "%s already exists" % + target_dir, CommandErrorCode.TARGET_ALREADY_EXIST) + os.makedirs(target_dir) + self._file_gen_lite('hdf_vendor_kconfig.template', target_dir, + 'Kconfig', {}) + board_parent_path = HdfToolSettings().get_board_parent_path(board) + if not board_parent_path: + board_parent_path = 'vendor/hisilicon' + data_model = { + "board_parent_path": board_parent_path + } + self._file_gen_lite('hdf_vendor_mk.template', target_dir, + 'hdf_vendor.mk', data_model) + + def _add_module_handler(self): + self.check_arg_raise_if_not_exist("vendor_name") + self.check_arg_raise_if_not_exist("module_name") + self.check_arg_raise_if_not_exist("kernel_name") + self.check_arg_raise_if_not_exist("board_name") + self.check_arg_raise_if_not_exist("driver_name") + + args_tuple = self.get_args() + root, vendor, module, driver, board, kernel = args_tuple + converter = hdf_utils.WordsConverter(self.args.module_name) + driver_name_converter = hdf_utils.WordsConverter(self.args.driver_name) + framework_hdf = hdf_utils.get_vendor_hdf_dir_framework(root) + if not os.path.exists(framework_hdf): + raise HdfToolException( + ' framework model path "%s" not exist' % + framework_hdf, CommandErrorCode.TARGET_NOT_EXIST) + # 在 framework 目录下创建对应的 module 的文件目录用于存放 .c 驱动文件 + framework_drv_root_dir = hdf_utils.get_drv_root_dir( + root, vendor, module) + if os.path.exists(framework_drv_root_dir): + raise HdfToolException('module "%s" already exist' % module, + CommandErrorCode.TARGET_ALREADY_EXIST) + os.makedirs(framework_drv_root_dir) + + # 创建 .c 模板驱动 + state, driver_file_path = self._add_driver_handler(*args_tuple) + if not state: + raise HdfToolException( + 'create drivers file fail "%s" ' % + driver_file_path.split("\\")[-1]) + adapter_hdf = hdf_utils.get_vendor_hdf_dir_adapter(root, kernel) + if not os.path.exists(adapter_hdf): + raise HdfToolException( + ' adapter model path "%s" not exist' % + adapter_hdf, CommandErrorCode.TARGET_NOT_EXIST) + + # 创建 adapter 路径下的 module 文件夹 + adapter_model_path = os.path.join(adapter_hdf, 'model', module) + if not os.path.exists(adapter_model_path): + os.makedirs(adapter_model_path) + + data_model = { + "module_upper_case": converter.upper_case(), + "module_lower_case": converter.lower_case(), + "driver_file_name": ("%s_driver.c" % + driver_name_converter.lower_case()), + "driver_name": driver_name_converter.lower_case() + } + # 创建 adapter 下的 module中的三个文件 + if kernel == 'liteos': + file_path, model_level_config_file_path = \ + self._add_module_handler_liteos( + framework_hdf, adapter_model_path, + data_model, converter, *args_tuple) + elif kernel == "linux": + file_path, model_level_config_file_path = \ + self._add_module_handler_linux( + framework_hdf, adapter_model_path, + data_model, *args_tuple) + else: + file_path = {} + model_level_config_file_path = {} + config_item = { + 'module_name': module, + 'module_path': file_path, + 'driver_name': "%s_driver.c" % driver, + 'driver_file_path': driver_file_path, + 'enabled': True + } + config_file_out = { + 'module_name': module, + 'module_path': file_path, + 'driver_name': driver_file_path.split("\\")[-1], + 'driver_file_path': driver_file_path, + 'module_level_config_path': model_level_config_file_path + } + config_file = hdf_utils.read_file( + os.path.join('resources', 'create_model.config')) + config_file_json = json.loads(config_file) + config_file_json[module] = config_file_out + if platform.system() == "Windows": + config_file_replace = json.dumps(config_file_json, indent=4).\ + replace(root.replace('\\', '\\\\') + '\\\\', "") + hdf_utils.write_file( + os.path.join('resources', 'create_model.config'), + config_file_replace.replace('\\\\', '/')) + if platform.system() == "Linux": + config_file_replace = json.dumps(config_file_json, indent=4).\ + replace(root + '/', "") + hdf_utils.write_file( + os.path.join('resources', 'create_model.config'), + config_file_replace) + return json.dumps(config_item) + + def _add_module_handler_liteos(self, framework_hdf, adapter_model_path, + data_model, converter, *args_tuple): + root, vendor, module, driver, board, kernel = args_tuple + liteos_file_path = {} + liteos_level_config_file_path = {} + liteos_file_name = ['BUILD.gn', 'Kconfig', 'Makefile'] + template_path = "/".join([framework_hdf] + ["tools", + "hdf_dev_eco_tool", "resources", "templates", "lite"]) + for file_name in liteos_file_name: + for i in os.listdir(template_path): + if i.find(file_name.split(".")[0]) > 0: + out_path = os.path.join(adapter_model_path, file_name) + self._render(os.path.join(template_path, i) + , out_path, data_model) + liteos_file_path[file_name] = out_path + # 修改 liteos 下的 Kconfig 文件 + vendor_k = HdfVendorKconfigFile(root, vendor, kernel, path="") + vendor_k_path = vendor_k.add_module([module, 'Kconfig']) + liteos_level_config_file_path[module+"_Kconfig"] = vendor_k_path + + # 修改 liteos 下的 hdf_lite.mk 文件 + vendor_mk = HdfVendorMkFile(root, vendor) + vendor_mk_path = vendor_mk.add_module(module) + liteos_level_config_file_path[module + "_hdf_lite"] = vendor_mk_path + + # 修改 liteos 下的 Build.gn 文件 + vendor_gn = HdfVendorBuildFile(root, vendor) + vendor_gn_path = vendor_gn.add_module(module) + liteos_level_config_file_path[module + "Build"] = vendor_gn_path + + # 修改 vendor/hisilicon/hispark_taurus/hdf_config/ + # device_info 下的 device_info.hcs 文件 + device_info = HdfDeviceInfoHcsFile( + root, vendor, module, board, driver, path="") + hcs_file_path = device_info.add_model_hcs_file_config() + liteos_file_path["devices_info.hcs"] = hcs_file_path + + # 修改 dot_configs 的配置文件 + dot_file_list = hdf_utils.get_dot_configs_path(root, vendor, board) + template_string = "LOSCFG_DRIVERS_HDF_${module_upper_case}=y\n" + new_demo_config = Template(template_string).substitute( + {"module_upper_case": converter.upper_case()}) + for dot_file in dot_file_list: + file_lines = hdf_utils.read_file_lines(dot_file) + file_lines[-1] = file_lines[-1].strip() + "\n" + if new_demo_config != file_lines[-1]: + file_lines.append(new_demo_config) + hdf_utils.write_file_lines(dot_file, file_lines) + liteos_level_config_file_path[module + "_dot_configs"] = dot_file_list + return liteos_file_path, liteos_level_config_file_path + + def _add_module_handler_linux(self, framework_hdf, adapter_model_path, + data_model, *args_tuple): + root, vendor, module, driver, board, kernel = args_tuple + linux_file_path = {} + linux_level_config_file_path = {} + linux_file_name = ['Kconfig', 'Makefile'] + template_path = "/".join([framework_hdf] + + ["tools", "hdf_dev_eco_tool", + "resources", "templates", "lite"]) + for file_name in linux_file_name: + for i in hdf_utils.template_filename_filtrate( + template_path, kernel): + if i.find(file_name.split(".")[0]) > 0: + out_path = os.path.join(adapter_model_path, file_name) + self._render(os.path.join(template_path, i), + out_path, data_model) + linux_file_path[file_name] = out_path + # 修改 linux 下的 Kconfig 文件 + vendor_k = HdfVendorKconfigFile(root, vendor, kernel, path="") + vendor_k_path = vendor_k.add_module([module, 'Kconfig']) + linux_level_config_file_path[module + "_Kconfig"] = vendor_k_path + + # 修改 linux 下的 Makefile 文件 + vendor_mk = HdfVendorMakeFile(root, vendor, kernel, path='') + vendor_mk_path = vendor_mk.add_module(data_model) + linux_level_config_file_path[module + "_Makefile"] = vendor_mk_path + + # 修改 vendor/hisilicon/hispark_taurus_linux/ + # hdf_config/device_info 下的 device_info.hcs 文件 + device_info = HdfDeviceInfoHcsFile( + root, vendor, module, board, driver, path="") + hcs_file_path = device_info.add_model_hcs_file_config() + linux_file_path["devices_info.hcs"] = hcs_file_path + + # 修改 dot_configs 的配置文件 + template_string = "CONFIG_DRIVERS_HDF_${module_upper_case}=y\n" + new_demo_config = Template(template_string).substitute(data_model) + defconfig_patch = HdfDefconfigAndPatch( + root, vendor, kernel, board, + data_model, new_demo_config) + + config_path = defconfig_patch.get_config_config() + patch_list = defconfig_patch.add_module(config_path) + config_path = defconfig_patch.get_config_patch() + defconfig_list = defconfig_patch.add_module(config_path) + linux_level_config_file_path[module + "_dot_configs"] = \ + list(set(patch_list + defconfig_list)) + return linux_file_path, linux_level_config_file_path + + def _add_driver_handler(self, *args_tuple): + root, vendor, module, driver, board, kernel = args_tuple + drv_converter = hdf_utils.WordsConverter(self.args.driver_name) + drv_src_dir = hdf_utils.get_drv_src_dir(root, vendor, module) + if os.path.exists(os.path.join(drv_src_dir, '%s_driver.c' % driver)): + raise HdfToolException( + 'driver "%s" already exist' % + driver, CommandErrorCode.TARGET_ALREADY_EXIST) + data_model = { + 'driver_lower_case': drv_converter.lower_case(), + 'driver_upper_camel_case': drv_converter.upper_camel_case(), + 'driver_lower_camel_case': drv_converter.lower_camel_case(), + 'driver_upper_case': drv_converter.upper_case() + } + self._file_gen_lite('hdf_driver.c.template', drv_src_dir, + '%s_driver.c' % driver, data_model) + driver_file_path = os.path.join( + drv_src_dir, '%s_driver.c' % driver) + return True, driver_file_path + + def _add_config_handler(self): + self.check_arg_raise_if_not_exist("module_name") + self.check_arg_raise_if_not_exist("driver_name") + self.check_arg_raise_if_not_exist("board_name") + root, _, module, driver, board = self.get_args() + drv_config = HdfDriverConfigFile(root, board, module, driver) + drv_config.create_driver() + return drv_config.get_drv_config_path() + + diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_command_error_code.py b/tools/hdf_dev_eco_tool/command_line/hdf_command_error_code.py new file mode 100755 index 0000000000000000000000000000000000000000..13162713803e2cde9788ef03cce564290185ed33 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_command_error_code.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +class CommandErrorCode(object): + MESSAGE_FORMAT_WRONG = 1001 + INTERFACE_ERROR = 1002 + TARGET_NOT_EXIST = 1003 + TARGET_ALREADY_EXIST = 1004 + FILE_FORMAT_WRONG = 1005 + UNKNOWN_ERROR = 1111 diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_command_handler_base.py b/tools/hdf_dev_eco_tool/command_line/hdf_command_handler_base.py new file mode 100755 index 0000000000000000000000000000000000000000..93eee0c1c53b89c93e5c4d40e12dc757424f53bd --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_command_handler_base.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os + +from .hdf_command_error_code import CommandErrorCode +from .hdf_tool_argument_parser import HdfToolArgumentParser +from hdf_tool_exception import HdfToolException +import hdf_utils + + +class HdfCommandHandlerBase(object): + def __init__(self): + self.cmd = 'base' + self.action_type = 'base_action_type' + self.handlers = {} + self.args = {} + self.parser = HdfToolArgumentParser() + + def run(self): + self.action_type = self._get_action_type() + if self.action_type in self.handlers: + return self.handlers[self.action_type]() + else: + raise HdfToolException( + 'unknown action_type: "%s" for "%s" cmd' % + (self.action_type, self.cmd), + CommandErrorCode.INTERFACE_ERROR) + + def _get_action_type(self): + try: + return getattr(self.args, 'action_type') + except AttributeError: + return '' + + def check_arg_raise_if_not_exist(self, arg): + try: + value = getattr(self.args, arg) + if not value: + raise AttributeError() + except AttributeError: + raise HdfToolException( + 'argument "--%s" is required for "%s" cmd' % + (arg, self.cmd), CommandErrorCode.INTERFACE_ERROR) + + def _get_arg(self, arg): + try: + value = getattr(self.args, arg) + return value + except AttributeError: + return '' + + def get_args(self): + args = ['vendor_name', 'module_name', + 'driver_name', 'board_name', 'kernel_name'] + ret = [self._get_arg('root_dir')] + for arg in args: + value = self._get_arg(arg) + if value: + value = hdf_utils.WordsConverter(value).lower_case() + ret.append(value) + return tuple(ret) + + @staticmethod + def check_path_raise_if_not_exist(full_path): + if not os.path.exists(full_path): + raise HdfToolException( + '%s not exist' % + full_path, CommandErrorCode.TARGET_NOT_EXIST) diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_command_line_server.py b/tools/hdf_dev_eco_tool/command_line/hdf_command_line_server.py new file mode 100755 index 0000000000000000000000000000000000000000..060c0c9fa7e98514dc66a625298501d7810ff36b --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_command_line_server.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import sys +import argparse + +from .hdf_tool_commands import HdfToolCommands +from .hdf_tool_argument_parser import HdfToolArgumentParser +from command_line.hdf_command_error_code import CommandErrorCode +from hdf_tool_exception import HdfToolException + + +class Message(object): + TYPE_REQ = 'request' + TYPE_QUIT = 'quit' + TYPE_ERROR = 'error' + TYPE_SUCCESS = 'success' + + def __init__(self, msg_type, content): + self.msg_type = msg_type + self.content = content + + def is_quit(self): + return self.msg_type.lower() == Message.TYPE_ERROR + + +def pack_message(msg_type, content, err_code=None): + body = bytearray(content, 'utf-8') + if not err_code: + header = bytearray('{},{}\n'.format(msg_type, len(body)), 'utf-8') + else: + header = bytearray('{},{},{}\n'.format(msg_type, len(body), err_code), + 'utf-8') + bytes_packet = bytearray().join([header, body]) + return bytes_packet + + +def decode_header(header): + header_parts = header.split(',') + if len(header_parts) < 2: + return -1, '', 0 + msg_type = header_parts[0] + body_len = int(header_parts[1]) + return 0, msg_type, body_len + + +def decode_body(body): + arg_parser = HdfToolArgumentParser() + arg_parser.add_argument('cmd') + arg_parser.add_argument('remainder_args', nargs=argparse.REMAINDER) + args = arg_parser.parse_args(body.strip().split(' ')) + remainder = [arg for arg in args.remainder_args if len(arg) != 0] + return args.cmd, remainder + + +class HdfCommandLineServer(object): + def __init__(self, input_obj, output_obj): + self.input_obj = input_obj + self.output_obj = output_obj + self.commands = HdfToolCommands() + + def _send_back(self, msg_type, content, error_code=None): + message_bytes = pack_message(msg_type, content, error_code) + self.output_obj.write(message_bytes) + self.output_obj.flush() + + def _send_back_success(self, content): + self._send_back(Message.TYPE_SUCCESS, content) + + def _send_back_error(self, error_code, content): + self._send_back(Message.TYPE_ERROR, content, error_code) + + def _read_header(self): + head_bytes = self.input_obj.readline() + msg_header = str(head_bytes, encoding="utf-8") + return decode_header(msg_header) + + def _read_body(self, body_len): + body_bytes = self.input_obj.read(body_len) + body = str(body_bytes, encoding='utf-8') + return decode_body(body) + + def run(self): + while True: + try: + ret, msg_type, body_len = self._read_header() + if ret != 0: + err_code = CommandErrorCode.MESSAGE_FORMAT_WRONG + self._send_back_error(err_code, 'header wrong') + continue + if msg_type == Message.TYPE_QUIT: + self._send_back_success('Bye bye!') + break + if body_len < 0: + err_code = CommandErrorCode.MESSAGE_FORMAT_WRONG + self._send_back_error(err_code, 'body len wrong') + continue + cmd, args = self._read_body(body_len) + ret = self.commands.run(cmd, args) + if ret: + self._send_back_success(str(ret)) + else: + self._send_back_success('') + except HdfToolException as exc: + try: + self._send_back_error(exc.error_code, exc.exc_msg) + except OSError: + sys.exit(-1) + except Exception as exc: + try: + self._send_back_error(CommandErrorCode.UNKNOWN_ERROR, + str(exc)) + except OSError: + sys.exit(-1) diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_defconfig_patch.py b/tools/hdf_dev_eco_tool/command_line/hdf_defconfig_patch.py new file mode 100755 index 0000000000000000000000000000000000000000..a90ace3c297f2e7ff88fb545b5bc78cb3643dc28 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_defconfig_patch.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re +from string import Template + +from hdf_tool_settings import HdfToolSettings +from .hdf_command_error_code import CommandErrorCode +from hdf_tool_exception import HdfToolException +import hdf_utils + + +class HdfDefconfigAndPatch(object): + def __init__(self, root, vendor, kernel, board, data_model, new_demo_config): + self.root = root + self.vendor = vendor + self.kernel = kernel + self.board = board + self.data_model = data_model + self.new_demo_config = new_demo_config + self.file_path = hdf_utils.get_template_file_path(root, vendor) + self.drivers_path_list = HdfToolSettings().get_board_parent_file(self.board) + + def get_config_config(self): + return os.path.join(self.root, "kernel", self.kernel, "config") + + def get_config_patch(self): + return os.path.join(self.root, "kernel", self.kernel, "patches") + + def add_module(self, path, files=[]): + for filename in os.listdir(path): + new_path = os.path.join(path, filename) + if not os.path.isdir(new_path): + self.find_file(new_path, files) + else: + self.add_module(new_path, files) + return files + + def delete_module(self, path): + lines = hdf_utils.read_file_lines(path) + if self.new_demo_config in lines or ("+" + self.new_demo_config) in lines: + if path.split(".")[-1] != "patch": + lines.remove(self.new_demo_config) + else: + lines.remove("+" + self.new_demo_config) + hdf_utils.write_file_lines(path, lines) + + def rename_vendor(self): + pattern = r'vendor/([a-zA-Z0-9_\-]+)/' + replacement = 'vendor/%s/' % self.vendor + new_content = re.sub(pattern, replacement, self.contents) + hdf_utils.write_file(self.file_path, new_content) + + def find_file(self, new_path, files): + if new_path.split("\\")[-1] in self.drivers_path_list or \ + new_path.split("/")[-1] in self.drivers_path_list: + files.append(new_path) + with open(new_path, "r+", encoding="utf-8") as f: + data = f.readlines() + insert_index = None + state = False + for index, line in enumerate(data): + if line.find("CONFIG_DRIVERS_HDF_INPUT=y") >= 0: + insert_index = index + elif line.find(self.new_demo_config) >= 0: + state = True + if not state: + if new_path.split(".")[-1] != "patch": + data.insert(insert_index + 1, + self.new_demo_config) + else: + data.insert(insert_index + 1, + "+" + self.new_demo_config) + with open(new_path, "w", encoding="utf-8") as f: + f.writelines(data) diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_delete_handler.py b/tools/hdf_dev_eco_tool/command_line/hdf_delete_handler.py new file mode 100755 index 0000000000000000000000000000000000000000..b4046e51b782bd8af80d5f7f8fc412b3833825b1 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_delete_handler.py @@ -0,0 +1,223 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + + +import json +import os +import shutil +from string import Template + +from hdf_tool_exception import HdfToolException +from .hdf_command_error_code import CommandErrorCode +from .hdf_command_handler_base import HdfCommandHandlerBase +from .hdf_defconfig_patch import HdfDefconfigAndPatch +from .hdf_device_info_hcs import HdfDeviceInfoHcsFile +from .hdf_vendor_build_file import HdfVendorBuildFile +from .hdf_vendor_kconfig_file import HdfVendorKconfigFile +from .hdf_vendor_makefile import HdfVendorMakeFile +from .hdf_vendor_mk_file import HdfVendorMkFile +from .hdf_module_kconfig_file import HdfModuleKconfigFile +from .hdf_module_mk_file import HdfModuleMkFile +from .hdf_driver_config_file import HdfDriverConfigFile +import hdf_utils + + +class HdfDeleteHandler(HdfCommandHandlerBase): + def __init__(self, args): + super(HdfDeleteHandler, self).__init__() + self.cmd = 'delete' + self.handlers = { + 'vendor': self._delete_vendor_handler, + 'module': self._delete_module_handler, + 'driver': self._delete_driver_handler, + } + self.parser.add_argument("--action_type", + help=' '.join(self.handlers.keys()), + required=True) + self.parser.add_argument("--root_dir", required=True) + self.parser.add_argument("--vendor_name") + self.parser.add_argument("--module_name") + self.parser.add_argument("--driver_name") + self.parser.add_argument("--board_name") + self.parser.add_argument("--kernel_name") + self.args = self.parser.parse_args(args) + + def _delete_vendor_handler(self): + self.check_arg_raise_if_not_exist("vendor_name") + self.check_arg_raise_if_not_exist("board_name") + root, vendor, _, _, _ = self.get_args() + vendor_hdf_dir = hdf_utils.get_vendor_hdf_dir(root, vendor) + print(vendor_hdf_dir) + if not os.path.exists(vendor_hdf_dir): + return + for module in os.listdir(vendor_hdf_dir): + mod_dir = os.path.join(vendor_hdf_dir, module) + if os.path.isdir(mod_dir): + self._delete_module(root, module) + shutil.rmtree(vendor_hdf_dir) + + def _delete_module(self, root, model, model_info): + for key, path_value in model_info.items(): + if key.split("_")[-1] == "name": + pass + elif key == "driver_file_path": + driver_file = os.path.join( + root, path_value.split(model)[0], model) + if os.path.exists(driver_file): + shutil.rmtree(driver_file) + else: + self._delete_file_func(root, key, model_info, model) + create_model_data = self._delete_model_info() + delete_model_info = hdf_utils.get_create_model_info( + root=root, create_data=json.dumps(create_model_data)) + return delete_model_info + + def _delete_model_info(self): + self.check_arg_raise_if_not_exist("root_dir") + self.check_arg_raise_if_not_exist("module_name") + root, _, module, _, _, _ = self.get_args() + adapter_framework = hdf_utils.get_vendor_hdf_dir_framework(root=root) + if not os.path.exists(adapter_framework): + raise HdfToolException( + ' adapter model path "%s" not exist' % + adapter_framework, CommandErrorCode.TARGET_NOT_EXIST) + create_file_save_path = os.path.join( + adapter_framework, "tools", "hdf_dev_eco_tool", + "resources", "create_model.config") + if not os.path.exists(create_file_save_path): + raise HdfToolException( + 'create file config "%s" not exist' % + create_file_save_path, CommandErrorCode.TARGET_NOT_EXIST) + data = hdf_utils.read_file(create_file_save_path) + write_data = json.loads(data) + write_data.pop(module) + hdf_utils.write_file(create_file_save_path, + json.dumps(write_data, indent=4)) + return write_data + + def _delete_module_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + self.check_arg_raise_if_not_exist("module_name") + root, _, module, _, _, _ = self.get_args() + adapter_framework = hdf_utils.get_vendor_hdf_dir_framework(root=root) + if not os.path.exists(adapter_framework): + raise HdfToolException( + ' adapter model path "%s" not exist' % + adapter_framework, CommandErrorCode.TARGET_NOT_EXIST) + create_file_save_path = os.path.join( + adapter_framework, "tools", "hdf_dev_eco_tool", + "resources", "create_model.config") + if not os.path.exists(create_file_save_path): + raise HdfToolException( + 'create file config "%s" not exist' % + create_file_save_path, CommandErrorCode.TARGET_NOT_EXIST) + data = hdf_utils.read_file(create_file_save_path) + file_info = json.loads(data) + model_info = file_info.get(module, None) + if model_info is None: + raise HdfToolException( + ' delete model "%s" not exist' % + module, CommandErrorCode.TARGET_NOT_EXIST) + else: + return self._delete_module(root, module, model_info) + + def _delete_driver(self, module, driver): + root, vendor, _, _, board = self.get_args() + drv_dir = hdf_utils.get_drv_dir(root, vendor, module, driver) + if os.path.exists(drv_dir): + shutil.rmtree(drv_dir) + k_path = hdf_utils.get_module_kconfig_path(root, vendor, module) + HdfModuleKconfigFile(root, module, k_path).delete_driver(driver) + HdfModuleMkFile(root, vendor, module).delete_driver(driver) + HdfDriverConfigFile(root, board, module, driver).delete_driver() + + def _delete_driver_handler(self): + self.check_arg_raise_if_not_exist("vendor_name") + self.check_arg_raise_if_not_exist("module_name") + self.check_arg_raise_if_not_exist("driver_name") + self.check_arg_raise_if_not_exist("board_name") + _, _, module, driver, _ = self.get_args() + self._delete_driver(module, driver) + + def _delete_file_func(self, key, model_info, model, root): + if key == "module_level_config_path": + for k, v in model_info["module_level_config_path"].items(): + if k == "%s_Makefile" % model: + HdfVendorMakeFile( + root, vendor="", kernel="", + path=os.path.join(root, v)).delete_module(model) + elif k == "%s_Kconfig" % model: + HdfVendorKconfigFile( + root, vendor="", kernel="", + path=os.path.join(root, v)).delete_module(model) + elif k == "%sBuild" % model: + HdfVendorBuildFile( + root, vendor="").delete_module( + file_path=os.path.join(root, v), model=model) + elif k == "%s_hdf_lite" % model: + HdfVendorMkFile( + root, vendor="").delete_module( + file_path=os.path.join(root, v), module=model) + elif k == "%s_dot_configs" % model: + for dot_path in v: + if dot_path.split(".")[-1] == "config": + template_string = \ + "LOSCFG_DRIVERS_HDF_${module_upper_case}=y\n" + else: + template_string = \ + "CONFIG_DRIVERS_HDF_${module_upper_case}=y\n" + new_demo_config = Template(template_string).\ + substitute({"module_upper_case": model.upper()}) + defconfig_patch = HdfDefconfigAndPatch( + root=root, vendor="", kernel="", board="", + data_model="", new_demo_config=new_demo_config) + defconfig_patch.delete_module( + path=os.path.join(root, dot_path)) + elif key == "module_path": + for k, v in model_info["module_path"].items(): + if v.endswith("hcs"): + hcs_path = os.path.join(root, v) + HdfDeviceInfoHcsFile( + root=root, vendor="", module="", + board="", driver="", path=hcs_path). \ + delete_driver(module=model) + else: + if v: + file_path = os.path.join(root, v) + if os.path.exists(file_path): + os.remove(file_path) + model_dir_path = "/".join(file_path.split("/")[:-1]) + if os.path.exists(model_dir_path): + file_list = os.listdir(model_dir_path) + if not file_list: + shutil.rmtree(model_dir_path) diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_device_info_hcs.py b/tools/hdf_dev_eco_tool/command_line/hdf_device_info_hcs.py new file mode 100755 index 0000000000000000000000000000000000000000..22cf8a431f9585d3a831d394d04505ec6c2976a7 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_device_info_hcs.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re +from string import Template + + +from .hdf_command_error_code import CommandErrorCode +from hdf_tool_exception import HdfToolException +import hdf_utils + + +class HdfDeviceInfoHcsFile(object): + def __init__(self, root, vendor, module, board, driver, path): + if not path: + self.module = module + self.vendor = vendor + self.board = board + self.root = root + self.driver = driver + self.lines = None + self.hcs_path = hdf_utils.get_hcs_file_path( + self.root, self.vendor, self.board) + else: + self.hcs_path = path + self.root = root + self.file_path = hdf_utils.get_template_file_path(root) + if not os.path.exists(self.file_path): + raise HdfToolException( + 'template file: %s not exist' % + self.file_path, CommandErrorCode.TARGET_NOT_EXIST) + if not os.path.exists(self.hcs_path): + raise HdfToolException( + 'hcs file: %s not exist' % + self.hcs_path, CommandErrorCode.TARGET_NOT_EXIST) + + def _save(self): + if self.lines: + hdf_utils.write_file(self.hcs_path, ''.join(self.lines)) + + def _find_line(self, pattern): + for index, line in enumerate(self.lines): + if re.search(pattern, line): + return index, line + return 0, '' + + def _find_last_include(self): + if not self.lines: + return 0 + i = len(self.lines) - 1 + while i >= 0: + line = self.lines[i] + if re.search(self.include_pattern, line): + return i + 1 + i -= 1 + return 0 + + def _create_makefile(self): + mk_path = os.path.join(self.file_dir, 'Makefile') + template_str = hdf_utils.get_template('hdf_hcs_makefile.template') + hdf_utils.write_file(mk_path, template_str) + + def check_and_create(self): + if self.lines: + return + if not os.path.exists(self.file_dir): + os.makedirs(self.file_dir) + self._create_makefile() + self.lines.append('#include "hdf_manager/manager_config.hcs"\n') + self._save() + + def add_driver(self, module, driver): + target_line = self.line_template % (module, driver) + target_pattern = self.line_pattern % (module, driver) + idx, line = self._find_line(target_pattern) + if line: + self.lines[idx] = target_line + else: + pos = self._find_last_include() + self.lines.insert(pos, target_line) + self._save() + + def delete_driver(self, module): + hcs_config = hdf_utils.read_file_lines(self.hcs_path) + index_info = {} + count = 0 + for index, line in enumerate(hcs_config): + if line.find("%s :: host" % module) > 0: + index_info["start_index"] = index + for child_index in range( + index_info["start_index"], len(hcs_config)): + if hcs_config[child_index].strip().endswith("{"): + count += 1 + elif hcs_config[child_index].strip() == "}": + count -= 1 + if count == 0: + index_info["end_index"] = child_index + break + break + if index_info: + self.lines = hcs_config[0:index_info["start_index"]] \ + + hcs_config[index_info["end_index"] + 1:] + self._save() + return True + + def add_model_hcs_file_config(self): + template_path = os.path.join(self.file_path, + 'device_info_hcs.template') + lines = list(map(lambda x: "\t\t" + x, + hdf_utils.read_file_lines(template_path))) + old_lines = list(filter(lambda x: x != "\n", + hdf_utils.read_file_lines(self.hcs_path))) + new_data = old_lines[:-2] + lines + old_lines[-2:] + d = {'driver_name': self.driver, 'model_name': self.module} + for index in range(len(new_data)): + new_data[index] = Template(new_data[index]).substitute(d) + with open(self.hcs_path, "w+", encoding="utf-8") as l: + for j in new_data: + l.write(j) + return self.hcs_path diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_dot_config_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_dot_config_file.py new file mode 100755 index 0000000000000000000000000000000000000000..a40f642102e43560a02d9309fa1ca648e723da0d --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_dot_config_file.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re + +import hdf_utils + + +class ConfigItem(object): + def __init__(self, index, line): + self.index = index + self.line = line + + +class HdfDotConfigFile(object): + def __init__(self, dot_config_path): + self.file_path = dot_config_path + if os.path.exists(dot_config_path): + self.lines = hdf_utils.read_file_lines(dot_config_path) + self.dot_config_exist = True + else: + self.lines = [] + self.dot_config_exist = False + self.cache = {} + self.hdf_item = 'LOSCFG_DRIVERS_HDF' + self.prefix = 'LOSCFG_' + + def save(self): + if not self.dot_config_exist: + return + with open(self.file_path, 'r+', newline='\n') as file: + file.truncate() + file.write(''.join(self.lines)) + + def _update_cache(self, item, index, line): + self.cache[item] = ConfigItem(index, line) + + def _find(self, item): + if item in self.cache: + index = self.cache[item].index + line = self.cache[item].line + return index, line + for index, line in enumerate(self.lines): + item_pattern = r'%s\s*(=|is)' % item + if re.search(item_pattern, line): + line_ = line.strip() + self._update_cache(item, index, line_) + return index, line_ + return 0, '' + + @staticmethod + def _is_line_enabled(line): + if line.strip().startswith('#'): + return False + return True + + def _enable(self, item): + if not item: + return + new_line = '%s=y\n' % item + index, line = self._find(item) + if line: + if self._is_line_enabled(line): + return + self.lines[index] = new_line + self._update_cache(item, index, new_line) + return + else: + count = len(self.lines) + self.lines.append(new_line) + self._update_cache(item, count, new_line) + + def _disable(self, item): + if not item: + return + new_line = '# %s is not set\n' % item + index, line = self._find(item) + if not line: + return + if not self._is_line_enabled(line): + return + self.lines[index] = new_line + self._update_cache(item, index, new_line) + + def _is_enabled(self, item): + if not item: + return False + index, line = self._find(item) + if not line: + return False + return self._is_line_enabled(line) + + def _add_prefix(self, item): + if not item.startswith(self.prefix): + item = '%s%s' % (self.prefix, item) + return item + + def is_enabled(self, item, depends_on_item): + if item: + item = self._add_prefix(item) + if depends_on_item: + depends_on_item = self._add_prefix(depends_on_item) + if depends_on_item: + if not self._is_enabled(depends_on_item): + return False + return self._is_enabled(item) + + def enable(self, item, depends_on_item): + if item: + item = self._add_prefix(item) + if depends_on_item: + depends_on_item = self._add_prefix(depends_on_item) + self._enable(self.hdf_item) + self._enable(depends_on_item) + self._enable(item) + + def disable(self, item): + if item: + item = self._add_prefix(item) + self._disable(item) + + def add_dot_config(self): + hdf_utils.get_dot_configs_path() diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_driver_config_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_driver_config_file.py new file mode 100755 index 0000000000000000000000000000000000000000..a4d045e451c38f36ed1caeb3d58e5473351bd992 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_driver_config_file.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +from string import Template + +from .hdf_manager_config_file import HdfManagerConfigFile +from .hdf_hcs_file import HdfHcsFile +from hdf_tool_settings import HdfToolSettings +from hdf_tool_exception import HdfToolException +from command_line.hdf_command_error_code import CommandErrorCode +import hdf_utils + + +class HdfDriverConfigFile(object): + def __init__(self, root, board, module, driver, kernel, only_path=False): + self.root = root + self.board = board + self.module = module + self.driver = driver + self.kernel = kernel + bpp = HdfToolSettings().get_board_parent_path(self.board) + board_path = os.path.join(self.root, bpp, self.board) + + if not os.path.exists(board_path): + raise HdfToolException('board: %s not exist' % board_path, + CommandErrorCode.TARGET_NOT_EXIST) + self.config_dir = os.path.join(board_path, 'hdf_config') + self.drv_dir = os.path.join(self.config_dir, self.module) + self.drv_config_path = os.path.join( + self.drv_dir, '%s_config.hcs' % self.driver) + if only_path: + return + manager_hcs_path = os.path.join(self.config_dir, 'device_info', + 'device_info.hcs') + self.manager_hcs = HdfManagerConfigFile(manager_hcs_path) + hdf_hcs_path = os.path.join(self.config_dir, 'hdf.hcs') + self.hdf_hcs = HdfHcsFile(hdf_hcs_path) + + def _check_and_create_common_config(self): + self.manager_hcs.check_and_create() + self.hdf_hcs.check_and_create() + if not os.path.exists(self.drv_dir): + os.makedirs(self.drv_dir) + + def create_driver(self): + self._check_and_create_common_config() + template_str = hdf_utils.get_template('hdf_driver_config.template') + config_content = Template(template_str).safe_substitute({}) + hdf_utils.write_file(self.drv_config_path, config_content) + self.manager_hcs.add_device(self.module, self.driver) + self.hdf_hcs.add_driver(self.module, self.driver) + + def delete_driver(self): + if not os.path.exists(self.drv_config_path): + return + os.remove(self.drv_config_path) + self.manager_hcs.delete_device(self.module, self.driver) + self.hdf_hcs.delete_driver(self.module, self.driver) + + def get_drv_config_path(self): + if os.path.exists(self.drv_config_path): + return os.path.realpath(self.drv_config_path) + else: + return '' diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_get_handler.py b/tools/hdf_dev_eco_tool/command_line/hdf_get_handler.py new file mode 100755 index 0000000000000000000000000000000000000000..4bb0230bec9c9faee33aee414c85e46cc65a776f --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_get_handler.py @@ -0,0 +1,214 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import json + +from .hdf_command_handler_base import HdfCommandHandlerBase +from .hdf_lite_mk_file import HdfLiteMkFile +from .hdf_vendor_kconfig_file import HdfVendorKconfigFile +from .hdf_module_kconfig_file import HdfModuleKconfigFile +from .hdf_driver_config_file import HdfDriverConfigFile +from command_line.hdf_command_error_code import CommandErrorCode +from hdf_tool_settings import HdfToolSettings +from hdf_tool_exception import HdfToolException +import hdf_tool_version +import hdf_utils + + +class HdfGetHandler(HdfCommandHandlerBase): + def __init__(self, args): + super(HdfGetHandler, self).__init__() + self.HDF_VERSION_MAJOR = 0 + self.HDF_VERSION_MINOR = 1 + self.cmd = 'get' + self.handlers = { + 'vendor_list': self._get_vendor_list_handler, + 'current_vendor': self._get_current_vendor_handler, + 'vendor_parent_path': self._get_vendor_parent_path_handler, + 'individual_vendor_path': self._get_individual_vendor_path_handler, + 'board_list': self._get_board_list_handler, + 'driver_list': self._get_driver_list_handler, + 'driver_file': self._get_driver_file_handler, + 'drv_config_file': self._get_drv_config_file_handler, + 'hdf_tool_core_version': self._get_version_handler, + 'model_list': self._get_model_dict, + 'version': self.__get_version, + } + self.parser.add_argument("--action_type", + help=' '.join(self.handlers.keys()), + required=True) + self.parser.add_argument("--root_dir") + self.parser.add_argument("--vendor_name") + self.parser.add_argument("--module_name") + self.parser.add_argument("--driver_name") + self.parser.add_argument("--board_name") + self.args = self.parser.parse_args(args) + + def _get_vendor_list_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + root = self.args.root_dir + vendor_root_dir = hdf_utils.get_vendor_root_dir(root) + vendors = [] + if os.path.exists(vendor_root_dir): + for vendor in os.listdir(vendor_root_dir): + hdf = hdf_utils.get_vendor_hdf_dir(root, vendor) + if os.path.exists(hdf): + vendors.append(vendor) + return ','.join(vendors) + + def _get_current_vendor_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + return HdfLiteMkFile(self.args.root_dir).get_current_vendor() + + @staticmethod + def _get_board_list_handler(): + settings = HdfToolSettings() + return settings.get_supported_boards() + + def _get_vendor_parent_path_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + target = hdf_utils.get_vendor_root_dir(self.args.root_dir) + return os.path.realpath(target) + + def _get_individual_vendor_path_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + self.check_arg_raise_if_not_exist("vendor_name") + root, vendor, _, _, _ = self.get_args() + target = hdf_utils.get_vendor_dir(root, vendor) + return os.path.realpath(target) + + @staticmethod + def _get_version_handler(): + return hdf_tool_version.get_version() + + def _get_driver_list_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + self.check_arg_raise_if_not_exist("vendor_name") + root, vendor, _, _, _ = self.get_args() + hdf_dir = hdf_utils.get_vendor_hdf_dir(root, vendor) + if not os.path.exists(hdf_dir): + raise HdfToolException('vendor "%s" not exist' % vendor, + CommandErrorCode.TARGET_NOT_EXIST) + modules = os.listdir(hdf_dir) + vendor_k = HdfVendorKconfigFile(root, vendor) + module_items = vendor_k.get_module_and_config_path() + drivers = {} + for item in module_items: + module, k_path = item + if module in modules: + models = \ + HdfModuleKconfigFile(root, module, + k_path).get_models() + drivers[module] = models + return json.dumps(drivers) + + def _get_driver_file_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + self.check_arg_raise_if_not_exist("vendor_name") + self.check_arg_raise_if_not_exist("module_name") + self.check_arg_raise_if_not_exist("driver_name") + root = os.path.realpath(self.args.root_dir) + _, vendor, module, driver, _ = self.get_args() + drv_dir = hdf_utils.get_drv_dir(root, vendor, module, driver) + if not os.path.exists(drv_dir): + raise HdfToolException( + 'driver directory: %s not exist' % + drv_dir, CommandErrorCode.TARGET_NOT_EXIST) + for root_path, dirs, files in os.walk(drv_dir): + for file in files: + if file.endswith('.c'): + return os.path.realpath(os.path.join(root_path, file)) + return '' + + def _get_drv_config_file_handler(self): + self.check_arg_raise_if_not_exist("root_dir") + self.check_arg_raise_if_not_exist("module_name") + self.check_arg_raise_if_not_exist("driver_name") + self.check_arg_raise_if_not_exist("board_name") + root, _, module, driver, board = self.get_args() + drv_config = HdfDriverConfigFile(root, board, module, driver, True) + return drv_config.get_drv_config_path() + + def _get_model_dict(self): + self.check_arg_raise_if_not_exist("root_dir") + root, _, _, _, _, _ = self.get_args() + adapter_framework = hdf_utils.get_vendor_hdf_dir_framework(root=root) + if not os.path.exists(adapter_framework): + raise HdfToolException( + ' adapter model path "%s" not exist' % + adapter_framework, CommandErrorCode.TARGET_NOT_EXIST) + create_file_save_path = os.path.join( + adapter_framework, "tools", "hdf_dev_eco_tool", + "resources", "create_model.config") + if not os.path.exists(create_file_save_path): + raise HdfToolException( + 'create file config "%s" not exist' % + create_file_save_path, CommandErrorCode.TARGET_NOT_EXIST) + out_model_list = [] + data = hdf_utils.read_file(create_file_save_path) + json_type_data = json.loads(data) + if not json_type_data: + return out_model_list + file_key_list = list(list(json_type_data. + items())[0][-1].keys())[:-1] + for k, v in json_type_data.items(): + model_file_path = {} + for key in file_key_list: + if key.split("_")[-1] == "path": + path_dict = json_type_data[k][key] + model_file_path = self._model_info( + path_dict, root, model_file_path, key) + out_model_list.append({k: model_file_path}) + return out_model_list + + def __get_version(self): + version_end = "\nCopyright (c) 2020-2021 Huawei Device Co., Ltd." + version_head = "hdf_dev_eco_tool version : " + return version_head + str(self.HDF_VERSION_MAJOR) \ + + "." + str(self.HDF_VERSION_MINOR) + version_end + + def _model_info(self, path_dict, root, model_file_path, key): + if isinstance(path_dict, dict): + for k_filename, file_path in path_dict.items(): + if not os.path.exists(os.path.join(root, file_path)): + model_file_path[k_filename] = " " + else: + model_file_path[k_filename] = path_dict[k_filename] + else: + hcs_file_path = os.path.join(root, path_dict) + if not os.path.exists(hcs_file_path): + model_file_path[key] = " " + else: + model_file_path[key] = path_dict + return model_file_path diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_hcs_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_hcs_file.py new file mode 100755 index 0000000000000000000000000000000000000000..9cb614cfe49386e26f30529f629f36e4edf0cebd --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_hcs_file.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re + +import hdf_utils + + +class HdfHcsFile(object): + def __init__(self, file_path): + self.file_path = file_path + self.file_dir = os.path.dirname(self.file_path) + if os.path.exists(self.file_path): + self.lines = hdf_utils.read_file_lines(self.file_path) + else: + self.lines = [] + self.line_template = '#include "%s/%s_config.hcs"\n' + self.line_pattern = r'^\s*#include\s+"%s/%s_config.hcs"' + self.include_pattern = r'^\s*#include' + + def _save(self): + if self.lines: + hdf_utils.write_file(self.file_path, ''.join(self.lines)) + + def _find_line(self, pattern): + for index, line in enumerate(self.lines): + if re.search(pattern, line): + return index, line + return 0, '' + + def _find_last_include(self): + if not self.lines: + return 0 + i = len(self.lines) - 1 + while i >= 0: + line = self.lines[i] + if re.search(self.include_pattern, line): + return i + 1 + i -= 1 + return 0 + + def _create_makefile(self): + mk_path = os.path.join(self.file_dir, 'Makefile') + template_str = hdf_utils.get_template('hdf_hcs_makefile.template') + hdf_utils.write_file(mk_path, template_str) + + def check_and_create(self): + if self.lines: + return + if not os.path.exists(self.file_dir): + os.makedirs(self.file_dir) + self._create_makefile() + self.lines.append('#include "hdf_manager/manager_config.hcs"\n') + self._save() + + def add_driver(self, module, driver): + target_line = self.line_template % (module, driver) + target_pattern = self.line_pattern % (module, driver) + idx, line = self._find_line(target_pattern) + if line: + self.lines[idx] = target_line + else: + pos = self._find_last_include() + self.lines.insert(pos, target_line) + self._save() + + def delete_driver(self, module, driver): + target_pattern = self.line_pattern % (module, driver) + idx, line = self._find_line(target_pattern) + if not line: + return + self.lines[idx] = '' + self._save() diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_lite_kconfig_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_lite_kconfig_file.py new file mode 100755 index 0000000000000000000000000000000000000000..077d7f20047b5b15fe789f285e2476c09c616167 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_lite_kconfig_file.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re + +from hdf_tool_settings import HdfToolSettings +import hdf_utils + + +class HdfLiteKconfigFile(object): + def __init__(self, root): + self.file_path = hdf_utils.get_hdf_lite_kconfig_path(root) + self.orig_exist = False + if os.path.exists(self.file_path): + self.lines = hdf_utils.read_file_lines(self.file_path) + self.orig_exist = True + else: + self.lines = [] + self.vendor_pattern = r'source\s+".*/vendor/.*/Kconfig"' + self.target_pattern_format = r'source\s+".*/vendor/%s/.*/Kconfig"' + self.target_index = -1 + + def _save(self): + if self.orig_exist: + hdf_utils.write_file(self.file_path, ''.join(self.lines)) + + def _find_all_vendors(self, target_vendor): + vendors = [] + for index, line in enumerate(self.lines): + if self.target_index == -1: + if re.search(self.target_pattern_format % target_vendor, line): + self.target_index = index + continue + if re.search(self.vendor_pattern, line): + vendors.append(index) + return vendors + + def _comment_other_vendors(self, other_vendors): + for index in other_vendors: + if not hdf_utils.is_commented_line(self.lines[index], '#'): + self.lines[index] = '# %s' % self.lines[index] + + def set_vendor(self, current_vendor): + other_vendors = self._find_all_vendors(current_vendor) + self._comment_other_vendors(other_vendors) + drivers_path = HdfToolSettings().get_drivers_path() + new_line = 'source "../../vendor/%s/%s/Kconfig"\n' % \ + (current_vendor, drivers_path) + if self.target_index != -1: + self.lines[self.target_index] = new_line + else: + pos = len(self.lines) - 1 + while pos > 0 and len(self.lines[pos].strip()) == 0: + pos -= 1 + pos += 1 + self.lines.insert(pos, new_line) + self._save() diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_lite_mk_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_lite_mk_file.py new file mode 100755 index 0000000000000000000000000000000000000000..e7dc12e823c01d5b38b75e045c9ecdba61e4fc97 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_lite_mk_file.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re + +from hdf_tool_settings import HdfToolSettings +import hdf_utils + + +class HdfLiteMkFile(object): + def __init__(self, root): + self.file_path = hdf_utils.get_hdf_lite_mk_path(root) + self.orig_exist = False + if os.path.exists(self.file_path): + self.lines = hdf_utils.read_file_lines(self.file_path) + self.orig_exist = True + else: + self.lines = [] + self.end_line_pattern = r'LITEOS_BASELIB\s*[+]=\s*--no-whole-archive' + self.vendor_pattern = r'include.*hdf_vendor[.]mk' + self.target_format = r'include .*vendor/%s/.*/hdf_vendor[.]mk' + self.end_line_index = -1 + self.target_line_index = -1 + self.current_vendor_pattern = r'^\s*include.*/device/(.*)/lite.mk' + + def _save(self): + if self.orig_exist: + hdf_utils.write_file(self.file_path, ''.join(self.lines)) + + def _find_all_vendors(self, current_vendor): + vendor_lines = [] + for index, line in enumerate(self.lines): + if self.target_line_index == -1: + if re.search(self.target_format % current_vendor, line): + self.target_line_index = index + continue + if self.end_line_index == -1: + if re.search(self.end_line_pattern, line): + self.end_line_index = index + continue + if re.search(self.vendor_pattern, line): + vendor_lines.append(index) + return vendor_lines + + def _comment_other_vendors(self, other_vendors): + for index in other_vendors: + if not hdf_utils.is_commented_line(self.lines[index], '#'): + self.lines[index] = '# %s' % self.lines[index] + + def set_vendor(self, current_vendor): + other_vendors = self._find_all_vendors(current_vendor) + self._comment_other_vendors(other_vendors) + drivers_path = HdfToolSettings().get_drivers_path() + new_line = \ + 'include $(LITEOSTOPDIR)/../../vendor/%s/%s/hdf_vendor.mk\n' % \ + (current_vendor, drivers_path) + if self.target_line_index != -1: + self.lines[self.target_line_index] = new_line + elif self.end_line_index != -1: + self.lines.insert(self.end_line_index, new_line) + else: + self.lines.append(new_line) + self._save() + + def get_current_vendor(self): + for line in self.lines: + match_obj = re.search(self.current_vendor_pattern, line) + if match_obj: + vendor_drivers_path = match_obj.group(1) + parts = vendor_drivers_path.split('/') + valid_parts = [part for part in parts if part] + if valid_parts: + return valid_parts[0] + return '' diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_lite_settings_mk_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_lite_settings_mk_file.py new file mode 100755 index 0000000000000000000000000000000000000000..e163130eaf93c74ca607a8685c4366420aebbee6 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_lite_settings_mk_file.py @@ -0,0 +1,110 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re + +from .hdf_command_error_code import CommandErrorCode +from hdf_tool_exception import HdfToolException +import hdf_utils + + +class HdfLiteSettingsMkFile(object): + def __init__(self, root_dir): + self.root_dir = root_dir + self.file_path = hdf_utils.get_hdf_lite_settings_mk_path(root_dir) + if os.path.exists(self.file_path): + self.lines = hdf_utils.read_file_lines(self.file_path) + else: + dir_path = os.path.dirname(self.file_path) + if not os.path.exists(dir_path): + os.makedirs(dir_path) + self.lines = [] + self.line_pattern = r'(%s\s*:=\s*)(.*)' + self.hdf_vendor_var_name = 'HDF_COMPILE_VENDOR' + self.board_vendor_path_var_name = 'HDF_SET_BOARD_VENDOR_PATH' + + def _save(self): + if self.lines: + hdf_utils.write_file(self.file_path, ''.join(self.lines)) + + def _append(self, new_line): + self.lines.append(new_line) + self._save() + + def _find_var_line(self, var_name): + for index, line in enumerate(self.lines): + if var_name in line: + return index, line + return 0, '' + + def _is_vendor_valid(self, vendor_name): + vendor_hdf_path = \ + hdf_utils.get_vendor_hdf_dir(self.root_dir, vendor_name) + if os.path.exists(vendor_hdf_path): + return True + return False + + def _set_var_value(self, var_name, var_value): + idx, var_line = self._find_var_line(var_name) + if var_line: + self.lines[idx] = re.sub(self.line_pattern % var_name, + r'\g<1>%s' % var_value, var_line) + self._save() + else: + new_line = '\n%s := %s' % (var_name, var_value) + self._append(new_line) + + def _get_var_value(self, var_name): + idx, var_line = self._find_var_line(var_name) + var_value = '' + if var_line: + match_obj = re.search(self.line_pattern % var_name, var_line) + if match_obj: + var_value = match_obj.group(2) + return var_value.strip() + + def set_vendor(self, vendor_name): + if not self._is_vendor_valid(vendor_name): + raise HdfToolException('vendor: "%s" not exist' % vendor_name, + CommandErrorCode.TARGET_NOT_EXIST) + self._set_var_value(self.hdf_vendor_var_name, vendor_name) + + def get_vendor(self): + vendor_name = self._get_var_value(self.hdf_vendor_var_name) + if self._is_vendor_valid(vendor_name): + return vendor_name + return '' + + def set_board_vendor_path(self, board_vendor_path): + self._set_var_value(self.board_vendor_path_var_name, board_vendor_path) diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_manager_config_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_manager_config_file.py new file mode 100755 index 0000000000000000000000000000000000000000..d856a393e792488ad74dc5f6eb1a84850d94d64d --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_manager_config_file.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re +from string import Template + +from hdf_tool_exception import HdfToolException +from command_line.hdf_command_error_code import CommandErrorCode +import hdf_utils + + +class HdfManagerConfigFile(object): + def __init__(self, file_path): + self.file_path = file_path + self.file_dir = os.path.dirname(self.file_path) + self.host_pattern = r'%s\s*::\s*host\s*{' + self.hdf_manager_pattern = r'device_info\s*{' + self.contents = '' + self._read_contents() + + def _read_contents(self): + if os.path.exists(self.file_path): + self.contents = hdf_utils.read_file(self.file_path) + else: + self.contents = '' + + def check_and_create(self): + if self.contents: + return + template_str = \ + hdf_utils.get_template('hdf_driver_manager_config.template') + self.contents = Template(template_str).safe_substitute({}) + if not os.path.exists(self.file_dir): + os.makedirs(self.file_dir) + hdf_utils.write_file(self.file_path, self.contents) + + def _find_range(self, pattern): + match_obj = re.search(pattern, self.contents) + if not match_obj: + return None + start = match_obj.start() + if start == -1: + return None + braces = [] + start += len(match_obj.group(0)) - 1 + end = start + for i in range(start, len(self.contents)): + if '{' == self.contents[i]: + braces.append('{') + elif '}' == self.contents[i]: + count = len(braces) + if count == 0: + return None + if count == 1: + end = i + break + braces.pop() + if end != start: + while end > start + 1: + end -= 1 + if self.contents[end] not in [' ', '\t']: + break + return hdf_utils.SectionRange(start, end) + return None + + @staticmethod + def _begin_end(module, driver): + dev_id = hdf_utils.get_id(module, driver) + device_begin = '\n/* 0: + del lines[index] + hdf_utils.write_file_lines(file_path, lines) + + def rename_vendor(self): + pattern = r'vendor/([a-zA-Z0-9_\-]+)/' + replacement = 'vendor/%s/' % self.vendor + new_content = re.sub(pattern, replacement, self.contents) + hdf_utils.write_file(self.file_path, new_content) diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_vendor_kconfig_file.py b/tools/hdf_dev_eco_tool/command_line/hdf_vendor_kconfig_file.py new file mode 100755 index 0000000000000000000000000000000000000000..2359dab2cedff9325d8bbc77c99f94e8be52e97e --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_vendor_kconfig_file.py @@ -0,0 +1,126 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re + +from hdf_tool_settings import HdfToolSettings +import hdf_utils + + +class HdfVendorKconfigFile(object): + def __init__(self, root, vendor, kernel, path): + if not path: + self.kernel = kernel + self.root = root + self.vendor = vendor + self.kconfig_path = hdf_utils.\ + get_vendor_kconfig_path(self.root, self.kernel) + else: + self.kconfig_path = path + if os.path.exists(self.kconfig_path): + self.lines = hdf_utils.read_file_lines(self.kconfig_path) + else: + self.lines = [] + drivers_path = HdfToolSettings().get_drivers_path_adapter() + if kernel.capitalize() == "Liteos" or re.search( + r'liteos/Kconfig', self.kconfig_path): + line_pattern = \ + r'^\s*source\s+"../../(%s/([a-zA-Z0-9_\-]+)/.*)"'\ + % (drivers_path) + self.line_re = re.compile(line_pattern) + self.line_prefix = 'source "../../%s/%s/%s"'\ + % (drivers_path, 'liteos', 'model') + elif kernel.capitalize() == "Linux" or re.search( + r'linux/Kconfig', self.kconfig_path): + line_pattern = \ + r'^\s*source\s+"../../(%s/([a-zA-Z0-9_\-]+)/.*)"'\ + % (drivers_path) + self.line_re = re.compile(line_pattern) + self.line_prefix = 'source "drivers/hdf/khdf/model/%s/Kconfig"\n' + + def _save(self): + if self.lines: + hdf_utils.write_file(self.kconfig_path, ''.join(self.lines)) + + def get_module_and_config_path(self): + tuples = [] + for line in self.lines: + match_obj = self.line_re.search(line) + if match_obj: + k_path_raw = match_obj.group(1).replace('/', os.sep) + k_path = os.path.join(self.root, k_path_raw) + tuples.append((match_obj.group(2), k_path)) + return tuples + + def _find_line(self, line_part): + for index, line in enumerate(self.lines): + if line_part in line: + return index, line + return 0, '' + + def add_module(self, module_to_k_path_parts): + module_k_part = '/'.join(module_to_k_path_parts) + index, line = self._find_line(module_k_part) + if line: + return + line = '\n%s/%s"\n' % (self.line_prefix, module_k_part) + if self.lines: + if self.lines[-1].endswith('\n'): + if self.kernel.capitalize() == "Liteos": + line = '%s/%s"\n' % (self.line_prefix[:-1], module_k_part) + elif self.kernel.capitalize() == "Linux": + line = self.line_prefix % (module_to_k_path_parts[0]) + for file_index, file_line in enumerate(self.lines): + if file_line.strip() == "endif": + self.lines.insert(file_index - 1, line) + break + self._save() + return self.kconfig_path + + def delete_module(self, module): + if self.line_prefix.find("Kconfig") > 0: + line_part = self.line_prefix % module + else: + line_part = '%s/%s/Kconfig"\n' % (self.line_prefix[:-1], module) + index, line = self._find_line(line_part) + if line: + self.lines[index] = '' + self._save() + + def rename_vendor(self, ): + for i, line in enumerate(self.lines): + pattern = r'vendor/([a-zA-Z0-9_\-]+)/' + replacement = 'vendor/%s/' % self.vendor + self.lines[i] = re.sub(pattern, replacement, line) + self._save() diff --git a/tools/hdf_dev_eco_tool/command_line/hdf_vendor_makefile.py b/tools/hdf_dev_eco_tool/command_line/hdf_vendor_makefile.py new file mode 100755 index 0000000000000000000000000000000000000000..4fda7b55f58b02770eb773b96e1fdb18c6983520 --- /dev/null +++ b/tools/hdf_dev_eco_tool/command_line/hdf_vendor_makefile.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import os +import re +from string import Template + +from .hdf_command_error_code import CommandErrorCode +from hdf_tool_exception import HdfToolException +import hdf_utils + + +class HdfVendorMakeFile(object): + def __init__(self, root, vendor, kernel, path): + if path: + self.file_path = path + else: + self.vendor = vendor + self.kernel = kernel + self.file_path = hdf_utils.get_vendor_makefile_path(root, self.kernel) + if not os.path.exists(self.file_path): + raise HdfToolException('file: %s not exist' % self.file_path, + CommandErrorCode.TARGET_NOT_EXIST) + self.contents = hdf_utils.read_file_lines(self.file_path) + self.template_str = \ + "obj-$(CONFIG_DRIVERS_HDF_${module_upper_case}) " \ + "+= model/${module_lower_case}/\n" + + def _begin_end(self, module): + module_id = hdf_utils.get_id(self.vendor, module) + begin = '\n# pos_range.end_pos + 1: + file.write(file_content[pos_range.end_pos + 1:]) + + +def append_and_save(file_content, file_path, new_section): + if not file_content or not file_path or not new_section: + return + with open(file_path, 'w', newline='\n') as file: + file.write(file_content) + _write_one_section(file, new_section) + + +def delete_and_save(file_content, file_path, delete_range): + if not file_content or not file_path or not delete_range: + return + length = len(file_content) + with open(file_path, 'w', newline='\n') as file: + file.write(file_content[:delete_range.start_pos]) + if delete_range.end_pos + 1 < length: + file.write(file_content[delete_range.end_pos + 1:]) + + +def replace_and_save(file_content, file_path, old_range, new_section): + if not file_content or not file_path or not old_range or not new_section: + return + with open(file_path, 'w', newline='\n') as file: + file.write(file_content[:old_range.start_pos]) + _write_one_section(file, new_section) + file.write(file_content[old_range.end_pos + 1:]) + + +def get_id(part2): + full_name = part2 + return hashlib.sha256(full_name.encode('utf8')).hexdigest()[:32] + + +def create_dirs(dir_path): + if dir_path and not os.path.exists(dir_path): + try: + os.makedirs(dir_path) + except Exception: + raise HdfToolException('create dirs fail: %s' % dir_path) + + +def read_file(file_path): + with open(file_path, encoding="utf-8") as file: + content = file.read() + return content + + +def read_file_lines(file_path, code_type="utf-8"): + with open(file_path, encoding=code_type) as file: + lines = file.readlines() + return lines + + +def write_file(file_path, content): + with open(file_path, 'w+', newline='\n') as file: + file.write(content) + + +def write_file_lines(file_path, content, code_type="utf-8"): + with open(file_path, 'w', encoding=code_type) as file: + file.writelines(content) + + +def get_framework_lite_dir(root): + return os.path.join(root, 'drivers', 'adapter', 'khdf', 'liteos') + + +def get_vendor_root_dir(root): + return os.path.join(root, 'vendor') + + +def get_vendor_dir(root, vendor): + return os.path.join(get_vendor_root_dir(root), vendor) + + +def get_vendor_hdf_dir_framework(root): + relative_path = HdfToolSettings().get_drivers_path_framework() + return os.path.join(root, relative_path) + + +def get_vendor_hdf_dir_adapter(root, kernel='liteos'): + relative_path = HdfToolSettings().get_drivers_path_adapter() + return os.path.join(root, relative_path, kernel) + + +def get_vendor_lite_mk_path(root): + return os.path.join(get_vendor_hdf_dir_adapter(root), 'hdf_lite.mk') + + +def get_vendor_makefile_path(root, kernel): + return os.path.join(get_vendor_hdf_dir_adapter(root, kernel), 'Makefile') + + +def get_dot_configs_path(root, vendor, board): + path = os.path.join(root, "vendor", vendor, board, 'kernel_configs') + return [os.path.join(path, i) for i in os.listdir(path)] + + +def get_module_dir(root, vendor, module): + return os.path.join(get_vendor_hdf_dir_framework(root), 'model', module) + + +def get_drv_root_dir(root, vendor, module): + return os.path.join(get_module_dir(root, vendor, module), 'driver') + + +def get_drv_dir(root, vendor, module, driver): + return os.path.join(get_drv_root_dir(root, vendor, module), driver) + + +def get_drv_src_dir(root, vendor, module): + return get_drv_root_dir(root, vendor, module) + + +def get_drv_include_dir(root, vendor, module, driver): + return os.path.join(get_drv_dir(root, vendor, module, driver), 'include') + + +def get_vendor_kconfig_path(root, kernel): + hdf_dir = get_vendor_hdf_dir_adapter(root, kernel) + return os.path.join(hdf_dir, 'Kconfig') + + +def get_module_kconfig_path(root, vendor, module): + return os.path.join(get_drv_root_dir(root, vendor, module), 'Kconfig') + + +def get_module_mk_path(root, vendor, module): + return os.path.join(get_drv_root_dir(root, vendor, module), 'Makefile') + + +def get_liteos_a_dot_config_path(root): + return os.path.join(root, 'kernel', 'liteos_a', '.config') + + +def get_resources_dir(): + cur_dir = os.path.realpath(os.path.dirname(__file__)) + return os.path.join(cur_dir, 'resources') + + +def get_templates_dir(): + return os.path.join(get_resources_dir(), 'templates') + + +def get_templates_lite_dir(): + return os.path.join(get_templates_dir(), 'lite') + + +def get_template(template_name, type_='lite'): + templates_dir = os.path.join(get_templates_dir(), type_) + template = os.path.join(templates_dir, template_name) + with open(template) as file: + template_str = file.read() + return template_str + + +def get_hdf_lite_settings_mk_path(root_dir): + return os.path.join(get_framework_lite_dir(root_dir), + 'hdf_lite_settings.mk') + + +def get_hdf_lite_mk_path(root_dir): + return os.path.join(get_framework_lite_dir(root_dir), + 'hdf_lite.mk') + + +def get_hdf_lite_kconfig_path(root_dir): + return os.path.join(get_framework_lite_dir(root_dir), + 'Kconfig') + + +def is_commented_line(line, comment_start): + if line.strip().startswith(comment_start): + return True + else: + return False + + +def get_vendor_gn_path(root): + return os.path.join(get_vendor_hdf_dir_adapter(root), 'BUILD.gn') + + +def get_template_file_path(root, kernel='liteos'): + template_relative_path = HdfToolSettings().get_template_path() + relative_path2 = HdfToolSettings().get_drivers_path_framework() + return os.path.join(root, relative_path2, template_relative_path) + + +def get_hcs_file_path(root, vendor, board): + return os.path.join(root, "vendor", vendor, board, "hdf_config", "device_info", "device_info.hcs") + + +def template_filename_filtrate(dir_path, kernal): + filename_list = [] + for filename in os.listdir(dir_path): + if filename.split("_")[0] == kernal.capitalize(): + filename_list.append(filename) + return filename_list + + +def get_create_model_info(root, create_data): + json_type_data = json.loads(create_data) + out_model_list = [] + if not json_type_data: + return [] + file_key_list = list(list(json_type_data.items())[0][-1].keys())[:-1] + for k, v in json_type_data.items(): + model_file_path = {} + for key in file_key_list: + if key.split("_")[-1] == "path": + path_dict = json_type_data[k][key] + if isinstance(path_dict, dict): + for k_filename, file_path in path_dict.items(): + if not os.path.exists(os.path.join(root, file_path)): + model_file_path[k_filename] = " " + else: + model_file_path[k_filename] = path_dict[k_filename] + else: + hcs_file_path = os.path.join(root, path_dict) + if not os.path.exists(hcs_file_path): + model_file_path[key] = " " + else: + model_file_path[key] = path_dict + out_model_list.append({k: model_file_path}) + return out_model_list \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/main.py b/tools/hdf_dev_eco_tool/main.py new file mode 100755 index 0000000000000000000000000000000000000000..a9aa14688ab4e0bfe9cc8cd3511a41190ab19a16 --- /dev/null +++ b/tools/hdf_dev_eco_tool/main.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021, Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of Willow Garage, Inc. nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +import sys +import argparse + +from command_line.hdf_tool_commands import HdfToolCommands +from hdf_tool_daemon_server import HdfToolDaemonServer +from hdf_tool_exception import HdfToolException +from command_line.hdf_command_error_code import CommandErrorCode + + +def check_python_version(): + if sys.version_info < (3, 0): + print('Please run with python version >= 3.0') + sys.exit(-1) + + +def main(): + check_python_version() + commands = HdfToolCommands() + help_info = 'Tools backend for hdf driver development.' + arg_parser = argparse.ArgumentParser(description=help_info) + arg_parser.add_argument('--run_as_daemon', action='store_true') + arg_parser.add_argument('--server_type', help='command_line or ls_hcs,' + 'default command_line', default='command_line') + arg_parser.add_argument('command', help=commands.help()) + arg_parser.add_argument('remainder_args', nargs=argparse.REMAINDER) + args = arg_parser.parse_args() + if args.run_as_daemon: + HdfToolDaemonServer(args.server_type).run() + return + try: + ret = commands.run(args.command, args.remainder_args) + if ret or isinstance(ret, list): + print(ret) + except HdfToolException as exc: + print('error: {}, {}'.format(exc.error_code, exc.exc_msg)) + except Exception as exc: + print('error: {}, {}'.format(CommandErrorCode.UNKNOWN_ERROR, str(exc))) + + +if __name__ == "__main__": + main() diff --git a/tools/hdf_dev_eco_tool/resources/create_model.config b/tools/hdf_dev_eco_tool/resources/create_model.config new file mode 100755 index 0000000000000000000000000000000000000000..9e26dfeeb6e641a33dae4961196235bdb965b21b --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/create_model.config @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/settings.json b/tools/hdf_dev_eco_tool/resources/settings.json new file mode 100755 index 0000000000000000000000000000000000000000..ecbf0e13764c3714fb33415e1df40e75685c643e --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/settings.json @@ -0,0 +1,23 @@ +{ + "supported_boards": { + "hispark_taurus": { + "board_parent_path": "vendor/hisilicon/hispark_taurus", + "dot_config_path": "kernel/liteos_a/tools/build/config", + "dot_configs": ["hispark_taurus_debug_shell_tee.config", "hispark_taurus_debug_shell.config" + ,"hispark_taurus_release_tee.config", "hispark_taurus_release.config" + , "hispark_taurus_clang_release_tee.config", "hispark_taurus_clang_release.config"] + }, + "hispark_taurus_linux": { + "patch_and_config": ["hi3516dv300.patch", "hi3516dv300_small_defconfig", "hi3516dv300_standard_defconfig", + "small_common_defconfig", "standard_common_defconfig"] + }, + "hispark_aries": { + "board_parent_path": "vendor/hisilicon/hispark_aries", + "dot_config_path": "kernel/liteos_a/tools/build/config", + "dot_configs": ["hispark_aries_clang_release.config", "hispark_aries_release.config", "hispark_aries_debug_shell.config"] + } + }, + "drivers_path_relative_to_vendor": "drivers/framework", + "drivers_path_relative_adapter": "drivers/adapter/khdf", + "template_file_path": "tools/hdf_dev_eco_tool/resources/templates/lite" +} diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_Kconfig.template b/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_Kconfig.template new file mode 100755 index 0000000000000000000000000000000000000000..952d17e018bbe388d81fdedb2c6e4e14a9bdbc5b --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_Kconfig.template @@ -0,0 +1,34 @@ +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +config DRIVERS_HDF_${module_upper_case} + bool "Enable HDF ${module_lower_case} driver" + default n + depends on DRIVERS_HDF + help + Answer Y to enable HDF ${module_lower_case} driver. \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_Makefile.template b/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_Makefile.template new file mode 100755 index 0000000000000000000000000000000000000000..d1a0267ebd7a7e399d613263a4cbb0084a36d537 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_Makefile.template @@ -0,0 +1,33 @@ +# +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. +# +# This software is licensed under the terms of the GNU General Public +# License version 2, as published by the Free Software Foundation, and +# may be copied, distributed, and modified under those terms. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# + +${module_upper_case}_ROOT_DIR = ../../../../../framework/model/${module_lower_case}/driver + +obj-$(CONFIG_DRIVERS_HDF_${module_upper_case}) += \ + $(${module_upper_case}_ROOT_DIR)/${driver_name}_driver.o + +ccflags-y += -I$(srctree)/drivers/hdf/framework/include/core \ + -I$(srctree)/drivers/hdf/framework/core/common/include/host \ + -I$(srctree)/drivers/hdf/framework/include/utils \ + -I$(srctree)/drivers/hdf/framework/include/osal \ + -I$(srctree)/drivers/hdf/framework/ability/sbuf/include \ + -I$(srctree)/drivers/hdf/framework/include/platform \ + -I$(srctree)/drivers/hdf/framework/include/config \ + -I$(srctree)/drivers/hdf/framework/core/host/include \ + -I$(srctree)/drivers/hdf/framework/core/shared/include \ + -I$(srctree)/drivers/hdf/framework/utils/include \ + -I$(srctree)/drivers/hdf/khdf/osal/include + +ccflags-y +=-I$(srctree)/bounds_checking_function/include \ + -I$(srctree)/drivers/hdf/evdev \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_built-in.template b/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_built-in.template new file mode 100755 index 0000000000000000000000000000000000000000..9cf4aa1b484097c0d8d9ef49e666b41ae76277a8 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/Linux_built-in.template @@ -0,0 +1,4 @@ +! +// 56 ` +../../../../../framework/model/${module_lower_case}/${module_lower_case}.o/ +/0 0 0 0 644 1912 ` \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_BUILD_gn.template b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_BUILD_gn.template new file mode 100755 index 0000000000000000000000000000000000000000..9b648355eb7d5aa0d3a22b5087058c55462efa3b --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_BUILD_gn.template @@ -0,0 +1,41 @@ +# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import("//drivers/adapter/khdf/liteos/hdf.gni") +module_switch = defined(LOSCFG_DRIVERS_HDF_${module_upper_case}) +module_name = "hdf_${module_lower_case}_driver" +hdf_driver(module_name) { + FRAMEWORKS_${module_upper_case}_ROOT = "$HDF_FRAMEWORKS_PATH/model/${module_lower_case}" + sources = ["$FRAMEWORKS_${module_upper_case}_ROOT/driver/${driver_file_name}"] + + include_dirs = [ + "//third_party/FreeBSD/sys/dev/evdev/" + ] +} + diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Device_Info_Hcs.template b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Device_Info_Hcs.template new file mode 100755 index 0000000000000000000000000000000000000000..64e5b0c460cf7975a2835eebb6a590c61940e537 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Device_Info_Hcs.template @@ -0,0 +1,15 @@ +${driver_name} :: host{ + hostName = "${driver_name}_host"; // hostƣhostڵijһ + priority = 100; // hostȼ0-200ֵԽȼԽͣĬ100ȼͬ򲻱֤hostļ˳ + device_${driver_name} :: device { // ${driver_name}豸ڵ + device0 :: deviceNode { // ${driver_name}DeviceNodeڵ + policy = 2; // policyֶ񷢲IJ + priority= 100; // ȼ0-200ֵԽȼԽͣĬ100ȼͬ򲻱֤deviceļ˳ + preload = 0; // ֶ + permission = 0664; // 豸ڵȨ + moduleName = "${driver_name}_driver"; // ƣֶεֵڽṹmoduleNameֵһ + serviceName = "${driver_name}_service"; // ⷢƣΨһ + deviceMatchAttr = ""; // ˽ƥĹؼ֣˽ñеmatch_attrֵ + } + } +} diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Kconfig.template b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Kconfig.template new file mode 100755 index 0000000000000000000000000000000000000000..bb09830c85d108bbb4c4714cee09d7c02747e567 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Kconfig.template @@ -0,0 +1,34 @@ +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +config DRIVERS_HDF_${module_upper_case} + bool "Enable HDF ${module_lower_case} driver" + default n + depends on DRIVERS_HDF + help + Answer Y to enable HDF ${module_lower_case} driver. \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Makefile.template b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Makefile.template new file mode 100755 index 0000000000000000000000000000000000000000..e4574aff02ca7ca39debbeee3b860cd4358ad12c --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/Liteos_Makefile.template @@ -0,0 +1,39 @@ +# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of +# conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list +# of conditions and the following disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used +# to endorse or promote products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk + +${module_upper_case}_ROOT_DIR = $(LITEOSTOPDIR)/../../drivers/framework/model/${module_lower_case} + +ifeq ($(LOSCFG_DRIVERS_HDF_${module_upper_case}), y) +MODULE_NAME := hdf_${module_lower_case}_driver +LOCAL_INCLUDE := $(${module_upper_case}_ROOT_DIR)/../../../../../third_party/FreeBSD/sys/dev/evdev +LOCAL_SRCS += $(${module_upper_case}_ROOT_DIR)/driver/${driver_file_name} + +endif +include $(HDF_DRIVER) diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/device_info_hcs.template b/tools/hdf_dev_eco_tool/resources/templates/lite/device_info_hcs.template new file mode 100755 index 0000000000000000000000000000000000000000..edf95d033cf4c4c801f1c3f689114cc2f3a29fe0 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/device_info_hcs.template @@ -0,0 +1,15 @@ +${model_name} :: host{ + hostName = "${model_name}_host"; + priority = 100; + device_${model_name} :: device { + device0 :: deviceNode { + policy = 2; + priority= 100; + preload = 0; + permission = 0664; + moduleName = "${driver_name}_driver"; + serviceName = "${driver_name}_service"; + deviceMatchAttr = ""; + } + } +} diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver.c.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver.c.template new file mode 100755 index 0000000000000000000000000000000000000000..9d890166d01fb782f4b00594a08fce6d88df9c31 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver.c.template @@ -0,0 +1,41 @@ +#include "hdf_device_desc.h" // HDF框架对驱动开放相关能力接口的头文件 +#include "hdf_log.h" // HDF 框架提供的日志接口头文件 + +#define HDF_LOG_TAG ${driver_lower_case}_driver // 打印日志所包含的标签,如果不定义则用默认定义的HDF_TAG标签 + +//驱动服务结构的定义 +struct ITestDriverService { + struct IDeviceIoService ioService; // 服务结构的首个成员必须是IDeviceIoService类型的成员 + //以下可添加自定义的驱动的服务接口 +}; + +//驱动对外提供的服务能力,将相关的服务接口绑定到HDF框架 +static int32_t Hdf${driver_upper_camel_case}DriverBind(struct HdfDeviceObject *deviceObject) { + // deviceObject为HDF框架给每一个驱动创建的设备对象,用来保存设备相关的私有数据和服务接口 + HDF_LOGI("${driver_lower_case} driver bind success"); + return 0; +} + +// 驱动自身业务初始的接口 +static int32_t Hdf${driver_upper_camel_case}DriverInit(struct HdfDeviceObject *deviceObject) { + HDF_LOGI("Hello world"); + return 0; +} + +// 驱动资源释放的接口 +static void Hdf${driver_upper_camel_case}DriverRelease(struct HdfDeviceObject *deviceObject) { + HDF_LOGI("${driver_lower_case} driver Release success"); + return; +} + +// 定义驱动入口的对象,必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量 +struct HdfDriverEntry g_${driver_lower_case}DriverEntry = { + .moduleVersion = 1, + .moduleName = "${driver_lower_case}_driver", + .Bind = Hdf${driver_upper_camel_case}DriverBind, + .Init = Hdf${driver_upper_camel_case}DriverInit, + .Release = Hdf${driver_upper_camel_case}DriverRelease, +}; + +// 调用HDF_INIT将驱动入口注册到HDF框架中,在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动,当Init调用异常时,HDF框架会调用Release释放驱动资源并退出。 +HDF_INIT(g_${driver_lower_case}DriverEntry); diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver.h.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver.h.template new file mode 100755 index 0000000000000000000000000000000000000000..3e462409471bf66a767e75772500fc7109b78773 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver.h.template @@ -0,0 +1,10 @@ +#ifndef _${driver_upper_case}_DRIVER_H +#define _${driver_upper_case}_DRIVER_H + +#include "hdf_device_desc.h" + +struct ${driver_upper_camel_case}DriverService { + struct IDeviceIoService ${driver_lower_camel_case}Service; +}; + +#endif/*_${driver_upper_case}_DRIVER_H*/ diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_config.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_config.template new file mode 100755 index 0000000000000000000000000000000000000000..0657e42b742d17241e1674f90405beb9e1efcec9 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_config.template @@ -0,0 +1,3 @@ +root { + +} \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_kconfig.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_kconfig.template new file mode 100755 index 0000000000000000000000000000000000000000..ade08c636aa2aea31496185191f3be414fa3fe94 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_kconfig.template @@ -0,0 +1,6 @@ +config DRIVERS_HDF_${driver_upper_case} + bool "Enable HDF ${driver_lower_case} driver" + default n + depends on DRIVERS_HDF_${module_upper_case} + help + Answer Y to enable HDF ${driver_lower_case} driver. \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_makefile.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_makefile.template new file mode 100755 index 0000000000000000000000000000000000000000..19d4f048c58354c90f933820b1f0f580c951be89 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_makefile.template @@ -0,0 +1,10 @@ +include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk + +MODULE_NAME := hdf${module_lower_case} + +LOCAL_INCLUDE := ./include +LOCAL_INCLUDE += ./src + +LOCAL_SRCS += $(wildcard ./src/*.c) + +include $(HDF_DRIVER) diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config.template new file mode 100755 index 0000000000000000000000000000000000000000..2695a43a1f73605d96473b9cf6745e7896a24789 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config.template @@ -0,0 +1,38 @@ +root { + device_info { + match_attr = "hdf_manager"; + template host { + hostName = ""; + priority = 100; + template device { + template deviceNode { + policy = 0; + priority = 100; + preload = 0; + permission = 0666; + moduleName = ""; + serviceName = ""; + deviceMatchAttr = ""; + } + } + } + platform :: host { + hostName = "platform_host"; + } + display :: host { + hostName = "display_host"; + } + network :: host { + hostName = "network_host"; + } + storage :: host { + hostName = "storage_host"; + } + media :: host { + hostName = "media_host"; + } + common :: host { + hostName = "common_host"; + } + } +} diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config_device.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config_device.template new file mode 100755 index 0000000000000000000000000000000000000000..04806197d6af761f6efef028d8a859d84b3b219e --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config_device.template @@ -0,0 +1,10 @@ + device_${driver_lower_case} :: device { + device0 :: deviceNode { + policy = 1; + priority = 100; + preload = 0; + permission = 0666; + moduleName = "${driver_lower_case}"; + serviceName = "${driver_lower_camel_case}Service"; + } + } \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config_host.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config_host.template new file mode 100755 index 0000000000000000000000000000000000000000..1dac297e3f7928b8a86ff751c0660b495e69ec5a --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_driver_manager_config_host.template @@ -0,0 +1,3 @@ + ${module_lower_case} :: host { + hostName = "${module_lower_case}_host"; + } \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_hcs_makefile.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_hcs_makefile.template new file mode 100755 index 0000000000000000000000000000000000000000..33e9df73be4d093f69c818067654a25443c41f6f --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_hcs_makefile.template @@ -0,0 +1,7 @@ +include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk + +MODULE_NAME := hdf_config + +LOCAL_PLATFORM_HCS_SRC := hdf.hcs + +include $(HDF_DRIVER) diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_kconfig.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_kconfig.template new file mode 100755 index 0000000000000000000000000000000000000000..3a5127a057271e0a061a8c378b4afb5a15eb70c3 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_kconfig.template @@ -0,0 +1,6 @@ +config DRIVERS_HDF_${module_upper_case} + bool "Enable HDF ${module_lower_case} module" + default n + depends on DRIVERS_HDF + help + Answer Y to enable HDF ${module_lower_case} module. diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_mk.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_mk.template new file mode 100755 index 0000000000000000000000000000000000000000..a2c9859343779f94df777dbb68a8d43cbfc68f78 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_mk.template @@ -0,0 +1,4 @@ +include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk + +MODULE_NAME := hdf${module_lower_case} +include $(HDF_DRIVER) diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_mk_driver.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_mk_driver.template new file mode 100755 index 0000000000000000000000000000000000000000..6502fecbd37b6bc418803dd4f20d42552830020a --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_module_mk_driver.template @@ -0,0 +1,5 @@ +ifeq ($(LOSCFG_DRIVERS_HDF_${driver_upper_case}), y) + LOCAL_INCLUDE += ./${driver_lower_case}/include + LOCAL_INCLUDE += ./${driver_lower_case}/src + LOCAL_SRCS += $(wildcard ./${driver_lower_case}/src/*.c) +endif \ No newline at end of file diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_kconfig.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_kconfig.template new file mode 100755 index 0000000000000000000000000000000000000000..8b137891791fe96927ad78e64b0aad7bded08bdc --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_kconfig.template @@ -0,0 +1 @@ + diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_mk.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_mk.template new file mode 100755 index 0000000000000000000000000000000000000000..5c51b1ec423facfacf47c7a310701b976deff98d --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_mk.template @@ -0,0 +1,2 @@ +LITEOS_BASELIB += -lhdf_config +LIB_SUBDIRS += $(LITEOSTOPDIR)/../../${board_parent_path}/$(LITEOS_PLATFORM)/config diff --git a/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_mk_module.template b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_mk_module.template new file mode 100755 index 0000000000000000000000000000000000000000..14e31ffdd935b9d46c93d1b0e3478e8d76b2d0a7 --- /dev/null +++ b/tools/hdf_dev_eco_tool/resources/templates/lite/hdf_vendor_mk_module.template @@ -0,0 +1,4 @@ +ifeq ($(LOSCFG_DRIVERS_HDF_${module_upper_case}), y) + LITEOS_BASELIB += -lhdf_${module_lower_case}_driver + LIB_SUBDIRS += $(LITEOS_DRIVERS_HDF)/model/${module_lower_case} +endif \ No newline at end of file