aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/core.mk56
-rw-r--r--common/math.mk270
-rw-r--r--common/strings.mk117
3 files changed, 443 insertions, 0 deletions
diff --git a/common/core.mk b/common/core.mk
new file mode 100644
index 000000000..e5264b072
--- /dev/null
+++ b/common/core.mk
@@ -0,0 +1,56 @@
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# Only use ANDROID_BUILD_SHELL to wrap around bash.
18# DO NOT use other shells such as zsh.
19ifdef ANDROID_BUILD_SHELL
20SHELL := $(ANDROID_BUILD_SHELL)
21else
22# Use bash, not whatever shell somebody has installed as /bin/sh
23# This is repeated from main.mk, since envsetup.sh runs this file
24# directly.
25SHELL := /bin/bash
26endif
27
28# Utility variables.
29empty :=
30space := $(empty) $(empty)
31comma := ,
32# Note that make will eat the newline just before endef.
33define newline
34
35
36endef
37# The pound character "#"
38define pound
39#
40endef
41# Unfortunately you can't simply define backslash as \ or \\.
42backslash := \a
43backslash := $(patsubst %a,%,$(backslash))
44
45# Prevent accidentally changing these variables
46.KATI_READONLY := SHELL empty space comma newline pound backslash
47
48# Basic warning/error wrappers. These will be redefined to include the local
49# module information when reading Android.mk files.
50define pretty-warning
51$(warning $(1))
52endef
53
54define pretty-error
55$(error $(1))
56endef
diff --git a/common/math.mk b/common/math.mk
new file mode 100644
index 000000000..ac3151e3c
--- /dev/null
+++ b/common/math.mk
@@ -0,0 +1,270 @@
1#
2# Copyright (C) 2017 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# Basic math functions for non-negative integers <= 100
19#
20# (SDK versions for example)
21###########################################################
22__MATH_POS_NUMBERS := 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 \
23 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 \
24 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 \
25 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 \
26 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
27__MATH_NUMBERS := 0 $(__MATH_POS_NUMBERS)
28
29math-error = $(call pretty-error,$(1))
30math-expect :=
31math-expect-true :=
32math-expect :=
33math-expect-error :=
34
35# Run the math tests with:
36# make -f ${ANDROID_BUILD_TOP}/build/make/core/math.mk RUN_MATH_TESTS=true
37# $(get_build_var CKATI) -f ${ANDROID_BUILD_TOP}//build/make/core/math.mk RUN_MATH_TESTS=true
38ifdef RUN_MATH_TESTS
39 MATH_TEST_FAILURE :=
40 MATH_TEST_ERROR :=
41 math-error = $(if $(MATH_TEST_ERROR),,$(eval MATH_TEST_ERROR:=$(1)))
42 define math-expect
43 $(eval got:=$$$1) \
44 $(if $(subst $(got),,$(2))$(subst $(2),,$(got))$(MATH_TEST_ERROR), \
45 $(if $(MATH_TEST_ERROR),$(warning $(MATH_TEST_ERROR)),$(warning $$$1 '$(got)' != '$(2)')) \
46 $(eval MATH_TEST_FAILURE := true)) \
47 $(eval MATH_TEST_ERROR :=) \
48 $(eval got:=)
49 endef
50 math-expect-true = $(call math-expect,$(1),true)
51 math-expect-false = $(call math-expect,$(1),)
52
53 define math-expect-error
54 $(eval got:=$$$1) \
55 $(if $(subst $(MATH_TEST_ERROR),,$(2))$(subst $(2),,$(MATH_TEST_ERROR)), \
56 $(warning '$(MATH_TEST_ERROR)' != '$(2)') \
57 $(eval MATH_TEST_FAILURE := true)) \
58 $(eval MATH_TEST_ERROR :=) \
59 $(eval got:=)
60 endef
61endif
62
63# Returns true if $(1) is a non-negative integer <= 100, otherwise returns nothing.
64define math_is_number
65$(strip \
66 $(if $(1),,$(call math-error,Argument missing)) \
67 $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
68 $(if $(filter $(1),$(__MATH_NUMBERS)),true))
69endef
70
71define math_is_zero
72$(strip \
73 $(if $(word 2,$(1)),$(call math-error,Multiple words in a single argument: $(1))) \
74 $(if $(filter 0,$(1)),true))
75endef
76
77$(call math-expect-true,(call math_is_number,0))
78$(call math-expect-true,(call math_is_number,2))
79$(call math-expect-false,(call math_is_number,foo))
80$(call math-expect-false,(call math_is_number,-1))
81$(call math-expect-error,(call math_is_number,1 2),Multiple words in a single argument: 1 2)
82$(call math-expect-error,(call math_is_number,no 2),Multiple words in a single argument: no 2)
83
84$(call math-expect-true,(call math_is_zero,0))
85$(call math-expect-false,(call math_is_zero,1))
86$(call math-expect-false,(call math_is_zero,foo))
87$(call math-expect-error,(call math_is_zero,1 2),Multiple words in a single argument: 1 2)
88$(call math-expect-error,(call math_is_zero,no 2),Multiple words in a single argument: no 2)
89
90define _math_check_valid
91$(if $(call math_is_number,$(1)),,$(call math-error,Only non-negative integers <= 100 are supported (not $(1))))
92endef
93
94$(call math-expect,(call _math_check_valid,0))
95$(call math-expect,(call _math_check_valid,1))
96$(call math-expect,(call _math_check_valid,100))
97$(call math-expect-error,(call _math_check_valid,-1),Only non-negative integers <= 100 are supported (not -1))
98$(call math-expect-error,(call _math_check_valid,101),Only non-negative integers <= 100 are supported (not 101))
99$(call math-expect-error,(call _math_check_valid,),Argument missing)
100$(call math-expect-error,(call _math_check_valid,1 2),Multiple words in a single argument: 1 2)
101
102# return a list containing integers ranging from [$(1),$(2)]
103define int_range_list
104$(strip \
105 $(call _math_check_valid,$(1))$(call _math_check_valid,$(2)) \
106 $(if $(call math_is_zero,$(1)),0)\
107 $(wordlist $(if $(call math_is_zero,$(1)),1,$(1)),$(2),$(__MATH_POS_NUMBERS)))
108endef
109
110$(call math-expect,(call int_range_list,0,1),0 1)
111$(call math-expect,(call int_range_list,1,1),1)
112$(call math-expect,(call int_range_list,1,2),1 2)
113$(call math-expect,(call int_range_list,2,1),)
114$(call math-expect-error,(call int_range_list,1,101),Only non-negative integers <= 100 are supported (not 101))
115
116
117# Returns the greater of $1 or $2.
118# If $1 or $2 is not a positive integer <= 100, then an error is generated.
119define math_max
120$(strip $(call _math_check_valid,$(1)) $(call _math_check_valid,$(2)) \
121 $(lastword $(filter $(1) $(2),$(__MATH_NUMBERS))))
122endef
123
124$(call math-expect-error,(call math_max),Argument missing)
125$(call math-expect-error,(call math_max,1),Argument missing)
126$(call math-expect-error,(call math_max,1 2,3),Multiple words in a single argument: 1 2)
127$(call math-expect,(call math_max,0,1),1)
128$(call math-expect,(call math_max,1,0),1)
129$(call math-expect,(call math_max,1,1),1)
130$(call math-expect,(call math_max,5,42),42)
131$(call math-expect,(call math_max,42,5),42)
132
133define math_gt_or_eq
134$(if $(filter $(1),$(call math_max,$(1),$(2))),true)
135endef
136
137define math_lt
138$(if $(call math_gt_or_eq,$(1),$(2)),,true)
139endef
140
141$(call math-expect-true,(call math_gt_or_eq, 2, 1))
142$(call math-expect-true,(call math_gt_or_eq, 1, 1))
143$(call math-expect-false,(call math_gt_or_eq, 1, 2))
144
145# $1 is the variable name to increment
146define inc_and_print
147$(strip $(eval $(1) := $($(1)) .)$(words $($(1))))
148endef
149
150ifdef RUN_MATH_TESTS
151a :=
152$(call math-expect,(call inc_and_print,a),1)
153$(call math-expect,(call inc_and_print,a),2)
154$(call math-expect,(call inc_and_print,a),3)
155$(call math-expect,(call inc_and_print,a),4)
156endif
157
158# Returns the words in $2 that are numbers and are less than $1
159define numbers_less_than
160$(strip \
161 $(foreach n,$2, \
162 $(if $(call math_is_number,$(n)), \
163 $(if $(call math_lt,$(n),$(1)), \
164 $(n)))))
165endef
166
167$(call math-expect,(call numbers_less_than,0,0 1 2 3),)
168$(call math-expect,(call numbers_less_than,1,0 2 1 3),0)
169$(call math-expect,(call numbers_less_than,2,0 2 1 3),0 1)
170$(call math-expect,(call numbers_less_than,3,0 2 1 3),0 2 1)
171$(call math-expect,(call numbers_less_than,4,0 2 1 3),0 2 1 3)
172$(call math-expect,(call numbers_less_than,3,0 2 1 3 2),0 2 1 2)
173
174_INT_LIMIT_WORDS := $(foreach a,x x,$(foreach b,x x x x x x x x x x x x x x x x,\
175 $(foreach c,x x x x x x x x x x x x x x x x,x x x x x x x x x x x x x x x x)))
176
177define _int_encode
178$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\
179 $(call math-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\
180 $(wordlist 1,$(1),$(_INT_LIMIT_WORDS)))
181endef
182
183# _int_max returns the maximum of the two arguments
184# input: two (x) lists; output: one (x) list
185# integer cannot be passed in directly. It has to be converted using _int_encode.
186define _int_max
187$(subst xx,x,$(join $(1),$(2)))
188endef
189
190# first argument is greater than second argument
191# output: non-empty if true
192# integer cannot be passed in directly. It has to be converted using _int_encode.
193define _int_greater-than
194$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2))))
195endef
196
197# first argument equals to second argument
198# output: non-empty if true
199# integer cannot be passed in directly. It has to be converted using _int_encode.
200define _int_equal
201$(filter $(words $(1)),$(words $(2)))
202endef
203
204# first argument is greater than or equal to second argument
205# output: non-empty if true
206# integer cannot be passed in directly. It has to be converted using _int_encode.
207define _int_greater-or-equal
208$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2))
209endef
210
211define int_plus
212$(words $(call _int_encode,$(1)) $(call _int_encode,$(2)))
213endef
214
215$(call math-expect,(call int_plus,0,0),0)
216$(call math-expect,(call int_plus,0,1),1)
217$(call math-expect,(call int_plus,1,0),1)
218$(call math-expect,(call int_plus,1,100),101)
219$(call math-expect,(call int_plus,100,100),200)
220
221define int_subtract
222$(strip \
223 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\
224 $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\
225 $(call math-error,subtract underflow $(1) - $(2))))
226endef
227
228$(call math-expect,(call int_subtract,0,0),0)
229$(call math-expect,(call int_subtract,1,0),1)
230$(call math-expect,(call int_subtract,1,1),0)
231$(call math-expect,(call int_subtract,100,1),99)
232$(call math-expect,(call int_subtract,200,100),100)
233$(call math-expect-error,(call int_subtract,0,1),subtract underflow 0 - 1)
234
235define int_multiply
236$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2))))
237endef
238
239$(call math-expect,(call int_multiply,0,0),0)
240$(call math-expect,(call int_multiply,1,0),0)
241$(call math-expect,(call int_multiply,1,1),1)
242$(call math-expect,(call int_multiply,100,1),100)
243$(call math-expect,(call int_multiply,1,100),100)
244$(call math-expect,(call int_multiply,4,100),400)
245$(call math-expect,(call int_multiply,100,4),400)
246
247define int_divide
248$(if $(filter 0,$(2)),$(call math-error,division by zero is not allowed!),$(strip \
249 $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \
250 $(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0)))
251endef
252
253$(call math-expect,(call int_divide,1,1),1)
254$(call math-expect,(call int_divide,200,1),200)
255$(call math-expect,(call int_divide,200,3),66)
256$(call math-expect,(call int_divide,1,2),0)
257$(call math-expect-error,(call int_divide,0,0),division by zero is not allowed!)
258$(call math-expect-error,(call int_divide,1,0),division by zero is not allowed!)
259
260ifdef RUN_MATH_TESTS
261 ifdef MATH_TEST_FAILURE
262 math-tests:
263 @echo FAIL
264 @false
265 else
266 math-tests:
267 @echo PASS
268 endif
269 .PHONY: math-tests
270endif
diff --git a/common/strings.mk b/common/strings.mk
new file mode 100644
index 000000000..ce6d6fbe9
--- /dev/null
+++ b/common/strings.mk
@@ -0,0 +1,117 @@
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###########################################################
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 :=
45
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