xref: /rk3399_ARM-atf/make_helpers/utilities.mk (revision 0fcee05f13a7fe235b4c07efa96ce4841627d522)
14731c00bSChris Kay#
2c3273703SChris Kay# Copyright (c) 2024-2025, 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#
25a57b94ecSChris Kay# The grouped-target symbol. Grouped targets are not supported on versions of
26a57b94ecSChris Kay# GNU Make <= 4.2, which was most recently packaged with Ubuntu 20.04.
27a57b94ecSChris Kay#
28a57b94ecSChris Kay
29a57b94ecSChris Kay& := $(if $(filter grouped-target,$(.FEATURES)),&)
30a57b94ecSChris Kay
31a57b94ecSChris Kay#
323af4eb50SChris Kay# Upper-case a string value.
333af4eb50SChris Kay#
343af4eb50SChris Kay# Parameters:
353af4eb50SChris Kay#
363af4eb50SChris Kay#   - $(1): The string to upper-case.
373af4eb50SChris Kay#
383af4eb50SChris Kay# Example usage:
393af4eb50SChris Kay#
403af4eb50SChris Kay#     $(call uppercase,HeLlO wOrLd) # "HELLO WORLD"
413af4eb50SChris Kay#
423af4eb50SChris Kay
433af4eb50SChris Kayuppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
443af4eb50SChris Kay
453af4eb50SChris Kay#
463af4eb50SChris Kay# Lower-case a string value.
473af4eb50SChris Kay#
483af4eb50SChris Kay# Parameters:
493af4eb50SChris Kay#
503af4eb50SChris Kay#   - $(1): The string to lower-case.
513af4eb50SChris Kay#
523af4eb50SChris Kay# Example usage:
533af4eb50SChris Kay#
543af4eb50SChris Kay#     $(call lowercase,HeLlO wOrLd) # "hello world"
553af4eb50SChris Kay#
563af4eb50SChris Kay
573af4eb50SChris Kaylowercase = $(shell echo $(call escape-shell,$(1)) | tr '[:upper:]' '[:lower:]')
580dfa3deaSChris Kay
590dfa3deaSChris Kay#
600dfa3deaSChris Kay# Determine the "truthiness" of a value.
610dfa3deaSChris Kay#
620dfa3deaSChris Kay# Parameters:
630dfa3deaSChris Kay#
640dfa3deaSChris Kay#   - $(1): The value to determine the truthiness of.
650dfa3deaSChris Kay#
660dfa3deaSChris Kay# A value is considered to be falsy if it is:
670dfa3deaSChris Kay#
680dfa3deaSChris Kay#   - empty, or
690dfa3deaSChris Kay#   - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
700dfa3deaSChris Kay#
710dfa3deaSChris Kay# If the value is truthy then the value is returned as-is, otherwise no value
720dfa3deaSChris Kay# is returned.
730dfa3deaSChris Kay#
740dfa3deaSChris Kay# Example usage:
750dfa3deaSChris Kay#
760dfa3deaSChris Kay#     truthy := y
770dfa3deaSChris Kay#     truthy-bool := $(call bool,$(truthy)) # "y"
780dfa3deaSChris Kay#
790dfa3deaSChris Kay#     falsy := n
800dfa3deaSChris Kay#     falsy-bool := $(call bool,$(falsy)) # <empty>
810dfa3deaSChris Kay#
820dfa3deaSChris Kay
830dfa3deaSChris Kaybool = $(filter-out 0 n no f false,$(call lowercase,$(1)))
840dfa3deaSChris Kay
850dfa3deaSChris Kay#
860dfa3deaSChris Kay# Determine the "truthiness" of a value, returning 0 or 1.
870dfa3deaSChris Kay#
880dfa3deaSChris Kay# Parameters:
890dfa3deaSChris Kay#
900dfa3deaSChris Kay#   - $(1): The value to determine the truthiness of.
910dfa3deaSChris Kay#
920dfa3deaSChris Kay# A value is considered to be falsy if it is:
930dfa3deaSChris Kay#
940dfa3deaSChris Kay#   - empty, or
950dfa3deaSChris Kay#   - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
960dfa3deaSChris Kay#
970dfa3deaSChris Kay# If the value is truthy then the value is returned as-is, otherwise no value
980dfa3deaSChris Kay# is returned.
990dfa3deaSChris Kay#
1000dfa3deaSChris Kay# Example usage:
1010dfa3deaSChris Kay#
1020dfa3deaSChris Kay#     truthy := y
1030dfa3deaSChris Kay#     truthy-bool := $(call bool,$(truthy)) # "1"
1040dfa3deaSChris Kay#
1050dfa3deaSChris Kay#     falsy := n
1060dfa3deaSChris Kay#     falsy-bool := $(call bool,$(falsy)) # "0"
1070dfa3deaSChris Kay#
1080dfa3deaSChris Kay
1090dfa3deaSChris Kaybool-01 = $(if $(call bool,$(1)),1,0)
110d2867397SChris Kay
111d2867397SChris Kay#
112d2867397SChris Kay# Determine whether a variable is defined or not.
113d2867397SChris Kay#
114d2867397SChris Kay# Parameters:
115d2867397SChris Kay#
116d2867397SChris Kay#   - $(1): The variable to check.
117d2867397SChris Kay#
118d2867397SChris Kay# Example usage:
119d2867397SChris Kay#
120d2867397SChris Kay#     xyz-defined := $(call defined,xyz) # <empty>
121d2867397SChris Kay#
122d2867397SChris Kay#     xyz :=
123d2867397SChris Kay#     xyz-defined := $(call defined,xyz) # <non-empty>
124d2867397SChris Kay#
125d2867397SChris Kay#     xyz := hello
126d2867397SChris Kay#     xyz-defined := $(call defined,xyz) # <non-empty>
127d2867397SChris Kay#
128d2867397SChris Kay
129d2867397SChris Kaydefined = $(call bool,$(filter-out undefined,$(origin $(1))))
130c3273703SChris Kay
131c3273703SChris Kay#
132*0fcee05fSHarrison Mutai# Extract include directories from compiler flags and convert them to absolute
133*0fcee05fSHarrison Mutai# paths.
134*0fcee05fSHarrison Mutai#
135*0fcee05fSHarrison Mutai# Parameters:
136*0fcee05fSHarrison Mutai#
137*0fcee05fSHarrison Mutai#   - $(1): A list of C compiler flags.
138*0fcee05fSHarrison Mutai#
139*0fcee05fSHarrison Mutai# Example:
140*0fcee05fSHarrison Mutai#
141*0fcee05fSHarrison Mutai#     includes := $(call include-dirs, -nostdlib -Iinclude-dir) # /absolute/path/to/include-dir
142*0fcee05fSHarrison Mutai#
143*0fcee05fSHarrison Mutai
144*0fcee05fSHarrison Mutaiinclude-dirs-pattern := $(call escape-shell,-I\s*("[^"]*"|'[^']*'|\S+))
145*0fcee05fSHarrison Mutaiinclude-dirs = $(shell \
146*0fcee05fSHarrison Mutai	printf '%s' $(call escape-shell,$1) | \
147*0fcee05fSHarrison Mutai	perl -nle 'print $$1 while /'$(include-dirs-pattern)'/g' | \
148*0fcee05fSHarrison Mutai	xargs realpath \
149*0fcee05fSHarrison Mutai)
150*0fcee05fSHarrison Mutai
151*0fcee05fSHarrison Mutai#
152c3273703SChris Kay# Determine the path to a program.
153c3273703SChris Kay#
154c3273703SChris Kay# Parameters:
155c3273703SChris Kay#
156c3273703SChris Kay#   - $(1): The program to search for.
157c3273703SChris Kay#
158c3273703SChris Kay# Example usage:
159c3273703SChris Kay#
160c3273703SChris Kay#     path-to-gcc := $(call which,gcc) # "/usr/bin/gcc"
161c3273703SChris Kay#
162c3273703SChris Kay
163c3273703SChris Kaywhich = $(shell command -v $(call escape-shell,$(1)) 2>/dev/null)
164