utilities.mk 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. # Upper-case a string value.
  19. #
  20. # Parameters:
  21. #
  22. # - $(1): The string to upper-case.
  23. #
  24. # Example usage:
  25. #
  26. # $(call uppercase,HeLlO wOrLd) # "HELLO WORLD"
  27. #
  28. uppercase = $(shell echo $(call escape-shell,$(1)) | tr '[:lower:]' '[:upper:]')
  29. #
  30. # Lower-case a string value.
  31. #
  32. # Parameters:
  33. #
  34. # - $(1): The string to lower-case.
  35. #
  36. # Example usage:
  37. #
  38. # $(call lowercase,HeLlO wOrLd) # "hello world"
  39. #
  40. lowercase = $(shell echo $(call escape-shell,$(1)) | tr '[:upper:]' '[:lower:]')
  41. #
  42. # Determine the "truthiness" of a value.
  43. #
  44. # Parameters:
  45. #
  46. # - $(1): The value to determine the truthiness of.
  47. #
  48. # A value is considered to be falsy if it is:
  49. #
  50. # - empty, or
  51. # - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
  52. #
  53. # If the value is truthy then the value is returned as-is, otherwise no value
  54. # is returned.
  55. #
  56. # Example usage:
  57. #
  58. # truthy := y
  59. # truthy-bool := $(call bool,$(truthy)) # "y"
  60. #
  61. # falsy := n
  62. # falsy-bool := $(call bool,$(falsy)) # <empty>
  63. #
  64. bool = $(filter-out 0 n no f false,$(call lowercase,$(1)))
  65. #
  66. # Determine the "truthiness" of a value, returning 0 or 1.
  67. #
  68. # Parameters:
  69. #
  70. # - $(1): The value to determine the truthiness of.
  71. #
  72. # A value is considered to be falsy if it is:
  73. #
  74. # - empty, or
  75. # - equal to "0", "N", "NO", "F" or "FALSE" after upper-casing.
  76. #
  77. # If the value is truthy then the value is returned as-is, otherwise no value
  78. # is returned.
  79. #
  80. # Example usage:
  81. #
  82. # truthy := y
  83. # truthy-bool := $(call bool,$(truthy)) # "1"
  84. #
  85. # falsy := n
  86. # falsy-bool := $(call bool,$(falsy)) # "0"
  87. #
  88. bool-01 = $(if $(call bool,$(1)),1,0)