1#
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17###########################################################
18## Convert to lower case without requiring a shell, which isn't cacheable.
19##
20## $(1): string
21###########################################################
22to-lower=$(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
23
24###########################################################
25## Convert to upper case without requiring a shell, which isn't cacheable.
26##
27## $(1): string
28###########################################################
29to-upper=$(subst a,A,$(subst b,B,$(subst c,C,$(subst d,D,$(subst e,E,$(subst f,F,$(subst g,G,$(subst h,H,$(subst i,I,$(subst j,J,$(subst k,K,$(subst l,L,$(subst m,M,$(subst n,N,$(subst o,O,$(subst p,P,$(subst q,Q,$(subst r,R,$(subst s,S,$(subst t,T,$(subst u,U,$(subst v,V,$(subst w,W,$(subst x,X,$(subst y,Y,$(subst z,Z,$1))))))))))))))))))))))))))
30
31# Test to-lower and to-upper
32lower := abcdefghijklmnopqrstuvwxyz-_
33upper := ABCDEFGHIJKLMNOPQRSTUVWXYZ-_
34
35ifneq ($(lower),$(call to-lower,$(upper)))
36  $(error to-lower sanity check failure)
37endif
38
39ifneq ($(upper),$(call to-upper,$(lower)))
40  $(error to-upper sanity check failure)
41endif
42
43lower :=
44upper :=
45
46###########################################################
47## Returns true if $(1) and $(2) are equal.  Returns
48## the empty string if they are not equal.
49###########################################################
50define streq
51$(strip $(if $(strip $(1)),\
52  $(if $(strip $(2)),\
53    $(if $(filter-out __,_$(subst $(strip $(1)),,$(strip $(2)))$(subst $(strip $(2)),,$(strip $(1)))_),,true), \
54    ),\
55  $(if $(strip $(2)),\
56    ,\
57    true)\
58 ))
59endef
60
61###########################################################
62## Convert "a b c" into "a:b:c"
63###########################################################
64define normalize-path-list
65$(subst $(space),:,$(strip $(1)))
66endef
67
68###########################################################
69## Convert "a b c" into "a,b,c"
70###########################################################
71define normalize-comma-list
72$(subst $(space),$(comma),$(strip $(1)))
73endef
74
75###########################################################
76## Read the word out of a colon-separated list of words.
77## This has the same behavior as the built-in function
78## $(word n,str).
79##
80## The individual words may not contain spaces.
81##
82## $(1): 1 based index
83## $(2): value of the form a:b:c...
84###########################################################
85
86define word-colon
87$(word $(1),$(subst :,$(space),$(2)))
88endef
89
90###########################################################
91## Convert "a=b c= d e = f = g h=" into "a=b c=d e= f=g h="
92##
93## $(1): list to collapse
94## $(2): if set, separator word; usually "=", ":", or ":="
95##       Defaults to "=" if not set.
96###########################################################
97
98define collapse-pairs
99$(strip \
100$(eval _cpSEP := $(strip $(if $(2),$(2),=)))\
101$(eval _cpLHS :=)\
102$(eval _cpRET :=)\
103$(foreach w,$(subst $(space)$(_cpSEP),$(_cpSEP),$(strip \
104            $(subst $(_cpSEP),$(space)$(_cpSEP)$(space),$(1)))),\
105  $(if $(findstring $(_cpSEP),$(w)),\
106    $(eval _cpRET += $(_cpLHS))$(eval _cpLHS := $(w)),\
107    $(eval _cpRET += $(_cpLHS)$(w))$(eval _cpLHS :=)))\
108$(if $(_cpLHS),$(_cpRET)$(space)$(_cpLHS),$(_cpRET))\
109$(eval _cpSEP :=)\
110$(eval _cpLHS :=)\
111$(eval _cpRET :=))
112endef
113
114# Sanity check for collapse-pairs.
115ifneq (a=b c=d e= f=g h=,$(call collapse-pairs,a=b c= d e = f = g h=))
116  $(error collapse-pairs sanity check failure)
117endif
118ifneq (a:=b c:=d e:=f g:=h,$(call collapse-pairs,a:=b c:= d e :=f g := h,:=))
119  $(error collapse-pairs sanity check failure)
120endif
121
122###########################################################
123## Given a list of pairs, if multiple pairs have the same
124## first components, keep only the first pair.
125##
126## $(1): list of pairs
127## $(2): the separator word, such as ":", "=", etc.
128define uniq-pairs-by-first-component
129$(eval _upbfc_fc_set :=)\
130$(strip $(foreach w,$(1), $(eval _first := $(word 1,$(subst $(2),$(space),$(w))))\
131    $(if $(filter $(_upbfc_fc_set),$(_first)),,$(w)\
132        $(eval _upbfc_fc_set += $(_first)))))\
133$(eval _upbfc_fc_set :=)\
134$(eval _first:=)
135endef
136