blob: ce6d6fbe9f676087835938621cb0efa13f54ec23 [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
31# Sanity-check 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 :=
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###########################################################
91## Convert "a=b c= d e = f" into "a=b c=d e=f"
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$(eval _cpSEP := $(strip $(if $(2),$(2),=)))\
100$(strip $(subst $(space)$(_cpSEP)$(space),$(_cpSEP),$(strip \
101 $(subst $(_cpSEP), $(_cpSEP) ,$(1)))$(space)))
102endef
103
104###########################################################
105## Given a list of pairs, if multiple pairs have the same
106## first components, keep only the first pair.
107##
108## $(1): list of pairs
109## $(2): the separator word, such as ":", "=", etc.
110define uniq-pairs-by-first-component
111$(eval _upbfc_fc_set :=)\
112$(strip $(foreach w,$(1), $(eval _first := $(word 1,$(subst $(2),$(space),$(w))))\
113 $(if $(filter $(_upbfc_fc_set),$(_first)),,$(w)\
114 $(eval _upbfc_fc_set += $(_first)))))\
115$(eval _upbfc_fc_set :=)\
116$(eval _first:=)
117endef