blob: 865301687ace3a06b4ca901136b738f871f550cc [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#----------------------------------------------------------------------------
2# Copyright (c) 2021 Arm Limited. All rights reserved.
3# SPDX-License-Identifier: Apache-2.0
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#----------------------------------------------------------------------------
17set(SCRIPTS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/scripts)
18
19##############################################################################
20# This function generates C++ files for images located in the directory it is
21# pointed at. NOTE: uses python
22##############################################################################
23function(generate_images_code input_dir src_out hdr_out img_size)
24
25 # Absolute paths for passing into python script
26 get_filename_component(input_dir_abs ${input_dir} ABSOLUTE)
27 get_filename_component(src_out_abs ${src_out} ABSOLUTE)
28 get_filename_component(hdr_out_abs ${hdr_out} ABSOLUTE)
29
30 message(STATUS "Generating image files from ${input_dir_abs}")
31 execute_process(
32 COMMAND ${PYTHON} ${SCRIPTS_DIR}/py/gen_rgb_cpp.py
33 --image_path ${input_dir_abs}
34 --source_folder_path ${src_out_abs}
35 --header_folder_path ${hdr_out_abs}
36 --image_size ${img_size} ${img_size}
37 RESULT_VARIABLE return_code
38 )
39 if (NOT return_code EQUAL "0")
40 message(FATAL_ERROR "Failed to generate image files.")
41 endif ()
42
43endfunction()
44
45##############################################################################
46# This function generates C++ files for audio files located in the directory it is
47# pointed at. NOTE: uses python
48##############################################################################
49function(generate_audio_code input_dir src_out hdr_out s_rate_opt mono_opt off_opt duration_opt res_type_opt min_sample_opt)
50
51 # Absolute paths for passing into python script
52 get_filename_component(input_dir_abs ${input_dir} ABSOLUTE)
53 get_filename_component(src_out_abs ${src_out} ABSOLUTE)
54 get_filename_component(hdr_out_abs ${hdr_out} ABSOLUTE)
55
56 to_py_bool(mono_opt mono_opt_py)
57
58 message(STATUS "Generating audio files from ${input_dir_abs}")
59 execute_process(
60 COMMAND ${PYTHON} ${SCRIPTS_DIR}/py/gen_audio_cpp.py
61 --audio_path ${input_dir_abs}
62 --source_folder_path ${src_out_abs}
63 --header_folder_path ${hdr_out_abs}
64 --sampling_rate ${s_rate_opt}
65 --mono ${mono_opt_py}
66 --offset ${off_opt}
67 --duration ${duration_opt}
68 --res_type ${res_type_opt}
69 --min_samples ${min_sample_opt}
70 RESULT_VARIABLE return_code
71 )
72 if (NOT return_code EQUAL "0")
73 message(FATAL_ERROR "Failed to generate audio files.")
74 endif ()
75
76endfunction()
77
78##############################################################################
79# This function generates default empty input C++ files for applications with no
80# external input. Main use is for the inference runner. NOTE: uses python
81##############################################################################
82function(generate_default_input_code hdr_out)
83
84 # Absolute paths for passing into python script
85 get_filename_component(hdr_out_abs ${hdr_out} ABSOLUTE)
86
87 message(STATUS "Generating default input files")
88 execute_process(
89 COMMAND ${PYTHON} ${SCRIPTS_DIR}/py/gen_default_input_cpp.py
90 --header_folder_path ${hdr_out_abs}
91 RESULT_VARIABLE return_code
92 )
93 if (NOT return_code EQUAL "0")
94 message(FATAL_ERROR "Failed to generate default input .")
95 endif ()
96
97endfunction()
98##############################################################################
99# This function generates C++ files for tflite NN model files.
100# @param[in] MODEL_PATH path to a tflite file
101# @param[in] DESTINATION directory in which the output cc must be
102# placed
103# @param[in] EXPRESSIONS C++ code expressions to add to the generated file
104# @param[in] NAMESPACE model name space
105# NOTE: Uses python
106##############################################################################
107function(generate_tflite_code)
108
109 set(multiValueArgs EXPRESSIONS NAMESPACE)
110 set(oneValueArgs MODEL_PATH DESTINATION)
111 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
112
113 # Absolute paths for passing into python script
114 get_filename_component(ABS_MODEL_PATH ${PARSED_MODEL_PATH} ABSOLUTE)
115 get_filename_component(ABS_DESTINATION ${PARSED_DESTINATION} ABSOLUTE)
116
117 if (EXISTS ${ABS_MODEL_PATH})
118 message(STATUS "Using ${ABS_MODEL_PATH}")
119 else ()
120 message(FATAL_ERROR "${ABS_MODEL_PATH} not found!")
121 endif ()
122
123
124 foreach(expression ${PARSED_EXPRESSIONS})
125 set(py_arg_exp ${py_arg_exp} --expression=${expression})
126 endforeach()
127
128 foreach(name ${PARSED_NAMESPACE})
129 set(py_arg_exp ${py_arg_exp} --namespaces=${name})
130 endforeach()
131
132 execute_process(
133 COMMAND ${PYTHON} ${SCRIPTS_DIR}/py/gen_model_cpp.py
134 --tflite_path ${ABS_MODEL_PATH}
135 --output_dir ${ABS_DESTINATION} ${py_arg_exp}
136 RESULT_VARIABLE return_code
137 )
138 if (NOT return_code EQUAL "0")
139 message(FATAL_ERROR "Failed to generate model files.")
140 endif ()
141endfunction()
142
143
144##############################################################################
145# This function generates C++ file for a given labels' text file.
146# @param[in] INPUT Path to the label text file
147# @param[in] DESTINATION_SRC directory in which the output cc must be
148# placed
149# @param[in] DESTINATION_HDR directory in which the output h file must be
150# placed
151# @param[in] OUTPUT_FILENAME Path to required output file
152# @param[in] NAMESPACE data name space
153# NOTE: Uses python
154##############################################################################
155function(generate_labels_code)
156
157 set(multiValueArgs NAMESPACE)
158 set(oneValueArgs INPUT DESTINATION_SRC DESTINATION_HDR OUTPUT_FILENAME)
159 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
160
161 # Absolute paths for passing into python script
162 get_filename_component(input_abs ${PARSED_INPUT} ABSOLUTE)
163 get_filename_component(src_out_abs ${PARSED_DESTINATION_SRC} ABSOLUTE)
164 get_filename_component(hdr_out_abs ${PARSED_DESTINATION_HDR} ABSOLUTE)
165
166 message(STATUS "Generating labels file from ${PARSED_INPUT}")
167 file(REMOVE "${hdr_out_abs}/${PARSED_OUTPUT_FILENAME}.hpp")
168 file(REMOVE "${src_out_abs}/${PARSED_OUTPUT_FILENAME}.cc")
169
170 foreach(name ${PARSED_NAMESPACE})
171 set(py_arg_exp ${py_arg_exp} --namespaces=${name})
172 endforeach()
173
174 message(STATUS "writing to ${hdr_out_abs}/${PARSED_OUTPUT_FILENAME}.hpp and ${src_out_abs}/${PARSED_OUTPUT_FILENAME}.cc")
175 execute_process(
176 COMMAND ${PYTHON} ${SCRIPTS_DIR}/py/gen_labels_cpp.py
177 --labels_file ${input_abs}
178 --source_folder_path ${src_out_abs}
179 --header_folder_path ${hdr_out_abs}
180 --output_file_name ${PARSED_OUTPUT_FILENAME} ${py_arg_exp}
181 RESULT_VARIABLE return_code
182 )
183 if (NOT return_code EQUAL "0")
184 message(FATAL_ERROR "Failed to generate label files.")
185 endif ()
186endfunction()
187
188
189##############################################################################
190# This function generates C++ data files for test located in the directory it is
191# pointed at.
192# @param[in] INPUT_DIR directory in which are the npy files
193# @param[in] DESTINATION_SRC directory in which the output cc must be
194# placed
195# @param[in] DESTINATION_HDR directory in which the output h file must be
196# placed
197# @param[in] USECASE name of the sub-usecase
198# @param[in] NAMESPACE data name space
199# NOTE: Uses python
200##############################################################################
201function(generate_test_data_code)
202
203 set(multiValueArgs NAMESPACE)
204 set(oneValueArgs INPUT_DIR DESTINATION_SRC DESTINATION_HDR USECASE)
205 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
206
207 # Absolute paths for passing into python script
208 get_filename_component(input_dir_abs ${PARSED_INPUT_DIR} ABSOLUTE)
209 get_filename_component(src_out_abs ${PARSED_DESTINATION_SRC} ABSOLUTE)
210 get_filename_component(hdr_out_abs ${PARSED_DESTINATION_HDR} ABSOLUTE)
211
212 foreach(name ${PARSED_NAMESPACE})
213 set(py_arg_exp ${py_arg_exp} --namespaces=${name})
214 endforeach()
215
216 message(STATUS "Generating test ifm and ofm files from ${input_dir_abs}")
217 execute_process(
218 COMMAND ${PYTHON} ${SCRIPTS_DIR}/py/gen_test_data_cpp.py
219 --data_folder_path ${input_dir_abs}
220 --source_folder_path ${src_out_abs}
221 --header_folder_path ${hdr_out_abs}
222 --usecase ${PARSED_USECASE}
223 ${py_arg_exp}
224 RESULT_VARIABLE return_code
225 )
226 if (NOT return_code EQUAL "0")
227 message(FATAL_ERROR "Failed to generate test data files.")
228 endif ()
229
230endfunction()
231
232
233##############################################################################
234# Function to prepare a python virtual environment for running the functions
235# outlined above.
236##############################################################################
237function(setup_source_generator)
238 if (${CMAKE_HOST_WIN32})
239# windows python3 has python.exe
240 set(PY_EXEC python)
241 set(PYTHON ${CMAKE_BINARY_DIR}/pyenv/Scripts/${PY_EXEC})
242 else()
243 set(PY_EXEC python3)
244 set(PYTHON ${CMAKE_BINARY_DIR}/pyenv/bin/${PY_EXEC})
245 endif()
246 set(PYTHON ${PYTHON} PARENT_SCOPE)
247
248 if (EXISTS ${PYTHON})
249 message(STATUS "Using existing python at ${PYTHON}")
250 return()
251 endif ()
252 message(STATUS "Configuring python environment at ${PYTHON}")
253 execute_process(
254 COMMAND ${PY_EXEC} -m venv ${CMAKE_BINARY_DIR}/pyenv
255 RESULT_VARIABLE return_code
256 )
257 if (NOT return_code EQUAL "0")
258 message(FATAL_ERROR "Failed to setup python3 environment")
259 endif ()
260
261 execute_process(COMMAND ${PYTHON} -m pip install wheel)
262
263 execute_process(
264 COMMAND ${PYTHON} -m pip install -r ${SCRIPTS_DIR}/py/requirements.txt
265 RESULT_VARIABLE return_code
266 )
267 if (NOT return_code EQUAL "0")
268 message(FATAL_ERROR "Failed to setup python3 environment")
269 endif ()
270endfunction()