1# vars for use by utils
2__empty :=
3__space := $(__empty) $(__empty)
4__colon := $(__empty):$(__empty)
5__underscore := $(__empty)_$(__empty)
6
7# $(call match-word,w1,w2)
8# checks if w1 == w2
9# How it works
10#   if (w1-w2 not __empty or w2-w1 not __empty) then not_match else match
11#
12# returns true or __empty
13#$(warning :$(1): :$(2): :$(subst $(1),,$(2)):) \
14#$(warning :$(2): :$(1): :$(subst $(2),,$(1)):) \
15#
16define match-word
17$(strip \
18  $(if $(or $(subst $(1),$(__empty),$(2)),$(subst $(2),$(__empty),$(1))),,true) \
19)
20endef
21
22# $(call find-word-in-list,w,wlist)
23# finds an exact match of word w in word list wlist
24#
25# How it works
26#   fill wlist spaces with __colon
27#   wrap w with __colon
28#   search word w in list wl, if found match m, return stripped word w
29#
30# returns stripped word or __empty
31define find-word-in-list
32$(strip \
33  $(eval wl:= $(__colon)$(subst $(__space),$(__colon),$(strip $(2)))$(__colon)) \
34  $(eval w:= $(__colon)$(strip $(1))$(__colon)) \
35  $(eval m:= $(findstring $(w),$(wl))) \
36  $(if $(m),$(1),) \
37)
38endef
39
40# $(call match-word-in-list,w,wlist)
41# does an exact match of word w in word list wlist
42# How it works
43#   if the input word is not __empty
44#     return output of an exact match of word w in wordlist wlist
45#   else
46#     return __empty
47# returns true or __empty
48define match-word-in-list
49$(strip \
50  $(if $(strip $(1)), \
51    $(call match-word,$(call find-word-in-list,$(1),$(2)),$(strip $(1))), \
52  ) \
53)
54endef
55
56# $(call match-prefix,p,delim,w/wlist)
57# matches prefix p in wlist using delimiter delim
58#
59# How it works
60#   trim the words in wlist w
61#   if find-word-in-list returns not __empty
62#     return true
63#   else
64#     return __empty
65#
66define match-prefix
67$(strip \
68  $(eval w := $(strip $(1)$(strip $(2)))) \
69  $(eval text := $(patsubst $(w)%,$(1),$(3))) \
70  $(if $(call match-word-in-list,$(1),$(text)),true,) \
71)
72endef
73
74# ----
75# The following utilities are meant for board platform specific
76# featurisation
77
78# $(call get-vendor-board-platforms,v)
79# returns list of board platforms for vendor v
80define get-vendor-board-platforms
81$($(1)_BOARD_PLATFORMS)
82endef
83
84# $(call is-board-platform,bp)
85# returns true or __empty
86define is-board-platform
87$(call match-word,$(1),$(TARGET_BOARD_PLATFORM))
88endef
89
90# $(call is-not-board-platform,bp)
91# returns true or __empty
92define is-not-board-platform
93$(if $(call match-word,$(1),$(TARGET_BOARD_PLATFORM)),,true)
94endef
95
96# $(call is-board-platform-in-list,bpl)
97# returns true or __empty
98define is-board-platform-in-list
99$(call match-word-in-list,$(TARGET_BOARD_PLATFORM),$(1))
100endef
101
102# $(call is-vendor-board-platform,vendor)
103# returns true or __empty
104define is-vendor-board-platform
105$(strip \
106  $(call match-word-in-list,$(TARGET_BOARD_PLATFORM),\
107    $(call get-vendor-board-platforms,$(1)) \
108  ) \
109)
110endef
111
112# $(call is-chipset-in-board-platform,chipset)
113# does a prefix match of chipset in TARGET_BOARD_PLATFORM
114# uses underscore as a delimiter
115#
116# returns true or __empty
117define is-chipset-in-board-platform
118$(call match-prefix,$(1),$(__underscore),$(TARGET_BOARD_PLATFORM))
119endef
120
121# $(call is-chipset-prefix-in-board-platform,prefix)
122# does a chipset prefix match in TARGET_BOARD_PLATFORM
123# assumes '_' and 'a' as the delimiter to the chipset prefix
124#
125# How it works
126#   if ($(prefix)_ or $(prefix)a match in board platform)
127#     return true
128#   else
129#     return __empty
130#
131define is-chipset-prefix-in-board-platform
132$(strip \
133  $(eval delim_a := $(__empty)a$(__empty)) \
134  $(if \
135    $(or \
136      $(call match-prefix,$(1),$(delim_a),$(TARGET_BOARD_PLATFORM)), \
137      $(call match-prefix,$(1),$(__underscore),$(TARGET_BOARD_PLATFORM)), \
138    ), \
139    true, \
140  ) \
141)
142endef
143
144#----
145# The following utilities are meant for Android Code Name
146# specific featurisation
147#
148# refer http://source.android.com/source/build-numbers.html
149# for code names and associated sdk versions
150CUPCAKE_SDK_VERSIONS := 3
151DONUT_SDK_VERSIONS   := 4
152ECLAIR_SDK_VERSIONS  := 5 6 7
153FROYO_SDK_VERSIONS   := 8
154GINGERBREAD_SDK_VERSIONS := 9 10
155HONEYCOMB_SDK_VERSIONS := 11 12 13
156ICECREAM_SANDWICH_SDK_VERSIONS := 14 15
157JELLY_BEAN_SDK_VERSIONS := 16 17 18
158
159# $(call is-platform-sdk-version-at-least,version)
160# version is a numeric SDK_VERSION defined above
161define is-platform-sdk-version-at-least
162$(strip \
163  $(if $(filter 1,$(shell echo "$$(( $(PLATFORM_SDK_VERSION) >= $(1) ))" )), \
164    true, \
165  ) \
166)
167endef
168
169# $(call is-android-codename,codename)
170# codename is one of cupcake,donut,eclair,froyo,gingerbread,icecream
171# please refer the $(codename)_SDK_VERSIONS declared above
172define is-android-codename
173$(strip \
174  $(if \
175    $(call match-word-in-list,$(PLATFORM_SDK_VERSION),$($(1)_SDK_VERSIONS)), \
176    true, \
177  ) \
178)
179endef
180
181# $(call is-android-codename-in-list,cnlist)
182# cnlist is combination/list of android codenames
183define is-android-codename-in-list
184$(strip \
185  $(eval acn := $(__empty)) \
186    $(foreach \
187      i,$(1),\
188      $(eval acn += \
189        $(if \
190          $(call \
191            match-word-in-list,\
192            $(PLATFORM_SDK_VERSION),\
193            $($(i)_SDK_VERSIONS)\
194          ),\
195          true,\
196        )\
197      )\
198    ) \
199  $(if $(strip $(acn)),true,) \
200)
201endef
202