utils.mk 5.5 KB

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