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