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 .gn_file_add_config import judge_driver_config_exists, analyze_parent_path
16
17
18def find_makefile_file_end_index(date_lines, model_name):
19    file_end_flag = "include $(HDF_DRIVER)"
20    end_index = 0
21    if model_name == "sensor":
22        model_dir_name = ("FRAMEWORKS_%s_ROOT" % model_name.upper())
23    else:
24        model_dir_name = ("%s_ROOT_DIR" % model_name.upper())
25    model_dir_value = ""
26
27    for index, line in enumerate(date_lines):
28        if line.startswith("#"):
29            continue
30        elif line.strip().startswith(file_end_flag):
31            end_index = index
32        elif line.strip().startswith(model_dir_name):
33            model_dir_value = line.split("=")[-1].strip()
34        else:
35            continue
36    result_tuple = (end_index, model_dir_name, model_dir_value)
37    return result_tuple
38
39
40def audio_makefile_file_operation(path, args_tuple):
41    source_path, head_path, module, driver, root, devices, kernel = args_tuple
42    makefile_gn_path = path
43    date_lines = hdf_utils.read_file_lines(makefile_gn_path)
44    judge_result = judge_driver_config_exists(date_lines, driver_name=driver)
45    if judge_result:
46        return
47    result_tuple = find_makefile_file_end_index(date_lines, model_name=module)
48    end_index, model_dir_name, model_dir_value = result_tuple
49    first_line = "\nifeq ($(LOSCFG_DRIVERS_HDF_${model_name_upper}_${driver_name_upper}), y)\n"
50    third_line = "LOCAL_INCLUDE += $(${file_parent_path})/${head_path}\n"
51    four_line = "endif\n"
52
53    if len(source_path) > 1:
54        sources_line_items = []
55        multi_resource = "LOCAL_SRCS += "
56        temp_line = r'$(${file_parent_path})/${source_path}'
57        for source in source_path:
58            temp_handle = Template(temp_line.replace("$(", "temp_flag"))
59            temp_dict = analyze_parent_path(
60                    date_lines, source, "", devices, root)
61            if source == source_path[-1]:
62                sources_line_items.append(len(multi_resource) * " ")
63                sources_line_items.append(temp_handle.substitute(temp_dict).replace("temp_flag", "$("))
64                sources_line_items.append("\n")
65            else:
66                sources_line_items.append(temp_handle.substitute(temp_dict).replace("temp_flag", "$("))
67                sources_line_items.append(" \\\n")
68        build_resource = "{}{}".format(multi_resource, "".join(sources_line_items))
69    else:
70        build_resource = "LOCAL_SRCS += $(${file_parent_path})/${source_path}\n"
71        for source in source_path:
72            temp_handle = Template(build_resource.replace("$(", "temp_flag"))
73            temp_dict = analyze_parent_path(
74                    date_lines, source, "", devices, root)
75            build_resource = temp_handle.substitute(temp_dict).replace("temp_flag", "$(")
76    makefile_add_template = "{}{}{}{}".format(first_line, build_resource, third_line, four_line)
77    makefile_replace_dict = analyze_parent_path(
78        date_lines, "", head_path[0], devices, root, kernel_type=kernel)
79    temp_handle = Template(makefile_add_template.replace("$(", "temp_flag"))
80    model_dict = {
81        'model_name_upper': module.upper(),
82        'driver_name_upper': driver.upper(),
83    }
84    makefile_replace_dict.update(model_dict)
85    new_line = temp_handle.substitute(makefile_replace_dict).replace("temp_flag", "$(")
86    date_lines = date_lines[:end_index - 1] + [new_line] + date_lines[end_index - 1:]
87    hdf_utils.write_file_lines(makefile_gn_path, date_lines)
88
89
90def makefile_file_operation(path, driver_file_path, head_path,
91                            module, driver, root_path):
92    makefile_gn_path = path
93    date_lines = hdf_utils.read_file_lines(makefile_gn_path)
94    judge_result = judge_driver_config_exists(date_lines, driver_name=driver)
95    if judge_result:
96        return
97    source_file_path = driver_file_path.replace('\\', '/')
98    result_tuple = find_makefile_file_end_index(date_lines, model_name=module)
99    end_index, model_dir_name, model_dir_value = result_tuple
100    makefile_add_template = get_liteos_mk_template(module)
101    include_model_info = model_dir_value.split("model")[-1].strip('"') + "/"
102    makefile_path_config = source_file_path.split(include_model_info)
103    temp_handle = Template(makefile_add_template.replace("$(", "temp_flag"))
104    base_replace_dict = {
105        'model_name_upper': module.upper(),
106        'driver_name_upper': driver.upper(),
107    }
108    if module == "display":
109        temp_replace = {
110            'source_file_path': '/'.join(
111                list(filter(lambda x: x, driver_file_path.split(root_path)[-1].
112                            strip(os.path.sep).split(os.path.sep)))),
113            'head_file_path': '/'.join(
114                list(filter(lambda x: x, head_path.split(root_path)[-1].
115                            strip(os.path.sep).split(os.path.sep)))[:-1])}
116    elif module == "sensor":
117        temp_replace = {
118            'source_file_path': source_file_path.split(module, 1)[-1][1:],
119            'head_file_path': '/'.join(list(filter(
120                lambda x: x, head_path.split(module, 1)[-1][1:].
121                    split(os.path.sep)))[:-1]).strip(os.path.sep)}
122    else:
123        temp_replace = {
124            'source_file_path': makefile_path_config[-1],
125            'head_file_path': '/'.join(
126                list(filter(lambda x: x, head_path.split("model")[-1].strip(
127                    os.path.sep).split(os.path.sep)[2:-1])))}
128    temp_replace.update(base_replace_dict)
129    new_line = temp_handle.substitute(temp_replace).replace("temp_flag", "$(")
130    endif_status = False
131    for index, v in enumerate(date_lines[::-1]):
132        if v.strip().startswith("endif"):
133            endif_status = True
134            end_line_index = len(date_lines) - index - 1
135            date_lines = date_lines[:end_line_index] + [new_line] + \
136                         date_lines[end_line_index:]
137            break
138    if not endif_status:
139        date_lines = date_lines + [new_line]
140    hdf_utils.write_file_lines(makefile_gn_path, date_lines)
141
142
143def get_liteos_mk_template(module):
144    first_line = "\nifeq ($(LOSCFG_DRIVERS_HDF_${model_name_upper}_${driver_name_upper}), y)\n"
145    input_include_line = "LOCAL_INCLUDE += $(${model_name_upper}_ROOT_DIR)/${head_file_path}\n"
146    input_second_line = "LOCAL_SRCS += $(${model_name_upper}_ROOT_DIR)/${source_file_path}\n"
147
148    sensor_include_line = "LOCAL_INCLUDE += $(PERIPHERAL_${model_name_upper}_ROOT)/${head_file_path}\n"
149    sensor_second_line = "LOCAL_SRCS += $(PERIPHERAL_${model_name_upper}_ROOT)/${source_file_path}\n"
150
151    display_include_line = "LOCAL_INCLUDE += $(LITEOSTOPDIR)/../../${head_file_path}\n"
152    display_second_line = "LOCAL_SRCS += $(LITEOSTOPDIR)/../../${source_file_path}\n"
153    third_line = "endif\n"
154    if module == "sensor":
155        makefile_add_template = first_line + sensor_include_line \
156                                + sensor_second_line + third_line
157    elif module == "display":
158        makefile_add_template = first_line + display_include_line \
159                                + display_second_line + third_line
160    else:
161        makefile_add_template = first_line + input_include_line \
162                                + input_second_line + third_line
163    return makefile_add_template