1#!/usr/bin/env python 2import glob 3import collections 4import os 5 6armcv_path = "arm_compute" 7src_path ="src" 8 9Target = collections.namedtuple('Target', 'name prefix basepath') 10 11core_targets = [ 12 Target("NEON", "NE", src_path), # NEON kernels are under src 13 Target("CL", "CL", src_path), # CL kernels are under src 14 Target("CPP", "CPP", armcv_path), # CPP kernels are under arm_compute 15 Target("GLES_COMPUTE", "GC", armcv_path) # GLES kernels are under arm_compute 16 ] 17 18# All functions are under arm_compute 19runtime_targets = [ 20 Target("NEON", "NE", armcv_path), 21 Target("CL", "CL", armcv_path), 22 Target("CPP", "CPP", armcv_path), 23 Target("GLES_COMPUTE", "GC", armcv_path) 24 ] 25 26core_path = "/core/" 27runtime_path = "/runtime/" 28include_str = "#include \"" 29 30def read_file(file): 31 with open(file, "r") as f: 32 lines = f.readlines() 33 return lines 34 35 36def write_file(file, lines): 37 with open(file, "w") as f: 38 for line in lines: 39 f.write(line) 40 41 42def remove_existing_includes(lines): 43 first_pos = next(i for i, line in enumerate(lines) if include_str in line) 44 return [x for x in lines if not x.startswith(include_str)], first_pos 45 46 47def add_updated_includes(lines, pos, includes): 48 lines[pos:pos] = includes 49 return lines 50 51 52def create_include_list(folder): 53 files_path = folder + "/*.h" 54 files = glob.glob(files_path) 55 updated_files = [include_str + folder + "/" + x.rsplit('/',1)[1] + "\"\n" for x in files] 56 updated_files.sort() 57 return updated_files 58 59 60def include_components(target, path, header_prefix, folder, subfolders=None): 61 for t in target: 62 target_path = t.basepath + path + t.name + "/" 63 components_file = target_path + t.prefix + header_prefix 64 if os.path.exists(components_file): 65 include_list = create_include_list(target_path + folder) 66 for s in subfolders or []: 67 include_list += create_include_list( target_path + folder + "/" + s) 68 include_list.sort() 69 lines = read_file(components_file) 70 lines, first_pos = remove_existing_includes(lines) 71 lines = add_updated_includes(lines, first_pos, include_list) 72 write_file(components_file, lines) 73 74 75if __name__ == "__main__": 76 # Include kernels 77 include_components(core_targets, core_path, "Kernels.h", "kernels", ["arm32", "arm64"]) 78 79 # Include functions 80 include_components(runtime_targets, runtime_path, "Functions.h", "functions") 81