arm-trusted-firmware/make_helpers/utilities.mk
Chris Kay 0dfa3dea7d 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>
2024-06-14 15:54:47 +00:00

102 lines
2.2 KiB
Makefile
Raw Blame History

#
# Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
space :=
space := $(space) $(space)
comma := ,
null := <20>
compat-path = $(subst $(space),$(null),$(1))
decompat-path = $(subst $(null), ,$(1))
absolute-path = $(call decompat-path,$(abspath $(call compat-path,$(1))))
real-path = $(call decompat-path,$(realpath $(call compat-path,$(1))))
file-name = $(call decompat-path,$(notdir $(call compat-path,$(1))))
directory-name = $(call decompat-path,$(dir $(call compat-path,$(1))))
escape-shell = '$(subst ','\'',$(1))'
#
# Upper-case a string value.
#
# Parameters:
#
# - $(1): The string to upper-case.
#
# Example usage:
#
# $(call uppercase,HeLlO wOrLd) # "HELLO WORLD"
#
uppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
#
# Lower-case a string value.
#
# Parameters:
#
# - $(1): The string to lower-case.
#
# Example usage:
#
# $(call lowercase,HeLlO wOrLd) # "hello world"
#
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)