xref: /aosp_15_r20/external/ComputeLibrary/scripts/include_functions_kernels.py (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (c) 2017-2018, 2020-2021 Arm Limited.
5#
6# SPDX-License-Identifier: MIT
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to
10# deal in the Software without restriction, including without limitation the
11# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12# sell copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in all
16# copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24# SOFTWARE.
25import glob
26import collections
27import os
28
29armcv_path = "arm_compute"
30src_path ="src"
31
32Target = collections.namedtuple('Target', 'name prefix basepath')
33
34core_targets = [
35    Target("NEON", "NE", src_path),             # Arm® Neon™ kernels are under src
36    Target("CL", "CL", src_path),               # CL kernels are under src
37    Target("CPP", "CPP", armcv_path)            # CPP kernels are under arm_compute
38    ]
39
40# All functions are under arm_compute
41runtime_targets = [
42    Target("NEON", "NE", armcv_path),
43    Target("CL", "CL", armcv_path),
44    Target("CPP", "CPP", armcv_path)
45    ]
46
47core_path = "/core/"
48runtime_path = "/runtime/"
49include_str = "#include \""
50
51def read_file(file):
52    with open(file, "r") as f:
53        lines = f.readlines()
54    return lines
55
56
57def write_file(file, lines):
58    with open(file, "w") as f:
59        for line in lines:
60            f.write(line)
61
62
63def remove_existing_includes(lines):
64    first_pos = next(i for i, line in enumerate(lines) if include_str in line)
65    return [x for x in lines if not x.startswith(include_str)], first_pos
66
67
68def add_updated_includes(lines, pos, includes):
69    lines[pos:pos] = includes
70    return lines
71
72
73def create_include_list(folder):
74    files_path = folder + "/*.h"
75    files = glob.glob(files_path)
76    updated_files = [include_str + folder + "/" + x.rsplit('/',1)[1] + "\"\n" for x in files]
77    updated_files.sort()
78    return updated_files
79
80
81def include_components(target, path, header_prefix, folder, subfolders=None):
82    for t in target:
83        target_path = t.basepath + path +  t.name + "/"
84        components_file = target_path + t.prefix + header_prefix
85        if os.path.exists(components_file):
86            include_list = create_include_list(target_path + folder)
87            for s in subfolders or []:
88                include_list += create_include_list( target_path + folder + "/" + s)
89            include_list.sort()
90            lines = read_file(components_file)
91            lines, first_pos = remove_existing_includes(lines)
92            lines = add_updated_includes(lines, first_pos, include_list)
93            write_file(components_file, lines)
94
95
96if __name__ == "__main__":
97    # Include kernels
98    include_components(core_targets, core_path, "Kernels.h", "kernels", ["arm32", "arm64"])
99
100    # Include functions
101    include_components(runtime_targets, runtime_path, "Functions.h", "functions")
102