alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | #---------------------------------------------------------------------------- |
| 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 | #---------------------------------------------------------------------------- |
| 17 | set(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 | ############################################################################## |
| 23 | function(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 | |
| 43 | endfunction() |
| 44 | |
| 45 | ############################################################################## |
| 46 | # This function generates C++ files for audio files located in the directory it is |
| 47 | # pointed at. NOTE: uses python |
| 48 | ############################################################################## |
| 49 | function(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 | |
| 76 | endfunction() |
| 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 | ############################################################################## |
| 82 | function(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 | |
| 97 | endfunction() |
| 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 | ############################################################################## |
| 107 | function(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 () |
| 141 | endfunction() |
| 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 | ############################################################################## |
| 155 | function(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 () |
| 186 | endfunction() |
| 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 |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 197 | # @param[in] NAMESPACE data name space |
| 198 | # NOTE: Uses python |
| 199 | ############################################################################## |
| 200 | function(generate_test_data_code) |
| 201 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 202 | set(multiValueArgs NAMESPACE INPUT_DIR) |
| 203 | set(oneValueArgs DESTINATION_SRC DESTINATION_HDR) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 204 | cmake_parse_arguments(PARSED "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) |
| 205 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 206 | list(LENGTH PARSED_INPUT_DIR input_dir_length) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 207 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 208 | if (${input_dir_length} GREATER 1) |
| 209 | set(add_extra_namespace TRUE) |
| 210 | else() |
| 211 | set(add_extra_namespace FALSE) |
| 212 | endif() |
| 213 | |
| 214 | foreach(input_dir ${PARSED_INPUT_DIR}) |
| 215 | unset(py_arg_exp) |
| 216 | file(GLOB_RECURSE input_data_files |
| 217 | "${input_dir}/*.npy" |
| 218 | ) |
| 219 | # no input NPY data found => skip code generation. |
| 220 | if(NOT input_data_files) |
Kshitij Sisodia | b59ba68 | 2021-11-23 17:19:52 +0000 | [diff] [blame] | 221 | message(WARNING "No files were found to generate input data: ${input_dir}") |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 222 | break() |
| 223 | endif() |
| 224 | |
| 225 | # Absolute paths for passing into python script |
| 226 | get_filename_component(input_dir_abs ${input_dir} ABSOLUTE) |
| 227 | get_filename_component(input_dir_name ${input_dir} NAME) |
| 228 | get_filename_component(src_out_abs ${PARSED_DESTINATION_SRC} ABSOLUTE) |
| 229 | get_filename_component(hdr_out_abs ${PARSED_DESTINATION_HDR} ABSOLUTE) |
| 230 | |
| 231 | foreach(name ${PARSED_NAMESPACE}) |
| 232 | set(py_arg_exp ${py_arg_exp} --namespaces=${name}) |
| 233 | endforeach() |
| 234 | |
| 235 | if (${add_extra_namespace}) |
| 236 | set(py_arg_exp ${py_arg_exp} --namespaces=${input_dir_name}) |
| 237 | endif() |
| 238 | |
| 239 | message(STATUS "Generating test ifm and ofm files from ${input_dir_abs}") |
| 240 | execute_process( |
| 241 | COMMAND ${PYTHON} ${SCRIPTS_DIR}/py/gen_test_data_cpp.py |
| 242 | --data_folder_path ${input_dir_abs} |
| 243 | --source_folder_path ${src_out_abs} |
| 244 | --header_folder_path ${hdr_out_abs} |
| 245 | --usecase ${input_dir_name} |
| 246 | ${py_arg_exp} |
| 247 | RESULT_VARIABLE return_code |
| 248 | ) |
| 249 | if (NOT return_code EQUAL "0") |
| 250 | message(FATAL_ERROR "Failed to generate test data files.") |
| 251 | endif () |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 252 | endforeach() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 253 | endfunction() |
| 254 | |
| 255 | |
| 256 | ############################################################################## |
| 257 | # Function to prepare a python virtual environment for running the functions |
| 258 | # outlined above. |
| 259 | ############################################################################## |
| 260 | function(setup_source_generator) |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 261 | |
| 262 | # If a virtual env has been created in the resources_downloaded directory, |
| 263 | # use it for source generator. Else, fall back to creating a virtual env |
| 264 | # in the current build directory. |
| 265 | if (EXISTS ${RESOURCES_DIR}/env) |
| 266 | set(DEFAULT_VENV_DIR ${RESOURCES_DIR}/env) |
| 267 | else() |
| 268 | set(DEFAULT_VENV_DIR ${CMAKE_BINARY_DIR}/venv) |
| 269 | endif() |
| 270 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 271 | if (${CMAKE_HOST_WIN32}) |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 272 | # Windows Python3 is python.exe |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 273 | set(PY_EXEC python) |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 274 | set(PYTHON ${DEFAULT_VENV_DIR}/Scripts/${PY_EXEC}) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 275 | else() |
| 276 | set(PY_EXEC python3) |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 277 | set(PYTHON ${DEFAULT_VENV_DIR}/bin/${PY_EXEC}) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 278 | endif() |
| 279 | set(PYTHON ${PYTHON} PARENT_SCOPE) |
Kshitij Sisodia | b1904b1 | 2022-04-21 09:48:10 +0100 | [diff] [blame] | 280 | set(PYTHON_VENV ${DEFAULT_VENV_DIR} PARENT_SCOPE) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 281 | |
| 282 | if (EXISTS ${PYTHON}) |
| 283 | message(STATUS "Using existing python at ${PYTHON}") |
| 284 | return() |
| 285 | endif () |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 286 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 287 | message(STATUS "Configuring python environment at ${PYTHON}") |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 288 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 289 | execute_process( |
Kshitij Sisodia | c22e80e | 2022-03-14 09:26:48 +0000 | [diff] [blame] | 290 | COMMAND ${PY_EXEC} -m venv ${DEFAULT_VENV_DIR} |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 291 | RESULT_VARIABLE return_code |
| 292 | ) |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 293 | if (NOT return_code STREQUAL "0") |
| 294 | message(FATAL_ERROR "Failed to setup python3 environment. Return code: ${return_code}") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 295 | endif () |
| 296 | |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 297 | execute_process( |
| 298 | COMMAND ${PYTHON} -m pip install --upgrade pip |
| 299 | RESULT_VARIABLE return_code |
| 300 | ) |
| 301 | if (NOT return_code EQUAL "0") |
| 302 | message(FATAL_ERROR "Failed to upgrade pip") |
| 303 | endif () |
| 304 | |
| 305 | execute_process( |
| 306 | COMMAND ${PYTHON} -m pip install wheel |
| 307 | RESULT_VARIABLE return_code |
| 308 | ) |
| 309 | if (NOT return_code EQUAL "0") |
| 310 | message(FATAL_ERROR "Failed to install wheel") |
| 311 | endif () |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 312 | |
| 313 | execute_process( |
| 314 | COMMAND ${PYTHON} -m pip install -r ${SCRIPTS_DIR}/py/requirements.txt |
| 315 | RESULT_VARIABLE return_code |
| 316 | ) |
| 317 | if (NOT return_code EQUAL "0") |
Kshitij Sisodia | 1da52ae | 2021-06-25 09:55:14 +0100 | [diff] [blame] | 318 | message(FATAL_ERROR "Failed to install requirements") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 319 | endif () |
| 320 | endfunction() |