xref: /rk3399_ARM-atf/make_helpers/utilities.mk (revision d28673971623b33d6078f62362e91d66cb6fd05b)
14731c00bSChris Kay#
24731c00bSChris Kay# Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
34731c00bSChris Kay#
44731c00bSChris Kay# SPDX-License-Identifier: BSD-3-Clause
54731c00bSChris Kay#
64731c00bSChris Kay
74731c00bSChris Kayspace :=
84731c00bSChris Kayspace := $(space) $(space)
94731c00bSChris Kaycomma := ,
104731c00bSChris Kay
114731c00bSChris Kaynull := �
124731c00bSChris Kay
134731c00bSChris Kaycompat-path = $(subst $(space),$(null),$(1))
144731c00bSChris Kaydecompat-path = $(subst $(null), ,$(1))
154731c00bSChris Kay
164731c00bSChris Kayabsolute-path = $(call decompat-path,$(abspath $(call compat-path,$(1))))
174731c00bSChris Kayreal-path = $(call decompat-path,$(realpath $(call compat-path,$(1))))
184731c00bSChris Kay
194731c00bSChris Kayfile-name = $(call decompat-path,$(notdir $(call compat-path,$(1))))
204731c00bSChris Kaydirectory-name = $(call decompat-path,$(dir $(call compat-path,$(1))))
214731c00bSChris Kay
224731c00bSChris Kayescape-shell = '$(subst ','\'',$(1))'
233af4eb50SChris Kay
243af4eb50SChris Kay#
253af4eb50SChris Kay# Upper-case a string value.
263af4eb50SChris Kay#
273af4eb50SChris Kay# Parameters:
283af4eb50SChris Kay#
293af4eb50SChris Kay#   - $(1): The string to upper-case.
303af4eb50SChris Kay#
313af4eb50SChris Kay# Example usage:
323af4eb50SChris Kay#
333af4eb50SChris Kay#     $(call uppercase,HeLlO wOrLd) # "HELLO WORLD"
343af4eb50SChris Kay#
353af4eb50SChris Kay
363af4eb50SChris Kayuppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
373af4eb50SChris Kay
383af4eb50SChris Kay#
393af4eb50SChris Kay# Lower-case a string value.
403af4eb50SChris Kay#
413af4eb50SChris Kay# Parameters:
423af4eb50SChris Kay#
433af4eb50SChris Kay#   - $(1): The string to lower-case.
443af4eb50SChris Kay#
453af4eb50SChris Kay# Example usage:
463af4eb50SChris Kay#
473af4eb50SChris Kay#     $(call lowercase,HeLlO wOrLd) # "hello world"
483af4eb50SChris Kay#
493af4eb50SChris Kay
503af4eb50SChris Kaylowercase = $(shell echo $(call escape-shell,$(1)) | tr '[:upper:]' '[:lower:]')
510dfa3deaSChris Kay
520dfa3deaSChris Kay#
530dfa3deaSChris Kay# Determine the "truthiness" of a value.
540dfa3deaSChris Kay#
550dfa3deaSChris Kay# Parameters:
560dfa3deaSChris Kay#
570dfa3deaSChris Kay#   - $(1): The value to determine the truthiness of.
580dfa3deaSChris Kay#
590dfa3deaSChris Kay# A value is considered to be falsy if it is:
600dfa3deaSChris Kay#
610dfa3deaSChris Kay#   - empty, or
620dfa3deaSChris Kay#   - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
630dfa3deaSChris Kay#
640dfa3deaSChris Kay# If the value is truthy then the value is returned as-is, otherwise no value
650dfa3deaSChris Kay# is returned.
660dfa3deaSChris Kay#
670dfa3deaSChris Kay# Example usage:
680dfa3deaSChris Kay#
690dfa3deaSChris Kay#     truthy := y
700dfa3deaSChris Kay#     truthy-bool := $(call bool,$(truthy)) # "y"
710dfa3deaSChris Kay#
720dfa3deaSChris Kay#     falsy := n
730dfa3deaSChris Kay#     falsy-bool := $(call bool,$(falsy)) # <empty>
740dfa3deaSChris Kay#
750dfa3deaSChris Kay
760dfa3deaSChris Kaybool = $(filter-out 0 n no f false,$(call lowercase,$(1)))
770dfa3deaSChris Kay
780dfa3deaSChris Kay#
790dfa3deaSChris Kay# Determine the "truthiness" of a value, returning 0 or 1.
800dfa3deaSChris Kay#
810dfa3deaSChris Kay# Parameters:
820dfa3deaSChris Kay#
830dfa3deaSChris Kay#   - $(1): The value to determine the truthiness of.
840dfa3deaSChris Kay#
850dfa3deaSChris Kay# A value is considered to be falsy if it is:
860dfa3deaSChris Kay#
870dfa3deaSChris Kay#   - empty, or
880dfa3deaSChris Kay#   - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
890dfa3deaSChris Kay#
900dfa3deaSChris Kay# If the value is truthy then the value is returned as-is, otherwise no value
910dfa3deaSChris Kay# is returned.
920dfa3deaSChris Kay#
930dfa3deaSChris Kay# Example usage:
940dfa3deaSChris Kay#
950dfa3deaSChris Kay#     truthy := y
960dfa3deaSChris Kay#     truthy-bool := $(call bool,$(truthy)) # "1"
970dfa3deaSChris Kay#
980dfa3deaSChris Kay#     falsy := n
990dfa3deaSChris Kay#     falsy-bool := $(call bool,$(falsy)) # "0"
1000dfa3deaSChris Kay#
1010dfa3deaSChris Kay
1020dfa3deaSChris Kaybool-01 = $(if $(call bool,$(1)),1,0)
103*d2867397SChris Kay
104*d2867397SChris Kay#
105*d2867397SChris Kay# Determine whether a variable is defined or not.
106*d2867397SChris Kay#
107*d2867397SChris Kay# Parameters:
108*d2867397SChris Kay#
109*d2867397SChris Kay#   - $(1): The variable to check.
110*d2867397SChris Kay#
111*d2867397SChris Kay# Example usage:
112*d2867397SChris Kay#
113*d2867397SChris Kay#     xyz-defined := $(call defined,xyz) # <empty>
114*d2867397SChris Kay#
115*d2867397SChris Kay#     xyz :=
116*d2867397SChris Kay#     xyz-defined := $(call defined,xyz) # <non-empty>
117*d2867397SChris Kay#
118*d2867397SChris Kay#     xyz := hello
119*d2867397SChris Kay#     xyz-defined := $(call defined,xyz) # <non-empty>
120*d2867397SChris Kay#
121*d2867397SChris Kay
122*d2867397SChris Kaydefined = $(call bool,$(filter-out undefined,$(origin $(1))))
123