utilities.mk 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #
  2. # Copyright (c) 2024, Arm Limited and Contributors. All rights reserved.
  3. #
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. #
  6. space :=
  7. space := $(space) $(space)
  8. comma := ,
  9. null := �
  10. compat-path = $(subst $(space),$(null),$(1))
  11. decompat-path = $(subst $(null), ,$(1))
  12. absolute-path = $(call decompat-path,$(abspath $(call compat-path,$(1))))
  13. real-path = $(call decompat-path,$(realpath $(call compat-path,$(1))))
  14. file-name = $(call decompat-path,$(notdir $(call compat-path,$(1))))
  15. directory-name = $(call decompat-path,$(dir $(call compat-path,$(1))))
  16. escape-shell = '$(subst ','\'',$(1))'
  17. #
  18. # The grouped-target symbol. Grouped targets are not supported on versions of
  19. # GNU Make <= 4.2, which was most recently packaged with Ubuntu 20.04.
  20. #
  21. & := $(if $(filter grouped-target,$(.FEATURES)),&)
  22. #
  23. # Upper-case a string value.
  24. #
  25. # Parameters:
  26. #
  27. # - $(1): The string to upper-case.
  28. #
  29. # Example usage:
  30. #
  31. # $(call uppercase,HeLlO wOrLd) # "HELLO WORLD"
  32. #
  33. uppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
  34. #
  35. # Lower-case a string value.
  36. #
  37. # Parameters:
  38. #
  39. # - $(1): The string to lower-case.
  40. #
  41. # Example usage:
  42. #
  43. # $(call lowercase,HeLlO wOrLd) # "hello world"
  44. #
  45. lowercase = $(shell echo $(call escape-shell,$(1)) | tr '[:upper:]' '[:lower:]')
  46. #
  47. # Determine the "truthiness" of a value.
  48. #
  49. # Parameters:
  50. #
  51. # - $(1): The value to determine the truthiness of.
  52. #
  53. # A value is considered to be falsy if it is:
  54. #
  55. # - empty, or
  56. # - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
  57. #
  58. # If the value is truthy then the value is returned as-is, otherwise no value
  59. # is returned.
  60. #
  61. # Example usage:
  62. #
  63. # truthy := y
  64. # truthy-bool := $(call bool,$(truthy)) # "y"
  65. #
  66. # falsy := n
  67. # falsy-bool := $(call bool,$(falsy)) # <empty>
  68. #
  69. bool = $(filter-out 0 n no f false,$(call lowercase,$(1)))
  70. #
  71. # Determine the "truthiness" of a value, returning 0 or 1.
  72. #
  73. # Parameters:
  74. #
  75. # - $(1): The value to determine the truthiness of.
  76. #
  77. # A value is considered to be falsy if it is:
  78. #
  79. # - empty, or
  80. # - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
  81. #
  82. # If the value is truthy then the value is returned as-is, otherwise no value
  83. # is returned.
  84. #
  85. # Example usage:
  86. #
  87. # truthy := y
  88. # truthy-bool := $(call bool,$(truthy)) # "1"
  89. #
  90. # falsy := n
  91. # falsy-bool := $(call bool,$(falsy)) # "0"
  92. #
  93. bool-01 = $(if $(call bool,$(1)),1,0)
  94. #
  95. # Determine whether a variable is defined or not.
  96. #
  97. # Parameters:
  98. #
  99. # - $(1): The variable to check.
  100. #
  101. # Example usage:
  102. #
  103. # xyz-defined := $(call defined,xyz) # <empty>
  104. #
  105. # xyz :=
  106. # xyz-defined := $(call defined,xyz) # <non-empty>
  107. #
  108. # xyz := hello
  109. # xyz-defined := $(call defined,xyz) # <non-empty>
  110. #
  111. defined = $(call bool,$(filter-out undefined,$(origin $(1))))