Gyorgy Szing | 4909180 | 2020-11-24 00:33:09 +0100 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2020, Arm Limited and Contributors. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #[===[.rst: |
| 9 | Misc utilities |
| 10 | -------------- |
| 11 | #]===] |
| 12 | |
| 13 | include_guard(DIRECTORY) |
| 14 | |
| 15 | #[===[.rst: |
| 16 | .. cmake:command:: check_args |
| 17 | |
| 18 | .. code-block:: cmake |
| 19 | |
| 20 | check_args(func_name REQ_ARG1 REQ_ARG2) |
| 21 | |
| 22 | Helper macro for argument checking in functions. First argument *func_name* is |
| 23 | the name of the function, other arguments are the names of the required |
| 24 | arguments to that function. The macro iterates through the list, and prints |
| 25 | and error message if not all arguments are defined. |
| 26 | |
| 27 | #]===] |
| 28 | macro(check_args) |
| 29 | set(_argv "${ARGV}") |
| 30 | list(SUBLIST _argv 0 1 _func) |
| 31 | list(SUBLIST _argv 1 -1 _args) |
| 32 | foreach(_arg IN LISTS _args) |
| 33 | if (NOT DEFINED _MY_PARAMS_${_arg}) |
| 34 | message(FATAL_ERROR "${_func}(): mandatory parameter '${_arg}' missing.") |
| 35 | endif() |
| 36 | endforeach() |
| 37 | endmacro() |
| 38 | |
| 39 | # Verify MSYS environment. |
| 40 | function(ts_verify_build_env) |
| 41 | if (WIN32) |
| 42 | #On MSYS2 64 bit builds do not work. Verify environment. |
| 43 | execute_process(COMMAND uname -s |
| 44 | OUTPUT_VARIABLE _os_name) |
| 45 | #If uname is present we assume MSYS environment and the os name must |
| 46 | #contain MING32. |
| 47 | if(_os_name STREQUAL "" AND NOT _os_name MATCHES ".*MINGW32.*") |
| 48 | message(FATAL_ERROR "This seems to be a 64 bit MINGW shell, which has issues. Please run the 32bit version.") |
| 49 | endif() |
| 50 | endif() |
| 51 | endFunction() |
Gyorgy Szing | b8d6f26 | 2023-06-05 12:36:02 +0200 | [diff] [blame] | 52 | |
| 53 | #[===[.rst: |
| 54 | .. cmake:command:: ts_add_uuid_to_exe_name |
| 55 | |
| 56 | .. code-block:: cmake |
| 57 | |
| 58 | ts_add_uuid_to_exe_name(TGT <target name> UUID "canonical string") |
| 59 | |
| 60 | A function to modify the file name of the binary produced by a deployment to allow the OP-TEE symbolize.py tool to |
| 61 | find it when analyzing stack dumps. This is only useful for SP deployments targeting OP-TEE. |
| 62 | The filename will follow the template <file name>_<UUID>.elf format, where |
| 63 | - file name is the original name already configured for the target |
| 64 | - UUID is an argument of this function |
| 65 | |
| 66 | INPUTS: |
| 67 | |
| 68 | ``TGT`` |
| 69 | Mandatory. The name of the target to manipulate. |
| 70 | |
| 71 | ``UUID`` |
| 72 | Mandatory. The UUID to be used to identify the SP. This has to match the UUID used by OP-TEE OS to identify the SP |
| 73 | runtime. |
| 74 | |
| 75 | #]===] |
| 76 | function(ts_add_uuid_to_exe_name) |
| 77 | set(options) |
| 78 | set(oneValueArgs TGT UUID) |
| 79 | set(multiValueArgs) |
| 80 | cmake_parse_arguments(_MY_PARAMS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 81 | |
| 82 | check_args(ts_add_uuid_to_exe_name TGT) |
| 83 | |
| 84 | get_target_property(_tgt_type ${_MY_PARAMS_TGT} TYPE) |
| 85 | if ("${_tgt_type}" STREQUAL "EXECUTABLE") |
| 86 | check_args(ts_add_uuid_to_exe_name UUID) |
| 87 | get_target_property(_out_name ${_MY_PARAMS_TGT} OUTPUT_NAME) |
| 88 | if (NOT _out_name) |
| 89 | set(_out_name "${_MY_PARAMS_TGT}") |
| 90 | endif() |
| 91 | get_target_property(_suffix ${_MY_PARAMS_TGT} SUFFIX) |
| 92 | if (NOT _suffix) |
| 93 | # Note CMAKE_EXECUTABLE_SUFFIX_<lang> might be needed here. Unfortunately |
| 94 | # this is only set, when it is set manually. It overrides the EXE_SUFFIX |
| 95 | # when set. |
| 96 | set(_suffix ${CMAKE_EXECUTABLE_SUFFIX}) |
| 97 | endif() |
| 98 | # If executable suffix is still not set at this point, use the full name as basename. |
| 99 | if (_suffix) |
| 100 | string(REGEX REPLACE "${_suffix}$" "" _base_name "${_out_name}") |
| 101 | else() |
| 102 | set(_base_name "${_out_name}") |
| 103 | endif() |
| 104 | |
| 105 | set(_out_name "${_base_name}_${_MY_PARAMS_UUID}${_suffix}") |
| 106 | set_target_properties(${_MY_PARAMS_TGT} PROPERTIES OUTPUT_NAME "${_out_name}") |
| 107 | endif() |
| 108 | endfunction() |
Gyorgy Szing | e9e0fed | 2025-01-06 15:24:04 +0100 | [diff] [blame] | 109 | |
| 110 | #[===[.rst: |
| 111 | .. cmake:command:: uint64_split |
| 112 | |
| 113 | .. code-block:: cmake |
| 114 | |
| 115 | uint64_split(VALUE 4294967296 OUT_PREFIX RES) |
| 116 | message("RES_LO=${RES_LO} RES_HI=${RES_HI}") |
| 117 | |
| 118 | uint64_split(VALUE 0x1122334455667788 OUT_PREFIX RES DECIMAL) |
| 119 | message("RES_LO=${RES_LO} RES_HI=${RES_HI}") |
| 120 | |
| 121 | Split an uint64 integer to uint32 integers. The returned values will be hexadecimal unless the ``DECIMAL`` |
| 122 | argument is passed. |
| 123 | The result is returned in two values <OUT_PREFIX>_LO and <OUT_PREFIX>_HI. |
| 124 | |
| 125 | INPUTS: |
| 126 | |
| 127 | ``VALUE`` |
| 128 | Mandatory. uint64 value to be converted. The value shall either be an integer (e.g. 123) or a string representing |
| 129 | an integer (e.g. "123"). Hexadecimal numbers can be specified with "0x" prefix. |
| 130 | |
| 131 | ``DECIMAL`` |
| 132 | Optional. Set the format of the returned values to be decimal instead of hexadecimal. |
| 133 | |
| 134 | OUTPUTS: |
| 135 | |
| 136 | ``OUT_PREFIX`` |
| 137 | Mandatory. The prefix of the output variables. Two variable will be created in the callers scope. <OUT_PREFIX>_LO |
| 138 | is the lower 32 bits and <OUT_PREFIX>_HI is the higher 32 bits. |
| 139 | |
| 140 | #]===] |
| 141 | function(uint64_split ) |
| 142 | set(options DECIMAL) |
| 143 | set(oneValueArgs VALUE OUT_PREFIX) |
| 144 | set(multiValueArgs) |
| 145 | cmake_parse_arguments(_MY_PARAMS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) |
| 146 | |
| 147 | check_args(uint64_split VALUE OUT_PREFIX) |
| 148 | |
| 149 | # Ensure the input is a valid uint64 integer |
| 150 | if (NOT "${_MY_PARAMS_VALUE}" MATCHES "^(0x[0-9a-fA-F]+)|([0-9]+)$") |
| 151 | message(FATAL_ERROR "${CMAKE_CURRENT_FUNCTION}: Invalid uint64 integer: ${_MY_PARAMS_VALUE}") |
| 152 | endif() |
| 153 | |
| 154 | if (_MY_PARAMS_DECIMAL) |
| 155 | set(_out_format "DECIMAL") |
| 156 | else() |
| 157 | set(_out_format "HEXADECIMAL") |
| 158 | endif() |
| 159 | |
| 160 | # Split the uint64 integer into two uint32 integers |
| 161 | math(EXPR _high_uint32 "(${_MY_PARAMS_VALUE} >> 32) & 0xFFFFFFFF" OUTPUT_FORMAT ${_out_format}) |
| 162 | math(EXPR _low_uint32 "${_MY_PARAMS_VALUE} & 0xFFFFFFFF" OUTPUT_FORMAT ${_out_format}) |
| 163 | |
| 164 | # Return the results |
| 165 | set(${_MY_PARAMS_OUT_PREFIX}_LO ${_low_uint32} PARENT_SCOPE) |
| 166 | set(${_MY_PARAMS_OUT_PREFIX}_HI ${_high_uint32} PARENT_SCOPE) |
| 167 | endfunction() |