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