Yifan Hong | c49bddf | 2018-09-21 16:39:37 -0700 | [diff] [blame] | 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 | ########################################################### |
| 22 | to-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 | ########################################################### |
| 29 | to-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 |
| 32 | lower := abcdefghijklmnopqrstuvwxyz-_ |
| 33 | upper := ABCDEFGHIJKLMNOPQRSTUVWXYZ-_ |
| 34 | |
| 35 | ifneq ($(lower),$(call to-lower,$(upper))) |
| 36 | $(error to-lower sanity check failure) |
| 37 | endif |
| 38 | |
| 39 | ifneq ($(upper),$(call to-upper,$(lower))) |
| 40 | $(error to-upper sanity check failure) |
| 41 | endif |
| 42 | |
| 43 | lower := |
| 44 | upper := |
Anton Hansson | 4967b34 | 2018-10-03 13:10:54 +0100 | [diff] [blame] | 45 | |
| 46 | ########################################################### |
| 47 | ## Returns true if $(1) and $(2) are equal. Returns |
| 48 | ## the empty string if they are not equal. |
| 49 | ########################################################### |
| 50 | define 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 | )) |
| 59 | endef |
| 60 | |
| 61 | ########################################################### |
| 62 | ## Convert "a b c" into "a:b:c" |
| 63 | ########################################################### |
| 64 | define normalize-path-list |
| 65 | $(subst $(space),:,$(strip $(1))) |
| 66 | endef |
| 67 | |
| 68 | ########################################################### |
| 69 | ## Convert "a b c" into "a,b,c" |
| 70 | ########################################################### |
| 71 | define normalize-comma-list |
| 72 | $(subst $(space),$(comma),$(strip $(1))) |
| 73 | endef |
| 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 | |
| 86 | define word-colon |
| 87 | $(word $(1),$(subst :,$(space),$(2))) |
| 88 | endef |
| 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 | |
| 98 | define collapse-pairs |
| 99 | $(eval _cpSEP := $(strip $(if $(2),$(2),=)))\ |
| 100 | $(strip $(subst $(space)$(_cpSEP)$(space),$(_cpSEP),$(strip \ |
| 101 | $(subst $(_cpSEP), $(_cpSEP) ,$(1)))$(space))) |
| 102 | endef |
| 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. |
| 110 | define 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:=) |
| 117 | endef |