utils.mk 4.5 KB

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