Gian Marco Iodice | 1682430 | 2017-09-28 15:41:37 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 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. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | import glob |
| 26 | import collections |
| 27 | import os |
| 28 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | armcv_path = "arm_compute" |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 30 | src_path ="src" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 32 | Target = collections.namedtuple('Target', 'name prefix basepath') |
| 33 | |
| 34 | core_targets = [ |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 35 | Target("NEON", "NE", src_path), # Arm® Neon™ kernels are under src |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 36 | Target("CL", "CL", src_path), # CL kernels are under src |
Manuel Bottini | ceaa0bf | 2021-02-16 15:15:19 +0000 | [diff] [blame] | 37 | Target("CPP", "CPP", armcv_path) # CPP kernels are under arm_compute |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 38 | ] |
| 39 | |
| 40 | # All functions are under arm_compute |
| 41 | runtime_targets = [ |
| 42 | Target("NEON", "NE", armcv_path), |
| 43 | Target("CL", "CL", armcv_path), |
Manuel Bottini | ceaa0bf | 2021-02-16 15:15:19 +0000 | [diff] [blame] | 44 | Target("CPP", "CPP", armcv_path) |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 45 | ] |
| 46 | |
| 47 | core_path = "/core/" |
| 48 | runtime_path = "/runtime/" |
| 49 | include_str = "#include \"" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 50 | |
| 51 | def read_file(file): |
| 52 | with open(file, "r") as f: |
| 53 | lines = f.readlines() |
| 54 | return lines |
| 55 | |
| 56 | |
| 57 | def write_file(file, lines): |
| 58 | with open(file, "w") as f: |
| 59 | for line in lines: |
| 60 | f.write(line) |
| 61 | |
| 62 | |
| 63 | def 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 | |
| 68 | def add_updated_includes(lines, pos, includes): |
| 69 | lines[pos:pos] = includes |
| 70 | return lines |
| 71 | |
| 72 | |
| 73 | def 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 | |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 81 | def include_components(target, path, header_prefix, folder, subfolders=None): |
| 82 | for t in target: |
| 83 | target_path = t.basepath + path + t.name + "/" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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) |
Anthony Barbier | 6a5627a | 2017-09-26 14:42:02 +0100 | [diff] [blame] | 87 | for s in subfolders or []: |
| 88 | include_list += create_include_list( target_path + folder + "/" + s) |
| 89 | include_list.sort() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | |
| 96 | if __name__ == "__main__": |
| 97 | # Include kernels |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 98 | include_components(core_targets, core_path, "Kernels.h", "kernels", ["arm32", "arm64"]) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | |
| 100 | # Include functions |
Sang-Hoon Park | 0307626 | 2020-11-04 17:32:38 +0000 | [diff] [blame] | 101 | include_components(runtime_targets, runtime_path, "Functions.h", "functions") |