Gunes Bayir | 66b4a6a | 2023-07-01 22:55:42 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright (c) 2023 Arm Limited. |
| 4 | # |
| 5 | # SPDX-License-Identifier: MIT |
| 6 | # |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | # of this software and associated documentation files (the "Software"), to |
| 9 | # deal in the Software without restriction, including without limitation the |
| 10 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 11 | # sell copies of the Software, and to permit persons to whom the Software is |
| 12 | # furnished to do so, subject to the following conditions: |
| 13 | # |
| 14 | # The above copyright notice and this permission notice shall be included in all |
| 15 | # copies or substantial portions of the Software. |
| 16 | # |
| 17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | # SOFTWARE. |
| 24 | |
| 25 | import argparse |
| 26 | import os |
| 27 | from jinja2 import Template |
| 28 | import datetime |
| 29 | |
| 30 | # Paths to exclude |
| 31 | excluded_paths = ["build", |
| 32 | "compute_kernel_writer/build", |
| 33 | "compute_kernel_writer/src", |
| 34 | "compute_kernel_writer/validation", |
| 35 | "docs/", |
| 36 | "documentation/", |
| 37 | "examples/", |
| 38 | "opencl-1.2-stubs/", |
| 39 | "release_repository/", |
| 40 | "opengles-3.1-stubs/", |
| 41 | "scripts/", |
| 42 | "tests/", |
| 43 | "/GLES_COMPUTE/", |
| 44 | "/graph/", |
| 45 | "/sve/", |
| 46 | "/SVE/", |
| 47 | "/sve2/", |
| 48 | "/SVE2/" |
| 49 | ] |
| 50 | |
| 51 | excluded_files = ["TracePoint.cpp"] |
| 52 | |
| 53 | # Android bp template to render |
| 54 | year = datetime.datetime.now().year |
| 55 | |
| 56 | bp_tm = Template( |
| 57 | """// |
| 58 | // Copyright © 2020-""" + str(year) + """ Arm Ltd. All rights reserved. |
| 59 | // SPDX-License-Identifier: MIT |
| 60 | // |
| 61 | |
| 62 | // OpenCL sources are NOT required by ArmNN or its Android NNAPI driver and are used for CI purposes only. |
| 63 | opencl_srcs = [ |
| 64 | {% for cl_src in cl_srcs -%} |
| 65 | "{{ cl_src }}", |
| 66 | {% endfor %} |
| 67 | ] |
| 68 | |
| 69 | bootstrap_go_package { |
| 70 | name: "arm_compute_library_nn_driver", |
| 71 | pkgPath: "arm_compute_library_nn_driver", |
| 72 | deps: [ |
| 73 | "blueprint", |
| 74 | "blueprint-pathtools", |
| 75 | "blueprint-proptools", |
| 76 | "soong", |
| 77 | "soong-android", |
| 78 | "soong-cc", |
| 79 | ], |
| 80 | srcs: [ |
| 81 | "scripts/arm_compute_library_nn_driver.go", |
| 82 | ], |
| 83 | pluginFor: [ "soong_build" ], |
| 84 | } |
| 85 | |
| 86 | arm_compute_library_defaults { |
| 87 | name: "acl-default-cppflags", |
| 88 | cppflags: [ |
| 89 | "-std=c++14", |
| 90 | "-fexceptions", |
| 91 | "-DBOOST_NO_AUTO_PTR", |
| 92 | "-DEMBEDDED_KERNELS", |
| 93 | "-DARM_COMPUTE_ASSERTS_ENABLED", |
| 94 | "-DARM_COMPUTE_CPP_SCHEDULER", |
| 95 | "-DENABLE_NEON", |
| 96 | "-DARM_COMPUTE_ENABLE_NEON", |
| 97 | "-Wno-unused-parameter", |
| 98 | "-DNO_DOT_IN_TOOLCHAIN", |
| 99 | "-Wno-implicit-fallthrough", |
| 100 | "-fPIC", |
| 101 | "-DACL_INTERNAL_TEST_CKW_IN_DF" |
| 102 | ], |
| 103 | rtti: true, |
| 104 | } |
| 105 | |
| 106 | cc_library_static { |
| 107 | name: "arm_compute_library", |
| 108 | defaults: ["acl-default-cppflags"], |
| 109 | proprietary: true, |
| 110 | local_include_dirs: ["build/android-arm64v8a/src/core", |
| 111 | "build/android-arm64v8a/src/core/CL", |
| 112 | "compute_kernel_writer/prototype/include", |
| 113 | "compute_kernel_writer/prototype", |
| 114 | "src/core/common", |
| 115 | "src/core/helpers", |
| 116 | "src/core/NEON/kernels/arm_gemm", |
| 117 | "src/core/NEON/kernels/assembly", |
| 118 | "src/core/NEON/kernels/convolution/common", |
| 119 | "src/core/NEON/kernels/convolution/winograd", |
| 120 | "src/cpu/kernels/assembly"], |
| 121 | export_include_dirs: [".", "./include"], |
| 122 | srcs: [ |
| 123 | {% for src in srcs -%} |
| 124 | "{{ src }}", |
| 125 | {% endfor %} |
| 126 | ], |
| 127 | arch: { |
| 128 | arm: { |
| 129 | srcs: [ |
| 130 | {% for arm_src in arm_srcs -%} |
| 131 | "{{ arm_src }}", |
| 132 | {% endfor %} |
| 133 | ], |
| 134 | }, |
| 135 | arm64: { |
| 136 | srcs: [ |
| 137 | {% for arm64_src in arm64_srcs -%} |
| 138 | "{{ arm64_src }}", |
| 139 | {% endfor %} |
| 140 | ], |
| 141 | }, |
| 142 | }, |
| 143 | rtti: true, |
| 144 | } |
| 145 | """) |
| 146 | |
| 147 | |
| 148 | def generate_bp_file(cpp_files, opencl_files): |
| 149 | arm_files = [f for f in cpp_files if "a32_" in f] |
| 150 | arm64_files = [f for f in cpp_files if any(a64 in f for a64 in ["a64_", "sve_", 'sme_', 'sme2_'])] |
| 151 | gen_files = [x for x in cpp_files if x not in arm_files + arm64_files] |
| 152 | |
| 153 | arm_files.sort() |
| 154 | arm64_files.sort() |
| 155 | gen_files.sort() |
| 156 | opencl_files.sort() |
| 157 | |
| 158 | bp_file = bp_tm.render(srcs=gen_files, |
| 159 | arm_srcs=arm_files, |
| 160 | arm64_srcs=arm64_files, |
| 161 | cl_srcs=opencl_files) |
| 162 | return bp_file |
| 163 | |
| 164 | |
| 165 | def list_all_files(repo_path): |
| 166 | """ Gets the list of files to include to the Android.bp |
| 167 | |
| 168 | :param repo_path: Path of the repository |
| 169 | :return: The filtered list of useful filess |
| 170 | """ |
| 171 | if not repo_path.endswith('/'): |
| 172 | repo_path = repo_path + "/" |
| 173 | |
| 174 | # Get cpp files |
| 175 | cpp_files = [] |
| 176 | cl_files = [] |
| 177 | for path, subdirs, files in os.walk(repo_path): |
| 178 | for file in files: |
| 179 | if file.endswith(".cpp"): |
| 180 | cpp_files.append(os.path.join(path, file)) |
| 181 | elif file.endswith(".cl"): |
| 182 | cl_files.append(os.path.join(path, file)) |
| 183 | # Include CL headers |
| 184 | if "src/core/CL/cl_kernels" in path and file.endswith(".h"): |
| 185 | cl_files.append(os.path.join(path, file)) |
| 186 | # Filter out unused cpp files |
| 187 | filtered_cpp_files = [] |
| 188 | for cpp_file in cpp_files: |
| 189 | if any(ep in cpp_file for ep in excluded_paths) or any(ef in cpp_file for ef in excluded_files): |
| 190 | continue |
| 191 | filtered_cpp_files.append(cpp_file.replace(repo_path, "")) |
| 192 | # Filter out unused cl files |
| 193 | filtered_cl_files = [] |
| 194 | for cl_file in cl_files: |
| 195 | if any(ep in cl_file for ep in excluded_paths): |
| 196 | continue |
| 197 | filtered_cl_files.append(cl_file.replace(repo_path, "")) |
| 198 | |
| 199 | return filtered_cpp_files, filtered_cl_files |
| 200 | |
| 201 | |
| 202 | if __name__ == "__main__": |
| 203 | # Parse arguments |
| 204 | parser = argparse.ArgumentParser('Generate Android.bp file for ComputeLibrary') |
| 205 | parser.add_argument('--folder', default=".", metavar="folder", dest='folder', type=str, required=False, help='Compute Library source path') |
| 206 | parser.add_argument('--output_file', metavar="output_file", default='Android.bp', type=str, required=False, help='Specify Android bp output file') |
| 207 | args = parser.parse_args() |
| 208 | |
| 209 | cpp_files, opencl_files = list_all_files(args.folder) |
| 210 | bp_file = generate_bp_file(cpp_files, opencl_files) |
| 211 | |
| 212 | with open(args.output_file, 'w') as f: |
| 213 | f.write(bp_file) |