build: add string casing facilities to utilities

This is a small modification to two existing functions in the build
system: `uppercase` and `lowercase`.

These functions have been moved to the common utilities makefile, and
use the `tr` tool to simplify their implementation. Behaviour is, for
virtually all use-cases, identical.

Change-Id: I0e459d92e454087e4188b2fa5968244e5db89906
Signed-off-by: Chris Kay <chris.kay@arm.com>
This commit is contained in:
Chris Kay 2024-05-29 22:09:26 +00:00
parent 887e69ee73
commit 3af4eb50c0
3 changed files with 30 additions and 20 deletions

View file

@ -17,22 +17,6 @@ define rwildcard
$(strip $(foreach d,$(wildcard ${1}*),$(call rwildcard,${d}/,${2}) $(filter $(subst *,%,%${2}),${d})))
endef
# This table is used in converting lower case to upper case.
uppercase_table:=a,A b,B c,C d,D e,E f,F g,G h,H i,I j,J k,K l,L m,M n,N o,O p,P q,Q r,R s,S t,T u,U v,V w,W x,X y,Y z,Z
# Internal macro used for converting lower case to upper case.
# $(1) = upper case table
# $(2) = String to convert
define uppercase_internal
$(if $(1),$$(subst $(firstword $(1)),$(call uppercase_internal,$(wordlist 2,$(words $(1)),$(1)),$(2))),$(2))
endef
# A macro for converting a string to upper case
# $(1) = String to convert
define uppercase
$(eval uppercase_result:=$(call uppercase_internal,$(uppercase_table),$(1)))$(uppercase_result)
endef
# Convenience function for setting a variable to 0 if not previously set
# $(eval $(call default_zero,FOO))
define default_zero

View file

@ -20,3 +20,31 @@ file-name = $(call decompat-path,$(notdir $(call compat-path,$(1))))
directory-name = $(call decompat-path,$(dir $(call compat-path,$(1))))
escape-shell = '$(subst ','\'',$(1))'
#
# Upper-case a string value.
#
# Parameters:
#
# - $(1): The string to upper-case.
#
# Example usage:
#
# $(call uppercase,HeLlO wOrLd) # "HELLO WORLD"
#
uppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
#
# Lower-case a string value.
#
# Parameters:
#
# - $(1): The string to lower-case.
#
# Example usage:
#
# $(call lowercase,HeLlO wOrLd) # "hello world"
#
lowercase = $(shell echo $(call escape-shell,$(1)) | tr '[:upper:]' '[:lower:]')

View file

@ -27,12 +27,10 @@ endef
# Determine option variable is defined or not then define it
define add_defined_option
ifdef $(1)
ifeq ($(findstring $(value $(1)), $(uppercase_table)),)
DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),)
else
ifeq ($(strip $(value $(1))),y)
DEFINES += -D$(1)$(if $(value $(1)),=1,)
endif
else
DEFINES += -D$(1)$(if $(value $(1)),=$(value $(1)),)
endif
endif
endef