blob: ecd9d2a0b9a2671e486e23d2430e4c0702f14871 [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})
26 get_path_type(${${name}} PATH_TYPE)
27 else()
28 get_path_type(${default} PATH_TYPE)
29 endif()
30
31 # Set the default type if path is not a dir or file path (or undefined)
32 if (NOT ${PATH_TYPE} STREQUAL PATH AND NOT ${PATH_TYPE} STREQUAL FILEPATH)
33 message(FATAL_ERROR "Invalid ${name}. It should be a dir or file path.")
34 endif()
35 set(type ${PATH_TYPE})
36 endif()
37
alexander3c798932021-03-26 21:42:19 +000038 if (NOT DEFINED ${name})
39 set(${name} ${default} CACHE ${type} ${description})
40 endif()
41
42 # if it is a path
43 if (${type} STREQUAL PATH)
44
45 # Get the absolute path, relative to the cmake root
46 get_filename_component(ABSPATH "${${name}}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
47
48 # check that this is a directory
49 if (NOT IS_DIRECTORY ${ABSPATH})
50 message(FATAL_ERROR
51 "Invalid directory path. Description: ${description}; Path: ${ABSPATH}")
52 endif()
53
54 set(${name} ${ABSPATH} CACHE ${type} ${description} FORCE)
55
56 # if this is a file path
57 elseif(${type} STREQUAL FILEPATH)
58
59 # Get the absolute path, relative to the cmake root
60 get_filename_component(ABSPATH "${${name}}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR})
61
62 # check that the file exists:
63 if (NOT EXISTS ${ABSPATH})
64 message(FATAL_ERROR
65 "Invalid file path. Description: ${description}; Path: ${ABSPATH}")
66 endif()
67
68 set(${name} ${ABSPATH} CACHE ${type} ${description} FORCE)
69
70 endif()
71
72 message(STATUS "User option ${name} is set to ${${name}}")
73 LIST(APPEND USER_OPTIONS ${name})
74 set(USER_OPTIONS ${USER_OPTIONS} CACHE INTERNAL "")
75
76endfunction()
77
78# Function to get the path type for a variable
79# Args:
80# path_var[in]: path variable for which the cmake path type is requested
81# cmake_path_type[out]: CMake path type. Set to FILEPATH when it is a file
82# or PATH when it points to a directory. If the path
83# is invalid, this remains empty.
84function(get_path_type path_var cmake_path_type)
85 # Validate path - get absolute value
86 get_filename_component(ABSPATH "${path_var}" ABSOLUTE
87 BASE_DIR ${CMAKE_SOURCE_DIR})
88
89 if (DEFINED path_var)
90 if (IS_DIRECTORY ${ABSPATH})
91 set(${cmake_path_type} PATH PARENT_SCOPE)
92 message(STATUS "Variable of PATH type")
93 elseif(EXISTS ${ABSPATH})
94 set(${cmake_path_type} FILEPATH PARENT_SCOPE)
95 else()
96 set(${cmake_path_type} "" PARENT_SCOPE)
97 endif()
98 else()
99 set(${cmake_path_type} UNINITIALIZED PARENT_SCOPE)
100 endif()
101
102endfunction()
103
104# Function to print all the user options added using the function `USER_OPTION`
105function(print_useroptions)
106 message(STATUS "--------------------------------------------------------------------------------------------------")
107 message(STATUS "Defined build user options:")
108 message(STATUS "")
109 foreach(opt ${USER_OPTIONS})
110 message(STATUS " ${opt}=${${opt}}")
111 endforeach()
112 message(STATUS "--------------------------------------------------------------------------------------------------")
113endfunction()
114
115function (SUBDIRLIST result curdir)
116 file(GLOB children RELATIVE ${curdir} ${curdir}/*)
117 set(dirlist "")
118 foreach(child ${children})
119 if(IS_DIRECTORY ${curdir}/${child})
120 LIST(APPEND dirlist ${child})
121 endif()
122 endforeach()
123 set(${result} ${dirlist} PARENT_SCOPE)
124endfunction()
125
126function(to_py_bool cmake_bool py_bool)
127 if(${${cmake_bool}})
128 set(${py_bool} True PARENT_SCOPE)
129 else()
130 set(${py_bool} False PARENT_SCOPE)
131 endif()
132endfunction()
133
134# Function to download a files from the Arm Model Zoo
135# Arguments:
Isabella Gottardib88705d2021-04-21 13:09:51 +0100136# model_zoo_version: hash of the Arm Model Zoo commit to use
alexander3c798932021-03-26 21:42:19 +0000137# file_sub_path: subpath within the model zoo respository
138# download_path: location where this file is to be downloaded (path including filename)
Isabella Gottardib88705d2021-04-21 13:09:51 +0100139function(download_file_from_modelzoo model_zoo_version file_sub_path download_path)
alexander3c798932021-03-26 21:42:19 +0000140
Isabella Gottardi2181d0a2021-04-07 09:27:38 +0100141 set(MODEL_ZOO_REPO "https://github.com/ARM-software/ML-zoo/raw")
alexander3c798932021-03-26 21:42:19 +0000142
143 string(JOIN "/" FILE_URL
Isabella Gottardib88705d2021-04-21 13:09:51 +0100144 ${MODEL_ZOO_REPO} ${model_zoo_version} ${file_sub_path})
alexander3c798932021-03-26 21:42:19 +0000145
146 message(STATUS "Downloading ${FILE_URL} to ${download_path}...")
147
148 file(DOWNLOAD ${FILE_URL} ${download_path}
149 STATUS DOWNLOAD_STATE)
150 list(GET DOWNLOAD_STATE 0 RET_VAL)
151
152 if(${RET_VAL})
153 list(GET DOWNLOAD_STATE 1 RET_MSG)
154 message(FATAL_ERROR "Download failed with error code: ${RET_VAL}; "
155 "Error message: ${RET_MSG}")
156 endif()
157
158endfunction()