Gyorgy Szing | 30fa987 | 2017-12-05 01:08:47 +0000 | [diff] [blame^] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2017, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | # Append a value to a string if not already present |
| 9 | # |
| 10 | # Append an item to a string if no item with matching key is already on the string. |
| 11 | # This function's intended purpose is to append unique flags to command line switches. |
| 12 | # |
| 13 | # Examples: |
| 14 | # string_append_unique_item(STRING C_FLAGS KEY "--target" VAL "--target=armv8m-arm-none-eabi") |
| 15 | # |
| 16 | # INPUTS: |
| 17 | # STRING - (mandatory) - name of the string to operate on |
| 18 | # KEY - (mandatory) - string to look for |
| 19 | # VAL - (mandatory) - value to put be added to the string |
| 20 | # |
| 21 | # OUTPUTS |
| 22 | # STRING is modified as needed. |
| 23 | # |
| 24 | function(string_append_unique_item) |
| 25 | #Parse our arguments |
| 26 | set( _OPTIONS_ARGS ) #No option (on/off) arguments (e.g. IGNORE_CASE) |
| 27 | set( _ONE_VALUE_ARGS STRING KEY VAL) #Single option arguments (e.g. PATH "./foo/bar") |
| 28 | set( _MULTI_VALUE_ARGS ) #List arguments (e.g. LANGUAGES C ASM CXX) |
| 29 | cmake_parse_arguments(_MY_PARAMS "${_OPTIONS_ARGS}" "${_ONE_VALUE_ARGS}" "${_MULTI_VALUE_ARGS}" ${ARGN} ) |
| 30 | |
| 31 | #Check mandatory parameters |
| 32 | if(NOT _MY_PARAMS_STRING) |
| 33 | failure("string_append_unique_item(): Missing STRING parameter!") |
| 34 | endif() |
| 35 | set(_STRING ${_MY_PARAMS_STRING}) |
| 36 | |
| 37 | if(NOT _MY_PARAMS_KEY) |
| 38 | failure("string_append_unique_item(): Missing KEY parameter!") |
| 39 | endif() |
| 40 | set(_KEY ${_MY_PARAMS_KEY}) |
| 41 | |
| 42 | if(NOT _MY_PARAMS_VAL) |
| 43 | failure("string_append_unique_item(): Missing VAL parameter!") |
| 44 | endif() |
| 45 | set(_VAL ${_MY_PARAMS_VAL}) |
| 46 | |
| 47 | #Scan the string. |
| 48 | STRING(REGEX MATCH "( |^) *${_KEY}" _FOUND "${${_STRING}}") |
| 49 | if("${_FOUND}" STREQUAL "") |
| 50 | set(${_STRING} "${${_STRING}} ${_VAL}" PARENT_SCOPE) |
| 51 | endif() |
| 52 | endfunction() |