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