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 | |
Daniel Cardenas | de67359 | 2020-07-30 22:11:54 +0000 | [diff] [blame] | 31 | # Test to-lower and to-upper |
Yifan Hong | c49bddf | 2018-09-21 16:39:37 -0700 | [diff] [blame] | 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 | ########################################################### |
Bob Badour | 879cfa8 | 2021-04-15 10:41:38 -0700 | [diff] [blame] | 91 | ## 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 | |
| 104 | define wordlist-colon |
| 105 | $(subst $(space),:,$(wordlist $(1),$(2),$(subst :,$(space),$(3)))) |
| 106 | endef |
| 107 | |
| 108 | ########################################################### |
Youkichi Hosoi | f6652f4 | 2020-04-01 05:07:24 +0900 | [diff] [blame] | 109 | ## Convert "a=b c= d e = f = g h=" into "a=b c=d e= f=g h=" |
Anton Hansson | 4967b34 | 2018-10-03 13:10:54 +0100 | [diff] [blame] | 110 | ## |
| 111 | ## $(1): list to collapse |
| 112 | ## $(2): if set, separator word; usually "=", ":", or ":=" |
| 113 | ## Defaults to "=" if not set. |
| 114 | ########################################################### |
| 115 | |
| 116 | define collapse-pairs |
Youkichi Hosoi | f6652f4 | 2020-04-01 05:07:24 +0900 | [diff] [blame] | 117 | $(strip \ |
Anton Hansson | 4967b34 | 2018-10-03 13:10:54 +0100 | [diff] [blame] | 118 | $(eval _cpSEP := $(strip $(if $(2),$(2),=)))\ |
Youkichi Hosoi | f6652f4 | 2020-04-01 05:07:24 +0900 | [diff] [blame] | 119 | $(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 Hansson | 4967b34 | 2018-10-03 13:10:54 +0100 | [diff] [blame] | 130 | endef |
| 131 | |
Youkichi Hosoi | f6652f4 | 2020-04-01 05:07:24 +0900 | [diff] [blame] | 132 | # Sanity check for collapse-pairs. |
| 133 | ifneq (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) |
| 135 | endif |
| 136 | ifneq (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) |
| 138 | endif |
| 139 | |
Anton Hansson | 4967b34 | 2018-10-03 13:10:54 +0100 | [diff] [blame] | 140 | ########################################################### |
| 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. |
| 146 | define 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:=) |
| 153 | endef |