Steven Moreland | a64f336 | 2017-05-31 12:48:55 -0700 | [diff] [blame] | 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 positive integers <= 100 |
| 19 | # |
| 20 | # (SDK versions for example) |
| 21 | ########################################################### |
| 22 | __MATH_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 | |
| 28 | # Returns true if $(1) is a positive integer <= 100, otherwise returns nothing. |
| 29 | define math_is_number |
| 30 | $(strip \ |
| 31 | $(if $(1),,$(error Argument missing)) \ |
| 32 | $(if $(word 2,$(1)),$(error Multiple words in a single argument: $(1))) \ |
| 33 | $(if $(filter $(1),$(__MATH_NUMBERS)),true)) |
| 34 | endef |
| 35 | |
| 36 | #$(warning true == $(call math_is_number,2)) |
| 37 | #$(warning == $(call math_is_number,foo)) |
| 38 | #$(call math_is_number,1 2) |
| 39 | #$(call math_is_number,no 2) |
| 40 | |
| 41 | define _math_check_valid |
| 42 | $(if $(call math_is_number,$(1)),,$(error Only positive integers <= 100 are supported (not $(1)))) |
| 43 | endef |
| 44 | |
| 45 | #$(call _math_check_valid,0) |
| 46 | #$(call _math_check_valid,1) |
| 47 | #$(call _math_check_valid,100) |
| 48 | #$(call _math_check_valid,101) |
| 49 | #$(call _math_check_valid,) |
| 50 | #$(call _math_check_valid,1 2) |
| 51 | |
| 52 | # Returns the greater of $1 or $2. |
| 53 | # If $1 or $2 is not a positive integer <= 100, then an error is generated. |
| 54 | define math_max |
| 55 | $(strip $(call _math_check_valid,$(1)) $(call _math_check_valid,$(2)) \ |
| 56 | $(lastword $(filter $(1) $(2),$(__MATH_NUMBERS)))) |
| 57 | endef |
| 58 | |
| 59 | #$(call math_max) |
| 60 | #$(call math_max,1) |
| 61 | #$(call math_max,1 2,3) |
| 62 | #$(warning 1 == $(call math_max,1,1)) |
| 63 | #$(warning 42 == $(call math_max,5,42)) |
| 64 | #$(warning 42 == $(call math_max,42,5)) |
| 65 | |
| 66 | define math_gt_or_eq |
| 67 | $(if $(filter $(1),$(call math_max,$(1),$(2))),true) |
| 68 | endef |
| 69 | |
| 70 | #$(warning $(call math_gt_or_eq, 2, 1)) |
| 71 | #$(warning $(call math_gt_or_eq, 1, 1)) |
| 72 | #$(warning $(if $(call math_gt_or_eq, 1, 2),false,true)) |
| 73 | |
| 74 | # $1 is the variable name to increment |
| 75 | define inc_and_print |
| 76 | $(strip $(eval $(1) := $($(1)) .)$(words $($(1)))) |
| 77 | endef |