blob: 9aa19b93ea758a3dc91e9fcc1f0828cf798b7c81 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001#----------------------------------------------------------------------------
Isabella Gottardief2b9dd2022-02-16 14:24:03 +00002# Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003# 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
18##############################################################################
19# Helper function to provide user option and corresponding default value
20##############################################################################
21function(USER_OPTION name description default type)
22
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010023 if (${type} STREQUAL PATH_OR_FILE)
24
25 if (DEFINED ${name})
alexander50a06502021-05-12 19:06:02 +010026 get_filename_component(ABSPATH "${${name}}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
27
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010028 get_path_type(${${name}} PATH_TYPE)
29 else()
alexander50a06502021-05-12 19:06:02 +010030 get_filename_component(ABSPATH "${default}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
31
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010032 get_path_type(${default} PATH_TYPE)
33 endif()
34
alexander50a06502021-05-12 19:06:02 +010035 if (NOT EXISTS ${ABSPATH})
36 message(FATAL_ERROR
37 "Invalid directory or file path. Description: ${description}; Path: ${ABSPATH}")
38 endif()
39
Isabella Gottardi2181d0a2021-04-07 09:27:38 +010040 # Set the default type if path is not a dir or file path (or undefined)
41 if (NOT ${PATH_TYPE} STREQUAL PATH AND NOT ${PATH_TYPE} STREQUAL FILEPATH)
42 message(FATAL_ERROR "Invalid ${name}. It should be a dir or file path.")
43 endif()
44 set(type ${PATH_TYPE})
45 endif()
46
alexander3c798932021-03-26 21:42:19 +000047 if (NOT DEFINED ${name})
48 set(${name} ${default} CACHE ${type} ${description})
49 endif()
50
51 # if it is a path
52 if (${type} STREQUAL PATH)
53
54 # Get the absolute path, relative to the cmake root
55 get_filename_component(ABSPATH "${${name}}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
56
57 # check that this is a directory
58 if (NOT IS_DIRECTORY ${ABSPATH})
59 message(FATAL_ERROR
60 "Invalid directory path. Description: ${description}; Path: ${ABSPATH}")
61 endif()
62
63 set(${name} ${ABSPATH} CACHE ${type} ${description} FORCE)
64
65 # if this is a file path
66 elseif(${type} STREQUAL FILEPATH)
67
68 # Get the absolute path, relative to the cmake root
69 get_filename_component(ABSPATH "${${name}}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
70
71 # check that the file exists:
72 if (NOT EXISTS ${ABSPATH})
73 message(FATAL_ERROR
74 "Invalid file path. Description: ${description}; Path: ${ABSPATH}")
75 endif()
76
77 set(${name} ${ABSPATH} CACHE ${type} ${description} FORCE)
78
79 endif()
80
81 message(STATUS "User option ${name} is set to ${${name}}")
82 LIST(APPEND USER_OPTIONS ${name})
83 set(USER_OPTIONS ${USER_OPTIONS} CACHE INTERNAL "")
84
85endfunction()
86
Kshitij Sisodiab59ba682021-11-23 17:19:52 +000087
88# Function to check if a variable is defined, and throw
89# an error if it is not.
90function(assert_defined var_name)
91 if (NOT DEFINED ${var_name})
92 message(FATAL_ERROR "ERROR: ${var_name} is undefined!")
93 endif()
94endfunction()
95
alexander3c798932021-03-26 21:42:19 +000096# Function to get the path type for a variable
97# Args:
98# path_var[in]: path variable for which the cmake path type is requested
99# cmake_path_type[out]: CMake path type. Set to FILEPATH when it is a file
100# or PATH when it points to a directory. If the path
101# is invalid, this remains empty.
102function(get_path_type path_var cmake_path_type)
103 # Validate path - get absolute value
104 get_filename_component(ABSPATH "${path_var}" ABSOLUTE
105 BASE_DIR ${CMAKE_SOURCE_DIR})
106
107 if (DEFINED path_var)
108 if (IS_DIRECTORY ${ABSPATH})
109 set(${cmake_path_type} PATH PARENT_SCOPE)
110 message(STATUS "Variable of PATH type")
111 elseif(EXISTS ${ABSPATH})
112 set(${cmake_path_type} FILEPATH PARENT_SCOPE)
113 else()
114 set(${cmake_path_type} "" PARENT_SCOPE)
115 endif()
116 else()
117 set(${cmake_path_type} UNINITIALIZED PARENT_SCOPE)
118 endif()
119
120endfunction()
121
122# Function to print all the user options added using the function `USER_OPTION`
123function(print_useroptions)
124 message(STATUS "--------------------------------------------------------------------------------------------------")
125 message(STATUS "Defined build user options:")
126 message(STATUS "")
127 foreach(opt ${USER_OPTIONS})
128 message(STATUS " ${opt}=${${opt}}")
129 endforeach()
130 message(STATUS "--------------------------------------------------------------------------------------------------")
131endfunction()
132
133function (SUBDIRLIST result curdir)
134 file(GLOB children RELATIVE ${curdir} ${curdir}/*)
135 set(dirlist "")
136 foreach(child ${children})
137 if(IS_DIRECTORY ${curdir}/${child})
138 LIST(APPEND dirlist ${child})
139 endif()
140 endforeach()
141 set(${result} ${dirlist} PARENT_SCOPE)
142endfunction()
143
144function(to_py_bool cmake_bool py_bool)
145 if(${${cmake_bool}})
146 set(${py_bool} True PARENT_SCOPE)
147 else()
148 set(${py_bool} False PARENT_SCOPE)
149 endif()
150endfunction()
151
152# Function to download a files from the Arm Model Zoo
153# Arguments:
Isabella Gottardib88705d2021-04-21 13:09:51 +0100154# model_zoo_version: hash of the Arm Model Zoo commit to use
Isabella Gottardief2b9dd2022-02-16 14:24:03 +0000155# file_sub_path: subpath within the model zoo repository
alexander3c798932021-03-26 21:42:19 +0000156# download_path: location where this file is to be downloaded (path including filename)
Isabella Gottardib88705d2021-04-21 13:09:51 +0100157function(download_file_from_modelzoo model_zoo_version file_sub_path download_path)
alexander3c798932021-03-26 21:42:19 +0000158
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100159 set(MODEL_ZOO_REPO "https://github.com/ARM-software/ML-zoo/raw")
alexander3c798932021-03-26 21:42:19 +0000160
161 string(JOIN "/" FILE_URL
Isabella Gottardib88705d2021-04-21 13:09:51 +0100162 ${MODEL_ZOO_REPO} ${model_zoo_version} ${file_sub_path})
alexander3c798932021-03-26 21:42:19 +0000163
164 message(STATUS "Downloading ${FILE_URL} to ${download_path}...")
165
166 file(DOWNLOAD ${FILE_URL} ${download_path}
167 STATUS DOWNLOAD_STATE)
168 list(GET DOWNLOAD_STATE 0 RET_VAL)
169
170 if(${RET_VAL})
171 list(GET DOWNLOAD_STATE 1 RET_MSG)
172 message(FATAL_ERROR "Download failed with error code: ${RET_VAL}; "
173 "Error message: ${RET_MSG}")
174 endif()
175
176endfunction()
alexandercb8a9872022-02-11 13:23:22 +0000177
178function(add_platform_build_configuration)
179
180 set(oneValueArgs TARGET_PLATFORM)
181 cmake_parse_arguments(PARSED "" "${oneValueArgs}" "" ${ARGN} )
182 message(STATUS "Searching for ${PARSED_TARGET_PLATFORM} build configuration")
183 list(APPEND PLATFORM_BUILD_CONFIG_DIRS ${CMAKE_SCRIPTS_DIR}/platforms)
184
185 FIND_PATH(PLATFORM_BUILD_CONFIG
186 NAMES build_configuration.cmake
187 PATH_SUFFIXES ${PARSED_TARGET_PLATFORM}
188 PATHS ${PLATFORM_BUILD_CONFIG_DIRS}
189 )
190
191 message(STATUS "Found build configuration: ${PLATFORM_BUILD_CONFIG}")
192 include(${PLATFORM_BUILD_CONFIG}/build_configuration.cmake)
Isabella Gottardief2b9dd2022-02-16 14:24:03 +0000193endfunction()
194
195function(check_update_public_resources resource_downloaded_dir)
196
197 string(JOIN "/" FILE_URL ${resource_downloaded_dir})
198 execute_process(
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +0000199 COMMAND python3 ${SCRIPTS_DIR}/py/check_update_resources_downloaded.py
Isabella Gottardief2b9dd2022-02-16 14:24:03 +0000200 --resource_downloaded_dir ${resource_downloaded_dir}
Kshitij Sisodia6b9adad2022-03-01 13:42:18 +0000201 --setup_script_path ${SCRIPTS_DIR}/../set_up_default_resources.py
Isabella Gottardief2b9dd2022-02-16 14:24:03 +0000202 RESULT_VARIABLE return_code
203 )
204 if (NOT return_code EQUAL "0")
205 if (NOT return_code EQUAL "1")
206 # Return code equal to 2 or else means an error in the resources_downloaded folder
207 message(FATAL_ERROR "Resources downloaded error, please run: set_up_default_resources.py")
208 else()
209 # Return code equal to 1 means that resources_downloaded need to be updated
210 message(FATAL_ERROR "Resources downloaded need to be updated, please run: set_up_default_resources.py --clean")
211 endif()
212 endif ()
213
214endfunction()
Eanna O Cathain4c0ac912022-09-22 15:18:34 +0100215
216function(set_input_file_path_user_option file_extension use_case)
217 if(NOT USE_SINGLE_INPUT)
218 USER_OPTION(${use_case}_FILE_PATH "Directory with custom input files, or path to a single input file, to use in the evaluation application."
219 ${CMAKE_CURRENT_SOURCE_DIR}/resources/${use_case}/samples/
220 PATH_OR_FILE)
221 else()
222 file(GLOB_RECURSE INPUTS ${CMAKE_CURRENT_SOURCE_DIR}/resources/${use_case}/samples/*${file_extension})
223 list (GET INPUTS 0 FIRST_INPUT_FILE)
224 USER_OPTION(${use_case}_FILE_PATH "Directory with custom input files, or path to a single input file, to use in the evaluation application."
225 ${FIRST_INPUT_FILE}
226 PATH_OR_FILE)
227 endif()
228endfunction()