xref: /rk3399_ARM-atf/make_helpers/utilities.mk (revision ec932236c55b3d90cb250d8a4c53d1dee8e57d54)
1#
2# Copyright (c) 2024-2025, Arm Limited and Contributors. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7space :=
8space := $(space) $(space)
9comma := ,
10
11null := �
12
13compat-path = $(subst $(space),$(null),$(1))
14decompat-path = $(subst $(null), ,$(1))
15
16absolute-path = $(call decompat-path,$(abspath $(call compat-path,$(1))))
17real-path = $(call decompat-path,$(realpath $(call compat-path,$(1))))
18
19file-name = $(call decompat-path,$(notdir $(call compat-path,$(1))))
20directory-name = $(call decompat-path,$(dir $(call compat-path,$(1))))
21
22escape-shell = '$(subst ','\'',$(1))'
23
24#
25# The grouped-target symbol. Grouped targets are not supported on versions of
26# GNU Make <= 4.2, which was most recently packaged with Ubuntu 20.04.
27#
28
29& := $(if $(filter grouped-target,$(.FEATURES)),&)
30
31#
32# Upper-case a string value.
33#
34# Parameters:
35#
36#   - $(1): The string to upper-case.
37#
38# Example usage:
39#
40#     $(call uppercase,HeLlO wOrLd) # "HELLO WORLD"
41#
42
43uppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
44
45#
46# Lower-case a string value.
47#
48# Parameters:
49#
50#   - $(1): The string to lower-case.
51#
52# Example usage:
53#
54#     $(call lowercase,HeLlO wOrLd) # "hello world"
55#
56
57lowercase = $(shell echo $(call escape-shell,$(1)) | tr '[:upper:]' '[:lower:]')
58
59#
60# Determine the "truthiness" of a value.
61#
62# Parameters:
63#
64#   - $(1): The value to determine the truthiness of.
65#
66# A value is considered to be falsy if it is:
67#
68#   - empty, or
69#   - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
70#
71# If the value is truthy then the value is returned as-is, otherwise no value
72# is returned.
73#
74# Example usage:
75#
76#     truthy := y
77#     truthy-bool := $(call bool,$(truthy)) # "y"
78#
79#     falsy := n
80#     falsy-bool := $(call bool,$(falsy)) # <empty>
81#
82
83bool = $(filter-out 0 n no f false,$(call lowercase,$(1)))
84
85#
86# Determine the "truthiness" of a value, returning 0 or 1.
87#
88# Parameters:
89#
90#   - $(1): The value to determine the truthiness of.
91#
92# A value is considered to be falsy if it is:
93#
94#   - empty, or
95#   - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
96#
97# If the value is truthy then the value is returned as-is, otherwise no value
98# is returned.
99#
100# Example usage:
101#
102#     truthy := y
103#     truthy-bool := $(call bool,$(truthy)) # "1"
104#
105#     falsy := n
106#     falsy-bool := $(call bool,$(falsy)) # "0"
107#
108
109bool-01 = $(if $(call bool,$(1)),1,0)
110
111#
112# Determine whether a variable is defined or not.
113#
114# Parameters:
115#
116#   - $(1): The variable to check.
117#
118# Example usage:
119#
120#     xyz-defined := $(call defined,xyz) # <empty>
121#
122#     xyz :=
123#     xyz-defined := $(call defined,xyz) # <non-empty>
124#
125#     xyz := hello
126#     xyz-defined := $(call defined,xyz) # <non-empty>
127#
128
129defined = $(call bool,$(filter-out undefined,$(origin $(1))))
130
131#
132# Extract include directories from compiler flags and convert them to absolute
133# paths.
134#
135# Parameters:
136#
137#   - $(1): A list of C compiler flags.
138#
139# Example:
140#
141#     includes := $(call include-dirs, -nostdlib -Iinclude-dir) # /absolute/path/to/include-dir
142#
143
144include-dirs-pattern := $(call escape-shell,-I\s*("[^"]*"|'[^']*'|\S+))
145include-dirs = $(shell \
146	printf '%s' $(call escape-shell,$1) | \
147	perl -nle 'print $$1 while /'$(include-dirs-pattern)'/g' | \
148	xargs realpath \
149)
150
151#
152# Determine the path to a program.
153#
154# Parameters:
155#
156#   - $(1): The program to search for.
157#
158# Example usage:
159#
160#     path-to-gcc := $(call which,gcc) # "/usr/bin/gcc"
161#
162
163which = $(shell command -v $(call escape-shell,$(1)) 2>/dev/null)
164