blob: 06ae1847a7ae49c58f67df261a54140733426d0d [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#----------------------------------------------------------------------------
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
87# Function to get the path type for a variable
88# Args:
89# path_var[in]: path variable for which the cmake path type is requested
90# cmake_path_type[out]: CMake path type. Set to FILEPATH when it is a file
91# or PATH when it points to a directory. If the path
92# is invalid, this remains empty.
93function(get_path_type path_var cmake_path_type)
94 # Validate path - get absolute value
95 get_filename_component(ABSPATH "${path_var}" ABSOLUTE
96 BASE_DIR ${CMAKE_SOURCE_DIR})
97
98 if (DEFINED path_var)
99 if (IS_DIRECTORY ${ABSPATH})
100 set(${cmake_path_type} PATH PARENT_SCOPE)
101 message(STATUS "Variable of PATH type")
102 elseif(EXISTS ${ABSPATH})
103 set(${cmake_path_type} FILEPATH PARENT_SCOPE)
104 else()
105 set(${cmake_path_type} "" PARENT_SCOPE)
106 endif()
107 else()
108 set(${cmake_path_type} UNINITIALIZED PARENT_SCOPE)
109 endif()
110
111endfunction()
112
113# Function to print all the user options added using the function `USER_OPTION`
114function(print_useroptions)
115 message(STATUS "--------------------------------------------------------------------------------------------------")
116 message(STATUS "Defined build user options:")
117 message(STATUS "")
118 foreach(opt ${USER_OPTIONS})
119 message(STATUS " ${opt}=${${opt}}")
120 endforeach()
121 message(STATUS "--------------------------------------------------------------------------------------------------")
122endfunction()
123
124function (SUBDIRLIST result curdir)
125 file(GLOB children RELATIVE ${curdir} ${curdir}/*)
126 set(dirlist "")
127 foreach(child ${children})
128 if(IS_DIRECTORY ${curdir}/${child})
129 LIST(APPEND dirlist ${child})
130 endif()
131 endforeach()
132 set(${result} ${dirlist} PARENT_SCOPE)
133endfunction()
134
135function(to_py_bool cmake_bool py_bool)
136 if(${${cmake_bool}})
137 set(${py_bool} True PARENT_SCOPE)
138 else()
139 set(${py_bool} False PARENT_SCOPE)
140 endif()
141endfunction()
142
143# Function to download a files from the Arm Model Zoo
144# Arguments:
Isabella Gottardib88705d2021-04-21 13:09:51 +0100145# model_zoo_version: hash of the Arm Model Zoo commit to use
alexander3c798932021-03-26 21:42:19 +0000146# file_sub_path: subpath within the model zoo respository
147# download_path: location where this file is to be downloaded (path including filename)
Isabella Gottardib88705d2021-04-21 13:09:51 +0100148function(download_file_from_modelzoo model_zoo_version file_sub_path download_path)
alexander3c798932021-03-26 21:42:19 +0000149
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100150 set(MODEL_ZOO_REPO "https://github.com/ARM-software/ML-zoo/raw")
alexander3c798932021-03-26 21:42:19 +0000151
152 string(JOIN "/" FILE_URL
Isabella Gottardib88705d2021-04-21 13:09:51 +0100153 ${MODEL_ZOO_REPO} ${model_zoo_version} ${file_sub_path})
alexander3c798932021-03-26 21:42:19 +0000154
155 message(STATUS "Downloading ${FILE_URL} to ${download_path}...")
156
157 file(DOWNLOAD ${FILE_URL} ${download_path}
158 STATUS DOWNLOAD_STATE)
159 list(GET DOWNLOAD_STATE 0 RET_VAL)
160
161 if(${RET_VAL})
162 list(GET DOWNLOAD_STATE 1 RET_MSG)
163 message(FATAL_ERROR "Download failed with error code: ${RET_VAL}; "
164 "Error message: ${RET_MSG}")
165 endif()
166
167endfunction()