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 | |
| 18 | ############################################################################## |
| 19 | # Helper function to provide user option and corresponding default value |
| 20 | ############################################################################## |
| 21 | function(USER_OPTION name description default type) |
| 22 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 23 | if (${type} STREQUAL PATH_OR_FILE) |
| 24 | |
| 25 | if (DEFINED ${name}) |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 26 | get_filename_component(ABSPATH "${${name}}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR}) |
| 27 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 28 | get_path_type(${${name}} PATH_TYPE) |
| 29 | else() |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 30 | get_filename_component(ABSPATH "${default}" ABSOLUTE BASE_DIR ${CMAKE_SOURCE_DIR}) |
| 31 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 32 | get_path_type(${default} PATH_TYPE) |
| 33 | endif() |
| 34 | |
alexander | 50a0650 | 2021-05-12 19:06:02 +0100 | [diff] [blame] | 35 | if (NOT EXISTS ${ABSPATH}) |
| 36 | message(FATAL_ERROR |
| 37 | "Invalid directory or file path. Description: ${description}; Path: ${ABSPATH}") |
| 38 | endif() |
| 39 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 40 | # 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 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | 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 | |
| 85 | endfunction() |
| 86 | |
Kshitij Sisodia | b59ba68 | 2021-11-23 17:19:52 +0000 | [diff] [blame] | 87 | |
| 88 | # Function to check if a variable is defined, and throw |
| 89 | # an error if it is not. |
| 90 | function(assert_defined var_name) |
| 91 | if (NOT DEFINED ${var_name}) |
| 92 | message(FATAL_ERROR "ERROR: ${var_name} is undefined!") |
| 93 | endif() |
| 94 | endfunction() |
| 95 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | # 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. |
| 102 | function(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 | |
| 120 | endfunction() |
| 121 | |
| 122 | # Function to print all the user options added using the function `USER_OPTION` |
| 123 | function(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 "--------------------------------------------------------------------------------------------------") |
| 131 | endfunction() |
| 132 | |
| 133 | function (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) |
| 142 | endfunction() |
| 143 | |
| 144 | function(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() |
| 150 | endfunction() |
| 151 | |
| 152 | # Function to download a files from the Arm Model Zoo |
| 153 | # Arguments: |
Isabella Gottardi | b88705d | 2021-04-21 13:09:51 +0100 | [diff] [blame] | 154 | # model_zoo_version: hash of the Arm Model Zoo commit to use |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 155 | # file_sub_path: subpath within the model zoo respository |
| 156 | # download_path: location where this file is to be downloaded (path including filename) |
Isabella Gottardi | b88705d | 2021-04-21 13:09:51 +0100 | [diff] [blame] | 157 | function(download_file_from_modelzoo model_zoo_version file_sub_path download_path) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | |
Isabella Gottardi | 2181d0a | 2021-04-07 09:27:38 +0100 | [diff] [blame] | 159 | set(MODEL_ZOO_REPO "https://github.com/ARM-software/ML-zoo/raw") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 160 | |
| 161 | string(JOIN "/" FILE_URL |
Isabella Gottardi | b88705d | 2021-04-21 13:09:51 +0100 | [diff] [blame] | 162 | ${MODEL_ZOO_REPO} ${model_zoo_version} ${file_sub_path}) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 163 | |
| 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 | |
| 176 | endfunction() |