Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 2 | # Copyright (c) 2017-2019, Arm Limited. All rights reserved. |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 8 | |
| 9 | #Print a message and exit with failure |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 10 | # |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 11 | #Examples: |
| 12 | # failure("Something is wrong!") |
| 13 | |
| 14 | function(failure) |
| 15 | message(FATAL_ERROR "${ARGN}") |
| 16 | endfunction() |
| 17 | |
| 18 | #Append a value to a string if not already present |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 19 | # |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 20 | #Append an item to a string if no item with matching key is already on the string. |
| 21 | #This function's intended purpose is to append unique flags to command line switches. |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 22 | # |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 23 | #Examples: |
| 24 | # string_append_unique_item(STRING C_FLAGS KEY "--target" VAL "--target=armv8m-arm-none-eabi") |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 25 | # |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 26 | #INPUTS: |
| 27 | # STRING - (mandatory) - name of the string to operate on |
| 28 | # KEY - (mandatory) - string to look for |
| 29 | # VAL - (mandatory) - value to put be added to the string |
| 30 | # |
| 31 | #OUTPUTS |
| 32 | # STRING is modified as needed. |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 33 | # |
| 34 | function(string_append_unique_item) |
| 35 | #Parse our arguments |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 36 | set( _OPTIONS_ARGS ) #No option (on/off) arguments (e.g. IGNORE_CASE) |
| 37 | set( _ONE_VALUE_ARGS STRING KEY VAL) #Single option arguments (e.g. PATH "./foo/bar") |
| 38 | set( _MULTI_VALUE_ARGS ) #List arguments (e.g. LANGUAGES C ASM CXX) |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 39 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 40 | |
| 41 | #Check mandatory parameters |
| 42 | if(NOT _MY_PARAMS_STRING) |
| 43 | failure("string_append_unique_item(): Missing STRING parameter!") |
| 44 | endif() |
| 45 | set(_STRING ${_MY_PARAMS_STRING}) |
| 46 | |
| 47 | if(NOT _MY_PARAMS_KEY) |
| 48 | failure("string_append_unique_item(): Missing KEY parameter!") |
| 49 | endif() |
| 50 | set(_KEY ${_MY_PARAMS_KEY}) |
| 51 | |
| 52 | if(NOT _MY_PARAMS_VAL) |
| 53 | failure("string_append_unique_item(): Missing VAL parameter!") |
| 54 | endif() |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 55 | set(_VAL ${_MY_PARAMS_VAL}) |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 56 | |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 57 | #Scan the string. |
| 58 | STRING(REGEX MATCH "( |^) *${_KEY}" _FOUND "${${_STRING}}") |
| 59 | if("${_FOUND}" STREQUAL "") |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 60 | set(${_STRING} "${${_STRING}} ${_VAL}" PARENT_SCOPE) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 61 | endif() |
| 62 | endfunction() |
| 63 | |
| 64 | |
| 65 | #Convert \ directory separators to / on windows systems |
| 66 | # |
| 67 | #Convert the directory separators to forward slash on windows. Avoid |
| 68 | #conversion if path contains any forward slashes to avoid mixing up cygwin or |
| 69 | #mingw paths where an extra caharacter is escaped (i.e. "/c/Program\ Files/") |
| 70 | # |
| 71 | #Examples: |
| 72 | # set(MY_PATH "C:\foo\bar") |
| 73 | # win_fix_dir_sep(PATH MY_PATH) |
| 74 | # |
| 75 | #INPUTS: |
| 76 | # PATH - (mandatory) - name of the string variable to operate on |
| 77 | # |
| 78 | #OUTPUTS |
| 79 | # PATH is modified as needed. |
| 80 | # |
| 81 | function(win_fix_dir_sep) |
| 82 | #Parse our arguments |
| 83 | set( _OPTIONS_ARGS ) #No option (on/off) arguments (e.g. IGNORE_CASE) |
| 84 | set( _ONE_VALUE_ARGS PATH ) #Single option arguments (e.g. PATH "./foo/bar") |
| 85 | set( _MULTI_VALUE_ARGS ) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 86 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 87 | |
| 88 | #Check mandatory parameters |
| 89 | if(NOT _MY_PARAMS_PATH) |
| 90 | failure("win_fix_dir_sep(): Missing mandatory parameter PATH!") |
| 91 | endif() |
| 92 | set(_PATH ${_MY_PARAMS_PATH}) |
| 93 | |
| 94 | #To avoid trouble on windows change directory separator. |
| 95 | if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows") |
| 96 | #Do not convert directory separator if there are forward slashes |
| 97 | #present. This is to avoid mixing up escaped characters in cygwin |
| 98 | #or mingw paths (i.e. c:/Program\ Files/something) |
| 99 | string(FIND "${${_PATH}}" "/" _is_found) |
| 100 | if (_is_found LESS 0) |
| 101 | string(REPLACE "\\" "/" ${_PATH} "${${_PATH}}") |
| 102 | set(${_PATH} "${${_PATH}}" PARENT_SCOPE) |
| 103 | endif() |
| 104 | endif() |
Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame] | 105 | endfunction() |