• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2022 Huawei Device Co., Ltd.
5#
6# HDF is dual licensed: you can use it either under the terms of
7# the GPL, or the BSD license, at your option.
8# See the LICENSE file in the root of this repository for complete details.
9
10
11import os
12from string import Template
13
14import hdf_utils
15from ..liteos.gn_file_add_config import analyze_parent_path
16
17
18def find_makefile_file_end_index(date_lines, model_name):
19    file_end_flag = "ccflags-y"
20    end_index = 0
21    model_dir_name = ("%s_ROOT_DIR" % model_name.upper())
22    model_dir_value = ""
23
24    for index, line in enumerate(date_lines):
25        if line.startswith("#"):
26            continue
27        elif line.strip().startswith(file_end_flag):
28            end_index = index
29            break
30        elif line.strip().startswith(model_dir_name):
31            model_dir_value = line.split("=")[-1].strip()
32        else:
33            continue
34    result_tuple = (end_index, model_dir_name, model_dir_value)
35    return result_tuple
36
37
38def formate_mk_config_build(source_path, date_lines, devices, root):
39    if len(source_path) > 1:
40        sources_line = ""
41        obj_first_line = "\nobj-$(CONFIG_DRIVERS_HDF_${model_name_upper}_${driver_name_upper}) += \\" + "\n"
42        temp_line = "\t\t\t\t$(${file_parent_path})/${source_path}.o"
43        for source in source_path:
44            temp_handle = Template(temp_line.replace("$(", "temp_flag"))
45            temp_dict = analyze_parent_path(
46                date_lines, source, "", devices, root)
47            try:
48                temp_dict['source_path'] = temp_dict.get(
49                    'source_path').strip(".c")
50            except KeyError as _:
51                continue
52            finally:
53                pass
54            if source == source_path[-1]:
55                sources_line += temp_handle.substitute(
56                    temp_dict).replace("temp_flag", "$(") + "\n"
57            else:
58                sources_line += temp_handle.substitute(
59                    temp_dict).replace("temp_flag", "$(") + " \\" + "\n"
60        build_resource = obj_first_line + sources_line
61    else:
62        build_resource = "LOCAL_SRCS += $(${file_parent_path})/${source_path}\n"
63        for source in source_path:
64            temp_handle = Template(build_resource.replace("$(", "temp_flag"))
65            temp_dict = analyze_parent_path(
66                date_lines, source, "", devices, root)
67            build_resource = temp_handle.substitute(temp_dict).replace("temp_flag", "$(")
68    return build_resource
69
70
71def audio_linux_makefile_operation(path, args_tuple):
72    source_path, head_path, module, driver, root, devices = args_tuple
73    makefile_gn_path = path
74    date_lines = hdf_utils.read_file_lines(makefile_gn_path)
75    judge_result = judge_driver_config_exists(date_lines, driver_name=driver)
76    if judge_result:
77        return
78    build_resource = formate_mk_config_build(source_path, date_lines, devices, root)
79    head_line = []
80    ccflags_first_line = "\nccflags-$(CONFIG_DRIVERS_HDF_${model_name_upper}_${driver_name_upper}) += \\" + "\n"
81    temp_line = "\t\t\t\t-I$(srctree)/$(${file_parent_path})/${head_path}"
82    for head_file in head_path:
83        temp_handle = Template(temp_line.replace("$(", "temp_flag"))
84        temp_dict = analyze_parent_path(
85            date_lines, "", head_file, devices, root)
86        if head_file == head_path[-1]:
87            temp_str = "".join([temp_handle.substitute(
88                temp_dict).replace("temp_flag", "$("), "\n"])
89        else:
90            temp_str = "".join([temp_handle.substitute(
91                temp_dict).replace("temp_flag", "$("), " \\", "\n"])
92        head_line.append(temp_str)
93    build_head = ccflags_first_line + "".join(head_line)
94    makefile_add_template = build_resource + build_head
95    temp_handle = Template(makefile_add_template.replace("$(", "temp_flag"))
96    temp_replace = {
97        'model_name_upper': module.upper(),
98        'driver_name_upper': driver.upper(),
99    }
100    new_line = temp_handle.substitute(temp_replace).replace("temp_flag", "$(")
101    date_lines = date_lines + [new_line]
102    hdf_utils.write_file_lines(makefile_gn_path, date_lines)
103
104
105def judge_driver_config_exists(date_lines, driver_name):
106    for _, line in enumerate(date_lines):
107        if line.startswith("#"):
108            continue
109        elif line.find(driver_name) != -1:
110            return True
111    return False
112
113
114def linux_makefile_operation(path, driver_file_path, head_path, module, driver):
115    makefile_gn_path = path
116    date_lines = hdf_utils.read_file_lines(makefile_gn_path)
117    source_file_path = driver_file_path.replace('\\', '/')
118    result_tuple = find_makefile_file_end_index(date_lines, model_name=module)
119    judge_result = judge_driver_config_exists(date_lines, driver_name=driver)
120    if judge_result:
121        return
122    end_index, model_dir_name, model_dir_value = result_tuple
123
124    if module == "sensor":
125        first_line = "\nobj-$(CONFIG_DRIVERS_HDF_${model_name_upper}_${driver_name_upper}) += \\\n"
126        second_line = "              $(SENSOR_ROOT_CHIPSET)/${source_file_path}\n"
127        third_line = "\n"
128        makefile_add_template = first_line + second_line + third_line
129        temp_handle = Template(makefile_add_template.replace("$(", "temp_flag"))
130        str_start = source_file_path.find(module) + len(module) + 1
131        temp_replace = {
132            'model_name_upper': module.upper(),
133            'driver_name_upper': driver.upper(),
134            'source_file_path': source_file_path[str_start:].replace(".c", ".o")
135        }
136    else:
137        first_line = "\nobj-$(CONFIG_DRIVERS_HDF_${model_name_upper}_${driver_name_upper}) += \\\n"
138        second_line = "              $(${model_name_upper}_ROOT_DIR)/${source_file_path}\n"
139        third_line = "ccflags-y += -I$(srctree)/drivers/hdf/framework/model/${head_file_path}\n"
140        makefile_add_template = first_line + second_line + third_line
141        include_model_info = model_dir_value.split("model")[-1].strip('"') + "/"
142        makefile_path_config = source_file_path.split(include_model_info)
143        temp_handle = Template(makefile_add_template.replace("$(", "temp_flag"))
144        temp_replace = {
145            'model_name_upper': module.upper(),
146            'driver_name_upper': driver.upper(),
147            'source_file_path': makefile_path_config[-1].replace(".c", ".o"),
148            'head_file_path': '/'.join(head_path.split("model")[-1].strip(os.path.sep).split(os.path.sep)[:-1])
149        }
150    new_line = temp_handle.substitute(temp_replace).replace("temp_flag", "$(")
151    endif_status = False
152    for index, v in enumerate(date_lines[::-1]):
153        if v.strip().startswith("endif"):
154            endif_status = True
155            end_line_index = len(date_lines) - index - 1
156            date_lines = date_lines[:end_line_index] + [new_line] + [date_lines[end_line_index]]
157            break
158    if not endif_status:
159        date_lines = date_lines + [new_line]
160    hdf_utils.write_file_lines(makefile_gn_path, date_lines)
161