blob: 452d0bf91403354fc0e2f4be2ac2e158ecc9ba11 [file] [log] [blame]
Gyorgy Szing30fa9872017-12-05 01:08:47 +00001#-------------------------------------------------------------------------------
Gyorgy Szing74dae3c2018-09-27 17:00:46 +02002# Copyright (c) 2017-2019, Arm Limited. All rights reserved.
Gyorgy Szing30fa9872017-12-05 01:08:47 +00003#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6#-------------------------------------------------------------------------------
7
Gyorgy Szing74dae3c2018-09-27 17:00:46 +02008
9#Print a message and exit with failure
Gyorgy Szing30fa9872017-12-05 01:08:47 +000010#
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020011#Examples:
12# failure("Something is wrong!")
13
14function(failure)
15 message(FATAL_ERROR "${ARGN}")
16endfunction()
17
18#Append a value to a string if not already present
Gyorgy Szing30fa9872017-12-05 01:08:47 +000019#
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020020#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 Szing30fa9872017-12-05 01:08:47 +000022#
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020023#Examples:
24# string_append_unique_item(STRING C_FLAGS KEY "--target" VAL "--target=armv8m-arm-none-eabi")
Gyorgy Szing30fa9872017-12-05 01:08:47 +000025#
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020026#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 Szing30fa9872017-12-05 01:08:47 +000033#
34function(string_append_unique_item)
35 #Parse our arguments
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020036 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 Szing30fa9872017-12-05 01:08:47 +000039 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 Szing74dae3c2018-09-27 17:00:46 +020055 set(_VAL ${_MY_PARAMS_VAL})
Gyorgy Szing30fa9872017-12-05 01:08:47 +000056
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020057 #Scan the string.
58 STRING(REGEX MATCH "( |^) *${_KEY}" _FOUND "${${_STRING}}")
59 if("${_FOUND}" STREQUAL "")
Gyorgy Szing30fa9872017-12-05 01:08:47 +000060 set(${_STRING} "${${_STRING}} ${_VAL}" PARENT_SCOPE)
Gyorgy Szing74dae3c2018-09-27 17:00:46 +020061 endif()
62endfunction()
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#
81function(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 Szing30fa9872017-12-05 01:08:47 +0000105endfunction()