utils.mk 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ################################################################################
  2. #
  3. # This file contains various utility macros and variables used about
  4. # everywhere in make constructs.
  5. #
  6. ################################################################################
  7. # Strip quotes and then whitespaces
  8. qstrip = $(strip $(subst ",,$(1)))
  9. #"))
  10. # Variables for use in Make constructs
  11. comma := ,
  12. empty :=
  13. space := $(empty) $(empty)
  14. tab := $(empty) $(empty)
  15. escape := $(shell printf '\x1b')
  16. # make 4.3:
  17. # https://lwn.net/Articles/810071/
  18. # Number signs (#) appearing inside a macro reference or function invocation
  19. # no longer introduce comments and should not be escaped with backslashes:
  20. # thus a call such as:
  21. # foo := $(shell echo '#')
  22. # is legal. Previously the number sign needed to be escaped, for example:
  23. # foo := $(shell echo '\#')
  24. # Now this latter will resolve to "\#". If you want to write makefiles
  25. # portable to both versions, assign the number sign to a variable:
  26. # H := \#
  27. # foo := $(shell echo '$H')
  28. SHARP_SIGN := \#
  29. # Case conversion macros. This is inspired by the 'up' macro from gmsl
  30. # (http://gmsl.sf.net). It is optimised very heavily because these macros
  31. # are used a lot. It is about 5 times faster than forking a shell and tr.
  32. #
  33. # The caseconvert-helper creates a definition of the case conversion macro.
  34. # After expansion by the outer $(eval ), the UPPERCASE macro is defined as:
  35. # $(strip $(eval __tmp := $(1)) $(eval __tmp := $(subst a,A,$(__tmp))) ... )
  36. # In other words, every letter is substituted one by one.
  37. #
  38. # The caseconvert-helper allows us to create this definition out of the
  39. # [FROM] and [TO] lists, so we don't need to write down every substitution
  40. # manually. The uses of $ and $$ quoting are chosen in order to do as
  41. # much expansion as possible up-front.
  42. #
  43. # Note that it would be possible to conceive a slightly more optimal
  44. # implementation that avoids the use of __tmp, but that would be even
  45. # more unreadable and is not worth the effort.
  46. [FROM] := a b c d e f g h i j k l m n o p q r s t u v w x y z - .
  47. [TO] := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _ _
  48. define caseconvert-helper
  49. $(1) = $$(strip \
  50. $$(eval __tmp := $$(1))\
  51. $(foreach c, $(2),\
  52. $$(eval __tmp := $$(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)),$$(__tmp))))\
  53. $$(__tmp))
  54. endef
  55. $(eval $(call caseconvert-helper,UPPERCASE,$(join $(addsuffix :,$([FROM])),$([TO]))))
  56. $(eval $(call caseconvert-helper,LOWERCASE,$(join $(addsuffix :,$([TO])),$([FROM]))))
  57. # Reverse the orders of words in a list. Again, inspired by the gmsl
  58. # 'reverse' macro.
  59. reverse = $(if $(1),$(call reverse,$(wordlist 2,$(words $(1)),$(1))) $(firstword $(1)))
  60. # Sanitize macro cleans up generic strings so it can be used as a filename
  61. # and in rules. Particularly useful for VCS version strings, that can contain
  62. # slashes, colons (OK in filenames but not in rules), and spaces.
  63. sanitize = $(subst $(space),_,$(subst :,_,$(subst /,_,$(strip $(1)))))
  64. # MESSAGE Macro -- display a message in bold type
  65. MESSAGE = echo "$(TERM_BOLD)>>> $($(PKG)_NAME) $($(PKG)_VERSION) $(call qstrip,$(1))$(TERM_RESET)"
  66. TERM_BOLD := $(shell tput smso 2>/dev/null)
  67. TERM_RESET := $(shell tput rmso 2>/dev/null)
  68. # Utility functions for 'find'
  69. # findfileclauses(filelist) => -name 'X' -o -name 'Y'
  70. findfileclauses = $(call notfirstword,$(patsubst %,-o -name '%',$(1)))
  71. # finddirclauses(base, dirlist) => -path 'base/dirX' -o -path 'base/dirY'
  72. finddirclauses = $(call notfirstword,$(patsubst %,-o -path '$(1)/%',$(2)))
  73. # Miscellaneous utility functions
  74. # notfirstword(wordlist): returns all but the first word in wordlist
  75. notfirstword = $(wordlist 2,$(words $(1)),$(1))
  76. # build a comma-separated list of items, from a space-separated
  77. # list of items: a b c d --> a, b, c, d
  78. make-comma-list = $(subst $(space),$(comma)$(space),$(strip $(1)))
  79. # build a comma-separated list of double-quoted items, from a space-separated
  80. # list of unquoted items: a b c d --> "a", "b", "c", "d"
  81. make-dq-comma-list = $(call make-comma-list,$(patsubst %,"%",$(strip $(1))))
  82. # build a comma-separated list of single-quoted items, from a space-separated
  83. # list of unquoted items: a b c d --> 'a', 'b', 'c', 'd'
  84. make-sq-comma-list = $(call make-comma-list,$(patsubst %,'%',$(strip $(1))))
  85. # Needed for the foreach loops to loop over the list of hooks, so that
  86. # each hook call is properly separated by a newline.
  87. define sep
  88. endef
  89. PERCENT = %
  90. QUOTE = '
  91. # ' # Meh... syntax-highlighting
  92. # This macro properly escapes a command string, then prints it with printf:
  93. #
  94. # - first, backslash '\' are self-escaped, so that they do not escape
  95. # the following char and so that printf properly outputs a backslash;
  96. #
  97. # - next, single quotes are escaped by closing an existing one, adding
  98. # an escaped one, and re-openning a new one (see below for the reason);
  99. #
  100. # - then '%' signs are self-escaped so that the printf does not interpret
  101. # them as a format specifier, in case the variable contains an actual
  102. # printf with a format;
  103. #
  104. # - finally, $(sep) is replaced with the literal '\n' so that make does
  105. # not break on the so-expanded variable, but so that the printf does
  106. # correctly output an LF.
  107. #
  108. # Note: this must be escaped in this order to avoid over-escaping the
  109. # previously escaped elements.
  110. #
  111. # Once everything has been escaped, it is passed between single quotes
  112. # (that's why the single-quotes are escaped they way they are, above,
  113. # and why the dollar sign is not escaped) to printf(1). A trailing
  114. # newline is appended, too.
  115. #
  116. # Note: leading or trailing spaces are *not* stripped.
  117. #
  118. define PRINTF
  119. printf '$(subst $(sep),\n,\
  120. $(subst $(PERCENT),$(PERCENT)$(PERCENT),\
  121. $(subst $(QUOTE),$(QUOTE)\$(QUOTE)$(QUOTE),\
  122. $(subst \,\\,$(1)))))\n'
  123. endef