build: add facilities for interpreting boolean values

This is another small addition to the build system utlities to make it
easier to determine the truthiness of an arbitrary value.

This change adds the `bool` function, which takes a value and determines
whether the value is "truthy". We consider a value to be truthy if it is
NOT one of: "0", "n", "no", "f" or "false" (all case-insensitive).

If the value is truthy then it is returned as-is. Otherwise, no value
is returned.

Change-Id: I19347f4c3ae00a6b448514a28cc2d9d06f683f25
Signed-off-by: Chris Kay <chris.kay@arm.com>
This commit is contained in:
Chris Kay 2024-05-29 22:16:31 +00:00
parent 3af4eb50c0
commit 0dfa3dea7d

View file

@ -48,3 +48,55 @@ uppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
#
lowercase = $(shell echo $(call escape-shell,$(1)) | tr '[:upper:]' '[:lower:]')
#
# Determine the "truthiness" of a value.
#
# Parameters:
#
# - $(1): The value to determine the truthiness of.
#
# A value is considered to be falsy if it is:
#
# - empty, or
# - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
#
# If the value is truthy then the value is returned as-is, otherwise no value
# is returned.
#
# Example usage:
#
# truthy := y
# truthy-bool := $(call bool,$(truthy)) # "y"
#
# falsy := n
# falsy-bool := $(call bool,$(falsy)) # <empty>
#
bool = $(filter-out 0 n no f false,$(call lowercase,$(1)))
#
# Determine the "truthiness" of a value, returning 0 or 1.
#
# Parameters:
#
# - $(1): The value to determine the truthiness of.
#
# A value is considered to be falsy if it is:
#
# - empty, or
# - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
#
# If the value is truthy then the value is returned as-is, otherwise no value
# is returned.
#
# Example usage:
#
# truthy := y
# truthy-bool := $(call bool,$(truthy)) # "1"
#
# falsy := n
# falsy-bool := $(call bool,$(falsy)) # "0"
#
bool-01 = $(if $(call bool,$(1)),1,0)