blob: 49c12867e6a6b0d71493b1a19b0663652c85ffb8 [file] [log] [blame]
Gian Marco Iodice16824302017-09-28 15:41:37 +01001#!/usr/bin/env python
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +00002# -*- coding: utf-8 -*-
3
Jakub Sujak6e56bf32023-08-23 14:42:26 +01004# Copyright (c) 2017-2018, 2020-2021, 2023 Arm Limited.
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +00005#
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.
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025import glob
26import collections
27import os
28
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029armcv_path = "arm_compute"
Sang-Hoon Park03076262020-11-04 17:32:38 +000030src_path ="src"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
Sang-Hoon Park03076262020-11-04 17:32:38 +000032Target = collections.namedtuple('Target', 'name prefix basepath')
33
34core_targets = [
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000035 Target("NEON", "NE", src_path), # Arm® Neon™ kernels are under src
Sang-Hoon Park03076262020-11-04 17:32:38 +000036 Target("CL", "CL", src_path), # CL kernels are under src
Manuel Bottiniceaa0bf2021-02-16 15:15:19 +000037 Target("CPP", "CPP", armcv_path) # CPP kernels are under arm_compute
Sang-Hoon Park03076262020-11-04 17:32:38 +000038 ]
39
40# All functions are under arm_compute
41runtime_targets = [
42 Target("NEON", "NE", armcv_path),
43 Target("CL", "CL", armcv_path),
Manuel Bottiniceaa0bf2021-02-16 15:15:19 +000044 Target("CPP", "CPP", armcv_path)
Sang-Hoon Park03076262020-11-04 17:32:38 +000045 ]
46
47core_path = "/core/"
48runtime_path = "/runtime/"
49include_str = "#include \""
Anthony Barbier6ff3b192017-09-04 18:44:23 +010050
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]
Jakub Sujak6e56bf32023-08-23 14:42:26 +010077 updated_files.sort(key=lambda x: x.lower())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078 return updated_files
79
80
Sang-Hoon Park03076262020-11-04 17:32:38 +000081def include_components(target, path, header_prefix, folder, subfolders=None):
82 for t in target:
83 target_path = t.basepath + path + t.name + "/"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 components_file = target_path + t.prefix + header_prefix
85 if os.path.exists(components_file):
86 include_list = create_include_list(target_path + folder)
Anthony Barbier6a5627a2017-09-26 14:42:02 +010087 for s in subfolders or []:
88 include_list += create_include_list( target_path + folder + "/" + s)
Jakub Sujak6e56bf32023-08-23 14:42:26 +010089 include_list.sort(key=lambda x: x.lower())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 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
Sang-Hoon Park03076262020-11-04 17:32:38 +000098 include_components(core_targets, core_path, "Kernels.h", "kernels", ["arm32", "arm64"])
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099
100 # Include functions
Sang-Hoon Park03076262020-11-04 17:32:38 +0000101 include_components(runtime_targets, runtime_path, "Functions.h", "functions")