blob: 768d061b20554a6ffe2182d23d0ff9d37759b3dd [file] [log] [blame]
Yifan Hongc49bddf2018-09-21 16:39:37 -07001#
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
Daniel Cardenasde673592020-07-30 22:11:54 +000031# Test to-lower and to-upper
Yifan Hongc49bddf2018-09-21 16:39:37 -070032lower := 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 :=
Anton Hansson4967b342018-10-03 13:10:54 +010045
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###########################################################
Bob Badour879cfa82021-04-15 10:41:38 -070091## Read a colon-separated sublist out of a colon-separated
92## list of words.
93## This has similar behavior to the built-in function
94## $(wordlist s,e,str) except both the input and output
95## word lists are colon-separated.
96##
97## The individual words may not contain spaces.
98##
99## $(1): 1 based index start
100## $(2): 1 based index end (can be 0)
101## $(3): value of the form a:b:c...
102###########################################################
103
104define wordlist-colon
105$(subst $(space),:,$(wordlist $(1),$(2),$(subst :,$(space),$(3))))
106endef
107
108###########################################################
Youkichi Hosoif6652f42020-04-01 05:07:24 +0900109## Convert "a=b c= d e = f = g h=" into "a=b c=d e= f=g h="
Anton Hansson4967b342018-10-03 13:10:54 +0100110##
111## $(1): list to collapse
112## $(2): if set, separator word; usually "=", ":", or ":="
113## Defaults to "=" if not set.
114###########################################################
115
116define collapse-pairs
Youkichi Hosoif6652f42020-04-01 05:07:24 +0900117$(strip \
Anton Hansson4967b342018-10-03 13:10:54 +0100118$(eval _cpSEP := $(strip $(if $(2),$(2),=)))\
Youkichi Hosoif6652f42020-04-01 05:07:24 +0900119$(eval _cpLHS :=)\
120$(eval _cpRET :=)\
121$(foreach w,$(subst $(space)$(_cpSEP),$(_cpSEP),$(strip \
122 $(subst $(_cpSEP),$(space)$(_cpSEP)$(space),$(1)))),\
123 $(if $(findstring $(_cpSEP),$(w)),\
124 $(eval _cpRET += $(_cpLHS))$(eval _cpLHS := $(w)),\
125 $(eval _cpRET += $(_cpLHS)$(w))$(eval _cpLHS :=)))\
126$(if $(_cpLHS),$(_cpRET)$(space)$(_cpLHS),$(_cpRET))\
127$(eval _cpSEP :=)\
128$(eval _cpLHS :=)\
129$(eval _cpRET :=))
Anton Hansson4967b342018-10-03 13:10:54 +0100130endef
131
Youkichi Hosoif6652f42020-04-01 05:07:24 +0900132# Sanity check for collapse-pairs.
133ifneq (a=b c=d e= f=g h=,$(call collapse-pairs,a=b c= d e = f = g h=))
134 $(error collapse-pairs sanity check failure)
135endif
136ifneq (a:=b c:=d e:=f g:=h,$(call collapse-pairs,a:=b c:= d e :=f g := h,:=))
137 $(error collapse-pairs sanity check failure)
138endif
139
Anton Hansson4967b342018-10-03 13:10:54 +0100140###########################################################
141## Given a list of pairs, if multiple pairs have the same
142## first components, keep only the first pair.
143##
144## $(1): list of pairs
145## $(2): the separator word, such as ":", "=", etc.
146define uniq-pairs-by-first-component
147$(eval _upbfc_fc_set :=)\
148$(strip $(foreach w,$(1), $(eval _first := $(word 1,$(subst $(2),$(space),$(w))))\
149 $(if $(filter $(_upbfc_fc_set),$(_first)),,$(w)\
150 $(eval _upbfc_fc_set += $(_first)))))\
151$(eval _upbfc_fc_set :=)\
152$(eval _first:=)
153endef