ONE - On-device Neural Engine
Loading...
Searching...
No Matches
resolve_includes.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3# Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17# Copyright (c) 2016, 2017 ARM Limited.
18#
19# SPDX-License-Identifier: MIT
20#
21# Permission is hereby granted, free of charge, to any person obtaining a copy
22# of this software and associated documentation files (the "Software"), to
23# deal in the Software without restriction, including without limitation the
24# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
25# sell copies of the Software, and to permit persons to whom the Software is
26# furnished to do so, subject to the following conditions:
27#
28# The above copyright notice and this permission notice shall be included in all
29# copies or substantial portions of the Software.
30#
31# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37# SOFTWARE.
38
39import collections
40import os.path
41import re
42import subprocess
43import glob
44
45
46def resolve_includes(target, source):
47 # File collection
48 FileEntry = collections.namedtuple('FileEntry', 'target_name file_contents')
49
50 # Include pattern
51 pattern = re.compile("#include \"(.*)\"")
52
53 # Get file contents
54 files = []
55 for i in range(len(source)):
56 src = source[i]
57 dst = target[i]
58 f = open(src)
59 cts = f.read()
60 f.close()
61 contents = cts.splitlines()
62 entry = FileEntry(target_name=dst, file_contents=contents)
63 files.append((os.path.basename(src), entry))
64
65 # Create dictionary of tupled list
66 files_dict = dict(files)
67
68 # Check for includes (can only be files in the same folder)
69 final_files = []
70 for file in files:
71 done = False
72 tmp_file = file[1].file_contents
73 print(file[1].target_name)
74 while not done:
75 file_count = 0
76 updated_file = []
77 for line in tmp_file:
78 found = pattern.search(line)
79 if found:
80 include_file = found.group(1)
81 data = files_dict[include_file].file_contents
82 updated_file.extend(data)
83 else:
84 updated_file.append(line)
85 file_count += 1
86
87 # Check if all include are replaced.
88 if file_count == len(tmp_file):
89 done = True
90
91 # Update temp file
92 tmp_file = updated_file
93
94 # Append and prepend string literal identifiers and add expanded file to final list
95 tmp_file.insert(0, "R\"(\n")
96 tmp_file.append("\n)\"")
97 entry = FileEntry(target_name=file[1].target_name, file_contents=tmp_file)
98 final_files.append((file[0], entry))
99
100 # Write output files
101 for file in final_files:
102 with open(file[1].target_name, 'w+') as out_file:
103 out_file.write("\n".join(file[1].file_contents))
104
105
106# Generate embed files
107cl_files = glob.glob('src/core/CL/cl_kernels/*.cl')
108cl_files += glob.glob('src/core/CL/cl_kernels/*.h')
109
110# DEBUG: print cl files
111print("cl_files:")
112print(cl_files)
113
114embed_files = [f + "embed" for f in cl_files]
115print("embed_files:")
116print(embed_files)
117
118resolve_includes(embed_files, cl_files)