blob: fcccd24060b1a5f721ae3d242440ea1439b4845b [file] [log] [blame]
Chris Kay2c09bf62024-04-09 16:30:52 +00001#
2# Copyright (c) 2024, 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))'
Chris Kayd3972772024-05-29 22:09:26 +000023
24#
Chris Kay834cfe12024-08-06 15:32:57 +000025# 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#
Chris Kayd3972772024-05-29 22:09:26 +000032# 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:]')
Chris Kay0fda3ab2024-05-29 22:16:31 +000058
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)
Chris Kaya152e752024-09-26 14:18:04 +0000110
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))))